+/*-\r
+ * ============LICENSE_START=======================================================\r
+ * ONAP Policy API\r
+ * ================================================================================\r
+ * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.\r
+ * ================================================================================\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ *\r
+ * SPDX-License-Identifier: Apache-2.0\r
+ * ============LICENSE_END=========================================================\r
+ */\r
+\r
+package org.onap.policy.api.main.rest.provider;\r
+\r
+import java.util.ArrayList;\r
+import java.util.List;\r
+import java.util.Map;\r
+import javax.ws.rs.core.Response;\r
+import org.onap.policy.api.main.parameters.ApiParameterGroup;\r
+import org.onap.policy.common.parameters.ParameterService;\r
+import org.onap.policy.models.base.PfModelException;\r
+import org.onap.policy.models.pdp.concepts.PdpGroup;\r
+import org.onap.policy.models.provider.PolicyModelsProvider;\r
+import org.onap.policy.models.provider.PolicyModelsProviderFactory;\r
+import org.onap.policy.models.provider.PolicyModelsProviderParameters;\r
+import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;\r
+import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;\r
+\r
+/**\r
+ * Super class for providers that use a model provider.\r
+ */\r
+public class CommonModelProvider implements AutoCloseable {\r
+\r
+ protected final PolicyModelsProvider modelsProvider;\r
+\r
+ /**\r
+ * Constructs the object, populating {@link #modelsProvider}.\r
+ *\r
+ * @throws PfModelException if an error occurs\r
+ */\r
+ public CommonModelProvider() throws PfModelException {\r
+\r
+ ApiParameterGroup parameterGroup = ParameterService.get("ApiGroup");\r
+ PolicyModelsProviderParameters providerParameters = parameterGroup.getDatabaseProviderParameters();\r
+ modelsProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(providerParameters);\r
+ }\r
+\r
+ /**\r
+ * Closes the connection to database.\r
+ *\r
+ * @throws PfModelException the PfModel parsing exception\r
+ */\r
+ @Override\r
+ public void close() throws PfModelException {\r
+\r
+ modelsProvider.close();\r
+ }\r
+\r
+ /**\r
+ * Checks if service template contains any policy.\r
+ *\r
+ * @param serviceTemplate the service template to check against\r
+ *\r
+ * @return boolean whether service template contains any policy\r
+ */\r
+ protected boolean hasPolicy(ToscaServiceTemplate serviceTemplate) {\r
+\r
+ return hasData(serviceTemplate.getToscaTopologyTemplate().getPolicies());\r
+ }\r
+\r
+ /**\r
+ * Checks if service template contains any policy type.\r
+ *\r
+ * @param serviceTemplate the service template to check against\r
+ *\r
+ * @return boolean whether service template contains any policy type\r
+ */\r
+ protected boolean hasPolicyType(ToscaServiceTemplate serviceTemplate) {\r
+\r
+ return hasData(serviceTemplate.getPolicyTypes());\r
+ }\r
+\r
+ /**\r
+ * Checks if the first element of a list contains data.\r
+ *\r
+ * @param list list to be examined\r
+ * @return {@code true} if the list contains data, {@code false} otherwise\r
+ */\r
+ protected <T> boolean hasData(List<Map<String, T>> list) {\r
+\r
+ return (list != null && !list.isEmpty() && !list.get(0).isEmpty());\r
+ }\r
+\r
+ /**\r
+ * Validates that some text represents a number.\r
+ *\r
+ * @param text text to be validated\r
+ * @param errorMsg error message included in the exception, if the text is not a valid\r
+ * number\r
+ * @throws PfModelException if the text is not a valid number\r
+ */\r
+ protected void validNumber(String text, String errorMsg) throws PfModelException {\r
+ try {\r
+ Integer.parseInt(text);\r
+\r
+ } catch (NumberFormatException exc) {\r
+ throw new PfModelException(Response.Status.BAD_REQUEST, errorMsg, exc);\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Constructs returned message for policy delete rule violation.\r
+ *\r
+ * @param policyId the ID of policy\r
+ * @param policyVersion the version of policy\r
+ * @param pdpGroups the list of pdp groups\r
+ *\r
+ * @return the constructed message\r
+ */\r
+ protected String constructDeletePolicyViolationMessage(String policyId, String policyVersion,\r
+ List<PdpGroup> pdpGroups) {\r
+\r
+ List<String> pdpGroupNameVersionList = new ArrayList<>(pdpGroups.size());\r
+ for (PdpGroup pdpGroup : pdpGroups) {\r
+ pdpGroupNameVersionList.add(pdpGroup.getName() + ":" + pdpGroup.getVersion());\r
+ }\r
+ String deployedPdpGroups = String.join(",", pdpGroupNameVersionList);\r
+ return "policy with ID " + policyId + ":" + policyVersion\r
+ + " cannot be deleted as it is deployed in pdp groups " + deployedPdpGroups;\r
+ }\r
+\r
+ /**\r
+ * Constructs returned message for policy type delete rule violation.\r
+ *\r
+ * @param policyTypeId the ID of policy type\r
+ * @param policyTypeVersion the version of policy type\r
+ * @param policies the list of policies that parameterizes specified policy type\r
+ *\r
+ * @return the constructed message\r
+ */\r
+ protected String constructDeletePolicyTypeViolationMessage(String policyTypeId, String policyTypeVersion,\r
+ List<ToscaPolicy> policies) {\r
+\r
+ List<String> policyNameVersionList = new ArrayList<>(policies.size());\r
+ for (ToscaPolicy policy : policies) {\r
+ policyNameVersionList.add(policy.getName() + ":" + policy.getVersion());\r
+ }\r
+ String parameterizedPolicies = String.join(",", policyNameVersionList);\r
+ return "policy type with ID " + policyTypeId + ":" + policyTypeVersion\r
+ + " cannot be deleted as it is parameterized by policies " + parameterizedPolicies;\r
+ }\r
+}\r