2 * Copyright © 2016-2018 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.openecomp.sdc.tosca.services;
19 import java.io.ByteArrayInputStream;
20 import java.io.ByteArrayOutputStream;
21 import java.io.IOException;
22 import java.io.NotSerializableException;
23 import java.io.ObjectInputStream;
24 import java.io.ObjectOutputStream;
25 import java.util.ArrayList;
26 import java.util.Collections;
27 import java.util.HashMap;
28 import java.util.List;
29 import java.util.ListIterator;
31 import java.util.Objects;
32 import java.util.Optional;
34 import lombok.AccessLevel;
35 import lombok.NoArgsConstructor;
36 import org.apache.commons.collections4.CollectionUtils;
37 import org.apache.commons.collections4.MapUtils;
38 import org.onap.sdc.tosca.datatypes.model.AttributeDefinition;
39 import org.onap.sdc.tosca.datatypes.model.CapabilityAssignment;
40 import org.onap.sdc.tosca.datatypes.model.CapabilityDefinition;
41 import org.onap.sdc.tosca.datatypes.model.Constraint;
42 import org.onap.sdc.tosca.datatypes.model.EntrySchema;
43 import org.onap.sdc.tosca.datatypes.model.GroupDefinition;
44 import org.onap.sdc.tosca.datatypes.model.Import;
45 import org.onap.sdc.tosca.datatypes.model.NodeTemplate;
46 import org.onap.sdc.tosca.datatypes.model.NodeType;
47 import org.onap.sdc.tosca.datatypes.model.ParameterDefinition;
48 import org.onap.sdc.tosca.datatypes.model.PolicyDefinition;
49 import org.onap.sdc.tosca.datatypes.model.PropertyDefinition;
50 import org.onap.sdc.tosca.datatypes.model.RelationshipTemplate;
51 import org.onap.sdc.tosca.datatypes.model.RequirementAssignment;
52 import org.onap.sdc.tosca.datatypes.model.RequirementDefinition;
53 import org.onap.sdc.tosca.datatypes.model.ServiceTemplate;
54 import org.onap.sdc.tosca.datatypes.model.SubstitutionMapping;
55 import org.onap.sdc.tosca.datatypes.model.TopologyTemplate;
56 import org.onap.sdc.tosca.datatypes.model.heatextend.ParameterDefinitionExt;
57 import org.onap.sdc.tosca.services.ToscaExtensionYamlUtil;
58 import org.onap.sdc.tosca.services.YamlUtil;
59 import org.openecomp.core.utilities.CommonMethods;
60 import org.openecomp.sdc.common.errors.CoreException;
61 import org.openecomp.sdc.logging.api.Logger;
62 import org.openecomp.sdc.logging.api.LoggerFactory;
63 import org.openecomp.sdc.tosca.datatypes.ToscaCapabilityType;
64 import org.openecomp.sdc.tosca.datatypes.ToscaFunctions;
65 import org.openecomp.sdc.tosca.datatypes.ToscaRelationshipType;
66 import org.openecomp.sdc.tosca.errors.InvalidAddActionNullEntityErrorBuilder;
67 import org.openecomp.sdc.tosca.errors.InvalidRequirementAssignmentErrorBuilder;
68 import org.openecomp.sdc.tosca.services.impl.ToscaAnalyzerServiceImpl;
71 * The type Data model util.
73 @NoArgsConstructor(access = AccessLevel.PRIVATE)
74 public class DataModelUtil {
76 private static final Logger LOGGER = LoggerFactory.getLogger(DataModelUtil.class);
77 private static final String SERVICE_TEMPLATE = "Service Template";
78 private static final String NODE_TYPE = "Node Type";
81 * Add substitution mapping.
83 * @param serviceTemplate the service template
84 * @param substitutionMapping the substitution mapping
86 public static void addSubstitutionMapping(ServiceTemplate serviceTemplate,
87 SubstitutionMapping substitutionMapping) {
88 if (serviceTemplate == null) {
89 throw new CoreException(new InvalidAddActionNullEntityErrorBuilder("Substitution Mapping", SERVICE_TEMPLATE)
93 if (serviceTemplate.getTopology_template() == null) {
94 serviceTemplate.setTopology_template(new TopologyTemplate());
96 serviceTemplate.getTopology_template().setSubstitution_mappings(substitutionMapping);
100 * Gets node template directives.
102 * @param nodeTemplate the node template
103 * @return the directives
105 public static List<String> getDirectives(NodeTemplate nodeTemplate) {
106 if (Objects.isNull(nodeTemplate) || Objects.isNull(nodeTemplate.getDirectives())) {
107 return Collections.emptyList();
109 return nodeTemplate.getDirectives();
113 * Add substitution mapping req.
115 * @param serviceTemplate the service template
116 * @param substitutionMappingRequirementId the substitution mapping requirement id
117 * @param substitutionMappingRequirementList the substitution mapping requirement list
119 public static void addSubstitutionMappingReq(ServiceTemplate serviceTemplate,
120 String substitutionMappingRequirementId,
121 List<String> substitutionMappingRequirementList) {
122 if (serviceTemplate == null) {
123 throw new CoreException(
124 new InvalidAddActionNullEntityErrorBuilder("Substitution Mapping Requirements", SERVICE_TEMPLATE)
128 if (serviceTemplate.getTopology_template() == null) {
129 serviceTemplate.setTopology_template(new TopologyTemplate());
131 if (serviceTemplate.getTopology_template().getSubstitution_mappings() == null) {
132 serviceTemplate.getTopology_template().setSubstitution_mappings(new SubstitutionMapping());
134 if (serviceTemplate.getTopology_template().getSubstitution_mappings().getRequirements() == null) {
135 serviceTemplate.getTopology_template().getSubstitution_mappings().setRequirements(new HashMap<>());
138 serviceTemplate.getTopology_template().getSubstitution_mappings().getRequirements()
139 .put(substitutionMappingRequirementId, substitutionMappingRequirementList);
143 * Add substitution mapping capability.
145 * @param serviceTemplate the service template
146 * @param substitutionMappingCapabilityId the substitution mapping capability id
147 * @param substitutionMappingCapabilityList the substitution mapping capability list
149 public static void addSubstitutionMappingCapability(ServiceTemplate serviceTemplate,
150 String substitutionMappingCapabilityId,
151 List<String> substitutionMappingCapabilityList) {
152 if (serviceTemplate == null) {
153 throw new CoreException(new InvalidAddActionNullEntityErrorBuilder("Substitution Mapping Capabilities",
154 SERVICE_TEMPLATE).build());
157 if (serviceTemplate.getTopology_template() == null) {
158 serviceTemplate.setTopology_template(new TopologyTemplate());
160 if (serviceTemplate.getTopology_template().getSubstitution_mappings() == null) {
161 serviceTemplate.getTopology_template().setSubstitution_mappings(new SubstitutionMapping());
163 if (serviceTemplate.getTopology_template().getSubstitution_mappings().getCapabilities() == null) {
164 serviceTemplate.getTopology_template().getSubstitution_mappings().setCapabilities(new HashMap<>());
167 serviceTemplate.getTopology_template().getSubstitution_mappings().getCapabilities()
168 .putIfAbsent(substitutionMappingCapabilityId, substitutionMappingCapabilityList);
172 * Gets node templates from the service template.
174 * @param serviceTemplate the service template
175 * @return the service template node templates and empty map if not present
177 public static Map<String, NodeTemplate> getNodeTemplates(ServiceTemplate serviceTemplate) {
178 if (Objects.isNull(serviceTemplate) || Objects.isNull(serviceTemplate.getTopology_template())
179 || MapUtils.isEmpty(serviceTemplate.getTopology_template().getNode_templates())) {
180 return new HashMap<>();
183 return serviceTemplate.getTopology_template().getNode_templates();
187 * Gets groups from the service template.
189 * @param serviceTemplate the service template
190 * @return the service template groups and empty map if not present
192 public static Map<String, GroupDefinition> getGroups(ServiceTemplate serviceTemplate) {
193 if (Objects.isNull(serviceTemplate) || Objects.isNull(serviceTemplate.getTopology_template())
194 || MapUtils.isEmpty(serviceTemplate.getTopology_template().getGroups())) {
195 return new HashMap<>();
198 return serviceTemplate.getTopology_template().getGroups();
204 * @param serviceTemplate the service template
205 * @param nodeTemplateId the node template id
206 * @param nodeTemplate the node template
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());
214 TopologyTemplate topologyTemplate = serviceTemplate.getTopology_template();
215 if (Objects.isNull(topologyTemplate)) {
216 topologyTemplate = new TopologyTemplate();
217 serviceTemplate.setTopology_template(topologyTemplate);
219 if (topologyTemplate.getNode_templates() == null) {
220 topologyTemplate.setNode_templates(new HashMap<>());
222 topologyTemplate.getNode_templates().put(nodeTemplateId, nodeTemplate);
226 * Add capabilities def to node type.
228 * @param nodeType the node type
229 * @param capabilities the capability definitions
231 public static void addNodeTypeCapabilitiesDef(NodeType nodeType, Map<String, CapabilityDefinition> capabilities) {
232 if (MapUtils.isEmpty(capabilities)) {
236 if (nodeType == null) {
237 throw new CoreException(
238 new InvalidAddActionNullEntityErrorBuilder("Capability Definition", NODE_TYPE).build());
241 if (MapUtils.isEmpty(nodeType.getCapabilities())) {
242 nodeType.setCapabilities(new HashMap<>());
245 for (Map.Entry<String, CapabilityDefinition> entry : capabilities.entrySet()) {
246 nodeType.getCapabilities().put(entry.getKey(), entry.getValue());
251 * Set capabilities def to node type.
253 * @param nodeType the node type
254 * @param capabilities the capability definitions
256 public static void setNodeTypeCapabilitiesDef(NodeType nodeType, Map<String, CapabilityDefinition> capabilities) {
257 if (MapUtils.isEmpty(capabilities)) {
261 if (nodeType == null) {
262 throw new CoreException(new InvalidAddActionNullEntityErrorBuilder("Capability Definition", NODE_TYPE)
266 if (MapUtils.isEmpty(nodeType.getCapabilities())) {
267 nodeType.setCapabilities(new HashMap<>());
270 if (MapUtils.isNotEmpty(capabilities)) {
271 nodeType.setCapabilities(new HashMap<>());
273 for (Map.Entry<String, CapabilityDefinition> entry : capabilities.entrySet()) {
274 nodeType.getCapabilities().put(entry.getKey(), entry.getValue());
280 * Add policy definition.
282 * @param serviceTemplate the service template
283 * @param policyId the policy id
284 * @param policyDefinition the policy definition
286 public static void addPolicyDefinition(ServiceTemplate serviceTemplate, String policyId,
287 PolicyDefinition policyDefinition) {
288 if (serviceTemplate == null) {
289 throw new CoreException(
290 new InvalidAddActionNullEntityErrorBuilder("Policy Definition", SERVICE_TEMPLATE).build());
292 TopologyTemplate topologyTemplate = serviceTemplate.getTopology_template();
293 if (Objects.isNull(topologyTemplate)) {
294 topologyTemplate = new TopologyTemplate();
295 serviceTemplate.setTopology_template(topologyTemplate);
297 if (topologyTemplate.getPolicies() == null) {
298 topologyTemplate.setPolicies(new HashMap<>());
300 topologyTemplate.getPolicies().put(policyId, policyDefinition);
306 * @param serviceTemplate the service template
307 * @param nodeTypeId the node type id
308 * @param nodeType the node type
310 public static void addNodeType(ServiceTemplate serviceTemplate, String nodeTypeId, NodeType nodeType) {
311 if (serviceTemplate == null) {
312 throw new CoreException(new InvalidAddActionNullEntityErrorBuilder(NODE_TYPE, SERVICE_TEMPLATE).build());
314 if (serviceTemplate.getNode_types() == null) {
315 serviceTemplate.setNode_types(new HashMap<>());
317 serviceTemplate.getNode_types().put(nodeTypeId, nodeType);
321 * Add relationship template.
323 * @param serviceTemplate the service template
324 * @param relationshipTemplateId the relationship template id
325 * @param relationshipTemplate the relationship template
327 public static void addRelationshipTemplate(ServiceTemplate serviceTemplate, String relationshipTemplateId,
328 RelationshipTemplate relationshipTemplate) {
329 if (serviceTemplate == null) {
330 throw new CoreException(
331 new InvalidAddActionNullEntityErrorBuilder("Relationship Template", SERVICE_TEMPLATE).build());
333 if (serviceTemplate.getTopology_template() == null) {
334 serviceTemplate.setTopology_template(new TopologyTemplate());
336 if (serviceTemplate.getTopology_template().getRelationship_templates() == null) {
337 serviceTemplate.getTopology_template().setRelationship_templates(new HashMap<>());
339 serviceTemplate.getTopology_template().getRelationship_templates()
340 .put(relationshipTemplateId, relationshipTemplate);
344 * Add requirement assignment.
346 * @param nodeTemplate the node template
347 * @param requirementId the requirement id
348 * @param requirementAssignment the requirement assignment
350 public static void addRequirementAssignment(NodeTemplate nodeTemplate, String requirementId,
351 RequirementAssignment requirementAssignment) {
352 if (nodeTemplate == null) {
353 throw new CoreException(
354 new InvalidAddActionNullEntityErrorBuilder("Requirement Assignment",
355 "Node Template").build());
357 if (requirementAssignment.getNode() == null) {
358 throw new CoreException(new InvalidRequirementAssignmentErrorBuilder(requirementId).build());
361 if (nodeTemplate.getRequirements() == null) {
362 nodeTemplate.setRequirements(new ArrayList<>());
364 Map<String, RequirementAssignment> requirement = new HashMap<>();
365 requirement.put(requirementId, requirementAssignment);
366 nodeTemplate.getRequirements().add(requirement);
370 * Creates a new requirement assignment object for attachment requirement.
372 * @param node the node
373 * @return the attachment requirement assignment object
375 public static RequirementAssignment createAttachmentRequirementAssignment(String node) {
376 RequirementAssignment requirement = new RequirementAssignment();
377 requirement.setCapability(ToscaCapabilityType.NATIVE_ATTACHMENT);
378 requirement.setNode(node);
379 requirement.setRelationship(ToscaRelationshipType.ATTACHES_TO);
384 * Gets node template.
386 * @param serviceTemplate the service template
387 * @param nodeTemplateId the node template id
388 * @return the node template
390 public static NodeTemplate getNodeTemplate(ServiceTemplate serviceTemplate, String nodeTemplateId) {
391 if (serviceTemplate == null || serviceTemplate.getTopology_template() == null
392 || serviceTemplate.getTopology_template().getNode_templates() == null) {
395 return serviceTemplate.getTopology_template().getNode_templates().get(nodeTemplateId);
401 * @param serviceTemplate the service template
402 * @param nodeTypeId the node type id
403 * @return the node type
405 public static NodeType getNodeType(ServiceTemplate serviceTemplate, String nodeTypeId) {
406 if (serviceTemplate == null || serviceTemplate.getNode_types() == null) {
409 return serviceTemplate.getNode_types().get(nodeTypeId);
413 * Gets requirement definition.
415 * @param nodeType the node type
416 * @param requirementDefinitionId the requirement definition id
417 * @return the requirement definition
419 public static Optional<RequirementDefinition> getRequirementDefinition(NodeType nodeType,
420 String requirementDefinitionId) {
421 if (nodeType == null || nodeType.getRequirements() == null || requirementDefinitionId == null) {
422 return Optional.empty();
424 return getRequirementDefinition(nodeType.getRequirements(), requirementDefinitionId);
428 * get requirement definition from requirement definition list by req key.
430 * @param requirementsDefinitionList requirement definition list
431 * @param requirementKey requirement key
433 public static Optional<RequirementDefinition> getRequirementDefinition(
434 List<Map<String, RequirementDefinition>> requirementsDefinitionList, String requirementKey) {
435 if (CollectionUtils.isEmpty(requirementsDefinitionList)) {
436 return Optional.empty();
439 ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
440 for (Map<String, RequirementDefinition> requirementMap : requirementsDefinitionList) {
441 if (requirementMap.containsKey(requirementKey)) {
442 RequirementDefinition requirementDefinition = toscaExtensionYamlUtil.yamlToObject(
443 toscaExtensionYamlUtil.objectToYaml(requirementMap.get(requirementKey)),
444 RequirementDefinition.class);
445 return Optional.of(requirementDefinition);
448 return Optional.empty();
452 * Gets capability definition.
454 * @param nodeType the node type
455 * @param capabilityDefinitionId the capability definition id
456 * @return the capability definition
458 public static Optional<CapabilityDefinition> getCapabilityDefinition(NodeType nodeType,
459 String capabilityDefinitionId) {
460 if (nodeType == null || nodeType.getCapabilities() == null || capabilityDefinitionId == null) {
461 return Optional.empty();
463 return Optional.ofNullable(nodeType.getCapabilities().get(capabilityDefinitionId));
467 * Add group definition to topology template.
469 * @param serviceTemplate the service template
470 * @param groupName the group name
471 * @param group the group
473 public static void addGroupDefinitionToTopologyTemplate(ServiceTemplate serviceTemplate, String groupName,
474 GroupDefinition group) {
475 if (serviceTemplate == null) {
476 throw new CoreException(
477 new InvalidAddActionNullEntityErrorBuilder("Group Definition", SERVICE_TEMPLATE).build());
480 TopologyTemplate topologyTemplate = serviceTemplate.getTopology_template();
481 if (Objects.isNull(topologyTemplate)) {
482 topologyTemplate = new TopologyTemplate();
483 serviceTemplate.setTopology_template(topologyTemplate);
485 if (topologyTemplate.getGroups() == null) {
486 topologyTemplate.setGroups(new HashMap<>());
488 if (serviceTemplate.getTopology_template().getGroups() == null) {
489 Map<String, GroupDefinition> groups = new HashMap<>();
490 serviceTemplate.getTopology_template().setGroups(groups);
493 serviceTemplate.getTopology_template().getGroups().put(groupName, group);
497 * Adds a group member to an existing group in the service template.
499 * @param serviceTemplate the service template
500 * @param groupName the group name
501 * @param groupMemberId the group member id
503 public static void addGroupMember(ServiceTemplate serviceTemplate, String groupName, String groupMemberId) {
504 TopologyTemplate topologyTemplate = serviceTemplate.getTopology_template();
505 if (Objects.isNull(topologyTemplate) || topologyTemplate.getGroups() == null
506 || topologyTemplate.getGroups().get(groupName) == null) {
510 GroupDefinition groupDefinition = topologyTemplate.getGroups().get(groupName);
511 if (CollectionUtils.isEmpty(groupDefinition.getMembers())) {
512 groupDefinition.setMembers(new ArrayList<>());
515 if (!groupDefinition.getMembers().contains(groupMemberId)) {
516 groupDefinition.getMembers().add(groupMemberId);
521 * Create parameter definition property definition.
523 * @param type the type
524 * @param description the description
525 * @param required the required
526 * @param constraints the constraints
527 * @param entrySchema the entry schema
528 * @param defaultVal the default val
529 * @return the property definition
531 public static ParameterDefinition createParameterDefinition(String type, String description, Boolean required,
532 List<Constraint> constraints, EntrySchema entrySchema,
534 ParameterDefinition paramDef = new ParameterDefinition();
535 paramDef.setType(type);
536 paramDef.setDescription(description);
537 paramDef.setRequired(required);
538 paramDef.setConstraints(constraints);
539 paramDef.setEntry_schema(entrySchema == null ? null : entrySchema.clone());
540 paramDef.set_default(defaultVal);
545 * Create requirement requirement definition.
547 * @param capability the capability
548 * @param node the node
549 * @param relationship the relationship
550 * @param occurrences the occurrences
551 * @return the requirement definition
553 public static RequirementDefinition createRequirement(String capability, String node, String relationship,
554 Object[] occurrences) {
555 RequirementDefinition requirementDefinition = new RequirementDefinition();
556 requirementDefinition.setCapability(capability);
557 requirementDefinition.setNode(node);
558 requirementDefinition.setRelationship(relationship);
559 if (occurrences != null) {
560 requirementDefinition.setOccurrences(occurrences);
562 return requirementDefinition;
566 * Create entry schema entry schema.
568 * @param type the type
569 * @param description the description
570 * @param constraints the constraints
571 * @return the entry schema
573 public static EntrySchema createEntrySchema(String type, String description, List<Constraint> constraints) {
574 if (Objects.isNull(type) && Objects.isNull(description) && CollectionUtils.isEmpty(constraints)) {
578 EntrySchema entrySchema = new EntrySchema();
579 entrySchema.setType(type);
580 entrySchema.setDescription(description);
581 entrySchema.setConstraints(constraints);
586 * Create get input property value from list parameter map.
588 * @param inputPropertyListName the input property list name
589 * @param indexInTheList the index in the list
590 * @param nestedPropertyName the nested property name
593 public static Map createGetInputPropertyValueFromListParameter(String inputPropertyListName, int indexInTheList,
594 String... nestedPropertyName) {
595 List<Object> propertyList = new ArrayList<>();
596 propertyList.add(inputPropertyListName);
597 propertyList.add(indexInTheList);
598 if (nestedPropertyName != null) {
599 Collections.addAll(propertyList, nestedPropertyName);
601 Map<String, Object> getInputProperty = new HashMap<>();
602 getInputProperty.put(ToscaFunctions.GET_INPUT.getFunctionName(), propertyList);
603 return getInputProperty;
607 * Convert property def to parameter def parameter definition ext.
609 * @param propertyDefinition the property definition
610 * @return the parameter definition ext
612 public static ParameterDefinitionExt convertPropertyDefToParameterDef(PropertyDefinition propertyDefinition) {
613 if (propertyDefinition == null) {
617 ParameterDefinitionExt parameterDefinition = new ParameterDefinitionExt();
618 parameterDefinition.setType(propertyDefinition.getType());
619 parameterDefinition.setDescription(propertyDefinition.getDescription());
620 parameterDefinition.setRequired(propertyDefinition.getRequired());
621 parameterDefinition.set_default(propertyDefinition.get_default());
622 parameterDefinition.setStatus(propertyDefinition.getStatus());
623 parameterDefinition.setConstraints(propertyDefinition.getConstraints());
624 parameterDefinition.setEntry_schema(Objects.isNull(propertyDefinition.getEntry_schema()) ? null :
625 propertyDefinition.getEntry_schema().clone());
626 parameterDefinition.setHidden(false);
627 parameterDefinition.setImmutable(false);
628 return parameterDefinition;
632 * Convert attribute def to parameter def parameter definition ext.
634 * @param attributeDefinition the attribute definition
635 * @param outputValue the output value
636 * @return the parameter definition ext
638 public static ParameterDefinitionExt convertAttributeDefToParameterDef(AttributeDefinition attributeDefinition,
639 Map<String, List> outputValue) {
640 if (attributeDefinition == null) {
643 ParameterDefinitionExt parameterDefinition = new ParameterDefinitionExt();
644 parameterDefinition.setDescription(attributeDefinition.getDescription());
645 parameterDefinition.setValue(outputValue);
646 return parameterDefinition;
649 public static boolean isNodeTemplate(String entryId, ServiceTemplate serviceTemplate) {
650 return serviceTemplate.getTopology_template().getNode_templates() != null
651 && serviceTemplate.getTopology_template().getNode_templates().get(entryId) != null;
655 * Add Input parameter.
657 * @param serviceTemplate the service template
658 * @param parameterDefinitionId the parameter definition id
659 * @param parameterDefinition the parameter definition
661 public static void addInputParameterToTopologyTemplate(ServiceTemplate serviceTemplate,
662 String parameterDefinitionId,
663 ParameterDefinition parameterDefinition) {
664 if (Objects.isNull(serviceTemplate)) {
665 throw new CoreException(
666 new InvalidAddActionNullEntityErrorBuilder("Topology Template Input Parameter", SERVICE_TEMPLATE)
669 TopologyTemplate topologyTemplate = serviceTemplate.getTopology_template();
670 if (Objects.isNull(topologyTemplate)) {
671 topologyTemplate = new TopologyTemplate();
672 serviceTemplate.setTopology_template(topologyTemplate);
674 if (topologyTemplate.getInputs() == null) {
675 topologyTemplate.setInputs(new HashMap<>());
677 topologyTemplate.getInputs().put(parameterDefinitionId, parameterDefinition);
681 * Add Output parameter.
683 * @param serviceTemplate the service template
684 * @param parameterDefinitionId the parameter definition id
685 * @param parameterDefinition the parameter definition
687 public static void addOutputParameterToTopologyTemplate(ServiceTemplate serviceTemplate,
688 String parameterDefinitionId,
689 ParameterDefinition parameterDefinition) {
690 if (Objects.isNull(serviceTemplate)) {
691 throw new CoreException(
692 new InvalidAddActionNullEntityErrorBuilder("Topology Template Output Parameter", SERVICE_TEMPLATE)
695 TopologyTemplate topologyTemplate = serviceTemplate.getTopology_template();
696 if (Objects.isNull(topologyTemplate)) {
697 topologyTemplate = new TopologyTemplate();
698 serviceTemplate.setTopology_template(topologyTemplate);
700 if (topologyTemplate.getOutputs() == null) {
701 topologyTemplate.setOutputs(new HashMap<>());
703 topologyTemplate.getOutputs().put(parameterDefinitionId, parameterDefinition);
707 * Add requirement def to requirement def list.
709 * @param requirementList requirement list
710 * @param requirementDef added requirement def
712 public static void addRequirementToList(List<Map<String, RequirementDefinition>> requirementList,
713 Map<String, RequirementDefinition> requirementDef) {
714 if (requirementDef == null) {
717 if (requirementList == null) {
718 requirementList = new ArrayList<>();
721 for (Map.Entry<String, RequirementDefinition> entry : requirementDef.entrySet()) {
722 CommonMethods.mergeEntryInList(entry.getKey(), entry.getValue(), requirementList);
727 * get node template requirement.
729 * @param nodeTemplate node template
731 public static Map<String, RequirementAssignment> getNodeTemplateRequirements(NodeTemplate nodeTemplate) {
732 if (Objects.isNull(nodeTemplate)) {
735 List<Map<String, RequirementAssignment>> templateRequirements = nodeTemplate.getRequirements();
737 Map<String, RequirementAssignment> nodeTemplateRequirementsAssignment = new HashMap<>();
738 if (CollectionUtils.isEmpty(templateRequirements)) {
739 return nodeTemplateRequirementsAssignment;
741 ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
742 for (Map<String, RequirementAssignment> requirementAssignmentMap : templateRequirements) {
743 for (Map.Entry<String, RequirementAssignment> requirementEntry : requirementAssignmentMap.entrySet()) {
744 RequirementAssignment requirementAssignment = (toscaExtensionYamlUtil.yamlToObject(
745 toscaExtensionYamlUtil.objectToYaml(requirementEntry.getValue()), RequirementAssignment.class));
746 nodeTemplateRequirementsAssignment.put(requirementEntry.getKey(), requirementAssignment);
749 return nodeTemplateRequirementsAssignment;
753 * Gets the list of requirements for the node template.
755 * @param nodeTemplate the node template
756 * @return the node template requirement list and null if the node has no requirements
758 public static List<Map<String, RequirementAssignment>> getNodeTemplateRequirementList(NodeTemplate nodeTemplate) {
759 ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
760 //Creating concrete objects
761 List<Map<String, RequirementAssignment>> requirements = nodeTemplate.getRequirements();
762 List<Map<String, RequirementAssignment>> concreteRequirementList = null;
763 if (requirements != null) {
764 concreteRequirementList = new ArrayList<>();
765 ListIterator<Map<String, RequirementAssignment>> reqListIterator = requirements.listIterator();
766 while (reqListIterator.hasNext()) {
767 Map<String, RequirementAssignment> requirement = reqListIterator.next();
768 Map<String, RequirementAssignment> concreteRequirement = new HashMap<>();
769 for (Map.Entry<String, RequirementAssignment> reqEntry : requirement.entrySet()) {
770 RequirementAssignment requirementAssignment = (toscaExtensionYamlUtil.yamlToObject(
771 toscaExtensionYamlUtil.objectToYaml(reqEntry.getValue()), RequirementAssignment.class));
772 concreteRequirement.put(reqEntry.getKey(), requirementAssignment);
773 concreteRequirementList.add(concreteRequirement);
774 reqListIterator.remove();
777 requirements.clear();
778 requirements.addAll(concreteRequirementList);
779 nodeTemplate.setRequirements(requirements);
781 return concreteRequirementList;
785 * get requirement assignment from requirement assignment list by req key.
787 * @param requirementsAssignmentList requirement definition list
788 * @param requirementKey requirement key
790 public static Optional<List<RequirementAssignment>> getRequirementAssignment(
791 List<Map<String, RequirementAssignment>> requirementsAssignmentList, String requirementKey) {
792 if (CollectionUtils.isEmpty(requirementsAssignmentList)) {
793 return Optional.empty();
796 List<RequirementAssignment> matchRequirementAssignmentList = new ArrayList<>();
797 for (Map<String, RequirementAssignment> requirementMap : requirementsAssignmentList) {
798 if (requirementMap.containsKey(requirementKey)) {
799 ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
800 RequirementAssignment requirementAssignment = (toscaExtensionYamlUtil.yamlToObject(
801 toscaExtensionYamlUtil.objectToYaml(requirementMap.get(requirementKey)),
802 RequirementAssignment.class));
803 matchRequirementAssignmentList.add(requirementAssignment);
806 if (CollectionUtils.isEmpty(matchRequirementAssignmentList)) {
807 return Optional.empty();
809 return Optional.of(matchRequirementAssignmentList);
813 * remove requirement definition from requirement definition list by req key.
815 * @param requirementsDefinitionList requirement definition list
816 * @param requirementKey requirement key
818 public static void removeRequirementsDefinition(List<Map<String, RequirementDefinition>> requirementsDefinitionList,
819 String requirementKey) {
820 if (requirementsDefinitionList == null) {
824 List<Map<String, RequirementDefinition>> mapToBeRemoved = new ArrayList<>();
825 for (Map<String, RequirementDefinition> reqMap : requirementsDefinitionList) {
826 reqMap.remove(requirementKey);
827 if (reqMap.isEmpty()) {
828 mapToBeRemoved.add(reqMap);
831 for (Map<String, RequirementDefinition> removeMap : mapToBeRemoved) {
832 requirementsDefinitionList.remove(removeMap);
837 * remove requirement assignment from requirement definition list by req key.
839 * @param requirementsAssignmentList requirement Assignment list
840 * @param requirementKey requirement key
842 public static void removeRequirementsAssignment(List<Map<String, RequirementAssignment>> requirementsAssignmentList,
843 String requirementKey) {
844 if (requirementsAssignmentList == null) {
848 List<Map<String, RequirementAssignment>> mapToBeRemoved = new ArrayList<>();
849 for (Map<String, RequirementAssignment> reqMap : requirementsAssignmentList) {
850 reqMap.remove(requirementKey);
851 if (reqMap.isEmpty()) {
852 mapToBeRemoved.add(reqMap);
855 for (Map<String, RequirementAssignment> removeMap : mapToBeRemoved) {
856 requirementsAssignmentList.remove(removeMap);
862 * Remove requirement assignment.
864 * @param nodeTemplate the node template
865 * @param requirementKey the requirement key
866 * @param requirementAssignmentToBeDeleted the requirement assignment to be deleted
868 public static void removeRequirementAssignment(NodeTemplate nodeTemplate, String requirementKey,
869 RequirementAssignment requirementAssignmentToBeDeleted) {
870 ToscaAnalyzerService toscaAnalyzerService = new ToscaAnalyzerServiceImpl();
871 List<Map<String, RequirementAssignment>> nodeTemplateRequirements = nodeTemplate.getRequirements();
872 if (nodeTemplateRequirements == null) {
876 ListIterator<Map<String, RequirementAssignment>> iter = nodeTemplateRequirements.listIterator();
877 while (iter.hasNext()) {
878 Map<String, RequirementAssignment> reqMap = iter.next();
879 RequirementAssignment requirementAssignment = reqMap.get(requirementKey);
880 if (requirementAssignment != null) {
881 boolean isDesiredRequirementAssignment = toscaAnalyzerService
882 .isDesiredRequirementAssignment(requirementAssignment,
883 requirementAssignmentToBeDeleted
885 requirementAssignmentToBeDeleted.getNode(),
886 requirementAssignmentToBeDeleted
888 if (isDesiredRequirementAssignment) {
896 * Return the suffix of the input namespace For an exampale - for abc.sdf.vsrx, return vsrx
898 * @param namespace namespace
899 * @return String namespace suffix
901 public static String getNamespaceSuffix(String namespace) {
902 if (namespace == null) {
905 String delimiterChar = ".";
906 if (namespace.contains(delimiterChar)) {
907 return namespace.substring(namespace.lastIndexOf(delimiterChar) + 1);
913 * Return true if the input import exist in the input imports list.
915 * @param imports namespace
916 * @param importId namespace
917 * @return true if exist, false if not exist
919 public static boolean isImportAddedToServiceTemplate(List<Map<String, Import>> imports, String importId) {
920 for (Map<String, Import> anImport : imports) {
921 if (anImport.containsKey(importId)) {
929 * Get output parameter according to the input outputParameterId.
931 * @param serviceTemplate service template
932 * @param outputParameterId output parameter id
933 * @return ParameterDefinition - output parameter
935 public static ParameterDefinition getOuputParameter(ServiceTemplate serviceTemplate, String outputParameterId) {
936 if (serviceTemplate == null || serviceTemplate.getTopology_template() == null
937 || serviceTemplate.getTopology_template().getOutputs() == null) {
940 return serviceTemplate.getTopology_template().getOutputs().get(outputParameterId);
944 * Gets input parameters in a service template.
946 * @param serviceTemplate the service template
947 * @return the input parameters
949 public static Map<String, ParameterDefinition> getInputParameters(ServiceTemplate serviceTemplate) {
950 if (serviceTemplate == null || serviceTemplate.getTopology_template() == null
951 || serviceTemplate.getTopology_template().getInputs() == null) {
954 return serviceTemplate.getTopology_template().getInputs();
958 * Gets relationship templates in a service template.
960 * @param serviceTemplate the service template
961 * @return the relationship template
963 public static Map<String, RelationshipTemplate> getRelationshipTemplates(ServiceTemplate serviceTemplate) {
964 if (serviceTemplate == null || serviceTemplate.getTopology_template() == null
965 || serviceTemplate.getTopology_template().getRelationship_templates() == null) {
968 return serviceTemplate.getTopology_template().getRelationship_templates();
972 * Get property value according to the input propertyId.
974 * @param nodeTemplate node template
975 * @param propertyId property id
976 * @return Object property Value
978 public static Object getPropertyValue(NodeTemplate nodeTemplate, String propertyId) {
979 if (nodeTemplate == null || nodeTemplate.getProperties() == null) {
982 return nodeTemplate.getProperties().get(propertyId);
986 * Get node template properties according to the input node template id.
988 * @param serviceTemplate service template
989 * @param nodeTemplateId node template id
990 * @return node template properties
992 public static Map<String, Object> getNodeTemplateProperties(ServiceTemplate serviceTemplate,
993 String nodeTemplateId) {
994 if (serviceTemplate == null || serviceTemplate.getTopology_template() == null
995 || serviceTemplate.getTopology_template().getNode_templates() == null
996 || serviceTemplate.getTopology_template().getNode_templates().get(nodeTemplateId) == null) {
999 return serviceTemplate.getTopology_template().getNode_templates().get(nodeTemplateId).getProperties();
1003 * Adds a property to a node template.
1005 * @param nodeTemplate the node template
1006 * @param propertyKey the property key
1007 * @param propertyValue the property value
1009 public static void addNodeTemplateProperty(NodeTemplate nodeTemplate, String propertyKey, Object propertyValue) {
1010 if (Objects.isNull(nodeTemplate)) {
1014 if (MapUtils.isEmpty(nodeTemplate.getProperties())) {
1015 nodeTemplate.setProperties(new HashMap<>());
1018 nodeTemplate.getProperties().put(propertyKey, propertyValue);
1022 * Gets substitution mappings in a service template.
1024 * @param serviceTemplate the service template
1025 * @return the substitution mappings
1027 public static SubstitutionMapping getSubstitutionMappings(ServiceTemplate serviceTemplate) {
1028 if (serviceTemplate == null || serviceTemplate.getTopology_template() == null
1029 || serviceTemplate.getTopology_template().getSubstitution_mappings() == null) {
1032 return serviceTemplate.getTopology_template().getSubstitution_mappings();
1037 * Compare two requirement assignment objects for equality.
1039 * @param first the first requirement assignment object
1040 * @param second the second requirement assignment object
1041 * @return true if objects are equal and false otherwise
1043 public static boolean compareRequirementAssignment(RequirementAssignment first, RequirementAssignment second) {
1044 return (first.getCapability().equals(second.getCapability()) && first.getNode().equals(second.getNode())
1045 && first.getRelationship().equals(second.getRelationship()));
1049 * Gets a deep copy clone of the input object.
1051 * @param <T> the type parameter
1052 * @param objectValue the object value
1053 * @param clazz the clazz
1054 * @return the cloned object
1056 public static <T> Object getClonedObject(Object objectValue, Class<T> clazz) {
1057 YamlUtil yamlUtil = new ToscaExtensionYamlUtil();
1058 Object clonedObjectValue;
1059 String objectToYaml = yamlUtil.objectToYaml(objectValue);
1060 clonedObjectValue = yamlUtil.yamlToObject(objectToYaml, clazz);
1061 return clonedObjectValue;
1065 * Gets a deep copy clone of the input object.
1067 * @param obj the object to be cloned
1068 * @return the cloned object
1070 public static Object getClonedObject(Object obj) {
1071 Object clonedObjectValue;
1074 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
1075 ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
1076 objectOutputStream.writeObject(obj);
1077 //Deserialize object
1078 ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
1079 ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
1080 clonedObjectValue = objectInputStream.readObject();
1081 } catch (NotSerializableException ex) {
1082 LOGGER.debug(ex.getMessage(), ex);
1083 return getClonedObject(obj, obj.getClass());
1084 } catch (IOException | ClassNotFoundException ex) {
1085 LOGGER.debug(ex.getMessage(), ex);
1088 return clonedObjectValue;
1092 * Add substitution filtering property.
1094 * @param templateName the substitution service template name
1095 * @param nodeTemplate the node template
1096 * @param count the count
1098 public static void addSubstitutionFilteringProperty(String templateName, NodeTemplate nodeTemplate, int count) {
1099 Map<String, Object> serviceTemplateFilterPropertyValue = new HashMap<>();
1100 Map<String, Object> properties = nodeTemplate.getProperties();
1101 serviceTemplateFilterPropertyValue.put(ToscaConstants.SUBSTITUTE_SERVICE_TEMPLATE_PROPERTY_NAME, templateName);
1102 serviceTemplateFilterPropertyValue.put(ToscaConstants.COUNT_PROPERTY_NAME, count);
1103 properties.put(ToscaConstants.SERVICE_TEMPLATE_FILTER_PROPERTY_NAME, serviceTemplateFilterPropertyValue);
1104 nodeTemplate.setProperties(properties);
1108 * Adding binding requirement from port node template to compute node template.
1110 * @param computeNodeTemplateId compute node template id
1111 * @param portNodeTemplate port node template
1113 public static void addBindingReqFromPortToCompute(String computeNodeTemplateId, NodeTemplate portNodeTemplate) {
1114 RequirementAssignment requirementAssignment = new RequirementAssignment();
1115 requirementAssignment.setCapability(ToscaCapabilityType.NATIVE_NETWORK_BINDABLE);
1116 requirementAssignment.setRelationship(ToscaRelationshipType.NATIVE_NETWORK_BINDS_TO);
1117 requirementAssignment.setNode(computeNodeTemplateId);
1118 addRequirementAssignment(portNodeTemplate, ToscaConstants.BINDING_REQUIREMENT_ID, requirementAssignment);
1122 * Create substitution template substitution mapping.
1124 * @param nodeTypeKey the node type key
1125 * @param substitutionNodeType the substitution node type
1126 * @param mapping the mapping
1127 * @return the substitution mapping
1129 public static SubstitutionMapping createSubstitutionTemplateSubMapping(String nodeTypeKey,
1130 NodeType substitutionNodeType,
1131 Map<String, Map<String, List<String>>> mapping) {
1132 SubstitutionMapping substitutionMapping = new SubstitutionMapping();
1133 substitutionMapping.setNode_type(nodeTypeKey);
1134 substitutionMapping.setCapabilities(manageCapabilityMapping(substitutionNodeType.getCapabilities(),
1135 mapping.get(ToscaConstants.CAPABILITY)));
1136 substitutionMapping.setRequirements(
1137 manageRequirementMapping(substitutionNodeType.getRequirements(), mapping.get("requirement")));
1138 return substitutionMapping;
1142 * Add node template capability.
1144 * @param nodeTemplate the node template
1145 * @param capabilityId the capability id
1146 * @param capabilityProperties the capability properties
1147 * @param capabilityAttributes the capability attributes
1149 public static void addNodeTemplateCapability(NodeTemplate nodeTemplate, String capabilityId,
1150 Map<String, Object> capabilityProperties,
1151 Map<String, Object> capabilityAttributes) {
1152 Map<String, CapabilityAssignment> capabilities = nodeTemplate.getCapabilities();
1153 if (Objects.isNull(capabilities)) {
1154 capabilities = new HashMap<>();
1156 CapabilityAssignment capabilityAssignment = new CapabilityAssignment();
1157 capabilityAssignment.setProperties(capabilityProperties);
1158 capabilityAssignment.setAttributes(capabilityAttributes);
1159 capabilities.put(capabilityId, capabilityAssignment);
1160 nodeTemplate.setCapabilities(capabilities);
1163 private static Map<String, List<String>> manageRequirementMapping(
1164 List<Map<String, RequirementDefinition>> requirementList,
1165 Map<String, List<String>> requirementSubstitutionMapping) {
1166 if (requirementList == null) {
1169 Map<String, List<String>> requirementMapping = new HashMap<>();
1170 String requirementKey;
1171 List<String> requirementMap;
1172 for (Map<String, RequirementDefinition> requirementDefMap : requirementList) {
1173 for (Map.Entry<String, RequirementDefinition> entry : requirementDefMap.entrySet()) {
1174 requirementKey = entry.getKey();
1175 requirementMap = requirementSubstitutionMapping.get(requirementKey);
1176 requirementMapping.put(requirementKey, requirementMap);
1179 return requirementMapping;
1182 private static Map<String, List<String>> manageCapabilityMapping(Map<String, CapabilityDefinition> capabilities,
1183 Map<String, List<String>> capabilitySubstitutionMapping) {
1184 if (capabilities == null) {
1188 Map<String, List<String>> capabilityMapping = new HashMap<>();
1189 String capabilityKey;
1190 List<String> capabilityMap;
1191 for (Map.Entry<String, CapabilityDefinition> entry : capabilities.entrySet()) {
1192 capabilityKey = entry.getKey();
1193 capabilityMap = capabilitySubstitutionMapping.get(capabilityKey);
1194 capabilityMapping.put(capabilityKey, capabilityMap);
1196 return capabilityMapping;
1199 public static void addSubstitutionNodeTypeRequirements(NodeType substitutionNodeType,
1200 List<Map<String, RequirementDefinition>> requirementsList,
1201 String templateName) {
1202 if (CollectionUtils.isEmpty(requirementsList)) {
1205 if (substitutionNodeType.getRequirements() == null) {
1206 substitutionNodeType.setRequirements(new ArrayList<>());
1209 for (Map<String, RequirementDefinition> requirementDef : requirementsList) {
1210 for (Map.Entry<String, RequirementDefinition> entry : requirementDef.entrySet()) {
1211 Map<String, RequirementDefinition> requirementMap = new HashMap<>();
1212 requirementMap.put(entry.getKey() + "_" + templateName, entry.getValue().clone());
1213 substitutionNodeType.getRequirements().add(requirementMap);
1218 public static boolean isNodeTemplateSectionMissingFromServiceTemplate(ServiceTemplate serviceTemplate) {
1219 return Objects.isNull(serviceTemplate.getTopology_template()) || MapUtils.isEmpty(
1220 serviceTemplate.getTopology_template().getNode_templates());
1224 * Gets relationship template in a service template according to the relationship id.
1226 * @param serviceTemplate the service template
1227 * @param relationshipId the relationship id
1228 * @return the relationship template
1230 public static Optional<RelationshipTemplate> getRelationshipTemplate(ServiceTemplate serviceTemplate,
1231 String relationshipId) {
1232 if (serviceTemplate == null || serviceTemplate.getTopology_template() == null
1233 || serviceTemplate.getTopology_template().getRelationship_templates() == null
1234 || serviceTemplate.getTopology_template().getRelationship_templates().get(relationshipId) == null) {
1235 return Optional.empty();
1237 return Optional.of(serviceTemplate.getTopology_template().getRelationship_templates().get(relationshipId));