70cae71f1775b0f32bd8386a83a14923f92bfb0e
[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         if (nodeTemplate.getRequirements() == null) {
377             nodeTemplate.setRequirements(new ArrayList<>());
378         }
379         Map<String, RequirementAssignment> requirement = new HashMap<>();
380         requirement.put(requirementId, requirementAssignment);
381         nodeTemplate.getRequirements().add(requirement);
382     }
383
384     /**
385      * Creates a new requirement assignment object for attachment requirement.
386      *
387      * @param node the node
388      * @return the attachment requirement assignment object
389      */
390     public static RequirementAssignment createAttachmentRequirementAssignment(String node) {
391         RequirementAssignment requirement = new RequirementAssignment();
392         requirement.setCapability(ToscaCapabilityType.NATIVE_ATTACHMENT);
393         requirement.setNode(node);
394         requirement.setRelationship(ToscaRelationshipType.ATTACHES_TO);
395         return requirement;
396     }
397
398     /**
399      * Gets node template.
400      *
401      * @param serviceTemplate the service template
402      * @param nodeTemplateId  the node template id
403      * @return the node template
404      */
405     public static NodeTemplate getNodeTemplate(ServiceTemplate serviceTemplate, String nodeTemplateId) {
406         if (serviceTemplate == null || serviceTemplate.getTopology_template() == null
407                 || serviceTemplate.getTopology_template().getNode_templates() == null) {
408             return null;
409         }
410         return serviceTemplate.getTopology_template().getNode_templates().get(nodeTemplateId);
411     }
412
413     /**
414      * Gets node type.
415      *
416      * @param serviceTemplate the service template
417      * @param nodeTypeId      the node type id
418      * @return the node type
419      */
420     public static NodeType getNodeType(ServiceTemplate serviceTemplate, String nodeTypeId) {
421         if (serviceTemplate == null || serviceTemplate.getNode_types() == null) {
422             return null;
423         }
424         return serviceTemplate.getNode_types().get(nodeTypeId);
425     }
426
427     /**
428      * Gets requirement definition.
429      *
430      * @param nodeType                the node type
431      * @param requirementDefinitionId the requirement definition id
432      * @return the requirement definition
433      */
434     public static Optional<RequirementDefinition> getRequirementDefinition(NodeType nodeType,
435                                                                            String requirementDefinitionId) {
436         if (nodeType == null || nodeType.getRequirements() == null || requirementDefinitionId == null) {
437             return Optional.empty();
438         }
439         return getRequirementDefinition(nodeType.getRequirements(), requirementDefinitionId);
440     }
441
442     /**
443      * get requirement definition from requirement definition list by req key.
444      *
445      * @param requirementsDefinitionList requirement definition list
446      * @param requirementKey             requirement key
447      */
448     public static Optional<RequirementDefinition> getRequirementDefinition(
449             List<Map<String, RequirementDefinition>> requirementsDefinitionList, String requirementKey) {
450         if (CollectionUtils.isEmpty(requirementsDefinitionList)) {
451             return Optional.empty();
452         }
453
454         ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
455         for (Map<String, RequirementDefinition> requirementMap : requirementsDefinitionList) {
456             if (requirementMap.containsKey(requirementKey)) {
457                 RequirementDefinition requirementDefinition = toscaExtensionYamlUtil.yamlToObject(
458                         toscaExtensionYamlUtil.objectToYaml(requirementMap.get(requirementKey)),
459                         RequirementDefinition.class);
460                 return Optional.of(requirementDefinition);
461             }
462         }
463         return Optional.empty();
464     }
465
466     /**
467      * Gets capability definition.
468      *
469      * @param nodeType               the node type
470      * @param capabilityDefinitionId the capability definition id
471      * @return the capability definition
472      */
473     public static Optional<CapabilityDefinition> getCapabilityDefinition(NodeType nodeType,
474                                                                          String capabilityDefinitionId) {
475         if (nodeType == null || nodeType.getCapabilities() == null || capabilityDefinitionId == null) {
476             return Optional.empty();
477         }
478         return Optional.ofNullable(nodeType.getCapabilities().get(capabilityDefinitionId));
479     }
480
481     /**
482      * Add group definition to topology template.
483      *
484      * @param serviceTemplate the service template
485      * @param groupName       the group name
486      * @param group           the group
487      */
488     public static void addGroupDefinitionToTopologyTemplate(ServiceTemplate serviceTemplate, String groupName,
489                                                             GroupDefinition group) {
490         if (serviceTemplate == null) {
491             throw new CoreException(
492                     new InvalidAddActionNullEntityErrorBuilder("Group Definition", SERVICE_TEMPLATE).build());
493         }
494
495         TopologyTemplate topologyTemplate = serviceTemplate.getTopology_template();
496         if (Objects.isNull(topologyTemplate)) {
497             topologyTemplate = new TopologyTemplate();
498             serviceTemplate.setTopology_template(topologyTemplate);
499         }
500         if (topologyTemplate.getGroups() == null) {
501             topologyTemplate.setGroups(new HashMap<>());
502         }
503         if (serviceTemplate.getTopology_template().getGroups() == null) {
504             Map<String, GroupDefinition> groups = new HashMap<>();
505             serviceTemplate.getTopology_template().setGroups(groups);
506         }
507
508         serviceTemplate.getTopology_template().getGroups().put(groupName, group);
509     }
510
511     /**
512      * Adds a group member to an existing group in the service template.
513      *
514      * @param serviceTemplate the service template
515      * @param groupName       the group name
516      * @param groupMemberId   the group member id
517      */
518     public static void addGroupMember(ServiceTemplate serviceTemplate, String groupName, String groupMemberId) {
519         TopologyTemplate topologyTemplate = serviceTemplate.getTopology_template();
520         if (Objects.isNull(topologyTemplate) || topologyTemplate.getGroups() == null
521                 || topologyTemplate.getGroups().get(groupName) == null) {
522             return;
523         }
524
525         GroupDefinition groupDefinition = topologyTemplate.getGroups().get(groupName);
526         if (CollectionUtils.isEmpty(groupDefinition.getMembers())) {
527             groupDefinition.setMembers(new ArrayList<>());
528         }
529
530         if (!groupDefinition.getMembers().contains(groupMemberId)) {
531             groupDefinition.getMembers().add(groupMemberId);
532         }
533     }
534
535     /**
536      * Create parameter definition property definition.
537      *
538      * @param type        the type
539      * @param description the description
540      * @param required    the required
541      * @param constraints the constraints
542      * @param entrySchema the entry schema
543      * @param defaultVal  the default val
544      * @return the property definition
545      */
546     public static ParameterDefinition createParameterDefinition(String type, String description, Boolean required,
547                                                                 List<Constraint> constraints, EntrySchema entrySchema,
548                                                                 Object defaultVal) {
549         ParameterDefinition paramDef = new ParameterDefinition();
550         paramDef.setType(type);
551         paramDef.setDescription(description);
552         paramDef.setRequired(required);
553         paramDef.setConstraints(constraints);
554         paramDef.setEntry_schema(entrySchema == null ? null : entrySchema.clone());
555         paramDef.set_default(defaultVal);
556         return paramDef;
557     }
558
559     /**
560      * Create requirement requirement definition.
561      *
562      * @param capability   the capability
563      * @param node         the node
564      * @param relationship the relationship
565      * @param occurrences  the occurrences
566      * @return the requirement definition
567      */
568     public static RequirementDefinition createRequirement(String capability, String node, String relationship,
569                                                           Object[] occurrences) {
570         RequirementDefinition requirementDefinition = new RequirementDefinition();
571         requirementDefinition.setCapability(capability);
572         requirementDefinition.setNode(node);
573         requirementDefinition.setRelationship(relationship);
574         if (occurrences != null) {
575             requirementDefinition.setOccurrences(occurrences);
576         }
577         return requirementDefinition;
578     }
579
580     /**
581      * Create entry schema entry schema.
582      *
583      * @param type        the type
584      * @param description the description
585      * @param constraints the constraints
586      * @return the entry schema
587      */
588     public static EntrySchema createEntrySchema(String type, String description, List<Constraint> constraints) {
589         if (Objects.isNull(type) && Objects.isNull(description) && CollectionUtils.isEmpty(constraints)) {
590             return null;
591         }
592
593         EntrySchema entrySchema = new EntrySchema();
594         entrySchema.setType(type);
595         entrySchema.setDescription(description);
596         entrySchema.setConstraints(constraints);
597         return entrySchema;
598     }
599
600     /**
601      * Create get input property value from list parameter map.
602      *
603      * @param inputPropertyListName the input property list name
604      * @param indexInTheList        the index in the list
605      * @param nestedPropertyName    the nested property name
606      * @return the map
607      */
608     public static Map createGetInputPropertyValueFromListParameter(String inputPropertyListName, int indexInTheList,
609                                                                    String... nestedPropertyName) {
610         List<Object> propertyList = new ArrayList<>();
611         propertyList.add(inputPropertyListName);
612         propertyList.add(indexInTheList);
613         if (nestedPropertyName != null) {
614             Collections.addAll(propertyList, nestedPropertyName);
615         }
616         Map<String, Object> getInputProperty = new HashMap<>();
617         getInputProperty.put(ToscaFunctions.GET_INPUT.getFunctionName(), propertyList);
618         return getInputProperty;
619     }
620
621     /**
622      * Convert property def to parameter def parameter definition ext.
623      *
624      * @param propertyDefinition the property definition
625      * @return the parameter definition ext
626      */
627     public static ParameterDefinitionExt convertPropertyDefToParameterDef(PropertyDefinition propertyDefinition) {
628         if (propertyDefinition == null) {
629             return null;
630         }
631
632         ParameterDefinitionExt parameterDefinition = new ParameterDefinitionExt();
633         parameterDefinition.setType(propertyDefinition.getType());
634         parameterDefinition.setDescription(propertyDefinition.getDescription());
635         parameterDefinition.setRequired(propertyDefinition.getRequired());
636         parameterDefinition.set_default(propertyDefinition.get_default());
637         parameterDefinition.setStatus(propertyDefinition.getStatus());
638         parameterDefinition.setConstraints(propertyDefinition.getConstraints());
639         parameterDefinition.setEntry_schema(Objects.isNull(propertyDefinition.getEntry_schema()) ? null :
640                 propertyDefinition.getEntry_schema().clone());
641         parameterDefinition.setHidden(false);
642         parameterDefinition.setImmutable(false);
643         return parameterDefinition;
644     }
645
646     /**
647      * Convert attribute def to parameter def parameter definition ext.
648      *
649      * @param attributeDefinition the attribute definition
650      * @param outputValue         the output value
651      * @return the parameter definition ext
652      */
653     public static ParameterDefinitionExt convertAttributeDefToParameterDef(AttributeDefinition attributeDefinition,
654                                                                            Map<String, List> outputValue) {
655         if (attributeDefinition == null) {
656             return null;
657         }
658         ParameterDefinitionExt parameterDefinition = new ParameterDefinitionExt();
659         parameterDefinition.setDescription(attributeDefinition.getDescription());
660         parameterDefinition.setValue(outputValue);
661         return parameterDefinition;
662     }
663
664     public static boolean isNodeTemplate(String entryId, ServiceTemplate serviceTemplate) {
665         return serviceTemplate.getTopology_template().getNode_templates() != null
666                 && serviceTemplate.getTopology_template().getNode_templates().get(entryId) != null;
667     }
668
669     /**
670      * Add Input parameter.
671      *
672      * @param serviceTemplate       the service template
673      * @param parameterDefinitionId the parameter definition id
674      * @param parameterDefinition   the parameter definition
675      */
676     public static void addInputParameterToTopologyTemplate(ServiceTemplate serviceTemplate,
677                                                            String parameterDefinitionId,
678                                                            ParameterDefinition parameterDefinition) {
679         if (Objects.isNull(serviceTemplate)) {
680             throw new CoreException(
681                     new InvalidAddActionNullEntityErrorBuilder("Topology Template Input Parameter", SERVICE_TEMPLATE)
682                             .build());
683         }
684         TopologyTemplate topologyTemplate = serviceTemplate.getTopology_template();
685         if (Objects.isNull(topologyTemplate)) {
686             topologyTemplate = new TopologyTemplate();
687             serviceTemplate.setTopology_template(topologyTemplate);
688         }
689         if (topologyTemplate.getInputs() == null) {
690             topologyTemplate.setInputs(new HashMap<>());
691         }
692         topologyTemplate.getInputs().put(parameterDefinitionId, parameterDefinition);
693     }
694
695     /**
696      * Add Output parameter.
697      *
698      * @param serviceTemplate       the service template
699      * @param parameterDefinitionId the parameter definition id
700      * @param parameterDefinition   the parameter definition
701      */
702     public static void addOutputParameterToTopologyTemplate(ServiceTemplate serviceTemplate,
703                                                             String parameterDefinitionId,
704                                                             ParameterDefinition parameterDefinition) {
705         if (Objects.isNull(serviceTemplate)) {
706             throw new CoreException(
707                     new InvalidAddActionNullEntityErrorBuilder("Topology Template Output Parameter", SERVICE_TEMPLATE)
708                             .build());
709         }
710         TopologyTemplate topologyTemplate = serviceTemplate.getTopology_template();
711         if (Objects.isNull(topologyTemplate)) {
712             topologyTemplate = new TopologyTemplate();
713             serviceTemplate.setTopology_template(topologyTemplate);
714         }
715         if (topologyTemplate.getOutputs() == null) {
716             topologyTemplate.setOutputs(new HashMap<>());
717         }
718         topologyTemplate.getOutputs().put(parameterDefinitionId, parameterDefinition);
719     }
720
721     /**
722      * Add requirement def to requirement def list.
723      *
724      * @param requirementList requirement list
725      * @param requirementDef  added requirement def
726      */
727     public static void addRequirementToList(List<Map<String, RequirementDefinition>> requirementList,
728                                             Map<String, RequirementDefinition> requirementDef) {
729         if (requirementDef == null) {
730             return;
731         }
732         if (requirementList == null) {
733             requirementList = new ArrayList<>();
734         }
735
736         for (Map.Entry<String, RequirementDefinition> entry : requirementDef.entrySet()) {
737             CommonMethods.mergeEntryInList(entry.getKey(), entry.getValue(), requirementList);
738         }
739     }
740
741     /**
742      * get node template requirement.
743      *
744      * @param nodeTemplate node template
745      */
746     public static Map<String, RequirementAssignment> getNodeTemplateRequirements(NodeTemplate nodeTemplate) {
747         if (Objects.isNull(nodeTemplate)) {
748             return null;
749         }
750         List<Map<String, RequirementAssignment>> templateRequirements = nodeTemplate.getRequirements();
751
752         Map<String, RequirementAssignment> nodeTemplateRequirementsAssignment = new HashMap<>();
753         if (CollectionUtils.isEmpty(templateRequirements)) {
754             return nodeTemplateRequirementsAssignment;
755         }
756         ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
757         for (Map<String, RequirementAssignment> requirementAssignmentMap : templateRequirements) {
758             for (Map.Entry<String, RequirementAssignment> requirementEntry : requirementAssignmentMap.entrySet()) {
759                 RequirementAssignment requirementAssignment = (toscaExtensionYamlUtil.yamlToObject(
760                         toscaExtensionYamlUtil.objectToYaml(requirementEntry.getValue()), RequirementAssignment.class));
761                 nodeTemplateRequirementsAssignment.put(requirementEntry.getKey(), requirementAssignment);
762             }
763         }
764         return nodeTemplateRequirementsAssignment;
765     }
766
767     /**
768      * Gets the list of requirements for the node template.
769      *
770      * @param nodeTemplate the node template
771      * @return the node template requirement list and null if the node has no requirements
772      */
773     public static List<Map<String, RequirementAssignment>> getNodeTemplateRequirementList(NodeTemplate nodeTemplate) {
774         ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
775         //Creating concrete objects
776         List<Map<String, RequirementAssignment>> requirements = nodeTemplate.getRequirements();
777         List<Map<String, RequirementAssignment>> concreteRequirementList = null;
778         if (requirements != null) {
779             concreteRequirementList = new ArrayList<>();
780             ListIterator<Map<String, RequirementAssignment>> reqListIterator = requirements.listIterator();
781             while (reqListIterator.hasNext()) {
782                 Map<String, RequirementAssignment> requirement = reqListIterator.next();
783                 Map<String, RequirementAssignment> concreteRequirement = new HashMap<>();
784                 for (Map.Entry<String, RequirementAssignment> reqEntry : requirement.entrySet()) {
785                     RequirementAssignment requirementAssignment = (toscaExtensionYamlUtil.yamlToObject(
786                             toscaExtensionYamlUtil.objectToYaml(reqEntry.getValue()), RequirementAssignment.class));
787                     concreteRequirement.put(reqEntry.getKey(), requirementAssignment);
788                     concreteRequirementList.add(concreteRequirement);
789                     reqListIterator.remove();
790                 }
791             }
792             requirements.clear();
793             requirements.addAll(concreteRequirementList);
794             nodeTemplate.setRequirements(requirements);
795         }
796         return concreteRequirementList;
797     }
798
799     /**
800      * get requirement assignment from requirement assignment list by req key.
801      *
802      * @param requirementsAssignmentList requirement definition list
803      * @param requirementKey             requirement key
804      */
805     public static Optional<List<RequirementAssignment>> getRequirementAssignment(
806             List<Map<String, RequirementAssignment>> requirementsAssignmentList, String requirementKey) {
807         if (CollectionUtils.isEmpty(requirementsAssignmentList)) {
808             return Optional.empty();
809         }
810
811         List<RequirementAssignment> matchRequirementAssignmentList = new ArrayList<>();
812         for (Map<String, RequirementAssignment> requirementMap : requirementsAssignmentList) {
813             if (requirementMap.containsKey(requirementKey)) {
814                 ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
815                 RequirementAssignment requirementAssignment = (toscaExtensionYamlUtil.yamlToObject(
816                         toscaExtensionYamlUtil.objectToYaml(requirementMap.get(requirementKey)),
817                         RequirementAssignment.class));
818                 matchRequirementAssignmentList.add(requirementAssignment);
819             }
820         }
821         if (CollectionUtils.isEmpty(matchRequirementAssignmentList)) {
822             return Optional.empty();
823         }
824         return Optional.of(matchRequirementAssignmentList);
825     }
826
827     /**
828      * remove requirement definition from requirement definition list by req key.
829      *
830      * @param requirementsDefinitionList requirement definition list
831      * @param requirementKey             requirement key
832      */
833     public static void removeRequirementsDefinition(List<Map<String, RequirementDefinition>> requirementsDefinitionList,
834                                                     String requirementKey) {
835         if (requirementsDefinitionList == null) {
836             return;
837         }
838
839         List<Map<String, RequirementDefinition>> mapToBeRemoved = new ArrayList<>();
840         for (Map<String, RequirementDefinition> reqMap : requirementsDefinitionList) {
841             reqMap.remove(requirementKey);
842             if (reqMap.isEmpty()) {
843                 mapToBeRemoved.add(reqMap);
844             }
845         }
846         for (Map<String, RequirementDefinition> removeMap : mapToBeRemoved) {
847             requirementsDefinitionList.remove(removeMap);
848         }
849     }
850
851     /**
852      * remove requirement assignment from requirement definition list by req key.
853      *
854      * @param requirementsAssignmentList requirement Assignment list
855      * @param requirementKey             requirement key
856      */
857     public static void removeRequirementsAssignment(List<Map<String, RequirementAssignment>> requirementsAssignmentList,
858                                                     String requirementKey) {
859         if (requirementsAssignmentList == null) {
860             return;
861         }
862
863         List<Map<String, RequirementAssignment>> mapToBeRemoved = new ArrayList<>();
864         for (Map<String, RequirementAssignment> reqMap : requirementsAssignmentList) {
865             reqMap.remove(requirementKey);
866             if (reqMap.isEmpty()) {
867                 mapToBeRemoved.add(reqMap);
868             }
869         }
870         for (Map<String, RequirementAssignment> removeMap : mapToBeRemoved) {
871             requirementsAssignmentList.remove(removeMap);
872         }
873     }
874
875
876     /**
877      * Remove requirement assignment.
878      *
879      * @param nodeTemplate                     the node template
880      * @param requirementKey                   the requirement key
881      * @param requirementAssignmentToBeDeleted the requirement assignment to be deleted
882      */
883     public static void removeRequirementAssignment(NodeTemplate nodeTemplate, String requirementKey,
884                                                    RequirementAssignment requirementAssignmentToBeDeleted) {
885         ToscaAnalyzerService toscaAnalyzerService = new ToscaAnalyzerServiceImpl();
886         List<Map<String, RequirementAssignment>> nodeTemplateRequirements = nodeTemplate.getRequirements();
887         if (nodeTemplateRequirements == null) {
888             return;
889         }
890
891         ListIterator<Map<String, RequirementAssignment>> iter = nodeTemplateRequirements.listIterator();
892         while (iter.hasNext()) {
893             Map<String, RequirementAssignment> reqMap = iter.next();
894             RequirementAssignment requirementAssignment = reqMap.get(requirementKey);
895             if (requirementAssignment != null) {
896                 boolean isDesiredRequirementAssignment = toscaAnalyzerService
897                         .isDesiredRequirementAssignment(requirementAssignment,
898                                 requirementAssignmentToBeDeleted
899                                         .getCapability(),
900                                 requirementAssignmentToBeDeleted.getNode(),
901                                 requirementAssignmentToBeDeleted
902                                         .getRelationship());
903                 if (isDesiredRequirementAssignment) {
904                     iter.remove();
905                 }
906             }
907         }
908     }
909
910     /**
911      * Return the suffix of the input namespace For an exampale - for abc.sdf.vsrx, return vsrx
912      *
913      * @param namespace namespace
914      * @return String namespace suffix
915      */
916     public static String getNamespaceSuffix(String namespace) {
917         if (namespace == null) {
918             return null;
919         }
920         String delimiterChar = ".";
921         if (namespace.contains(delimiterChar)) {
922             return namespace.substring(namespace.lastIndexOf(delimiterChar) + 1);
923         }
924         return namespace;
925     }
926
927     /**
928      * Return true if the input import exist in the input imports list.
929      *
930      * @param imports  namespace
931      * @param importId namespace
932      * @return true if exist, false if not exist
933      */
934     public static boolean isImportAddedToServiceTemplate(List<Map<String, Import>> imports, String importId) {
935         for (Map<String, Import> anImport : imports) {
936             if (anImport.containsKey(importId)) {
937                 return true;
938             }
939         }
940         return false;
941     }
942
943     /**
944      * Get output parameter according to the input outputParameterId.
945      *
946      * @param serviceTemplate   service template
947      * @param outputParameterId output parameter id
948      * @return ParameterDefinition - output parameter
949      */
950     public static ParameterDefinition getOuputParameter(ServiceTemplate serviceTemplate, String outputParameterId) {
951         if (serviceTemplate == null || serviceTemplate.getTopology_template() == null
952                 || serviceTemplate.getTopology_template().getOutputs() == null) {
953             return null;
954         }
955         return serviceTemplate.getTopology_template().getOutputs().get(outputParameterId);
956     }
957
958     /**
959      * Gets input parameters in a service template.
960      *
961      * @param serviceTemplate the service template
962      * @return the input parameters
963      */
964     public static Map<String, ParameterDefinition> getInputParameters(ServiceTemplate serviceTemplate) {
965         if (serviceTemplate == null || serviceTemplate.getTopology_template() == null
966                 || serviceTemplate.getTopology_template().getInputs() == null) {
967             return null;
968         }
969         return serviceTemplate.getTopology_template().getInputs();
970     }
971
972     /**
973      * Gets relationship templates in a service template.
974      *
975      * @param serviceTemplate the service template
976      * @return the relationship template
977      */
978     public static Map<String, RelationshipTemplate> getRelationshipTemplates(ServiceTemplate serviceTemplate) {
979         if (serviceTemplate == null || serviceTemplate.getTopology_template() == null
980                 || serviceTemplate.getTopology_template().getRelationship_templates() == null) {
981             return null;
982         }
983         return serviceTemplate.getTopology_template().getRelationship_templates();
984     }
985
986     /**
987      * Get property value according to the input propertyId.
988      *
989      * @param nodeTemplate node template
990      * @param propertyId   property id
991      * @return Object        property Value
992      */
993     public static Object getPropertyValue(NodeTemplate nodeTemplate, String propertyId) {
994         if (nodeTemplate == null || nodeTemplate.getProperties() == null) {
995             return null;
996         }
997         return nodeTemplate.getProperties().get(propertyId);
998     }
999
1000     /**
1001      * Get node template properties according to the input node template id.
1002      *
1003      * @param serviceTemplate service template
1004      * @param nodeTemplateId  node template id
1005      * @return node template properties
1006      */
1007     public static Map<String, Object> getNodeTemplateProperties(ServiceTemplate serviceTemplate,
1008                                                                 String nodeTemplateId) {
1009         if (serviceTemplate == null || serviceTemplate.getTopology_template() == null
1010                 || serviceTemplate.getTopology_template().getNode_templates() == null
1011                 || serviceTemplate.getTopology_template().getNode_templates().get(nodeTemplateId) == null) {
1012             return null;
1013         }
1014         return serviceTemplate.getTopology_template().getNode_templates().get(nodeTemplateId).getProperties();
1015     }
1016
1017     /**
1018      * Adds a property to a node template.
1019      *
1020      * @param nodeTemplate  the node template
1021      * @param propertyKey   the property key
1022      * @param propertyValue the property value
1023      */
1024     public static void addNodeTemplateProperty(NodeTemplate nodeTemplate, String propertyKey, Object propertyValue) {
1025         if (Objects.isNull(nodeTemplate)) {
1026             return;
1027         }
1028
1029         if (MapUtils.isEmpty(nodeTemplate.getProperties())) {
1030             nodeTemplate.setProperties(new HashMap<>());
1031         }
1032
1033         nodeTemplate.getProperties().put(propertyKey, propertyValue);
1034     }
1035
1036     /**
1037      * Gets substitution mappings in a service template.
1038      *
1039      * @param serviceTemplate the service template
1040      * @return the substitution mappings
1041      */
1042     public static SubstitutionMapping getSubstitutionMappings(ServiceTemplate serviceTemplate) {
1043         if (serviceTemplate == null || serviceTemplate.getTopology_template() == null
1044                 || serviceTemplate.getTopology_template().getSubstitution_mappings() == null) {
1045             return null;
1046         }
1047         return serviceTemplate.getTopology_template().getSubstitution_mappings();
1048     }
1049
1050
1051     /**
1052      * Compare two requirement assignment objects for equality.
1053      *
1054      * @param first  the first requirement assignment object
1055      * @param second the second  requirement assignment object
1056      * @return true if objects are equal and false otherwise
1057      */
1058     public static boolean compareRequirementAssignment(RequirementAssignment first, RequirementAssignment second) {
1059         return (first.getCapability().equals(second.getCapability()) && first.getNode().equals(second.getNode())
1060                 && first.getRelationship().equals(second.getRelationship()));
1061     }
1062
1063     /**
1064      * Gets a deep copy clone of the input object.
1065      *
1066      * @param <T>         the type parameter
1067      * @param objectValue the object value
1068      * @param clazz       the clazz
1069      * @return the cloned object
1070      */
1071     public static <T> Object getClonedObject(Object objectValue, Class<T> clazz) {
1072         YamlUtil yamlUtil = new ToscaExtensionYamlUtil();
1073         Object clonedObjectValue;
1074         String objectToYaml = yamlUtil.objectToYaml(objectValue);
1075         clonedObjectValue = yamlUtil.yamlToObject(objectToYaml, clazz);
1076         return clonedObjectValue;
1077     }
1078
1079     /**
1080      * Gets a deep copy clone of the input object.
1081      *
1082      * @param obj the object to be cloned
1083      * @return the cloned object
1084      */
1085     public static Object getClonedObject(Object obj) {
1086         Object clonedObjectValue;
1087         try {
1088             //Serialize object
1089             ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
1090             ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
1091             objectOutputStream.writeObject(obj);
1092             //Deserialize object
1093             ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
1094             ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
1095             clonedObjectValue = objectInputStream.readObject();
1096         } catch (NotSerializableException ex) {
1097             LOGGER.debug(ex.getMessage(), ex);
1098             return getClonedObject(obj, obj.getClass());
1099         } catch (IOException | ClassNotFoundException ex) {
1100             LOGGER.debug(ex.getMessage(), ex);
1101             return null;
1102         }
1103         return clonedObjectValue;
1104     }
1105
1106     /**
1107      * Add substitution filtering property.
1108      *
1109      * @param templateName the substitution service template name
1110      * @param nodeTemplate the node template
1111      * @param count        the count
1112      */
1113     public static void addSubstitutionFilteringProperty(String templateName, NodeTemplate nodeTemplate, int count) {
1114         Map<String, Object> serviceTemplateFilterPropertyValue = new HashMap<>();
1115         Map<String, Object> properties = nodeTemplate.getProperties();
1116         serviceTemplateFilterPropertyValue.put(ToscaConstants.SUBSTITUTE_SERVICE_TEMPLATE_PROPERTY_NAME, templateName);
1117         serviceTemplateFilterPropertyValue.put(ToscaConstants.COUNT_PROPERTY_NAME, count);
1118         properties.put(ToscaConstants.SERVICE_TEMPLATE_FILTER_PROPERTY_NAME, serviceTemplateFilterPropertyValue);
1119         nodeTemplate.setProperties(properties);
1120     }
1121
1122     /**
1123      * Adding binding requirement from port node template to compute node template.
1124      *
1125      * @param computeNodeTemplateId compute node template id
1126      * @param portNodeTemplate      port node template
1127      */
1128     public static void addBindingReqFromPortToCompute(String computeNodeTemplateId, NodeTemplate portNodeTemplate) {
1129         RequirementAssignment requirementAssignment = new RequirementAssignment();
1130         requirementAssignment.setCapability(ToscaCapabilityType.NATIVE_NETWORK_BINDABLE);
1131         requirementAssignment.setRelationship(ToscaRelationshipType.NATIVE_NETWORK_BINDS_TO);
1132         requirementAssignment.setNode(computeNodeTemplateId);
1133         addRequirementAssignment(portNodeTemplate, ToscaConstants.BINDING_REQUIREMENT_ID, requirementAssignment);
1134     }
1135
1136     /**
1137      * Create substitution template substitution mapping.
1138      *
1139      * @param nodeTypeKey          the node type key
1140      * @param substitutionNodeType the substitution node type
1141      * @param mapping              the mapping
1142      * @return the substitution mapping
1143      */
1144     public static SubstitutionMapping createSubstitutionTemplateSubMapping(String nodeTypeKey,
1145                                                                            NodeType substitutionNodeType,
1146                                                                            Map<String, Map<String, List<String>>> mapping) {
1147         SubstitutionMapping substitutionMapping = new SubstitutionMapping();
1148         substitutionMapping.setNode_type(nodeTypeKey);
1149         substitutionMapping.setCapabilities(manageCapabilityMapping(substitutionNodeType.getCapabilities(),
1150                 mapping.get(ToscaConstants.CAPABILITY)));
1151         substitutionMapping.setRequirements(
1152                 manageRequirementMapping(substitutionNodeType.getRequirements(), mapping.get("requirement")));
1153         return substitutionMapping;
1154     }
1155
1156     /**
1157      * Add node template capability.
1158      *
1159      * @param nodeTemplate         the node template
1160      * @param capabilityId         the capability id
1161      * @param capabilityProperties the capability properties
1162      * @param capabilityAttributes the capability attributes
1163      */
1164     public static void addNodeTemplateCapability(NodeTemplate nodeTemplate, String capabilityId,
1165                                                  Map<String, Object> capabilityProperties,
1166                                                  Map<String, Object> capabilityAttributes) {
1167         Map<String, CapabilityAssignment> capabilities = nodeTemplate.getCapabilities();
1168         if (Objects.isNull(capabilities)) {
1169             capabilities = new HashMap<>();
1170         }
1171         CapabilityAssignment capabilityAssignment = new CapabilityAssignment();
1172         capabilityAssignment.setProperties(capabilityProperties);
1173         capabilityAssignment.setAttributes(capabilityAttributes);
1174         capabilities.put(capabilityId, capabilityAssignment);
1175         nodeTemplate.setCapabilities(capabilities);
1176     }
1177
1178     private static Map<String, List<String>> manageRequirementMapping(
1179             List<Map<String, RequirementDefinition>> requirementList,
1180             Map<String, List<String>> requirementSubstitutionMapping) {
1181         if (requirementList == null) {
1182             return null;
1183         }
1184         Map<String, List<String>> requirementMapping = new HashMap<>();
1185         String requirementKey;
1186         List<String> requirementMap;
1187         for (Map<String, RequirementDefinition> requirementDefMap : requirementList) {
1188             for (Map.Entry<String, RequirementDefinition> entry : requirementDefMap.entrySet()) {
1189                 requirementKey = entry.getKey();
1190                 requirementMap = requirementSubstitutionMapping.get(requirementKey);
1191                 requirementMapping.put(requirementKey, requirementMap);
1192             }
1193         }
1194         return requirementMapping;
1195     }
1196
1197     private static Map<String, List<String>> manageCapabilityMapping(Map<String, CapabilityDefinition> capabilities,
1198                                                                      Map<String, List<String>> capabilitySubstitutionMapping) {
1199         if (capabilities == null) {
1200             return null;
1201         }
1202
1203         Map<String, List<String>> capabilityMapping = new HashMap<>();
1204         String capabilityKey;
1205         List<String> capabilityMap;
1206         for (Map.Entry<String, CapabilityDefinition> entry : capabilities.entrySet()) {
1207             capabilityKey = entry.getKey();
1208             capabilityMap = capabilitySubstitutionMapping.get(capabilityKey);
1209             capabilityMapping.put(capabilityKey, capabilityMap);
1210         }
1211         return capabilityMapping;
1212     }
1213
1214     public static void addSubstitutionNodeTypeRequirements(NodeType substitutionNodeType,
1215                                                            List<Map<String, RequirementDefinition>> requirementsList,
1216                                                            String templateName) {
1217         if (CollectionUtils.isEmpty(requirementsList)) {
1218             return;
1219         }
1220         if (substitutionNodeType.getRequirements() == null) {
1221             substitutionNodeType.setRequirements(new ArrayList<>());
1222         }
1223
1224         for (Map<String, RequirementDefinition> requirementDef : requirementsList) {
1225             for (Map.Entry<String, RequirementDefinition> entry : requirementDef.entrySet()) {
1226                 Map<String, RequirementDefinition> requirementMap = new HashMap<>();
1227                 requirementMap.put(entry.getKey() + "_" + templateName, entry.getValue().clone());
1228                 substitutionNodeType.getRequirements().add(requirementMap);
1229             }
1230         }
1231     }
1232
1233     public static boolean isNodeTemplateSectionMissingFromServiceTemplate(ServiceTemplate serviceTemplate) {
1234         return Objects.isNull(serviceTemplate.getTopology_template()) || MapUtils.isEmpty(
1235                 serviceTemplate.getTopology_template().getNode_templates());
1236     }
1237
1238     /**
1239      * Gets relationship template in a service template according to the relationship id.
1240      *
1241      * @param serviceTemplate the service template
1242      * @param relationshipId  the relationship id
1243      * @return the relationship template
1244      */
1245     public static Optional<RelationshipTemplate> getRelationshipTemplate(ServiceTemplate serviceTemplate,
1246                                                                          String relationshipId) {
1247         if (serviceTemplate == null || serviceTemplate.getTopology_template() == null
1248                 || serviceTemplate.getTopology_template().getRelationship_templates() == null
1249                 || serviceTemplate.getTopology_template().getRelationship_templates().get(relationshipId) == null) {
1250             return Optional.empty();
1251         }
1252         return Optional.of(serviceTemplate.getTopology_template().getRelationship_templates().get(relationshipId));
1253     }
1254
1255
1256 }