From fd79f7920d454c35d6a8c02d430d9beba434dcc2 Mon Sep 17 00:00:00 2001 From: liamfallon Date: Mon, 10 Feb 2020 15:23:33 +0000 Subject: 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 --- .../onap/policy/models/tosca/utils/ToscaUtils.java | 72 ++++++++++++++-------- 1 file changed, 46 insertions(+), 26 deletions(-) (limited to 'models-tosca/src/main/java/org') 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 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 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 getPredefinedDataTypes() { + return PREDEFINED_TOSCA_DATA_TYPES; + } + /** * Assert that data types have been specified correctly. * @@ -140,7 +143,6 @@ public final class ToscaUtils { final Function 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> getEntityTypeAncestors( + public static Collection> getEntityTypeAncestors( @NonNull PfConceptContainer entityTypes, @NonNull JpaToscaEntityType entityType, @NonNull final PfValidationResult result) { @@ -222,7 +224,7 @@ public final class ToscaUtils { } @SuppressWarnings("unchecked") - Set> ancestorEntitySet = (Set>) entityTypes + Set> ancestorEntitySet = (Set>) 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 entityTypes, + @NonNull final PfConceptKey searchKey) { + + PfValidationResult result = new PfValidationResult(); + + @SuppressWarnings("unchecked") + Set> filteredEntitySet = + (Set>) 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())); } } -- cgit 1.2.3-korg