aboutsummaryrefslogtreecommitdiffstats
path: root/models-tosca
diff options
context:
space:
mode:
authorliamfallon <liam.fallon@est.tech>2020-02-10 15:23:33 +0000
committerliamfallon <liam.fallon@est.tech>2020-02-10 15:23:37 +0000
commitfd79f7920d454c35d6a8c02d430d9beba434dcc2 (patch)
tree51e1d57733fbefe68b33ed5469f97596cf5dddcf /models-tosca
parentd98e90fef90fbbc79536825699d1ea980c28eb1a (diff)
Get the tree of parent entities for an entity in a container
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>
Diffstat (limited to 'models-tosca')
-rw-r--r--models-tosca/src/main/java/org/onap/policy/models/tosca/utils/ToscaUtils.java72
-rw-r--r--models-tosca/src/test/java/org/onap/policy/models/tosca/utils/ToscaUtilsTest.java123
2 files changed, 169 insertions, 26 deletions
diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/utils/ToscaUtils.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/utils/ToscaUtils.java
index fffad86e0..390692277 100644
--- a/models-tosca/src/main/java/org/onap/policy/models/tosca/utils/ToscaUtils.java
+++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/utils/ToscaUtils.java
@@ -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.
@@ -77,6 +71,15 @@ public final class ToscaUtils {
}
/**
+ * 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
@@ -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()));
}
}
diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/utils/ToscaUtilsTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/utils/ToscaUtilsTest.java
index a5568bab8..bad89e909 100644
--- a/models-tosca/src/test/java/org/onap/policy/models/tosca/utils/ToscaUtilsTest.java
+++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/utils/ToscaUtilsTest.java
@@ -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");
+ }
}