-/*-
- * ============LICENSE_START=======================================================
- * ONAP Policy API
- * ================================================================================
- * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
- * ================================================================================
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- * SPDX-License-Identifier: Apache-2.0
- * ============LICENSE_END=========================================================
- */
-
-package org.onap.policy.api.main.rest.provider;
-
-import org.onap.policy.models.tosca.ToscaPolicy;
-import org.onap.policy.models.tosca.ToscaPolicyList;
-
-/**
- * Class to provide all kinds of policy operations.
- */
-public class PolicyProvider {
-
- private static final String POST_OK = "Successfully created";
- private static final String DELETE_OK = "Successfully deleted";
-
- /**
- * Retrieves a list of policies matching specified ID and version of both policy type and policy.
- *
- * @param policyTypeId the ID of policy type
- * @param policyTypeVersion the version of policy type
- * @param policyId the ID of policy
- * @param policyVersion the version of policy
- *
- * @return the ToscaPolicyList object containing a list of policies matching specified fields
- */
- public ToscaPolicyList fetchPolicies(String policyTypeId, String policyTypeVersion,
- String policyId, String policyVersion) {
- // placeholder
- return new ToscaPolicyList();
- }
-
- /**
- * Creates a new policy for a policy type ID and version.
- *
- * @param policyTypeId the ID of policy type
- * @param policyTypeVersion the version of policy type
- * @param body the entity body of policy
- *
- * @return a string message indicating the operation results
- */
- public String createPolicy(String policyTypeId, String policyTypeVersion, ToscaPolicy body) {
- // placeholder
- return POST_OK;
- }
-
- /**
- * Deletes the policies matching specified ID and version of both policy type and policy.
- *
- * @param policyTypeId the ID of policy type
- * @param policyTypeVersion the version of policy type
- * @param policyId the ID of policy
- * @param policyVersion the version of policy
- *
- * @return a string message indicating the operation results
- */
- public String deletePolicies(String policyTypeId, String policyTypeVersion,
- String policyId, String policyVersion) {
- // placeholder
- return DELETE_OK;
- }
-}
+/*-\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.HashMap;\r
+import java.util.List;\r
+import java.util.Map;\r
+import javax.ws.rs.core.Response;\r
+import org.apache.commons.lang3.tuple.Pair;\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.pdp.concepts.PdpGroupFilter;\r
+import org.onap.policy.models.pdp.concepts.PdpSubGroup;\r
+import org.onap.policy.models.pdp.enums.PdpState;\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.ToscaPolicyFilter;\r
+import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier;\r
+import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;\r
+import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;\r
+\r
+/**\r
+ * Class to provide all kinds of policy operations.\r
+ *\r
+ * @author Chenfei Gao (cgao@research.att.com)\r
+ */\r
+public class PolicyProvider implements AutoCloseable {\r
+\r
+ private PolicyModelsProvider modelsProvider;\r
+\r
+ /**\r
+ * Default constructor.\r
+ */\r
+ public PolicyProvider() throws PfModelException {\r
+\r
+ ApiParameterGroup parameterGroup = ParameterService.get("ApiGroup");\r
+ PolicyModelsProviderParameters providerParameters = parameterGroup.getDatabaseProviderParameters();\r
+ modelsProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(providerParameters);\r
+ }\r
+\r
+ /**\r
+ * Retrieves a list of policies matching specified ID and version of both policy type and policy.\r
+ *\r
+ * @param policyTypeId the ID of policy type\r
+ * @param policyTypeVersion the version of policy type\r
+ * @param policyId the ID of policy\r
+ * @param policyVersion the version of policy\r
+ *\r
+ * @return the ToscaServiceTemplate object\r
+ *\r
+ * @throws PfModelException the PfModel parsing exception\r
+ */\r
+ public ToscaServiceTemplate fetchPolicies(String policyTypeId, String policyTypeVersion,\r
+ String policyId, String policyVersion) throws PfModelException {\r
+\r
+ ToscaPolicyFilter policyFilter = ToscaPolicyFilter.builder()\r
+ .name(policyId).version(policyVersion)\r
+ .type(policyTypeId).typeVersion(policyTypeVersion).build();\r
+ ToscaServiceTemplate serviceTemplate = modelsProvider.getFilteredPolicies(policyFilter);\r
+\r
+ if (!hasPolicy(serviceTemplate)) {\r
+ throw new PfModelException(Response.Status.NOT_FOUND,\r
+ constructResourceNotFoundMessage(policyTypeId, policyTypeVersion, policyId, policyVersion));\r
+ }\r
+\r
+ return serviceTemplate;\r
+ }\r
+\r
+ /**\r
+ * Retrieves a list of policies with the latest versions that match specified policy type id and version.\r
+ *\r
+ * @param policyTypeId the ID of policy type\r
+ * @param policyTypeVersion the version of policy type\r
+ * @param policyId the ID of the policy\r
+ *\r
+ * @return the ToscaServiceTemplate object\r
+ *\r
+ * @throws PfModelException the PfModel parsing exception\r
+ */\r
+ public ToscaServiceTemplate fetchLatestPolicies(String policyTypeId, String policyTypeVersion,\r
+ String policyId) throws PfModelException {\r
+\r
+ ToscaPolicyFilter policyFilter = ToscaPolicyFilter.builder()\r
+ .name(policyId).version(ToscaPolicyFilter.LATEST_VERSION)\r
+ .type(policyTypeId).typeVersion(policyTypeVersion).build();\r
+ ToscaServiceTemplate serviceTemplate = modelsProvider.getFilteredPolicies(policyFilter);\r
+\r
+ if (!hasPolicy(serviceTemplate)) {\r
+ throw new PfModelException(Response.Status.NOT_FOUND,\r
+ constructResourceNotFoundMessage(policyTypeId, policyTypeVersion, policyId, null));\r
+ }\r
+\r
+ return serviceTemplate;\r
+ }\r
+\r
+ /**\r
+ * Retrieves a list of deployed policies in each pdp group.\r
+ *\r
+ * @param policyTypeId the ID of policy type\r
+ * @param policyTypeVersion the version of policy type\r
+ * @param policyId the ID of the policy\r
+ *\r
+ * @return a list of deployed policies in each pdp group\r
+ *\r
+ * @throws PfModelException the PfModel parsing exception\r
+ */\r
+ public Map<Pair<String, String>, List<ToscaPolicy>> fetchDeployedPolicies(\r
+ String policyTypeId, String policyTypeVersion, String policyId) throws PfModelException {\r
+\r
+ List<ToscaPolicyTypeIdentifier> policyTypes = new ArrayList<>();\r
+ policyTypes.add(new ToscaPolicyTypeIdentifier(policyTypeId, policyTypeVersion));\r
+ PdpGroupFilter pdpGroupFilter = PdpGroupFilter.builder().policyTypeList(policyTypes)\r
+ .groupState(PdpState.ACTIVE).pdpState(PdpState.ACTIVE).build();\r
+ List<PdpGroup> pdpGroups = modelsProvider.getFilteredPdpGroups(pdpGroupFilter);\r
+\r
+ if (pdpGroups.isEmpty()) {\r
+ throw new PfModelException(Response.Status.NOT_FOUND,\r
+ constructDeploymentNotFoundMessage(policyTypeId, policyTypeVersion, policyId));\r
+ }\r
+\r
+ Map<Pair<String, String>, List<ToscaPolicy>> deployedPolicyMap =\r
+ constructDeployedPolicyMap(pdpGroups, policyId);\r
+ if (deployedPolicyMap.isEmpty()) {\r
+ throw new PfModelException(Response.Status.NOT_FOUND,\r
+ constructDeploymentNotFoundMessage(policyTypeId, policyTypeVersion, policyId));\r
+ }\r
+\r
+ return deployedPolicyMap;\r
+ }\r
+\r
+ /**\r
+ * Creates a new policy for a policy type ID and version.\r
+ *\r
+ * @param policyTypeId the ID of policy type\r
+ * @param policyTypeVersion the version of policy type\r
+ * @param body the entity body of policy\r
+ *\r
+ * @return the ToscaServiceTemplate object\r
+ *\r
+ * @throws PfModelException the PfModel parsing exception\r
+ */\r
+ public ToscaServiceTemplate createPolicy(String policyTypeId, String policyTypeVersion,\r
+ ToscaServiceTemplate body) throws PfModelException {\r
+\r
+ validatePolicyTypeExist(policyTypeId, policyTypeVersion);\r
+ validatePolicyTypeMatch(policyTypeId, policyTypeVersion, body);\r
+\r
+ return modelsProvider.createPolicies(body);\r
+ }\r
+\r
+ /**\r
+ * Deletes the policy matching specified ID and version of both policy type and policy.\r
+ *\r
+ * @param policyTypeId the ID of policy type\r
+ * @param policyTypeVersion the version of policy type\r
+ * @param policyId the ID of policy\r
+ * @param policyVersion the version of policy\r
+ *\r
+ * @return the ToscaServiceTemplate object\r
+ *\r
+ * @throws PfModelException the PfModel parsing exception\r
+ */\r
+ public ToscaServiceTemplate deletePolicy(String policyTypeId, String policyTypeVersion,\r
+ String policyId, String policyVersion) throws PfModelException {\r
+\r
+ validateDeleteEligibility(policyTypeId, policyTypeVersion, policyId, policyVersion);\r
+\r
+ ToscaServiceTemplate serviceTemplate = modelsProvider.deletePolicy(policyId, policyVersion);\r
+\r
+ if (!hasPolicy(serviceTemplate)) {\r
+ throw new PfModelException(Response.Status.NOT_FOUND,\r
+ constructResourceNotFoundMessage(policyTypeId, policyTypeVersion, policyId, policyVersion));\r
+ }\r
+\r
+ return serviceTemplate;\r
+ }\r
+\r
+ /**\r
+ * Validates whether policy type exists.\r
+ *\r
+ * @param policyTypeId the ID of policy type\r
+ * @param policyTypeVersion the version of policy type\r
+ *\r
+ * @throws PfModelException the PfModel parsing exception\r
+ */\r
+ private void validatePolicyTypeExist(String policyTypeId, String policyTypeVersion) throws PfModelException {\r
+\r
+ ToscaServiceTemplate serviceTemplate = modelsProvider.getPolicyTypes(policyTypeId, policyTypeVersion);\r
+ if (!hasPolicyType(serviceTemplate)) {\r
+ throw new PfModelException(Response.Status.NOT_FOUND,\r
+ "policy type with ID " + policyTypeId + ":" + policyTypeVersion + " does not exist");\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Validates the match between policy type specified in path and the one specified in type of policy.\r
+ *\r
+ * @param policyTypeId the ID of policy type\r
+ * @param policyTypeVersion the version of policy type\r
+ * @param serviceTemplate the ToscaServiceTemplate to validate\r
+ *\r
+ * @throws PfModelException the PfModel parsing exception\r
+ */\r
+ private void validatePolicyTypeMatch(String policyTypeId, String policyTypeVersion,\r
+ ToscaServiceTemplate serviceTemplate) throws PfModelException {\r
+\r
+ List<Map<String, ToscaPolicy>> policies = serviceTemplate.getToscaTopologyTemplate().getPolicies();\r
+ for (Map<String, ToscaPolicy> policy : policies) {\r
+ if (policy.size() > 1) {\r
+ throw new PfModelException(Response.Status.BAD_REQUEST,\r
+ "one policy block contains more than one policies");\r
+ }\r
+ ToscaPolicy policyContent = policy.values().iterator().next();\r
+ if (!policyTypeId.equalsIgnoreCase(policyContent.getType())) {\r
+ throw new PfModelException(Response.Status.BAD_REQUEST, "policy type id does not match");\r
+ }\r
+ if (policyContent.getTypeVersion() != null\r
+ && !policyTypeVersion.equalsIgnoreCase(policyContent.getTypeVersion())) {\r
+ throw new PfModelException(Response.Status.BAD_REQUEST, "policy type version does not match");\r
+ }\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Validates whether specified policy can be deleted based on the rule that deployed policy cannot be deleted.\r
+ *\r
+ * @param policyTypeId the ID of policy type\r
+ * @param policyTypeVersion the version of policy type\r
+ * @param policyId the ID of policy\r
+ * @param policyVersion the version of policy\r
+ *\r
+ * @throws PfModelException the PfModel parsing exception\r
+ */\r
+ private void validateDeleteEligibility(String policyTypeId, String policyTypeVersion,\r
+ String policyId, String policyVersion) throws PfModelException {\r
+\r
+ List<ToscaPolicyTypeIdentifier> policyTypes = new ArrayList<>();\r
+ policyTypes.add(new ToscaPolicyTypeIdentifier(policyTypeId, policyTypeVersion));\r
+ List<ToscaPolicyIdentifier> policies = new ArrayList<>();\r
+ policies.add(new ToscaPolicyIdentifier(policyId, policyVersion));\r
+ PdpGroupFilter pdpGroupFilter = PdpGroupFilter.builder()\r
+ .policyTypeList(policyTypes).policyList(policies).build();\r
+\r
+ List<PdpGroup> pdpGroups = modelsProvider.getFilteredPdpGroups(pdpGroupFilter);\r
+\r
+ if (!pdpGroups.isEmpty()) {\r
+ throw new PfModelException(Response.Status.CONFLICT,\r
+ constructDeleteRuleViolationMessage(policyId, policyVersion, pdpGroups));\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
+ private String constructDeleteRuleViolationMessage(\r
+ String policyId, String policyVersion, List<PdpGroup> pdpGroups) {\r
+\r
+ List<String> pdpGroupNameVersionList = new ArrayList<>();\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 the map of deployed pdp groups and deployed policies.\r
+ *\r
+ * @param pdpGroups the list of pdp groups that contain the specified policy\r
+ * @param policyId the ID of policy\r
+ *\r
+ * @return the constructed map of pdp groups and deployed policies\r
+ *\r
+ * @throws PfModelException the PfModel parsing exception\r
+ */\r
+ private Map<Pair<String, String>, List<ToscaPolicy>> constructDeployedPolicyMap(\r
+ List<PdpGroup> pdpGroups, String policyId) throws PfModelException {\r
+\r
+ Map<Pair<String, String>, List<ToscaPolicy>> deployedPolicyMap = new HashMap<>();\r
+ for (PdpGroup pdpGroup : pdpGroups) {\r
+ List<ToscaPolicyIdentifier> policyIdentifiers = new ArrayList<>();\r
+ for (PdpSubGroup pdpSubGroup : pdpGroup.getPdpSubgroups()) {\r
+ for (ToscaPolicyIdentifier policyIdentifier : pdpSubGroup.getPolicies()) {\r
+ if (policyId.equalsIgnoreCase(policyIdentifier.getName())) {\r
+ policyIdentifiers.add(policyIdentifier);\r
+ }\r
+ }\r
+ }\r
+ List<ToscaPolicy> deployedPolicies = new ArrayList<>();\r
+ if (!policyIdentifiers.isEmpty()) {\r
+ for (ToscaPolicyIdentifier policyIdentifier : policyIdentifiers) {\r
+ deployedPolicies.addAll(\r
+ modelsProvider.getPolicyList(policyIdentifier.getName(), policyIdentifier.getVersion()));\r
+ }\r
+ }\r
+ if (!deployedPolicies.isEmpty()) {\r
+ deployedPolicyMap.put(Pair.of(pdpGroup.getName(), pdpGroup.getVersion()), deployedPolicies);\r
+ }\r
+ }\r
+ return deployedPolicyMap;\r
+ }\r
+\r
+ /**\r
+ * Constructs returned message for not found resource.\r
+ *\r
+ * @param policyTypeId the ID of policy type\r
+ * @param policyTypeVersion the version of policy type\r
+ * @param policyId the ID of policy\r
+ * @param policyVersion the version of policy\r
+ *\r
+ * @return constructed message\r
+ */\r
+ private String constructResourceNotFoundMessage(String policyTypeId, String policyTypeVersion,\r
+ String policyId, String policyVersion) {\r
+\r
+ return "policy with ID " + policyId + ":" + policyVersion\r
+ + " and type " + policyTypeId + ":" + policyTypeVersion + " does not exist";\r
+ }\r
+\r
+ /**\r
+ * Constructs returned message for not found policy deployment.\r
+ *\r
+ * @param policyTypeId the ID of policy type\r
+ * @param policyTypeVersion the version of policy type\r
+ * @param policyId the ID of policy\r
+ * @param policyVersion the version of policy\r
+ *\r
+ * @return constructed message\r
+ */\r
+ private String constructDeploymentNotFoundMessage(String policyTypeId, String policyTypeVersion,\r
+ String policyId) {\r
+\r
+ return "could not find policy with ID " + policyId + " and type "\r
+ + policyTypeId + ":" + policyTypeVersion + " deployed in any pdp group";\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
+ private boolean hasPolicy(ToscaServiceTemplate serviceTemplate) {\r
+\r
+ if (serviceTemplate.getToscaTopologyTemplate().getPolicies() == null) {\r
+ return false;\r
+ } else if (serviceTemplate.getToscaTopologyTemplate().getPolicies().isEmpty()) {\r
+ return false;\r
+ } else if (serviceTemplate.getToscaTopologyTemplate().getPolicies().get(0).isEmpty()) {\r
+ return false;\r
+ } else {\r
+ return true;\r
+ }\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
+ private boolean hasPolicyType(ToscaServiceTemplate serviceTemplate) {\r
+\r
+ if (serviceTemplate.getPolicyTypes() == null) {\r
+ return false;\r
+ } else if (serviceTemplate.getPolicyTypes().isEmpty()) {\r
+ return false;\r
+ } else if (serviceTemplate.getPolicyTypes().get(0).isEmpty()) {\r
+ return false;\r
+ } else {\r
+ return true;\r
+ }\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