Support functions in TOSCA Simple Profile in YAML
[sdc.git] / openecomp-be / lib / openecomp-tosca-lib / src / main / java / org / openecomp / sdc / tosca / services / DataModelUtil.java
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.openecomp.sdc.tosca.services;
18
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;
30 import java.util.Map;
31 import java.util.Objects;
32 import java.util.Optional;
33
34 import lombok.AccessLevel;
35 import lombok.NoArgsConstructor;
36 import org.apache.commons.collections4.CollectionUtils;
37 import org.apache.commons.collections4.MapUtils;
38 import org.onap.sdc.tosca.datatypes.model.AttributeDefinition;
39 import org.onap.sdc.tosca.datatypes.model.CapabilityAssignment;
40 import org.onap.sdc.tosca.datatypes.model.CapabilityDefinition;
41 import org.onap.sdc.tosca.datatypes.model.Constraint;
42 import org.onap.sdc.tosca.datatypes.model.EntrySchema;
43 import org.onap.sdc.tosca.datatypes.model.GroupDefinition;
44 import org.onap.sdc.tosca.datatypes.model.Import;
45 import org.onap.sdc.tosca.datatypes.model.NodeTemplate;
46 import org.onap.sdc.tosca.datatypes.model.NodeType;
47 import org.onap.sdc.tosca.datatypes.model.ParameterDefinition;
48 import org.onap.sdc.tosca.datatypes.model.PolicyDefinition;
49 import org.onap.sdc.tosca.datatypes.model.PropertyDefinition;
50 import org.onap.sdc.tosca.datatypes.model.RelationshipTemplate;
51 import org.onap.sdc.tosca.datatypes.model.RequirementAssignment;
52 import org.onap.sdc.tosca.datatypes.model.RequirementDefinition;
53 import org.onap.sdc.tosca.datatypes.model.ServiceTemplate;
54 import org.onap.sdc.tosca.datatypes.model.SubstitutionMapping;
55 import org.onap.sdc.tosca.datatypes.model.TopologyTemplate;
56 import org.onap.sdc.tosca.datatypes.model.heatextend.ParameterDefinitionExt;
57 import org.onap.sdc.tosca.services.ToscaExtensionYamlUtil;
58 import org.onap.sdc.tosca.services.YamlUtil;
59 import org.openecomp.core.utilities.CommonMethods;
60 import org.openecomp.sdc.common.errors.CoreException;
61 import org.openecomp.sdc.logging.api.Logger;
62 import org.openecomp.sdc.logging.api.LoggerFactory;
63 import org.openecomp.sdc.tosca.datatypes.ToscaCapabilityType;
64 import org.openecomp.sdc.tosca.datatypes.ToscaFunctions;
65 import org.openecomp.sdc.tosca.datatypes.ToscaRelationshipType;
66 import org.openecomp.sdc.tosca.errors.InvalidAddActionNullEntityErrorBuilder;
67 import org.openecomp.sdc.tosca.errors.InvalidRequirementAssignmentErrorBuilder;
68 import org.openecomp.sdc.tosca.services.impl.ToscaAnalyzerServiceImpl;
69
70 /**
71  * The type Data model util.
72  */
73 @NoArgsConstructor(access = AccessLevel.PRIVATE)
74 public class DataModelUtil {
75
76     private static final Logger LOGGER = LoggerFactory.getLogger(DataModelUtil.class);
77     private static final String SERVICE_TEMPLATE = "Service Template";
78     private static final String NODE_TYPE = "Node Type";
79
80     /**
81      * Add substitution mapping.
82      *
83      * @param serviceTemplate     the service template
84      * @param substitutionMapping the substitution mapping
85      */
86     public static void addSubstitutionMapping(ServiceTemplate serviceTemplate,
87                                               SubstitutionMapping substitutionMapping) {
88         if (serviceTemplate == null) {
89             throw new CoreException(new InvalidAddActionNullEntityErrorBuilder("Substitution Mapping", SERVICE_TEMPLATE)
90                     .build());
91         }
92
93         if (serviceTemplate.getTopology_template() == null) {
94             serviceTemplate.setTopology_template(new TopologyTemplate());
95         }
96         serviceTemplate.getTopology_template().setSubstitution_mappings(substitutionMapping);
97     }
98
99     /**
100      * Gets node template directives.
101      *
102      * @param nodeTemplate the node template
103      * @return the directives
104      */
105     public static List<String> getDirectives(NodeTemplate nodeTemplate) {
106         if (Objects.isNull(nodeTemplate) || Objects.isNull(nodeTemplate.getDirectives())) {
107             return Collections.emptyList();
108         }
109         return nodeTemplate.getDirectives();
110     }
111
112     /**
113      * Add substitution mapping req.
114      *
115      * @param serviceTemplate                    the service template
116      * @param substitutionMappingRequirementId   the substitution mapping requirement id
117      * @param substitutionMappingRequirementList the substitution mapping requirement list
118      */
119     public static void addSubstitutionMappingReq(ServiceTemplate serviceTemplate,
120                                                  String substitutionMappingRequirementId,
121                                                  List<String> substitutionMappingRequirementList) {
122         if (serviceTemplate == null) {
123             throw new CoreException(
124                     new InvalidAddActionNullEntityErrorBuilder("Substitution Mapping Requirements", SERVICE_TEMPLATE)
125                             .build());
126         }
127
128         if (serviceTemplate.getTopology_template() == null) {
129             serviceTemplate.setTopology_template(new TopologyTemplate());
130         }
131         if (serviceTemplate.getTopology_template().getSubstitution_mappings() == null) {
132             serviceTemplate.getTopology_template().setSubstitution_mappings(new SubstitutionMapping());
133         }
134         if (serviceTemplate.getTopology_template().getSubstitution_mappings().getRequirements() == null) {
135             serviceTemplate.getTopology_template().getSubstitution_mappings().setRequirements(new HashMap<>());
136         }
137
138         serviceTemplate.getTopology_template().getSubstitution_mappings().getRequirements()
139                 .put(substitutionMappingRequirementId, substitutionMappingRequirementList);
140     }
141
142     /**
143      * Add substitution mapping capability.
144      *
145      * @param serviceTemplate                   the service template
146      * @param substitutionMappingCapabilityId   the substitution mapping capability id
147      * @param substitutionMappingCapabilityList the substitution mapping capability list
148      */
149     public static void addSubstitutionMappingCapability(ServiceTemplate serviceTemplate,
150                                                         String substitutionMappingCapabilityId,
151                                                         List<String> substitutionMappingCapabilityList) {
152         if (serviceTemplate == null) {
153             throw new CoreException(new InvalidAddActionNullEntityErrorBuilder("Substitution Mapping Capabilities",
154                     SERVICE_TEMPLATE).build());
155         }
156
157         if (serviceTemplate.getTopology_template() == null) {
158             serviceTemplate.setTopology_template(new TopologyTemplate());
159         }
160         if (serviceTemplate.getTopology_template().getSubstitution_mappings() == null) {
161             serviceTemplate.getTopology_template().setSubstitution_mappings(new SubstitutionMapping());
162         }
163         if (serviceTemplate.getTopology_template().getSubstitution_mappings().getCapabilities() == null) {
164             serviceTemplate.getTopology_template().getSubstitution_mappings().setCapabilities(new HashMap<>());
165         }
166
167         serviceTemplate.getTopology_template().getSubstitution_mappings().getCapabilities()
168                 .putIfAbsent(substitutionMappingCapabilityId, substitutionMappingCapabilityList);
169     }
170
171     /**
172      * Gets node templates from the service template.
173      *
174      * @param serviceTemplate the service template
175      * @return the service template node templates and empty map if not present
176      */
177     public static Map<String, NodeTemplate> getNodeTemplates(ServiceTemplate serviceTemplate) {
178         if (Objects.isNull(serviceTemplate) || Objects.isNull(serviceTemplate.getTopology_template())
179                 || MapUtils.isEmpty(serviceTemplate.getTopology_template().getNode_templates())) {
180             return new HashMap<>();
181         }
182
183         return serviceTemplate.getTopology_template().getNode_templates();
184     }
185
186     /**
187      * Gets groups from the service template.
188      *
189      * @param serviceTemplate the service template
190      * @return the service template groups and empty map if not present
191      */
192     public static Map<String, GroupDefinition> getGroups(ServiceTemplate serviceTemplate) {
193         if (Objects.isNull(serviceTemplate) || Objects.isNull(serviceTemplate.getTopology_template())
194                 || MapUtils.isEmpty(serviceTemplate.getTopology_template().getGroups())) {
195             return new HashMap<>();
196         }
197
198         return serviceTemplate.getTopology_template().getGroups();
199     }
200
201     /**
202      * Add node template.
203      *
204      * @param serviceTemplate the service template
205      * @param nodeTemplateId  the node template id
206      * @param nodeTemplate    the node template
207      */
208     public static void addNodeTemplate(ServiceTemplate serviceTemplate, String nodeTemplateId,
209                                        NodeTemplate nodeTemplate) {
210         if (serviceTemplate == null) {
211             throw new CoreException(
212                     new InvalidAddActionNullEntityErrorBuilder("Node Template", SERVICE_TEMPLATE).build());
213         }
214         TopologyTemplate topologyTemplate = serviceTemplate.getTopology_template();
215         if (Objects.isNull(topologyTemplate)) {
216             topologyTemplate = new TopologyTemplate();
217             serviceTemplate.setTopology_template(topologyTemplate);
218         }
219         if (topologyTemplate.getNode_templates() == null) {
220             topologyTemplate.setNode_templates(new HashMap<>());
221         }
222         topologyTemplate.getNode_templates().put(nodeTemplateId, nodeTemplate);
223     }
224
225     /**
226      * Add capabilities def to node type.
227      *
228      * @param nodeType     the node type
229      * @param capabilities the capability definitions
230      */
231     public static void addNodeTypeCapabilitiesDef(NodeType nodeType, Map<String, CapabilityDefinition> capabilities) {
232         if (MapUtils.isEmpty(capabilities)) {
233             return;
234         }
235
236         if (nodeType == null) {
237             throw new CoreException(
238                     new InvalidAddActionNullEntityErrorBuilder("Capability Definition", NODE_TYPE).build());
239         }
240
241         if (MapUtils.isEmpty(nodeType.getCapabilities())) {
242             nodeType.setCapabilities(new HashMap<>());
243         }
244
245         for (Map.Entry<String, CapabilityDefinition> entry : capabilities.entrySet()) {
246             nodeType.getCapabilities().put(entry.getKey(), entry.getValue());
247         }
248     }
249
250     /**
251      * Set capabilities def to node type.
252      *
253      * @param nodeType     the node type
254      * @param capabilities the capability definitions
255      */
256     public static void setNodeTypeCapabilitiesDef(NodeType nodeType, Map<String, CapabilityDefinition> capabilities) {
257         if (MapUtils.isEmpty(capabilities)) {
258             return;
259         }
260
261         if (nodeType == null) {
262             throw new CoreException(new InvalidAddActionNullEntityErrorBuilder("Capability Definition", NODE_TYPE)
263                     .build());
264         }
265
266         if (MapUtils.isEmpty(nodeType.getCapabilities())) {
267             nodeType.setCapabilities(new HashMap<>());
268         }
269
270         if (MapUtils.isNotEmpty(capabilities)) {
271             nodeType.setCapabilities(new HashMap<>());
272
273             for (Map.Entry<String, CapabilityDefinition> entry : capabilities.entrySet()) {
274                 nodeType.getCapabilities().put(entry.getKey(), entry.getValue());
275             }
276         }
277     }
278
279     /**
280      * Add policy definition.
281      *
282      * @param serviceTemplate  the service template
283      * @param policyId         the policy id
284      * @param policyDefinition the policy definition
285      */
286     public static void addPolicyDefinition(ServiceTemplate serviceTemplate, String policyId,
287                                            PolicyDefinition policyDefinition) {
288         if (serviceTemplate == null) {
289             throw new CoreException(
290                     new InvalidAddActionNullEntityErrorBuilder("Policy Definition", SERVICE_TEMPLATE).build());
291         }
292         TopologyTemplate topologyTemplate = serviceTemplate.getTopology_template();
293         if (Objects.isNull(topologyTemplate)) {
294             topologyTemplate = new TopologyTemplate();
295             serviceTemplate.setTopology_template(topologyTemplate);
296         }
297         if (topologyTemplate.getPolicies() == null) {
298             topologyTemplate.setPolicies(new HashMap<>());
299         }
300         topologyTemplate.getPolicies().put(policyId, policyDefinition);
301     }
302
303     /**
304      * Add node type.
305      *
306      * @param serviceTemplate the service template
307      * @param nodeTypeId      the node type id
308      * @param nodeType        the node type
309      */
310     public static void addNodeType(ServiceTemplate serviceTemplate, String nodeTypeId, NodeType nodeType) {
311         if (serviceTemplate == null) {
312             throw new CoreException(new InvalidAddActionNullEntityErrorBuilder(NODE_TYPE, SERVICE_TEMPLATE).build());
313         }
314         if (serviceTemplate.getNode_types() == null) {
315             serviceTemplate.setNode_types(new HashMap<>());
316         }
317         serviceTemplate.getNode_types().put(nodeTypeId, nodeType);
318     }
319
320     /**
321      * Add relationship template.
322      *
323      * @param serviceTemplate        the service template
324      * @param relationshipTemplateId the relationship template id
325      * @param relationshipTemplate   the relationship template
326      */
327     public static void addRelationshipTemplate(ServiceTemplate serviceTemplate, String relationshipTemplateId,
328                                                RelationshipTemplate relationshipTemplate) {
329         if (serviceTemplate == null) {
330             throw new CoreException(
331                     new InvalidAddActionNullEntityErrorBuilder("Relationship Template", SERVICE_TEMPLATE).build());
332         }
333         if (serviceTemplate.getTopology_template() == null) {
334             serviceTemplate.setTopology_template(new TopologyTemplate());
335         }
336         if (serviceTemplate.getTopology_template().getRelationship_templates() == null) {
337             serviceTemplate.getTopology_template().setRelationship_templates(new HashMap<>());
338         }
339         serviceTemplate.getTopology_template().getRelationship_templates()
340                 .put(relationshipTemplateId, relationshipTemplate);
341     }
342
343     /**
344      * Add requirement assignment.
345      *
346      * @param nodeTemplate          the node template
347      * @param requirementId         the requirement id
348      * @param requirementAssignment the requirement assignment
349      */
350     public static void addRequirementAssignment(NodeTemplate nodeTemplate, String requirementId,
351                                                 RequirementAssignment requirementAssignment) {
352         if (nodeTemplate == null) {
353             throw new CoreException(
354                     new InvalidAddActionNullEntityErrorBuilder("Requirement Assignment",
355                             "Node Template").build());
356         }
357         if (requirementAssignment.getNode() == null) {
358             throw new CoreException(new InvalidRequirementAssignmentErrorBuilder(requirementId).build());
359         }
360
361         if (nodeTemplate.getRequirements() == null) {
362             nodeTemplate.setRequirements(new ArrayList<>());
363         }
364         Map<String, RequirementAssignment> requirement = new HashMap<>();
365         requirement.put(requirementId, requirementAssignment);
366         nodeTemplate.getRequirements().add(requirement);
367     }
368
369     /**
370      * Creates a new requirement assignment object for attachment requirement.
371      *
372      * @param node the node
373      * @return the attachment requirement assignment object
374      */
375     public static RequirementAssignment createAttachmentRequirementAssignment(String node) {
376         RequirementAssignment requirement = new RequirementAssignment();
377         requirement.setCapability(ToscaCapabilityType.NATIVE_ATTACHMENT);
378         requirement.setNode(node);
379         requirement.setRelationship(ToscaRelationshipType.ATTACHES_TO);
380         return requirement;
381     }
382
383     /**
384      * Gets node template.
385      *
386      * @param serviceTemplate the service template
387      * @param nodeTemplateId  the node template id
388      * @return the node template
389      */
390     public static NodeTemplate getNodeTemplate(ServiceTemplate serviceTemplate, String nodeTemplateId) {
391         if (serviceTemplate == null || serviceTemplate.getTopology_template() == null
392                 || serviceTemplate.getTopology_template().getNode_templates() == null) {
393             return null;
394         }
395         return serviceTemplate.getTopology_template().getNode_templates().get(nodeTemplateId);
396     }
397
398     /**
399      * Gets node type.
400      *
401      * @param serviceTemplate the service template
402      * @param nodeTypeId      the node type id
403      * @return the node type
404      */
405     public static NodeType getNodeType(ServiceTemplate serviceTemplate, String nodeTypeId) {
406         if (serviceTemplate == null || serviceTemplate.getNode_types() == null) {
407             return null;
408         }
409         return serviceTemplate.getNode_types().get(nodeTypeId);
410     }
411
412     /**
413      * Gets requirement definition.
414      *
415      * @param nodeType                the node type
416      * @param requirementDefinitionId the requirement definition id
417      * @return the requirement definition
418      */
419     public static Optional<RequirementDefinition> getRequirementDefinition(NodeType nodeType,
420                                                                            String requirementDefinitionId) {
421         if (nodeType == null || nodeType.getRequirements() == null || requirementDefinitionId == null) {
422             return Optional.empty();
423         }
424         return getRequirementDefinition(nodeType.getRequirements(), requirementDefinitionId);
425     }
426
427     /**
428      * get requirement definition from requirement definition list by req key.
429      *
430      * @param requirementsDefinitionList requirement definition list
431      * @param requirementKey             requirement key
432      */
433     public static Optional<RequirementDefinition> getRequirementDefinition(
434             List<Map<String, RequirementDefinition>> requirementsDefinitionList, String requirementKey) {
435         if (CollectionUtils.isEmpty(requirementsDefinitionList)) {
436             return Optional.empty();
437         }
438
439         ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
440         for (Map<String, RequirementDefinition> requirementMap : requirementsDefinitionList) {
441             if (requirementMap.containsKey(requirementKey)) {
442                 RequirementDefinition requirementDefinition = toscaExtensionYamlUtil.yamlToObject(
443                         toscaExtensionYamlUtil.objectToYaml(requirementMap.get(requirementKey)),
444                         RequirementDefinition.class);
445                 return Optional.of(requirementDefinition);
446             }
447         }
448         return Optional.empty();
449     }
450
451     /**
452      * Gets capability definition.
453      *
454      * @param nodeType               the node type
455      * @param capabilityDefinitionId the capability definition id
456      * @return the capability definition
457      */
458     public static Optional<CapabilityDefinition> getCapabilityDefinition(NodeType nodeType,
459                                                                          String capabilityDefinitionId) {
460         if (nodeType == null || nodeType.getCapabilities() == null || capabilityDefinitionId == null) {
461             return Optional.empty();
462         }
463         return Optional.ofNullable(nodeType.getCapabilities().get(capabilityDefinitionId));
464     }
465
466     /**
467      * Add group definition to topology template.
468      *
469      * @param serviceTemplate the service template
470      * @param groupName       the group name
471      * @param group           the group
472      */
473     public static void addGroupDefinitionToTopologyTemplate(ServiceTemplate serviceTemplate, String groupName,
474                                                             GroupDefinition group) {
475         if (serviceTemplate == null) {
476             throw new CoreException(
477                     new InvalidAddActionNullEntityErrorBuilder("Group Definition", SERVICE_TEMPLATE).build());
478         }
479
480         TopologyTemplate topologyTemplate = serviceTemplate.getTopology_template();
481         if (Objects.isNull(topologyTemplate)) {
482             topologyTemplate = new TopologyTemplate();
483             serviceTemplate.setTopology_template(topologyTemplate);
484         }
485         if (topologyTemplate.getGroups() == null) {
486             topologyTemplate.setGroups(new HashMap<>());
487         }
488         if (serviceTemplate.getTopology_template().getGroups() == null) {
489             Map<String, GroupDefinition> groups = new HashMap<>();
490             serviceTemplate.getTopology_template().setGroups(groups);
491         }
492
493         serviceTemplate.getTopology_template().getGroups().put(groupName, group);
494     }
495
496     /**
497      * Adds a group member to an existing group in the service template.
498      *
499      * @param serviceTemplate the service template
500      * @param groupName       the group name
501      * @param groupMemberId   the group member id
502      */
503     public static void addGroupMember(ServiceTemplate serviceTemplate, String groupName, String groupMemberId) {
504         TopologyTemplate topologyTemplate = serviceTemplate.getTopology_template();
505         if (Objects.isNull(topologyTemplate) || topologyTemplate.getGroups() == null
506                 || topologyTemplate.getGroups().get(groupName) == null) {
507             return;
508         }
509
510         GroupDefinition groupDefinition = topologyTemplate.getGroups().get(groupName);
511         if (CollectionUtils.isEmpty(groupDefinition.getMembers())) {
512             groupDefinition.setMembers(new ArrayList<>());
513         }
514
515         if (!groupDefinition.getMembers().contains(groupMemberId)) {
516             groupDefinition.getMembers().add(groupMemberId);
517         }
518     }
519
520     /**
521      * Create parameter definition property definition.
522      *
523      * @param type        the type
524      * @param description the description
525      * @param required    the required
526      * @param constraints the constraints
527      * @param entrySchema the entry schema
528      * @param defaultVal  the default val
529      * @return the property definition
530      */
531     public static ParameterDefinition createParameterDefinition(String type, String description, Boolean required,
532                                                                 List<Constraint> constraints, EntrySchema entrySchema,
533                                                                 Object defaultVal) {
534         ParameterDefinition paramDef = new ParameterDefinition();
535         paramDef.setType(type);
536         paramDef.setDescription(description);
537         paramDef.setRequired(required);
538         paramDef.setConstraints(constraints);
539         paramDef.setEntry_schema(entrySchema == null ? null : entrySchema.clone());
540         paramDef.set_default(defaultVal);
541         return paramDef;
542     }
543
544     /**
545      * Create requirement requirement definition.
546      *
547      * @param capability   the capability
548      * @param node         the node
549      * @param relationship the relationship
550      * @param occurrences  the occurrences
551      * @return the requirement definition
552      */
553     public static RequirementDefinition createRequirement(String capability, String node, String relationship,
554                                                           Object[] occurrences) {
555         RequirementDefinition requirementDefinition = new RequirementDefinition();
556         requirementDefinition.setCapability(capability);
557         requirementDefinition.setNode(node);
558         requirementDefinition.setRelationship(relationship);
559         if (occurrences != null) {
560             requirementDefinition.setOccurrences(occurrences);
561         }
562         return requirementDefinition;
563     }
564
565     /**
566      * Create entry schema entry schema.
567      *
568      * @param type        the type
569      * @param description the description
570      * @param constraints the constraints
571      * @return the entry schema
572      */
573     public static EntrySchema createEntrySchema(String type, String description, List<Constraint> constraints) {
574         if (Objects.isNull(type) && Objects.isNull(description) && CollectionUtils.isEmpty(constraints)) {
575             return null;
576         }
577
578         EntrySchema entrySchema = new EntrySchema();
579         entrySchema.setType(type);
580         entrySchema.setDescription(description);
581         entrySchema.setConstraints(constraints);
582         return entrySchema;
583     }
584
585     /**
586      * Create get input property value from list parameter map.
587      *
588      * @param inputPropertyListName the input property list name
589      * @param indexInTheList        the index in the list
590      * @param nestedPropertyName    the nested property name
591      * @return the map
592      */
593     public static Map createGetInputPropertyValueFromListParameter(String inputPropertyListName, int indexInTheList,
594                                                                    String... nestedPropertyName) {
595         List<Object> propertyList = new ArrayList<>();
596         propertyList.add(inputPropertyListName);
597         propertyList.add(indexInTheList);
598         if (nestedPropertyName != null) {
599             Collections.addAll(propertyList, nestedPropertyName);
600         }
601         Map<String, Object> getInputProperty = new HashMap<>();
602         getInputProperty.put(ToscaFunctions.GET_INPUT.getFunctionName(), propertyList);
603         return getInputProperty;
604     }
605
606     /**
607      * Convert property def to parameter def parameter definition ext.
608      *
609      * @param propertyDefinition the property definition
610      * @return the parameter definition ext
611      */
612     public static ParameterDefinitionExt convertPropertyDefToParameterDef(PropertyDefinition propertyDefinition) {
613         if (propertyDefinition == null) {
614             return null;
615         }
616
617         ParameterDefinitionExt parameterDefinition = new ParameterDefinitionExt();
618         parameterDefinition.setType(propertyDefinition.getType());
619         parameterDefinition.setDescription(propertyDefinition.getDescription());
620         parameterDefinition.setRequired(propertyDefinition.getRequired());
621         parameterDefinition.set_default(propertyDefinition.get_default());
622         parameterDefinition.setStatus(propertyDefinition.getStatus());
623         parameterDefinition.setConstraints(propertyDefinition.getConstraints());
624         parameterDefinition.setEntry_schema(Objects.isNull(propertyDefinition.getEntry_schema()) ? null :
625                 propertyDefinition.getEntry_schema().clone());
626         parameterDefinition.setHidden(false);
627         parameterDefinition.setImmutable(false);
628         return parameterDefinition;
629     }
630
631     /**
632      * Convert attribute def to parameter def parameter definition ext.
633      *
634      * @param attributeDefinition the attribute definition
635      * @param outputValue         the output value
636      * @return the parameter definition ext
637      */
638     public static ParameterDefinitionExt convertAttributeDefToParameterDef(AttributeDefinition attributeDefinition,
639                                                                            Map<String, List> outputValue) {
640         if (attributeDefinition == null) {
641             return null;
642         }
643         ParameterDefinitionExt parameterDefinition = new ParameterDefinitionExt();
644         parameterDefinition.setDescription(attributeDefinition.getDescription());
645         parameterDefinition.setValue(outputValue);
646         return parameterDefinition;
647     }
648
649     public static boolean isNodeTemplate(String entryId, ServiceTemplate serviceTemplate) {
650         return serviceTemplate.getTopology_template().getNode_templates() != null
651                 && serviceTemplate.getTopology_template().getNode_templates().get(entryId) != null;
652     }
653
654     /**
655      * Add Input parameter.
656      *
657      * @param serviceTemplate       the service template
658      * @param parameterDefinitionId the parameter definition id
659      * @param parameterDefinition   the parameter definition
660      */
661     public static void addInputParameterToTopologyTemplate(ServiceTemplate serviceTemplate,
662                                                            String parameterDefinitionId,
663                                                            ParameterDefinition parameterDefinition) {
664         if (Objects.isNull(serviceTemplate)) {
665             throw new CoreException(
666                     new InvalidAddActionNullEntityErrorBuilder("Topology Template Input Parameter", SERVICE_TEMPLATE)
667                             .build());
668         }
669         TopologyTemplate topologyTemplate = serviceTemplate.getTopology_template();
670         if (Objects.isNull(topologyTemplate)) {
671             topologyTemplate = new TopologyTemplate();
672             serviceTemplate.setTopology_template(topologyTemplate);
673         }
674         if (topologyTemplate.getInputs() == null) {
675             topologyTemplate.setInputs(new HashMap<>());
676         }
677         topologyTemplate.getInputs().put(parameterDefinitionId, parameterDefinition);
678     }
679
680     /**
681      * Add Output parameter.
682      *
683      * @param serviceTemplate       the service template
684      * @param parameterDefinitionId the parameter definition id
685      * @param parameterDefinition   the parameter definition
686      */
687     public static void addOutputParameterToTopologyTemplate(ServiceTemplate serviceTemplate,
688                                                             String parameterDefinitionId,
689                                                             ParameterDefinition parameterDefinition) {
690         if (Objects.isNull(serviceTemplate)) {
691             throw new CoreException(
692                     new InvalidAddActionNullEntityErrorBuilder("Topology Template Output Parameter", SERVICE_TEMPLATE)
693                             .build());
694         }
695         TopologyTemplate topologyTemplate = serviceTemplate.getTopology_template();
696         if (Objects.isNull(topologyTemplate)) {
697             topologyTemplate = new TopologyTemplate();
698             serviceTemplate.setTopology_template(topologyTemplate);
699         }
700         if (topologyTemplate.getOutputs() == null) {
701             topologyTemplate.setOutputs(new HashMap<>());
702         }
703         topologyTemplate.getOutputs().put(parameterDefinitionId, parameterDefinition);
704     }
705
706     /**
707      * Add requirement def to requirement def list.
708      *
709      * @param requirementList requirement list
710      * @param requirementDef  added requirement def
711      */
712     public static void addRequirementToList(List<Map<String, RequirementDefinition>> requirementList,
713                                             Map<String, RequirementDefinition> requirementDef) {
714         if (requirementDef == null) {
715             return;
716         }
717         if (requirementList == null) {
718             requirementList = new ArrayList<>();
719         }
720
721         for (Map.Entry<String, RequirementDefinition> entry : requirementDef.entrySet()) {
722             CommonMethods.mergeEntryInList(entry.getKey(), entry.getValue(), requirementList);
723         }
724     }
725
726     /**
727      * get node template requirement.
728      *
729      * @param nodeTemplate node template
730      */
731     public static Map<String, RequirementAssignment> getNodeTemplateRequirements(NodeTemplate nodeTemplate) {
732         if (Objects.isNull(nodeTemplate)) {
733             return null;
734         }
735         List<Map<String, RequirementAssignment>> templateRequirements = nodeTemplate.getRequirements();
736
737         Map<String, RequirementAssignment> nodeTemplateRequirementsAssignment = new HashMap<>();
738         if (CollectionUtils.isEmpty(templateRequirements)) {
739             return nodeTemplateRequirementsAssignment;
740         }
741         ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
742         for (Map<String, RequirementAssignment> requirementAssignmentMap : templateRequirements) {
743             for (Map.Entry<String, RequirementAssignment> requirementEntry : requirementAssignmentMap.entrySet()) {
744                 RequirementAssignment requirementAssignment = (toscaExtensionYamlUtil.yamlToObject(
745                         toscaExtensionYamlUtil.objectToYaml(requirementEntry.getValue()), RequirementAssignment.class));
746                 nodeTemplateRequirementsAssignment.put(requirementEntry.getKey(), requirementAssignment);
747             }
748         }
749         return nodeTemplateRequirementsAssignment;
750     }
751
752     /**
753      * Gets the list of requirements for the node template.
754      *
755      * @param nodeTemplate the node template
756      * @return the node template requirement list and null if the node has no requirements
757      */
758     public static List<Map<String, RequirementAssignment>> getNodeTemplateRequirementList(NodeTemplate nodeTemplate) {
759         ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
760         //Creating concrete objects
761         List<Map<String, RequirementAssignment>> requirements = nodeTemplate.getRequirements();
762         List<Map<String, RequirementAssignment>> concreteRequirementList = null;
763         if (requirements != null) {
764             concreteRequirementList = new ArrayList<>();
765             ListIterator<Map<String, RequirementAssignment>> reqListIterator = requirements.listIterator();
766             while (reqListIterator.hasNext()) {
767                 Map<String, RequirementAssignment> requirement = reqListIterator.next();
768                 Map<String, RequirementAssignment> concreteRequirement = new HashMap<>();
769                 for (Map.Entry<String, RequirementAssignment> reqEntry : requirement.entrySet()) {
770                     RequirementAssignment requirementAssignment = (toscaExtensionYamlUtil.yamlToObject(
771                             toscaExtensionYamlUtil.objectToYaml(reqEntry.getValue()), RequirementAssignment.class));
772                     concreteRequirement.put(reqEntry.getKey(), requirementAssignment);
773                     concreteRequirementList.add(concreteRequirement);
774                     reqListIterator.remove();
775                 }
776             }
777             requirements.clear();
778             requirements.addAll(concreteRequirementList);
779             nodeTemplate.setRequirements(requirements);
780         }
781         return concreteRequirementList;
782     }
783
784     /**
785      * get requirement assignment from requirement assignment list by req key.
786      *
787      * @param requirementsAssignmentList requirement definition list
788      * @param requirementKey             requirement key
789      */
790     public static Optional<List<RequirementAssignment>> getRequirementAssignment(
791             List<Map<String, RequirementAssignment>> requirementsAssignmentList, String requirementKey) {
792         if (CollectionUtils.isEmpty(requirementsAssignmentList)) {
793             return Optional.empty();
794         }
795
796         List<RequirementAssignment> matchRequirementAssignmentList = new ArrayList<>();
797         for (Map<String, RequirementAssignment> requirementMap : requirementsAssignmentList) {
798             if (requirementMap.containsKey(requirementKey)) {
799                 ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
800                 RequirementAssignment requirementAssignment = (toscaExtensionYamlUtil.yamlToObject(
801                         toscaExtensionYamlUtil.objectToYaml(requirementMap.get(requirementKey)),
802                         RequirementAssignment.class));
803                 matchRequirementAssignmentList.add(requirementAssignment);
804             }
805         }
806         if (CollectionUtils.isEmpty(matchRequirementAssignmentList)) {
807             return Optional.empty();
808         }
809         return Optional.of(matchRequirementAssignmentList);
810     }
811
812     /**
813      * remove requirement definition from requirement definition list by req key.
814      *
815      * @param requirementsDefinitionList requirement definition list
816      * @param requirementKey             requirement key
817      */
818     public static void removeRequirementsDefinition(List<Map<String, RequirementDefinition>> requirementsDefinitionList,
819                                                     String requirementKey) {
820         if (requirementsDefinitionList == null) {
821             return;
822         }
823
824         List<Map<String, RequirementDefinition>> mapToBeRemoved = new ArrayList<>();
825         for (Map<String, RequirementDefinition> reqMap : requirementsDefinitionList) {
826             reqMap.remove(requirementKey);
827             if (reqMap.isEmpty()) {
828                 mapToBeRemoved.add(reqMap);
829             }
830         }
831         for (Map<String, RequirementDefinition> removeMap : mapToBeRemoved) {
832             requirementsDefinitionList.remove(removeMap);
833         }
834     }
835
836     /**
837      * remove requirement assignment from requirement definition list by req key.
838      *
839      * @param requirementsAssignmentList requirement Assignment list
840      * @param requirementKey             requirement key
841      */
842     public static void removeRequirementsAssignment(List<Map<String, RequirementAssignment>> requirementsAssignmentList,
843                                                     String requirementKey) {
844         if (requirementsAssignmentList == null) {
845             return;
846         }
847
848         List<Map<String, RequirementAssignment>> mapToBeRemoved = new ArrayList<>();
849         for (Map<String, RequirementAssignment> reqMap : requirementsAssignmentList) {
850             reqMap.remove(requirementKey);
851             if (reqMap.isEmpty()) {
852                 mapToBeRemoved.add(reqMap);
853             }
854         }
855         for (Map<String, RequirementAssignment> removeMap : mapToBeRemoved) {
856             requirementsAssignmentList.remove(removeMap);
857         }
858     }
859
860
861     /**
862      * Remove requirement assignment.
863      *
864      * @param nodeTemplate                     the node template
865      * @param requirementKey                   the requirement key
866      * @param requirementAssignmentToBeDeleted the requirement assignment to be deleted
867      */
868     public static void removeRequirementAssignment(NodeTemplate nodeTemplate, String requirementKey,
869                                                    RequirementAssignment requirementAssignmentToBeDeleted) {
870         ToscaAnalyzerService toscaAnalyzerService = new ToscaAnalyzerServiceImpl();
871         List<Map<String, RequirementAssignment>> nodeTemplateRequirements = nodeTemplate.getRequirements();
872         if (nodeTemplateRequirements == null) {
873             return;
874         }
875
876         ListIterator<Map<String, RequirementAssignment>> iter = nodeTemplateRequirements.listIterator();
877         while (iter.hasNext()) {
878             Map<String, RequirementAssignment> reqMap = iter.next();
879             RequirementAssignment requirementAssignment = reqMap.get(requirementKey);
880             if (requirementAssignment != null) {
881                 boolean isDesiredRequirementAssignment = toscaAnalyzerService
882                         .isDesiredRequirementAssignment(requirementAssignment,
883                                 requirementAssignmentToBeDeleted
884                                         .getCapability(),
885                                 requirementAssignmentToBeDeleted.getNode(),
886                                 requirementAssignmentToBeDeleted
887                                         .getRelationship());
888                 if (isDesiredRequirementAssignment) {
889                     iter.remove();
890                 }
891             }
892         }
893     }
894
895     /**
896      * Return the suffix of the input namespace For an exampale - for abc.sdf.vsrx, return vsrx
897      *
898      * @param namespace namespace
899      * @return String namespace suffix
900      */
901     public static String getNamespaceSuffix(String namespace) {
902         if (namespace == null) {
903             return null;
904         }
905         String delimiterChar = ".";
906         if (namespace.contains(delimiterChar)) {
907             return namespace.substring(namespace.lastIndexOf(delimiterChar) + 1);
908         }
909         return namespace;
910     }
911
912     /**
913      * Return true if the input import exist in the input imports list.
914      *
915      * @param imports  namespace
916      * @param importId namespace
917      * @return true if exist, false if not exist
918      */
919     public static boolean isImportAddedToServiceTemplate(List<Map<String, Import>> imports, String importId) {
920         for (Map<String, Import> anImport : imports) {
921             if (anImport.containsKey(importId)) {
922                 return true;
923             }
924         }
925         return false;
926     }
927
928     /**
929      * Get output parameter according to the input outputParameterId.
930      *
931      * @param serviceTemplate   service template
932      * @param outputParameterId output parameter id
933      * @return ParameterDefinition - output parameter
934      */
935     public static ParameterDefinition getOuputParameter(ServiceTemplate serviceTemplate, String outputParameterId) {
936         if (serviceTemplate == null || serviceTemplate.getTopology_template() == null
937                 || serviceTemplate.getTopology_template().getOutputs() == null) {
938             return null;
939         }
940         return serviceTemplate.getTopology_template().getOutputs().get(outputParameterId);
941     }
942
943     /**
944      * Gets input parameters in a service template.
945      *
946      * @param serviceTemplate the service template
947      * @return the input parameters
948      */
949     public static Map<String, ParameterDefinition> getInputParameters(ServiceTemplate serviceTemplate) {
950         if (serviceTemplate == null || serviceTemplate.getTopology_template() == null
951                 || serviceTemplate.getTopology_template().getInputs() == null) {
952             return null;
953         }
954         return serviceTemplate.getTopology_template().getInputs();
955     }
956
957     /**
958      * Gets relationship templates in a service template.
959      *
960      * @param serviceTemplate the service template
961      * @return the relationship template
962      */
963     public static Map<String, RelationshipTemplate> getRelationshipTemplates(ServiceTemplate serviceTemplate) {
964         if (serviceTemplate == null || serviceTemplate.getTopology_template() == null
965                 || serviceTemplate.getTopology_template().getRelationship_templates() == null) {
966             return null;
967         }
968         return serviceTemplate.getTopology_template().getRelationship_templates();
969     }
970
971     /**
972      * Get property value according to the input propertyId.
973      *
974      * @param nodeTemplate node template
975      * @param propertyId   property id
976      * @return Object        property Value
977      */
978     public static Object getPropertyValue(NodeTemplate nodeTemplate, String propertyId) {
979         if (nodeTemplate == null || nodeTemplate.getProperties() == null) {
980             return null;
981         }
982         return nodeTemplate.getProperties().get(propertyId);
983     }
984
985     /**
986      * Get node template properties according to the input node template id.
987      *
988      * @param serviceTemplate service template
989      * @param nodeTemplateId  node template id
990      * @return node template properties
991      */
992     public static Map<String, Object> getNodeTemplateProperties(ServiceTemplate serviceTemplate,
993                                                                 String nodeTemplateId) {
994         if (serviceTemplate == null || serviceTemplate.getTopology_template() == null
995                 || serviceTemplate.getTopology_template().getNode_templates() == null
996                 || serviceTemplate.getTopology_template().getNode_templates().get(nodeTemplateId) == null) {
997             return null;
998         }
999         return serviceTemplate.getTopology_template().getNode_templates().get(nodeTemplateId).getProperties();
1000     }
1001
1002     /**
1003      * Adds a property to a node template.
1004      *
1005      * @param nodeTemplate  the node template
1006      * @param propertyKey   the property key
1007      * @param propertyValue the property value
1008      */
1009     public static void addNodeTemplateProperty(NodeTemplate nodeTemplate, String propertyKey, Object propertyValue) {
1010         if (Objects.isNull(nodeTemplate)) {
1011             return;
1012         }
1013
1014         if (MapUtils.isEmpty(nodeTemplate.getProperties())) {
1015             nodeTemplate.setProperties(new HashMap<>());
1016         }
1017
1018         nodeTemplate.getProperties().put(propertyKey, propertyValue);
1019     }
1020
1021     /**
1022      * Gets substitution mappings in a service template.
1023      *
1024      * @param serviceTemplate the service template
1025      * @return the substitution mappings
1026      */
1027     public static SubstitutionMapping getSubstitutionMappings(ServiceTemplate serviceTemplate) {
1028         if (serviceTemplate == null || serviceTemplate.getTopology_template() == null
1029                 || serviceTemplate.getTopology_template().getSubstitution_mappings() == null) {
1030             return null;
1031         }
1032         return serviceTemplate.getTopology_template().getSubstitution_mappings();
1033     }
1034
1035
1036     /**
1037      * Compare two requirement assignment objects for equality.
1038      *
1039      * @param first  the first requirement assignment object
1040      * @param second the second  requirement assignment object
1041      * @return true if objects are equal and false otherwise
1042      */
1043     public static boolean compareRequirementAssignment(RequirementAssignment first, RequirementAssignment second) {
1044         return (first.getCapability().equals(second.getCapability()) && first.getNode().equals(second.getNode())
1045                 && first.getRelationship().equals(second.getRelationship()));
1046     }
1047
1048     /**
1049      * Gets a deep copy clone of the input object.
1050      *
1051      * @param <T>         the type parameter
1052      * @param objectValue the object value
1053      * @param clazz       the clazz
1054      * @return the cloned object
1055      */
1056     public static <T> Object getClonedObject(Object objectValue, Class<T> clazz) {
1057         YamlUtil yamlUtil = new ToscaExtensionYamlUtil();
1058         Object clonedObjectValue;
1059         String objectToYaml = yamlUtil.objectToYaml(objectValue);
1060         clonedObjectValue = yamlUtil.yamlToObject(objectToYaml, clazz);
1061         return clonedObjectValue;
1062     }
1063
1064     /**
1065      * Gets a deep copy clone of the input object.
1066      *
1067      * @param obj the object to be cloned
1068      * @return the cloned object
1069      */
1070     public static Object getClonedObject(Object obj) {
1071         Object clonedObjectValue;
1072         try {
1073             //Serialize object
1074             ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
1075             ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
1076             objectOutputStream.writeObject(obj);
1077             //Deserialize object
1078             ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
1079             ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
1080             clonedObjectValue = objectInputStream.readObject();
1081         } catch (NotSerializableException ex) {
1082             LOGGER.debug(ex.getMessage(), ex);
1083             return getClonedObject(obj, obj.getClass());
1084         } catch (IOException | ClassNotFoundException ex) {
1085             LOGGER.debug(ex.getMessage(), ex);
1086             return null;
1087         }
1088         return clonedObjectValue;
1089     }
1090
1091     /**
1092      * Add substitution filtering property.
1093      *
1094      * @param templateName the substitution service template name
1095      * @param nodeTemplate the node template
1096      * @param count        the count
1097      */
1098     public static void addSubstitutionFilteringProperty(String templateName, NodeTemplate nodeTemplate, int count) {
1099         Map<String, Object> serviceTemplateFilterPropertyValue = new HashMap<>();
1100         Map<String, Object> properties = nodeTemplate.getProperties();
1101         serviceTemplateFilterPropertyValue.put(ToscaConstants.SUBSTITUTE_SERVICE_TEMPLATE_PROPERTY_NAME, templateName);
1102         serviceTemplateFilterPropertyValue.put(ToscaConstants.COUNT_PROPERTY_NAME, count);
1103         properties.put(ToscaConstants.SERVICE_TEMPLATE_FILTER_PROPERTY_NAME, serviceTemplateFilterPropertyValue);
1104         nodeTemplate.setProperties(properties);
1105     }
1106
1107     /**
1108      * Adding binding requirement from port node template to compute node template.
1109      *
1110      * @param computeNodeTemplateId compute node template id
1111      * @param portNodeTemplate      port node template
1112      */
1113     public static void addBindingReqFromPortToCompute(String computeNodeTemplateId, NodeTemplate portNodeTemplate) {
1114         RequirementAssignment requirementAssignment = new RequirementAssignment();
1115         requirementAssignment.setCapability(ToscaCapabilityType.NATIVE_NETWORK_BINDABLE);
1116         requirementAssignment.setRelationship(ToscaRelationshipType.NATIVE_NETWORK_BINDS_TO);
1117         requirementAssignment.setNode(computeNodeTemplateId);
1118         addRequirementAssignment(portNodeTemplate, ToscaConstants.BINDING_REQUIREMENT_ID, requirementAssignment);
1119     }
1120
1121     /**
1122      * Create substitution template substitution mapping.
1123      *
1124      * @param nodeTypeKey          the node type key
1125      * @param substitutionNodeType the substitution node type
1126      * @param mapping              the mapping
1127      * @return the substitution mapping
1128      */
1129     public static SubstitutionMapping createSubstitutionTemplateSubMapping(String nodeTypeKey,
1130                                                                            NodeType substitutionNodeType,
1131                                                                            Map<String, Map<String, List<String>>> mapping) {
1132         SubstitutionMapping substitutionMapping = new SubstitutionMapping();
1133         substitutionMapping.setNode_type(nodeTypeKey);
1134         substitutionMapping.setCapabilities(manageCapabilityMapping(substitutionNodeType.getCapabilities(),
1135                 mapping.get(ToscaConstants.CAPABILITY)));
1136         substitutionMapping.setRequirements(
1137                 manageRequirementMapping(substitutionNodeType.getRequirements(), mapping.get("requirement")));
1138         return substitutionMapping;
1139     }
1140
1141     /**
1142      * Add node template capability.
1143      *
1144      * @param nodeTemplate         the node template
1145      * @param capabilityId         the capability id
1146      * @param capabilityProperties the capability properties
1147      * @param capabilityAttributes the capability attributes
1148      */
1149     public static void addNodeTemplateCapability(NodeTemplate nodeTemplate, String capabilityId,
1150                                                  Map<String, Object> capabilityProperties,
1151                                                  Map<String, Object> capabilityAttributes) {
1152         Map<String, CapabilityAssignment> capabilities = nodeTemplate.getCapabilities();
1153         if (Objects.isNull(capabilities)) {
1154             capabilities = new HashMap<>();
1155         }
1156         CapabilityAssignment capabilityAssignment = new CapabilityAssignment();
1157         capabilityAssignment.setProperties(capabilityProperties);
1158         capabilityAssignment.setAttributes(capabilityAttributes);
1159         capabilities.put(capabilityId, capabilityAssignment);
1160         nodeTemplate.setCapabilities(capabilities);
1161     }
1162
1163     private static Map<String, List<String>> manageRequirementMapping(
1164             List<Map<String, RequirementDefinition>> requirementList,
1165             Map<String, List<String>> requirementSubstitutionMapping) {
1166         if (requirementList == null) {
1167             return null;
1168         }
1169         Map<String, List<String>> requirementMapping = new HashMap<>();
1170         String requirementKey;
1171         List<String> requirementMap;
1172         for (Map<String, RequirementDefinition> requirementDefMap : requirementList) {
1173             for (Map.Entry<String, RequirementDefinition> entry : requirementDefMap.entrySet()) {
1174                 requirementKey = entry.getKey();
1175                 requirementMap = requirementSubstitutionMapping.get(requirementKey);
1176                 requirementMapping.put(requirementKey, requirementMap);
1177             }
1178         }
1179         return requirementMapping;
1180     }
1181
1182     private static Map<String, List<String>> manageCapabilityMapping(Map<String, CapabilityDefinition> capabilities,
1183                                                                      Map<String, List<String>> capabilitySubstitutionMapping) {
1184         if (capabilities == null) {
1185             return null;
1186         }
1187
1188         Map<String, List<String>> capabilityMapping = new HashMap<>();
1189         String capabilityKey;
1190         List<String> capabilityMap;
1191         for (Map.Entry<String, CapabilityDefinition> entry : capabilities.entrySet()) {
1192             capabilityKey = entry.getKey();
1193             capabilityMap = capabilitySubstitutionMapping.get(capabilityKey);
1194             capabilityMapping.put(capabilityKey, capabilityMap);
1195         }
1196         return capabilityMapping;
1197     }
1198
1199     public static void addSubstitutionNodeTypeRequirements(NodeType substitutionNodeType,
1200                                                            List<Map<String, RequirementDefinition>> requirementsList,
1201                                                            String templateName) {
1202         if (CollectionUtils.isEmpty(requirementsList)) {
1203             return;
1204         }
1205         if (substitutionNodeType.getRequirements() == null) {
1206             substitutionNodeType.setRequirements(new ArrayList<>());
1207         }
1208
1209         for (Map<String, RequirementDefinition> requirementDef : requirementsList) {
1210             for (Map.Entry<String, RequirementDefinition> entry : requirementDef.entrySet()) {
1211                 Map<String, RequirementDefinition> requirementMap = new HashMap<>();
1212                 requirementMap.put(entry.getKey() + "_" + templateName, entry.getValue().clone());
1213                 substitutionNodeType.getRequirements().add(requirementMap);
1214             }
1215         }
1216     }
1217
1218     public static boolean isNodeTemplateSectionMissingFromServiceTemplate(ServiceTemplate serviceTemplate) {
1219         return Objects.isNull(serviceTemplate.getTopology_template()) || MapUtils.isEmpty(
1220                 serviceTemplate.getTopology_template().getNode_templates());
1221     }
1222
1223     /**
1224      * Gets relationship template in a service template according to the relationship id.
1225      *
1226      * @param serviceTemplate the service template
1227      * @param relationshipId  the relationship id
1228      * @return the relationship template
1229      */
1230     public static Optional<RelationshipTemplate> getRelationshipTemplate(ServiceTemplate serviceTemplate,
1231                                                                          String relationshipId) {
1232         if (serviceTemplate == null || serviceTemplate.getTopology_template() == null
1233                 || serviceTemplate.getTopology_template().getRelationship_templates() == null
1234                 || serviceTemplate.getTopology_template().getRelationship_templates().get(relationshipId) == null) {
1235             return Optional.empty();
1236         }
1237         return Optional.of(serviceTemplate.getTopology_template().getRelationship_templates().get(relationshipId));
1238     }
1239
1240
1241 }