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