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