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