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