Fix sonar issues in policy-api
[policy/api.git] / main / src / main / java / org / onap / policy / api / main / rest / provider / CommonModelProvider.java
1 /*-\r
2  * ============LICENSE_START=======================================================\r
3  * ONAP Policy API\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
10  *\r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  *\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
18  *\r
19  * SPDX-License-Identifier: Apache-2.0\r
20  * ============LICENSE_END=========================================================\r
21  */\r
22 \r
23 package org.onap.policy.api.main.rest.provider;\r
24 \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
38 \r
39 /**\r
40  * Super class for providers that use a model provider.\r
41  */\r
42 public class CommonModelProvider implements AutoCloseable {\r
43 \r
44     protected final PolicyModelsProvider modelsProvider;\r
45 \r
46     /**\r
47      * Constructs the object, populating {@link #modelsProvider}.\r
48      *\r
49      * @throws PfModelException if an error occurs\r
50      */\r
51     public CommonModelProvider() throws PfModelException {\r
52 \r
53         ApiParameterGroup parameterGroup = ParameterService.get("ApiGroup");\r
54         PolicyModelsProviderParameters providerParameters = parameterGroup.getDatabaseProviderParameters();\r
55         modelsProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(providerParameters);\r
56     }\r
57 \r
58     /**\r
59      * Closes the connection to database.\r
60      *\r
61      * @throws PfModelException the PfModel parsing exception\r
62      */\r
63     @Override\r
64     public void close() throws PfModelException {\r
65 \r
66         modelsProvider.close();\r
67     }\r
68 \r
69     /**\r
70      * Checks if service template contains any policy.\r
71      *\r
72      * @param serviceTemplate the service template to check against\r
73      *\r
74      * @return boolean whether service template contains any policy\r
75      */\r
76     protected boolean hasPolicy(ToscaServiceTemplate serviceTemplate) {\r
77 \r
78         return hasData(serviceTemplate.getToscaTopologyTemplate().getPolicies());\r
79     }\r
80 \r
81     /**\r
82      * Checks if service template contains any policy type.\r
83      *\r
84      * @param serviceTemplate the service template to check against\r
85      *\r
86      * @return boolean whether service template contains any policy type\r
87      */\r
88     protected boolean hasPolicyType(ToscaServiceTemplate serviceTemplate) {\r
89 \r
90         return hasData(serviceTemplate.getPolicyTypes());\r
91     }\r
92 \r
93     /**\r
94      * Checks if the first element of a list contains data.\r
95      *\r
96      * @param list list to be examined\r
97      * @return {@code true} if the list contains data, {@code false} otherwise\r
98      */\r
99     protected <T> boolean hasData(List<Map<String, T>> list) {\r
100 \r
101         return (list != null && !list.isEmpty() && !list.get(0).isEmpty());\r
102     }\r
103 \r
104     /**\r
105      * Validates that some text represents a number.\r
106      *\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
109      *        number\r
110      * @throws PfModelException if the text is not a valid number\r
111      */\r
112     protected void validNumber(String text, String errorMsg) throws PfModelException {\r
113         try {\r
114             Integer.parseInt(text);\r
115 \r
116         } catch (NumberFormatException exc) {\r
117             throw new PfModelException(Response.Status.BAD_REQUEST, errorMsg, exc);\r
118         }\r
119     }\r
120 \r
121     /**\r
122      * Constructs returned message for policy delete rule violation.\r
123      *\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
127      *\r
128      * @return the constructed message\r
129      */\r
130     protected String constructDeletePolicyViolationMessage(String policyId, String policyVersion,\r
131                     List<PdpGroup> pdpGroups) {\r
132 \r
133         List<String> pdpGroupNameVersionList = new ArrayList<>(pdpGroups.size());\r
134         for (PdpGroup pdpGroup : pdpGroups) {\r
135             pdpGroupNameVersionList.add(pdpGroup.getName() + ":" + pdpGroup.getVersion());\r
136         }\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
140     }\r
141 \r
142     /**\r
143      * Constructs returned message for policy type delete rule violation.\r
144      *\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
148      *\r
149      * @return the constructed message\r
150      */\r
151     protected String constructDeletePolicyTypeViolationMessage(String policyTypeId, String policyTypeVersion,\r
152                     List<ToscaPolicy> policies) {\r
153 \r
154         List<String> policyNameVersionList = new ArrayList<>(policies.size());\r
155         for (ToscaPolicy policy : policies) {\r
156             policyNameVersionList.add(policy.getName() + ":" + policy.getVersion());\r
157         }\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
161     }\r
162 }\r