2 * ============LICENSE_START=======================================================
\r
4 * ================================================================================
\r
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
\r
6 * ================================================================================
\r
7 * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * you may not use this file except in compliance with the License.
\r
9 * You may obtain a copy of the License at
\r
11 * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * Unless required by applicable law or agreed to in writing, software
\r
14 * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * See the License for the specific language governing permissions and
\r
17 * limitations under the License.
\r
19 * SPDX-License-Identifier: Apache-2.0
\r
20 * ============LICENSE_END=========================================================
\r
23 package org.onap.policy.api.main.rest.provider;
\r
25 import java.util.ArrayList;
\r
26 import java.util.List;
\r
27 import java.util.Map;
\r
28 import javax.ws.rs.core.Response;
\r
29 import org.onap.policy.api.main.parameters.ApiParameterGroup;
\r
30 import org.onap.policy.common.parameters.ParameterService;
\r
31 import org.onap.policy.models.base.PfModelException;
\r
32 import org.onap.policy.models.pdp.concepts.PdpGroup;
\r
33 import org.onap.policy.models.provider.PolicyModelsProvider;
\r
34 import org.onap.policy.models.provider.PolicyModelsProviderFactory;
\r
35 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
\r
36 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
\r
37 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
\r
40 * Super class for providers that use a model provider.
\r
42 public class CommonModelProvider implements AutoCloseable {
\r
44 protected final PolicyModelsProvider modelsProvider;
\r
47 * Constructs the object, populating {@link #modelsProvider}.
\r
49 * @throws PfModelException if an error occurs
\r
51 public CommonModelProvider() throws PfModelException {
\r
53 ApiParameterGroup parameterGroup = ParameterService.get("ApiGroup");
\r
54 PolicyModelsProviderParameters providerParameters = parameterGroup.getDatabaseProviderParameters();
\r
55 modelsProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(providerParameters);
\r
59 * Closes the connection to database.
\r
61 * @throws PfModelException the PfModel parsing exception
\r
64 public void close() throws PfModelException {
\r
66 modelsProvider.close();
\r
70 * Checks if service template contains any policy.
\r
72 * @param serviceTemplate the service template to check against
\r
74 * @return boolean whether service template contains any policy
\r
76 protected boolean hasPolicy(ToscaServiceTemplate serviceTemplate) {
\r
78 return hasData(serviceTemplate.getToscaTopologyTemplate().getPolicies());
\r
82 * Checks if service template contains any policy type.
\r
84 * @param serviceTemplate the service template to check against
\r
86 * @return boolean whether service template contains any policy type
\r
88 protected boolean hasPolicyType(ToscaServiceTemplate serviceTemplate) {
\r
90 return hasData(serviceTemplate.getPolicyTypes());
\r
94 * Checks if the first element of a list contains data.
\r
96 * @param list list to be examined
\r
97 * @return {@code true} if the list contains data, {@code false} otherwise
\r
99 protected <T> boolean hasData(List<Map<String, T>> list) {
\r
101 return (list != null && !list.isEmpty() && !list.get(0).isEmpty());
\r
105 * Validates that some text represents a number.
\r
107 * @param text text to be validated
\r
108 * @param errorMsg error message included in the exception, if the text is not a valid
\r
110 * @throws PfModelException if the text is not a valid number
\r
112 protected void validNumber(String text, String errorMsg) throws PfModelException {
\r
114 Integer.parseInt(text);
\r
116 } catch (NumberFormatException exc) {
\r
117 throw new PfModelException(Response.Status.BAD_REQUEST, errorMsg, exc);
\r
122 * Constructs returned message for policy delete rule violation.
\r
124 * @param policyId the ID of policy
\r
125 * @param policyVersion the version of policy
\r
126 * @param pdpGroups the list of pdp groups
\r
128 * @return the constructed message
\r
130 protected String constructDeletePolicyViolationMessage(String policyId, String policyVersion,
\r
131 List<PdpGroup> pdpGroups) {
\r
133 List<String> pdpGroupNameVersionList = new ArrayList<>(pdpGroups.size());
\r
134 for (PdpGroup pdpGroup : pdpGroups) {
\r
135 pdpGroupNameVersionList.add(pdpGroup.getName() + ":" + pdpGroup.getVersion());
\r
137 String deployedPdpGroups = String.join(",", pdpGroupNameVersionList);
\r
138 return "policy with ID " + policyId + ":" + policyVersion
\r
139 + " cannot be deleted as it is deployed in pdp groups " + deployedPdpGroups;
\r
143 * Constructs returned message for policy type delete rule violation.
\r
145 * @param policyTypeId the ID of policy type
\r
146 * @param policyTypeVersion the version of policy type
\r
147 * @param policies the list of policies that parameterizes specified policy type
\r
149 * @return the constructed message
\r
151 protected String constructDeletePolicyTypeViolationMessage(String policyTypeId, String policyTypeVersion,
\r
152 List<ToscaPolicy> policies) {
\r
154 List<String> policyNameVersionList = new ArrayList<>(policies.size());
\r
155 for (ToscaPolicy policy : policies) {
\r
156 policyNameVersionList.add(policy.getName() + ":" + policy.getVersion());
\r
158 String parameterizedPolicies = String.join(",", policyNameVersionList);
\r
159 return "policy type with ID " + policyTypeId + ":" + policyTypeVersion
\r
160 + " cannot be deleted as it is parameterized by policies " + parameterizedPolicies;
\r