2 * Copyright © 2016-2018 European Support Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.openecomp.sdc.tosca.services;
19 import java.io.ByteArrayInputStream;
20 import java.io.ByteArrayOutputStream;
21 import java.io.IOException;
22 import java.io.NotSerializableException;
23 import java.io.ObjectInputStream;
24 import java.io.ObjectOutputStream;
25 import java.util.ArrayList;
26 import java.util.Collections;
27 import java.util.HashMap;
28 import java.util.List;
29 import java.util.ListIterator;
31 import java.util.Objects;
32 import java.util.Optional;
33 import org.apache.commons.collections4.CollectionUtils;
34 import org.apache.commons.collections4.MapUtils;
35 import org.onap.sdc.tosca.datatypes.model.AttributeDefinition;
36 import org.onap.sdc.tosca.datatypes.model.CapabilityAssignment;
37 import org.onap.sdc.tosca.datatypes.model.CapabilityDefinition;
38 import org.onap.sdc.tosca.datatypes.model.Constraint;
39 import org.onap.sdc.tosca.datatypes.model.EntrySchema;
40 import org.onap.sdc.tosca.datatypes.model.GroupDefinition;
41 import org.onap.sdc.tosca.datatypes.model.Import;
42 import org.onap.sdc.tosca.datatypes.model.NodeTemplate;
43 import org.onap.sdc.tosca.datatypes.model.NodeType;
44 import org.onap.sdc.tosca.datatypes.model.ParameterDefinition;
45 import org.onap.sdc.tosca.datatypes.model.PolicyDefinition;
46 import org.onap.sdc.tosca.datatypes.model.PropertyDefinition;
47 import org.onap.sdc.tosca.datatypes.model.RelationshipTemplate;
48 import org.onap.sdc.tosca.datatypes.model.RequirementAssignment;
49 import org.onap.sdc.tosca.datatypes.model.RequirementDefinition;
50 import org.onap.sdc.tosca.datatypes.model.ServiceTemplate;
51 import org.onap.sdc.tosca.datatypes.model.SubstitutionMapping;
52 import org.onap.sdc.tosca.datatypes.model.TopologyTemplate;
53 import org.onap.sdc.tosca.datatypes.model.heatextend.ParameterDefinitionExt;
54 import org.onap.sdc.tosca.services.ToscaExtensionYamlUtil;
55 import org.onap.sdc.tosca.services.YamlUtil;
56 import org.openecomp.core.utilities.CommonMethods;
57 import org.openecomp.sdc.common.errors.CoreException;
58 import org.openecomp.sdc.logging.api.Logger;
59 import org.openecomp.sdc.logging.api.LoggerFactory;
60 import org.openecomp.sdc.tosca.datatypes.ToscaCapabilityType;
61 import org.openecomp.sdc.tosca.datatypes.ToscaFunctions;
62 import org.openecomp.sdc.tosca.datatypes.ToscaRelationshipType;
63 import org.openecomp.sdc.tosca.errors.InvalidAddActionNullEntityErrorBuilder;
64 import org.openecomp.sdc.tosca.errors.InvalidRequirementAssignmentErrorBuilder;
65 import org.openecomp.sdc.tosca.services.impl.ToscaAnalyzerServiceImpl;
68 * The type Data model util.
70 public class DataModelUtil {
72 private static final Logger LOGGER = LoggerFactory.getLogger(DataModelUtil.class);
73 private static final String SERVICE_TEMPLATE = "Service Template";
74 private static final String NODE_TYPE = "Node Type";
76 private DataModelUtil() {
77 // prevent instantiation
81 * Add substitution mapping.
83 * @param serviceTemplate the service template
84 * @param substitutionMapping the substitution mapping
86 public static void addSubstitutionMapping(ServiceTemplate serviceTemplate,
87 SubstitutionMapping substitutionMapping) {
88 if (serviceTemplate == null) {
89 throw new CoreException(new InvalidAddActionNullEntityErrorBuilder("Substitution Mapping", SERVICE_TEMPLATE)
93 if (serviceTemplate.getTopology_template() == null) {
94 serviceTemplate.setTopology_template(new TopologyTemplate());
96 serviceTemplate.getTopology_template().setSubstitution_mappings(substitutionMapping);
100 * Gets node template directives.
102 * @param nodeTemplate the node template
103 * @return the directives
105 public static List<String> getDirectives(NodeTemplate nodeTemplate) {
106 if (Objects.isNull(nodeTemplate) || Objects.isNull(nodeTemplate.getDirectives())) {
107 return Collections.emptyList();
109 return nodeTemplate.getDirectives();
113 * Add substitution mapping req.
115 * @param serviceTemplate the service template
116 * @param substitutionMappingRequirementId the substitution mapping requirement id
117 * @param substitutionMappingRequirementList the substitution mapping requirement list
119 public static void addSubstitutionMappingReq(ServiceTemplate serviceTemplate,
120 String substitutionMappingRequirementId, List<String> substitutionMappingRequirementList) {
121 if (serviceTemplate == null) {
122 throw new CoreException(
123 new InvalidAddActionNullEntityErrorBuilder("Substitution Mapping Requirements", SERVICE_TEMPLATE)
127 if (serviceTemplate.getTopology_template() == null) {
128 serviceTemplate.setTopology_template(new TopologyTemplate());
130 if (serviceTemplate.getTopology_template().getSubstitution_mappings() == null) {
131 serviceTemplate.getTopology_template().setSubstitution_mappings(new SubstitutionMapping());
133 if (serviceTemplate.getTopology_template().getSubstitution_mappings().getRequirements() == null) {
134 serviceTemplate.getTopology_template().getSubstitution_mappings().setRequirements(new HashMap<>());
137 serviceTemplate.getTopology_template().getSubstitution_mappings().getRequirements()
138 .put(substitutionMappingRequirementId, substitutionMappingRequirementList);
142 * Add substitution mapping capability.
144 * @param serviceTemplate the service template
145 * @param substitutionMappingCapabilityId the substitution mapping capability id
146 * @param substitutionMappingCapabilityList the substitution mapping capability list
148 public static void addSubstitutionMappingCapability(ServiceTemplate serviceTemplate,
149 String substitutionMappingCapabilityId, List<String> substitutionMappingCapabilityList) {
150 if (serviceTemplate == null) {
151 throw new CoreException(new InvalidAddActionNullEntityErrorBuilder("Substitution Mapping Capabilities",
152 SERVICE_TEMPLATE).build());
155 if (serviceTemplate.getTopology_template() == null) {
156 serviceTemplate.setTopology_template(new TopologyTemplate());
158 if (serviceTemplate.getTopology_template().getSubstitution_mappings() == null) {
159 serviceTemplate.getTopology_template().setSubstitution_mappings(new SubstitutionMapping());
161 if (serviceTemplate.getTopology_template().getSubstitution_mappings().getCapabilities() == null) {
162 serviceTemplate.getTopology_template().getSubstitution_mappings().setCapabilities(new HashMap<>());
165 serviceTemplate.getTopology_template().getSubstitution_mappings().getCapabilities()
166 .putIfAbsent(substitutionMappingCapabilityId, substitutionMappingCapabilityList);
170 * Gets node templates from the service template.
172 * @param serviceTemplate the service template
173 * @return the service template node templates and empty map if not present
175 public static Map<String, NodeTemplate> getNodeTemplates(ServiceTemplate serviceTemplate) {
176 if (Objects.isNull(serviceTemplate) || Objects.isNull(serviceTemplate.getTopology_template())
177 || MapUtils.isEmpty(serviceTemplate.getTopology_template().getNode_templates())) {
178 return new HashMap<>();
181 return serviceTemplate.getTopology_template().getNode_templates();
185 * Gets groups from the service template.
187 * @param serviceTemplate the service template
188 * @return the service template groups and empty map if not present
190 public static Map<String, GroupDefinition> getGroups(ServiceTemplate serviceTemplate) {
191 if (Objects.isNull(serviceTemplate) || Objects.isNull(serviceTemplate.getTopology_template())
192 || MapUtils.isEmpty(serviceTemplate.getTopology_template().getGroups())) {
193 return new HashMap<>();
196 return serviceTemplate.getTopology_template().getGroups();
202 * @param serviceTemplate the service template
203 * @param nodeTemplateId the node template id
204 * @param nodeTemplate the node template
206 public static void addNodeTemplate(ServiceTemplate serviceTemplate, String nodeTemplateId,
207 NodeTemplate nodeTemplate) {
208 if (serviceTemplate == null) {
209 throw new CoreException(
210 new InvalidAddActionNullEntityErrorBuilder("Node Template", SERVICE_TEMPLATE).build());
212 TopologyTemplate topologyTemplate = serviceTemplate.getTopology_template();
213 if (Objects.isNull(topologyTemplate)) {
214 topologyTemplate = new TopologyTemplate();
215 serviceTemplate.setTopology_template(topologyTemplate);
217 if (topologyTemplate.getNode_templates() == null) {
218 topologyTemplate.setNode_templates(new HashMap<>());
220 topologyTemplate.getNode_templates().put(nodeTemplateId, nodeTemplate);
224 * Add capabilities def to node type.
226 * @param nodeType the node type
227 * @param capabilities the capability definitions
229 public static void addNodeTypeCapabilitiesDef(NodeType nodeType, Map<String, CapabilityDefinition> capabilities) {
230 if (MapUtils.isEmpty(capabilities) || capabilities.entrySet().isEmpty()) {
234 if (nodeType == null) {
235 throw new CoreException(
236 new InvalidAddActionNullEntityErrorBuilder("Capability Definition", NODE_TYPE).build());
239 if (MapUtils.isEmpty(nodeType.getCapabilities())) {
240 nodeType.setCapabilities(new HashMap<>());
243 for (Map.Entry<String, CapabilityDefinition> entry : capabilities.entrySet()) {
244 nodeType.getCapabilities().put(entry.getKey(), entry.getValue());
249 * Set capabilities def to node type.
251 * @param nodeType the node type
252 * @param capabilities the capability definitions
254 public static void setNodeTypeCapabilitiesDef(NodeType nodeType, Map<String, CapabilityDefinition> capabilities) {
255 if (MapUtils.isEmpty(capabilities) || capabilities.entrySet().isEmpty()) {
259 if (nodeType == null) {
260 throw new CoreException(new InvalidAddActionNullEntityErrorBuilder("Capability Definition", NODE_TYPE)
264 if (MapUtils.isEmpty(nodeType.getCapabilities())) {
265 nodeType.setCapabilities(new HashMap<>());
267 if (capabilities.size() > 0) {
268 nodeType.setCapabilities(new HashMap<>());
270 for (Map.Entry<String, CapabilityDefinition> entry : capabilities.entrySet()) {
271 nodeType.getCapabilities().put(entry.getKey(), entry.getValue());
276 * Add policy definition.
278 * @param serviceTemplate the service template
279 * @param policyId the policy id
280 * @param policyDefinition the policy definition
282 public static void addPolicyDefinition(ServiceTemplate serviceTemplate, String policyId,
283 PolicyDefinition policyDefinition) {
284 if (serviceTemplate == null) {
285 throw new CoreException(
286 new InvalidAddActionNullEntityErrorBuilder("Policy Definition", SERVICE_TEMPLATE).build());
288 TopologyTemplate topologyTemplate = serviceTemplate.getTopology_template();
289 if (Objects.isNull(topologyTemplate)) {
290 topologyTemplate = new TopologyTemplate();
291 serviceTemplate.setTopology_template(topologyTemplate);
293 if (topologyTemplate.getPolicies() == null) {
294 topologyTemplate.setPolicies(new HashMap<>());
296 topologyTemplate.getPolicies().put(policyId, policyDefinition);
302 * @param serviceTemplate the service template
303 * @param nodeTypeId the node type id
304 * @param nodeType the node type
306 public static void addNodeType(ServiceTemplate serviceTemplate, String nodeTypeId, NodeType nodeType) {
307 if (serviceTemplate == null) {
308 throw new CoreException(new InvalidAddActionNullEntityErrorBuilder(NODE_TYPE, SERVICE_TEMPLATE).build());
310 if (serviceTemplate.getNode_types() == null) {
311 serviceTemplate.setNode_types(new HashMap<>());
313 serviceTemplate.getNode_types().put(nodeTypeId, nodeType);
317 * Add relationship template.
319 * @param serviceTemplate the service template
320 * @param relationshipTemplateId the relationship template id
321 * @param relationshipTemplate the relationship template
323 public static void addRelationshipTemplate(ServiceTemplate serviceTemplate, String relationshipTemplateId,
324 RelationshipTemplate relationshipTemplate) {
325 if (serviceTemplate == null) {
326 throw new CoreException(
327 new InvalidAddActionNullEntityErrorBuilder("Relationship Template", SERVICE_TEMPLATE).build());
329 if (serviceTemplate.getTopology_template() == null) {
330 serviceTemplate.setTopology_template(new TopologyTemplate());
332 if (serviceTemplate.getTopology_template().getRelationship_templates() == null) {
333 serviceTemplate.getTopology_template().setRelationship_templates(new HashMap<>());
335 serviceTemplate.getTopology_template().getRelationship_templates()
336 .put(relationshipTemplateId, relationshipTemplate);
340 * Add requirement assignment.
342 * @param nodeTemplate the node template
343 * @param requirementId the requirement id
344 * @param requirementAssignment the requirement assignment
346 public static void addRequirementAssignment(NodeTemplate nodeTemplate, String requirementId,
347 RequirementAssignment requirementAssignment) {
348 if (nodeTemplate == null) {
349 throw new CoreException(
350 new InvalidAddActionNullEntityErrorBuilder("Requirement Assignment",
351 "Node Template").build());
353 if (requirementAssignment.getNode() == null) {
354 throw new CoreException(new InvalidRequirementAssignmentErrorBuilder(requirementId).build());
357 if (nodeTemplate.getRequirements() == null) {
358 nodeTemplate.setRequirements(new ArrayList<>());
360 Map<String, RequirementAssignment> requirement = new HashMap<>();
361 requirement.put(requirementId, requirementAssignment);
362 nodeTemplate.getRequirements().add(requirement);
366 * Creates a new requirement assignment object for attachment requirement.
368 * @param node the node
369 * @return the attachment requirement assignment object
371 public static RequirementAssignment createAttachmentRequirementAssignment(String node) {
372 RequirementAssignment requirement = new RequirementAssignment();
373 requirement.setCapability(ToscaCapabilityType.NATIVE_ATTACHMENT);
374 requirement.setNode(node);
375 requirement.setRelationship(ToscaRelationshipType.ATTACHES_TO);
380 * Gets node template.
382 * @param serviceTemplate the service template
383 * @param nodeTemplateId the node template id
384 * @return the node template
386 public static NodeTemplate getNodeTemplate(ServiceTemplate serviceTemplate, String nodeTemplateId) {
387 if (serviceTemplate == null || serviceTemplate.getTopology_template() == null
388 || serviceTemplate.getTopology_template().getNode_templates() == null) {
391 return serviceTemplate.getTopology_template().getNode_templates().get(nodeTemplateId);
397 * @param serviceTemplate the service template
398 * @param nodeTypeId the node type id
399 * @return the node type
401 public static NodeType getNodeType(ServiceTemplate serviceTemplate, String nodeTypeId) {
402 if (serviceTemplate == null || serviceTemplate.getNode_types() == null) {
405 return serviceTemplate.getNode_types().get(nodeTypeId);
409 * Gets requirement definition.
411 * @param nodeType the node type
412 * @param requirementDefinitionId the requirement definition id
413 * @return the requirement definition
415 public static Optional<RequirementDefinition> getRequirementDefinition(NodeType nodeType,
416 String requirementDefinitionId) {
417 if (nodeType == null || nodeType.getRequirements() == null || requirementDefinitionId == null) {
418 return Optional.empty();
420 return getRequirementDefinition(nodeType.getRequirements(), requirementDefinitionId);
424 * get requirement definition from requirement definition list by req key.
426 * @param requirementsDefinitionList requirement definition list
427 * @param requirementKey requirement key
429 public static Optional<RequirementDefinition> getRequirementDefinition(
430 List<Map<String, RequirementDefinition>> requirementsDefinitionList, String requirementKey) {
431 if (CollectionUtils.isEmpty(requirementsDefinitionList)) {
432 return Optional.empty();
435 ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
436 for (Map<String, RequirementDefinition> requirementMap : requirementsDefinitionList) {
437 if (requirementMap.containsKey(requirementKey)) {
438 RequirementDefinition requirementDefinition = toscaExtensionYamlUtil.yamlToObject(
439 toscaExtensionYamlUtil.objectToYaml(requirementMap.get(requirementKey)),
440 RequirementDefinition.class);
441 return Optional.of(requirementDefinition);
444 return Optional.empty();
448 * Gets capability definition.
450 * @param nodeType the node type
451 * @param capabilityDefinitionId the capability definition id
452 * @return the capability definition
454 public static Optional<CapabilityDefinition> getCapabilityDefinition(NodeType nodeType,
455 String capabilityDefinitionId) {
456 if (nodeType == null || nodeType.getCapabilities() == null || capabilityDefinitionId == null) {
457 return Optional.empty();
459 return Optional.ofNullable(nodeType.getCapabilities().get(capabilityDefinitionId));
463 * Add group definition to topology template.
465 * @param serviceTemplate the service template
466 * @param groupName the group name
467 * @param group the group
469 public static void addGroupDefinitionToTopologyTemplate(ServiceTemplate serviceTemplate, String groupName,
470 GroupDefinition group) {
471 if (serviceTemplate == null) {
472 throw new CoreException(
473 new InvalidAddActionNullEntityErrorBuilder("Group Definition", SERVICE_TEMPLATE).build());
476 TopologyTemplate topologyTemplate = serviceTemplate.getTopology_template();
477 if (Objects.isNull(topologyTemplate)) {
478 topologyTemplate = new TopologyTemplate();
479 serviceTemplate.setTopology_template(topologyTemplate);
481 if (topologyTemplate.getGroups() == null) {
482 topologyTemplate.setGroups(new HashMap<>());
484 if (serviceTemplate.getTopology_template().getGroups() == null) {
485 Map<String, GroupDefinition> groups = new HashMap<>();
486 serviceTemplate.getTopology_template().setGroups(groups);
489 serviceTemplate.getTopology_template().getGroups().put(groupName, group);
493 * Adds a group member to an existing group in the service template.
495 * @param serviceTemplate the service template
496 * @param groupName the group name
497 * @param groupMemberId the group member id
499 public static void addGroupMember(ServiceTemplate serviceTemplate, String groupName, String groupMemberId) {
500 TopologyTemplate topologyTemplate = serviceTemplate.getTopology_template();
501 if (Objects.isNull(topologyTemplate) || topologyTemplate.getGroups() == null
502 || topologyTemplate.getGroups().get(groupName) == null) {
506 GroupDefinition groupDefinition = topologyTemplate.getGroups().get(groupName);
507 if (CollectionUtils.isEmpty(groupDefinition.getMembers())) {
508 groupDefinition.setMembers(new ArrayList<>());
511 if (!groupDefinition.getMembers().contains(groupMemberId)) {
512 groupDefinition.getMembers().add(groupMemberId);
517 * Create parameter definition property definition.
519 * @param type the type
520 * @param description the description
521 * @param required the required
522 * @param constraints the constraints
523 * @param entrySchema the entry schema
524 * @param defaultVal the default val
525 * @return the property definition
527 public static ParameterDefinition createParameterDefinition(String type, String description, boolean required,
528 List<Constraint> constraints, EntrySchema entrySchema, Object defaultVal) {
529 ParameterDefinition paramDef = new ParameterDefinition();
530 paramDef.setType(type);
531 paramDef.setDescription(description);
532 paramDef.setRequired(required);
533 paramDef.setConstraints(constraints);
534 paramDef.setEntry_schema(entrySchema == null ? null : entrySchema.clone());
535 paramDef.set_default(defaultVal);
540 * Create requirement requirement definition.
542 * @param capability the capability
543 * @param node the node
544 * @param relationship the relationship
545 * @param occurrences the occurrences
546 * @return the requirement definition
548 public static RequirementDefinition createRequirement(String capability, String node, String relationship,
549 Object[] occurrences) {
550 RequirementDefinition requirementDefinition = new RequirementDefinition();
551 requirementDefinition.setCapability(capability);
552 requirementDefinition.setNode(node);
553 requirementDefinition.setRelationship(relationship);
554 if (occurrences != null) {
555 requirementDefinition.setOccurrences(occurrences);
557 return requirementDefinition;
561 * Create entry schema entry schema.
563 * @param type the type
564 * @param description the description
565 * @param constraints the constraints
566 * @return the entry schema
568 public static EntrySchema createEntrySchema(String type, String description, List<Constraint> constraints) {
569 if (Objects.isNull(type) && Objects.isNull(description) && CollectionUtils.isEmpty(constraints)) {
573 EntrySchema entrySchema = new EntrySchema();
574 entrySchema.setType(type);
575 entrySchema.setDescription(description);
576 entrySchema.setConstraints(constraints);
581 * Create get input property value from list parameter map.
583 * @param inputPropertyListName the input property list name
584 * @param indexInTheList the index in the list
585 * @param nestedPropertyName the nested property name
588 public static Map createGetInputPropertyValueFromListParameter(String inputPropertyListName, int indexInTheList,
589 String... nestedPropertyName) {
590 List<Object> propertyList = new ArrayList<>();
591 propertyList.add(inputPropertyListName);
592 propertyList.add(indexInTheList);
593 if (nestedPropertyName != null) {
594 Collections.addAll(propertyList, nestedPropertyName);
596 Map<String, Object> getInputProperty = new HashMap<>();
597 getInputProperty.put(ToscaFunctions.GET_INPUT.getDisplayName(), propertyList);
598 return getInputProperty;
602 * Convert property def to parameter def parameter definition ext.
604 * @param propertyDefinition the property definition
605 * @return the parameter definition ext
607 public static ParameterDefinitionExt convertPropertyDefToParameterDef(PropertyDefinition propertyDefinition) {
608 if (propertyDefinition == null) {
612 ParameterDefinitionExt parameterDefinition = new ParameterDefinitionExt();
613 parameterDefinition.setType(propertyDefinition.getType());
614 parameterDefinition.setDescription(propertyDefinition.getDescription());
615 parameterDefinition.setRequired(propertyDefinition.getRequired());
616 parameterDefinition.set_default(propertyDefinition.get_default());
617 parameterDefinition.setStatus(propertyDefinition.getStatus());
618 parameterDefinition.setConstraints(propertyDefinition.getConstraints());
619 parameterDefinition.setEntry_schema(Objects.isNull(propertyDefinition.getEntry_schema()) ? null :
620 propertyDefinition.getEntry_schema().clone());
621 parameterDefinition.setHidden(false);
622 parameterDefinition.setImmutable(false);
623 return parameterDefinition;
627 * Convert attribute def to parameter def parameter definition ext.
629 * @param attributeDefinition the attribute definition
630 * @param outputValue the output value
631 * @return the parameter definition ext
633 public static ParameterDefinitionExt convertAttributeDefToParameterDef(AttributeDefinition attributeDefinition,
634 Map<String, List> outputValue) {
635 if (attributeDefinition == null) {
638 ParameterDefinitionExt parameterDefinition = new ParameterDefinitionExt();
639 parameterDefinition.setDescription(attributeDefinition.getDescription());
640 parameterDefinition.setValue(outputValue);
641 return parameterDefinition;
644 public static boolean isNodeTemplate(String entryId, ServiceTemplate serviceTemplate) {
645 return serviceTemplate.getTopology_template().getNode_templates() != null
646 && serviceTemplate.getTopology_template().getNode_templates().get(entryId) != null;
650 * Add Input parameter.
652 * @param serviceTemplate the service template
653 * @param parameterDefinitionId the parameter definition id
654 * @param parameterDefinition the parameter definition
656 public static void addInputParameterToTopologyTemplate(ServiceTemplate serviceTemplate,
657 String parameterDefinitionId, ParameterDefinition parameterDefinition) {
658 if (Objects.isNull(serviceTemplate)) {
659 throw new CoreException(
660 new InvalidAddActionNullEntityErrorBuilder("Topology Template Input Parameter", SERVICE_TEMPLATE)
663 TopologyTemplate topologyTemplate = serviceTemplate.getTopology_template();
664 if (Objects.isNull(topologyTemplate)) {
665 topologyTemplate = new TopologyTemplate();
666 serviceTemplate.setTopology_template(topologyTemplate);
668 if (topologyTemplate.getInputs() == null) {
669 topologyTemplate.setInputs(new HashMap<>());
671 topologyTemplate.getInputs().put(parameterDefinitionId, parameterDefinition);
675 * Add Output parameter.
677 * @param serviceTemplate the service template
678 * @param parameterDefinitionId the parameter definition id
679 * @param parameterDefinition the parameter definition
681 public static void addOutputParameterToTopologyTemplate(ServiceTemplate serviceTemplate,
682 String parameterDefinitionId, ParameterDefinition parameterDefinition) {
683 if (Objects.isNull(serviceTemplate)) {
684 throw new CoreException(
685 new InvalidAddActionNullEntityErrorBuilder("Topology Template Output Parameter", SERVICE_TEMPLATE)
688 TopologyTemplate topologyTemplate = serviceTemplate.getTopology_template();
689 if (Objects.isNull(topologyTemplate)) {
690 topologyTemplate = new TopologyTemplate();
691 serviceTemplate.setTopology_template(topologyTemplate);
693 if (topologyTemplate.getOutputs() == null) {
694 topologyTemplate.setOutputs(new HashMap<>());
696 topologyTemplate.getOutputs().put(parameterDefinitionId, parameterDefinition);
700 * Add requirement def to requirement def list.
702 * @param requirementList requirement list
703 * @param requirementDef added requirement def
705 public static void addRequirementToList(List<Map<String, RequirementDefinition>> requirementList,
706 Map<String, RequirementDefinition> requirementDef) {
707 if (requirementDef == null) {
710 if (requirementList == null) {
711 requirementList = new ArrayList<>();
714 for (Map.Entry<String, RequirementDefinition> entry : requirementDef.entrySet()) {
715 CommonMethods.mergeEntryInList(entry.getKey(), entry.getValue(), requirementList);
720 * get node template requirement.
722 * @param nodeTemplate node template
724 public static Map<String, RequirementAssignment> getNodeTemplateRequirements(NodeTemplate nodeTemplate) {
725 if (Objects.isNull(nodeTemplate)) {
728 List<Map<String, RequirementAssignment>> templateRequirements = nodeTemplate.getRequirements();
730 Map<String, RequirementAssignment> nodeTemplateRequirementsAssignment = new HashMap<>();
731 if (CollectionUtils.isEmpty(templateRequirements)) {
732 return nodeTemplateRequirementsAssignment;
734 ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
735 for (Map<String, RequirementAssignment> requirementAssignmentMap : templateRequirements) {
736 for (Map.Entry<String, RequirementAssignment> requirementEntry : requirementAssignmentMap.entrySet()) {
737 RequirementAssignment requirementAssignment = (toscaExtensionYamlUtil.yamlToObject(
738 toscaExtensionYamlUtil.objectToYaml(requirementEntry.getValue()), RequirementAssignment.class));
739 nodeTemplateRequirementsAssignment.put(requirementEntry.getKey(), requirementAssignment);
742 return nodeTemplateRequirementsAssignment;
746 * Gets the list of requirements for the node template.
748 * @param nodeTemplate the node template
749 * @return the node template requirement list and null if the node has no requirements
751 public static List<Map<String, RequirementAssignment>> getNodeTemplateRequirementList(NodeTemplate nodeTemplate) {
752 ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
753 //Creating concrete objects
754 List<Map<String, RequirementAssignment>> requirements = nodeTemplate.getRequirements();
755 List<Map<String, RequirementAssignment>> concreteRequirementList = null;
756 if (requirements != null) {
757 concreteRequirementList = new ArrayList<>();
758 ListIterator<Map<String, RequirementAssignment>> reqListIterator = requirements.listIterator();
759 while (reqListIterator.hasNext()) {
760 Map<String, RequirementAssignment> requirement = reqListIterator.next();
761 Map<String, RequirementAssignment> concreteRequirement = new HashMap<>();
762 for (Map.Entry<String, RequirementAssignment> reqEntry : requirement.entrySet()) {
763 RequirementAssignment requirementAssignment = (toscaExtensionYamlUtil.yamlToObject(
764 toscaExtensionYamlUtil.objectToYaml(reqEntry.getValue()), RequirementAssignment.class));
765 concreteRequirement.put(reqEntry.getKey(), requirementAssignment);
766 concreteRequirementList.add(concreteRequirement);
767 reqListIterator.remove();
770 requirements.clear();
771 requirements.addAll(concreteRequirementList);
772 nodeTemplate.setRequirements(requirements);
774 return concreteRequirementList;
778 * get requirement assignment from requirement assignment list by req key.
780 * @param requirementsAssignmentList requirement definition list
781 * @param requirementKey requirement key
783 public static Optional<List<RequirementAssignment>> getRequirementAssignment(
784 List<Map<String, RequirementAssignment>> requirementsAssignmentList, String requirementKey) {
785 if (CollectionUtils.isEmpty(requirementsAssignmentList)) {
786 return Optional.empty();
789 List<RequirementAssignment> matchRequirementAssignmentList = new ArrayList<>();
790 for (Map<String, RequirementAssignment> requirementMap : requirementsAssignmentList) {
791 if (requirementMap.containsKey(requirementKey)) {
792 ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
793 RequirementAssignment requirementAssignment = (toscaExtensionYamlUtil.yamlToObject(
794 toscaExtensionYamlUtil.objectToYaml(requirementMap.get(requirementKey)),
795 RequirementAssignment.class));
796 matchRequirementAssignmentList.add(requirementAssignment);
799 if (CollectionUtils.isEmpty(matchRequirementAssignmentList)) {
800 return Optional.empty();
802 return Optional.of(matchRequirementAssignmentList);
806 * remove requirement definition from requirement definition list by req key.
808 * @param requirementsDefinitionList requirement definition list
809 * @param requirementKey requirement key
811 public static void removeRequirementsDefinition(List<Map<String, RequirementDefinition>> requirementsDefinitionList,
812 String requirementKey) {
813 if (requirementsDefinitionList == null) {
817 List<Map<String, RequirementDefinition>> mapToBeRemoved = new ArrayList<>();
818 for (Map<String, RequirementDefinition> reqMap : requirementsDefinitionList) {
819 reqMap.remove(requirementKey);
820 if (reqMap.isEmpty()) {
821 mapToBeRemoved.add(reqMap);
824 for (Map<String, RequirementDefinition> removeMap : mapToBeRemoved) {
825 requirementsDefinitionList.remove(removeMap);
830 * remove requirement assignment from requirement definition list by req key.
832 * @param requirementsAssignmentList requirement Assignment list
833 * @param requirementKey requirement key
835 public static void removeRequirementsAssignment(List<Map<String, RequirementAssignment>> requirementsAssignmentList,
836 String requirementKey) {
837 if (requirementsAssignmentList == null) {
841 List<Map<String, RequirementAssignment>> mapToBeRemoved = new ArrayList<>();
842 for (Map<String, RequirementAssignment> reqMap : requirementsAssignmentList) {
843 reqMap.remove(requirementKey);
844 if (reqMap.isEmpty()) {
845 mapToBeRemoved.add(reqMap);
848 for (Map<String, RequirementAssignment> removeMap : mapToBeRemoved) {
849 requirementsAssignmentList.remove(removeMap);
855 * Remove requirement assignment.
857 * @param nodeTemplate the node template
858 * @param requirementKey the requirement key
859 * @param requirementAssignmentToBeDeleted the requirement assignment to be deleted
861 public static void removeRequirementAssignment(NodeTemplate nodeTemplate, String requirementKey,
862 RequirementAssignment requirementAssignmentToBeDeleted) {
863 ToscaAnalyzerService toscaAnalyzerService = new ToscaAnalyzerServiceImpl();
864 List<Map<String, RequirementAssignment>> nodeTemplateRequirements = nodeTemplate.getRequirements();
865 if (nodeTemplateRequirements == null) {
869 ListIterator<Map<String, RequirementAssignment>> iter = nodeTemplateRequirements.listIterator();
870 while (iter.hasNext()) {
871 Map<String, RequirementAssignment> reqMap = iter.next();
872 RequirementAssignment requirementAssignment = reqMap.get(requirementKey);
873 if (requirementAssignment != null) {
874 boolean isDesiredRequirementAssignment = toscaAnalyzerService
875 .isDesiredRequirementAssignment(requirementAssignment,
876 requirementAssignmentToBeDeleted
878 requirementAssignmentToBeDeleted.getNode(),
879 requirementAssignmentToBeDeleted
881 if (isDesiredRequirementAssignment) {
889 * Return the suffix of the input namespace For an exampale - for abc.sdf.vsrx, return vsrx
891 * @param namespace namespace
892 * @return String namespace suffix
894 public static String getNamespaceSuffix(String namespace) {
895 if (namespace == null) {
898 String delimiterChar = ".";
899 if (namespace.contains(delimiterChar)) {
900 return namespace.substring(namespace.lastIndexOf(delimiterChar) + 1);
906 * Return true if the input import exist in the input imports list.
908 * @param imports namespace
909 * @param importId namespace
910 * @return true if exist, false if not exist
912 public static boolean isImportAddedToServiceTemplate(List<Map<String, Import>> imports, String importId) {
913 for (Map<String, Import> anImport : imports) {
914 if (anImport.containsKey(importId)) {
922 * Get output parameter according to the input outputParameterId.
924 * @param serviceTemplate service template
925 * @param outputParameterId output parameter id
926 * @return ParameterDefinition - output parameter
928 public static ParameterDefinition getOuputParameter(ServiceTemplate serviceTemplate, String outputParameterId) {
929 if (serviceTemplate == null || serviceTemplate.getTopology_template() == null
930 || serviceTemplate.getTopology_template().getOutputs() == null) {
933 return serviceTemplate.getTopology_template().getOutputs().get(outputParameterId);
937 * Gets input parameters in a service template.
939 * @param serviceTemplate the service template
940 * @return the input parameters
942 public static Map<String, ParameterDefinition> getInputParameters(ServiceTemplate serviceTemplate) {
943 if (serviceTemplate == null || serviceTemplate.getTopology_template() == null
944 || serviceTemplate.getTopology_template().getInputs() == null) {
947 return serviceTemplate.getTopology_template().getInputs();
951 * Gets relationship templates in a service template.
953 * @param serviceTemplate the service template
954 * @return the relationship template
956 public static Map<String, RelationshipTemplate> getRelationshipTemplates(ServiceTemplate serviceTemplate) {
957 if (serviceTemplate == null || serviceTemplate.getTopology_template() == null
958 || serviceTemplate.getTopology_template().getRelationship_templates() == null) {
961 return serviceTemplate.getTopology_template().getRelationship_templates();
965 * Get property value according to the input propertyId.
967 * @param nodeTemplate node template
968 * @param propertyId property id
969 * @return Object property Value
971 public static Object getPropertyValue(NodeTemplate nodeTemplate, String propertyId) {
972 if (nodeTemplate == null || nodeTemplate.getProperties() == null) {
975 return nodeTemplate.getProperties().get(propertyId);
979 * Get node template properties according to the input node template id.
981 * @param serviceTemplate service template
982 * @param nodeTemplateId node template id
983 * @return node template properties
985 public static Map<String, Object> getNodeTemplateProperties(ServiceTemplate serviceTemplate,
986 String nodeTemplateId) {
987 if (serviceTemplate == null || serviceTemplate.getTopology_template() == null
988 || serviceTemplate.getTopology_template().getNode_templates() == null
989 || serviceTemplate.getTopology_template().getNode_templates().get(nodeTemplateId) == null) {
992 return serviceTemplate.getTopology_template().getNode_templates().get(nodeTemplateId).getProperties();
996 * Adds a property to a node template.
998 * @param nodeTemplate the node template
999 * @param propertyKey the property key
1000 * @param propertyValue the property value
1002 public static void addNodeTemplateProperty(NodeTemplate nodeTemplate, String propertyKey, Object propertyValue) {
1003 if (Objects.isNull(nodeTemplate)) {
1007 if (MapUtils.isEmpty(nodeTemplate.getProperties())) {
1008 nodeTemplate.setProperties(new HashMap<>());
1011 nodeTemplate.getProperties().put(propertyKey, propertyValue);
1015 * Gets substitution mappings in a service template.
1017 * @param serviceTemplate the service template
1018 * @return the substitution mappings
1020 public static SubstitutionMapping getSubstitutionMappings(ServiceTemplate serviceTemplate) {
1021 if (serviceTemplate == null || serviceTemplate.getTopology_template() == null
1022 || serviceTemplate.getTopology_template().getSubstitution_mappings() == null) {
1025 return serviceTemplate.getTopology_template().getSubstitution_mappings();
1030 * Compare two requirement assignment objects for equality.
1032 * @param first the first requirement assignment object
1033 * @param second the second requirement assignment object
1034 * @return true if objects are equal and false otherwise
1036 public static boolean compareRequirementAssignment(RequirementAssignment first, RequirementAssignment second) {
1037 return (first.getCapability().equals(second.getCapability()) && first.getNode().equals(second.getNode())
1038 && first.getRelationship().equals(second.getRelationship()));
1042 * Gets a deep copy clone of the input object.
1044 * @param <T> the type parameter
1045 * @param objectValue the object value
1046 * @param clazz the clazz
1047 * @return the cloned object
1049 public static <T> Object getClonedObject(Object objectValue, Class<T> clazz) {
1050 YamlUtil yamlUtil = new ToscaExtensionYamlUtil();
1051 Object clonedObjectValue;
1052 String objectToYaml = yamlUtil.objectToYaml(objectValue);
1053 clonedObjectValue = yamlUtil.yamlToObject(objectToYaml, clazz);
1054 return clonedObjectValue;
1058 * Gets a deep copy clone of the input object.
1060 * @param obj the object to be cloned
1061 * @return the cloned object
1063 public static Object getClonedObject(Object obj) {
1064 Object clonedObjectValue;
1067 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
1068 ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
1069 objectOutputStream.writeObject(obj);
1070 //Deserialize object
1071 ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
1072 ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
1073 clonedObjectValue = objectInputStream.readObject();
1074 } catch (NotSerializableException ex) {
1075 LOGGER.debug(ex.getMessage(), ex);
1076 return getClonedObject(obj, obj.getClass());
1077 } catch (IOException | ClassNotFoundException ex) {
1078 LOGGER.debug(ex.getMessage(), ex);
1081 return clonedObjectValue;
1085 * Add substitution filtering property.
1087 * @param templateName the substitution service template name
1088 * @param nodeTemplate the node template
1089 * @param count the count
1091 public static void addSubstitutionFilteringProperty(String templateName, NodeTemplate nodeTemplate, int count) {
1092 Map<String, Object> serviceTemplateFilterPropertyValue = new HashMap<>();
1093 Map<String, Object> properties = nodeTemplate.getProperties();
1094 serviceTemplateFilterPropertyValue.put(ToscaConstants.SUBSTITUTE_SERVICE_TEMPLATE_PROPERTY_NAME, templateName);
1095 serviceTemplateFilterPropertyValue.put(ToscaConstants.COUNT_PROPERTY_NAME, count);
1096 properties.put(ToscaConstants.SERVICE_TEMPLATE_FILTER_PROPERTY_NAME, serviceTemplateFilterPropertyValue);
1097 nodeTemplate.setProperties(properties);
1101 * Adding binding requirement from port node template to compute node template.
1103 * @param computeNodeTemplateId compute node template id
1104 * @param portNodeTemplate port node template
1106 public static void addBindingReqFromPortToCompute(String computeNodeTemplateId, NodeTemplate portNodeTemplate) {
1107 RequirementAssignment requirementAssignment = new RequirementAssignment();
1108 requirementAssignment.setCapability(ToscaCapabilityType.NATIVE_NETWORK_BINDABLE);
1109 requirementAssignment.setRelationship(ToscaRelationshipType.NATIVE_NETWORK_BINDS_TO);
1110 requirementAssignment.setNode(computeNodeTemplateId);
1111 addRequirementAssignment(portNodeTemplate, ToscaConstants.BINDING_REQUIREMENT_ID, requirementAssignment);
1115 * Create substitution template substitution mapping.
1117 * @param nodeTypeKey the node type key
1118 * @param substitutionNodeType the substitution node type
1119 * @param mapping the mapping
1120 * @return the substitution mapping
1122 public static SubstitutionMapping createSubstitutionTemplateSubMapping(String nodeTypeKey,
1123 NodeType substitutionNodeType, Map<String, Map<String, List<String>>> mapping) {
1124 SubstitutionMapping substitutionMapping = new SubstitutionMapping();
1125 substitutionMapping.setNode_type(nodeTypeKey);
1126 substitutionMapping.setCapabilities(manageCapabilityMapping(substitutionNodeType.getCapabilities(),
1127 mapping.get(ToscaConstants.CAPABILITY)));
1128 substitutionMapping.setRequirements(
1129 manageRequirementMapping(substitutionNodeType.getRequirements(), mapping.get("requirement")));
1130 return substitutionMapping;
1134 * Add node template capability.
1136 * @param nodeTemplate the node template
1137 * @param capabilityId the capability id
1138 * @param capabilityProperties the capability properties
1139 * @param capabilityAttributes the capability attributes
1141 public static void addNodeTemplateCapability(NodeTemplate nodeTemplate, String capabilityId,
1142 Map<String, Object> capabilityProperties, Map<String, Object> capabilityAttributes) {
1143 Map<String, CapabilityAssignment> capabilities = nodeTemplate.getCapabilities();
1144 if (Objects.isNull(capabilities)) {
1145 capabilities = new HashMap<>();
1147 CapabilityAssignment capabilityAssignment = new CapabilityAssignment();
1148 capabilityAssignment.setProperties(capabilityProperties);
1149 capabilityAssignment.setAttributes(capabilityAttributes);
1150 capabilities.put(capabilityId, capabilityAssignment);
1151 nodeTemplate.setCapabilities(capabilities);
1154 private static Map<String, List<String>> manageRequirementMapping(
1155 List<Map<String, RequirementDefinition>> requirementList,
1156 Map<String, List<String>> requirementSubstitutionMapping) {
1157 if (requirementList == null) {
1160 Map<String, List<String>> requirementMapping = new HashMap<>();
1161 String requirementKey;
1162 List<String> requirementMap;
1163 for (Map<String, RequirementDefinition> requirementDefMap : requirementList) {
1164 for (Map.Entry<String, RequirementDefinition> entry : requirementDefMap.entrySet()) {
1165 requirementKey = entry.getKey();
1166 requirementMap = requirementSubstitutionMapping.get(requirementKey);
1167 requirementMapping.put(requirementKey, requirementMap);
1170 return requirementMapping;
1173 private static Map<String, List<String>> manageCapabilityMapping(Map<String, CapabilityDefinition> capabilities,
1174 Map<String, List<String>> capabilitySubstitutionMapping) {
1175 if (capabilities == null) {
1179 Map<String, List<String>> capabilityMapping = new HashMap<>();
1180 String capabilityKey;
1181 List<String> capabilityMap;
1182 for (Map.Entry<String, CapabilityDefinition> entry : capabilities.entrySet()) {
1183 capabilityKey = entry.getKey();
1184 capabilityMap = capabilitySubstitutionMapping.get(capabilityKey);
1185 capabilityMapping.put(capabilityKey, capabilityMap);
1187 return capabilityMapping;
1190 public static void addSubstitutionNodeTypeRequirements(NodeType substitutionNodeType,
1191 List<Map<String, RequirementDefinition>> requirementsList, String templateName) {
1192 if (CollectionUtils.isEmpty(requirementsList)) {
1195 if (substitutionNodeType.getRequirements() == null) {
1196 substitutionNodeType.setRequirements(new ArrayList<>());
1199 for (Map<String, RequirementDefinition> requirementDef : requirementsList) {
1200 for (Map.Entry<String, RequirementDefinition> entry : requirementDef.entrySet()) {
1201 Map<String, RequirementDefinition> requirementMap = new HashMap<>();
1202 requirementMap.put(entry.getKey() + "_" + templateName, entry.getValue().clone());
1203 substitutionNodeType.getRequirements().add(requirementMap);
1208 public static boolean isNodeTemplateSectionMissingFromServiceTemplate(ServiceTemplate serviceTemplate) {
1209 return Objects.isNull(serviceTemplate.getTopology_template()) || MapUtils.isEmpty(
1210 serviceTemplate.getTopology_template().getNode_templates());
1214 * Gets relationship template in a service template according to the relationship id.
1216 * @param serviceTemplate the service template
1217 * @param relationshipId the relationship id
1218 * @return the relationship template
1220 public static Optional<RelationshipTemplate> getRelationshipTemplate(ServiceTemplate serviceTemplate,
1221 String relationshipId) {
1222 if (serviceTemplate == null || serviceTemplate.getTopology_template() == null
1223 || serviceTemplate.getTopology_template().getRelationship_templates() == null
1224 || serviceTemplate.getTopology_template().getRelationship_templates().get(relationshipId) == null) {
1225 return Optional.empty();
1227 return Optional.of(serviceTemplate.getTopology_template().getRelationship_templates().get(relationshipId));