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