Merge "Parse new model ids from operation policy"
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / simple / provider / SimpleToscaProvider.java
index 6e356d0..ef8ac05 100644 (file)
 
 package org.onap.policy.models.tosca.simple.provider;
 
-import javax.ws.rs.core.Response;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
 
 import lombok.NonNull;
 
+import org.onap.policy.models.base.PfConcept;
 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.simple.concepts.JpaToscaPolicies;
 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicy;
@@ -35,8 +37,6 @@ import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyTypes;
 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
 import org.onap.policy.models.tosca.simple.concepts.JpaToscaTopologyTemplate;
 import org.onap.policy.models.tosca.utils.ToscaUtils;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 /**
  * This class provides the provision of information on TOSCA concepts in the database to callers.
@@ -44,18 +44,16 @@ import org.slf4j.LoggerFactory;
  * @author Liam Fallon (liam.fallon@est.tech)
  */
 public class SimpleToscaProvider {
-    private static final Logger LOGGER = LoggerFactory.getLogger(SimpleToscaProvider.class);
-
     /**
      * Get policy types.
      *
      * @param dao the DAO to use to access the database
-     * @param policyTypeKey the policy type key for the policy types to be retrieved. A null key name returns all policy
-     *        types. A null key version returns all versions of the policy type name specified in the key.
+     * @param name the name of the policy type to get, set to null to get all policy types
+     * @param version the version of the policy type to get, set to null to get all versions
      * @return the policy types found
      * @throws PfModelException on errors getting policy types
      */
-    public JpaToscaServiceTemplate getPolicyTypes(@NonNull final PfDao dao, @NonNull final PfConceptKey policyTypeKey)
+    public JpaToscaServiceTemplate getPolicyTypes(@NonNull final PfDao dao, final String name, final String version)
             throws PfModelException {
 
         // Create the structure of the TOSCA service template to contain the policy type
@@ -63,15 +61,10 @@ public class SimpleToscaProvider {
         serviceTemplate.setPolicyTypes(new JpaToscaPolicyTypes());
 
         // Add the policy type to the TOSCA service template
-        JpaToscaPolicyType policyType = dao.get(JpaToscaPolicyType.class, policyTypeKey);
-        if (policyType != null) {
-            serviceTemplate.getPolicyTypes().getConceptMap().put(policyTypeKey, policyType);
-            return serviceTemplate;
-        } else {
-            String errorMessage = "policy type not found: " + policyTypeKey.getId();
-            LOGGER.warn(errorMessage);
-            throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
-        }
+        List<JpaToscaPolicyType> jpaPolicyTypeList = dao.getFiltered(JpaToscaPolicyType.class, name, version);
+        serviceTemplate.getPolicyTypes().getConceptMap().putAll(asConceptMap(jpaPolicyTypeList));
+
+        return serviceTemplate;
     }
 
     /**
@@ -143,11 +136,11 @@ public class SimpleToscaProvider {
      * @return the TOSCA service template containing the policy types that were deleted
      * @throws PfModelException on errors deleting policy types
      */
-    public JpaToscaServiceTemplate deletePolicyTypes(@NonNull final PfDao dao,
-            @NonNull final PfConceptKey policyTypeKey)
+    public JpaToscaServiceTemplate deletePolicyType(@NonNull final PfDao dao, @NonNull final PfConceptKey policyTypeKey)
             throws PfModelException {
 
-        JpaToscaServiceTemplate serviceTemplate = getPolicyTypes(dao, policyTypeKey);
+        JpaToscaServiceTemplate serviceTemplate =
+                getPolicyTypes(dao, policyTypeKey.getName(), policyTypeKey.getVersion());
 
         dao.delete(JpaToscaPolicyType.class, policyTypeKey);
 
@@ -158,12 +151,12 @@ public class SimpleToscaProvider {
      * Get policies.
      *
      * @param dao the DAO to use to access the database
-     * @param policyKey the policy key for the policies to be retrieved. The parent name and version must be specified.
-     *        A null local name returns all policies for a parent policy type.
+     * @param name the name of the policy to get, set to null to get all policy types
+     * @param version the version of the policy to get, set to null to get all versions
      * @return the policies found
      * @throws PfModelException on errors getting policies
      */
-    public JpaToscaServiceTemplate getPolicies(@NonNull final PfDao dao, @NonNull final PfConceptKey policyKey)
+    public JpaToscaServiceTemplate getPolicies(@NonNull final PfDao dao, final String name, final String version)
             throws PfModelException {
 
         // Create the structure of the TOSCA service template to contain the policy type
@@ -171,16 +164,10 @@ public class SimpleToscaProvider {
         serviceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate());
         serviceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies());
 
-        // Add the policy to the TOSCA service template
-        JpaToscaPolicy policy = dao.get(JpaToscaPolicy.class, policyKey);
-        if (policy != null) {
-            serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(policyKey, policy);
-            return serviceTemplate;
-        } else {
-            String errorMessage = "policy not found: " + policyKey.getId();
-            LOGGER.warn(errorMessage);
-            throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
-        }
+        // Add the policy type to the TOSCA service template
+        List<JpaToscaPolicy> jpaPolicyList = dao.getFiltered(JpaToscaPolicy.class, name, version);
+        serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().putAll(asConceptMap(jpaPolicyList));
+        return serviceTemplate;
     }
 
     /**
@@ -251,13 +238,28 @@ public class SimpleToscaProvider {
      * @return the TOSCA service template containing the policies that were deleted
      * @throws PfModelException on errors deleting policies
      */
-    public JpaToscaServiceTemplate deletePolicies(@NonNull final PfDao dao, @NonNull final PfConceptKey policyKey)
+    public JpaToscaServiceTemplate deletePolicy(@NonNull final PfDao dao, @NonNull final PfConceptKey policyKey)
             throws PfModelException {
 
-        JpaToscaServiceTemplate serviceTemplate = getPolicies(dao, policyKey);
+        JpaToscaServiceTemplate serviceTemplate = getPolicies(dao, policyKey.getName(), policyKey.getVersion());
 
         dao.delete(JpaToscaPolicy.class, policyKey);
 
         return serviceTemplate;
     }
+
+    /**
+     * Convert a list of concepts to a map of concepts.
+     *
+     * @param conceptList the concept list
+     * @return the concept map
+     */
+    private <T extends PfConcept> Map<PfConceptKey, T> asConceptMap(List<T> conceptList) {
+        Map<PfConceptKey, T> conceptMap = new LinkedHashMap<>();
+        for (T concept : conceptList) {
+            conceptMap.put((PfConceptKey) concept.getKey(), concept);
+        }
+
+        return conceptMap;
+    }
 }