Implement validation and hierarchical get
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / utils / ToscaUtils.java
index a02bfa4..cc04319 100644 (file)
@@ -1,6 +1,6 @@
 /*-
  * ============LICENSE_START=======================================================
- *  Copyright (C) 2019 Nordix Foundation.
+ *  Copyright (C) 2019-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.models.tosca.utils;
 
+import java.util.Collection;
+import java.util.Set;
+import java.util.function.Function;
+
 import javax.ws.rs.core.Response;
 
+import lombok.NonNull;
+
+import org.apache.commons.collections4.CollectionUtils;
+import org.onap.policy.models.base.PfConcept;
+import org.onap.policy.models.base.PfConceptContainer;
+import org.onap.policy.models.base.PfConceptKey;
+import org.onap.policy.models.base.PfKey;
 import org.onap.policy.models.base.PfModelRuntimeException;
-import org.onap.policy.models.tosca.simple.concepts.ToscaServiceTemplate;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+import org.onap.policy.models.base.PfNameVersion;
+import org.onap.policy.models.base.PfValidationMessage;
+import org.onap.policy.models.base.PfValidationResult;
+import org.onap.policy.models.base.PfValidationResult.ValidationResult;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity;
+import org.onap.policy.models.tosca.simple.concepts.JpaToscaEntityType;
+import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
 
 /**
  * Utility class for TOSCA concepts.
@@ -33,53 +48,225 @@ import org.slf4j.LoggerFactory;
  * @author Liam Fallon (liam.fallon@est.tech)
  */
 public final class ToscaUtils {
-    private static final Logger LOGGER = LoggerFactory.getLogger(ToscaUtils.class);
+    private static final String ROOT_KEY_NAME_SUFFIX = ".Root";
+
+    // @formatter:off
+    private static final Set<PfConceptKey> PREDEFINED_TOSCA_DATA_TYPES = Set.of(
+            new PfConceptKey("string",                       PfKey.NULL_KEY_VERSION),
+            new PfConceptKey("integer",                      PfKey.NULL_KEY_VERSION),
+            new PfConceptKey("float",                        PfKey.NULL_KEY_VERSION),
+            new PfConceptKey("boolean",                      PfKey.NULL_KEY_VERSION),
+            new PfConceptKey("timestamp",                    PfKey.NULL_KEY_VERSION),
+            new PfConceptKey("null",                         PfKey.NULL_KEY_VERSION),
+            new PfConceptKey("list",                         PfKey.NULL_KEY_VERSION),
+            new PfConceptKey("map",                          PfKey.NULL_KEY_VERSION),
+            new PfConceptKey("scalar-unit.size",             PfKey.NULL_KEY_VERSION),
+            new PfConceptKey("scalar-unit.time",             PfKey.NULL_KEY_VERSION),
+            new PfConceptKey("scalar-unit.frequency",        PfKey.NULL_KEY_VERSION),
+            new PfConceptKey("tosca.datatypes.TimeInterval", PfKey.NULL_KEY_VERSION)
+        );
+    // @formatter:on
 
     /**
      * Private constructor to prevent subclassing.
      */
     private ToscaUtils() {
+        // Private constructor to prevent subclassing
+    }
+
+    /**
+     * Get the predefined policy types.
+     *
+     * @return the predefined policy types
+     */
+    public static Collection<PfConceptKey> getPredefinedDataTypes() {
+        return PREDEFINED_TOSCA_DATA_TYPES;
+    }
+
+    /**
+     * Assert that data types have been specified correctly.
+     *
+     * @param serviceTemplate the service template containing data types to be checked
+     */
+    public static void assertDataTypesExist(final JpaToscaServiceTemplate serviceTemplate) {
+        assertExist(serviceTemplate, ToscaUtils::checkDataTypesExist);
+    }
+
+    /**
+     * Assert that policy types have been specified correctly.
+     *
+     * @param serviceTemplate the service template containing policy types to be checked
+     */
+    public static void assertPolicyTypesExist(final JpaToscaServiceTemplate serviceTemplate) {
+        assertExist(serviceTemplate, ToscaUtils::checkPolicyTypesExist);
+    }
+
+    /**
+     * Assert that policies have been specified correctly.
+     *
+     * @param serviceTemplate the service template containing policy types to be checked
+     */
+    public static void assertPoliciesExist(final JpaToscaServiceTemplate serviceTemplate) {
+        assertExist(serviceTemplate, ToscaUtils::checkPoliciesExist);
+    }
+
+    /**
+     * Check that data types have been specified correctly.
+     *
+     * @param serviceTemplate the service template containing data types to be checked
+     */
+    public static boolean doDataTypesExist(final JpaToscaServiceTemplate serviceTemplate) {
+        return doExist(serviceTemplate, ToscaUtils::checkDataTypesExist);
+    }
+
+    /**
+     * Check that policy types have been specified correctly.
+     *
+     * @param serviceTemplate the service template containing policy types to be checked
+     */
+    public static boolean doPolicyTypesExist(final JpaToscaServiceTemplate serviceTemplate) {
+        return doExist(serviceTemplate, ToscaUtils::checkPolicyTypesExist);
+    }
+
+    /**
+     * Check that policies have been specified correctly.
+     *
+     * @param serviceTemplate the service template containing policy types to be checked
+     */
+    public static boolean doPoliciesExist(final JpaToscaServiceTemplate serviceTemplate) {
+
+        return doExist(serviceTemplate, ToscaUtils::checkPoliciesExist);
+    }
+
+    /**
+     * Assert that something have been specified correctly.
+     *
+     * @param serviceTemplate the service template containing policy types to be checked
+     */
+    public static void assertExist(final JpaToscaServiceTemplate serviceTemplate,
+            final Function<JpaToscaServiceTemplate, String> checkerFunction) {
+        String message = checkerFunction.apply(serviceTemplate);
+        if (message != null) {
+            throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, message);
+        }
     }
 
     /**
-     * Check if policy types have been specified is initialized.
+     * Check that something have been specified correctly.
+     *
+     * @param serviceTemplate the service template containing policy types to be checked
      */
-    public static void assertPolicyTypesExist(final ToscaServiceTemplate serviceTemplate) {
+    public static boolean doExist(final JpaToscaServiceTemplate serviceTemplate,
+            final Function<JpaToscaServiceTemplate, String> checkerFunction) {
+        return checkerFunction.apply(serviceTemplate) == null;
+    }
+
+    /**
+     * Check if data types have been specified correctly.
+     */
+    public static String checkDataTypesExist(final JpaToscaServiceTemplate serviceTemplate) {
+        if (serviceTemplate.getDataTypes() == null) {
+            return "no data types specified on service template";
+        }
+
+        if (serviceTemplate.getDataTypes().getConceptMap().isEmpty()) {
+            return "list of data types specified on service template is empty";
+        }
+
+        return null;
+    }
+
+    /**
+     * Check if policy types have been specified correctly.
+     */
+    public static String checkPolicyTypesExist(final JpaToscaServiceTemplate serviceTemplate) {
         if (serviceTemplate.getPolicyTypes() == null) {
-            String errorMessage = "no policy types specified on service template";
-            LOGGER.warn(errorMessage);
-            throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
+            return "no policy types specified on service template";
         }
 
         if (serviceTemplate.getPolicyTypes().getConceptMap().isEmpty()) {
-            String errorMessage = "list of policy types specified on service template is empty";
-            LOGGER.warn(errorMessage);
-            throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
+            return "list of policy types specified on service template is empty";
         }
+
+        return null;
     }
 
     /**
-     * Check if policy types have been specified is initialized.
+     * Check if policies have been specified correctly.
      */
-    public static void assertPoliciesExist(final ToscaServiceTemplate serviceTemplate) {
+    public static String checkPoliciesExist(final JpaToscaServiceTemplate serviceTemplate) {
         if (serviceTemplate.getTopologyTemplate() == null) {
-            String errorMessage = "topology template not specified on service template";
-            LOGGER.warn(errorMessage);
-            throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
+            return "topology template not specified on service template";
         }
 
         if (serviceTemplate.getTopologyTemplate().getPolicies() == null) {
-            String errorMessage = "no policies specified on topology template of service template";
-            LOGGER.warn(errorMessage);
-            throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
+            return "no policies specified on topology template of service template";
         }
 
         if (serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().isEmpty()) {
-            String errorMessage = "list of policies specified on topology template of service template is empty";
-            LOGGER.warn(errorMessage);
-            throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
+            return "list of policies specified on topology template of service template is empty";
+        }
+
+        return null;
+    }
+
+    /**
+     * Find all the ancestors of an entity type.
+     *
+     * @param entityTypes the set of entity types that exist
+     * @param entityType the entity type for which to get the parents
+     * @param result the result of the ancestor search with any warnings or errors
+     * @return the entity set containing the ancestors of the incoming entity
+     */
+    public static Collection<JpaToscaEntityType<ToscaEntity>> getEntityTypeAncestors(
+            @NonNull PfConceptContainer<? extends PfConcept, ? extends PfNameVersion> entityTypes,
+            @NonNull JpaToscaEntityType<?> entityType, @NonNull final PfValidationResult result) {
+
+        PfConceptKey parentEntityTypeKey = entityType.getDerivedFrom();
+        if (parentEntityTypeKey == null || parentEntityTypeKey.getName().endsWith(ROOT_KEY_NAME_SUFFIX)) {
+            return CollectionUtils.emptyCollection();
+        }
+
+        @SuppressWarnings("unchecked")
+        Set<JpaToscaEntityType<ToscaEntity>> ancestorEntitySet = (Set<JpaToscaEntityType<ToscaEntity>>) entityTypes
+                .getAll(parentEntityTypeKey.getName(), parentEntityTypeKey.getVersion());
+
+        if (ancestorEntitySet.isEmpty()) {
+            result.addValidationMessage(new PfValidationMessage(entityType.getKey(), ToscaUtils.class,
+                    ValidationResult.INVALID, "parent " + parentEntityTypeKey.getId() + " of entity not found"));
+        } else {
+            for (JpaToscaEntityType<?> filteredEntityType : ancestorEntitySet) {
+                ancestorEntitySet.addAll(getEntityTypeAncestors(entityTypes, filteredEntityType, result));
+            }
         }
+        return ancestorEntitySet;
     }
 
+    /**
+     * Get the entity tree from a concept container for a given entity key.
+     *
+     * @param entityTypes the concept container containing entity types
+     * @param entityName the name of the entity
+     * @param entityVersion the version of the entity
+     */
+    public static void getEntityTree(
+            @NonNull final PfConceptContainer<? extends PfConcept, ? extends PfNameVersion> entityTypes,
+            final String entityName, final String entityVersion) {
+
+        PfValidationResult result = new PfValidationResult();
+
+        @SuppressWarnings("unchecked")
+        Set<JpaToscaEntityType<?>> filteredEntitySet =
+                (Set<JpaToscaEntityType<?>>) entityTypes.getAll(entityName, entityVersion);
+        for (JpaToscaEntityType<?> filteredEntityType : filteredEntitySet) {
+            filteredEntitySet.addAll(ToscaUtils.getEntityTypeAncestors(entityTypes, filteredEntityType, result));
+        }
+
+        if (!result.isValid()) {
+            throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, result.toString());
+        }
 
+        entityTypes.getConceptMap().entrySet()
+                .removeIf(entityEntry -> !filteredEntitySet.contains(entityEntry.getValue()));
+    }
 }