diff options
author | Manzon, Inna (im453s) <im453s@intl.att.com> | 2018-12-25 17:41:33 +0200 |
---|---|---|
committer | Manzon, Inna (im453s) <im453s@intl.att.com> | 2019-01-07 10:27:45 +0200 |
commit | 16cfd402db3048735615f11751c512386dfc0382 (patch) | |
tree | e95778eaefdb78ad72b744935f94ec92471f3dae /src/main/java/org/onap | |
parent | e502e8a5b21819ddaceb7c7d3ab387467231cc3c (diff) |
SDC Tosca Parser getEntity API
Change-Id: I4232c4ca85432bf9c81a81d717bee23b187182aa
Issue-ID: SDC-1967
Signed-off-by: Manzon, Inna (im453s) <im453s@intl.att.com>
Diffstat (limited to 'src/main/java/org/onap')
17 files changed, 566 insertions, 84 deletions
diff --git a/src/main/java/org/onap/sdc/tosca/parser/api/IEntityDetails.java b/src/main/java/org/onap/sdc/tosca/parser/api/IEntityDetails.java index f8e8c6e..c4393cc 100644 --- a/src/main/java/org/onap/sdc/tosca/parser/api/IEntityDetails.java +++ b/src/main/java/org/onap/sdc/tosca/parser/api/IEntityDetails.java @@ -1,20 +1,76 @@ package org.onap.sdc.tosca.parser.api; import org.onap.sdc.tosca.parser.enums.EntityTemplateType; +import org.onap.sdc.toscaparser.api.CapabilityAssignment; import org.onap.sdc.toscaparser.api.Property; +import org.onap.sdc.toscaparser.api.RequirementAssignment; import org.onap.sdc.toscaparser.api.elements.Metadata; import java.util.List; import java.util.Map; public interface IEntityDetails { + /** + * Retrieves entity instance template type. + * @return {@link EntityTemplateType} enum entry describing given object type + */ EntityTemplateType getType(); + + /** + * Retrieves entity instance name + */ String getName(); + + /** + * Retrieves entity {@link Metadata} object + */ Metadata getMetadata(); + + /** + * Retrieves entity instance properties + * @return map of entity property names and corresponding {@link Property} object instances + */ Map<String, Property> getProperties(); - List<Property> getPropertyList(); + + /** + * Retrieves member nodes of the entity instance + * @return List of member nodes entity objects + */ List<IEntityDetails> getMemberNodes(); + + /** + * Retrieves node template containing the current entity instance. + * @return parent entity instance or null if the entity is contained by service + */ IEntityDetails getParent(); + + /** + * Retrieves path to the searched entity instance in the model. It is based on the collection of the node templates names delimited by #. + * The entity instance may exist in the service node templates, in the VF node templates or in the nested of nested CVFC. + * The path will include the VF name, then the names of the CVFC recursively. + * If the entity instance is located in the service directly, the path is empty string + */ String getPath(); -} + /** + * Retrieves map of requirements of the entity instance + * @return map of entity requirement names and corresponding {@link RequirementAssignment} object instances + */ + Map<String, RequirementAssignment> getRequirements(); + + /** + * Retrieves map of capabilities of the entity instance + * @return map of entity capability names and corresponding {@link CapabilityAssignment} object instances + */ + Map<String, CapabilityAssignment> getCapabilities(); + + /** + * Retrieves list of policy target names + */ + List<String> getTargets(); + + /** + * Retrieves list of policy target entity instances (groups or node templates) + */ + List<IEntityDetails> getTargetNodes(); +}
\ No newline at end of file diff --git a/src/main/java/org/onap/sdc/tosca/parser/api/ISdcCsarHelper.java b/src/main/java/org/onap/sdc/tosca/parser/api/ISdcCsarHelper.java index b64d0de..129f7b4 100644 --- a/src/main/java/org/onap/sdc/tosca/parser/api/ISdcCsarHelper.java +++ b/src/main/java/org/onap/sdc/tosca/parser/api/ISdcCsarHelper.java @@ -42,6 +42,7 @@ public interface ISdcCsarHelper { * @param nodeType - the TOSCA type of the node. * @return service node templates of this type. */ + @Deprecated public List<NodeTemplate> getServiceNodeTemplatesByType(String nodeType); /** @@ -49,6 +50,7 @@ public interface ISdcCsarHelper { * * @return service node templates. */ + @Deprecated public List<NodeTemplate> getServiceNodeTemplates(); /** @@ -57,6 +59,7 @@ public interface ISdcCsarHelper { * @param vfCustomizationUuid - customizationUuid of VF instance. * @return list of vfModule groups. */ + @Deprecated public List<Group> getVfModulesByVf(String vfCustomizationUuid); @@ -84,6 +87,7 @@ public interface ISdcCsarHelper { * @param pathToPropertyLeafValue - the full path of the required property. * @return the leaf value as String, or null if there's no such property, or it's not a leaf. */ + @Deprecated public String getNodeTemplatePropertyLeafValue(NodeTemplate nodeTemplate, String pathToPropertyLeafValue); /** @@ -110,6 +114,7 @@ public interface ISdcCsarHelper { * @param pathToPropertyLeafValue - the full path of the required property. * @return the leaf value as Object, or null if there's no such property. It's up to the caller to cast it to a proper type. */ + @Deprecated public Object getNodeTemplatePropertyValueAsObject(NodeTemplate nodeTemplate, String pathToPropertyLeafValue); /** @@ -119,6 +124,7 @@ public interface ISdcCsarHelper { * @param propertyName - the name of the required property. * @return the leaf value as String, or null if there's no such property, or it's not a leaf. */ + @Deprecated public String getGroupPropertyLeafValue(Group group, String propertyName); /** @@ -128,18 +134,21 @@ public interface ISdcCsarHelper { * @param propertyName - the name of the required property. * @return the leaf value as Object, or null if there's no such property. It's up to the caller to cast it to a proper type. */ + @Deprecated public Object getGroupPropertyAsObject(Group group, String propertyName); /** * Get all VL node templates of the CSAR service. * @return - all VL node templates. */ + @Deprecated public List<NodeTemplate> getServiceVlList(); /** * Get all VF node templates of the CSAR service. * @return - all VF node templates. */ + @Deprecated public List<NodeTemplate> getServiceVfList(); @@ -169,6 +178,7 @@ public interface ISdcCsarHelper { * @param metadataPropertyName - the name of the metadata property. * @return metadata property value */ + @Deprecated public String getMetadataPropertyValue(Metadata metadata, String metadataPropertyName); @@ -234,6 +244,7 @@ public interface ISdcCsarHelper { * @param vfCustomizationId - customizationUuid of the VF node template. * @return all VFC node templates from a specified VF */ + @Deprecated public List<NodeTemplate> getVfcListByVf(String vfCustomizationId); /** @@ -241,6 +252,7 @@ public interface ISdcCsarHelper { * @param vfCustomizationId - customizationUuid of the VF node template. * @return all CP node templates from a specified VF */ + @Deprecated public List<NodeTemplate> getCpListByVf(String vfCustomizationId); /** @@ -261,6 +273,7 @@ public interface ISdcCsarHelper { * @return node templates from vf with the names as in members section. * */ + @Deprecated public List<NodeTemplate> getMembersOfVfModule(NodeTemplate vf, Group vfModule); @@ -297,6 +310,7 @@ public interface ISdcCsarHelper { * Get all allotted node templates from this service. * @return all allotted node templates from this service. */ + @Deprecated public List<NodeTemplate> getAllottedResources(); /** @@ -313,6 +327,7 @@ public interface ISdcCsarHelper { * @param nodeTemplate - node template object * @return - node type string. */ + @Deprecated public String getTypeOfNodeTemplate(NodeTemplate nodeTemplate); /** @@ -355,6 +370,7 @@ public interface ISdcCsarHelper { * @param vfc - VFC node template to look for CP-related props. * @return map <b>CP node template name</b> to a map of <b>property name</b> - <b> property value as object</b>. */ + @Deprecated public Map<String, Map<String, Object>> getCpPropertiesFromVfcAsObject(NodeTemplate vfc); /** @@ -362,6 +378,7 @@ public interface ISdcCsarHelper { * @param nt - node template * @return customization UUID of a node template. */ + @Deprecated public String getNodeTemplateCustomizationUuid(NodeTemplate nt); /** @@ -380,6 +397,7 @@ public interface ISdcCsarHelper { * @param sdcType - the SDC type of the node. * @return node templates of this SDC type. */ + @Deprecated public List<NodeTemplate> getNodeTemplateBySdcType(NodeTemplate parentNodeTemplate, SdcTypes sdcType); /** @@ -388,6 +406,7 @@ public interface ISdcCsarHelper { * @param sdcType - the SDC type of the node (for example, CP, VF...). * @return service node templates of this SDC type. */ + @Deprecated public List<NodeTemplate> getServiceNodeTemplateBySdcType(SdcTypes sdcType); /** @@ -395,6 +414,7 @@ public interface ISdcCsarHelper { * @param vfCustomizationUuid - the Customization UUID of the node. * @return VNF Configuration Node Template. */ + @Deprecated public NodeTemplate getVnfConfig(String vfCustomizationUuid); /** @@ -402,6 +422,7 @@ public interface ISdcCsarHelper { * @param nodeTemplate - Node Template to check * @return true if node template has topology template, false if not. */ + @Deprecated public boolean hasTopology(NodeTemplate nodeTemplate); /** @@ -409,6 +430,7 @@ public interface ISdcCsarHelper { * @param nodeTemplate - Node Template to get its children * @return return list of children node templates for node template. */ + @Deprecated public List<NodeTemplate> getNodeTemplateChildren(NodeTemplate nodeTemplate); /** @@ -416,6 +438,7 @@ public interface ISdcCsarHelper { * @param nodeName - the name of the node template. * @return service-level node template with this name, or null if no such node template was found. */ + @Deprecated public NodeTemplate getServiceNodeTemplateByNodeName(String nodeName); /** @@ -424,6 +447,7 @@ public interface ISdcCsarHelper { * @param nt - Node template to get its Metadata object. * @return Metadata for this node template, or null if not found. */ + @Deprecated public Metadata getNodeTemplateMetadata(NodeTemplate nt); /** @@ -434,6 +458,7 @@ public interface ISdcCsarHelper { * @return CapabilitiesAssignments that contains list of capability assignments for the node template.<br> * If none found, an empty list will be returned. */ + @Deprecated public CapabilityAssignments getCapabilitiesOf(NodeTemplate nt); /** @@ -444,6 +469,7 @@ public interface ISdcCsarHelper { * @return RequirementAssignments that contains list of requirement assignments for the node template. * If none found, an empty list will be returned. */ + @Deprecated public RequirementAssignments getRequirementsOf(NodeTemplate nt); /** @@ -453,12 +479,14 @@ public interface ISdcCsarHelper { * @param pathToPropertyLeafValue - the full path of the required property. * @return the leaf value as String, or null if there's no such property, or it's not a leaf. */ + @Deprecated public String getCapabilityPropertyLeafValue(CapabilityAssignment capability, String pathToPropertyLeafValue); /** * Get all the policies of the main topology template (either VF or service) * @return the list of the policies */ + @Deprecated public List<Policy> getPoliciesOfTopologyTemplate(); /** @@ -466,6 +494,7 @@ public interface ISdcCsarHelper { * @param policyTypeName the name of the policy type * @return the list of the policies */ + @Deprecated public List<Policy> getPoliciesOfTopologyTemplateByToscaPolicyType(String policyTypeName); /** @@ -473,6 +502,7 @@ public interface ISdcCsarHelper { * @param nodeTemplate the node template * @return the list of the policies */ + @Deprecated public List<Policy> getPoliciesOfOriginOfNodeTemplate(NodeTemplate nodeTemplate); /** @@ -481,6 +511,7 @@ public interface ISdcCsarHelper { * @param policyTypeName the name of the policy type * @return the list of the policies */ + @Deprecated List<Policy> getPoliciesOfOriginOfNodeTemplateByToscaPolicyType(NodeTemplate nodeTemplate, String policyTypeName); /** @@ -488,6 +519,7 @@ public interface ISdcCsarHelper { * @param policyName the name of the policy * @return the list of the node templates */ + @Deprecated List<NodeTemplate> getPolicyTargetsFromTopologyTemplate(String policyName); /** @@ -496,6 +528,7 @@ public interface ISdcCsarHelper { * @param policyName the name of the policy * @return the list of the node templates */ + @Deprecated List<NodeTemplate> getPolicyTargetsFromOrigin(NodeTemplate nodeTemplate, String policyName); /** @@ -503,6 +536,7 @@ public interface ISdcCsarHelper { * @param nodeTemplateName the name of the node template * @return the node template */ + @Deprecated NodeTemplate getNodeTemplateByName(String nodeTemplateName); /** @@ -510,6 +544,7 @@ public interface ISdcCsarHelper { * @param targetNode the node template * @return the list of the policies */ + @Deprecated List<Policy> getPoliciesOfTarget(NodeTemplate targetNode); /** @@ -518,6 +553,7 @@ public interface ISdcCsarHelper { * @param policyTypeName the name of the policy type * @return the list of the policies */ + @Deprecated List<Policy> getPoliciesOfTargetByToscaPolicyType(NodeTemplate nodeTemplate, String policyTypeName); /** @@ -525,6 +561,7 @@ public interface ISdcCsarHelper { * @param nodeTemplate the node template * @return the list of the groups */ + @Deprecated ArrayList<Group> getGroupsOfOriginOfNodeTemplate(NodeTemplate nodeTemplate); /** @@ -532,12 +569,14 @@ public interface ISdcCsarHelper { * @param groupType the group type * @return the list of the groups */ + @Deprecated ArrayList<Group> getGroupsOfTopologyTemplateByToscaGroupType(String groupType); /** * Get all groups of this of the main topology template (either VF or service) * @return the list of the groups */ + @Deprecated ArrayList<Group> getGroupsOfTopologyTemplate(); /** @@ -546,6 +585,7 @@ public interface ISdcCsarHelper { * @param groupType the group type * @return the list of the groups */ + @Deprecated ArrayList<Group> getGroupsOfOriginOfNodeTemplateByToscaGroupType(NodeTemplate nodeTemplate, String groupType); /** @@ -553,6 +593,7 @@ public interface ISdcCsarHelper { * @param groupName the name of the group * @return the list of the node templates */ + @Deprecated List<NodeTemplate> getGroupMembersFromTopologyTemplate(String groupName); /** @@ -561,6 +602,7 @@ public interface ISdcCsarHelper { * @param groupName the name of the group * @return the list of the node templates */ + @Deprecated List<NodeTemplate> getGroupMembersOfOriginOfNodeTemplate(NodeTemplate nodeTemplate, String groupName); /** @@ -612,6 +654,7 @@ public interface ISdcCsarHelper { * Path is based on the collection of the node templates names delimited by #. * @return List of property values. If none found, empty list will be returned. */ + @Deprecated List<String> getPropertyLeafValueByPropertyNamePathAndNodeTemplatePath(String propertyNamePath, String nodeTemplatePath); boolean isNodeTypeSupported(NodeTemplate nodeTemplate); @@ -622,7 +665,7 @@ public interface ISdcCsarHelper { * SDC (node template) type, tosca type as well as optional customizationUUID and UUID * @param topologyTemplateQuery parameters of the topology template containing the above entity. * Includes SDC type of the container and optional customizationUUID - * @param isRecursive indicates if the search is recursive or not + * @param isRecursive indicates if the search within the required topology template is recursive. * @return list of @{@link IEntityDetails} objects containing information about the found entities. * If either no entities found or the provided query is incorrect, an empty list is returned */ diff --git a/src/main/java/org/onap/sdc/tosca/parser/elements/EntityDetails.java b/src/main/java/org/onap/sdc/tosca/parser/elements/EntityDetails.java index 91c7144..2567700 100644 --- a/src/main/java/org/onap/sdc/tosca/parser/elements/EntityDetails.java +++ b/src/main/java/org/onap/sdc/tosca/parser/elements/EntityDetails.java @@ -2,23 +2,24 @@ package org.onap.sdc.tosca.parser.elements; import org.onap.sdc.tosca.parser.api.IEntityDetails; import org.onap.sdc.tosca.parser.enums.EntityTemplateType; +import org.onap.sdc.toscaparser.api.CapabilityAssignment; import org.onap.sdc.toscaparser.api.EntityTemplate; -import org.onap.sdc.toscaparser.api.NodeTemplate; import org.onap.sdc.toscaparser.api.Property; +import org.onap.sdc.toscaparser.api.RequirementAssignment; -import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.Map; +import java.util.stream.Collectors; public abstract class EntityDetails implements IEntityDetails { private final EntityTemplate entityTemplate; + private final IEntityDetails parentNodeTemplate; - private final IEntityDetails parentNode; - - EntityDetails(EntityTemplate entityTemplate, NodeTemplate parentNode) { + EntityDetails(EntityTemplate entityTemplate) { this.entityTemplate = entityTemplate; - this.parentNode = EntityDetailsFactory.createEntityDetails(EntityTemplateType.NODE_TEMPLATE, parentNode, null); + this.parentNodeTemplate = EntityDetailsFactory.createEntityDetails(EntityTemplateType.NODE_TEMPLATE, entityTemplate.getParentNodeTemplate()); } @Override @@ -36,27 +37,54 @@ public abstract class EntityDetails implements IEntityDetails { } @Override - public List<Property> getPropertyList() { - return entityTemplate.getPropertiesObjects(); + public List<IEntityDetails> getMemberNodes() { + return Collections.emptyList(); } @Override - public List<IEntityDetails> getMemberNodes() { - List<IEntityDetails> ntList = new ArrayList<>(); - ntList.add(getParent()); - return ntList; + public IEntityDetails getParent() { + return parentNodeTemplate; } @Override - public IEntityDetails getParent() { - //todo - update after adding parent to the EntityTemplate class - return parentNode; + public Map<String, RequirementAssignment> getRequirements() { + return entityTemplate.getRequirements() + .getAll() + .stream() + .collect(Collectors.toMap(RequirementAssignment::getName, ra->ra)); + } + + @Override + public Map<String, CapabilityAssignment> getCapabilities() { + return entityTemplate.getCapabilities() + .getAll() + .stream() + .collect(Collectors.toMap(CapabilityAssignment::getName, ca->ca)); + } + + @Override + public List<String> getTargets() { + return Collections.emptyList(); + } + + @Override + public List<IEntityDetails> getTargetNodes() { + return Collections.emptyList(); } @Override public String getPath() { - //todo - update after adding parent to the EntityTemplate class - return "jenny vTSBC vlan VNF 0#abstract_ssc#ssc_ssc_avpn_port_0"; + StringBuilder pathBld = new StringBuilder(""); + EntityTemplate currentEntityParent = entityTemplate.getParentNodeTemplate(); + + while (currentEntityParent != null) { + if (pathBld.length() != 0) { + pathBld.insert(0,"#"); + } + pathBld.insert(0, currentEntityParent.getName()); + currentEntityParent = currentEntityParent.getParentNodeTemplate(); + } + return pathBld.toString(); } diff --git a/src/main/java/org/onap/sdc/tosca/parser/elements/EntityDetailsFactory.java b/src/main/java/org/onap/sdc/tosca/parser/elements/EntityDetailsFactory.java index 65968b8..e8477c6 100644 --- a/src/main/java/org/onap/sdc/tosca/parser/elements/EntityDetailsFactory.java +++ b/src/main/java/org/onap/sdc/tosca/parser/elements/EntityDetailsFactory.java @@ -2,25 +2,25 @@ package org.onap.sdc.tosca.parser.elements; import org.onap.sdc.tosca.parser.enums.EntityTemplateType; import org.onap.sdc.toscaparser.api.EntityTemplate; -import org.onap.sdc.toscaparser.api.NodeTemplate; public class EntityDetailsFactory { private EntityDetailsFactory(){} - //TODO: the parent should be retrieved from the entityTemplate and not passed as a separate parameter - public static EntityDetails createEntityDetails(EntityTemplateType entityTemplateType, EntityTemplate entityTemplate, NodeTemplate parent) { + public static EntityDetails createEntityDetails(EntityTemplateType entityTemplateType, EntityTemplate entityTemplate) { EntityDetails entityDetails = null; - if (entityTemplate != null) { + if (entityTemplate != null && entityTemplateType != null) { switch (entityTemplateType) { case NODE_TEMPLATE: - entityDetails = new NodeTemplateEntityDetails(entityTemplate, parent); + entityDetails = new NodeTemplateEntityDetails(entityTemplate); break; case POLICY: - entityDetails = new PolicyEntityDetails(entityTemplate, parent); + entityDetails = new PolicyEntityDetails(entityTemplate); break; case GROUP: - entityDetails = new GroupEntityDetails(entityTemplate, parent); + entityDetails = new GroupEntityDetails(entityTemplate); + break; + default: break; } } diff --git a/src/main/java/org/onap/sdc/tosca/parser/elements/GroupEntityDetails.java b/src/main/java/org/onap/sdc/tosca/parser/elements/GroupEntityDetails.java index 82488a7..32a91aa 100644 --- a/src/main/java/org/onap/sdc/tosca/parser/elements/GroupEntityDetails.java +++ b/src/main/java/org/onap/sdc/tosca/parser/elements/GroupEntityDetails.java @@ -4,18 +4,28 @@ import org.onap.sdc.tosca.parser.api.IEntityDetails; import org.onap.sdc.tosca.parser.enums.EntityTemplateType; import org.onap.sdc.toscaparser.api.EntityTemplate; import org.onap.sdc.toscaparser.api.Group; -import org.onap.sdc.toscaparser.api.NodeTemplate; import org.onap.sdc.toscaparser.api.elements.Metadata; +import java.util.Collections; import java.util.List; import java.util.stream.Collectors; public class GroupEntityDetails extends EntityDetails { private final Group group; + private final List<IEntityDetails> memberNodes; - GroupEntityDetails(EntityTemplate entityTemplate, NodeTemplate parentNode) { - super(entityTemplate,parentNode); + GroupEntityDetails(EntityTemplate entityTemplate) { + super(entityTemplate); group = (Group)getEntityTemplate(); + if (group.getMemberNodes() != null) { + memberNodes = group.getMemberNodes() + .stream() + .map(m->EntityDetailsFactory.createEntityDetails(EntityTemplateType.NODE_TEMPLATE, m)) + .collect(Collectors.toList()); + } + else { + memberNodes = Collections.emptyList(); + } } @Override @@ -25,10 +35,7 @@ public class GroupEntityDetails extends EntityDetails { @Override public List<IEntityDetails> getMemberNodes() { - return group.getMemberNodes() - .stream() - .map(m->EntityDetailsFactory.createEntityDetails(EntityTemplateType.NODE_TEMPLATE, m, null)) - .collect(Collectors.toList()); + return memberNodes; } @Override diff --git a/src/main/java/org/onap/sdc/tosca/parser/elements/NodeTemplateEntityDetails.java b/src/main/java/org/onap/sdc/tosca/parser/elements/NodeTemplateEntityDetails.java index c6dd69f..ad3b1a0 100644 --- a/src/main/java/org/onap/sdc/tosca/parser/elements/NodeTemplateEntityDetails.java +++ b/src/main/java/org/onap/sdc/tosca/parser/elements/NodeTemplateEntityDetails.java @@ -9,8 +9,8 @@ public class NodeTemplateEntityDetails extends EntityDetails { private final NodeTemplate nodeTemplate; - NodeTemplateEntityDetails(EntityTemplate entityTemplate, NodeTemplate parentNode) { - super(entityTemplate, parentNode); + NodeTemplateEntityDetails(EntityTemplate entityTemplate) { + super(entityTemplate); nodeTemplate = (NodeTemplate)getEntityTemplate(); } diff --git a/src/main/java/org/onap/sdc/tosca/parser/elements/PolicyEntityDetails.java b/src/main/java/org/onap/sdc/tosca/parser/elements/PolicyEntityDetails.java index 39027c3..7b97175 100644 --- a/src/main/java/org/onap/sdc/tosca/parser/elements/PolicyEntityDetails.java +++ b/src/main/java/org/onap/sdc/tosca/parser/elements/PolicyEntityDetails.java @@ -1,17 +1,22 @@ package org.onap.sdc.tosca.parser.elements; +import org.onap.sdc.tosca.parser.api.IEntityDetails; import org.onap.sdc.tosca.parser.enums.EntityTemplateType; import org.onap.sdc.toscaparser.api.EntityTemplate; -import org.onap.sdc.toscaparser.api.NodeTemplate; import org.onap.sdc.toscaparser.api.Policy; import org.onap.sdc.toscaparser.api.elements.Metadata; +import java.util.List; +import java.util.stream.Collectors; + public class PolicyEntityDetails extends EntityDetails { + private static final String NODE_TEMPLATES_TARGET_TYPE = "node_templates"; + private final Policy policy; - PolicyEntityDetails(EntityTemplate entityTemplate, NodeTemplate parentNode) { - super(entityTemplate, parentNode); + PolicyEntityDetails(EntityTemplate entityTemplate) { + super(entityTemplate); policy = (Policy)getEntityTemplate(); } @@ -24,4 +29,26 @@ public class PolicyEntityDetails extends EntityDetails { public Metadata getMetadata() { return policy.getMetaDataObj(); } + + @Override + public List<String> getTargets() { + if (policy.getTargets() != null) { + return policy.getTargets(); + } + return super.getTargets(); + } + + @Override + public List<IEntityDetails> getTargetNodes() { + if (policy.getTargetsType().equals(NODE_TEMPLATES_TARGET_TYPE)) { + return policy.getTargetsList() + .stream() + .map(o->EntityDetailsFactory.createEntityDetails(EntityTemplateType.NODE_TEMPLATE, (EntityTemplate)o)) + .collect(Collectors.toList()); + } + return policy.getTargetsList() + .stream() + .map(o->EntityDetailsFactory.createEntityDetails(EntityTemplateType.GROUP, (EntityTemplate)o)) + .collect(Collectors.toList()); + } } diff --git a/src/main/java/org/onap/sdc/tosca/parser/elements/queries/AllEntitiesQuery.java b/src/main/java/org/onap/sdc/tosca/parser/elements/queries/AllEntitiesQuery.java new file mode 100644 index 0000000..7c1cd25 --- /dev/null +++ b/src/main/java/org/onap/sdc/tosca/parser/elements/queries/AllEntitiesQuery.java @@ -0,0 +1,46 @@ +package org.onap.sdc.tosca.parser.elements.queries; + +import com.google.common.collect.Lists; +import org.onap.sdc.tosca.parser.api.IEntityDetails; +import org.onap.sdc.tosca.parser.enums.EntityTemplateType; +import org.onap.sdc.toscaparser.api.NodeTemplate; +import org.onap.sdc.toscaparser.api.ToscaTemplate; + +import java.util.List; + +/** + * Implements EntityQuery object for NodeTemplates, Groups and Policies + */ + +public class AllEntitiesQuery extends EntityQuery { + + AllEntitiesQuery() { + super(EntityTemplateType.ALL, null, null); + } + + @Override + public List<IEntityDetails> getEntitiesFromTopologyTemplate(NodeTemplate nodeTemplate) { + List<IEntityDetails> allEntities = Lists.newArrayList(); + if (nodeTemplate.getSubMappingToscaTemplate() != null) { + allEntities.addAll(GroupEntityQuery.convertGroupLisToEntityDetailsList(nodeTemplate.getSubMappingToscaTemplate().getGroups().stream())); + allEntities.addAll(NodeTemplateEntityQuery.convertNodeTemplatesListToEntityDetailsList(nodeTemplate.getSubMappingToscaTemplate().getNodeTemplates().stream())); + } + if (nodeTemplate.getOriginComponentTemplate() != null) { + allEntities.addAll(PolicyEntityQuery.convertPolicyLisToEntityDetailsList(nodeTemplate.getOriginComponentTemplate().getPolicies().stream())); + } + return allEntities; + } + + + @Override + public List<IEntityDetails> getEntitiesFromService(ToscaTemplate toscaTemplate) { + List<IEntityDetails> allEntities = Lists.newArrayList(); + allEntities.addAll(GroupEntityQuery.convertGroupLisToEntityDetailsList(toscaTemplate.getGroups().stream())); + allEntities.addAll(NodeTemplateEntityQuery.convertNodeTemplatesListToEntityDetailsList(toscaTemplate.getNodeTemplates().stream())); + allEntities.addAll(PolicyEntityQuery.convertPolicyLisToEntityDetailsList(toscaTemplate.getPolicies().stream())); + return allEntities; + } + +} + + diff --git a/src/main/java/org/onap/sdc/tosca/parser/elements/queries/EntityQuery.java b/src/main/java/org/onap/sdc/tosca/parser/elements/queries/EntityQuery.java index ba17423..167a568 100644 --- a/src/main/java/org/onap/sdc/tosca/parser/elements/queries/EntityQuery.java +++ b/src/main/java/org/onap/sdc/tosca/parser/elements/queries/EntityQuery.java @@ -1,11 +1,17 @@ package org.onap.sdc.tosca.parser.elements.queries; -import org.apache.commons.lang3.StringUtils; +import org.onap.sdc.tosca.parser.api.IEntityDetails; import org.onap.sdc.tosca.parser.enums.EntityTemplateType; import org.onap.sdc.tosca.parser.enums.SdcTypes; -import org.onap.sdc.toscaparser.api.EntityTemplate; +import org.onap.sdc.tosca.parser.impl.SdcPropertyNames; +import org.onap.sdc.toscaparser.api.NodeTemplate; +import org.onap.sdc.toscaparser.api.ToscaTemplate; +import org.onap.sdc.toscaparser.api.elements.Metadata; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.util.List; +import java.util.Objects; /** * This class describes an entity searched and retrieved by SDC Tosca Parser API @@ -13,6 +19,8 @@ import java.util.List; */ public abstract class EntityQuery { + private static final Logger logger = LoggerFactory.getLogger(EntityQuery.class.getName()); + private final EntityTemplateType entityType; private final SdcTypes nodeTemplateType; @@ -37,9 +45,9 @@ public abstract class EntityQuery { this.customizationUUID = customizationUUID; } - public abstract List<EntityTemplate> searchByTopologyTemplate(TopologyTemplateQuery topologyTemplateQuery); + public abstract List<IEntityDetails> getEntitiesFromTopologyTemplate(NodeTemplate nodeTemplate); - public abstract EntityTemplateType getType(); + public abstract List<IEntityDetails> getEntitiesFromService(ToscaTemplate toscaTemplate); public EntityTemplateType getEntityType() { return entityType; @@ -61,8 +69,19 @@ public abstract class EntityQuery { return customizationUUID; } - public boolean searchAllEntities() { - return StringUtils.isEmpty(toscaType) && nodeTemplateType == null; + boolean isSearchCriteriaMatched(Metadata metadata, String toscaType, String uuidKeyName, String cuuidKeyName) { + return Objects.nonNull(metadata) + && isStringMatchingOrNull(metadata.getValue(uuidKeyName), getUUID()) + && isStringMatchingOrNull(metadata.getValue(cuuidKeyName), getCustomizationUUID()) + && isStringMatchingOrNull(toscaType, getToscaType()); + } + + boolean isSearchCriteriaMatched(Metadata metadata, String toscaType) { + return isSearchCriteriaMatched(metadata, toscaType, SdcPropertyNames.PROPERTY_NAME_UUID, SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID); + } + + static boolean isStringMatchingOrNull(String currentUid, String uidInQuery) { + return uidInQuery == null || uidInQuery.equals(currentUid); } public static EntityQueryBuilder newBuilder(EntityTemplateType entityTemplateType) { @@ -73,6 +92,12 @@ public abstract class EntityQuery { return new EntityQueryBuilder(sdcType); } + @Override + public String toString() { + return String.format("EntityType=%s, nodeTemplateType=%s, toscaType=%s, uUID=%s, customizationUUID=%s", + entityType, nodeTemplateType, toscaType, uUID, customizationUUID); + } + public static EntityQueryBuilder newBuilder(String toscaType) { return new EntityQueryBuilder(toscaType); } @@ -97,8 +122,13 @@ public abstract class EntityQuery { case POLICY: entityQuery = new PolicyEntityQuery(); break; + case ALL: + entityQuery = new AllEntitiesQuery(); + break; default: - throw new IllegalArgumentException("Wrong entity query type: " + entityTemplateType); + String wrongTypeMsg = (String.format("Wrong entity query type: %s", entityTemplateType)); + logger.error(wrongTypeMsg); + throw new IllegalArgumentException(wrongTypeMsg); } } diff --git a/src/main/java/org/onap/sdc/tosca/parser/elements/queries/GroupEntityQuery.java b/src/main/java/org/onap/sdc/tosca/parser/elements/queries/GroupEntityQuery.java index 37a3ccd..8b1cd25 100644 --- a/src/main/java/org/onap/sdc/tosca/parser/elements/queries/GroupEntityQuery.java +++ b/src/main/java/org/onap/sdc/tosca/parser/elements/queries/GroupEntityQuery.java @@ -1,30 +1,55 @@ package org.onap.sdc.tosca.parser.elements.queries; +import com.google.common.collect.Lists; +import org.onap.sdc.tosca.parser.api.IEntityDetails; +import org.onap.sdc.tosca.parser.elements.EntityDetailsFactory; import org.onap.sdc.tosca.parser.enums.EntityTemplateType; -import org.onap.sdc.toscaparser.api.EntityTemplate; +import org.onap.sdc.toscaparser.api.Group; +import org.onap.sdc.toscaparser.api.NodeTemplate; +import org.onap.sdc.toscaparser.api.ToscaTemplate; import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; /** * Implements EntityQuery object for Groups */ public class GroupEntityQuery extends EntityQuery { - public GroupEntityQuery() { + private static final String VF_MODULE_UUID = "vfModuleModelUUID"; + private static final String VF_MODULE_CUSTOMIZATION_UUID = "vfModuleModelCustomizationUUID"; + + GroupEntityQuery() { super(EntityTemplateType.GROUP, null, null); } @Override - public List<EntityTemplate> searchByTopologyTemplate(TopologyTemplateQuery topologyTemplateQuery) { - return null; + public List<IEntityDetails> getEntitiesFromTopologyTemplate(NodeTemplate nodeTemplate) { + if (nodeTemplate.getSubMappingToscaTemplate() != null) { + return convertGroupLisToEntityDetailsList(filter(nodeTemplate.getSubMappingToscaTemplate().getGroups())); + } + return Lists.newArrayList(); } @Override - public EntityTemplateType getType() { - return EntityTemplateType.GROUP; + public List<IEntityDetails> getEntitiesFromService(ToscaTemplate toscaTemplate) { + return convertGroupLisToEntityDetailsList(filter(toscaTemplate.getGroups())); } - public GroupEntityQuery(String toscaType) { + GroupEntityQuery(String toscaType) { super(EntityTemplateType.GROUP, null, toscaType); } + + static List<IEntityDetails> convertGroupLisToEntityDetailsList(Stream<Group> groups) { + return groups.map(gr->EntityDetailsFactory.createEntityDetails(EntityTemplateType.GROUP, gr)) + .collect(Collectors.toList()); + } + + private Stream<Group> filter(List<Group> groupList) { + return groupList.stream() + .filter(gr->isSearchCriteriaMatched(gr.getMetadata(), gr.getType()) || + isSearchCriteriaMatched(gr.getMetadata(), gr.getType(), VF_MODULE_UUID, VF_MODULE_CUSTOMIZATION_UUID)); + } + } diff --git a/src/main/java/org/onap/sdc/tosca/parser/elements/queries/NodeTemplateEntityQuery.java b/src/main/java/org/onap/sdc/tosca/parser/elements/queries/NodeTemplateEntityQuery.java index 4e01bd8..b5a7a2e 100644 --- a/src/main/java/org/onap/sdc/tosca/parser/elements/queries/NodeTemplateEntityQuery.java +++ b/src/main/java/org/onap/sdc/tosca/parser/elements/queries/NodeTemplateEntityQuery.java @@ -1,34 +1,61 @@ package org.onap.sdc.tosca.parser.elements.queries; +import com.google.common.collect.Lists; +import org.onap.sdc.tosca.parser.api.IEntityDetails; +import org.onap.sdc.tosca.parser.elements.EntityDetailsFactory; import org.onap.sdc.tosca.parser.enums.EntityTemplateType; import org.onap.sdc.tosca.parser.enums.SdcTypes; -import org.onap.sdc.toscaparser.api.EntityTemplate; +import org.onap.sdc.tosca.parser.impl.SdcPropertyNames; +import org.onap.sdc.toscaparser.api.NodeTemplate; +import org.onap.sdc.toscaparser.api.ToscaTemplate; import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; /** * Implements EntityQuery object for NodeTemplates */ public class NodeTemplateEntityQuery extends EntityQuery { - public NodeTemplateEntityQuery() { + NodeTemplateEntityQuery() { super(EntityTemplateType.NODE_TEMPLATE, null, null); } + NodeTemplateEntityQuery(SdcTypes nodeTemplateType) { + super(EntityTemplateType.NODE_TEMPLATE, nodeTemplateType, null); + } + + NodeTemplateEntityQuery(String toscaType) { + super(EntityTemplateType.NODE_TEMPLATE, null, toscaType); + } + @Override - public EntityTemplateType getType() { - return EntityTemplateType.NODE_TEMPLATE; + public List<IEntityDetails> getEntitiesFromTopologyTemplate(NodeTemplate nodeTemplate) { + if (nodeTemplate.getSubMappingToscaTemplate() != null) { + return convertNodeTemplatesListToEntityDetailsList(filter(nodeTemplate.getSubMappingToscaTemplate() + .getNodeTemplates())); + } + return Lists.newArrayList(); } @Override - public List<EntityTemplate> searchByTopologyTemplate(TopologyTemplateQuery topologyTemplateQuery) { - return null; + public List<IEntityDetails> getEntitiesFromService(ToscaTemplate toscaTemplate) { + return convertNodeTemplatesListToEntityDetailsList(filter(toscaTemplate.getNodeTemplates())); } - public NodeTemplateEntityQuery(SdcTypes nodeTemplateType) { - super(EntityTemplateType.NODE_TEMPLATE, nodeTemplateType, null); + static List<IEntityDetails> convertNodeTemplatesListToEntityDetailsList(Stream<NodeTemplate> nodeTemplates) { + return nodeTemplates + .map(nt->EntityDetailsFactory.createEntityDetails(EntityTemplateType.NODE_TEMPLATE, nt)) + .collect(Collectors.toList()); } - public NodeTemplateEntityQuery(String toscaType) { - super(EntityTemplateType.NODE_TEMPLATE, null, toscaType); + private Stream<NodeTemplate> filter(List<NodeTemplate> nodeTemplateList) { + return nodeTemplateList.stream() + .filter(nt->isSearchCriteriaMatched(nt.getMetaData(), nt.getType())) + .filter(nt->getNodeTemplateType() == null || + isStringMatchingOrNull(nt.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_TYPE), + getNodeTemplateType().getValue())); } + + } diff --git a/src/main/java/org/onap/sdc/tosca/parser/elements/queries/PolicyEntityQuery.java b/src/main/java/org/onap/sdc/tosca/parser/elements/queries/PolicyEntityQuery.java index 8ab5e4c..2a0a0ab 100644 --- a/src/main/java/org/onap/sdc/tosca/parser/elements/queries/PolicyEntityQuery.java +++ b/src/main/java/org/onap/sdc/tosca/parser/elements/queries/PolicyEntityQuery.java @@ -1,30 +1,52 @@ package org.onap.sdc.tosca.parser.elements.queries; +import com.google.common.collect.Lists; +import org.onap.sdc.tosca.parser.api.IEntityDetails; +import org.onap.sdc.tosca.parser.elements.EntityDetailsFactory; import org.onap.sdc.tosca.parser.enums.EntityTemplateType; -import org.onap.sdc.toscaparser.api.EntityTemplate; +import org.onap.sdc.toscaparser.api.NodeTemplate; +import org.onap.sdc.toscaparser.api.Policy; +import org.onap.sdc.toscaparser.api.ToscaTemplate; import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; /** * Implements EntityQuery object for Policies */ public class PolicyEntityQuery extends EntityQuery { - public PolicyEntityQuery() { + PolicyEntityQuery() { super(EntityTemplateType.POLICY, null, null); } + PolicyEntityQuery(String toscaType) { + super(EntityTemplateType.POLICY, null, toscaType); + } + @Override - public List<EntityTemplate> searchByTopologyTemplate(TopologyTemplateQuery topologyTemplateQuery) { - return null; + public List<IEntityDetails> getEntitiesFromTopologyTemplate(NodeTemplate nodeTemplate) { + if (nodeTemplate.getOriginComponentTemplate() != null) { + return convertPolicyLisToEntityDetailsList(filter(nodeTemplate.getOriginComponentTemplate().getPolicies())); + } + return Lists.newArrayList(); } @Override - public EntityTemplateType getType() { - return EntityTemplateType.POLICY; + public List<IEntityDetails> getEntitiesFromService(ToscaTemplate toscaTemplate) { + return convertPolicyLisToEntityDetailsList(filter(toscaTemplate.getPolicies())); } - public PolicyEntityQuery(String toscaType) { - super(EntityTemplateType.POLICY, null, toscaType); + static List<IEntityDetails> convertPolicyLisToEntityDetailsList(Stream<Policy> policies) { + return policies + .map(p->EntityDetailsFactory.createEntityDetails(EntityTemplateType.POLICY, p)) + .collect(Collectors.toList()); } + + private Stream<Policy> filter(List<Policy> policyList) { + return policyList.stream() + .filter(p->isSearchCriteriaMatched(p.getMetaDataObj(), p.getType())); + } + } diff --git a/src/main/java/org/onap/sdc/tosca/parser/elements/queries/TopologyTemplateQuery.java b/src/main/java/org/onap/sdc/tosca/parser/elements/queries/TopologyTemplateQuery.java index 47b10a8..668345c 100644 --- a/src/main/java/org/onap/sdc/tosca/parser/elements/queries/TopologyTemplateQuery.java +++ b/src/main/java/org/onap/sdc/tosca/parser/elements/queries/TopologyTemplateQuery.java @@ -1,14 +1,22 @@ package org.onap.sdc.tosca.parser.elements.queries; import org.onap.sdc.tosca.parser.enums.SdcTypes; +import org.onap.sdc.tosca.parser.impl.SdcPropertyNames; +import org.onap.sdc.toscaparser.api.NodeTemplate; +import org.onap.sdc.toscaparser.api.elements.Metadata; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Objects; /** * This class describes a node template instance containing an entity searched and retrieved by SDC Tosca Parser API * It is used as the API input parameter. See the {@link org.onap.sdc.tosca.parser.api.ISdcCsarHelper} */ public class TopologyTemplateQuery { + private static final Logger logger = LoggerFactory.getLogger(TopologyTemplateQuery.class.getName()); - public void setCustomizationUUID(String customizationUUID) { + void setCustomizationUUID(String customizationUUID) { this.customizationUUID = customizationUUID; } @@ -21,6 +29,11 @@ public class TopologyTemplateQuery { } public static TopologyTemplateQueryBuilder newBuilder(SdcTypes sdcType) { + if (!SdcTypes.isComplex(sdcType.getValue())) { + String wrongTypeMsg = (String.format("Given type is not Topology template %s", sdcType)); + logger.error(wrongTypeMsg); + throw new IllegalArgumentException(wrongTypeMsg); + } return new TopologyTemplateQueryBuilder(sdcType); } @@ -32,6 +45,37 @@ public class TopologyTemplateQuery { return customizationUUID; } + public Boolean isMatchingSearchCriteria(NodeTemplate nodeTemplate) { + boolean isMatched = Objects.nonNull(nodeTemplate.getMetaData()) && isSearchedTemplate(nodeTemplate.getMetaData()); + if(logger.isDebugEnabled()) { + logger.debug("Node template {} is{} matching search criteria", nodeTemplate.getName(), isMatched ? "" : " not"); + } + return isMatched; + } + + public boolean isSameSdcType(Metadata metadata) { + final String nodeType = metadata.getValue(SdcPropertyNames.PROPERTY_NAME_TYPE); + return Objects.nonNull(nodeType) && ( + sdcType.getValue().equals(nodeType) || isServiceSearched(nodeType)); + } + + private boolean isSearchedTemplate(Metadata metadata) { + return isSameSdcType(metadata) && + (sdcType == SdcTypes.SERVICE || + //don't check customizationUUID for service + EntityQuery.isStringMatchingOrNull(metadata.getValue(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID), + getCustomizationUUID())); + } + + private boolean isServiceSearched(String nodeType) { + return sdcType == SdcTypes.SERVICE && SdcTypes.isComplex(nodeType); + } + + @Override + public String toString() { + return String.format("sdcType=%s, customizationUUID=%s", sdcType.getValue(), customizationUUID); + } + public static class TopologyTemplateQueryBuilder { private TopologyTemplateQuery topologyTemplateQuery; private TopologyTemplateQueryBuilder(SdcTypes sdcType) { topologyTemplateQuery = new TopologyTemplateQuery(sdcType);} diff --git a/src/main/java/org/onap/sdc/tosca/parser/enums/EntityTemplateType.java b/src/main/java/org/onap/sdc/tosca/parser/enums/EntityTemplateType.java index 7f85673..925d58b 100644 --- a/src/main/java/org/onap/sdc/tosca/parser/enums/EntityTemplateType.java +++ b/src/main/java/org/onap/sdc/tosca/parser/enums/EntityTemplateType.java @@ -6,6 +6,7 @@ package org.onap.sdc.tosca.parser.enums; public enum EntityTemplateType { NODE_TEMPLATE, GROUP, - POLICY; + POLICY, + ALL; } diff --git a/src/main/java/org/onap/sdc/tosca/parser/enums/SdcTypes.java b/src/main/java/org/onap/sdc/tosca/parser/enums/SdcTypes.java index 99bdd81..b041972 100644 --- a/src/main/java/org/onap/sdc/tosca/parser/enums/SdcTypes.java +++ b/src/main/java/org/onap/sdc/tosca/parser/enums/SdcTypes.java @@ -26,7 +26,8 @@ import java.util.stream.Collectors; public enum SdcTypes { - CP("CP"), VL("VL"), VF("VF"), CR("CR"), VFC("VFC"), PNF("PNF"), SERVICE("Service"), CVFC("CVFC"), SERVICE_PROXY("Service Proxy"), CONFIGURATION("Configuration"); + CP("CP"), VL("VL"), VF("VF"), CR("CR"), VFC("VFC"), PNF("PNF"), SERVICE("Service"), CVFC("CVFC"), + SERVICE_PROXY("Service Proxy"), CONFIGURATION("Configuration"), VFC_ALLOTTED_RESOURCE("AllottedResource") ; private String value; diff --git a/src/main/java/org/onap/sdc/tosca/parser/impl/QueryProcessor.java b/src/main/java/org/onap/sdc/tosca/parser/impl/QueryProcessor.java new file mode 100644 index 0000000..42b499b --- /dev/null +++ b/src/main/java/org/onap/sdc/tosca/parser/impl/QueryProcessor.java @@ -0,0 +1,127 @@ +package org.onap.sdc.tosca.parser.impl; + +import com.google.common.collect.Lists; +import org.onap.sdc.tosca.parser.api.IEntityDetails; +import org.onap.sdc.tosca.parser.elements.queries.EntityQuery; +import org.onap.sdc.tosca.parser.elements.queries.TopologyTemplateQuery; +import org.onap.sdc.tosca.parser.enums.EntityTemplateType; +import org.onap.sdc.tosca.parser.enums.SdcTypes; +import org.onap.sdc.toscaparser.api.NodeTemplate; +import org.onap.sdc.toscaparser.api.ToscaTemplate; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +/** + * Performs search for entity templates inside node template according to query criteria + */ +class QueryProcessor { + private static final Logger logger = LoggerFactory.getLogger(QueryProcessor.class.getName()); + + private final EntityQuery entityQuery; + private final TopologyTemplateQuery topologyTemplateQuery; + private final ToscaTemplate toscaTemplate; + private boolean isRecursive = false; + + QueryProcessor(ToscaTemplate toscaTemplate, EntityQuery entityQuery, TopologyTemplateQuery topologyTemplateQuery, boolean isRecursive) { + this.toscaTemplate = toscaTemplate; + this.entityQuery = entityQuery == null ? EntityQuery.newBuilder(EntityTemplateType.ALL).build() : entityQuery; + this.topologyTemplateQuery = topologyTemplateQuery; + this.isRecursive = isRecursive; + } + + List<IEntityDetails> doQuery() { + List<IEntityDetails> entityDetailsList = Lists.newArrayList(); + if (isServiceSearch()) { + //search for entities inside the service + if (logger.isDebugEnabled()) { + logger.debug("Service {} is searched for {}", toscaTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_NAME), entityQuery); + } + entityDetailsList.addAll(entityQuery.getEntitiesFromService(toscaTemplate)); + + if (!isRecursive) { + return entityDetailsList; + } + } + + List<NodeTemplate> foundTopologyTemplates = getInternalTopologyTemplates(toscaTemplate.getNodeTemplates(), false); + if (isRecursive) { + if (logger.isDebugEnabled()) { + logger.debug("Search for entities recursively"); + } + //go over internal topology templates of the found templates + // and search for instances of the same type + //if the queried topology template is "SERVICE", search for all instances of templates in the service + List<NodeTemplate> internalTopologyTemplates = foundTopologyTemplates.stream() + .filter(nt->nt.getSubMappingToscaTemplate() != null) + .map(nt->getInternalTopologyTemplates(nt.getSubMappingToscaTemplate().getNodeTemplates(), true)) + .flatMap(List::stream) + .collect(Collectors.toList()); + foundTopologyTemplates.addAll(internalTopologyTemplates); + } + if (logger.isDebugEnabled()) { + logger.debug("Found topology templates {} matching following query criteria: {}", + foundTopologyTemplates, topologyTemplateQuery); + } + //go over all node templates found according to query criteria and recursive flag and + // search for the requested entities. + entityDetailsList.addAll(searchEntitiesInsideTopologyTemplates(foundTopologyTemplates)); + + return entityDetailsList; + } + + private Map<String, NodeTemplate> convertListToMap(List<NodeTemplate> nodeTemplateList) { + // we use map to avoid duplicate search through same node templates + return nodeTemplateList.stream() + .collect(Collectors.toMap(NodeTemplate::getName, nt->nt, (nt1, nt2)->nt1)); + } + + private List<IEntityDetails> searchEntitiesInsideTopologyTemplates(List<NodeTemplate> foundTopologyTemplates) { + return convertListToMap(foundTopologyTemplates) + .values() + .stream() + .map(entityQuery::getEntitiesFromTopologyTemplate) + .flatMap(List::stream) + .collect(Collectors.toList()); + } + + private boolean isServiceSearch() { + return topologyTemplateQuery.getNodeTemplateType() == SdcTypes.SERVICE; + } + + private List<NodeTemplate> getInternalTopologyTemplates(List<NodeTemplate> nodeTemplateList, boolean searchTypeOnly) { + return nodeTemplateList + .stream() + .map(child->getTopologyTemplatesByQuery(child, searchTypeOnly)) + .flatMap(List::stream) + .collect(Collectors.toList()); + } + + private List<NodeTemplate> getTopologyTemplatesByQuery(NodeTemplate current, boolean searchTypeOnly) { + List<NodeTemplate> topologyTemplateList = Lists.newArrayList(); + + boolean isTopologyTemplateFound = searchTypeOnly ? + topologyTemplateQuery.isSameSdcType(current.getMetaData()) : topologyTemplateQuery.isMatchingSearchCriteria(current); + if (isTopologyTemplateFound) { + topologyTemplateList.add(current); + if (!isServiceSearch()) { + //recursion stop condition + return topologyTemplateList; + } + } + if (SdcTypes.isComplex(current.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_TYPE)) && current.getSubMappingToscaTemplate() != null) { + //search the node template inside a given topology template + topologyTemplateList.addAll(current.getSubMappingToscaTemplate().getNodeTemplates() + .stream() + .map(nt->getTopologyTemplatesByQuery(nt, searchTypeOnly)) + .flatMap(List::stream) + .collect(Collectors.toList())); + } + return topologyTemplateList; + } + + +} diff --git a/src/main/java/org/onap/sdc/tosca/parser/impl/SdcCsarHelperImpl.java b/src/main/java/org/onap/sdc/tosca/parser/impl/SdcCsarHelperImpl.java index 06bf48a..1ae3471 100644 --- a/src/main/java/org/onap/sdc/tosca/parser/impl/SdcCsarHelperImpl.java +++ b/src/main/java/org/onap/sdc/tosca/parser/impl/SdcCsarHelperImpl.java @@ -39,11 +39,8 @@ import org.apache.commons.lang3.tuple.Pair; import org.onap.sdc.tosca.parser.api.IEntityDetails; import org.onap.sdc.tosca.parser.api.ISdcCsarHelper; import org.onap.sdc.tosca.parser.config.ConfigurationManager; -import org.onap.sdc.tosca.parser.elements.EntityDetailsFactory; -import org.onap.sdc.tosca.parser.elements.NodeTemplateEntityDetails; -import org.onap.sdc.tosca.parser.elements.queries.TopologyTemplateQuery; import org.onap.sdc.tosca.parser.elements.queries.EntityQuery; -import org.onap.sdc.tosca.parser.enums.EntityTemplateType; +import org.onap.sdc.tosca.parser.elements.queries.TopologyTemplateQuery; import org.onap.sdc.tosca.parser.enums.FilterType; import org.onap.sdc.tosca.parser.enums.PropertySchemaType; import org.onap.sdc.tosca.parser.enums.SdcTypes; @@ -1208,12 +1205,13 @@ public class SdcCsarHelperImpl implements ISdcCsarHelper { @Override public List<IEntityDetails> getEntity(EntityQuery entityQuery, TopologyTemplateQuery topologyTemplateQuery, boolean isRecursive) { - List<IEntityDetails> foundEntities = new ArrayList<>(); - List<NodeTemplate> vfcList = getVfcListByVf("05e77410-a1d8-44fe-8440-b9410c8f98ee"); - NodeTemplate vfc = getNodeTemplateByCustomizationUuid(vfcList, "1fdc9625-dfec-48e1-aaf8-7b92f78ca854"); - NodeTemplate cp = getChildNodeTemplateByName(vfc, "ssc_ssc_avpn_port_0"); - foundEntities.add(EntityDetailsFactory.createEntityDetails(EntityTemplateType.NODE_TEMPLATE, cp, vfc)); - return foundEntities; + + if (log.isDebugEnabled()) { + log.debug("getEntity request: EntityQuery <{}>, TopologyTemplateQuery <{}>, isRecursive<{}>", + entityQuery, topologyTemplateQuery, isRecursive); + } + return new QueryProcessor(toscaTemplate, entityQuery, topologyTemplateQuery, isRecursive).doQuery(); } + } |