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