Fix sonar issues in policy-api
[policy/api.git] / main / src / main / java / org / onap / policy / api / main / rest / provider / LegacyGuardPolicyProvider.java
index 186ce0a..2f45086 100644 (file)
@@ -26,14 +26,9 @@ import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 import javax.ws.rs.core.Response;
-import org.onap.policy.api.main.parameters.ApiParameterGroup;
-import org.onap.policy.common.parameters.ParameterService;
 import org.onap.policy.models.base.PfModelException;
 import org.onap.policy.models.pdp.concepts.PdpGroup;
 import org.onap.policy.models.pdp.concepts.PdpGroupFilter;
-import org.onap.policy.models.provider.PolicyModelsProvider;
-import org.onap.policy.models.provider.PolicyModelsProviderFactory;
-import org.onap.policy.models.provider.PolicyModelsProviderParameters;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier;
 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput;
 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyOutput;
@@ -43,18 +38,16 @@ import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyOutput;
  *
  * @author Chenfei Gao (cgao@research.att.com)
  */
-public class LegacyGuardPolicyProvider implements AutoCloseable {
+public class LegacyGuardPolicyProvider extends CommonModelProvider {
+
+    private static final String INVALID_POLICY_VERSION = "legacy policy version is not an integer";
 
-    private PolicyModelsProvider modelsProvider;
 
     /**
      * Default constructor.
      */
     public LegacyGuardPolicyProvider() throws PfModelException {
-
-        ApiParameterGroup parameterGroup = ParameterService.get("ApiGroup");
-        PolicyModelsProviderParameters providerParameters = parameterGroup.getDatabaseProviderParameters();
-        modelsProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(providerParameters);
+        super();
     }
 
     /**
@@ -68,7 +61,10 @@ public class LegacyGuardPolicyProvider implements AutoCloseable {
     public Map<String, LegacyGuardPolicyOutput> fetchGuardPolicy(String policyId, String policyVersion)
             throws PfModelException {
 
-        return modelsProvider.getGuardPolicy(policyId);
+        if (policyVersion != null) {
+            validNumber(policyVersion, INVALID_POLICY_VERSION);
+        }
+        return modelsProvider.getGuardPolicy(policyId, policyVersion);
     }
 
     /**
@@ -78,7 +74,8 @@ public class LegacyGuardPolicyProvider implements AutoCloseable {
      *
      * @return the map of LegacyGuardPolicyOutput objectst
      */
-    public Map<String, LegacyGuardPolicyOutput> createGuardPolicy(LegacyGuardPolicyInput body) throws PfModelException {
+    public Map<String, LegacyGuardPolicyOutput> createGuardPolicy(LegacyGuardPolicyInput body)
+            throws PfModelException {
 
         return modelsProvider.createGuardPolicy(body);
     }
@@ -94,9 +91,10 @@ public class LegacyGuardPolicyProvider implements AutoCloseable {
     public Map<String, LegacyGuardPolicyOutput> deleteGuardPolicy(String policyId, String policyVersion)
             throws PfModelException {
 
+        validNumber(policyVersion, INVALID_POLICY_VERSION);
         validateDeleteEligibility(policyId, policyVersion);
 
-        return modelsProvider.deleteGuardPolicy(policyId);
+        return modelsProvider.deleteGuardPolicy(policyId, policyVersion);
     }
 
     /**
@@ -117,39 +115,7 @@ public class LegacyGuardPolicyProvider implements AutoCloseable {
 
         if (!pdpGroups.isEmpty()) {
             throw new PfModelException(Response.Status.CONFLICT,
-                    constructDeleteRuleViolationMessage(policyId, policyVersion, pdpGroups));
+                    constructDeletePolicyViolationMessage(policyId, policyVersion, pdpGroups));
         }
     }
-
-    /**
-     * Constructs returned message for policy delete rule violation.
-     *
-     * @param policyId the ID of policy
-     * @param policyVersion the version of policy
-     * @param pdpGroups the list of pdp groups
-     *
-     * @return the constructed message
-     */
-    private String constructDeleteRuleViolationMessage(
-            String policyId, String policyVersion, List<PdpGroup> pdpGroups) {
-
-        List<String> pdpGroupNameVersionList = new ArrayList<>();
-        for (PdpGroup pdpGroup : pdpGroups) {
-            pdpGroupNameVersionList.add(pdpGroup.getName() + ":" + pdpGroup.getVersion());
-        }
-        String deployedPdpGroups = String.join(",", pdpGroupNameVersionList);
-        return "policy with ID " + policyId + ":" + policyVersion
-                + " cannot be deleted as it is deployed in pdp groups " + deployedPdpGroups;
-    }
-
-    /**
-     * Closes the connection to database.
-     *
-     * @throws PfModelException the PfModel parsing exception
-     */
-    @Override
-    public void close() throws PfModelException {
-
-        modelsProvider.close();
-    }
 }
\ No newline at end of file