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