+ /**
+ * 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()));