Get the tree of parent entities for an entity in a container 85/101485/1
authorliamfallon <liam.fallon@est.tech>
Mon, 10 Feb 2020 15:23:33 +0000 (15:23 +0000)
committerliamfallon <liam.fallon@est.tech>
Mon, 10 Feb 2020 15:23:37 +0000 (15:23 +0000)
Utilityu method to filter out all bit an entity and its parents from a
service template entity container.

Issue-ID: POLICY-1402
Change-Id: I8c3b4c21b27fd8787cdfde0e1b726646f52c437b
Signed-off-by: liamfallon <liam.fallon@est.tech>
models-examples/src/main/resources/policies/vCPE.policy.monitoring.input.tosca.yaml
models-examples/src/main/resources/policies/vDNS.policy.monitoring.input.tosca.json
models-examples/src/main/resources/policies/vDNS.policy.monitoring.input.tosca.yaml
models-examples/src/main/resources/policies/vFirewall.policy.monitoring.input.tosca.json
models-tosca/src/main/java/org/onap/policy/models/tosca/utils/ToscaUtils.java
models-tosca/src/test/java/org/onap/policy/models/tosca/utils/ToscaUtilsTest.java

index 74961ef..7f01101 100644 (file)
@@ -11,15 +11,15 @@ topology_template:
            properties:
              tca_policy:
                 domain: measurementsForVfScaling
-                metricsPerEventName: 
-                  - 
+                metricsPerEventName:
+                  -
                     eventName: Measurement_vGMUX
                     controlLoopSchemaType: VNF
                     policyScope: DCAE
                     policyName: "DCAE.Config_tca-hi-lo"
                     policyVersion: "v0.0.1"
-                    thresholds: 
-                      - 
+                    thresholds:
+                      -
                         closedLoopControlName: "ControlLoop-vCPE-48f0c2c3-a172-4192-9ae3-052274181b6e"
                         version: "1.0.2"
                         fieldPath: "$.event.measurementsForVfScalingFields.additionalMeasurements[*].arrayOfFields[0].value"
@@ -27,7 +27,7 @@ topology_template:
                         direction: EQUAL
                         severity: MAJOR
                         closedLoopEventStatus: ABATED
-                      - 
+                      -
                         closedLoopControlName: "ControlLoop-vCPE-48f0c2c3-a172-4192-9ae3-052274181b6e"
                         version: "1.0.2"
                         fieldPath: "$.event.measurementsForVfScalingFields.additionalMeasurements[*].arrayOfFields[0].value"
index 19b0e8e..bb31f4b 100644 (file)
@@ -1,26 +1,26 @@
 {
     "tosca_definitions_version": "tosca_simple_yaml_1_0_0",
-    "topology_template": 
+    "topology_template":
     {
-        "policies": 
+        "policies":
         [
             {
-                "onap.scaleout.tca": 
+                "onap.scaleout.tca":
                 {
                     "type": "onap.policies.monitoring.cdap.tca.hi.lo.app",
                     "type_version": "1.0.0",
                     "version": "1.0.0",
-                    "metadata": 
+                    "metadata":
                     {
                         "policy-id": "onap.scaleout.tca"
                     },
 
-                    "properties": 
+                    "properties":
                     {
-                        "tca_policy": 
+                        "tca_policy":
                         {
                             "domain": "measurementsForVfScaling",
-                            "metricsPerEventName": 
+                            "metricsPerEventName":
                             [
                                 {
                                     "eventName": "vLoadBalancer",
@@ -28,7 +28,7 @@
                                     "policyScope": "type=configuration",
                                     "policyName": "onap.scaleout.tca",
                                     "policyVersion": "v0.0.1",
-                                    "thresholds": 
+                                    "thresholds":
                                     [
                                         {
                                             "closedLoopControlName": "ControlLoop-vDNS-6f37f56d-a87d-4b85-b6a9-cc953cf779b3",
index 4cf17cc..d8e37da 100644 (file)
@@ -19,7 +19,7 @@ topology_template:
                         policyName: "onap.scaleout.tca"
                         policyVersion: "v0.0.1"
                         thresholds:
-                        - 
+                        -
                            closedLoopControlName: "ControlLoop-vDNS-6f37f56d-a87d-4b85-b6a9-cc953cf779b3"
                            closedLoopEventStatus: ONSET
                            version: "1.0.2"
index f96e26d..b247672 100644 (file)
@@ -1,26 +1,26 @@
 {
     "tosca_definitions_version": "tosca_simple_yaml_1_0_0",
-    "topology_template": 
+    "topology_template":
     {
-        "policies": 
+        "policies":
         [
             {
-                "onap.vfirewall.tca": 
+                "onap.vfirewall.tca":
                 {
                     "type": "onap.policies.monitoring.cdap.tca.hi.lo.app",
                     "type_version": "1.0.0",
                     "version": "1.0.0",
-                    "metadata": 
+                    "metadata":
                     {
                         "policy-id": "onap.vfirewall.tca"
                     },
 
-                    "properties": 
+                    "properties":
                     {
-                        "tca_policy": 
+                        "tca_policy":
                         {
                             "domain": "measurementsForVfScaling",
-                            "metricsPerEventName": 
+                            "metricsPerEventName":
                             [
                                 {
                                     "eventName": "vLoadBalancer",
@@ -28,7 +28,7 @@
                                     "policyScope": "resource=vLoadBalancer;type=configuration",
                                     "policyName": "onap.vfirewall.tca",
                                     "policyVersion": "v0.0.1",
-                                    "thresholds": 
+                                    "thresholds":
                                     [
                                         {
                                             "closedLoopControlName": "ControlLoop-vFirewall-d0a1dfc6-94f5-4fd4-a5b5-4630b438850a",
index fffad86..3906922 100644 (file)
@@ -21,7 +21,6 @@
 package org.onap.policy.models.tosca.utils;
 
 import java.util.Collection;
-import java.util.LinkedHashSet;
 import java.util.Set;
 import java.util.function.Function;
 
@@ -39,10 +38,9 @@ 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;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 /**
  * Utility class for TOSCA concepts.
@@ -50,24 +48,20 @@ 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";
 
-    private static final Set<PfConceptKey> PREDEFINED_TOSCA_DATA_TYPES = new LinkedHashSet<>();
-
-    // @formatter:off
-    static {
-        PREDEFINED_TOSCA_DATA_TYPES.add(new PfConceptKey("string",    PfKey.NULL_KEY_VERSION));
-        PREDEFINED_TOSCA_DATA_TYPES.add(new PfConceptKey("integer",   PfKey.NULL_KEY_VERSION));
-        PREDEFINED_TOSCA_DATA_TYPES.add(new PfConceptKey("float",     PfKey.NULL_KEY_VERSION));
-        PREDEFINED_TOSCA_DATA_TYPES.add(new PfConceptKey("boolean",   PfKey.NULL_KEY_VERSION));
-        PREDEFINED_TOSCA_DATA_TYPES.add(new PfConceptKey("timestamp", PfKey.NULL_KEY_VERSION));
-        PREDEFINED_TOSCA_DATA_TYPES.add(new PfConceptKey("null",      PfKey.NULL_KEY_VERSION));
-        PREDEFINED_TOSCA_DATA_TYPES.add(new PfConceptKey("list",      PfKey.NULL_KEY_VERSION));
-        PREDEFINED_TOSCA_DATA_TYPES.add(new PfConceptKey("map",       PfKey.NULL_KEY_VERSION));
-    }
     // @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)
+        );
+    // @formatter:on
 
     /**
      * Private constructor to prevent subclassing.
@@ -76,6 +70,15 @@ public final class 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.
      *
@@ -140,7 +143,6 @@ public final class ToscaUtils {
             final Function<JpaToscaServiceTemplate, String> checkerFunction) {
         String message = checkerFunction.apply(serviceTemplate);
         if (message != null) {
-            LOGGER.warn(message);
             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, message);
         }
     }
@@ -210,9 +212,9 @@ public final class ToscaUtils {
      * @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
+     * @return the entity set containing the ancestors of the incoming entity
      */
-    public static Collection<? extends JpaToscaEntityType<?>> getEntityTypeAncestors(
+    public static Collection<JpaToscaEntityType<ToscaEntity>> getEntityTypeAncestors(
             @NonNull PfConceptContainer<? extends PfConcept, ? extends PfNameVersion> entityTypes,
             @NonNull JpaToscaEntityType<?> entityType, @NonNull final PfValidationResult result) {
 
@@ -222,7 +224,7 @@ public final class ToscaUtils {
         }
 
         @SuppressWarnings("unchecked")
-        Set<JpaToscaEntityType<?>> ancestorEntitySet = (Set<JpaToscaEntityType<?>>) entityTypes
+        Set<JpaToscaEntityType<ToscaEntity>> ancestorEntitySet = (Set<JpaToscaEntityType<ToscaEntity>>) entityTypes
                 .getAll(parentEntityTypeKey.getName(), parentEntityTypeKey.getVersion());
 
         if (ancestorEntitySet.isEmpty()) {
@@ -237,11 +239,29 @@ public final class ToscaUtils {
     }
 
     /**
-     * Get the predefined policy types.
+     * Get the entity tree from a concept container for a given entity key.
      *
-     * @return the predefined policy types
+     * @param entityTypes the concept container containing entity types
+     * @param searchKey the key to search for
      */
-    public static Collection<?> getPredefinedDataTypes() {
-        return PREDEFINED_TOSCA_DATA_TYPES;
+    public static void getEntityTree(
+            @NonNull final PfConceptContainer<? extends PfConcept, ? extends PfNameVersion> entityTypes,
+            @NonNull final PfConceptKey searchKey) {
+
+        PfValidationResult result = new PfValidationResult();
+
+        @SuppressWarnings("unchecked")
+        Set<JpaToscaEntityType<?>> filteredEntitySet =
+                (Set<JpaToscaEntityType<?>>) entityTypes.getAll(searchKey.getName(), searchKey.getVersion());
+        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()));
     }
 }
index a5568ba..bad89e9 100644 (file)
@@ -24,6 +24,7 @@ import static org.assertj.core.api.Assertions.assertThatCode;
 import static org.assertj.core.api.Assertions.assertThatThrownBy;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotEquals;
 import static org.junit.Assert.assertTrue;
 
 import org.junit.Test;
@@ -248,4 +249,126 @@ public class ToscaUtilsTest {
     public void testGetPredefinedDataTypes() {
         assertTrue(ToscaUtils.getPredefinedDataTypes().contains(new PfConceptKey("string", PfKey.NULL_KEY_VERSION)));
     }
+
+    @Test
+    public void testgetEntityTree() {
+        assertThatThrownBy(() -> {
+            ToscaUtils.getEntityTree(null, null);
+        }).hasMessageMatching("entityTypes is marked .*on.*ull but is null");
+
+        assertThatThrownBy(() -> {
+            ToscaUtils.getEntityTree(null, new PfConceptKey());
+        }).hasMessageMatching("entityTypes is marked .*on.*ull but is null");
+
+        assertThatThrownBy(() -> {
+            ToscaUtils.getEntityTree(new JpaToscaDataTypes(), null);
+        }).hasMessageMatching("searchKey is marked .*on.*ull but is null");
+
+        JpaToscaDataTypes dataTypes = new JpaToscaDataTypes(new PfConceptKey("datatypes", "0.0.1"));
+        JpaToscaDataTypes filteredDataTypes = new JpaToscaDataTypes(new PfConceptKey("datatypes", "0.0.1"));
+        ToscaUtils.getEntityTree(filteredDataTypes, new PfConceptKey());
+        assertEquals(dataTypes, filteredDataTypes);
+
+        JpaToscaDataType dt0 = new JpaToscaDataType(new PfConceptKey("dt0", "0.0.1"));
+        dataTypes.getConceptMap().put(dt0.getKey(), dt0);
+        filteredDataTypes.getConceptMap().put(dt0.getKey(), dt0);
+        ToscaUtils.getEntityTree(filteredDataTypes, new PfConceptKey());
+        assertNotEquals(dataTypes, filteredDataTypes);
+        assertTrue(filteredDataTypes.getConceptMap().isEmpty());
+
+        filteredDataTypes.getConceptMap().put(dt0.getKey(), dt0);
+        ToscaUtils.getEntityTree(filteredDataTypes, dt0.getKey());
+        assertEquals(dataTypes, filteredDataTypes);
+
+        JpaToscaDataType dt1 = new JpaToscaDataType(new PfConceptKey("dt1", "0.0.1"));
+        dt1.setDerivedFrom(dt0.getKey());
+
+        JpaToscaDataType dt2 = new JpaToscaDataType(new PfConceptKey("dt2", "0.0.1"));
+        dt2.setDerivedFrom(dt0.getKey());
+
+        JpaToscaDataType dt3 = new JpaToscaDataType(new PfConceptKey("dt3", "0.0.1"));
+        dt3.setDerivedFrom(dt0.getKey());
+
+        JpaToscaDataType dt4 = new JpaToscaDataType(new PfConceptKey("dt4", "0.0.1"));
+        dt4.setDerivedFrom(dt3.getKey());
+
+        JpaToscaDataType dt5 = new JpaToscaDataType(new PfConceptKey("dt5", "0.0.1"));
+        dt5.setDerivedFrom(dt4.getKey());
+
+        JpaToscaDataType dt6 = new JpaToscaDataType(new PfConceptKey("dt6", "0.0.1"));
+        dt6.setDerivedFrom(dt5.getKey());
+
+        JpaToscaDataType dt7 = new JpaToscaDataType(new PfConceptKey("dt7", "0.0.1"));
+
+        JpaToscaDataType dt8 = new JpaToscaDataType(new PfConceptKey("dt8", "0.0.1"));
+        dt8.setDerivedFrom(dt7.getKey());
+
+        JpaToscaDataType dt9 = new JpaToscaDataType(new PfConceptKey("dt9", "0.0.1"));
+        dt9.setDerivedFrom(dt8.getKey());
+
+        dataTypes.getConceptMap().put(dt0.getKey(), dt0);
+        dataTypes.getConceptMap().put(dt1.getKey(), dt1);
+        dataTypes.getConceptMap().put(dt2.getKey(), dt2);
+        dataTypes.getConceptMap().put(dt3.getKey(), dt3);
+        dataTypes.getConceptMap().put(dt4.getKey(), dt4);
+        dataTypes.getConceptMap().put(dt5.getKey(), dt5);
+        dataTypes.getConceptMap().put(dt6.getKey(), dt6);
+        dataTypes.getConceptMap().put(dt7.getKey(), dt7);
+        dataTypes.getConceptMap().put(dt8.getKey(), dt8);
+        dataTypes.getConceptMap().put(dt9.getKey(), dt9);
+
+        ToscaUtils.getEntityTree(filteredDataTypes, dt0.getKey());
+        assertEquals(1, filteredDataTypes.getConceptMap().size());
+
+        filteredDataTypes = new JpaToscaDataTypes(dataTypes);
+        ToscaUtils.getEntityTree(filteredDataTypes, dt1.getKey());
+        assertEquals(2, filteredDataTypes.getConceptMap().size());
+
+        filteredDataTypes = new JpaToscaDataTypes(dataTypes);
+        ToscaUtils.getEntityTree(filteredDataTypes, dt2.getKey());
+        assertEquals(2, filteredDataTypes.getConceptMap().size());
+
+        filteredDataTypes = new JpaToscaDataTypes(dataTypes);
+        ToscaUtils.getEntityTree(filteredDataTypes, dt3.getKey());
+        assertEquals(2, filteredDataTypes.getConceptMap().size());
+
+        filteredDataTypes = new JpaToscaDataTypes(dataTypes);
+        ToscaUtils.getEntityTree(filteredDataTypes, dt4.getKey());
+        assertEquals(3, filteredDataTypes.getConceptMap().size());
+
+        filteredDataTypes = new JpaToscaDataTypes(dataTypes);
+        ToscaUtils.getEntityTree(filteredDataTypes, dt5.getKey());
+        assertEquals(4, filteredDataTypes.getConceptMap().size());
+
+        filteredDataTypes = new JpaToscaDataTypes(dataTypes);
+        ToscaUtils.getEntityTree(filteredDataTypes, dt6.getKey());
+        assertEquals(5, filteredDataTypes.getConceptMap().size());
+        assertTrue(filteredDataTypes.getConceptMap().containsValue(dt0));
+        assertFalse(filteredDataTypes.getConceptMap().containsValue(dt1));
+        assertFalse(filteredDataTypes.getConceptMap().containsValue(dt2));
+        assertTrue(filteredDataTypes.getConceptMap().containsValue(dt3));
+        assertTrue(filteredDataTypes.getConceptMap().containsValue(dt4));
+        assertTrue(filteredDataTypes.getConceptMap().containsValue(dt5));
+        assertTrue(filteredDataTypes.getConceptMap().containsValue(dt6));
+
+        filteredDataTypes = new JpaToscaDataTypes(dataTypes);
+        ToscaUtils.getEntityTree(filteredDataTypes, dt7.getKey());
+        assertEquals(1, filteredDataTypes.getConceptMap().size());
+
+        filteredDataTypes = new JpaToscaDataTypes(dataTypes);
+        ToscaUtils.getEntityTree(filteredDataTypes, dt8.getKey());
+        assertEquals(2, filteredDataTypes.getConceptMap().size());
+
+        filteredDataTypes = new JpaToscaDataTypes(dataTypes);
+        ToscaUtils.getEntityTree(filteredDataTypes, dt9.getKey());
+        assertEquals(3, filteredDataTypes.getConceptMap().size());
+
+        dt9.setDerivedFrom(new PfConceptKey("i.dont.Exist", "0.0.0"));
+        filteredDataTypes = new JpaToscaDataTypes(dataTypes);
+
+        assertThatThrownBy(() -> {
+            final JpaToscaDataTypes badDataTypes = new JpaToscaDataTypes(dataTypes);
+            ToscaUtils.getEntityTree(badDataTypes, dt9.getKey());
+        }).hasMessageContaining("parent i.dont.Exist:0.0.0 of entity not found");
+    }
 }