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 8ce9a49..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
 \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
+\r
 import javax.ws.rs.core.Response;\r
+\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.PfConceptKey;\r
 import org.onap.policy.models.base.PfModelException;\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.ToscaServiceTemplate;\r
 \r
 /**\r
@@ -40,19 +41,13 @@ import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
  *\r
  * @author Chenfei Gao (cgao@research.att.com)\r
  */\r
-public class PolicyProvider {\r
-\r
-    private PolicyModelsProvider modelsProvider;\r
+public class PolicyProvider extends CommonModelProvider {\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
-        modelsProvider.init();\r
+        super();\r
     }\r
 \r
     /**\r
@@ -67,17 +62,17 @@ public class PolicyProvider {
      *\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
-        validatePathParam(policyTypeId, policyTypeVersion);\r
-        ToscaServiceTemplate serviceTemplate;\r
-        if (policyId == null) {\r
-            serviceTemplate = modelsProvider.getPolicies4PolicyType(policyTypeId, policyTypeVersion);\r
-        } else {\r
-            serviceTemplate = modelsProvider.getPolicies(policyId, policyVersion);\r
+    public ToscaServiceTemplate fetchPolicies(String policyTypeId, String policyTypeVersion, String policyId,\r
+            String policyVersion) throws PfModelException {\r
+\r
+        ToscaServiceTemplate serviceTemplate =\r
+                getFilteredPolicies(policyTypeId, policyTypeVersion, policyId, policyVersion);\r
+\r
+        if (!hasPolicy(serviceTemplate)) {\r
+            throw new PfModelException(Response.Status.NOT_FOUND,\r
+                    constructResourceNotFoundMessage(policyTypeId, policyTypeVersion, policyId, policyVersion));\r
         }\r
-        close();\r
+\r
         return serviceTemplate;\r
     }\r
 \r
@@ -92,12 +87,17 @@ public class PolicyProvider {
      *\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
+        ToscaServiceTemplate serviceTemplate =\r
+                getFilteredPolicies(policyTypeId, policyTypeVersion, policyId, ToscaPolicyFilter.LATEST_VERSION);\r
+\r
+        if (!hasPolicy(serviceTemplate)) {\r
+            throw new PfModelException(Response.Status.NOT_FOUND,\r
+                    constructResourceNotFoundMessage(policyTypeId, policyTypeVersion, policyId, null));\r
+        }\r
 \r
-        validatePathParam(policyTypeId, policyTypeVersion);\r
-        ToscaServiceTemplate serviceTemplate = modelsProvider.getLatestPolicies(policyId);\r
-        close();\r
         return serviceTemplate;\r
     }\r
 \r
@@ -112,110 +112,102 @@ public class PolicyProvider {
      *\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
-        validatePathParam(policyTypeId, policyTypeVersion);\r
-        Map<Pair<String, String>, List<ToscaPolicy>> deployedPolicies = modelsProvider.getDeployedPolicyList(policyId);\r
-        close();\r
-        return deployedPolicies;\r
+        return collectDeployedPolicies(policyId, new PfConceptKey(policyTypeId, policyTypeVersion),\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
+    public ToscaServiceTemplate createPolicy(String policyTypeId, String policyTypeVersion, ToscaServiceTemplate body)\r
+            throws PfModelException {\r
 \r
-        validatePathParam(policyTypeId, policyTypeVersion);\r
-        validatePolicyTypeMatch(policyTypeId, policyTypeVersion, body);\r
-        ToscaServiceTemplate serviceTemplate = modelsProvider.createPolicies(body);\r
-        close();\r
-        return serviceTemplate;\r
+        return modelsProvider.createPolicies(body);\r
     }\r
 \r
     /**\r
-     * Deletes the policy matching specified ID and version of both policy type and policy.\r
+     * Creates one or more new policies.\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
+     * @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 deletePolicy(String policyTypeId, String policyTypeVersion,\r
-                                 String policyId, String policyVersion) throws PfModelException {\r
-\r
-        validatePathParam(policyTypeId, policyTypeVersion);\r
-        ToscaServiceTemplate serviceTemplate = modelsProvider.deletePolicy(policyId, policyVersion);\r
-        close();\r
-        return serviceTemplate;\r
+    public ToscaServiceTemplate createPolicies(ToscaServiceTemplate body) throws PfModelException {\r
+        return modelsProvider.createPolicies(body);\r
     }\r
 \r
     /**\r
-     * Checks the validation of policy type info passed in as path param.\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
-    private void validatePathParam(String policyTypeId, String policyTypeVersion) throws PfModelException {\r
+    public ToscaServiceTemplate deletePolicy(String policyTypeId, String policyTypeVersion, String policyId,\r
+            String policyVersion) throws PfModelException {\r
 \r
-        // Check policy type existence\r
-        try {\r
-            modelsProvider.getPolicyTypes(policyTypeId, policyTypeVersion);\r
-        } catch (Exception e) {\r
-            throw new PfModelException(Response.Status.NOT_FOUND, "specified policy type does not exist", e);\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 the match between policy type specified in path param and the one specified in type of policy.\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
-     * @param body the ToscaServiceTemplate to create\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, ToscaServiceTemplate body)\r
-            throws PfModelException {\r
+    private ToscaServiceTemplate getFilteredPolicies(String policyTypeName, String policyTypeVersion, String policyName,\r
+            String policyVersion) throws PfModelException {\r
 \r
-        List<Map<String, ToscaPolicy>> policies = body.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
-                    || !policyTypeVersion.equalsIgnoreCase(policyContent.getVersion())) {\r
-                throw new PfModelException(Response.Status.BAD_REQUEST, "policy type info does not match");\r
-            }\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
-     * Closes the connection to database.\r
+     * Constructs returned message for not found resource.\r
      *\r
-     * @throws PfModelException the PfModel parsing exception\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 void close() throws PfModelException {\r
-        try {\r
-            modelsProvider.close();\r
-        } catch (Exception e) {\r
-            throw new PfModelException(\r
-                    Response.Status.INTERNAL_SERVER_ERROR, "error closing connection to database", e);\r
-        }\r
+    private String constructResourceNotFoundMessage(String policyTypeId, String policyTypeVersion, String policyId,\r
+            String policyVersion) {\r
+\r
+        return "policy with ID " + policyId + ":" + policyVersion + " and type " + policyTypeId + ":"\r
+                + policyTypeVersion + " does not exist";\r
     }\r
-}
\ No newline at end of file
+}\r