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