Added the new versioning validation for policy and policy type
[policy/api.git] / main / src / main / java / org / onap / policy / api / main / rest / provider / PolicyProvider.java
index 99dcd71..e45674c 100644 (file)
@@ -2,7 +2,7 @@
  * ============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
  * ================================================================================\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
 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 java.util.Map.Entry;\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
@@ -66,10 +69,8 @@ public class PolicyProvider extends CommonModelProvider {
     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
+        ToscaServiceTemplate serviceTemplate = getFilteredPolicies(\r
+                policyTypeId, policyTypeVersion, policyId, policyVersion);\r
 \r
         if (!hasPolicy(serviceTemplate)) {\r
             throw new PfModelException(Response.Status.NOT_FOUND,\r
@@ -93,10 +94,8 @@ public class PolicyProvider extends CommonModelProvider {
     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
+        ToscaServiceTemplate serviceTemplate = getFilteredPolicies(\r
+                policyTypeId, policyTypeVersion, policyId, ToscaPolicyFilter.LATEST_VERSION);\r
 \r
         if (!hasPolicy(serviceTemplate)) {\r
             throw new PfModelException(Response.Status.NOT_FOUND,\r
@@ -121,15 +120,15 @@ public class PolicyProvider extends CommonModelProvider {
             String policyTypeId, 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
@@ -140,7 +139,7 @@ public class PolicyProvider extends CommonModelProvider {
 \r
         validatePolicyTypeExist(policyTypeId, policyTypeVersion);\r
         validatePolicyTypeMatch(policyTypeId, policyTypeVersion, body);\r
-\r
+        validatePolicyVersion(body);\r
         return modelsProvider.createPolicies(body);\r
     }\r
 \r
@@ -155,6 +154,7 @@ public class PolicyProvider extends CommonModelProvider {
      */\r
     public ToscaServiceTemplate createPolicies(ToscaServiceTemplate body) throws PfModelException {\r
 \r
+        validatePolicyVersion(body);\r
         return modelsProvider.createPolicies(body);\r
     }\r
 \r
@@ -244,9 +244,9 @@ public class PolicyProvider extends CommonModelProvider {
     private void validateDeleteEligibility(String policyTypeId, String policyTypeVersion,\r
             String policyId, String policyVersion) throws PfModelException {\r
 \r
-        List<ToscaPolicyTypeIdentifier> policyTypes = new ArrayList<>();\r
+        List<ToscaPolicyTypeIdentifier> policyTypes = new ArrayList<>(1);\r
         policyTypes.add(new ToscaPolicyTypeIdentifier(policyTypeId, policyTypeVersion));\r
-        List<ToscaPolicyIdentifier> policies = new ArrayList<>();\r
+        List<ToscaPolicyIdentifier> policies = new ArrayList<>(1);\r
         policies.add(new ToscaPolicyIdentifier(policyId, policyVersion));\r
         PdpGroupFilter pdpGroupFilter = PdpGroupFilter.builder()\r
                 .policyTypeList(policyTypes).policyList(policies).build();\r
@@ -259,6 +259,145 @@ public class PolicyProvider extends CommonModelProvider {
         }\r
     }\r
 \r
+    /**\r
+     * Validates the provided policy version in the payload.\r
+     *\r
+     * @param body the provided TOSCA service template which contains the policies\r
+     *\r
+     * @throws PfModelException the PfModel parsing exception\r
+     */\r
+    private void validatePolicyVersion(ToscaServiceTemplate body) throws PfModelException {\r
+\r
+        validatePolicyVersionExist(body);\r
+        validateNoDuplicateVersionInDb(body);\r
+        validateNoDuplicateVersionInPl(body);\r
+    }\r
+\r
+    /**\r
+     * Validates the existence of at least one policies in the provided payload.\r
+     *\r
+     * @param body the TOSCA service template payload to check against\r
+     *\r
+     * @throws PfModelException the PfModel parsing exception\r
+     */\r
+    private void validatePolicyVersionExist(ToscaServiceTemplate body) throws PfModelException {\r
+\r
+        List<String> invalidPolicyNames = new ArrayList<>(body.getToscaTopologyTemplate().getPolicies().size());\r
+        for (Map<String, ToscaPolicy> policy : body.getToscaTopologyTemplate().getPolicies()) {\r
+            ToscaPolicy policyContent = policy.values().iterator().next();\r
+            if (policyContent.getVersion() == null) {\r
+                invalidPolicyNames.add(policy.keySet().iterator().next());\r
+            }\r
+        }\r
+\r
+        if (!invalidPolicyNames.isEmpty()) {\r
+            String errMsg = "mandatory 'version' field is missing in policies: "\r
+                + String.join(", ", invalidPolicyNames);\r
+            throw new PfModelException(Response.Status.NOT_ACCEPTABLE, errMsg);\r
+        }\r
+    }\r
+\r
+    /**\r
+     * Validates there is no duplicate policy version stored in the database.\r
+     *\r
+     * @param body the TOSCA service template payload to check against\r
+     *\r
+     * @throws PfModelException the PfModel parsing exception\r
+     */\r
+    private void validateNoDuplicateVersionInDb(ToscaServiceTemplate body) throws PfModelException {\r
+\r
+        Map<String, String> invalidPolicies = new HashMap<>();\r
+        for (Map<String, ToscaPolicy> policy: body.getToscaTopologyTemplate().getPolicies()) {\r
+            ToscaPolicy policyContent = policy.values().iterator().next();\r
+            String policyName = policy.keySet().iterator().next();\r
+            String policyVersion = policyContent.getVersion();\r
+            String policyTypeName = policyContent.getType();\r
+            String policyTypeVersion = policyContent.getTypeVersion();\r
+            ToscaServiceTemplate serviceTemplate =\r
+                    getFilteredPolicies(policyTypeName, policyTypeVersion, policyName, policyVersion);\r
+            if (hasPolicy(serviceTemplate)) {\r
+                String latestVersion = getFilteredPolicies(policyTypeName, policyTypeVersion,\r
+                        policyName, ToscaPolicyFilter.LATEST_VERSION).getToscaTopologyTemplate().getPoliciesAsMap()\r
+                        .values().iterator().next().getVersion();\r
+                invalidPolicies.put(String.join(":", policyName, policyVersion), latestVersion);\r
+            }\r
+        }\r
+\r
+        if (!invalidPolicies.isEmpty()) {\r
+            List<String> duplicateVersions = new ArrayList<>(5);\r
+            for (Entry<String, String> invalidPolicy : invalidPolicies.entrySet()) {\r
+                String eachDuplicateVersion = "policy " + invalidPolicy.getKey()\r
+                    + " already exists; its latest version is " + invalidPolicy.getValue();\r
+                duplicateVersions.add(eachDuplicateVersion);\r
+            }\r
+            throw new PfModelException(Response.Status.NOT_ACCEPTABLE, String.join("\n", duplicateVersions));\r
+        }\r
+    }\r
+\r
+    /**\r
+     * Validates there is no duplicate policy version in the provided payload if multiple policies are present.\r
+     *\r
+     * @param body the TOSCA service template payload to check against\r
+     *\r
+     * @throws PfModelException the PfModel parsing exception\r
+     */\r
+    private void validateNoDuplicateVersionInPl(ToscaServiceTemplate body) throws PfModelException {\r
+\r
+        List<Map<String, ToscaPolicy>> policies = body.getToscaTopologyTemplate().getPolicies();\r
+        List<String> duplicateVersions = new ArrayList<>();\r
+        for (int i = 0; i < policies.size() - 1; i++) {\r
+            for (int j = i + 1; j < policies.size(); j++) {\r
+                if (hasSameNameVersion(policies.get(i), policies.get(j))) {\r
+                    String nameVersion = policies.get(i).keySet().iterator().next() + ":"\r
+                        + policies.get(i).values().iterator().next().getVersion();\r
+                    duplicateVersions.add(nameVersion);\r
+                }\r
+            }\r
+        }\r
+\r
+        if (!duplicateVersions.isEmpty()) {\r
+            String errMsg = "the same version of policies '" + String.join(", ", duplicateVersions)\r
+                + "' appear multiple times in the payload";\r
+            throw new PfModelException(Response.Status.NOT_ACCEPTABLE, errMsg);\r
+        }\r
+    }\r
+\r
+    /**\r
+     * Checks if two policies have the same name and version.\r
+     *\r
+     * @param policy1 the first policy\r
+     * @param policy2 the second policy\r
+     *\r
+     * @return the boolean flag to indicate the result\r
+     */\r
+    private boolean hasSameNameVersion(Map<String, ToscaPolicy> policy1, Map<String, ToscaPolicy> policy2) {\r
+\r
+        return (policy1.keySet().iterator().next().equals(policy2.keySet().iterator().next())\r
+                && policy1.values().iterator().next().getVersion().equals(\r
+                        policy2.values().iterator().next().getVersion()));\r
+    }\r
+\r
+    /**\r
+     * Retrieves the specified version of the policy.\r
+     *\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
+     * @return the TOSCA service template containing the specified version of the policy\r
+     *\r
+     * @throws PfModelException the PfModel parsing exception\r
+     */\r
+    private ToscaServiceTemplate getFilteredPolicies(String policyTypeName, String policyTypeVersion,\r
+            String policyName, String policyVersion) throws PfModelException {\r
+\r
+        ToscaPolicyFilter policyFilter = ToscaPolicyFilter.builder()\r
+                .name(policyName).version(policyVersion)\r
+                .type(policyTypeName).typeVersion(policyTypeVersion).build();\r
+        return modelsProvider.getFilteredPolicies(policyFilter);\r
+    }\r
+\r
     /**\r
      * Constructs returned message for not found resource.\r
      *\r