Changes for Checkstyle 8.32
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / legacy / provider / LegacyProvider.java
index 04a010f..4d03f05 100644 (file)
@@ -1,6 +1,7 @@
 /*-
  * ============LICENSE_START=======================================================
- *  Copyright (C) 2019 Nordix Foundation.
+ *  Copyright (C) 2019-2020 Nordix Foundation.
+ *  Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * 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.models.tosca.legacy.provider;
 
-import java.util.ArrayList;
-import java.util.List;
 import java.util.Map;
-
 import javax.ws.rs.core.Response;
-
 import lombok.NonNull;
-
+import org.onap.policy.models.base.PfConceptKey;
 import org.onap.policy.models.base.PfModelException;
 import org.onap.policy.models.base.PfModelRuntimeException;
 import org.onap.policy.models.dao.PfDao;
-import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput;
-import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyOutput;
 import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy;
-import org.onap.policy.models.tosca.legacy.mapping.LegacyGuardPolicyMapper;
 import org.onap.policy.models.tosca.legacy.mapping.LegacyOperationalPolicyMapper;
-import org.onap.policy.models.tosca.simple.concepts.ToscaPolicies;
-import org.onap.policy.models.tosca.simple.concepts.ToscaPolicy;
-import org.onap.policy.models.tosca.simple.concepts.ToscaServiceTemplate;
-import org.onap.policy.models.tosca.simple.concepts.ToscaTopologyTemplate;
+import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicy;
+import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
 import org.onap.policy.models.tosca.simple.provider.SimpleToscaProvider;
+import org.onap.policy.models.tosca.utils.ToscaUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -52,37 +45,31 @@ import org.slf4j.LoggerFactory;
 public class LegacyProvider {
     private static final Logger LOGGER = LoggerFactory.getLogger(LegacyProvider.class);
 
-    private static final String FIRST_POLICY_VERSION = "1";
+    public static final String LEGACY_MINOR_PATCH_SUFFIX = ".0.0";
 
     // Recurring constants
-    private static final String NO_POLICY_FOUND_FOR_POLICY_ID = "no policy found for policy ID: ";
+    private static final String NO_POLICY_FOUND_FOR_POLICY = "no policy found for policy: ";
 
     /**
      * Get legacy operational policy.
      *
      * @param dao the DAO to use to access the database
      * @param policyId ID of the policy.
+     * @param policyVersion version of the policy.
      * @return the policies found
      * @throws PfModelException on errors getting policies
      */
-    public LegacyOperationalPolicy getOperationalPolicy(@NonNull final PfDao dao, @NonNull final String policyId)
-            throws PfModelException {
+    public LegacyOperationalPolicy getOperationalPolicy(@NonNull final PfDao dao, @NonNull final String policyId,
+        final String policyVersion) throws PfModelException {
 
-        ToscaPolicy newestPolicy = getLatestPolicy(dao, policyId);
+        LOGGER.debug("->getOperationalPolicy: policyId={}, policyVersion={}", policyId, policyVersion);
 
-        if (newestPolicy == null) {
-            String errorMessage = NO_POLICY_FOUND_FOR_POLICY_ID + policyId;
-            LOGGER.warn(errorMessage);
-            throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
-        }
+        LegacyOperationalPolicy legacyOperationalPolicy =
+            new LegacyOperationalPolicyMapper().fromToscaServiceTemplate(getLegacyPolicy(dao, policyId, policyVersion));
 
-        // Create the structure of the TOSCA service template to contain the policy type
-        ToscaServiceTemplate serviceTemplate = new ToscaServiceTemplate();
-        serviceTemplate.setTopologyTemplate(new ToscaTopologyTemplate());
-        serviceTemplate.getTopologyTemplate().setPolicies(new ToscaPolicies());
-        serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(newestPolicy.getKey(), newestPolicy);
-
-        return new LegacyOperationalPolicyMapper().fromToscaServiceTemplate(serviceTemplate);
+        LOGGER.debug("<-getOperationalPolicy: policyId={}, policyVersion={}, legacyOperationalPolicy={}", policyId,
+            policyVersion, legacyOperationalPolicy);
+        return legacyOperationalPolicy;
     }
 
     /**
@@ -94,24 +81,20 @@ public class LegacyProvider {
      * @throws PfModelException on errors creating policies
      */
     public LegacyOperationalPolicy createOperationalPolicy(@NonNull final PfDao dao,
-            @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException {
+        @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException {
 
-        // We need to find the latest policy and update the major version, if there is no policy with this ID, then
-        // we set it to the first version
-        ToscaPolicy newestPolicy = getLatestPolicy(dao, legacyOperationalPolicy.getPolicyId());
+        LOGGER.debug("->createOperationalPolicy: legacyOperationalPolicy={}", legacyOperationalPolicy);
 
-        if (newestPolicy == null) {
-            legacyOperationalPolicy.setPolicyVersion(FIRST_POLICY_VERSION);
-        } else {
-            legacyOperationalPolicy.setPolicyVersion(Integer.toString(newestPolicy.getKey().getMajorVersion() + 1));
-        }
+        JpaToscaServiceTemplate legacyOperationalServiceTemplate =
+            new LegacyOperationalPolicyMapper().toToscaServiceTemplate(legacyOperationalPolicy);
 
-        ToscaServiceTemplate incomingServiceTemplate =
-                new LegacyOperationalPolicyMapper().toToscaServiceTemplate(legacyOperationalPolicy);
-        ToscaServiceTemplate outgoingingServiceTemplate =
-                new SimpleToscaProvider().createPolicies(dao, incomingServiceTemplate);
+        new SimpleToscaProvider().createPolicies(dao, legacyOperationalServiceTemplate);
 
-        return new LegacyOperationalPolicyMapper().fromToscaServiceTemplate(outgoingingServiceTemplate);
+        LegacyOperationalPolicy createdLegacyOperationalPolicy =
+            new LegacyOperationalPolicyMapper().fromToscaServiceTemplate(legacyOperationalServiceTemplate);
+
+        LOGGER.debug("<-createOperationalPolicy: createdLegacyOperationalPolicy={}", createdLegacyOperationalPolicy);
+        return createdLegacyOperationalPolicy;
     }
 
     /**
@@ -123,26 +106,19 @@ public class LegacyProvider {
      * @throws PfModelException on errors updating policies
      */
     public LegacyOperationalPolicy updateOperationalPolicy(@NonNull final PfDao dao,
-            @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException {
+        @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException {
 
-        // We need to find the latest policy and use the major version, if there is no policy with this ID, then
-        // we have an error
-        ToscaPolicy newestPolicy = getLatestPolicy(dao, legacyOperationalPolicy.getPolicyId());
+        LOGGER.debug("->updateOperationalPolicy: legacyOperationalPolicy={}", legacyOperationalPolicy);
+        JpaToscaServiceTemplate incomingServiceTemplate =
+            new LegacyOperationalPolicyMapper().toToscaServiceTemplate(legacyOperationalPolicy);
+        JpaToscaServiceTemplate outgoingingServiceTemplate =
+            new SimpleToscaProvider().updatePolicies(dao, incomingServiceTemplate);
 
-        if (newestPolicy == null) {
-            String errorMessage = NO_POLICY_FOUND_FOR_POLICY_ID + legacyOperationalPolicy.getPolicyId();
-            LOGGER.warn(errorMessage);
-            throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
-        } else {
-            legacyOperationalPolicy.setPolicyVersion(Integer.toString(newestPolicy.getKey().getMajorVersion()));
-        }
+        LegacyOperationalPolicy updatedLegacyOperationalPolicy =
+            new LegacyOperationalPolicyMapper().fromToscaServiceTemplate(outgoingingServiceTemplate);
 
-        ToscaServiceTemplate incomingServiceTemplate =
-                new LegacyOperationalPolicyMapper().toToscaServiceTemplate(legacyOperationalPolicy);
-        ToscaServiceTemplate outgoingingServiceTemplate =
-                new SimpleToscaProvider().createPolicies(dao, incomingServiceTemplate);
-
-        return new LegacyOperationalPolicyMapper().fromToscaServiceTemplate(outgoingingServiceTemplate);
+        LOGGER.debug("<-updateOperationalPolicy: updatedLegacyOperationalPolicy={}", updatedLegacyOperationalPolicy);
+        return updatedLegacyOperationalPolicy;
     }
 
     /**
@@ -150,152 +126,51 @@ public class LegacyProvider {
      *
      * @param dao the DAO to use to access the database
      * @param policyId ID of the policy.
+     * @param policyVersion version of the policy.
      * @return the deleted policy
      * @throws PfModelException on errors deleting policies
      */
-    public LegacyOperationalPolicy deleteOperationalPolicy(@NonNull final PfDao dao, @NonNull final String policyId)
-            throws PfModelException {
-
-        // Get all the policies in the database and check the policy ID against the policies returned
-        List<ToscaPolicy> policyList = dao.getAll(ToscaPolicy.class);
-
-        // Find the latest policy that matches the ID
-        List<ToscaPolicy> policyDeleteList = new ArrayList<>();
-
-        for (ToscaPolicy policy : policyList) {
-            if (policyId.equals(policy.getKey().getName())) {
-                policyDeleteList.add(policy);
-            }
-        }
-
-        if (policyDeleteList.isEmpty()) {
-            String errorMessage = NO_POLICY_FOUND_FOR_POLICY_ID + policyId;
-            LOGGER.warn(errorMessage);
-            throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
-        }
-
-        // Create the structure of the TOSCA service template to contain the policy type
-        ToscaServiceTemplate serviceTemplate = new ToscaServiceTemplate();
-        serviceTemplate.setTopologyTemplate(new ToscaTopologyTemplate());
-        serviceTemplate.getTopologyTemplate().setPolicies(new ToscaPolicies());
-
-        for (ToscaPolicy deletePolicy : policyDeleteList) {
-            dao.delete(deletePolicy);
-            serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(deletePolicy.getKey(),
-                    deletePolicy);
-        }
-
-        return new LegacyOperationalPolicyMapper().fromToscaServiceTemplate(serviceTemplate);
-    }
-
-    /**
-     * Get legacy guard policy.
-     *
-     * @param dao the DAO to use to access the database
-     * @param policyId ID of the policy.
-     * @return the policies found
-     * @throws PfModelException on errors getting policies
-     */
-    public Map<String, LegacyGuardPolicyOutput> getGuardPolicy(@NonNull final PfDao dao, @NonNull final String policyId)
-            throws PfModelException {
-
-        ToscaPolicy newestPolicy = getLatestPolicy(dao, policyId);
-
-        if (newestPolicy == null) {
-            String errorMessage = NO_POLICY_FOUND_FOR_POLICY_ID + policyId;
-            LOGGER.warn(errorMessage);
-            throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
-        }
-
-        // Create the structure of the TOSCA service template to contain the policy type
-        ToscaServiceTemplate serviceTemplate = new ToscaServiceTemplate();
-        serviceTemplate.setTopologyTemplate(new ToscaTopologyTemplate());
-        serviceTemplate.getTopologyTemplate().setPolicies(new ToscaPolicies());
-        serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(newestPolicy.getKey(), newestPolicy);
-
-        return new LegacyGuardPolicyMapper().fromToscaServiceTemplate(serviceTemplate);
-    }
-
-    /**
-     * Create legacy guard policy.
-     *
-     * @param dao the DAO to use to access the database
-     * @param legacyGuardPolicy the definition of the policy to be created.
-     * @return the created policy
-     * @throws PfModelException on errors creating policies
-     */
-    public Map<String, LegacyGuardPolicyOutput> createGuardPolicy(@NonNull final PfDao dao,
-            @NonNull final LegacyGuardPolicyInput legacyGuardPolicy) throws PfModelException {
+    public LegacyOperationalPolicy deleteOperationalPolicy(@NonNull final PfDao dao, @NonNull final String policyId,
+        @NonNull final String policyVersion) throws PfModelException {
 
-        ToscaServiceTemplate incomingServiceTemplate =
-                new LegacyGuardPolicyMapper().toToscaServiceTemplate(legacyGuardPolicy);
-        ToscaServiceTemplate outgoingingServiceTemplate =
-                new SimpleToscaProvider().createPolicies(dao, incomingServiceTemplate);
+        LOGGER.debug("->deleteOperationalPolicy: policyId={}, policyVersion={}", policyId, policyVersion);
 
-        return new LegacyGuardPolicyMapper().fromToscaServiceTemplate(outgoingingServiceTemplate);
-    }
-
-    /**
-     * Update legacy guard policy.
-     *
-     * @param dao the DAO to use to access the database
-     * @param legacyGuardPolicy the definition of the policy to be updated
-     * @return the updated policy
-     * @throws PfModelException on errors updating policies
-     */
-    public Map<String, LegacyGuardPolicyOutput> updateGuardPolicy(@NonNull final PfDao dao,
-            @NonNull final LegacyGuardPolicyInput legacyGuardPolicy) throws PfModelException {
+        JpaToscaServiceTemplate deleteServiceTemplate = new SimpleToscaProvider().deletePolicy(dao,
+            new PfConceptKey(policyId, policyVersion + LEGACY_MINOR_PATCH_SUFFIX));
+        LegacyOperationalPolicy legacyOperationalPolicy =
+            new LegacyOperationalPolicyMapper().fromToscaServiceTemplate(deleteServiceTemplate);
 
-        ToscaServiceTemplate incomingServiceTemplate =
-                new LegacyGuardPolicyMapper().toToscaServiceTemplate(legacyGuardPolicy);
-        ToscaServiceTemplate outgoingingServiceTemplate =
-                new SimpleToscaProvider().createPolicies(dao, incomingServiceTemplate);
-
-        return new LegacyGuardPolicyMapper().fromToscaServiceTemplate(outgoingingServiceTemplate);
+        LOGGER.debug("<-deleteOperationalPolicy: policyId={}, policyVersion={}, legacyOperationalPolicy={}", policyId,
+            policyVersion, legacyOperationalPolicy);
+        return legacyOperationalPolicy;
     }
 
-
     /**
-     * Delete legacy guard policy.
+     * Get the JPA Policy for a policy ID and version.
      *
-     * @param dao the DAO to use to access the database
-     * @param policyId ID of the policy.
-     * @return the deleted policy
-     * @throws PfModelException on errors deleting policies
+     * @param dao The DAO to search
+     * @param policyId the policy ID to search for
+     * @param policyVersion the policy version to search for
+     * @return the JPA policy found
+     * @throws PfModelException if a policy is not found
      */
-    public Map<String, LegacyGuardPolicyOutput> deleteGuardPolicy(@NonNull final PfDao dao,
-            @NonNull final String policyId) throws PfModelException {
-
-        // Get all the policies in the database and check the policy ID against the policies returned
-        List<ToscaPolicy> policyList = dao.getAll(ToscaPolicy.class);
-
-        // Find the latest policy that matches the ID
-        List<ToscaPolicy> policyDeleteList = new ArrayList<>();
-
-        for (ToscaPolicy policy : policyList) {
-            if (policyId.equals(policy.getKey().getName())) {
-                policyDeleteList.add(policy);
-            }
+    private JpaToscaServiceTemplate getLegacyPolicy(final PfDao dao, final String policyId, final String policyVersion)
+        throws PfModelException {
+        JpaToscaServiceTemplate foundPolicyServiceTemplate = null;
+        if (policyVersion == null) {
+            foundPolicyServiceTemplate = getLatestPolicy(dao, policyId);
+        } else {
+            foundPolicyServiceTemplate =
+                new SimpleToscaProvider().getPolicies(dao, policyId, policyVersion + LEGACY_MINOR_PATCH_SUFFIX);
         }
 
-        if (policyDeleteList.isEmpty()) {
-            String errorMessage = NO_POLICY_FOUND_FOR_POLICY_ID + policyId;
+        if (foundPolicyServiceTemplate == null) {
+            String errorMessage = NO_POLICY_FOUND_FOR_POLICY + policyId + ':' + policyVersion;
             LOGGER.warn(errorMessage);
             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
         }
 
-        // Create the structure of the TOSCA service template to contain the policy type
-        ToscaServiceTemplate serviceTemplate = new ToscaServiceTemplate();
-        serviceTemplate.setTopologyTemplate(new ToscaTopologyTemplate());
-        serviceTemplate.getTopologyTemplate().setPolicies(new ToscaPolicies());
-
-        for (ToscaPolicy deletePolicy : policyDeleteList) {
-            dao.delete(deletePolicy);
-            serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(deletePolicy.getKey(),
-                    deletePolicy);
-        }
-
-        return new LegacyGuardPolicyMapper().fromToscaServiceTemplate(serviceTemplate);
+        return foundPolicyServiceTemplate;
     }
 
     /**
@@ -304,25 +179,36 @@ public class LegacyProvider {
      * @param dao The DAO to read from
      * @param policyId the ID of the policy
      * @return the policy
+     * @throws PfModelException on exceptions getting the policies
      */
-    private ToscaPolicy getLatestPolicy(final PfDao dao, final String policyId) {
+    private JpaToscaServiceTemplate getLatestPolicy(final PfDao dao, final String policyId) throws PfModelException {
         // Get all the policies in the database and check the policy ID against the policies returned
-        List<ToscaPolicy> policyList = dao.getAll(ToscaPolicy.class);
+        JpaToscaServiceTemplate serviceTemplate = new SimpleToscaProvider().getPolicies(dao, policyId, null);
+
+        if (!ToscaUtils.doPoliciesExist(serviceTemplate)) {
+            return null;
+        }
 
         // Find the latest policy that matches the ID
-        ToscaPolicy newestPolicy = null;
+        final Map<PfConceptKey, JpaToscaPolicy> policyMap =
+            serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap();
+        PfConceptKey newestPolicyKey = null;
 
-        for (ToscaPolicy policy : policyList) {
+        for (JpaToscaPolicy policy : policyMap.values()) {
             if (!policyId.equals(policy.getKey().getName())) {
                 continue;
             }
 
             // We found a matching policy
-            if (newestPolicy == null || policy.getKey().isNewerThan(newestPolicy.getKey())) {
+            if (newestPolicyKey == null || policy.getKey().isNewerThan(newestPolicyKey)) {
                 // First policy found
-                newestPolicy = policy;
+                newestPolicyKey = policy.getKey();
             }
         }
-        return newestPolicy;
+
+        final PfConceptKey newestPolicyFinalKey = newestPolicyKey;
+        policyMap.keySet().removeIf(key -> !key.equals(newestPolicyFinalKey));
+
+        return serviceTemplate;
     }
 }