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