Add changes for safe delete in policy-models
[policy/api.git] / main / src / main / java / org / onap / policy / api / main / rest / provider / PolicyProvider.java
index 99dcd71..6b12395 100644 (file)
@@ -2,7 +2,8 @@
  * ============LICENSE_START=======================================================\r
  * ONAP Policy API\r
  * ================================================================================\r
- * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.\r
+ * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.\r
+ * Modifications Copyright (C) 2020 Nordix Foundation.\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
@@ -25,16 +26,14 @@ package org.onap.policy.api.main.rest.provider;
 import java.util.ArrayList;\r
 import java.util.List;\r
 import java.util.Map;\r
+\r
 import javax.ws.rs.core.Response;\r
+\r
 import org.apache.commons.lang3.tuple.Pair;\r
 import org.onap.policy.models.base.PfConceptKey;\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.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
@@ -63,13 +62,11 @@ public class PolicyProvider extends CommonModelProvider {
      *\r
      * @throws PfModelException the PfModel parsing exception\r
      */\r
-    public ToscaServiceTemplate fetchPolicies(String policyTypeId, String policyTypeVersion,\r
-            String policyId, String policyVersion) throws PfModelException {\r
+    public ToscaServiceTemplate fetchPolicies(String policyTypeId, String policyTypeVersion, String policyId,\r
+            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
+        ToscaServiceTemplate serviceTemplate =\r
+                getFilteredPolicies(policyTypeId, policyTypeVersion, policyId, policyVersion);\r
 \r
         if (!hasPolicy(serviceTemplate)) {\r
             throw new PfModelException(Response.Status.NOT_FOUND,\r
@@ -90,13 +87,11 @@ public class PolicyProvider extends CommonModelProvider {
      *\r
      * @throws PfModelException the PfModel parsing exception\r
      */\r
-    public ToscaServiceTemplate fetchLatestPolicies(String policyTypeId, String policyTypeVersion,\r
-            String policyId) throws PfModelException {\r
+    public ToscaServiceTemplate fetchLatestPolicies(String policyTypeId, String policyTypeVersion, String policyId)\r
+            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
+        ToscaServiceTemplate serviceTemplate =\r
+                getFilteredPolicies(policyTypeId, policyTypeVersion, policyId, ToscaPolicyFilter.LATEST_VERSION);\r
 \r
         if (!hasPolicy(serviceTemplate)) {\r
             throw new PfModelException(Response.Status.NOT_FOUND,\r
@@ -117,29 +112,26 @@ public class PolicyProvider extends CommonModelProvider {
      *\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
+    public Map<Pair<String, String>, List<ToscaPolicy>> fetchDeployedPolicies(String policyTypeId,\r
+            String policyTypeVersion, String policyId) throws PfModelException {\r
 \r
         return collectDeployedPolicies(policyId, new PfConceptKey(policyTypeId, policyTypeVersion),\r
-                modelsProvider::getPolicyList, List::addAll, new ArrayList<>());\r
+                modelsProvider::getPolicyList, List::addAll, new ArrayList<>(5));\r
     }\r
 \r
     /**\r
-     * Creates a new policy for a policy type ID and version.\r
+     * Creates one or more new policies for the same 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
+     * @param body the entity body of polic(ies)\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
+    public ToscaServiceTemplate createPolicy(String policyTypeId, String policyTypeVersion, ToscaServiceTemplate body)\r
+            throws PfModelException {\r
 \r
         return modelsProvider.createPolicies(body);\r
     }\r
@@ -154,7 +146,6 @@ public class PolicyProvider extends CommonModelProvider {
      * @throws PfModelException the PfModel parsing exception\r
      */\r
     public ToscaServiceTemplate createPolicies(ToscaServiceTemplate body) throws PfModelException {\r
-\r
         return modelsProvider.createPolicies(body);\r
     }\r
 \r
@@ -170,10 +161,8 @@ public class PolicyProvider extends CommonModelProvider {
      *\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
+    public ToscaServiceTemplate deletePolicy(String policyTypeId, String policyTypeVersion, String policyId,\r
+            String policyVersion) throws PfModelException {\r
 \r
         ToscaServiceTemplate serviceTemplate = modelsProvider.deletePolicy(policyId, policyVersion);\r
 \r
@@ -186,77 +175,23 @@ public class PolicyProvider extends CommonModelProvider {
     }\r
 \r
     /**\r
-     * Validates whether policy type exists.\r
+     * Retrieves the specified version of the policy.\r
      *\r
-     * @param policyTypeId the ID of policy type\r
-     * @param policyTypeVersion the version of policy type\r
+     * @param policyTypeName the name of the policy type\r
+     * @param policyTypeVersion the version of the policy type\r
+     * @param policyName the name of the policy\r
+     * @param policyVersion the version of the policy\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
+     * @return the TOSCA service template containing the specified version of the policy\r
      *\r
      * @throws PfModelException the PfModel parsing exception\r
      */\r
-    private void validatePolicyTypeMatch(String policyTypeId, String policyTypeVersion,\r
-            ToscaServiceTemplate serviceTemplate) throws PfModelException {\r
+    private ToscaServiceTemplate getFilteredPolicies(String policyTypeName, String policyTypeVersion, String policyName,\r
+            String policyVersion) 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
-                    constructDeletePolicyViolationMessage(policyId, policyVersion, pdpGroups));\r
-        }\r
+        ToscaPolicyFilter policyFilter = ToscaPolicyFilter.builder().name(policyName).version(policyVersion)\r
+                .type(policyTypeName).typeVersion(policyTypeVersion).build();\r
+        return modelsProvider.getFilteredPolicies(policyFilter);\r
     }\r
 \r
     /**\r
@@ -269,10 +204,10 @@ public class PolicyProvider extends CommonModelProvider {
      *\r
      * @return constructed message\r
      */\r
-    private String constructResourceNotFoundMessage(String policyTypeId, String policyTypeVersion,\r
-            String policyId, String policyVersion) {\r
+    private String constructResourceNotFoundMessage(String policyTypeId, String policyTypeVersion, String policyId,\r
+            String policyVersion) {\r
 \r
-        return "policy with ID " + policyId + ":" + policyVersion\r
-                + " and type " + policyTypeId + ":" + policyTypeVersion + " does not exist";\r
+        return "policy with ID " + policyId + ":" + policyVersion + " and type " + policyTypeId + ":"\r
+                + policyTypeVersion + " does not exist";\r
     }\r
 }\r