diff options
author | shiria <shiri.amichai@amdocs.com> | 2018-06-04 11:07:29 +0300 |
---|---|---|
committer | Avi Gaffa <avi.gaffa@amdocs.com> | 2018-06-05 14:31:34 +0000 |
commit | 09c6320df7f768e42edeaedd7ba69e6fb13d433d (patch) | |
tree | 92883e0297c3d47ab039685ca91f2f47a1dbcd46 /openecomp-be/lib/openecomp-tosca-lib/src/main/java | |
parent | d9cae980e80021dfad0bc9366d32d2cf996cfa90 (diff) |
fix get flat node type
update node type interface
update return value to object which include the type hierarchy
Change-Id: I97623c7bbad0223a174370d13aabf4c3efe9c21e
Issue-ID: SDC-1394
Signed-off-by: shiria <shiri.amichai@amdocs.com>
Diffstat (limited to 'openecomp-be/lib/openecomp-tosca-lib/src/main/java')
5 files changed, 1214 insertions, 1211 deletions
diff --git a/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/datatypes/ToscaFlatData.java b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/datatypes/ToscaFlatData.java new file mode 100644 index 0000000000..b898640df9 --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/datatypes/ToscaFlatData.java @@ -0,0 +1,57 @@ +/* + * Copyright © 2016-2018 European Support Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.openecomp.sdc.tosca.datatypes; + + +import java.util.ArrayList; +import java.util.List; + +import org.apache.commons.collections4.CollectionUtils; + +public class ToscaFlatData { + + private Object flatEntity; + private ToscaElementTypes elementType; + private List<String> inheritanceHierarchyType; + + public ToscaElementTypes getElementType() { + return elementType; + } + + public void setElementType(ToscaElementTypes elementType) { + this.elementType = elementType; + } + + public Object getFlatEntity() { + return flatEntity; + } + + public void setFlatEntity(Object flatEntity) { + this.flatEntity = flatEntity; + } + + public List<String> getInheritanceHierarchyType() { + return inheritanceHierarchyType; + } + + public void addInheritanceHierarchyType(String inheritedType) { + if (CollectionUtils.isEmpty(inheritanceHierarchyType)) { + inheritanceHierarchyType = new ArrayList<>(); + } + this.inheritanceHierarchyType.add(inheritedType); + } +} diff --git a/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/services/DataModelUtil.java b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/services/DataModelUtil.java index a8629aa287..fc411bf009 100644 --- a/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/services/DataModelUtil.java +++ b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/services/DataModelUtil.java @@ -45,7 +45,6 @@ import org.onap.sdc.tosca.datatypes.model.EntrySchema; import org.onap.sdc.tosca.datatypes.model.GroupDefinition; import org.onap.sdc.tosca.datatypes.model.Import; import org.onap.sdc.tosca.datatypes.model.InterfaceDefinition; -import org.onap.sdc.tosca.datatypes.model.InterfaceDefinitionTemplate; import org.onap.sdc.tosca.datatypes.model.InterfaceDefinitionType; import org.onap.sdc.tosca.datatypes.model.InterfaceType; import org.onap.sdc.tosca.datatypes.model.NodeTemplate; @@ -101,11 +100,10 @@ public class DataModelUtil { * @param substitutionMapping the substitution mapping */ public static void addSubstitutionMapping(ServiceTemplate serviceTemplate, - SubstitutionMapping substitutionMapping) { + SubstitutionMapping substitutionMapping) { if (serviceTemplate == null) { - throw new CoreException( - new InvalidAddActionNullEntityErrorBuilder("Substitution Mapping", SERVICE_TEMPLATE) - .build()); + throw new CoreException(new InvalidAddActionNullEntityErrorBuilder("Substitution Mapping", SERVICE_TEMPLATE) + .build()); } if (serviceTemplate.getTopology_template() == null) { @@ -135,12 +133,11 @@ public class DataModelUtil { * @param substitutionMappingRequirementList the substitution mapping requirement list */ public static void addSubstitutionMappingReq(ServiceTemplate serviceTemplate, - String substitutionMappingRequirementId, - List<String> substitutionMappingRequirementList) { + String substitutionMappingRequirementId, + List<String> substitutionMappingRequirementList) { if (serviceTemplate == null) { - throw new CoreException( - new InvalidAddActionNullEntityErrorBuilder("Substitution Mapping Requirements", - SERVICE_TEMPLATE).build()); + throw new CoreException(new InvalidAddActionNullEntityErrorBuilder("Substitution Mapping Requirements", + SERVICE_TEMPLATE).build()); } if (serviceTemplate.getTopology_template() == null) { @@ -149,14 +146,12 @@ public class DataModelUtil { if (serviceTemplate.getTopology_template().getSubstitution_mappings() == null) { serviceTemplate.getTopology_template().setSubstitution_mappings(new SubstitutionMapping()); } - if (serviceTemplate.getTopology_template().getSubstitution_mappings().getRequirements() - == null) { - serviceTemplate.getTopology_template().getSubstitution_mappings() - .setRequirements(new HashMap<>()); + if (serviceTemplate.getTopology_template().getSubstitution_mappings().getRequirements() == null) { + serviceTemplate.getTopology_template().getSubstitution_mappings().setRequirements(new HashMap<>()); } serviceTemplate.getTopology_template().getSubstitution_mappings().getRequirements() - .put(substitutionMappingRequirementId, substitutionMappingRequirementList); + .put(substitutionMappingRequirementId, substitutionMappingRequirementList); } /** @@ -167,12 +162,11 @@ public class DataModelUtil { * @param substitutionMappingCapabilityList the substitution mapping capability list */ public static void addSubstitutionMappingCapability(ServiceTemplate serviceTemplate, - String substitutionMappingCapabilityId, - List<String> substitutionMappingCapabilityList) { + String substitutionMappingCapabilityId, + List<String> substitutionMappingCapabilityList) { if (serviceTemplate == null) { - throw new CoreException( - new InvalidAddActionNullEntityErrorBuilder("Substitution Mapping Capabilities", - SERVICE_TEMPLATE).build()); + throw new CoreException(new InvalidAddActionNullEntityErrorBuilder("Substitution Mapping Capabilities", + SERVICE_TEMPLATE).build()); } if (serviceTemplate.getTopology_template() == null) { @@ -181,14 +175,12 @@ public class DataModelUtil { if (serviceTemplate.getTopology_template().getSubstitution_mappings() == null) { serviceTemplate.getTopology_template().setSubstitution_mappings(new SubstitutionMapping()); } - if (serviceTemplate.getTopology_template().getSubstitution_mappings().getCapabilities() - == null) { - serviceTemplate.getTopology_template().getSubstitution_mappings() - .setCapabilities(new HashMap<>()); + if (serviceTemplate.getTopology_template().getSubstitution_mappings().getCapabilities() == null) { + serviceTemplate.getTopology_template().getSubstitution_mappings().setCapabilities(new HashMap<>()); } serviceTemplate.getTopology_template().getSubstitution_mappings().getCapabilities() - .putIfAbsent(substitutionMappingCapabilityId, substitutionMappingCapabilityList); + .putIfAbsent(substitutionMappingCapabilityId, substitutionMappingCapabilityList); } /** @@ -198,9 +190,8 @@ public class DataModelUtil { * @return the service template node templates and empty map if not present */ public static Map<String, NodeTemplate> getNodeTemplates(ServiceTemplate serviceTemplate) { - if (Objects.isNull(serviceTemplate) - || Objects.isNull(serviceTemplate.getTopology_template()) - || MapUtils.isEmpty(serviceTemplate.getTopology_template().getNode_templates())) { + if (Objects.isNull(serviceTemplate) || Objects.isNull(serviceTemplate.getTopology_template()) + || MapUtils.isEmpty(serviceTemplate.getTopology_template().getNode_templates())) { return new HashMap<>(); } @@ -214,9 +205,8 @@ public class DataModelUtil { * @return the service template groups and empty map if not present */ public static Map<String, GroupDefinition> getGroups(ServiceTemplate serviceTemplate) { - if (Objects.isNull(serviceTemplate) - || Objects.isNull(serviceTemplate.getTopology_template()) - || MapUtils.isEmpty(serviceTemplate.getTopology_template().getGroups())) { + if (Objects.isNull(serviceTemplate) || Objects.isNull(serviceTemplate.getTopology_template()) + || MapUtils.isEmpty(serviceTemplate.getTopology_template().getGroups())) { return new HashMap<>(); } @@ -231,10 +221,10 @@ public class DataModelUtil { * @param nodeTemplate the node template */ public static void addNodeTemplate(ServiceTemplate serviceTemplate, String nodeTemplateId, - NodeTemplate nodeTemplate) { + NodeTemplate nodeTemplate) { if (serviceTemplate == null) { - throw new CoreException( - new InvalidAddActionNullEntityErrorBuilder("Node Template", SERVICE_TEMPLATE).build()); + throw new CoreException(new InvalidAddActionNullEntityErrorBuilder("Node Template", SERVICE_TEMPLATE) + .build()); } TopologyTemplate topologyTemplate = serviceTemplate.getTopology_template(); if (Objects.isNull(topologyTemplate)) { @@ -253,15 +243,14 @@ public class DataModelUtil { * @param nodeType the node type * @param capabilities the capability definitions */ - public static void addNodeTypeCapabilitiesDef(NodeType nodeType, - Map<String, CapabilityDefinition> capabilities) { + public static void addNodeTypeCapabilitiesDef(NodeType nodeType, Map<String, CapabilityDefinition> capabilities) { if (MapUtils.isEmpty(capabilities) || capabilities.entrySet().isEmpty()) { return; } if (nodeType == null) { - throw new CoreException( - new InvalidAddActionNullEntityErrorBuilder("Capability Definition", NODE_TYPE).build()); + throw new CoreException(new InvalidAddActionNullEntityErrorBuilder("Capability Definition", NODE_TYPE) + .build()); } if (MapUtils.isEmpty(nodeType.getCapabilities())) { @@ -283,11 +272,10 @@ public class DataModelUtil { * @param policyDefinition the policy definition */ public static void addPolicyDefinition(ServiceTemplate serviceTemplate, String policyId, - PolicyDefinition policyDefinition) { + PolicyDefinition policyDefinition) { if (serviceTemplate == null) { - throw new CoreException( - new InvalidAddActionNullEntityErrorBuilder("Policy Definition", SERVICE_TEMPLATE) - .build()); + throw new CoreException(new InvalidAddActionNullEntityErrorBuilder("Policy Definition", SERVICE_TEMPLATE) + .build()); } TopologyTemplate topologyTemplate = serviceTemplate.getTopology_template(); if (Objects.isNull(topologyTemplate)) { @@ -307,11 +295,9 @@ public class DataModelUtil { * @param nodeTypeId the node type id * @param nodeType the node type */ - public static void addNodeType(ServiceTemplate serviceTemplate, String nodeTypeId, - NodeType nodeType) { + public static void addNodeType(ServiceTemplate serviceTemplate, String nodeTypeId, NodeType nodeType) { if (serviceTemplate == null) { - throw new CoreException( - new InvalidAddActionNullEntityErrorBuilder(NODE_TYPE, SERVICE_TEMPLATE).build()); + throw new CoreException(new InvalidAddActionNullEntityErrorBuilder(NODE_TYPE, SERVICE_TEMPLATE).build()); } if (serviceTemplate.getNode_types() == null) { serviceTemplate.setNode_types(new HashMap<>()); @@ -326,13 +312,11 @@ public class DataModelUtil { * @param relationshipTemplateId the relationship template id * @param relationshipTemplate the relationship template */ - public static void addRelationshipTemplate(ServiceTemplate serviceTemplate, - String relationshipTemplateId, - RelationshipTemplate relationshipTemplate) { + public static void addRelationshipTemplate(ServiceTemplate serviceTemplate, String relationshipTemplateId, + RelationshipTemplate relationshipTemplate) { if (serviceTemplate == null) { - throw new CoreException( - new InvalidAddActionNullEntityErrorBuilder("Relationship Template", SERVICE_TEMPLATE) - .build()); + throw new CoreException(new InvalidAddActionNullEntityErrorBuilder("Relationship Template", + SERVICE_TEMPLATE).build()); } if (serviceTemplate.getTopology_template() == null) { serviceTemplate.setTopology_template(new TopologyTemplate()); @@ -341,7 +325,7 @@ public class DataModelUtil { serviceTemplate.getTopology_template().setRelationship_templates(new HashMap<>()); } serviceTemplate.getTopology_template().getRelationship_templates() - .put(relationshipTemplateId, relationshipTemplate); + .put(relationshipTemplateId, relationshipTemplate); } /** @@ -352,11 +336,10 @@ public class DataModelUtil { * @param requirementAssignment the requirement assignment */ public static void addRequirementAssignment(NodeTemplate nodeTemplate, String requirementId, - RequirementAssignment requirementAssignment) { + RequirementAssignment requirementAssignment) { if (nodeTemplate == null) { - throw new CoreException( - new InvalidAddActionNullEntityErrorBuilder("Requirement Assignment", "Node Template") - .build()); + throw new CoreException(new InvalidAddActionNullEntityErrorBuilder("Requirement Assignment", + "Node Template").build()); } if (requirementAssignment.getNode() == null) { throw new CoreException(new InvalidRequirementAssignmentErrorBuilder(requirementId).build()); @@ -391,11 +374,9 @@ public class DataModelUtil { * @param nodeTemplateId the node template id * @return the node template */ - public static NodeTemplate getNodeTemplate(ServiceTemplate serviceTemplate, - String nodeTemplateId) { - if (serviceTemplate == null - || serviceTemplate.getTopology_template() == null - || serviceTemplate.getTopology_template().getNode_templates() == null) { + public static NodeTemplate getNodeTemplate(ServiceTemplate serviceTemplate, String nodeTemplateId) { + if (serviceTemplate == null || serviceTemplate.getTopology_template() == null + || serviceTemplate.getTopology_template().getNode_templates() == null) { return null; } return serviceTemplate.getTopology_template().getNode_templates().get(nodeTemplateId); @@ -422,18 +403,12 @@ public class DataModelUtil { * @param requirementDefinitionId the requirement definition id * @return the requirement definition */ - public static Optional<RequirementDefinition> getRequirementDefinition( - NodeType nodeType, - String requirementDefinitionId) { + public static Optional<RequirementDefinition> getRequirementDefinition(NodeType nodeType, + String requirementDefinitionId) { if (nodeType == null || nodeType.getRequirements() == null || requirementDefinitionId == null) { return Optional.empty(); } - for (Map<String, RequirementDefinition> reqMap : nodeType.getRequirements()) { - if (reqMap.containsKey(requirementDefinitionId)) { - return Optional.of(reqMap.get(requirementDefinitionId)); - } - } - return Optional.empty(); + return getRequirementDefinition(nodeType.getRequirements(), requirementDefinitionId); } /** @@ -442,16 +417,19 @@ public class DataModelUtil { * @param requirementsDefinitionList requirement definition list * @param requirementKey requirement key */ - public static Optional<RequirementDefinition> getRequirementDefinition( - List<Map<String, RequirementDefinition>> requirementsDefinitionList, - String requirementKey) { + public static Optional<RequirementDefinition> getRequirementDefinition(List<Map<String, RequirementDefinition>> requirementsDefinitionList, + String requirementKey) { if (CollectionUtils.isEmpty(requirementsDefinitionList)) { return Optional.empty(); } + ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil(); for (Map<String, RequirementDefinition> requirementMap : requirementsDefinitionList) { if (requirementMap.containsKey(requirementKey)) { - return Optional.of(requirementMap.get(requirementKey)); + RequirementDefinition requirementDefinition = toscaExtensionYamlUtil.yamlToObject( + toscaExtensionYamlUtil.objectToYaml(requirementMap.get(requirementKey)), + RequirementDefinition.class); + return Optional.of(requirementDefinition); } } return Optional.empty(); @@ -464,9 +442,8 @@ public class DataModelUtil { * @param capabilityDefinitionId the capability definition id * @return the capability definition */ - public static Optional<CapabilityDefinition> getCapabilityDefinition( - NodeType nodeType, - String capabilityDefinitionId) { + public static Optional<CapabilityDefinition> getCapabilityDefinition(NodeType nodeType, + String capabilityDefinitionId) { if (nodeType == null || nodeType.getCapabilities() == null || capabilityDefinitionId == null) { return Optional.empty(); } @@ -480,12 +457,11 @@ public class DataModelUtil { * @param groupName the group name * @param group the group */ - public static void addGroupDefinitionToTopologyTemplate(ServiceTemplate serviceTemplate, - String groupName, GroupDefinition group) { + public static void addGroupDefinitionToTopologyTemplate(ServiceTemplate serviceTemplate, String groupName, + GroupDefinition group) { if (serviceTemplate == null) { - throw new CoreException( - new InvalidAddActionNullEntityErrorBuilder("Group Definition", SERVICE_TEMPLATE) - .build()); + throw new CoreException(new InvalidAddActionNullEntityErrorBuilder("Group Definition", SERVICE_TEMPLATE) + .build()); } TopologyTemplate topologyTemplate = serviceTemplate.getTopology_template(); @@ -511,13 +487,10 @@ public class DataModelUtil { * @param groupName the group name * @param groupMemberId the group member id */ - public static void addGroupMember(ServiceTemplate serviceTemplate, - String groupName, - String groupMemberId) { + public static void addGroupMember(ServiceTemplate serviceTemplate, String groupName, String groupMemberId) { TopologyTemplate topologyTemplate = serviceTemplate.getTopology_template(); - if (Objects.isNull(topologyTemplate) - || topologyTemplate.getGroups() == null - || topologyTemplate.getGroups().get(groupName) == null) { + if (Objects.isNull(topologyTemplate) || topologyTemplate.getGroups() == null + || topologyTemplate.getGroups().get(groupName) == null) { return; } @@ -542,11 +515,9 @@ public class DataModelUtil { * @param defaultVal the default val * @return the property definition */ - public static ParameterDefinition createParameterDefinition(String type, String description, - boolean required, - List<Constraint> constraints, - EntrySchema entrySchema, - Object defaultVal) { + public static ParameterDefinition createParameterDefinition(String type, String description, boolean required, + List<Constraint> constraints, + EntrySchema entrySchema, Object defaultVal) { ParameterDefinition paramDef = new ParameterDefinition(); paramDef.setType(type); paramDef.setDescription(description); @@ -566,8 +537,8 @@ public class DataModelUtil { * @param occurrences the occurrences * @return the requirement definition */ - public static RequirementDefinition createRequirement(String capability, String node, - String relationship, Object[] occurrences) { + public static RequirementDefinition createRequirement(String capability, String node, String relationship, + Object[] occurrences) { RequirementDefinition requirementDefinition = new RequirementDefinition(); requirementDefinition.setCapability(capability); requirementDefinition.setNode(node); @@ -586,10 +557,8 @@ public class DataModelUtil { * @param constraints the constraints * @return the entry schema */ - public static EntrySchema createEntrySchema(String type, String description, - List<Constraint> constraints) { - if (Objects.isNull(type) && Objects.isNull(description) - && CollectionUtils.isEmpty(constraints)) { + public static EntrySchema createEntrySchema(String type, String description, List<Constraint> constraints) { + if (Objects.isNull(type) && Objects.isNull(description) && CollectionUtils.isEmpty(constraints)) { return null; } @@ -608,9 +577,8 @@ public class DataModelUtil { * @param nestedPropertyName the nested property name * @return the map */ - public static Map createGetInputPropertyValueFromListParameter(String inputPropertyListName, - int indexInTheList, - String... nestedPropertyName) { + public static Map createGetInputPropertyValueFromListParameter(String inputPropertyListName, int indexInTheList, + String... nestedPropertyName) { List<Object> propertyList = new ArrayList<>(); propertyList.add(inputPropertyListName); propertyList.add(indexInTheList); @@ -628,8 +596,7 @@ public class DataModelUtil { * @param propertyDefinition the property definition * @return the parameter definition ext */ - public static ParameterDefinitionExt convertPropertyDefToParameterDef( - PropertyDefinition propertyDefinition) { + public static ParameterDefinitionExt convertPropertyDefToParameterDef(PropertyDefinition propertyDefinition) { if (propertyDefinition == null) { return null; } @@ -641,8 +608,8 @@ public class DataModelUtil { parameterDefinition.set_default(propertyDefinition.get_default()); parameterDefinition.setStatus(propertyDefinition.getStatus()); parameterDefinition.setConstraints(propertyDefinition.getConstraints()); - parameterDefinition.setEntry_schema(Objects.isNull(propertyDefinition.getEntry_schema()) ? null - : propertyDefinition.getEntry_schema().clone()); + parameterDefinition.setEntry_schema(Objects.isNull(propertyDefinition.getEntry_schema()) ? null : + propertyDefinition.getEntry_schema().clone()); parameterDefinition.setHidden(false); parameterDefinition.setImmutable(false); return parameterDefinition; @@ -655,8 +622,8 @@ public class DataModelUtil { * @param outputValue the output value * @return the parameter definition ext */ - public static ParameterDefinitionExt convertAttributeDefToParameterDef( - AttributeDefinition attributeDefinition, Map<String, List> outputValue) { + public static ParameterDefinitionExt convertAttributeDefToParameterDef(AttributeDefinition attributeDefinition, + Map<String, List> outputValue) { if (attributeDefinition == null) { return null; } @@ -668,7 +635,7 @@ public class DataModelUtil { public static boolean isNodeTemplate(String entryId, ServiceTemplate serviceTemplate) { return serviceTemplate.getTopology_template().getNode_templates() != null - && serviceTemplate.getTopology_template().getNode_templates().get(entryId) != null; + && serviceTemplate.getTopology_template().getNode_templates().get(entryId) != null; } /** @@ -679,12 +646,11 @@ public class DataModelUtil { * @param parameterDefinition the parameter definition */ public static void addInputParameterToTopologyTemplate(ServiceTemplate serviceTemplate, - String parameterDefinitionId, - ParameterDefinition parameterDefinition) { + String parameterDefinitionId, + ParameterDefinition parameterDefinition) { if (Objects.isNull(serviceTemplate)) { - throw new CoreException( - new InvalidAddActionNullEntityErrorBuilder("Topology Template Input Parameter", - SERVICE_TEMPLATE).build()); + throw new CoreException(new InvalidAddActionNullEntityErrorBuilder("Topology Template Input Parameter", + SERVICE_TEMPLATE).build()); } TopologyTemplate topologyTemplate = serviceTemplate.getTopology_template(); if (Objects.isNull(topologyTemplate)) { @@ -705,12 +671,11 @@ public class DataModelUtil { * @param parameterDefinition the parameter definition */ public static void addOutputParameterToTopologyTemplate(ServiceTemplate serviceTemplate, - String parameterDefinitionId, - ParameterDefinition parameterDefinition) { + String parameterDefinitionId, + ParameterDefinition parameterDefinition) { if (Objects.isNull(serviceTemplate)) { - throw new CoreException( - new InvalidAddActionNullEntityErrorBuilder("Topology Template Output Parameter", - SERVICE_TEMPLATE).build()); + throw new CoreException(new InvalidAddActionNullEntityErrorBuilder("Topology Template Output Parameter", + SERVICE_TEMPLATE).build()); } TopologyTemplate topologyTemplate = serviceTemplate.getTopology_template(); if (Objects.isNull(topologyTemplate)) { @@ -730,7 +695,7 @@ public class DataModelUtil { * @param requirementDef added requirement def */ public static void addRequirementToList(List<Map<String, RequirementDefinition>> requirementList, - Map<String, RequirementDefinition> requirementDef) { + Map<String, RequirementDefinition> requirementDef) { if (requirementDef == null) { return; } @@ -748,8 +713,7 @@ public class DataModelUtil { * * @param nodeTemplate node template */ - public static Map<String, RequirementAssignment> getNodeTemplateRequirements( - NodeTemplate nodeTemplate) { + public static Map<String, RequirementAssignment> getNodeTemplateRequirements(NodeTemplate nodeTemplate) { if (Objects.isNull(nodeTemplate)) { return null; } @@ -761,14 +725,11 @@ public class DataModelUtil { } YamlUtil yamlUtil = new YamlUtil(); for (Map<String, RequirementAssignment> requirementAssignmentMap : templateRequirements) { - for (Map.Entry<String, RequirementAssignment> requirementEntry : requirementAssignmentMap - .entrySet()) { - RequirementAssignment requirementAssignment = ( - yamlUtil - .yamlToObject(yamlUtil.objectToYaml(requirementEntry.getValue()), - RequirementAssignment.class)); - nodeTemplateRequirementsAssignment - .put(requirementEntry.getKey(), requirementAssignment); + for (Map.Entry<String, RequirementAssignment> requirementEntry : requirementAssignmentMap.entrySet()) { + RequirementAssignment requirementAssignment = + (yamlUtil.yamlToObject(yamlUtil.objectToYaml(requirementEntry.getValue()), + RequirementAssignment.class)); + nodeTemplateRequirementsAssignment.put(requirementEntry.getKey(), requirementAssignment); } } return nodeTemplateRequirementsAssignment; @@ -780,24 +741,20 @@ public class DataModelUtil { * @param nodeTemplate the node template * @return the node template requirement list and null if the node has no requirements */ - public static List<Map<String, RequirementAssignment>> getNodeTemplateRequirementList( - NodeTemplate nodeTemplate) { + public static List<Map<String, RequirementAssignment>> getNodeTemplateRequirementList(NodeTemplate nodeTemplate) { ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil(); //Creating concrete objects List<Map<String, RequirementAssignment>> requirements = nodeTemplate.getRequirements(); List<Map<String, RequirementAssignment>> concreteRequirementList = null; if (requirements != null) { concreteRequirementList = new ArrayList<>(); - ListIterator<Map<String, RequirementAssignment>> reqListIterator = requirements - .listIterator(); + ListIterator<Map<String, RequirementAssignment>> reqListIterator = requirements.listIterator(); while (reqListIterator.hasNext()) { Map<String, RequirementAssignment> requirement = reqListIterator.next(); Map<String, RequirementAssignment> concreteRequirement = new HashMap<>(); for (Map.Entry<String, RequirementAssignment> reqEntry : requirement.entrySet()) { - RequirementAssignment requirementAssignment = ( - toscaExtensionYamlUtil - .yamlToObject(toscaExtensionYamlUtil.objectToYaml(reqEntry.getValue()), - RequirementAssignment.class)); + RequirementAssignment requirementAssignment = (toscaExtensionYamlUtil.yamlToObject( + toscaExtensionYamlUtil.objectToYaml(reqEntry.getValue()), RequirementAssignment.class)); concreteRequirement.put(reqEntry.getKey(), requirementAssignment); concreteRequirementList.add(concreteRequirement); reqListIterator.remove(); @@ -816,9 +773,8 @@ public class DataModelUtil { * @param requirementsAssignmentList requirement definition list * @param requirementKey requirement key */ - public static Optional<List<RequirementAssignment>> getRequirementAssignment( - List<Map<String, RequirementAssignment>> requirementsAssignmentList, - String requirementKey) { + public static Optional<List<RequirementAssignment>> getRequirementAssignment(List<Map<String, RequirementAssignment>> requirementsAssignmentList, + String requirementKey) { if (CollectionUtils.isEmpty(requirementsAssignmentList)) { return Optional.empty(); } @@ -827,10 +783,9 @@ public class DataModelUtil { for (Map<String, RequirementAssignment> requirementMap : requirementsAssignmentList) { if (requirementMap.containsKey(requirementKey)) { YamlUtil yamlUtil = new YamlUtil(); - RequirementAssignment requirementAssignment = ( - yamlUtil - .yamlToObject(yamlUtil.objectToYaml(requirementMap.get(requirementKey)), - RequirementAssignment.class)); + RequirementAssignment requirementAssignment = + (yamlUtil.yamlToObject(yamlUtil.objectToYaml(requirementMap.get(requirementKey)), + RequirementAssignment.class)); matchRequirementAssignmentList.add(requirementAssignment); } } @@ -846,9 +801,8 @@ public class DataModelUtil { * @param requirementsDefinitionList requirement definition list * @param requirementKey requirement key */ - public static void removeRequirementsDefinition( - List<Map<String, RequirementDefinition>> requirementsDefinitionList, - String requirementKey) { + public static void removeRequirementsDefinition(List<Map<String, RequirementDefinition>> requirementsDefinitionList, + String requirementKey) { if (requirementsDefinitionList == null) { return; } @@ -871,9 +825,8 @@ public class DataModelUtil { * @param requirementsAssignmentList requirement Assignment list * @param requirementKey requirement key */ - public static void removeRequirementsAssignment( - List<Map<String, RequirementAssignment>> requirementsAssignmentList, - String requirementKey) { + public static void removeRequirementsAssignment(List<Map<String, RequirementAssignment>> requirementsAssignmentList, + String requirementKey) { if (requirementsAssignmentList == null) { return; } @@ -898,13 +851,10 @@ public class DataModelUtil { * @param requirementKey the requirement key * @param requirementAssignmentToBeDeleted the requirement assignment to be deleted */ - public static void removeRequirementAssignment( - NodeTemplate nodeTemplate, - String requirementKey, - RequirementAssignment requirementAssignmentToBeDeleted) { + public static void removeRequirementAssignment(NodeTemplate nodeTemplate, String requirementKey, + RequirementAssignment requirementAssignmentToBeDeleted) { ToscaAnalyzerService toscaAnalyzerService = new ToscaAnalyzerServiceImpl(); - List<Map<String, RequirementAssignment>> nodeTemplateRequirements = nodeTemplate - .getRequirements(); + List<Map<String, RequirementAssignment>> nodeTemplateRequirements = nodeTemplate.getRequirements(); if (nodeTemplateRequirements == null) { return; } @@ -915,10 +865,12 @@ public class DataModelUtil { RequirementAssignment requirementAssignment = reqMap.get(requirementKey); if (requirementAssignment != null) { boolean isDesiredRequirementAssignment = toscaAnalyzerService - .isDesiredRequirementAssignment(requirementAssignment, - requirementAssignmentToBeDeleted.getCapability(), - requirementAssignmentToBeDeleted.getNode(), - requirementAssignmentToBeDeleted.getRelationship()); + .isDesiredRequirementAssignment(requirementAssignment, + requirementAssignmentToBeDeleted + .getCapability(), + requirementAssignmentToBeDeleted.getNode(), + requirementAssignmentToBeDeleted + .getRelationship()); if (isDesiredRequirementAssignment) { iter.remove(); } @@ -950,8 +902,7 @@ public class DataModelUtil { * @param importId namespace * @return true if exist, false if not exist */ - public static boolean isImportAddedToServiceTemplate(List<Map<String, Import>> imports, - String importId) { + public static boolean isImportAddedToServiceTemplate(List<Map<String, Import>> imports, String importId) { for (Map<String, Import> anImport : imports) { if (anImport.containsKey(importId)) { return true; @@ -967,11 +918,9 @@ public class DataModelUtil { * @param outputParameterId output parameter id * @return ParameterDefinition - output parameter */ - public static ParameterDefinition getOuputParameter(ServiceTemplate serviceTemplate, - String outputParameterId) { - if (serviceTemplate == null - || serviceTemplate.getTopology_template() == null - || serviceTemplate.getTopology_template().getOutputs() == null) { + public static ParameterDefinition getOuputParameter(ServiceTemplate serviceTemplate, String outputParameterId) { + if (serviceTemplate == null || serviceTemplate.getTopology_template() == null + || serviceTemplate.getTopology_template().getOutputs() == null) { return null; } return serviceTemplate.getTopology_template().getOutputs().get(outputParameterId); @@ -983,11 +932,9 @@ public class DataModelUtil { * @param serviceTemplate the service template * @return the input parameters */ - public static Map<String, ParameterDefinition> getInputParameters(ServiceTemplate - serviceTemplate) { - if (serviceTemplate == null - || serviceTemplate.getTopology_template() == null - || serviceTemplate.getTopology_template().getInputs() == null) { + public static Map<String, ParameterDefinition> getInputParameters(ServiceTemplate serviceTemplate) { + if (serviceTemplate == null || serviceTemplate.getTopology_template() == null + || serviceTemplate.getTopology_template().getInputs() == null) { return null; } return serviceTemplate.getTopology_template().getInputs(); @@ -999,11 +946,9 @@ public class DataModelUtil { * @param serviceTemplate the service template * @return the relationship template */ - public static Map<String, RelationshipTemplate> getRelationshipTemplates(ServiceTemplate - serviceTemplate) { - if (serviceTemplate == null - || serviceTemplate.getTopology_template() == null - || serviceTemplate.getTopology_template().getRelationship_templates() == null) { + public static Map<String, RelationshipTemplate> getRelationshipTemplates(ServiceTemplate serviceTemplate) { + if (serviceTemplate == null || serviceTemplate.getTopology_template() == null + || serviceTemplate.getTopology_template().getRelationship_templates() == null) { return null; } return serviceTemplate.getTopology_template().getRelationship_templates(); @@ -1016,10 +961,8 @@ public class DataModelUtil { * @param propertyId property id * @return Object property Value */ - public static Object getPropertyValue(NodeTemplate nodeTemplate, - String propertyId) { - if (nodeTemplate == null - || nodeTemplate.getProperties() == null) { + public static Object getPropertyValue(NodeTemplate nodeTemplate, String propertyId) { + if (nodeTemplate == null || nodeTemplate.getProperties() == null) { return null; } return nodeTemplate.getProperties().get(propertyId); @@ -1033,15 +976,13 @@ public class DataModelUtil { * @return node template properties */ public static Map<String, Object> getNodeTemplateProperties(ServiceTemplate serviceTemplate, - String nodeTemplateId) { - if (serviceTemplate == null - || serviceTemplate.getTopology_template() == null - || serviceTemplate.getTopology_template().getNode_templates() == null - || serviceTemplate.getTopology_template().getNode_templates().get(nodeTemplateId) == null) { + String nodeTemplateId) { + if (serviceTemplate == null || serviceTemplate.getTopology_template() == null + || serviceTemplate.getTopology_template().getNode_templates() == null + || serviceTemplate.getTopology_template().getNode_templates().get(nodeTemplateId) == null) { return null; } - return serviceTemplate.getTopology_template().getNode_templates().get(nodeTemplateId) - .getProperties(); + return serviceTemplate.getTopology_template().getNode_templates().get(nodeTemplateId).getProperties(); } /** @@ -1051,9 +992,7 @@ public class DataModelUtil { * @param propertyKey the property key * @param propertyValue the property value */ - public static void addNodeTemplateProperty(NodeTemplate nodeTemplate, - String propertyKey, - Object propertyValue) { + public static void addNodeTemplateProperty(NodeTemplate nodeTemplate, String propertyKey, Object propertyValue) { if (Objects.isNull(nodeTemplate)) { return; } @@ -1072,9 +1011,8 @@ public class DataModelUtil { * @return the substitution mappings */ public static SubstitutionMapping getSubstitutionMappings(ServiceTemplate serviceTemplate) { - if (serviceTemplate == null - || serviceTemplate.getTopology_template() == null - || serviceTemplate.getTopology_template().getSubstitution_mappings() == null) { + if (serviceTemplate == null || serviceTemplate.getTopology_template() == null + || serviceTemplate.getTopology_template().getSubstitution_mappings() == null) { return null; } return serviceTemplate.getTopology_template().getSubstitution_mappings(); @@ -1088,11 +1026,8 @@ public class DataModelUtil { * @param second the second requirement assignment object * @return true if objects are equal and false otherwise */ - public static boolean compareRequirementAssignment(RequirementAssignment first, - RequirementAssignment second) { - return ( - first.getCapability().equals(second.getCapability()) - && first.getNode().equals(second.getNode()) + public static boolean compareRequirementAssignment(RequirementAssignment first, RequirementAssignment second) { + return (first.getCapability().equals(second.getCapability()) && first.getNode().equals(second.getNode()) && first.getRelationship().equals(second.getRelationship())); } @@ -1126,8 +1061,7 @@ public class DataModelUtil { ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream); objectOutputStream.writeObject(obj); //Deserialize object - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream - .toByteArray()); + ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray()); ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream); clonedObjectValue = objectInputStream.readObject(); } catch (NotSerializableException ex) { @@ -1147,15 +1081,12 @@ public class DataModelUtil { * @param nodeTemplate the node template * @param count the count */ - public static void addSubstitutionFilteringProperty(String templateName, - NodeTemplate nodeTemplate, int count) { + public static void addSubstitutionFilteringProperty(String templateName, NodeTemplate nodeTemplate, int count) { Map<String, Object> serviceTemplateFilterPropertyValue = new HashMap<>(); Map<String, Object> properties = nodeTemplate.getProperties(); - serviceTemplateFilterPropertyValue.put(ToscaConstants - .SUBSTITUTE_SERVICE_TEMPLATE_PROPERTY_NAME, templateName); + serviceTemplateFilterPropertyValue.put(ToscaConstants.SUBSTITUTE_SERVICE_TEMPLATE_PROPERTY_NAME, templateName); serviceTemplateFilterPropertyValue.put(ToscaConstants.COUNT_PROPERTY_NAME, count); - properties.put(ToscaConstants.SERVICE_TEMPLATE_FILTER_PROPERTY_NAME, - serviceTemplateFilterPropertyValue); + properties.put(ToscaConstants.SERVICE_TEMPLATE_FILTER_PROPERTY_NAME, serviceTemplateFilterPropertyValue); nodeTemplate.setProperties(properties); } @@ -1165,14 +1096,12 @@ public class DataModelUtil { * @param computeNodeTemplateId compute node template id * @param portNodeTemplate port node template */ - public static void addBindingReqFromPortToCompute(String computeNodeTemplateId, - NodeTemplate portNodeTemplate) { + public static void addBindingReqFromPortToCompute(String computeNodeTemplateId, NodeTemplate portNodeTemplate) { RequirementAssignment requirementAssignment = new RequirementAssignment(); requirementAssignment.setCapability(ToscaCapabilityType.NATIVE_NETWORK_BINDABLE); requirementAssignment.setRelationship(ToscaRelationshipType.NATIVE_NETWORK_BINDS_TO); requirementAssignment.setNode(computeNodeTemplateId); - addRequirementAssignment(portNodeTemplate, ToscaConstants.BINDING_REQUIREMENT_ID, - requirementAssignment); + addRequirementAssignment(portNodeTemplate, ToscaConstants.BINDING_REQUIREMENT_ID, requirementAssignment); } /** @@ -1183,18 +1112,15 @@ public class DataModelUtil { * @param mapping the mapping * @return the substitution mapping */ - public static SubstitutionMapping createSubstitutionTemplateSubMapping( - String nodeTypeKey, - NodeType substitutionNodeType, - Map<String, Map<String, List<String>>> mapping) { + public static SubstitutionMapping createSubstitutionTemplateSubMapping(String nodeTypeKey, + NodeType substitutionNodeType, + Map<String, Map<String, List<String>>> mapping) { SubstitutionMapping substitutionMapping = new SubstitutionMapping(); substitutionMapping.setNode_type(nodeTypeKey); - substitutionMapping.setCapabilities( - manageCapabilityMapping(substitutionNodeType.getCapabilities(), mapping.get(ToscaConstants - .CAPABILITY))); + substitutionMapping.setCapabilities(manageCapabilityMapping(substitutionNodeType.getCapabilities(), + mapping.get(ToscaConstants.CAPABILITY))); substitutionMapping.setRequirements( - manageRequirementMapping(substitutionNodeType.getRequirements(), - mapping.get("requirement"))); + manageRequirementMapping(substitutionNodeType.getRequirements(), mapping.get("requirement"))); return substitutionMapping; } @@ -1207,8 +1133,8 @@ public class DataModelUtil { * @param capabilityAttributes the capability attributes */ public static void addNodeTemplateCapability(NodeTemplate nodeTemplate, String capabilityId, - Map<String, Object> capabilityProperties, - Map<String, Object> capabilityAttributes) { + Map<String, Object> capabilityProperties, + Map<String, Object> capabilityAttributes) { Map<String, CapabilityAssignment> capabilities = nodeTemplate.getCapabilities(); if (Objects.isNull(capabilities)) { capabilities = new HashMap<>(); @@ -1220,9 +1146,8 @@ public class DataModelUtil { nodeTemplate.setCapabilities(capabilities); } - private static Map<String, List<String>> manageRequirementMapping( - List<Map<String, RequirementDefinition>> requirementList, - Map<String, List<String>> requirementSubstitutionMapping) { + private static Map<String, List<String>> manageRequirementMapping(List<Map<String, RequirementDefinition>> requirementList, + Map<String, List<String>> requirementSubstitutionMapping) { if (requirementList == null) { return null; } @@ -1239,9 +1164,8 @@ public class DataModelUtil { return requirementMapping; } - private static Map<String, List<String>> manageCapabilityMapping( - Map<String, CapabilityDefinition> capabilities, - Map<String, List<String>> capabilitySubstitutionMapping) { + private static Map<String, List<String>> manageCapabilityMapping(Map<String, CapabilityDefinition> capabilities, + Map<String, List<String>> capabilitySubstitutionMapping) { if (capabilities == null) { return null; } @@ -1257,14 +1181,18 @@ public class DataModelUtil { return capabilityMapping; } - - public static void addInterfaceOperation(ServiceTemplate serviceTemplate, - String interfaceId, - String operationId, - OperationDefinition operationDefinition) { + /** + * Add interface operation. + * + * @param serviceTemplate the service template + * @param interfaceId the interface id + * @param operationId the operation id + * @param operationDefinition the operation definition + */ + public static void addInterfaceOperation(ServiceTemplate serviceTemplate, String interfaceId, String operationId, + OperationDefinition operationDefinition) { Map<String, Object> interfaceTypes = serviceTemplate.getInterface_types(); - if (MapUtils.isEmpty(interfaceTypes) - || Objects.isNull(interfaceTypes.get(interfaceId))) { + if (MapUtils.isEmpty(interfaceTypes) || Objects.isNull(interfaceTypes.get(interfaceId))) { return; } @@ -1289,8 +1217,7 @@ public class DataModelUtil { interfaceValue -> convertedInterfaceTypes.put(interfaceEntry.getKey(), interfaceValue)); } catch (Exception e) { LOGGER.error("Cannot create interface object", e); - throw new CoreException( - new ToscaInvalidInterfaceValueErrorBuilder(e.getMessage()).build()); + throw new CoreException(new ToscaInvalidInterfaceValueErrorBuilder(e.getMessage()).build()); } } @@ -1298,35 +1225,23 @@ public class DataModelUtil { } public static <T extends InterfaceDefinition> Optional<T> convertObjToInterfaceDefinition(String interfaceId, - Object interfaceObj, - Class<T> interfaceClass) { + Object interfaceObj, + Class<T> interfaceClass) { try { - Optional<T> interfaceDefinition = - CommonUtil.createObjectUsingSetters(interfaceObj, interfaceClass); + Optional<T> interfaceDefinition = CommonUtil.createObjectUsingSetters(interfaceObj, interfaceClass); interfaceDefinition.ifPresent(interfaceDefinitionType1 -> updateInterfaceDefinitionOperations( - CommonUtil.getObjectAsMap(interfaceObj), - interfaceDefinitionType1, getOperationClass(interfaceClass))); + CommonUtil.getObjectAsMap(interfaceObj), interfaceDefinitionType1)); return interfaceDefinition; } catch (Exception ex) { - LOGGER.error("Could not create {} from {}", InterfaceDefinitionType.class.getName(), - interfaceId, ex); - throw new CoreException( - new CreateInterfaceObjectErrorBuilder(InterfaceDefinitionType.class.getName(), - interfaceId, - ex.getMessage()).build()); + LOGGER.error("Could not create {} from {}", InterfaceDefinitionType.class.getName(), interfaceId, ex); + throw new CoreException(new CreateInterfaceObjectErrorBuilder(InterfaceDefinitionType.class.getName(), + interfaceId, ex.getMessage()).build()); } } - private static <T extends OperationDefinition, V extends InterfaceDefinition> Class<T> getOperationClass( - Class<V> interfaceClass) { - return interfaceClass.equals(InterfaceDefinitionType.class) - ? (Class<T>) OperationDefinitionType.class - : (Class<T>) OperationDefinitionTemplate.class; - } - public static Optional<InterfaceType> convertObjToInterfaceType(String interfaceId, - Object interfaceObj) { + public static Optional<InterfaceType> convertObjToInterfaceType(String interfaceId, Object interfaceObj) { try { Optional<InterfaceType> interfaceType = CommonUtil.createObjectUsingSetters(interfaceObj, InterfaceType.class); @@ -1336,9 +1251,8 @@ public class DataModelUtil { return interfaceType; } catch (Exception ex) { LOGGER.error("Could not create {} from {}", InterfaceType.class.getName(), interfaceId, ex); - throw new CoreException( - new CreateInterfaceObjectErrorBuilder(InterfaceType.class.getName(), interfaceId, - ex.getMessage()).build()); + throw new CoreException(new CreateInterfaceObjectErrorBuilder(InterfaceType.class.getName(), interfaceId, + ex.getMessage()).build()); } } @@ -1346,6 +1260,10 @@ public class DataModelUtil { return converInterfaceToToscaInterfaceObj(interfaceType); } + public static Optional<Object> convertInterfaceDefinitionTypeToObj(InterfaceDefinitionType interfaceDefinitionType) { + return converInterfaceToToscaInterfaceObj(interfaceDefinitionType); + } + private static Optional<Object> converInterfaceToToscaInterfaceObj(Object interfaceEntity) { if (Objects.isNull(interfaceEntity)) { return Optional.empty(); @@ -1363,75 +1281,53 @@ public class DataModelUtil { return Optional.of(objectMapper.convertValue(interfaceAsMap, Object.class)); } - private static void updateInterfaceTypeOperations(Map<String, Object> interfaceAsMap, - InterfaceType interfaceType) { + private static void updateInterfaceTypeOperations(Map<String, Object> interfaceAsMap, InterfaceType interfaceType) { Set<String> fieldNames = CommonUtil.getClassFieldNames(InterfaceType.class); for (Map.Entry<String, Object> entry : interfaceAsMap.entrySet()) { Optional<? extends OperationDefinition> operationDefinition = - createOperation(entry.getKey(), entry.getValue(), fieldNames, - OperationDefinitionType.class); - operationDefinition - .ifPresent(operation -> interfaceType.addOperation(entry.getKey(), operation)); + createOperation(entry.getKey(), entry.getValue(), fieldNames, OperationDefinitionType.class); + operationDefinition.ifPresent(operation -> interfaceType.addOperation(entry.getKey(), operation)); } } private static Optional<? extends OperationDefinition> createOperation(String propertyName, - Object operationCandidate, - Set<String> fieldNames, - Class<? extends OperationDefinition> - operationClass) { + Object operationCandidate, + Set<String> fieldNames, + Class<? extends OperationDefinition> operationClass) { if (!fieldNames.contains(propertyName)) { try { return CommonUtil.createObjectUsingSetters(operationCandidate, operationClass); } catch (Exception ex) { LOGGER.error("Could not create Operation from {}", propertyName, ex); - throw new CoreException( - new CreateInterfaceOperationObjectErrorBuilder(propertyName, ex.getMessage()).build()); + throw new CoreException(new CreateInterfaceOperationObjectErrorBuilder(propertyName, ex.getMessage()) + .build()); } } return Optional.empty(); } - private static <T extends OperationDefinition> void updateInterfaceDefinitionOperations(Map<String, Object> - interfaceAsMap, - InterfaceDefinition - interfaceDefinition, - Class<T> operationClass) { - Set<String> fieldNames = CommonUtil.getClassFieldNames(interfaceDefinition.getClass()); - Optional<? extends OperationDefinition> operationDefinition; + private static <T extends OperationDefinition> void updateInterfaceDefinitionOperations(Map<String, Object> interfaceAsMap, + InterfaceDefinition interfaceDefinition) { + { + Set<String> fieldNames = CommonUtil.getClassFieldNames(InterfaceDefinitionType.class); + Optional<? extends OperationDefinition> operationDefinition; - for (Map.Entry<String, Object> entry : interfaceAsMap.entrySet()) { - operationDefinition = - createOperation(entry.getKey(), entry.getValue(), fieldNames, operationClass); - operationDefinition.ifPresent(operation -> addOperationToInterface(interfaceDefinition, - entry.getKey(), operation)); + for (Map.Entry<String, Object> entry : interfaceAsMap.entrySet()) { + operationDefinition = createOperation(entry.getKey(), entry.getValue(), fieldNames, + interfaceDefinition instanceof InterfaceDefinitionType ? OperationDefinitionType.class : + OperationDefinitionTemplate.class); + operationDefinition.ifPresent(operation -> interfaceDefinition.addOperation(entry.getKey(), operation)); + } } } - private static void addOperationToInterface(InterfaceDefinition interfaceDefinition, - String operationName, - OperationDefinition operationDefinition) { - if (interfaceDefinition instanceof InterfaceDefinitionType) { - InterfaceDefinitionType interfaceDefinitionType = - (InterfaceDefinitionType) interfaceDefinition; - interfaceDefinitionType.addOperation(operationName, (OperationDefinitionType) - operationDefinition); - } - if (interfaceDefinition instanceof InterfaceDefinitionTemplate) { - InterfaceDefinitionTemplate interfaceDefinitionTemplate = - (InterfaceDefinitionTemplate) interfaceDefinition; - interfaceDefinitionTemplate.addOperation(operationName, (OperationDefinitionTemplate) - operationDefinition); - } - } public static void addSubstitutionNodeTypeRequirements(NodeType substitutionNodeType, - List<Map<String, RequirementDefinition>> - requirementsList, - String templateName) { + List<Map<String, RequirementDefinition>> requirementsList, + String templateName) { if (CollectionUtils.isEmpty(requirementsList)) { return; } @@ -1448,9 +1344,27 @@ public class DataModelUtil { } } - public static boolean isNodeTemplateSectionMissingFromServiceTemplate( - ServiceTemplate serviceTemplate) { - return Objects.isNull(serviceTemplate.getTopology_template()) - || MapUtils.isEmpty(serviceTemplate.getTopology_template().getNode_templates()); + public static boolean isNodeTemplateSectionMissingFromServiceTemplate(ServiceTemplate serviceTemplate) { + return Objects.isNull(serviceTemplate.getTopology_template()) || MapUtils.isEmpty( + serviceTemplate.getTopology_template().getNode_templates()); + } + + /** + * Gets relationship template in a service template according to the relationship id. + * + * @param serviceTemplate the service template + * @param relationshipId the relationship id + * @return the relationship template + */ + public static Optional<RelationshipTemplate> getRelationshipTemplate(ServiceTemplate serviceTemplate, + String relationshipId) { + if (serviceTemplate == null || serviceTemplate.getTopology_template() == null + || serviceTemplate.getTopology_template().getRelationship_templates() == null + || serviceTemplate.getTopology_template().getRelationship_templates().get(relationshipId) == null) { + return Optional.empty(); + } + return Optional.of(serviceTemplate.getTopology_template().getRelationship_templates().get(relationshipId)); } + + } diff --git a/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/services/ToscaAnalyzerService.java b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/services/ToscaAnalyzerService.java index e4d75c1296..34f5a905fd 100644 --- a/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/services/ToscaAnalyzerService.java +++ b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/services/ToscaAnalyzerService.java @@ -17,7 +17,7 @@ package org.openecomp.sdc.tosca.services; import org.openecomp.sdc.tosca.datatypes.ToscaElementTypes; -import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel; +import org.openecomp.sdc.tosca.datatypes.ToscaFlatData; import org.onap.sdc.tosca.datatypes.model.CapabilityDefinition; import org.onap.sdc.tosca.datatypes.model.DefinitionOfDataType; import org.onap.sdc.tosca.datatypes.model.InterfaceDefinitionType; @@ -33,67 +33,62 @@ import java.util.List; import java.util.Map; import java.util.Optional; -public interface ToscaAnalyzerService { +import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel; - /* - node template with type equal to node type or derived from node type - */ - Map<String, NodeTemplate> getNodeTemplatesByType(ServiceTemplate serviceTemplate, String nodeType, - ToscaServiceModel toscaServiceModel); +public interface ToscaAnalyzerService { - Optional<NodeType> fetchNodeType(String nodeTypeKey, Collection<ServiceTemplate> serviceTemplates); + /* + node template with type equal to node type or derived from node type + */ + Map<String, NodeTemplate> getNodeTemplatesByType(ServiceTemplate serviceTemplate, String nodeType, + ToscaServiceModel toscaServiceModel); - boolean isTypeOf(NodeTemplate nodeTemplate, String nodeType, ServiceTemplate serviceTemplate, - ToscaServiceModel toscaServiceModel); + Optional<NodeType> fetchNodeType(String nodeTypeKey, Collection<ServiceTemplate> serviceTemplates); - List<RequirementAssignment> getRequirements(NodeTemplate nodeTemplate, String requirementId); + boolean isTypeOf(NodeTemplate nodeTemplate, String nodeType, ServiceTemplate serviceTemplate, + ToscaServiceModel toscaServiceModel); - Optional<NodeTemplate> getNodeTemplateById(ServiceTemplate serviceTemplate, - String nodeTemplateId); + boolean isTypeOf(InterfaceDefinitionType interfaceDefinition, String interfaceType, ServiceTemplate serviceTemplate, + ToscaServiceModel toscaServiceModel); - Optional<String> getSubstituteServiceTemplateName(String substituteNodeTemplateId, - NodeTemplate substitutableNodeTemplate); + boolean isTypeOf(DefinitionOfDataType parameterDefinition, String dataType, ServiceTemplate serviceTemplate, + ToscaServiceModel toscaServiceModel); - Map<String, NodeTemplate> getSubstitutableNodeTemplates(ServiceTemplate serviceTemplate); + List<RequirementAssignment> getRequirements(NodeTemplate nodeTemplate, String requirementId); - Optional<Map.Entry<String, NodeTemplate>> getSubstitutionMappedNodeTemplateByExposedReq( - String substituteServiceTemplateFileName, ServiceTemplate substituteServiceTemplate, - String requirementId); + Optional<NodeTemplate> getNodeTemplateById(ServiceTemplate serviceTemplate, String nodeTemplateId); - /* - match only for the input which is not null - */ - boolean isDesiredRequirementAssignment(RequirementAssignment requirementAssignment, - String capability, String node, String relationship); + Optional<String> getSubstituteServiceTemplateName(String substituteNodeTemplateId, + NodeTemplate substitutableNodeTemplate); - Object getFlatEntity(ToscaElementTypes elementType, String type, ServiceTemplate serviceTemplate, - ToscaServiceModel toscaModel); + Map<String, NodeTemplate> getSubstitutableNodeTemplates(ServiceTemplate serviceTemplate); - boolean isSubstitutableNodeTemplate(NodeTemplate nodeTemplate); + Optional<Map.Entry<String, NodeTemplate>> getSubstitutionMappedNodeTemplateByExposedReq(String substituteServiceTemplateFileName, + ServiceTemplate substituteServiceTemplate, + String requirementId); - NodeType createInitSubstitutionNodeType(ServiceTemplate substitutionServiceTemplate, - String nodeTypeDerivedFromValue); + /* + match only for the input which is not null + */ + boolean isDesiredRequirementAssignment(RequirementAssignment requirementAssignment, String capability, String node, + String relationship); - boolean isRequirementExistInNodeTemplate(NodeTemplate nodeTemplate, - String requirementId, - RequirementAssignment requirementAssignment); + ToscaFlatData getFlatEntity(ToscaElementTypes elementType, String type, ServiceTemplate serviceTemplate, + ToscaServiceModel toscaModel); - Map<String, PropertyDefinition> manageSubstitutionNodeTypeProperties( - ServiceTemplate substitutionServiceTemplate); + boolean isSubstitutableNodeTemplate(NodeTemplate nodeTemplate); - Map<String, CapabilityDefinition> calculateExposedCapabilities( - Map<String, CapabilityDefinition> nodeTypeCapabilitiesDefinition, - Map<String, Map<String, RequirementAssignment>> fullFilledRequirementsDefinitionMap); + NodeType createInitSubstitutionNodeType(ServiceTemplate substitutionServiceTemplate, + String nodeTypeDerivedFromValue); - List<Map<String, RequirementDefinition>> calculateExposedRequirements( - List<Map<String, RequirementDefinition>> nodeTypeRequirementsDefinitionList, - Map<String, RequirementAssignment> nodeTemplateRequirementsAssignment); + boolean isRequirementExistInNodeTemplate(NodeTemplate nodeTemplate, String requirementId, + RequirementAssignment requirementAssignment); - boolean isTypeOf(InterfaceDefinitionType interfaceDefinition, String interfaceType, - ServiceTemplate - serviceTemplate, ToscaServiceModel toscaServiceModel); + Map<String, PropertyDefinition> manageSubstitutionNodeTypeProperties(ServiceTemplate substitutionServiceTemplate); - boolean isTypeOf(DefinitionOfDataType parameterDefinition, String dataType, ServiceTemplate - serviceTemplate, ToscaServiceModel toscaServiceModel); + Map<String, CapabilityDefinition> calculateExposedCapabilities(Map<String, CapabilityDefinition> nodeTypeCapabilitiesDefinition, + Map<String, Map<String, RequirementAssignment>> fullFilledRequirementsDefinitionMap); + List<Map<String, RequirementDefinition>> calculateExposedRequirements(List<Map<String, RequirementDefinition>> nodeTypeRequirementsDefinitionList, + Map<String, RequirementAssignment> nodeTemplateRequirementsAssignment); } diff --git a/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/services/ToscaConstants.java b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/services/ToscaConstants.java index 4ce2535401..d81983057d 100644 --- a/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/services/ToscaConstants.java +++ b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/services/ToscaConstants.java @@ -36,6 +36,9 @@ public class ToscaConstants { //General public static final String TOSCA_DEFINITIONS_VERSION = "tosca_simple_yaml_1_0_0"; public static final String MODELABLE_ENTITY_NAME_SELF = "SELF"; + public static final String MODELABLE_ENTITY_NAME_HOST = "HOST"; + public static final String MODELABLE_ENTITY_NAME_SOURCE = "SOURCE"; + public static final String MODELABLE_ENTITY_NAME_TARGET = "TARGET"; public static final String NODE_TEMPLATE_DIRECTIVE_SUBSTITUTABLE = "substitutable"; public static final String UNBOUNDED = "UNBOUNDED"; public static final String ST_METADATA_TEMPLATE_NAME = "template_name"; diff --git a/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/services/impl/ToscaAnalyzerServiceImpl.java b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/services/impl/ToscaAnalyzerServiceImpl.java index 46d9b902ca..46e58bdc81 100644 --- a/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/services/impl/ToscaAnalyzerServiceImpl.java +++ b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/services/impl/ToscaAnalyzerServiceImpl.java @@ -22,7 +22,7 @@ import org.apache.commons.lang3.StringUtils; import org.openecomp.core.utilities.CommonMethods; import org.openecomp.sdc.common.errors.CoreException; import org.openecomp.sdc.tosca.datatypes.ToscaElementTypes; -import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel; +import org.openecomp.sdc.tosca.datatypes.ToscaFlatData; import org.onap.sdc.tosca.datatypes.model.AttributeDefinition; import org.onap.sdc.tosca.datatypes.model.CapabilityDefinition; import org.onap.sdc.tosca.datatypes.model.CapabilityType; @@ -38,18 +38,18 @@ import org.onap.sdc.tosca.datatypes.model.PropertyType; import org.onap.sdc.tosca.datatypes.model.RequirementAssignment; import org.onap.sdc.tosca.datatypes.model.RequirementDefinition; import org.onap.sdc.tosca.datatypes.model.ServiceTemplate; +import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel; +import org.openecomp.sdc.tosca.errors.CreateInterfaceObjectErrorBuilder; import org.openecomp.sdc.tosca.errors.ToscaElementTypeNotFoundErrorBuilder; import org.openecomp.sdc.tosca.errors.ToscaFileNotFoundErrorBuilder; import org.openecomp.sdc.tosca.errors.ToscaInvalidEntryNotFoundErrorBuilder; import org.openecomp.sdc.tosca.errors.ToscaInvalidSubstituteNodeTemplatePropertiesErrorBuilder; import org.openecomp.sdc.tosca.errors.ToscaInvalidSubstitutionServiceTemplateErrorBuilder; import org.openecomp.sdc.tosca.services.DataModelUtil; +import org.onap.sdc.tosca.services.ToscaExtensionYamlUtil; import org.openecomp.sdc.tosca.services.ToscaAnalyzerService; import org.openecomp.sdc.tosca.services.ToscaConstants; -import org.onap.sdc.tosca.services.ToscaExtensionYamlUtil; -import org.openecomp.sdc.tosca.services.ToscaUtil; -import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -60,877 +60,911 @@ import java.util.Map; import java.util.Objects; import java.util.Optional; import java.util.Set; +import java.lang.reflect.InvocationTargetException; + +import org.openecomp.sdc.tosca.services.ToscaUtil; public class ToscaAnalyzerServiceImpl implements ToscaAnalyzerService { - private final String GET_NODE_TYPE_METHOD_NAME = "getNode_types"; - private final String GET_DERIVED_FROM_METHOD_NAME = "getDerived_from"; - private final String GET_TYPE_METHOD_NAME = "getType"; - private final String GET_DATA_TYPE_METHOD_NAME = "getData_types"; - private final String GET_INTERFACE_TYPE_METHOD_NAME = "getInterface_types"; - private final String TOSCA_DOT = "tosca."; - private final String DOT_ROOT = ".Root"; - - @Override - public List<Map<String, RequirementDefinition>> calculateExposedRequirements( - List<Map<String, RequirementDefinition>> nodeTypeRequirementsDefinitionList, - Map<String, RequirementAssignment> nodeTemplateRequirementsAssignment) { - - if (nodeTypeRequirementsDefinitionList == null) { - return Collections.emptyList(); - } - for (Map.Entry<String, RequirementAssignment> entry : nodeTemplateRequirementsAssignment - .entrySet()) { - if (entry.getValue().getNode() != null) { - Optional<RequirementDefinition> requirementDefinition = - DataModelUtil.getRequirementDefinition(nodeTypeRequirementsDefinitionList, entry - .getKey()); - RequirementDefinition cloneRequirementDefinition; - if (requirementDefinition.isPresent()) { - cloneRequirementDefinition = requirementDefinition.get().clone(); - updateRequirementDefinition(nodeTypeRequirementsDefinitionList, entry, - cloneRequirementDefinition); - } - } else { - for (Map<String, RequirementDefinition> nodeTypeRequirementsMap : - nodeTypeRequirementsDefinitionList) { - updateMinMaxOccurencesForNodeTypeRequirement(entry, nodeTypeRequirementsMap); - } - } - } - return nodeTypeRequirementsDefinitionList; - } - - private void updateMinMaxOccurencesForNodeTypeRequirement( - Map.Entry<String, RequirementAssignment> entry, - Map<String, RequirementDefinition> nodeTypeRequirementsMap) { - Object max = nodeTypeRequirementsMap.get(entry.getKey()).getOccurrences() != null - && nodeTypeRequirementsMap.get(entry.getKey()).getOccurrences().length > 0 - ? nodeTypeRequirementsMap.get(entry.getKey()).getOccurrences()[1] : 1; - Object min = nodeTypeRequirementsMap.get(entry.getKey()).getOccurrences() != null - && nodeTypeRequirementsMap.get(entry.getKey()).getOccurrences().length > 0 - ? nodeTypeRequirementsMap.get(entry.getKey()).getOccurrences()[0] : 1; - nodeTypeRequirementsMap.get(entry.getKey()).setOccurrences(new Object[]{min, max}); - } - - private void updateRequirementDefinition( - List<Map<String, RequirementDefinition>> nodeTypeRequirementsDefinitionList, - Map.Entry<String, RequirementAssignment> entry, - RequirementDefinition cloneRequirementDefinition) { - if (!evaluateRequirementFulfillment(cloneRequirementDefinition)) { - CommonMethods.mergeEntryInList(entry.getKey(), cloneRequirementDefinition, - nodeTypeRequirementsDefinitionList); - } else { - DataModelUtil.removeRequirementsDefinition(nodeTypeRequirementsDefinitionList, entry - .getKey()); - } - } - - private static boolean evaluateRequirementFulfillment(RequirementDefinition - requirementDefinition) { - Object[] occurrences = requirementDefinition.getOccurrences(); - if (occurrences == null) { - requirementDefinition.setOccurrences(new Object[]{1, 1}); - return false; - } - if (occurrences[1].equals(ToscaConstants.UNBOUNDED)) { - return false; - } - - if (occurrences[1].equals(1)) { - return true; - } - occurrences[1] = (Integer) occurrences[1] - 1; - return false; - } - - @Override - public Map<String, CapabilityDefinition> calculateExposedCapabilities( - Map<String, CapabilityDefinition> nodeTypeCapabilitiesDefinition, - Map<String, Map<String, RequirementAssignment>> fullFilledRequirementsDefinitionMap) { - - String capabilityKey; - String capability; - String node; - for (Map.Entry<String, Map<String, RequirementAssignment>> entry : - fullFilledRequirementsDefinitionMap.entrySet()) { - for (Map.Entry<String, RequirementAssignment> fullFilledEntry : entry.getValue().entrySet()) { - - capability = fullFilledEntry.getValue().getCapability(); - node = fullFilledEntry.getValue().getNode(); - capabilityKey = capability + "_" + node; - CapabilityDefinition capabilityDefinition = nodeTypeCapabilitiesDefinition.get( - capabilityKey); - if (capabilityDefinition != null) { - CapabilityDefinition clonedCapabilityDefinition = capabilityDefinition.clone(); - nodeTypeCapabilitiesDefinition.put(capabilityKey, capabilityDefinition.clone()); - updateNodeTypeCapabilitiesDefinition(nodeTypeCapabilitiesDefinition, capabilityKey, - clonedCapabilityDefinition); - } - } - } - - Map<String, CapabilityDefinition> exposedCapabilitiesDefinition = new HashMap<>(); - for (Map.Entry<String, CapabilityDefinition> entry : nodeTypeCapabilitiesDefinition - .entrySet()) { - exposedCapabilitiesDefinition.put(entry.getKey(), entry.getValue()); - } - return exposedCapabilitiesDefinition; - } - - private void updateNodeTypeCapabilitiesDefinition( - Map<String, CapabilityDefinition> nodeTypeCapabilitiesDefinition, String capabilityKey, - CapabilityDefinition clonedCapabilityDefinition) { - if (evaluateCapabilityFulfillment(clonedCapabilityDefinition)) { - nodeTypeCapabilitiesDefinition.remove(capabilityKey); - } else { - nodeTypeCapabilitiesDefinition.put(capabilityKey, clonedCapabilityDefinition); - } - } - - private static boolean evaluateCapabilityFulfillment(CapabilityDefinition capabilityDefinition) { - - Object[] occurrences = capabilityDefinition.getOccurrences(); - if (occurrences == null) { - capabilityDefinition.setOccurrences(new Object[]{1, ToscaConstants.UNBOUNDED}); - return false; - } - if (occurrences[1].equals(ToscaConstants.UNBOUNDED)) { - return false; - } - - if (occurrences[1].equals(1)) { - return true; - } - occurrences[1] = (Integer) occurrences[1] - 1; - return false; - } - - /* - node template with type equal to node type or derived from node type + + private final String GET_NODE_TYPE_METHOD_NAME = "getNode_types"; + private final String GET_DERIVED_FROM_METHOD_NAME = "getDerived_from"; + private final String GET_TYPE_METHOD_NAME = "getType"; + private final String GET_DATA_TYPE_METHOD_NAME = "getData_types"; + private final String GET_INTERFACE_TYPE_METHOD_NAME = "getInterface_types"; + private final String TOSCA_DOT = "tosca."; + private final String DOT_ROOT = ".Root"; + + @Override + public List<Map<String, RequirementDefinition>> calculateExposedRequirements(List<Map<String, RequirementDefinition>> nodeTypeRequirementsDefinitionList, + Map<String, RequirementAssignment> nodeTemplateRequirementsAssignment) { + + if (nodeTypeRequirementsDefinitionList == null) { + return Collections.emptyList(); + } + for (Map.Entry<String, RequirementAssignment> entry : nodeTemplateRequirementsAssignment.entrySet()) { + if (entry.getValue().getNode() != null) { + Optional<RequirementDefinition> requirementDefinition = + DataModelUtil.getRequirementDefinition(nodeTypeRequirementsDefinitionList, entry.getKey()); + RequirementDefinition cloneRequirementDefinition; + if (requirementDefinition.isPresent()) { + cloneRequirementDefinition = requirementDefinition.get().clone(); + updateRequirementDefinition(nodeTypeRequirementsDefinitionList, entry, cloneRequirementDefinition); + } + } else { + for (Map<String, RequirementDefinition> nodeTypeRequirementsMap : nodeTypeRequirementsDefinitionList) { + updateMinMaxOccurencesForNodeTypeRequirement(entry, nodeTypeRequirementsMap); + } + } + } + return nodeTypeRequirementsDefinitionList; + } + + private void updateMinMaxOccurencesForNodeTypeRequirement(Map.Entry<String, RequirementAssignment> entry, + Map<String, RequirementDefinition> nodeTypeRequirementsMap) { + Object max = nodeTypeRequirementsMap.get(entry.getKey()).getOccurrences() != null + && nodeTypeRequirementsMap.get(entry.getKey()).getOccurrences().length > 0 ? + nodeTypeRequirementsMap.get(entry.getKey()).getOccurrences()[1] : 1; + Object min = nodeTypeRequirementsMap.get(entry.getKey()).getOccurrences() != null + && nodeTypeRequirementsMap.get(entry.getKey()).getOccurrences().length > 0 ? + nodeTypeRequirementsMap.get(entry.getKey()).getOccurrences()[0] : 1; + nodeTypeRequirementsMap.get(entry.getKey()).setOccurrences(new Object[] {min, max}); + } + + private void updateRequirementDefinition(List<Map<String, RequirementDefinition>> nodeTypeRequirementsDefinitionList, + Map.Entry<String, RequirementAssignment> entry, + RequirementDefinition cloneRequirementDefinition) { + if (!evaluateRequirementFulfillment(cloneRequirementDefinition)) { + CommonMethods + .mergeEntryInList(entry.getKey(), cloneRequirementDefinition, nodeTypeRequirementsDefinitionList); + } else { + DataModelUtil.removeRequirementsDefinition(nodeTypeRequirementsDefinitionList, entry.getKey()); + } + } + + private static boolean evaluateRequirementFulfillment(RequirementDefinition requirementDefinition) { + Object[] occurrences = requirementDefinition.getOccurrences(); + if (occurrences == null) { + requirementDefinition.setOccurrences(new Object[] {1, 1}); + return false; + } + if (occurrences[1].equals(ToscaConstants.UNBOUNDED)) { + return false; + } + + if (occurrences[1].equals(1)) { + return true; + } + occurrences[1] = (Integer) occurrences[1] - 1; + return false; + } + + @Override + public Map<String, CapabilityDefinition> calculateExposedCapabilities(Map<String, CapabilityDefinition> nodeTypeCapabilitiesDefinition, + Map<String, Map<String, RequirementAssignment>> fullFilledRequirementsDefinitionMap) { + + String capabilityKey; + String capability; + String node; + for (Map.Entry<String, Map<String, RequirementAssignment>> entry : fullFilledRequirementsDefinitionMap + .entrySet()) { + for (Map.Entry<String, RequirementAssignment> fullFilledEntry : entry.getValue().entrySet()) { + + capability = fullFilledEntry.getValue().getCapability(); + node = fullFilledEntry.getValue().getNode(); + capabilityKey = capability + "_" + node; + CapabilityDefinition capabilityDefinition = nodeTypeCapabilitiesDefinition.get(capabilityKey); + if (capabilityDefinition != null) { + CapabilityDefinition clonedCapabilityDefinition = capabilityDefinition.clone(); + nodeTypeCapabilitiesDefinition.put(capabilityKey, capabilityDefinition.clone()); + updateNodeTypeCapabilitiesDefinition(nodeTypeCapabilitiesDefinition, capabilityKey, + clonedCapabilityDefinition); + } + } + } + + Map<String, CapabilityDefinition> exposedCapabilitiesDefinition = new HashMap<>(); + for (Map.Entry<String, CapabilityDefinition> entry : nodeTypeCapabilitiesDefinition.entrySet()) { + exposedCapabilitiesDefinition.put(entry.getKey(), entry.getValue()); + } + return exposedCapabilitiesDefinition; + } + + private void updateNodeTypeCapabilitiesDefinition(Map<String, CapabilityDefinition> nodeTypeCapabilitiesDefinition, + String capabilityKey, + CapabilityDefinition clonedCapabilityDefinition) { + if (evaluateCapabilityFulfillment(clonedCapabilityDefinition)) { + nodeTypeCapabilitiesDefinition.remove(capabilityKey); + } else { + nodeTypeCapabilitiesDefinition.put(capabilityKey, clonedCapabilityDefinition); + } + } + + private static boolean evaluateCapabilityFulfillment(CapabilityDefinition capabilityDefinition) { + + Object[] occurrences = capabilityDefinition.getOccurrences(); + if (occurrences == null) { + capabilityDefinition.setOccurrences(new Object[] {1, ToscaConstants.UNBOUNDED}); + return false; + } + if (occurrences[1].equals(ToscaConstants.UNBOUNDED)) { + return false; + } + + if (occurrences[1].equals(1)) { + return true; + } + occurrences[1] = (Integer) occurrences[1] - 1; + return false; + } + + /* + node template with type equal to node type or derived from node type + */ + @Override + public Map<String, NodeTemplate> getNodeTemplatesByType(ServiceTemplate serviceTemplate, String nodeType, + ToscaServiceModel toscaServiceModel) { + Map<String, NodeTemplate> nodeTemplates = new HashMap<>(); + + if (Objects.nonNull(serviceTemplate.getTopology_template()) && MapUtils.isNotEmpty( + serviceTemplate.getTopology_template().getNode_templates())) { + for (Map.Entry<String, NodeTemplate> nodeTemplateEntry : serviceTemplate.getTopology_template() + .getNode_templates().entrySet()) { + if (isTypeOf(nodeTemplateEntry.getValue(), nodeType, serviceTemplate, toscaServiceModel)) { + nodeTemplates.put(nodeTemplateEntry.getKey(), nodeTemplateEntry.getValue()); + } + + } + } + return nodeTemplates; + } + + @Override + public Optional<NodeType> fetchNodeType(String nodeTypeKey, Collection<ServiceTemplate> serviceTemplates) { + Optional<Map<String, NodeType>> nodeTypeMap = serviceTemplates.stream().map(ServiceTemplate::getNode_types) + .filter(nodeTypes -> Objects.nonNull(nodeTypes) + && nodeTypes + .containsKey( + nodeTypeKey)) + .findFirst(); + return nodeTypeMap.map(stringNodeTypeMap -> stringNodeTypeMap.get(nodeTypeKey)); + } + + @Override + public boolean isTypeOf(NodeTemplate nodeTemplate, String nodeType, ServiceTemplate serviceTemplate, + ToscaServiceModel toscaServiceModel) { + return isTypeOf(nodeTemplate, nodeType, GET_NODE_TYPE_METHOD_NAME, serviceTemplate, toscaServiceModel); + } + + @Override + public List<RequirementAssignment> getRequirements(NodeTemplate nodeTemplate, String requirementId) { + List<RequirementAssignment> requirements = new ArrayList<>(); + List<Map<String, RequirementAssignment>> requirementList = nodeTemplate.getRequirements(); + if (requirementList != null) { + requirementList.stream().filter(reqMap -> reqMap.get(requirementId) != null).forEach(reqMap -> { + ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil(); + RequirementAssignment reqAssignment = toscaExtensionYamlUtil.yamlToObject( + toscaExtensionYamlUtil.objectToYaml(reqMap.get(requirementId)), RequirementAssignment.class); + requirements.add(reqAssignment); + }); + } + return requirements; + } + + @Override + public Optional<NodeTemplate> getNodeTemplateById(ServiceTemplate serviceTemplate, String nodeTemplateId) { + if ((serviceTemplate.getTopology_template() != null) && (serviceTemplate.getTopology_template() + .getNode_templates() != null) + && (serviceTemplate.getTopology_template().getNode_templates().get(nodeTemplateId) != null)) { + return Optional.of(serviceTemplate.getTopology_template().getNode_templates().get(nodeTemplateId)); + } + return Optional.empty(); + } + + @Override + public Optional<String> getSubstituteServiceTemplateName(String substituteNodeTemplateId, + NodeTemplate substitutableNodeTemplate) { + if (!isSubstitutableNodeTemplate(substitutableNodeTemplate)) { + return Optional.empty(); + } + + if (substitutableNodeTemplate.getProperties() != null && + substitutableNodeTemplate.getProperties().get(ToscaConstants.SERVICE_TEMPLATE_FILTER_PROPERTY_NAME) + != null) { + Object serviceTemplateFilter = + substitutableNodeTemplate.getProperties().get(ToscaConstants.SERVICE_TEMPLATE_FILTER_PROPERTY_NAME); + if (serviceTemplateFilter != null && serviceTemplateFilter instanceof Map) { + Object substituteServiceTemplate = + ((Map) serviceTemplateFilter).get(ToscaConstants.SUBSTITUTE_SERVICE_TEMPLATE_PROPERTY_NAME); + handleNoSubstituteServiceTemplate(substituteNodeTemplateId, substituteServiceTemplate); + return Optional.of(substituteServiceTemplate.toString()); + } + } + throw new CoreException(new ToscaInvalidSubstituteNodeTemplatePropertiesErrorBuilder(substituteNodeTemplateId) + .build()); + } + + private void handleNoSubstituteServiceTemplate(String substituteNodeTemplateId, Object substituteServiceTemplate) { + if (substituteServiceTemplate == null) { + throw new CoreException(new ToscaInvalidSubstituteNodeTemplatePropertiesErrorBuilder(substituteNodeTemplateId) + .build()); + } + } + + @Override + public Map<String, NodeTemplate> getSubstitutableNodeTemplates(ServiceTemplate serviceTemplate) { + Map<String, NodeTemplate> substitutableNodeTemplates = new HashMap<>(); + + if (serviceTemplate == null || serviceTemplate.getTopology_template() == null + || serviceTemplate.getTopology_template().getNode_templates() == null) { + return substitutableNodeTemplates; + } + + Map<String, NodeTemplate> nodeTemplates = serviceTemplate.getTopology_template().getNode_templates(); + for (Map.Entry<String, NodeTemplate> entry : nodeTemplates.entrySet()) { + String nodeTemplateId = entry.getKey(); + NodeTemplate nodeTemplate = entry.getValue(); + if (isSubstitutableNodeTemplate(nodeTemplate)) { + substitutableNodeTemplates.put(nodeTemplateId, nodeTemplate); + } + } + + return substitutableNodeTemplates; + } + + @Override + public Optional<Map.Entry<String, NodeTemplate>> getSubstitutionMappedNodeTemplateByExposedReq(String substituteServiceTemplateFileName, + ServiceTemplate substituteServiceTemplate, + String requirementId) { + if (isSubstitutionServiceTemplate(substituteServiceTemplateFileName, substituteServiceTemplate)) { + Map<String, List<String>> substitutionMappingRequirements = + substituteServiceTemplate.getTopology_template().getSubstitution_mappings().getRequirements(); + if (substitutionMappingRequirements != null) { + List<String> requirementMapping = substitutionMappingRequirements.get(requirementId); + if (requirementMapping != null && !requirementMapping.isEmpty()) { + String mappedNodeTemplateId = requirementMapping.get(0); + Optional<NodeTemplate> mappedNodeTemplate = + getNodeTemplateById(substituteServiceTemplate, mappedNodeTemplateId); + mappedNodeTemplate.orElseThrow( + () -> new CoreException(new ToscaInvalidEntryNotFoundErrorBuilder("Node Template", + mappedNodeTemplateId) + .build())); + Map.Entry<String, NodeTemplate> mappedNodeTemplateEntry = new Map.Entry<String, NodeTemplate>() { + @Override + public String getKey() { + return mappedNodeTemplateId; + } + + @Override + public NodeTemplate getValue() { + return mappedNodeTemplate.get(); + } + + @Override + public NodeTemplate setValue(NodeTemplate value) { + return null; + } + }; + return Optional.of(mappedNodeTemplateEntry); + } + } + } + return Optional.empty(); + } + + /* + match only for the input which is not null */ - @Override - public Map<String, NodeTemplate> getNodeTemplatesByType(ServiceTemplate serviceTemplate, - String nodeType, - ToscaServiceModel toscaServiceModel) { - Map<String, NodeTemplate> nodeTemplates = new HashMap<>(); - - if (Objects.nonNull(serviceTemplate.getTopology_template()) - && MapUtils.isNotEmpty(serviceTemplate.getTopology_template().getNode_templates())) { - for (Map.Entry<String, NodeTemplate> nodeTemplateEntry : serviceTemplate - .getTopology_template().getNode_templates().entrySet()) { - if (isTypeOf(nodeTemplateEntry.getValue(), nodeType, serviceTemplate, toscaServiceModel)) { - nodeTemplates.put(nodeTemplateEntry.getKey(), nodeTemplateEntry.getValue()); - } - - } - } - return nodeTemplates; - } - - @Override - public Optional<NodeType> fetchNodeType(String nodeTypeKey, Collection<ServiceTemplate> - serviceTemplates) { - Optional<Map<String, NodeType>> nodeTypeMap = serviceTemplates.stream() - .map(ServiceTemplate::getNode_types) - .filter(nodeTypes -> Objects.nonNull(nodeTypes) && nodeTypes.containsKey(nodeTypeKey)) - .findFirst(); - return nodeTypeMap.map(stringNodeTypeMap -> stringNodeTypeMap.get(nodeTypeKey)); - } - - @Override - public boolean isTypeOf(NodeTemplate nodeTemplate, String nodeType, - ServiceTemplate serviceTemplate, ToscaServiceModel toscaServiceModel) { - return isTypeOf(nodeTemplate, nodeType, GET_NODE_TYPE_METHOD_NAME, serviceTemplate, - toscaServiceModel); - } - - @Override - public List<RequirementAssignment> getRequirements(NodeTemplate nodeTemplate, - String requirementId) { - List<RequirementAssignment> requirements = new ArrayList<>(); - List<Map<String, RequirementAssignment>> requirementList = nodeTemplate.getRequirements(); - if (requirementList != null) { - requirementList.stream().filter(reqMap -> reqMap.get(requirementId) != null) - .forEach(reqMap -> { + @Override + public boolean isDesiredRequirementAssignment(RequirementAssignment requirementAssignment, String capability, + String node, String relationship) { + if (isSameCapability(requirementAssignment, capability)) { + return false; + } + + if (isSameRequirement(requirementAssignment, node)) { + return false; + } + + if (isSameRelationship(requirementAssignment, relationship)) { + return false; + } + + return !(capability == null && node == null && relationship == null); + + } + + private boolean isSameRelationship(RequirementAssignment requirementAssignment, String relationship) { + return relationship != null && (requirementAssignment.getRelationship() == null || !requirementAssignment + .getRelationship() + .equals(relationship)); + } + + private boolean isSameRequirement(RequirementAssignment requirementAssignment, String node) { + return node != null && (requirementAssignment.getNode() == null || !requirementAssignment.getNode() + .equals(node)); + } + + private boolean isSameCapability(RequirementAssignment requirementAssignment, String capability) { + return capability != null && (requirementAssignment.getCapability() == null || !requirementAssignment + .getCapability() + .equals(capability)); + } + + @Override + public ToscaFlatData getFlatEntity(ToscaElementTypes elementType, String typeId, ServiceTemplate serviceTemplate, + ToscaServiceModel toscaModel) { + ToscaFlatData flatData = new ToscaFlatData(); + flatData.setElementType(elementType); + + switch (elementType) { + case CAPABILITY_TYPE: + flatData.setFlatEntity(new CapabilityType()); + break; + case NODE_TYPE: + flatData.setFlatEntity(new NodeType()); + break; + case DATA_TYPE: + flatData.setFlatEntity(new DataType()); + break; + default: + throw new RuntimeException("Entity[" + elementType + "] id[" + typeId + "] flat not supported"); + } + + boolean isEntityFound = + scanAnFlatEntity(elementType, typeId, flatData, serviceTemplate, toscaModel, new ArrayList<>(), 0); + if (!isEntityFound) { + throw new CoreException(new ToscaElementTypeNotFoundErrorBuilder(typeId).build()); + } + + return flatData; + } + + @Override + public boolean isSubstitutableNodeTemplate(NodeTemplate nodeTemplate) { + return nodeTemplate.getDirectives() != null && nodeTemplate.getDirectives().contains( + ToscaConstants.NODE_TEMPLATE_DIRECTIVE_SUBSTITUTABLE); + } + + private <T> Optional<Boolean> isTypeExistInServiceTemplateHierarchy(String typeToMatch, String typeToSearch, + String getTypesMethodName, + ServiceTemplate serviceTemplate, + ToscaServiceModel toscaServiceModel, + Set<String> analyzedImportFiles) + throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { + Map<String, T> searchableTypes = + (Map<String, T>) serviceTemplate.getClass().getMethod(getTypesMethodName).invoke(serviceTemplate); + + if (!MapUtils.isEmpty(searchableTypes)) { + T typeObject = searchableTypes.get(typeToSearch); + if (Objects.nonNull(typeObject)) { + String derivedFromTypeVal = + (String) typeObject.getClass().getMethod(GET_DERIVED_FROM_METHOD_NAME).invoke(typeObject); + if (Objects.equals(derivedFromTypeVal, typeToMatch)) { + return Optional.of(true); + } else if (Objects.isNull(derivedFromTypeVal) || isTypeIsToscaRoot(derivedFromTypeVal)) { + return Optional.of(false); + } else { + return isTypeExistInServiceTemplateHierarchy(typeToMatch, derivedFromTypeVal, getTypesMethodName, + serviceTemplate, toscaServiceModel, null); + } + } else { + return isTypeExistInImports(typeToMatch, typeToSearch, getTypesMethodName, serviceTemplate, + toscaServiceModel, analyzedImportFiles); + } + } + return isTypeExistInImports(typeToMatch, typeToSearch, getTypesMethodName, serviceTemplate, toscaServiceModel, + analyzedImportFiles); + } + + private Optional<Boolean> isTypeExistInImports(String typeToMatch, String typeToSearch, String getTypesMethodName, + ServiceTemplate serviceTemplate, + ToscaServiceModel toscaServiceModel, Set<String> filesScanned) + throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { + List<Map<String, Import>> imports = serviceTemplate.getImports(); + if (CollectionUtils.isEmpty(imports)) { + return Optional.empty(); + } + + Set<String> createdFilesScanned = createFilesScannedSet(filesScanned); + + for (Map<String, Import> map : imports) { ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil(); - RequirementAssignment reqAssignment = toscaExtensionYamlUtil - .yamlToObject(toscaExtensionYamlUtil.objectToYaml(reqMap.get(requirementId)), - RequirementAssignment.class); - requirements.add(reqAssignment); - }); - } - return requirements; - } - - @Override - public Optional<NodeTemplate> getNodeTemplateById(ServiceTemplate serviceTemplate, - String nodeTemplateId) { - if ((serviceTemplate.getTopology_template() != null) - && (serviceTemplate.getTopology_template().getNode_templates() != null) - && (serviceTemplate.getTopology_template().getNode_templates().get(nodeTemplateId) - != null)) { - return Optional - .of(serviceTemplate.getTopology_template().getNode_templates().get(nodeTemplateId)); - } - return Optional.empty(); - } - - @Override - public Optional<String> getSubstituteServiceTemplateName(String substituteNodeTemplateId, - NodeTemplate substitutableNodeTemplate) { - if (!isSubstitutableNodeTemplate(substitutableNodeTemplate)) { - return Optional.empty(); - } - - if (substitutableNodeTemplate.getProperties() != null - && substitutableNodeTemplate.getProperties() - .get(ToscaConstants.SERVICE_TEMPLATE_FILTER_PROPERTY_NAME) != null) { - Object serviceTemplateFilter = substitutableNodeTemplate.getProperties() - .get(ToscaConstants.SERVICE_TEMPLATE_FILTER_PROPERTY_NAME); - if (serviceTemplateFilter != null && serviceTemplateFilter instanceof Map) { - Object substituteServiceTemplate = ((Map) serviceTemplateFilter) - .get(ToscaConstants.SUBSTITUTE_SERVICE_TEMPLATE_PROPERTY_NAME); - handleNoSubstituteServiceTemplate(substituteNodeTemplateId, substituteServiceTemplate); - return Optional.of(substituteServiceTemplate.toString()); - } - } - throw new CoreException( - new ToscaInvalidSubstituteNodeTemplatePropertiesErrorBuilder(substituteNodeTemplateId) - .build()); - } - - private void handleNoSubstituteServiceTemplate(String substituteNodeTemplateId, - Object substituteServiceTemplate) { - if (substituteServiceTemplate == null) { - throw new CoreException( - new ToscaInvalidSubstituteNodeTemplatePropertiesErrorBuilder(substituteNodeTemplateId) - .build()); - } - } - - @Override - public Map<String, NodeTemplate> getSubstitutableNodeTemplates(ServiceTemplate serviceTemplate) { - Map<String, NodeTemplate> substitutableNodeTemplates = new HashMap<>(); - - if (serviceTemplate == null - || serviceTemplate.getTopology_template() == null - || serviceTemplate.getTopology_template().getNode_templates() == null) { - return substitutableNodeTemplates; - } - - Map<String, NodeTemplate> nodeTemplates = - serviceTemplate.getTopology_template().getNode_templates(); - for (Map.Entry<String, NodeTemplate> entry : nodeTemplates.entrySet()) { - String nodeTemplateId = entry.getKey(); - NodeTemplate nodeTemplate = entry.getValue(); - if (isSubstitutableNodeTemplate(nodeTemplate)) { - substitutableNodeTemplates.put(nodeTemplateId, nodeTemplate); - } - } - - return substitutableNodeTemplates; - } - - @Override - public Optional<Map.Entry<String, NodeTemplate>> getSubstitutionMappedNodeTemplateByExposedReq( - String substituteServiceTemplateFileName, ServiceTemplate substituteServiceTemplate, - String requirementId) { - if (isSubstitutionServiceTemplate(substituteServiceTemplateFileName, - substituteServiceTemplate)) { - Map<String, List<String>> substitutionMappingRequirements = - substituteServiceTemplate.getTopology_template().getSubstitution_mappings() - .getRequirements(); - if (substitutionMappingRequirements != null) { - List<String> requirementMapping = substitutionMappingRequirements.get(requirementId); - if (requirementMapping != null && !requirementMapping.isEmpty()) { - String mappedNodeTemplateId = requirementMapping.get(0); - Optional<NodeTemplate> mappedNodeTemplate = - getNodeTemplateById(substituteServiceTemplate, mappedNodeTemplateId); - mappedNodeTemplate.orElseThrow(() -> new CoreException( - new ToscaInvalidEntryNotFoundErrorBuilder("Node Template", mappedNodeTemplateId) - .build())); - Map.Entry<String, NodeTemplate> mappedNodeTemplateEntry = - new Map.Entry<String, NodeTemplate>() { - @Override - public String getKey() { - return mappedNodeTemplateId; + Import anImport = toscaExtensionYamlUtil + .yamlToObject(toscaExtensionYamlUtil.objectToYaml(map.values().iterator().next()), + Import.class); + handleImportWithNoFileEntry(anImport); + String importFile = anImport.getFile(); + ServiceTemplate template = toscaServiceModel.getServiceTemplates().get(fetchFileNameForImport(importFile, + serviceTemplate.getMetadata() == null ? null : serviceTemplate.getMetadata().get("filename"))); + if (Objects.isNull(template) || createdFilesScanned + .contains(ToscaUtil.getServiceTemplateFileName(template))) { + continue; + } else { + createdFilesScanned.add(ToscaUtil.getServiceTemplateFileName(template)); + } + Optional<Boolean> typeExistInServiceTemplateHierarchy = + isTypeExistInServiceTemplateHierarchy(typeToMatch, typeToSearch, getTypesMethodName, template, + toscaServiceModel, createdFilesScanned); + if (typeExistInServiceTemplateHierarchy.isPresent() && (typeExistInServiceTemplateHierarchy.get())) { + createdFilesScanned.clear(); + return Optional.of(true); + } + + } + return Optional.of(false); + } + + private void handleImportWithNoFileEntry(Import anImport) { + if (Objects.isNull(anImport) || Objects.isNull(anImport.getFile())) { + throw new RuntimeException("import without file entry"); + } + } + + private Set<String> createFilesScannedSet(Set<String> filesScanned) { + Set<String> retFileScanned = filesScanned; + if (Objects.isNull(retFileScanned)) { + retFileScanned = new HashSet<>(); + } + return retFileScanned; + } + + private boolean isTypeIsToscaRoot(String type) { + return (type.contains(TOSCA_DOT) && type.contains(DOT_ROOT)); + } + + private boolean isSubstitutionServiceTemplate(String substituteServiceTemplateFileName, + ServiceTemplate substituteServiceTemplate) { + if (substituteServiceTemplate != null && substituteServiceTemplate.getTopology_template() != null + && substituteServiceTemplate.getTopology_template().getSubstitution_mappings() != null) { + if (substituteServiceTemplate.getTopology_template().getSubstitution_mappings().getNode_type() == null) { + throw new CoreException(new ToscaInvalidSubstitutionServiceTemplateErrorBuilder(substituteServiceTemplateFileName) + .build()); + } + return true; + } + return false; + + } + + private boolean scanAnFlatEntity(ToscaElementTypes elementType, String typeId, ToscaFlatData flatData, + ServiceTemplate serviceTemplate, ToscaServiceModel toscaModel, + List<String> filesScanned, int rootScanStartInx) { + + + boolean entityFound = + enrichEntityFromCurrentServiceTemplate(elementType, typeId, flatData, serviceTemplate, toscaModel, + filesScanned, rootScanStartInx); + if (!entityFound) { + List<Map<String, Import>> imports = serviceTemplate.getImports(); + if (CollectionUtils.isEmpty(imports)) { + return false; + } + boolean found = false; + for (Map<String, Import> importMap : imports) { + if (found) { + return true; } + found = isFlatEntity(importMap, flatData, serviceTemplate, filesScanned, toscaModel, elementType, + typeId); + } + return found; + } + return true; + } - @Override - public NodeTemplate getValue() { - return mappedNodeTemplate.get(); + private boolean isFlatEntity(Map<String, Import> importMap, ToscaFlatData flatData, ServiceTemplate serviceTemplate, + List<String> filesScanned, ToscaServiceModel toscaModel, + ToscaElementTypes elementType, String typeId) { + boolean found = false; + ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil(); + for (Object importObject : importMap.values()) { + Import importServiceTemplate = toscaExtensionYamlUtil + .yamlToObject(toscaExtensionYamlUtil.objectToYaml(importObject), + Import.class); + String fileName = fetchFileNameForImport(importServiceTemplate.getFile(), + serviceTemplate.getMetadata() == null ? null : serviceTemplate.getMetadata().get("filename")); + if (filesScanned.contains(fileName)) { + return false; + } else { + filesScanned.add(fileName); + } + ServiceTemplate template = toscaModel.getServiceTemplates().get(fileName); + if (Objects.isNull(template)) { + throw new CoreException(new ToscaFileNotFoundErrorBuilder(fileName).build()); + } + found = scanAnFlatEntity(elementType, typeId, flatData, template, toscaModel, filesScanned, + filesScanned.size()); + } + return found; + } + + private String fetchFileNameForImport(String importServiceTemplateFile, String currentMetadatafileName) { + if (importServiceTemplateFile.contains("../")) { + return importServiceTemplateFile.replace("../", ""); + } else if (currentMetadatafileName != null && currentMetadatafileName.indexOf('/') != -1) { + return currentMetadatafileName.substring(0, currentMetadatafileName.indexOf('/')) + "/" + + importServiceTemplateFile; + } else { + return importServiceTemplateFile; + } + + } + + private boolean enrichEntityFromCurrentServiceTemplate(ToscaElementTypes elementType, String typeId, + ToscaFlatData flatData, + ServiceTemplate serviceTemplate, + ToscaServiceModel toscaModel, + List<String> filesScanned, int rootScanStartInx) { + switch (elementType) { + case CAPABILITY_TYPE: + if (enrichCapabilityType(elementType, typeId, flatData, serviceTemplate, toscaModel, filesScanned, + rootScanStartInx)) { + return false; + } + break; + case NODE_TYPE: + if (enrichNodeTypeInfo(elementType, typeId, flatData, serviceTemplate, toscaModel, filesScanned, + rootScanStartInx)) { + return false; + } + break; + case DATA_TYPE: + if (enrichDataTypeInfo(elementType, typeId, flatData, serviceTemplate, toscaModel, filesScanned, + rootScanStartInx)) { + return false; } + break; + default: + throw new RuntimeException("Entity[" + elementType + "] id[" + typeId + "] flat not supported"); + } + + return true; + + + } + + private boolean enrichNodeTypeInfo(ToscaElementTypes elementType, String typeId, ToscaFlatData flatData, + ServiceTemplate serviceTemplate, ToscaServiceModel toscaModel, + List<String> filesScanned, int rootScanStartInx) { + String derivedFrom; + if (serviceTemplate.getNode_types() != null && serviceTemplate.getNode_types().containsKey(typeId)) { - @Override - public NodeTemplate setValue(NodeTemplate value) { - return null; + filesScanned.clear(); + flatData.addInheritanceHierarchyType(typeId); + NodeType targetNodeType = (NodeType) flatData.getFlatEntity(); + NodeType sourceNodeType = serviceTemplate.getNode_types().get(typeId); + derivedFrom = sourceNodeType.getDerived_from(); + if (derivedFrom != null) { + boolean isEntityFound = + scanAnFlatEntity(elementType, derivedFrom, flatData, serviceTemplate, toscaModel, filesScanned, + rootScanStartInx); + if (!isEntityFound) { + throw new CoreException(new ToscaElementTypeNotFoundErrorBuilder(typeId).build()); } - }; - return Optional.of(mappedNodeTemplateEntry); + } + combineNodeTypeInfo(sourceNodeType, targetNodeType); + } else { + return true; } - } + return false; } - return Optional.empty(); - } - /* - match only for the input which is not null - */ - @Override - public boolean isDesiredRequirementAssignment(RequirementAssignment requirementAssignment, - String capability, String node, - String relationship) { - if (isSameCapability(requirementAssignment, capability)) { - return false; - } - - if (isSameRequirement(requirementAssignment, node)) { - return false; - } - - if (isSameRelationship(requirementAssignment, relationship)) { - return false; - } - - return !(capability == null && node == null && relationship == null); - - } - - private boolean isSameRelationship(RequirementAssignment requirementAssignment, - String relationship) { - return relationship != null && (requirementAssignment.getRelationship() == null - || !requirementAssignment.getRelationship().equals(relationship)); - } - - private boolean isSameRequirement(RequirementAssignment requirementAssignment, String node) { - return node != null && (requirementAssignment.getNode() == null - || !requirementAssignment.getNode().equals(node)); - } - - private boolean isSameCapability(RequirementAssignment requirementAssignment, String capability) { - return capability != null && (requirementAssignment.getCapability() == null - || !requirementAssignment.getCapability().equals(capability)); - } - - @Override - public Object getFlatEntity(ToscaElementTypes elementType, String typeId, - ServiceTemplate serviceTemplate, ToscaServiceModel toscaModel) { - Object returnEntity; - - switch (elementType) { - case CAPABILITY_TYPE: - returnEntity = new CapabilityType(); - break; - case NODE_TYPE: - returnEntity = new NodeType(); - break; - case DATA_TYPE: - returnEntity = new DataType(); - break; - default: - throw new RuntimeException( - "Entity[" + elementType + "] id[" + typeId + "] flat not supported"); - } - - boolean isEntityFound = - scanAnFlatEntity(elementType, typeId, returnEntity, serviceTemplate, toscaModel, - new ArrayList<>(), 0); - if (!isEntityFound) { - throw new CoreException(new ToscaElementTypeNotFoundErrorBuilder(typeId).build()); - } - - return returnEntity; - } - - @Override - public boolean isSubstitutableNodeTemplate(NodeTemplate nodeTemplate) { - return nodeTemplate.getDirectives() != null - && nodeTemplate.getDirectives().contains(ToscaConstants - .NODE_TEMPLATE_DIRECTIVE_SUBSTITUTABLE); - } - - private <T> Optional<Boolean> isTypeExistInServiceTemplateHierarchy(String typeToMatch, - String typeToSearch, - String getTypesMethodName, - ServiceTemplate serviceTemplate, - ToscaServiceModel toscaServiceModel, - Set<String> analyzedImportFiles) - throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { - Map<String, T> searchableTypes = - (Map<String, T>) serviceTemplate.getClass().getMethod(getTypesMethodName) - .invoke(serviceTemplate); - - if (!MapUtils.isEmpty(searchableTypes)) { - T typeObject = searchableTypes.get(typeToSearch); - if (Objects.nonNull(typeObject)) { - String derivedFromTypeVal = - (String) typeObject.getClass().getMethod(GET_DERIVED_FROM_METHOD_NAME).invoke(typeObject); - if (Objects.equals(derivedFromTypeVal, typeToMatch)) { - return Optional.of(true); - } else if (Objects.isNull(derivedFromTypeVal) || isTypeIsToscaRoot(derivedFromTypeVal)) { - return Optional.of(false); + private boolean enrichDataTypeInfo(ToscaElementTypes elementType, String typeId, ToscaFlatData flatData, + ServiceTemplate serviceTemplate, ToscaServiceModel toscaModel, + List<String> filesScanned, int rootScanStartInx) { + String derivedFrom; + if (serviceTemplate.getData_types() != null && serviceTemplate.getData_types().containsKey(typeId)) { + + filesScanned.clear(); + flatData.addInheritanceHierarchyType(typeId); + DataType targetDataType = (DataType) flatData.getFlatEntity(); + DataType sourceDataType = serviceTemplate.getData_types().get(typeId); + derivedFrom = sourceDataType.getDerived_from(); + if (derivedFrom != null) { + boolean isEntityFound = + scanAnFlatEntity(elementType, derivedFrom, flatData, serviceTemplate, toscaModel, filesScanned, + rootScanStartInx); + if (!isEntityFound) { + throw new CoreException(new ToscaElementTypeNotFoundErrorBuilder(typeId).build()); + } + } + combineDataTypeInfo(sourceDataType, targetDataType); } else { - return isTypeExistInServiceTemplateHierarchy(typeToMatch, - derivedFromTypeVal, getTypesMethodName, serviceTemplate, toscaServiceModel, null); - } - } else { - return isTypeExistInImports(typeToMatch, typeToSearch, getTypesMethodName, - serviceTemplate, toscaServiceModel, analyzedImportFiles); - } - } - return isTypeExistInImports(typeToMatch, typeToSearch, getTypesMethodName, serviceTemplate, - toscaServiceModel, analyzedImportFiles); - } - - private Optional<Boolean> isTypeExistInImports(String typeToMatch, - String typeToSearch, - String getTypesMethodName, - ServiceTemplate serviceTemplate, - ToscaServiceModel toscaServiceModel, - Set<String> filesScanned) - throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { - List<Map<String, Import>> imports = serviceTemplate.getImports(); - if (CollectionUtils.isEmpty(imports)) { - return Optional.empty(); - } - - Set<String> createdFilesScanned = createFilesScannedSet(filesScanned); - - for (Map<String, Import> map : imports) { - ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil(); - Import anImport = toscaExtensionYamlUtil - .yamlToObject(toscaExtensionYamlUtil.objectToYaml(map.values().iterator().next()), - Import.class); - handleImportWithNoFileEntry(anImport); - String importFile = anImport.getFile(); - ServiceTemplate template = - toscaServiceModel.getServiceTemplates().get(fetchFileNameForImport(importFile, - serviceTemplate.getMetadata() == null ? null - : serviceTemplate.getMetadata().get("filename"))); - if (Objects.isNull(template) - || createdFilesScanned.contains(ToscaUtil.getServiceTemplateFileName(template))) { - continue; - } else { - createdFilesScanned.add(ToscaUtil.getServiceTemplateFileName(template)); - } - Optional<Boolean> typeExistInServiceTemplateHierarchy = - isTypeExistInServiceTemplateHierarchy(typeToMatch, typeToSearch, getTypesMethodName, - template, toscaServiceModel, createdFilesScanned); - if (typeExistInServiceTemplateHierarchy.isPresent() - && (typeExistInServiceTemplateHierarchy.get())) { - createdFilesScanned.clear(); - return Optional.of(true); - } - - } - return Optional.of(false); - } - - private void handleImportWithNoFileEntry(Import anImport) { - if (Objects.isNull(anImport) || Objects.isNull(anImport.getFile())) { - throw new RuntimeException("import without file entry"); - } - } - - private Set<String> createFilesScannedSet(Set<String> filesScanned) { - Set<String> retFileScanned = filesScanned; - if (Objects.isNull(retFileScanned)) { - retFileScanned = new HashSet<>(); - } - return retFileScanned; - } - - private boolean isTypeIsToscaRoot(String type) { - return (type.contains(TOSCA_DOT) && type.contains(DOT_ROOT)); - } - - private boolean isSubstitutionServiceTemplate(String substituteServiceTemplateFileName, - ServiceTemplate substituteServiceTemplate) { - if (substituteServiceTemplate != null - && substituteServiceTemplate.getTopology_template() != null - && substituteServiceTemplate.getTopology_template().getSubstitution_mappings() != null) { - if (substituteServiceTemplate.getTopology_template().getSubstitution_mappings() - .getNode_type() == null) { - throw new CoreException(new ToscaInvalidSubstitutionServiceTemplateErrorBuilder( - substituteServiceTemplateFileName).build()); - } - return true; - } - return false; - - } - - private boolean scanAnFlatEntity(ToscaElementTypes elementType, String typeId, Object entity, - ServiceTemplate serviceTemplate, ToscaServiceModel toscaModel, - List<String> filesScanned, int rootScanStartInx) { - - - boolean entityFound = enrichEntityFromCurrentServiceTemplate(elementType, typeId, entity, - serviceTemplate, toscaModel, filesScanned, rootScanStartInx); - if (!entityFound) { - List<Map<String, Import>> imports = serviceTemplate.getImports(); - if (CollectionUtils.isEmpty(imports)) { + return true; + } return false; - } - boolean found = false; - for (Map<String, Import> importMap : imports) { - if (found) { - return true; - } - found = isFlatEntity(importMap, entity, serviceTemplate, filesScanned, - toscaModel, elementType, typeId); - } - return found; - } - return true; - } - - private boolean isFlatEntity(Map<String, Import> importMap, - Object entity, - ServiceTemplate serviceTemplate, - List<String> filesScanned, - ToscaServiceModel toscaModel, - ToscaElementTypes elementType, String typeId) { - boolean found = false; - ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil(); - for (Object importObject : importMap.values()) { - Import importServiceTemplate = toscaExtensionYamlUtil - .yamlToObject(toscaExtensionYamlUtil.objectToYaml(importObject), Import.class); - String fileName = fetchFileNameForImport(importServiceTemplate.getFile(), - serviceTemplate.getMetadata() == null ? null : serviceTemplate.getMetadata().get( - "filename")); - if (filesScanned.contains(fileName)) { + } + + private boolean enrichCapabilityType(ToscaElementTypes elementType, String typeId, ToscaFlatData flatData, + ServiceTemplate serviceTemplate, ToscaServiceModel toscaModel, + List<String> filesScanned, int rootScanStartInx) { + String derivedFrom; + if (serviceTemplate.getCapability_types() != null && serviceTemplate.getCapability_types() + .containsKey(typeId)) { + + filesScanned.clear(); + flatData.addInheritanceHierarchyType(typeId); + CapabilityType targetCapabilityType = (CapabilityType) flatData.getFlatEntity(); + CapabilityType sourceCapabilityType = serviceTemplate.getCapability_types().get(typeId); + derivedFrom = sourceCapabilityType.getDerived_from(); + if (derivedFrom != null) { + boolean isEntityFound = + scanAnFlatEntity(elementType, derivedFrom, flatData, serviceTemplate, toscaModel, filesScanned, + rootScanStartInx); + if (!isEntityFound) { + throw new CoreException(new ToscaElementTypeNotFoundErrorBuilder(typeId).build()); + } + } + combineCapabilityTypeInfo(sourceCapabilityType, targetCapabilityType); + } else { + return true; + } return false; - } else { - filesScanned.add(fileName); - } - ServiceTemplate template = toscaModel.getServiceTemplates().get(fileName); - if (Objects.isNull(template)) { - throw new CoreException( - new ToscaFileNotFoundErrorBuilder(fileName).build()); - } - found = scanAnFlatEntity(elementType, typeId, entity, template, toscaModel, - filesScanned, filesScanned.size()); - } - return found; - } - - private String fetchFileNameForImport(String importServiceTemplateFile, - String currentMetadatafileName) { - if (importServiceTemplateFile.contains("../")) { - return importServiceTemplateFile.replace("../", ""); - } else if (currentMetadatafileName != null) { - return currentMetadatafileName.substring(0, currentMetadatafileName.indexOf('/')) + "/" - + importServiceTemplateFile; - } else { - return importServiceTemplateFile; - } - - } - - private boolean enrichEntityFromCurrentServiceTemplate(ToscaElementTypes elementType, - String typeId, Object entity, - ServiceTemplate serviceTemplate, - ToscaServiceModel toscaModel, - List<String> filesScanned, - int rootScanStartInx) { - switch (elementType) { - case CAPABILITY_TYPE: - if (enrichCapabilityType(elementType, typeId, entity, serviceTemplate, toscaModel, - filesScanned, rootScanStartInx)) { - return false; - } - break; - case NODE_TYPE: - if (enrichNodeTypeInfo(elementType, typeId, entity, serviceTemplate, toscaModel, - filesScanned, rootScanStartInx)) { - return false; - } - break; - case DATA_TYPE: - if (enrichDataTypeInfo(elementType, typeId, entity, serviceTemplate, toscaModel, - filesScanned, rootScanStartInx)) { - return false; - } - break; - default: - throw new RuntimeException( - "Entity[" + elementType + "] id[" + typeId + "] flat not supported"); - } - - return true; - - - } - - private boolean enrichNodeTypeInfo(ToscaElementTypes elementType, String typeId, Object entity, - ServiceTemplate serviceTemplate, ToscaServiceModel toscaModel, - List<String> filesScanned, int rootScanStartInx) { - String derivedFrom; - if (serviceTemplate.getNode_types() != null - && serviceTemplate.getNode_types().containsKey(typeId)) { - - filesScanned.clear(); - NodeType targetNodeType = (NodeType) entity; - NodeType sourceNodeType = serviceTemplate.getNode_types().get(typeId); - derivedFrom = sourceNodeType.getDerived_from(); - if (derivedFrom != null) { - scanAnFlatEntity(elementType, derivedFrom, entity, serviceTemplate, toscaModel, - filesScanned, rootScanStartInx); - } - combineNodeTypeInfo(sourceNodeType, targetNodeType); - } else { - return true; - } - return false; - } - - private boolean enrichDataTypeInfo(ToscaElementTypes elementType, String typeId, Object entity, - ServiceTemplate serviceTemplate, ToscaServiceModel toscaModel, - List<String> filesScanned, int rootScanStartInx) { - String derivedFrom; - if (serviceTemplate.getData_types() != null - && serviceTemplate.getData_types().containsKey(typeId)) { - - filesScanned.clear(); - DataType targetDataType = (DataType) entity; - DataType sourceDataType = serviceTemplate.getData_types().get(typeId); - derivedFrom = sourceDataType.getDerived_from(); - if (derivedFrom != null) { - scanAnFlatEntity(elementType, derivedFrom, entity, serviceTemplate, toscaModel, - filesScanned, rootScanStartInx); - } - combineDataTypeInfo(sourceDataType, targetDataType); - } else { - return true; - } - return false; - } - - private boolean enrichCapabilityType(ToscaElementTypes elementType, String typeId, Object entity, - ServiceTemplate serviceTemplate, - ToscaServiceModel toscaModel, List<String> filesScanned, - int rootScanStartInx) { - String derivedFrom; - if (serviceTemplate.getCapability_types() != null - && serviceTemplate.getCapability_types().containsKey(typeId)) { - - filesScanned.clear(); - CapabilityType targetCapabilityType = (CapabilityType) entity; - CapabilityType sourceCapabilityType = serviceTemplate.getCapability_types().get(typeId); - derivedFrom = sourceCapabilityType.getDerived_from(); - if (derivedFrom != null) { - scanAnFlatEntity(elementType, derivedFrom, entity, serviceTemplate, toscaModel, - filesScanned, rootScanStartInx); - } - combineCapabilityTypeInfo(sourceCapabilityType, targetCapabilityType); - } else { - return true; - } - return false; - } - - private void combineNodeTypeInfo(NodeType sourceNodeType, NodeType targetNodeType) { - targetNodeType.setDerived_from(sourceNodeType.getDerived_from()); - targetNodeType.setDescription(sourceNodeType.getDescription()); - targetNodeType.setVersion(sourceNodeType.getVersion()); - targetNodeType.setProperties( - CommonMethods.mergeMaps(targetNodeType.getProperties(), sourceNodeType.getProperties())); - targetNodeType.setInterfaces( - CommonMethods.mergeMaps(targetNodeType.getInterfaces(), sourceNodeType.getInterfaces())); - targetNodeType.setArtifacts( - CommonMethods.mergeMaps(targetNodeType.getArtifacts(), sourceNodeType.getArtifacts())); - targetNodeType.setAttributes( - CommonMethods.mergeMaps(targetNodeType.getAttributes(), sourceNodeType.getAttributes())); - targetNodeType.setCapabilities(CommonMethods - .mergeMaps(targetNodeType.getCapabilities(), sourceNodeType.getCapabilities())); - targetNodeType.setRequirements(CommonMethods - .mergeListsOfMap(targetNodeType.getRequirements(), sourceNodeType.getRequirements())); - - } - - private void combineDataTypeInfo(DataType sourceDataType, DataType targetDataType) { - targetDataType.setDerived_from(sourceDataType.getDerived_from()); - targetDataType.setDescription(sourceDataType.getDescription()); - targetDataType.setVersion(sourceDataType.getVersion()); - targetDataType.setProperties( - CommonMethods.mergeMaps(targetDataType.getProperties(), sourceDataType.getProperties())); - targetDataType.setConstraints( - CommonMethods.mergeLists(targetDataType.getConstraints(), sourceDataType.getConstraints())); - } - - - private void combineCapabilityTypeInfo(CapabilityType sourceCapabilityType, - CapabilityType targetCapabilityType) { - - targetCapabilityType.setAttributes(CommonMethods - .mergeMaps(targetCapabilityType.getAttributes(), sourceCapabilityType.getAttributes())); - targetCapabilityType.setProperties(CommonMethods - .mergeMaps(targetCapabilityType.getProperties(), sourceCapabilityType.getProperties())); - targetCapabilityType.setValid_source_types(CommonMethods - .mergeLists(targetCapabilityType.getValid_source_types(), - sourceCapabilityType.getValid_source_types())); - - if (StringUtils.isNotEmpty(sourceCapabilityType.getDerived_from())) { - targetCapabilityType.setDerived_from(sourceCapabilityType.getDerived_from()); - } - if (StringUtils.isNotEmpty(sourceCapabilityType.getDescription())) { - targetCapabilityType.setDescription(sourceCapabilityType.getDescription()); - } - if (StringUtils.isNotEmpty(sourceCapabilityType.getVersion())) { - targetCapabilityType.setVersion(sourceCapabilityType.getVersion()); - } - - - } - - - /* - * Create node type according to the input substitution service template, while the substitution - * service template can be mappted to this node type, for substitution mapping. - * - * @param substitutionServiceTemplate substitution serivce template - * @param nodeTypeDerivedFromValue derived from value for the created node type - * @return the node type - */ - @Override - public NodeType createInitSubstitutionNodeType(ServiceTemplate substitutionServiceTemplate, - String nodeTypeDerivedFromValue) { - NodeType substitutionNodeType = new NodeType(); - substitutionNodeType.setDerived_from(nodeTypeDerivedFromValue); - substitutionNodeType.setDescription(substitutionServiceTemplate.getDescription()); - substitutionNodeType - .setProperties(manageSubstitutionNodeTypeProperties(substitutionServiceTemplate)); - substitutionNodeType - .setAttributes(manageSubstitutionNodeTypeAttributes(substitutionServiceTemplate)); - return substitutionNodeType; - } - - @Override - public Map<String, PropertyDefinition> manageSubstitutionNodeTypeProperties( - ServiceTemplate substitutionServiceTemplate) { - Map<String, PropertyDefinition> substitutionNodeTypeProperties = new HashMap<>(); - Map<String, ParameterDefinition> properties = - substitutionServiceTemplate.getTopology_template().getInputs(); - if (properties == null) { - return null; - } - - PropertyDefinition propertyDefinition; - String toscaPropertyName; - for (Map.Entry<String, ParameterDefinition> entry : properties.entrySet()) { - toscaPropertyName = entry.getKey(); - propertyDefinition = new PropertyDefinition(); - ParameterDefinition parameterDefinition = - substitutionServiceTemplate.getTopology_template().getInputs().get(toscaPropertyName); - propertyDefinition.setType(parameterDefinition.getType()); - propertyDefinition.setDescription(parameterDefinition.getDescription()); - propertyDefinition.set_default(parameterDefinition.get_default()); - if (parameterDefinition.getRequired() != null) { - propertyDefinition.setRequired(parameterDefinition.getRequired()); - } - if (propertyDefinition.get_default() != null) { - propertyDefinition.setRequired(false); - } - if (!CollectionUtils.isEmpty(parameterDefinition.getConstraints())) { - propertyDefinition.setConstraints(parameterDefinition.getConstraints()); - } - propertyDefinition.setEntry_schema(parameterDefinition.getEntry_schema()); - if (parameterDefinition.getStatus() != null) { - propertyDefinition.setStatus(parameterDefinition.getStatus()); - } - substitutionNodeTypeProperties.put(toscaPropertyName, propertyDefinition); - } - return substitutionNodeTypeProperties; - } - - - private Map<String, AttributeDefinition> manageSubstitutionNodeTypeAttributes( - ServiceTemplate substitutionServiceTemplate) { - Map<String, AttributeDefinition> substitutionNodeTypeAttributes = new HashMap<>(); - Map<String, ParameterDefinition> attributes = - substitutionServiceTemplate.getTopology_template().getOutputs(); - if (attributes == null) { - return null; - } - AttributeDefinition attributeDefinition; - String toscaAttributeName; - - for (Map.Entry<String, ParameterDefinition> entry : attributes.entrySet()) { - attributeDefinition = new AttributeDefinition(); - toscaAttributeName = entry.getKey(); - ParameterDefinition parameterDefinition = - substitutionServiceTemplate.getTopology_template().getOutputs().get(toscaAttributeName); - if (parameterDefinition.getType() != null && !parameterDefinition.getType().isEmpty()) { - attributeDefinition.setType(parameterDefinition.getType()); - } else { - attributeDefinition.setType(PropertyType.STRING.getDisplayName()); - } - attributeDefinition.setDescription(parameterDefinition.getDescription()); - attributeDefinition.set_default(parameterDefinition.get_default()); - attributeDefinition.setEntry_schema(parameterDefinition.getEntry_schema()); - if (Objects.nonNull(parameterDefinition.getStatus())) { - attributeDefinition.setStatus(parameterDefinition.getStatus()); - } - substitutionNodeTypeAttributes.put(toscaAttributeName, attributeDefinition); - } - return substitutionNodeTypeAttributes; - } - - /** - * Checks if the requirement exists in the node template. + } + + private void combineNodeTypeInfo(NodeType sourceNodeType, NodeType targetNodeType) { + targetNodeType.setDerived_from(sourceNodeType.getDerived_from()); + targetNodeType.setDescription(sourceNodeType.getDescription()); + targetNodeType.setVersion(sourceNodeType.getVersion()); + targetNodeType + .setProperties(CommonMethods.mergeMaps(targetNodeType.getProperties(), sourceNodeType.getProperties())); + combineNodeTypeInterfaceInfo(sourceNodeType, targetNodeType); + targetNodeType + .setArtifacts(CommonMethods.mergeMaps(targetNodeType.getArtifacts(), sourceNodeType.getArtifacts())); + targetNodeType + .setAttributes(CommonMethods.mergeMaps(targetNodeType.getAttributes(), sourceNodeType.getAttributes())); + targetNodeType.setCapabilities( + CommonMethods.mergeMaps(targetNodeType.getCapabilities(), sourceNodeType.getCapabilities())); + targetNodeType.setRequirements( + CommonMethods.mergeListsOfMap(targetNodeType.getRequirements(), sourceNodeType.getRequirements())); + + } + + private InterfaceDefinitionType getInterfaceDefinitionType(String interfaceName, Object interfaceDefTypeObj) { + Optional<InterfaceDefinitionType> interfaceDefinitionType = DataModelUtil.convertObjToInterfaceDefinition( + interfaceName, interfaceDefTypeObj, InterfaceDefinitionType.class); + if (!interfaceDefinitionType.isPresent()) { + throw new CoreException(new CreateInterfaceObjectErrorBuilder("InterfaceDefinitionType", interfaceName, + "Invalid interface object").build()); + } + return interfaceDefinitionType.get(); + } + + private void combineNodeTypeInterfaceInfo(NodeType sourceNodeType, NodeType targetNodeType) { + Optional<Map<String, Object>> interfaceNoMerge = combineInterfaceNoMerge(sourceNodeType, targetNodeType); + if (interfaceNoMerge.isPresent()) { + targetNodeType.setInterfaces(interfaceNoMerge.get()); + return; + } + targetNodeType.setInterfaces(combineInterfaces(sourceNodeType, targetNodeType)); + } + + private Map<String, Object> combineInterfaces(NodeType sourceNodeType, NodeType targetNodeType) { + Map<String, Object> combineInterfaces = new HashMap<>(); + for (Map.Entry<String, Object> sourceInterfaceDefEntry : sourceNodeType.getInterfaces().entrySet()) { + String interfaceName = sourceInterfaceDefEntry.getKey(); + if (!MapUtils.isEmpty(targetNodeType.getInterfaces()) && targetNodeType.getInterfaces() + .containsKey(interfaceName)) { + combineInterfaces.put(interfaceName, + combineInterfaceDefinition(interfaceName, sourceInterfaceDefEntry.getValue(), + targetNodeType.getInterfaces().get(interfaceName))); + } else { + combineInterfaces.put(sourceInterfaceDefEntry.getKey(), sourceInterfaceDefEntry.getValue()); + } + } + + for (Map.Entry<String, Object> targetInterfaceDefEntry : targetNodeType.getInterfaces().entrySet()) { + String interfaceName = targetInterfaceDefEntry.getKey(); + if (!sourceNodeType.getInterfaces().containsKey(interfaceName)) { + combineInterfaces.put(targetInterfaceDefEntry.getKey(), targetInterfaceDefEntry.getValue()); + } + } + + return combineInterfaces; + } + + private Optional<Map<String, Object>> combineInterfaceNoMerge(NodeType sourceNodeType, NodeType targetNodeType) { + if ((MapUtils.isEmpty(sourceNodeType.getInterfaces()) && MapUtils.isEmpty(targetNodeType.getInterfaces()))) { + return Optional.empty(); + } + + if (MapUtils.isEmpty(sourceNodeType.getInterfaces()) && !MapUtils.isEmpty(targetNodeType.getInterfaces())) { + return Optional.of(targetNodeType.getInterfaces()); + } + + if (!MapUtils.isEmpty(sourceNodeType.getInterfaces()) && MapUtils.isEmpty(targetNodeType.getInterfaces())) { + return Optional.of(sourceNodeType.getInterfaces()); + } + return Optional.empty(); + + } + + private Object combineInterfaceDefinition(String interfaceName, Object sourceInterfaceDefType, + Object targetInterfaceDefType) { + InterfaceDefinitionType sourceInterface = getInterfaceDefinitionType(interfaceName, sourceInterfaceDefType); + InterfaceDefinitionType targetInterface = getInterfaceDefinitionType(interfaceName, targetInterfaceDefType); + InterfaceDefinitionType combineInterface = new InterfaceDefinitionType(); + combineInterface.setType(sourceInterface.getType()); + combineInterface.setInputs(CommonMethods.mergeMaps(targetInterface.getInputs(), sourceInterface.getInputs())); + combineInterface.setOperations( + CommonMethods.mergeMaps(targetInterface.getOperations(), sourceInterface.getOperations())); + + return DataModelUtil.convertInterfaceDefinitionTypeToObj(combineInterface).get(); + } + + private void combineDataTypeInfo(DataType sourceDataType, DataType targetDataType) { + targetDataType.setDerived_from(sourceDataType.getDerived_from()); + targetDataType.setDescription(sourceDataType.getDescription()); + targetDataType.setVersion(sourceDataType.getVersion()); + targetDataType + .setProperties(CommonMethods.mergeMaps(targetDataType.getProperties(), sourceDataType.getProperties())); + targetDataType.setConstraints( + CommonMethods.mergeLists(targetDataType.getConstraints(), sourceDataType.getConstraints())); + } + + + private void combineCapabilityTypeInfo(CapabilityType sourceCapabilityType, CapabilityType targetCapabilityType) { + + targetCapabilityType.setAttributes( + CommonMethods.mergeMaps(targetCapabilityType.getAttributes(), sourceCapabilityType.getAttributes())); + targetCapabilityType.setProperties( + CommonMethods.mergeMaps(targetCapabilityType.getProperties(), sourceCapabilityType.getProperties())); + targetCapabilityType.setValid_source_types(CommonMethods + .mergeLists(targetCapabilityType.getValid_source_types(), + sourceCapabilityType.getValid_source_types())); + + if (StringUtils.isNotEmpty(sourceCapabilityType.getDerived_from())) { + targetCapabilityType.setDerived_from(sourceCapabilityType.getDerived_from()); + } + if (StringUtils.isNotEmpty(sourceCapabilityType.getDescription())) { + targetCapabilityType.setDescription(sourceCapabilityType.getDescription()); + } + if (StringUtils.isNotEmpty(sourceCapabilityType.getVersion())) { + targetCapabilityType.setVersion(sourceCapabilityType.getVersion()); + } + + + } + + + /* + * Create node type according to the input substitution service template, while the substitution + * service template can be mappted to this node type, for substitution mapping. * - * @param nodeTemplate the node template - * @param requirementId the requirement id - * @param requirementAssignment the requirement assignment - * @return true if the requirement already exists and false otherwise + * @param substitutionServiceTemplate substitution serivce template + * @param nodeTypeDerivedFromValue derived from value for the created node type + * @return the node type */ - @Override - public boolean isRequirementExistInNodeTemplate(NodeTemplate nodeTemplate, - String requirementId, - RequirementAssignment requirementAssignment) { - List<Map<String, RequirementAssignment>> nodeTemplateRequirements = nodeTemplate - .getRequirements(); - return nodeTemplateRequirements != null && nodeTemplateRequirements.stream().anyMatch( - requirement -> requirement.containsKey(requirementId) && DataModelUtil - .compareRequirementAssignment(requirementAssignment, requirement.get(requirementId))); - } - - @Override - public boolean isTypeOf(InterfaceDefinitionType interfaceDefinition, String interfaceType, - ServiceTemplate serviceTemplate, ToscaServiceModel toscaServiceModel) { - return isTypeOf(interfaceDefinition, interfaceType, GET_INTERFACE_TYPE_METHOD_NAME, serviceTemplate, - toscaServiceModel); - } - - @Override - public boolean isTypeOf(DefinitionOfDataType parameterDefinition, String dataType, - ServiceTemplate serviceTemplate, ToscaServiceModel toscaServiceModel) { - return isTypeOf(parameterDefinition, dataType, GET_DATA_TYPE_METHOD_NAME, serviceTemplate, - toscaServiceModel); - } - - private <T> boolean isTypeOf(T object, String type, String getTypesMethodName, - ServiceTemplate serviceTemplate, - ToscaServiceModel toscaServiceModel) { - if (object == null) { - return false; - } - - try { - String objectType = (String) object.getClass().getMethod(GET_TYPE_METHOD_NAME).invoke(object); - if (Objects.equals(objectType, type)) { - return true; - } + @Override + public NodeType createInitSubstitutionNodeType(ServiceTemplate substitutionServiceTemplate, + String nodeTypeDerivedFromValue) { + NodeType substitutionNodeType = new NodeType(); + substitutionNodeType.setDerived_from(nodeTypeDerivedFromValue); + substitutionNodeType.setDescription(substitutionServiceTemplate.getDescription()); + substitutionNodeType.setProperties(manageSubstitutionNodeTypeProperties(substitutionServiceTemplate)); + substitutionNodeType.setAttributes(manageSubstitutionNodeTypeAttributes(substitutionServiceTemplate)); + return substitutionNodeType; + } + + @Override + public Map<String, PropertyDefinition> manageSubstitutionNodeTypeProperties(ServiceTemplate substitutionServiceTemplate) { + Map<String, PropertyDefinition> substitutionNodeTypeProperties = new HashMap<>(); + Map<String, ParameterDefinition> properties = substitutionServiceTemplate.getTopology_template().getInputs(); + if (properties == null) { + return null; + } + + PropertyDefinition propertyDefinition; + String toscaPropertyName; + for (Map.Entry<String, ParameterDefinition> entry : properties.entrySet()) { + toscaPropertyName = entry.getKey(); + propertyDefinition = new PropertyDefinition(); + ParameterDefinition parameterDefinition = + substitutionServiceTemplate.getTopology_template().getInputs().get(toscaPropertyName); + propertyDefinition.setType(parameterDefinition.getType()); + propertyDefinition.setDescription(parameterDefinition.getDescription()); + propertyDefinition.set_default(parameterDefinition.get_default()); + if (parameterDefinition.getRequired() != null) { + propertyDefinition.setRequired(parameterDefinition.getRequired()); + } + if (propertyDefinition.get_default() != null) { + propertyDefinition.setRequired(false); + } + if (!CollectionUtils.isEmpty(parameterDefinition.getConstraints())) { + propertyDefinition.setConstraints(parameterDefinition.getConstraints()); + } + propertyDefinition.setEntry_schema(parameterDefinition.getEntry_schema()); + if (parameterDefinition.getStatus() != null) { + propertyDefinition.setStatus(parameterDefinition.getStatus()); + } + substitutionNodeTypeProperties.put(toscaPropertyName, propertyDefinition); + } + return substitutionNodeTypeProperties; + } + + + private Map<String, AttributeDefinition> manageSubstitutionNodeTypeAttributes(ServiceTemplate substitutionServiceTemplate) { + Map<String, AttributeDefinition> substitutionNodeTypeAttributes = new HashMap<>(); + Map<String, ParameterDefinition> attributes = substitutionServiceTemplate.getTopology_template().getOutputs(); + if (attributes == null) { + return null; + } + AttributeDefinition attributeDefinition; + String toscaAttributeName; + + for (Map.Entry<String, ParameterDefinition> entry : attributes.entrySet()) { + attributeDefinition = new AttributeDefinition(); + toscaAttributeName = entry.getKey(); + ParameterDefinition parameterDefinition = + substitutionServiceTemplate.getTopology_template().getOutputs().get(toscaAttributeName); + if (parameterDefinition.getType() != null && !parameterDefinition.getType().isEmpty()) { + attributeDefinition.setType(parameterDefinition.getType()); + } else { + attributeDefinition.setType(PropertyType.STRING.getDisplayName()); + } + attributeDefinition.setDescription(parameterDefinition.getDescription()); + attributeDefinition.set_default(parameterDefinition.get_default()); + attributeDefinition.setEntry_schema(parameterDefinition.getEntry_schema()); + if (Objects.nonNull(parameterDefinition.getStatus())) { + attributeDefinition.setStatus(parameterDefinition.getStatus()); + } + substitutionNodeTypeAttributes.put(toscaAttributeName, attributeDefinition); + } + return substitutionNodeTypeAttributes; + } - Optional<Boolean> typeExistInServiceTemplateHierarchy = - isTypeExistInServiceTemplateHierarchy(type, objectType, getTypesMethodName, - serviceTemplate, toscaServiceModel, null); - return typeExistInServiceTemplateHierarchy.orElseThrow(() -> new CoreException( - new ToscaElementTypeNotFoundErrorBuilder(objectType).build())); + /** + * Checks if the requirement exists in the node template. + * + * @param nodeTemplate the node template + * @param requirementId the requirement id + * @param requirementAssignment the requirement assignment + * @return true if the requirement already exists and false otherwise + */ + @Override + public boolean isRequirementExistInNodeTemplate(NodeTemplate nodeTemplate, String requirementId, + RequirementAssignment requirementAssignment) { + List<Map<String, RequirementAssignment>> nodeTemplateRequirements = nodeTemplate.getRequirements(); + return nodeTemplateRequirements != null && nodeTemplateRequirements.stream().anyMatch( + requirement -> requirement.containsKey(requirementId) && DataModelUtil.compareRequirementAssignment( + requirementAssignment, requirement.get(requirementId))); + } + + @Override + public boolean isTypeOf(InterfaceDefinitionType interfaceDefinition, String interfaceType, + ServiceTemplate serviceTemplate, ToscaServiceModel toscaServiceModel) { + return isTypeOf(interfaceDefinition, interfaceType, GET_INTERFACE_TYPE_METHOD_NAME, serviceTemplate, + toscaServiceModel); + } - } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { - throw new RuntimeException(e); + @Override + public boolean isTypeOf(DefinitionOfDataType parameterDefinition, String dataType, ServiceTemplate serviceTemplate, + ToscaServiceModel toscaServiceModel) { + return isTypeOf(parameterDefinition, dataType, GET_DATA_TYPE_METHOD_NAME, serviceTemplate, toscaServiceModel); + } + + private <T> boolean isTypeOf(T object, String type, String getTypesMethodName, ServiceTemplate serviceTemplate, + ToscaServiceModel toscaServiceModel) { + if (object == null) { + return false; + } + + try { + String objectType = (String) object.getClass().getMethod(GET_TYPE_METHOD_NAME).invoke(object); + if (Objects.equals(objectType, type)) { + return true; + } + + Optional<Boolean> typeExistInServiceTemplateHierarchy = + isTypeExistInServiceTemplateHierarchy(type, objectType, getTypesMethodName, serviceTemplate, + toscaServiceModel, null); + return typeExistInServiceTemplateHierarchy.orElseThrow( + () -> new CoreException(new ToscaElementTypeNotFoundErrorBuilder(objectType).build())); + + } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { + throw new RuntimeException(e); + } } - } } + + |