Implement policy provider functions
[policy/api.git] / main / src / main / java / org / onap / policy / api / main / rest / provider / PolicyProvider.java
index 690bdbd..8ce9a49 100644 (file)
 \r
 package org.onap.policy.api.main.rest.provider;\r
 \r
-import org.onap.policy.models.tosca.simple.concepts.ToscaServiceTemplate;\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.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
  * Class to provide all kinds of policy operations.\r
@@ -31,7 +42,18 @@ import org.onap.policy.models.tosca.simple.concepts.ToscaServiceTemplate;
  */\r
 public class PolicyProvider {\r
 \r
-    private static final String DELETE_OK = "Successfully deleted";\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
+        modelsProvider.init();\r
+    }\r
 \r
     /**\r
      * Retrieves a list of policies matching specified ID and version of both policy type and policy.\r
@@ -42,11 +64,61 @@ public class PolicyProvider {
      * @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) {\r
-        // placeholder\r
-        return new ToscaServiceTemplate();\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
+        }\r
+        close();\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
+        validatePathParam(policyTypeId, policyTypeVersion);\r
+        ToscaServiceTemplate serviceTemplate = modelsProvider.getLatestPolicies(policyId);\r
+        close();\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
+        validatePathParam(policyTypeId, policyTypeVersion);\r
+        Map<Pair<String, String>, List<ToscaPolicy>> deployedPolicies = modelsProvider.getDeployedPolicyList(policyId);\r
+        close();\r
+        return deployedPolicies;\r
     }\r
 \r
     /**\r
@@ -57,26 +129,93 @@ public class PolicyProvider {
      * @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) {\r
-        // placeholder\r
-        return new ToscaServiceTemplate();\r
+                                             ToscaServiceTemplate body) throws PfModelException {\r
+\r
+        validatePathParam(policyTypeId, policyTypeVersion);\r
+        validatePolicyTypeMatch(policyTypeId, policyTypeVersion, body);\r
+        ToscaServiceTemplate serviceTemplate = modelsProvider.createPolicies(body);\r
+        close();\r
+        return serviceTemplate;\r
     }\r
 \r
     /**\r
-     * Deletes the policies matching specified ID and version of both policy type and policy.\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 a string message indicating the operation results\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
+    }\r
+\r
+    /**\r
+     * Checks the validation of policy type info passed in as path param.\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 validatePathParam(String policyTypeId, String policyTypeVersion) 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
+        }\r
+    }\r
+\r
+    /**\r
+     * Validates the match between policy type specified in path param and the one specified in type of policy.\r
+     *\r
+     * @param body the ToscaServiceTemplate to create\r
+     *\r
+     * @throws PfModelException the PfModel parsing exception\r
+     */\r
+    private void validatePolicyTypeMatch(String policyTypeId, String policyTypeVersion, ToscaServiceTemplate body)\r
+            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
+    }\r
+\r
+    /**\r
+     * Closes the connection to database.\r
+     *\r
+     * @throws PfModelException the PfModel parsing exception\r
      */\r
-    public String deletePolicies(String policyTypeId, String policyTypeVersion,\r
-                                 String policyId, String policyVersion) {\r
-        // placeholder\r
-        return DELETE_OK;\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
     }\r
 }
\ No newline at end of file