Add changes for safe delete in policy-models
[policy/api.git] / main / src / main / java / org / onap / policy / api / main / rest / provider / LegacyGuardPolicyProvider.java
index bdfe430..80c57d3 100644 (file)
@@ -2,7 +2,8 @@
  * ============LICENSE_START=======================================================
  * ONAP Policy API
  * ================================================================================
- * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
+ * Modifications Copyright (C) 2020 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 
 package org.onap.policy.api.main.rest.provider;
 
+import java.util.HashMap;
+import java.util.LinkedHashMap;
 import java.util.Map;
+import java.util.Map.Entry;
+
 import javax.ws.rs.core.Response;
-import org.onap.policy.api.main.parameters.ApiParameterGroup;
-import org.onap.policy.common.parameters.ParameterService;
+
+import org.apache.commons.lang3.tuple.Pair;
+import org.onap.policy.models.base.PfConceptKey;
 import org.onap.policy.models.base.PfModelException;
-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.legacy.concepts.LegacyGuardPolicyInput;
 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyOutput;
 
@@ -38,18 +41,23 @@ import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyOutput;
  *
  * @author Chenfei Gao (cgao@research.att.com)
  */
-public class LegacyGuardPolicyProvider {
+public class LegacyGuardPolicyProvider extends CommonModelProvider {
 
-    private PolicyModelsProvider modelsProvider;
+    private static final Map<String, PfConceptKey> GUARD_POLICY_TYPE_MAP = new LinkedHashMap<>();
+
+    static {
+        GUARD_POLICY_TYPE_MAP.put("guard.frequency.",
+                new PfConceptKey("onap.policies.controlloop.guard.FrequencyLimiter:1.0.0"));
+        GUARD_POLICY_TYPE_MAP.put("guard.minmax.", new PfConceptKey("onap.policies.controlloop.guard.MinMax:1.0.0"));
+        GUARD_POLICY_TYPE_MAP.put("guard.blacklist.",
+                new PfConceptKey("onap.policies.controlloop.guard.Blacklist:1.0.0"));
+    }
 
     /**
      * Default constructor.
      */
     public LegacyGuardPolicyProvider() throws PfModelException {
-
-        ApiParameterGroup parameterGroup = ParameterService.get("ApiGroup");
-        PolicyModelsProviderParameters providerParameters = parameterGroup.getDatabaseProviderParameters();
-        modelsProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(providerParameters);
+        super();
     }
 
     /**
@@ -63,10 +71,23 @@ public class LegacyGuardPolicyProvider {
     public Map<String, LegacyGuardPolicyOutput> fetchGuardPolicy(String policyId, String policyVersion)
             throws PfModelException {
 
-        Map<String, LegacyGuardPolicyOutput> guardPolicies = modelsProvider.getGuardPolicy(policyId);
+        return modelsProvider.getGuardPolicy(policyId, policyVersion);
+    }
 
-        close();
-        return guardPolicies;
+    /**
+     * Retrieves a list of deployed guard policies in each pdp group.
+     *
+     * @param policyId the ID of the policy
+     *
+     * @return a list of deployed policies in each pdp group
+     *
+     * @throws PfModelException the PfModel parsing exception
+     */
+    public Map<Pair<String, String>, Map<String, LegacyGuardPolicyOutput>> fetchDeployedGuardPolicies(String policyId)
+            throws PfModelException {
+
+        return collectDeployedPolicies(policyId, getGuardPolicyType(policyId), modelsProvider::getGuardPolicy,
+                Map::putAll, new HashMap<>());
     }
 
     /**
@@ -78,10 +99,7 @@ public class LegacyGuardPolicyProvider {
      */
     public Map<String, LegacyGuardPolicyOutput> createGuardPolicy(LegacyGuardPolicyInput body) throws PfModelException {
 
-        Map<String, LegacyGuardPolicyOutput> guardPolicies = modelsProvider.createGuardPolicy(body);
-
-        close();
-        return guardPolicies;
+        return modelsProvider.createGuardPolicy(body);
     }
 
     /**
@@ -95,23 +113,25 @@ public class LegacyGuardPolicyProvider {
     public Map<String, LegacyGuardPolicyOutput> deleteGuardPolicy(String policyId, String policyVersion)
             throws PfModelException {
 
-        Map<String, LegacyGuardPolicyOutput> guardPolicies = modelsProvider.deleteGuardPolicy(policyId);
-
-        close();
-        return guardPolicies;
+        return modelsProvider.deleteGuardPolicy(policyId, policyVersion);
     }
 
     /**
-     * Closes the connection to database.
+     * Retrieves guard policy type given guard policy ID.
+     *
+     * @param policyId the ID of guard policy
+     *
+     * @return the concept key of guard policy type
      *
      * @throws PfModelException the PfModel parsing exception
      */
-    private void close() throws PfModelException {
-        try {
-            modelsProvider.close();
-        } catch (Exception e) {
-            throw new PfModelException(
-                    Response.Status.INTERNAL_SERVER_ERROR, "error closing connection to database", e);
+    private PfConceptKey getGuardPolicyType(String policyId) throws PfModelException {
+
+        for (Entry<String, PfConceptKey> guardPolicyTypeEntry : GUARD_POLICY_TYPE_MAP.entrySet()) {
+            if (policyId.startsWith(guardPolicyTypeEntry.getKey())) {
+                return guardPolicyTypeEntry.getValue();
+            }
         }
+        throw new PfModelException(Response.Status.BAD_REQUEST, "No policy type defined for " + policyId);
     }
 }
\ No newline at end of file