Fix sonar issues in policy-api
[policy/api.git] / main / src / main / java / org / onap / policy / api / main / rest / provider / PolicyProvider.java
index 102f6f8..a4440da 100644 (file)
@@ -28,16 +28,11 @@ import java.util.List;
 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
@@ -49,18 +44,13 @@ import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
  *\r
  * @author Chenfei Gao (cgao@research.att.com)\r
  */\r
-public class PolicyProvider implements AutoCloseable {\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
+        super();\r
     }\r
 \r
     /**\r
@@ -270,31 +260,10 @@ public class PolicyProvider implements AutoCloseable {
 \r
         if (!pdpGroups.isEmpty()) {\r
             throw new PfModelException(Response.Status.CONFLICT,\r
-                    constructDeleteRuleViolationMessage(policyId, policyVersion, pdpGroups));\r
+                    constructDeletePolicyViolationMessage(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
@@ -310,21 +279,8 @@ public class PolicyProvider implements AutoCloseable {
 \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
+            List<ToscaPolicyIdentifier> policyIdentifiers = extractPolicyIdentifiers(policyId, pdpGroup);\r
+            List<ToscaPolicy> deployedPolicies = getDeployedPolicies(policyIdentifiers);\r
             if (!deployedPolicies.isEmpty()) {\r
                 deployedPolicyMap.put(Pair.of(pdpGroup.getName(), pdpGroup.getVersion()), deployedPolicies);\r
             }\r
@@ -332,6 +288,31 @@ public class PolicyProvider implements AutoCloseable {
         return deployedPolicyMap;\r
     }\r
 \r
+    private List<ToscaPolicyIdentifier> extractPolicyIdentifiers(String policyId, PdpGroup pdpGroup) {\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
+        return policyIdentifiers;\r
+    }\r
+\r
+    private List<ToscaPolicy> getDeployedPolicies(List<ToscaPolicyIdentifier> policyIdentifiers)\r
+                    throws PfModelException {\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
+        return deployedPolicies;\r
+    }\r
+\r
     /**\r
      * Constructs returned message for not found resource.\r
      *\r
@@ -365,55 +346,4 @@ public class PolicyProvider implements AutoCloseable {
         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