2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.tosca.services;
23 import org.apache.commons.collections4.CollectionUtils;
24 import org.apache.commons.collections4.MapUtils;
25 import org.openecomp.core.utilities.CommonMethods;
26 import org.openecomp.sdc.common.errors.CoreException;
27 import org.openecomp.sdc.datatypes.error.ErrorLevel;
28 import org.openecomp.sdc.logging.api.Logger;
29 import org.openecomp.sdc.logging.api.LoggerFactory;
30 import org.openecomp.sdc.logging.context.impl.MdcDataDebugMessage;
31 import org.openecomp.sdc.logging.context.impl.MdcDataErrorMessage;
32 import org.openecomp.sdc.logging.types.LoggerConstants;
33 import org.openecomp.sdc.logging.types.LoggerErrorCode;
34 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
35 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
36 import org.openecomp.sdc.tosca.datatypes.ToscaCapabilityType;
37 import org.openecomp.sdc.tosca.datatypes.ToscaFunctions;
38 import org.openecomp.sdc.tosca.datatypes.ToscaRelationshipType;
39 import org.openecomp.sdc.tosca.datatypes.model.AttributeDefinition;
40 import org.openecomp.sdc.tosca.datatypes.model.CapabilityDefinition;
41 import org.openecomp.sdc.tosca.datatypes.model.CapabilityType;
42 import org.openecomp.sdc.tosca.datatypes.model.Constraint;
43 import org.openecomp.sdc.tosca.datatypes.model.EntrySchema;
44 import org.openecomp.sdc.tosca.datatypes.model.GroupDefinition;
45 import org.openecomp.sdc.tosca.datatypes.model.Import;
46 import org.openecomp.sdc.tosca.datatypes.model.NodeTemplate;
47 import org.openecomp.sdc.tosca.datatypes.model.NodeType;
48 import org.openecomp.sdc.tosca.datatypes.model.ParameterDefinition;
49 import org.openecomp.sdc.tosca.datatypes.model.PolicyDefinition;
50 import org.openecomp.sdc.tosca.datatypes.model.PropertyDefinition;
51 import org.openecomp.sdc.tosca.datatypes.model.RelationshipTemplate;
52 import org.openecomp.sdc.tosca.datatypes.model.RequirementAssignment;
53 import org.openecomp.sdc.tosca.datatypes.model.RequirementDefinition;
54 import org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate;
55 import org.openecomp.sdc.tosca.datatypes.model.Status;
56 import org.openecomp.sdc.tosca.datatypes.model.SubstitutionMapping;
57 import org.openecomp.sdc.tosca.datatypes.model.TopologyTemplate;
58 import org.openecomp.sdc.tosca.datatypes.model.heatextend.ParameterDefinitionExt;
59 import org.openecomp.sdc.tosca.errors.InvalidAddActionNullEntityErrorBuilder;
60 import org.openecomp.sdc.tosca.errors.InvalidRequirementAssignmentErrorBuilder;
61 import org.openecomp.sdc.tosca.services.impl.ToscaAnalyzerServiceImpl;
63 import java.io.ByteArrayInputStream;
64 import java.io.ByteArrayOutputStream;
65 import java.io.IOException;
66 import java.io.NotSerializableException;
67 import java.io.ObjectInputStream;
68 import java.io.ObjectOutputStream;
69 import java.util.ArrayList;
70 import java.util.Arrays;
71 import java.util.Collections;
72 import java.util.HashMap;
73 import java.util.List;
74 import java.util.ListIterator;
76 import java.util.Objects;
77 import java.util.Optional;
80 * The type Data model util.
82 public class DataModelUtil {
85 * Add substitution mapping.
88 private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
89 private static final Logger logger = LoggerFactory.getLogger(DataModelUtil.class);
92 * Add substitution mapping.
94 * @param serviceTemplate the service template
95 * @param substitutionMapping the substitution mapping
97 public static void addSubstitutionMapping(ServiceTemplate serviceTemplate,
98 SubstitutionMapping substitutionMapping) {
99 mdcDataDebugMessage.debugEntryMessage(null, null);
101 if (serviceTemplate == null) {
102 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
103 LoggerTragetServiceName.ADD_ENTITIES_TO_TOSCA, ErrorLevel.ERROR.name(),
104 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.INVALID_ADD_ACTION);
105 throw new CoreException(
106 new InvalidAddActionNullEntityErrorBuilder("Substitution Mapping", "Service Template")
110 if (serviceTemplate.getTopology_template() == null) {
111 serviceTemplate.setTopology_template(new TopologyTemplate());
113 serviceTemplate.getTopology_template().setSubstitution_mappings(substitutionMapping);
115 mdcDataDebugMessage.debugExitMessage(null, null);
119 * Add substitution mapping req.
121 * @param serviceTemplate the service template
122 * @param substitutionMappingRequirementId the substitution mapping requirement id
123 * @param substitutionMappingRequirementList the substitution mapping requirement list
125 public static void addSubstitutionMappingReq(ServiceTemplate serviceTemplate,
126 String substitutionMappingRequirementId,
127 List<String> substitutionMappingRequirementList) {
130 mdcDataDebugMessage.debugEntryMessage(null, null);
132 if (serviceTemplate == null) {
133 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
134 LoggerTragetServiceName.ADD_ENTITIES_TO_TOSCA, ErrorLevel.ERROR.name(),
135 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.INVALID_ADD_ACTION);
136 throw new CoreException(
137 new InvalidAddActionNullEntityErrorBuilder("Substitution Mapping Requirements",
138 "Service Template").build());
141 if (serviceTemplate.getTopology_template() == null) {
142 serviceTemplate.setTopology_template(new TopologyTemplate());
144 if (serviceTemplate.getTopology_template().getSubstitution_mappings() == null) {
145 serviceTemplate.getTopology_template().setSubstitution_mappings(new SubstitutionMapping());
147 if (serviceTemplate.getTopology_template().getSubstitution_mappings().getRequirements()
149 serviceTemplate.getTopology_template().getSubstitution_mappings()
150 .setRequirements(new HashMap<>());
153 serviceTemplate.getTopology_template().getSubstitution_mappings().getRequirements()
154 .put(substitutionMappingRequirementId, substitutionMappingRequirementList);
156 mdcDataDebugMessage.debugExitMessage(null, null);
162 * @param serviceTemplate the service template
163 * @param nodeTemplateId the node template id
164 * @param nodeTemplate the node template
166 public static void addNodeTemplate(ServiceTemplate serviceTemplate, String nodeTemplateId,
167 NodeTemplate nodeTemplate) {
170 mdcDataDebugMessage.debugEntryMessage(null, null);
172 if (serviceTemplate == null) {
173 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
174 LoggerTragetServiceName.ADD_ENTITIES_TO_TOSCA, ErrorLevel.ERROR.name(),
175 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.INVALID_ADD_ACTION);
176 throw new CoreException(
177 new InvalidAddActionNullEntityErrorBuilder("Node Template", "Service Template").build());
179 TopologyTemplate topologyTemplate = serviceTemplate.getTopology_template();
180 if (Objects.isNull(topologyTemplate)) {
181 topologyTemplate = new TopologyTemplate();
182 serviceTemplate.setTopology_template(topologyTemplate);
184 if (topologyTemplate.getNode_templates() == null) {
185 topologyTemplate.setNode_templates(new HashMap<>());
187 topologyTemplate.getNode_templates().put(nodeTemplateId, nodeTemplate);
189 mdcDataDebugMessage.debugExitMessage(null, null);
194 * Add capability def.
196 * @param nodeType the node type
197 * @param capabilityId the capability id
198 * @param capabilityDefinition the capability definition
200 public static void addCapabilityDef(NodeType nodeType, String capabilityId,
201 CapabilityDefinition capabilityDefinition) {
204 mdcDataDebugMessage.debugEntryMessage(null, null);
206 if (nodeType == null) {
207 throw new CoreException(
208 new InvalidAddActionNullEntityErrorBuilder("Capability Definition", "Node Type").build());
210 if (Objects.isNull(nodeType.getCapabilities())) {
211 nodeType.setCapabilities(new HashMap<>());
213 nodeType.getCapabilities().put(capabilityId, capabilityDefinition);
215 mdcDataDebugMessage.debugExitMessage(null, null);
219 * Add capabilities def to node type.
221 * @param nodeType the node type
222 * @param capabilities the capability definitions
224 public static void addNodeTypeCapabilitiesDef(NodeType nodeType,
225 Map<String, CapabilityDefinition> capabilities) {
226 mdcDataDebugMessage.debugEntryMessage(null, null);
228 if (capabilities == null || capabilities.entrySet().size() == 0) {
232 if (nodeType == null) {
233 throw new CoreException(
234 new InvalidAddActionNullEntityErrorBuilder("Capability Definition", "Node Type").build());
237 if (MapUtils.isEmpty(nodeType.getCapabilities())) {
238 nodeType.setCapabilities(new HashMap<>());
240 if (capabilities.size() > 0) {
241 nodeType.setCapabilities(new HashMap<>());
243 for (Map.Entry<String, CapabilityDefinition> entry : capabilities.entrySet()) {
244 nodeType.getCapabilities().put(entry.getKey(), entry.getValue());
247 mdcDataDebugMessage.debugExitMessage(null, null);
251 * Add policy definition.
253 * @param serviceTemplate the service template
254 * @param policyId the policy id
255 * @param policyDefinition the policy definition
257 public static void addPolicyDefinition(ServiceTemplate serviceTemplate, String policyId,
258 PolicyDefinition policyDefinition) {
261 mdcDataDebugMessage.debugEntryMessage(null, null);
263 if (serviceTemplate == null) {
264 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
265 LoggerTragetServiceName.ADD_ENTITIES_TO_TOSCA, ErrorLevel.ERROR.name(),
266 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.INVALID_ADD_ACTION);
267 throw new CoreException(
268 new InvalidAddActionNullEntityErrorBuilder("Policy Definition", "Service Template")
271 TopologyTemplate topologyTemplate = serviceTemplate.getTopology_template();
272 if (Objects.isNull(topologyTemplate)) {
273 topologyTemplate = new TopologyTemplate();
274 serviceTemplate.setTopology_template(topologyTemplate);
276 if (topologyTemplate.getPolicies() == null) {
277 topologyTemplate.setPolicies(new HashMap<>());
279 topologyTemplate.getPolicies().put(policyId, policyDefinition);
281 mdcDataDebugMessage.debugExitMessage(null, null);
287 * @param serviceTemplate the service template
288 * @param nodeTypeId the node type id
289 * @param nodeType the node type
291 public static void addNodeType(ServiceTemplate serviceTemplate, String nodeTypeId,
295 mdcDataDebugMessage.debugEntryMessage(null, null);
297 if (serviceTemplate == null) {
298 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
299 LoggerTragetServiceName.ADD_ENTITIES_TO_TOSCA, ErrorLevel.ERROR.name(),
300 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.INVALID_ADD_ACTION);
301 throw new CoreException(
302 new InvalidAddActionNullEntityErrorBuilder("Node Type", "Service Template").build());
304 if (serviceTemplate.getNode_types() == null) {
305 serviceTemplate.setNode_types(new HashMap<>());
307 serviceTemplate.getNode_types().put(nodeTypeId, nodeType);
309 mdcDataDebugMessage.debugExitMessage(null, null);
313 * Add relationship template.
315 * @param serviceTemplate the service template
316 * @param relationshipTemplateId the relationship template id
317 * @param relationshipTemplate the relationship template
319 public static void addRelationshipTemplate(ServiceTemplate serviceTemplate,
320 String relationshipTemplateId,
321 RelationshipTemplate relationshipTemplate) {
324 mdcDataDebugMessage.debugEntryMessage(null, null);
326 if (serviceTemplate == null) {
327 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
328 LoggerTragetServiceName.ADD_ENTITIES_TO_TOSCA, ErrorLevel.ERROR.name(),
329 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.INVALID_ADD_ACTION);
330 throw new CoreException(
331 new InvalidAddActionNullEntityErrorBuilder("Relationship Template", "Service Template")
334 if (serviceTemplate.getTopology_template() == null) {
335 serviceTemplate.setTopology_template(new TopologyTemplate());
337 if (serviceTemplate.getTopology_template().getRelationship_templates() == null) {
338 serviceTemplate.getTopology_template().setRelationship_templates(new HashMap<>());
340 serviceTemplate.getTopology_template().getRelationship_templates()
341 .put(relationshipTemplateId, relationshipTemplate);
343 mdcDataDebugMessage.debugExitMessage(null, null);
347 * Add requirement assignment.
349 * @param nodeTemplate the node template
350 * @param requirementId the requirement id
351 * @param requirementAssignment the requirement assignment
353 public static void addRequirementAssignment(NodeTemplate nodeTemplate, String requirementId,
354 RequirementAssignment requirementAssignment) {
357 mdcDataDebugMessage.debugEntryMessage(null, null);
359 if (nodeTemplate == null) {
360 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
361 LoggerTragetServiceName.ADD_ENTITIES_TO_TOSCA, ErrorLevel.ERROR.name(),
362 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.INVALID_ADD_ACTION);
363 throw new CoreException(
364 new InvalidAddActionNullEntityErrorBuilder("Requirement Assignment", "Node Template")
367 if (requirementAssignment.getNode() == null) {
368 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
369 LoggerTragetServiceName.ADD_ENTITIES_TO_TOSCA, ErrorLevel.ERROR.name(),
370 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.INVALID_ADD_ACTION);
371 throw new CoreException(new InvalidRequirementAssignmentErrorBuilder(requirementId).build());
374 if (nodeTemplate.getRequirements() == null) {
375 nodeTemplate.setRequirements(new ArrayList<>());
377 Map<String, RequirementAssignment> requirement = new HashMap<>();
378 requirement.put(requirementId, requirementAssignment);
379 nodeTemplate.getRequirements().add(requirement);
381 mdcDataDebugMessage.debugExitMessage(null, null);
385 * Gets node template.
387 * @param serviceTemplate the service template
388 * @param nodeTemplateId the node template id
389 * @return the node template
391 public static NodeTemplate getNodeTemplate(ServiceTemplate serviceTemplate,
392 String nodeTemplateId) {
394 mdcDataDebugMessage.debugEntryMessage(null, null);
396 if (serviceTemplate == null
397 || serviceTemplate.getTopology_template() == null
398 || serviceTemplate.getTopology_template().getNode_templates() == null) {
402 mdcDataDebugMessage.debugExitMessage(null, null);
403 return serviceTemplate.getTopology_template().getNode_templates().get(nodeTemplateId);
409 * @param serviceTemplate the service template
410 * @param nodeTypeId the node type id
411 * @return the node type
413 public static NodeType getNodeType(ServiceTemplate serviceTemplate, String nodeTypeId) {
416 mdcDataDebugMessage.debugEntryMessage(null, null);
417 if (serviceTemplate == null || serviceTemplate.getNode_types() == null) {
421 mdcDataDebugMessage.debugExitMessage(null, null);
422 return serviceTemplate.getNode_types().get(nodeTypeId);
426 * Gets requirement definition.
428 * @param nodeType the node type
429 * @param requirementDefinitionId the requirement definition id
430 * @return the requirement definition
432 public static Optional<RequirementDefinition> getRequirementDefinition(
434 String requirementDefinitionId) {
437 mdcDataDebugMessage.debugEntryMessage(null, null);
439 if (nodeType == null || nodeType.getRequirements() == null || requirementDefinitionId == null) {
440 return Optional.empty();
442 for (Map<String, RequirementDefinition> reqMap : nodeType.getRequirements()) {
443 if (reqMap.containsKey(requirementDefinitionId)) {
444 return Optional.of(reqMap.get(requirementDefinitionId));
448 mdcDataDebugMessage.debugExitMessage(null, null);
449 return Optional.empty();
453 * get requirement defenition from requirement defenition list by req key.
455 * @param requirementsDefinitionList requirement defenition list
456 * @param requirementKey requirement key
458 public static Optional<RequirementDefinition> getRequirementDefinition(
459 List<Map<String, RequirementDefinition>> requirementsDefinitionList,
460 String requirementKey) {
461 mdcDataDebugMessage.debugEntryMessage(null, null);
462 if (CollectionUtils.isEmpty(requirementsDefinitionList)) {
463 return Optional.empty();
466 for (Map<String, RequirementDefinition> requirementMap : requirementsDefinitionList) {
467 if (requirementMap.containsKey(requirementKey)) {
468 mdcDataDebugMessage.debugExitMessage(null, null);
469 return Optional.of(requirementMap.get(requirementKey));
473 mdcDataDebugMessage.debugExitMessage(null, null);
474 return Optional.empty();
478 * Gets capability definition.
480 * @param nodeType the node type
481 * @param capabilityDefinitionId the capability definition id
482 * @return the capability definition
484 public static Optional<CapabilityDefinition> getCapabilityDefinition(
486 String capabilityDefinitionId) {
489 mdcDataDebugMessage.debugEntryMessage(null, null);
491 if (nodeType == null || nodeType.getCapabilities() == null || capabilityDefinitionId == null) {
492 return Optional.empty();
495 mdcDataDebugMessage.debugExitMessage(null, null);
496 return Optional.ofNullable(nodeType.getCapabilities().get(capabilityDefinitionId));
500 * Add group definition to topology template.
502 * @param serviceTemplate the service template
503 * @param groupName the group name
504 * @param group the group
506 public static void addGroupDefinitionToTopologyTemplate(ServiceTemplate serviceTemplate,
507 String groupName, GroupDefinition group) {
510 mdcDataDebugMessage.debugEntryMessage(null, null);
512 if (serviceTemplate == null) {
513 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
514 LoggerTragetServiceName.ADD_ENTITIES_TO_TOSCA, ErrorLevel.ERROR.name(),
515 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.INVALID_ADD_ACTION);
516 throw new CoreException(
517 new InvalidAddActionNullEntityErrorBuilder("Group Definition", "Service Template")
521 TopologyTemplate topologyTemplate = serviceTemplate.getTopology_template();
522 if (Objects.isNull(topologyTemplate)) {
523 topologyTemplate = new TopologyTemplate();
524 serviceTemplate.setTopology_template(topologyTemplate);
526 if (topologyTemplate.getGroups() == null) {
527 topologyTemplate.setGroups(new HashMap<>());
529 if (serviceTemplate.getTopology_template().getGroups() == null) {
530 Map<String, GroupDefinition> groups = new HashMap<>();
531 serviceTemplate.getTopology_template().setGroups(groups);
534 serviceTemplate.getTopology_template().getGroups().put(groupName, group);
535 mdcDataDebugMessage.debugExitMessage(null, null);
539 * Create property definition property definition.
541 * @param type the type
542 * @param description the description
543 * @param required the required
544 * @param constraints the constraints
545 * @param status the status
546 * @param entrySchema the entry schema
547 * @param defaultVal the default val
548 * @return the property definition
550 public static PropertyDefinition createPropertyDefinition(String type, String description,
552 List<Constraint> constraints,
554 EntrySchema entrySchema,
558 mdcDataDebugMessage.debugEntryMessage(null, null);
560 PropertyDefinition propDef = new PropertyDefinition();
561 propDef.setType(type);
562 propDef.setDescription(description);
563 propDef.setRequired(required);
564 propDef.setConstraints(constraints);
565 if (status != null) {
566 propDef.setStatus(status);
568 propDef.setEntry_schema(entrySchema);
569 propDef.set_default(defaultVal);
571 mdcDataDebugMessage.debugExitMessage(null, null);
576 * Create parameter definition property definition.
578 * @param type the type
579 * @param description the description
580 * @param value the value
581 * @param required the required
582 * @param constraints the constraints
583 * @param status the status
584 * @param entrySchema the entry schema
585 * @param defaultVal the default val
586 * @return the property definition
588 public static ParameterDefinition createParameterDefinition(String type, String description,
589 Object value, boolean required,
590 List<Constraint> constraints,
592 EntrySchema entrySchema,
596 mdcDataDebugMessage.debugEntryMessage(null, null);
598 ParameterDefinition paramDef = new ParameterDefinition();
599 paramDef.setType(type);
600 paramDef.setDescription(description);
601 paramDef.setValue(value);
602 paramDef.setRequired(required);
603 paramDef.setConstraints(constraints);
604 if (status != null) {
605 paramDef.setStatus(status);
607 paramDef.setEntry_schema(entrySchema == null ? null : entrySchema.clone());
608 paramDef.set_default(defaultVal);
610 mdcDataDebugMessage.debugExitMessage(null, null);
615 * Create requirement requirement definition.
617 * @param capability the capability
618 * @param node the node
619 * @param relationship the relationship
620 * @param occurrences the occurrences
621 * @return the requirement definition
623 public static RequirementDefinition createRequirement(String capability, String node,
624 String relationship, Object[] occurrences) {
627 mdcDataDebugMessage.debugEntryMessage(null, null);
629 RequirementDefinition requirementDefinition = new RequirementDefinition();
630 requirementDefinition.setCapability(capability);
631 requirementDefinition.setNode(node);
632 requirementDefinition.setRelationship(relationship);
633 if (occurrences != null) {
634 requirementDefinition.setOccurrences(occurrences);
637 mdcDataDebugMessage.debugExitMessage(null, null);
638 return requirementDefinition;
642 * Create attribute definition attribute definition.
644 * @param type the type
645 * @param description the description
646 * @param status the status
647 * @param entrySchema the entry schema
648 * @param defaultVal the default val
649 * @return the attribute definition
651 public static AttributeDefinition createAttributeDefinition(String type, String description,
653 EntrySchema entrySchema,
657 mdcDataDebugMessage.debugEntryMessage(null, null);
659 AttributeDefinition attributeDef = new AttributeDefinition();
660 attributeDef.setType(type);
662 if (description != null) {
663 attributeDef.setDescription(description);
665 if (status != null) {
666 attributeDef.setStatus(status);
668 attributeDef.setEntry_schema(entrySchema);
669 attributeDef.set_default(defaultVal);
671 mdcDataDebugMessage.debugExitMessage(null, null);
676 * Create valid values constraint constraint.
678 * @param values the values
679 * @return the constraint
681 public static Constraint createValidValuesConstraint(Object... values) {
684 mdcDataDebugMessage.debugEntryMessage(null, null);
686 Constraint validValues = new Constraint();
687 for (Object value : values) {
688 validValues.addValidValue(value);
691 mdcDataDebugMessage.debugExitMessage(null, null);
696 * Create metadata metadata.
698 * @param templateName the template name
699 * @param templateVersion the template version
700 * @param templateAuthor the template author
701 * @return the metadata
703 public static Map<String, String> createMetadata(String templateName, String templateVersion,
704 String templateAuthor) {
707 mdcDataDebugMessage.debugEntryMessage(null, null);
708 Map<String, String> metadata = new HashMap<>();
709 metadata.put(ToscaConstants.ST_METADATA_TEMPLATE_NAME, templateName);
710 metadata.put("template_version", templateVersion);
711 metadata.put("template_author", templateAuthor);
713 mdcDataDebugMessage.debugExitMessage(null, null);
718 * Create entry schema entry schema.
720 * @param type the type
721 * @param description the description
722 * @param constraints the constraints
723 * @return the entry schema
725 public static EntrySchema createEntrySchema(String type, String description,
726 List<Constraint> constraints) {
729 mdcDataDebugMessage.debugEntryMessage(null, null);
731 if(Objects.isNull(type) && Objects.isNull(description) && CollectionUtils.isEmpty(constraints)){
735 EntrySchema entrySchema = new EntrySchema();
736 entrySchema.setType(type);
737 entrySchema.setDescription(description);
738 entrySchema.setConstraints(constraints);
740 mdcDataDebugMessage.debugExitMessage(null, null);
745 * Create valid values constraints list list.
747 * @param values the values
750 public static List<Constraint> createValidValuesConstraintsList(String... values) {
753 mdcDataDebugMessage.debugEntryMessage(null, null);
755 List<Constraint> constraints;
756 Constraint validValues;
757 constraints = new ArrayList<>();
758 validValues = DataModelUtil.createValidValuesConstraint(values);
759 constraints.add(validValues);
761 mdcDataDebugMessage.debugExitMessage(null, null);
766 * Create greater or equal constrain constraint.
768 * @param value the value
769 * @return the constraint
771 public static Constraint createGreaterOrEqualConstrain(Object value) {
772 mdcDataDebugMessage.debugEntryMessage(null, null);
774 Constraint constraint = new Constraint();
775 constraint.setGreater_or_equal(value);
777 mdcDataDebugMessage.debugExitMessage(null, null);
782 * Gets constrain list.
784 * @param constrains the constrains
785 * @return the constrain list
787 public static List<Constraint> getConstrainList(Constraint... constrains) {
788 return Arrays.asList(constrains);
793 * Create get input property value from list parameter map.
795 * @param inputPropertyListName the input property list name
796 * @param indexInTheList the index in the list
797 * @param nestedPropertyName the nested property name
800 public static Map createGetInputPropertyValueFromListParameter(String inputPropertyListName,
802 String... nestedPropertyName) {
805 mdcDataDebugMessage.debugEntryMessage(null, null);
807 List propertyList = new ArrayList<>();
808 propertyList.add(inputPropertyListName);
809 propertyList.add(indexInTheList);
810 if (nestedPropertyName != null) {
811 Collections.addAll(propertyList, nestedPropertyName);
813 Map getInputProperty = new HashMap<>();
814 getInputProperty.put(ToscaFunctions.GET_INPUT.getDisplayName(), propertyList);
816 mdcDataDebugMessage.debugExitMessage(null, null);
817 return getInputProperty;
821 * Convert property def to parameter def parameter definition ext.
823 * @param propertyDefinition the property definition
824 * @return the parameter definition ext
826 public static ParameterDefinitionExt convertPropertyDefToParameterDef(
827 PropertyDefinition propertyDefinition) {
830 mdcDataDebugMessage.debugEntryMessage(null, null);
832 if (propertyDefinition == null) {
836 ParameterDefinitionExt parameterDefinition = new ParameterDefinitionExt();
837 parameterDefinition.setType(propertyDefinition.getType());
838 parameterDefinition.setDescription(propertyDefinition.getDescription());
839 parameterDefinition.setRequired(propertyDefinition.getRequired());
840 parameterDefinition.set_default(propertyDefinition.get_default());
841 parameterDefinition.setStatus(propertyDefinition.getStatus());
842 parameterDefinition.setConstraints(propertyDefinition.getConstraints());
843 parameterDefinition.setEntry_schema(Objects.isNull(propertyDefinition.getEntry_schema()) ? null
844 : propertyDefinition.getEntry_schema().clone());
845 parameterDefinition.setHidden(false);
846 parameterDefinition.setImmutable(false);
848 mdcDataDebugMessage.debugExitMessage(null, null);
849 return parameterDefinition;
853 * Convert attribute def to parameter def parameter definition ext.
855 * @param attributeDefinition the attribute definition
856 * @param outputValue the output value
857 * @return the parameter definition ext
859 public static ParameterDefinitionExt convertAttributeDefToParameterDef(
860 AttributeDefinition attributeDefinition, Map<String, List> outputValue) {
863 mdcDataDebugMessage.debugEntryMessage(null, null);
865 if (attributeDefinition == null) {
868 ParameterDefinitionExt parameterDefinition = new ParameterDefinitionExt();
869 parameterDefinition.setDescription(attributeDefinition.getDescription());
870 parameterDefinition.setValue(outputValue);
872 mdcDataDebugMessage.debugExitMessage(null, null);
873 return parameterDefinition;
877 * Convert capability type to capability definition capability definition.
879 * @param capabilityTypeId the capability type id
880 * @param capabilityType the capability type
881 * @param properties the properties
882 * @return the capability definition
884 public static CapabilityDefinition convertCapabilityTypeToCapabilityDefinition(
885 String capabilityTypeId, CapabilityType capabilityType, Map<String, Object> properties) {
888 mdcDataDebugMessage.debugEntryMessage(null, null);
890 CapabilityDefinition capabilityDefinition = new CapabilityDefinition();
891 capabilityDefinition.setAttributes(cloneAttributeDefinitionMap(capabilityType.getAttributes()));
892 capabilityDefinition.setProperties(clonePropertyDefinitionMap(capabilityType.getProperties()));
893 capabilityDefinition.setDescription(capabilityType.getDescription());
894 capabilityDefinition.setType(capabilityTypeId);
896 capabilityDefinition.getProperties()
899 .filter(entry -> properties.containsKey(entry.getKey()))
900 .forEach(entry -> entry.getValue()
901 .set_default(properties.get(entry.getKey())));
903 mdcDataDebugMessage.debugExitMessage(null, null);
904 return capabilityDefinition;
908 * Clone property definition map map.
910 * @param propertyDefinitionMap the property definition map
913 public static Map clonePropertyDefinitionMap(
914 Map<String, PropertyDefinition> propertyDefinitionMap) {
917 mdcDataDebugMessage.debugEntryMessage(null, null);
919 Map outMap = new HashMap<>();
920 for (String propertyDefKey : propertyDefinitionMap.keySet()) {
921 PropertyDefinition propertyDefValue = propertyDefinitionMap.get(propertyDefKey);
922 outMap.put(new String(propertyDefKey), propertyDefValue.clone());
925 mdcDataDebugMessage.debugExitMessage(null, null);
930 * Clone attribute definition map map.
932 * @param attributeDefinitionMap the attribute definition map
935 public static Map cloneAttributeDefinitionMap(
936 Map<String, AttributeDefinition> attributeDefinitionMap) {
939 mdcDataDebugMessage.debugEntryMessage(null, null);
941 Map outMap = new HashMap<>();
942 for (String attributeDefKey : attributeDefinitionMap.keySet()) {
943 AttributeDefinition attributeDefinition = attributeDefinitionMap.get(attributeDefKey);
944 outMap.put(new String(attributeDefKey), attributeDefinition.clone());
947 mdcDataDebugMessage.debugExitMessage(null, null);
951 public static boolean isNodeTemplate(String entryId, ServiceTemplate serviceTemplate) {
952 return serviceTemplate.getTopology_template().getNode_templates() != null
953 && serviceTemplate.getTopology_template().getNode_templates().get(entryId) != null;
957 * Add Input parameter.
959 * @param serviceTemplate the service template
960 * @param parameterDefinitionId the parameter definition id
961 * @param parameterDefinition the parameter definition
963 public static void addInputParameterToTopologyTemplate(ServiceTemplate serviceTemplate,
964 String parameterDefinitionId,
965 ParameterDefinition parameterDefinition) {
968 mdcDataDebugMessage.debugEntryMessage(null, null);
970 if (Objects.isNull(serviceTemplate)) {
971 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
972 LoggerTragetServiceName.ADD_ENTITIES_TO_TOSCA, ErrorLevel.ERROR.name(),
973 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.INVALID_ADD_ACTION);
974 throw new CoreException(
975 new InvalidAddActionNullEntityErrorBuilder("Topology Template Input Parameter",
976 "Service Template").build());
978 TopologyTemplate topologyTemplate = serviceTemplate.getTopology_template();
979 if (Objects.isNull(topologyTemplate)) {
980 topologyTemplate = new TopologyTemplate();
981 serviceTemplate.setTopology_template(topologyTemplate);
983 if (topologyTemplate.getInputs() == null) {
984 topologyTemplate.setInputs(new HashMap<>());
986 topologyTemplate.getInputs().put(parameterDefinitionId, parameterDefinition);
988 mdcDataDebugMessage.debugExitMessage(null, null);
993 * Add Output parameter.
995 * @param serviceTemplate the service template
996 * @param parameterDefinitionId the parameter definition id
997 * @param parameterDefinition the parameter definition
999 public static void addOutputParameterToTopologyTemplate(ServiceTemplate serviceTemplate,
1000 String parameterDefinitionId,
1001 ParameterDefinition parameterDefinition) {
1004 mdcDataDebugMessage.debugEntryMessage(null, null);
1006 if (Objects.isNull(serviceTemplate)) {
1007 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
1008 LoggerTragetServiceName.ADD_ENTITIES_TO_TOSCA, ErrorLevel.ERROR.name(),
1009 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.INVALID_ADD_ACTION);
1010 throw new CoreException(
1011 new InvalidAddActionNullEntityErrorBuilder("Topology Template Ouput Parameter",
1012 "Service Template").build());
1014 TopologyTemplate topologyTemplate = serviceTemplate.getTopology_template();
1015 if (Objects.isNull(topologyTemplate)) {
1016 topologyTemplate = new TopologyTemplate();
1017 serviceTemplate.setTopology_template(topologyTemplate);
1019 if (topologyTemplate.getOutputs() == null) {
1020 topologyTemplate.setOutputs(new HashMap<>());
1022 topologyTemplate.getOutputs().put(parameterDefinitionId, parameterDefinition);
1024 mdcDataDebugMessage.debugExitMessage(null, null);
1029 * Add requirement def to requirement def list.
1031 * @param requirementList requirement list
1032 * @param requirementDef added requirement def
1034 public static void addRequirementToList(List<Map<String, RequirementDefinition>> requirementList,
1035 Map<String, RequirementDefinition> requirementDef) {
1036 if (requirementDef == null) {
1039 if (requirementList == null) {
1040 requirementList = new ArrayList<Map<String, RequirementDefinition>>();
1043 for (Map.Entry<String, RequirementDefinition> entry : requirementDef.entrySet()) {
1044 CommonMethods.mergeEntryInList(entry.getKey(), entry.getValue(), requirementList);
1049 * get node template requirement.
1051 * @param nodeTemplate node template
1053 public static Map<String, RequirementAssignment> getNodeTemplateRequirements(
1054 NodeTemplate nodeTemplate) {
1055 mdcDataDebugMessage.debugEntryMessage(null, null);
1057 if (Objects.isNull(nodeTemplate)) {
1060 List<Map<String, RequirementAssignment>> templateRequirements = nodeTemplate.getRequirements();
1062 Map<String, RequirementAssignment> nodeTemplateRequirementsAssignment = new HashMap<>();
1063 if (CollectionUtils.isEmpty(templateRequirements)) {
1064 return nodeTemplateRequirementsAssignment;
1066 YamlUtil yamlUtil = new YamlUtil();
1067 for (Map<String, RequirementAssignment> requirementAssignmentMap : templateRequirements) {
1068 for (Map.Entry<String, RequirementAssignment> requirementEntry : requirementAssignmentMap
1070 RequirementAssignment requirementAssignment = (yamlUtil
1071 .yamlToObject(yamlUtil.objectToYaml(requirementEntry.getValue()),
1072 RequirementAssignment.class));
1073 nodeTemplateRequirementsAssignment
1074 .put(requirementEntry.getKey(), requirementAssignment);
1078 mdcDataDebugMessage.debugExitMessage(null, null);
1079 return nodeTemplateRequirementsAssignment;
1083 * Gets the list of requirements for the node template.
1085 * @param nodeTemplate the node template
1086 * @return the node template requirement list and null if the node has no requirements
1088 public static List<Map<String, RequirementAssignment>> getNodeTemplateRequirementList(
1089 NodeTemplate nodeTemplate) {
1090 mdcDataDebugMessage.debugEntryMessage(null, null);
1091 ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
1092 //Creating concrete objects
1093 List<Map<String, RequirementAssignment>> requirements = nodeTemplate.getRequirements();
1094 List<Map<String, RequirementAssignment>> concreteRequirementList = null;
1095 if (requirements != null) {
1096 concreteRequirementList = new ArrayList<>();
1097 ListIterator<Map<String, RequirementAssignment>> reqListIterator = requirements
1099 while (reqListIterator.hasNext()) {
1100 Map<String, RequirementAssignment> requirement = reqListIterator.next();
1101 Map<String, RequirementAssignment> concreteRequirement = new HashMap<>();
1102 for (Map.Entry<String, RequirementAssignment> reqEntry : requirement.entrySet()) {
1103 RequirementAssignment requirementAssignment = (toscaExtensionYamlUtil
1104 .yamlToObject(toscaExtensionYamlUtil.objectToYaml(reqEntry.getValue()),
1105 RequirementAssignment.class));
1106 concreteRequirement.put(reqEntry.getKey(), requirementAssignment);
1107 concreteRequirementList.add(concreteRequirement);
1108 reqListIterator.remove();
1111 requirements.clear();
1112 requirements.addAll(concreteRequirementList);
1113 nodeTemplate.setRequirements(requirements);
1115 mdcDataDebugMessage.debugExitMessage(null, null);
1116 return concreteRequirementList;
1120 * get requirement assignment from requirement assignment list by req key.
1122 * @param requirementsAssignmentList requirement defenition list
1123 * @param requirementKey requirement key
1125 public static Optional<List<RequirementAssignment>> getRequirementAssignment(
1126 List<Map<String, RequirementAssignment>> requirementsAssignmentList,
1127 String requirementKey) {
1129 mdcDataDebugMessage.debugEntryMessage(null, null);
1130 if (CollectionUtils.isEmpty(requirementsAssignmentList)) {
1131 return Optional.empty();
1134 List<RequirementAssignment> matchRequirementAssignmentList = new ArrayList<>();
1135 for (Map<String, RequirementAssignment> requirementMap : requirementsAssignmentList) {
1136 if (requirementMap.containsKey(requirementKey)) {
1137 YamlUtil yamlUtil = new YamlUtil();
1138 RequirementAssignment requirementAssignment = (yamlUtil
1139 .yamlToObject(yamlUtil.objectToYaml(requirementMap.get(requirementKey)),
1140 RequirementAssignment.class));
1141 matchRequirementAssignmentList.add(requirementAssignment);
1145 mdcDataDebugMessage.debugExitMessage(null, null);
1146 return Optional.ofNullable(matchRequirementAssignmentList);
1150 * remove requirement defenition from requirement defenition list by req key.
1152 * @param requirementsDefinitionList requirement defenition list
1153 * @param requirementKey requirement key
1155 public static void removeRequirementsDefinition(
1156 List<Map<String, RequirementDefinition>> requirementsDefinitionList,
1157 String requirementKey) {
1158 mdcDataDebugMessage.debugEntryMessage(null, null);
1159 if (requirementsDefinitionList == null) {
1163 List<Map<String, RequirementDefinition>> mapToBeRemoved = new ArrayList<>();
1164 for (Map<String, RequirementDefinition> reqMap : requirementsDefinitionList) {
1165 reqMap.remove(requirementKey);
1166 if (reqMap.isEmpty()) {
1167 mapToBeRemoved.add(reqMap);
1170 for (Map<String, RequirementDefinition> removeMap : mapToBeRemoved) {
1171 requirementsDefinitionList.remove(removeMap);
1174 mdcDataDebugMessage.debugExitMessage(null, null);
1178 * remove requirement assignment from requirement defenition list by req key.
1180 * @param requirementsAssignmentList requirement Assignment list
1181 * @param requirementKey requirement key
1183 public static void removeRequirementsAssignment(
1184 List<Map<String, RequirementAssignment>> requirementsAssignmentList,
1185 String requirementKey) {
1186 mdcDataDebugMessage.debugEntryMessage(null, null);
1187 if (requirementsAssignmentList == null) {
1191 List<Map<String, RequirementAssignment>> mapToBeRemoved = new ArrayList<>();
1192 for (Map<String, RequirementAssignment> reqMap : requirementsAssignmentList) {
1193 reqMap.remove(requirementKey);
1194 if (reqMap.isEmpty()) {
1195 mapToBeRemoved.add(reqMap);
1198 for (Map<String, RequirementAssignment> removeMap : mapToBeRemoved) {
1199 requirementsAssignmentList.remove(removeMap);
1202 mdcDataDebugMessage.debugExitMessage(null, null);
1207 * Remove requirement assignment.
1209 * @param nodeTemplate the node template
1210 * @param requirementKey the requirement key
1211 * @param requirementAssignmentToBeDeleted the requirement assignment to be deleted
1213 public static void removeRequirementAssignment(
1214 NodeTemplate nodeTemplate,
1215 String requirementKey,
1216 RequirementAssignment requirementAssignmentToBeDeleted) {
1217 mdcDataDebugMessage.debugEntryMessage(null, null);
1218 ToscaAnalyzerService toscaAnalyzerService = new ToscaAnalyzerServiceImpl();
1219 List<Map<String, RequirementAssignment>> nodeTemplateRequirements = nodeTemplate
1221 if (nodeTemplateRequirements == null) {
1225 Map<String, RequirementAssignment> mapToBeRemoved = new HashMap<>();
1226 ListIterator<Map<String, RequirementAssignment>> iter = nodeTemplateRequirements.listIterator();
1227 while (iter.hasNext()) {
1228 Map<String, RequirementAssignment> reqMap = iter.next();
1229 RequirementAssignment requirementAssignment = reqMap.get(requirementKey);
1230 if (requirementAssignment != null) {
1231 boolean isDesiredRequirementAssignment = toscaAnalyzerService
1232 .isDesiredRequirementAssignment(requirementAssignment,
1233 requirementAssignmentToBeDeleted.getCapability(),
1234 requirementAssignmentToBeDeleted.getNode(),
1235 requirementAssignmentToBeDeleted.getRelationship());
1236 if (isDesiredRequirementAssignment) {
1242 mdcDataDebugMessage.debugExitMessage(null, null);
1246 * Return the suffix of the input namespace
1247 * For an exmpale - for abc.sdf.vsrx, retrun vsrx
1249 * @param namespace namespace
1250 * @return String namespace suffix
1252 public static String getNamespaceSuffix(String namespace) {
1253 if (namespace == null) {
1256 String delimiterChar = ".";
1257 if (namespace.contains(delimiterChar)) {
1258 return namespace.substring(namespace.lastIndexOf(delimiterChar) + 1);
1264 * Return true if the input import exist in the input imports list.
1266 * @param imports namespace
1267 * @param importId namespace
1268 * @return true if exist, flase if not exist
1270 public static boolean isImportAddedToServiceTemplate(List<Map<String, Import>> imports,
1272 for (Map<String, Import> anImport : imports) {
1273 if (anImport.containsKey(importId)) {
1281 * Get output parameter according to the input outputParameterId.
1283 * @param serviceTemplate service template
1284 * @param outputParameterId output parameter id
1285 * @return ParameterDefinition - output parameter
1287 public static ParameterDefinition getOuputParameter(ServiceTemplate serviceTemplate,
1288 String outputParameterId) {
1289 mdcDataDebugMessage.debugEntryMessage(null, null);
1291 if (serviceTemplate == null
1292 || serviceTemplate.getTopology_template() == null
1293 || serviceTemplate.getTopology_template().getOutputs() == null) {
1297 mdcDataDebugMessage.debugExitMessage(null, null);
1298 return serviceTemplate.getTopology_template().getOutputs().get(outputParameterId);
1302 * Gets input parameters in a service template.
1304 * @param serviceTemplate the service template
1305 * @return the input parameters
1307 public static Map<String, ParameterDefinition> getInputParameters(ServiceTemplate
1309 if (serviceTemplate == null
1310 || serviceTemplate.getTopology_template() == null
1311 || serviceTemplate.getTopology_template().getInputs() == null) {
1314 return serviceTemplate.getTopology_template().getInputs();
1318 * Gets relationship templates in a service template.
1320 * @param serviceTemplate the service template
1321 * @return the relationship template
1323 public static Map<String, RelationshipTemplate> getRelationshipTemplates(ServiceTemplate
1325 if (serviceTemplate == null
1326 || serviceTemplate.getTopology_template() == null
1327 || serviceTemplate.getTopology_template().getRelationship_templates() == null) {
1330 return serviceTemplate.getTopology_template().getRelationship_templates();
1334 * Get property value according to the input propertyId.
1336 * @param nodeTemplate node template
1337 * @param propertyId property id
1338 * @return Object property Value
1340 public static Object getPropertyValue(NodeTemplate nodeTemplate,
1341 String propertyId) {
1342 mdcDataDebugMessage.debugEntryMessage(null, null);
1344 if (nodeTemplate == null
1345 || nodeTemplate.getProperties() == null) {
1349 mdcDataDebugMessage.debugExitMessage(null, null);
1350 return nodeTemplate.getProperties().get(propertyId);
1354 * Get node template properties according to the input node template id.
1356 * @param serviceTemplate service template
1357 * @param nodeTemplateId node template id
1358 * @return node template properties
1360 public static Map<String, Object> getNodeTemplateProperties(ServiceTemplate serviceTemplate,
1361 String nodeTemplateId) {
1362 mdcDataDebugMessage.debugEntryMessage(null, null);
1364 if (serviceTemplate == null
1365 || serviceTemplate.getTopology_template() == null
1366 || serviceTemplate.getTopology_template().getNode_templates() == null
1367 || serviceTemplate.getTopology_template().getNode_templates().get(nodeTemplateId) == null) {
1371 mdcDataDebugMessage.debugExitMessage(null, null);
1372 return serviceTemplate.getTopology_template().getNode_templates().get(nodeTemplateId)
1377 * Gets substitution mappings in a service template.
1379 * @param serviceTemplate the service template
1380 * @return the substitution mappings
1382 public static SubstitutionMapping getSubstitutionMappings(ServiceTemplate serviceTemplate) {
1383 mdcDataDebugMessage.debugEntryMessage(null, null);
1385 if (serviceTemplate == null
1386 || serviceTemplate.getTopology_template() == null
1387 || serviceTemplate.getTopology_template().getSubstitution_mappings() == null) {
1391 mdcDataDebugMessage.debugExitMessage(null, null);
1392 return serviceTemplate.getTopology_template().getSubstitution_mappings();
1397 * Compare two requirement assignment objects for equality.
1399 * @param first the first requirement assignement object
1400 * @param second the second requirement assignement object
1401 * @return true if objects are equal and false otherwise
1403 public static boolean compareRequirementAssignment(RequirementAssignment first,
1404 RequirementAssignment second) {
1405 if (first.getCapability().equals(second.getCapability())
1406 && first.getNode().equals(second.getNode())
1407 && first.getRelationship().equals(second.getRelationship())) {
1414 * Gets a deep copy clone of the input object.
1416 * @param <T> the type parameter
1417 * @param objectValue the object value
1418 * @param clazz the clazz
1419 * @return the cloned object
1421 public static <T> Object getClonedObject(Object objectValue, Class<T> clazz) {
1422 YamlUtil yamlUtil = new ToscaExtensionYamlUtil();
1423 Object clonedObjectValue;
1424 String objectToYaml = yamlUtil.objectToYaml(objectValue);
1425 clonedObjectValue = yamlUtil.yamlToObject(objectToYaml, clazz);
1426 return clonedObjectValue;
1430 * Gets a deep copy clone of the input object.
1432 * @param obj the object to be cloned
1433 * @return the cloned object
1435 public static Object getClonedObject(Object obj) {
1436 Object clonedObjectValue;
1439 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
1440 ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
1441 objectOutputStream.writeObject(obj);
1442 //Deserialize object
1443 ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream
1445 ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
1446 clonedObjectValue = objectInputStream.readObject();
1447 } catch (NotSerializableException ex) {
1448 logger.debug(ex.getMessage(), ex);
1449 return getClonedObject(obj, obj.getClass());
1450 } catch (IOException | ClassNotFoundException ex) {
1451 logger.debug(ex.getMessage(), ex);
1454 return clonedObjectValue;
1458 * Add substitution filtering property.
1460 * @param templateName the substitution service template name
1461 * @param nodeTemplate the node template
1462 * @param count the count
1464 public static void addSubstitutionFilteringProperty(String templateName,
1465 NodeTemplate nodeTemplate, int count) {
1466 Map<String, Object> serviceTemplateFilterPropertyValue = new HashMap<>();
1467 Map<String, Object> properties = nodeTemplate.getProperties();
1468 serviceTemplateFilterPropertyValue.put(ToscaConstants
1469 .SUBSTITUTE_SERVICE_TEMPLATE_PROPERTY_NAME, templateName);
1470 serviceTemplateFilterPropertyValue.put(ToscaConstants.COUNT_PROPERTY_NAME, count);
1471 properties.put(ToscaConstants.SERVICE_TEMPLATE_FILTER_PROPERTY_NAME,
1472 serviceTemplateFilterPropertyValue);
1473 nodeTemplate.setProperties(properties);
1477 * Adding binding requirement from port node template to compute node template.
1479 * @param computeNodeTemplateId compute node template id
1480 * @param portNodeTemplate port node template
1482 public static void addBindingReqFromPortToCompute(String computeNodeTemplateId,
1483 NodeTemplate portNodeTemplate) {
1486 mdcDataDebugMessage.debugEntryMessage(null, null);
1487 RequirementAssignment requirementAssignment = new RequirementAssignment();
1488 requirementAssignment.setCapability(ToscaCapabilityType.NATIVE_NETWORK_BINDABLE);
1489 requirementAssignment.setRelationship(ToscaRelationshipType.NATIVE_NETWORK_BINDS_TO);
1490 requirementAssignment.setNode(computeNodeTemplateId);
1491 addRequirementAssignment(portNodeTemplate, ToscaConstants.BINDING_REQUIREMENT_ID,
1492 requirementAssignment);
1493 mdcDataDebugMessage.debugExitMessage(null, null);
1496 public static SubstitutionMapping createSubstitutionTemplateSubMapping(
1498 NodeType substitutionNodeType,
1499 Map<String, Map<String, List<String>>> mapping) {
1500 mdcDataDebugMessage.debugEntryMessage(null, null);
1501 SubstitutionMapping substitutionMapping = new SubstitutionMapping();
1502 substitutionMapping.setNode_type(nodeTypeKey);
1503 substitutionMapping.setCapabilities(
1504 manageCapabilityMapping(substitutionNodeType.getCapabilities(), mapping.get("capability")));
1505 substitutionMapping.setRequirements(
1506 manageRequirementMapping(substitutionNodeType.getRequirements(),
1507 mapping.get("requirement")));
1509 mdcDataDebugMessage.debugExitMessage(null, null);
1510 return substitutionMapping;
1513 private static Map<String, List<String>> manageRequirementMapping(
1514 List<Map<String, RequirementDefinition>> requirementList,
1515 Map<String, List<String>> requirementSubstitutionMapping) {
1516 mdcDataDebugMessage.debugEntryMessage(null, null);
1518 if (requirementList == null) {
1521 Map<String, List<String>> requirementMapping = new HashMap<>();
1522 String requirementKey;
1523 List<String> requirementMap;
1524 for (Map<String, RequirementDefinition> requirementDefMap : requirementList) {
1525 for (Map.Entry<String, RequirementDefinition> entry : requirementDefMap.entrySet()) {
1526 requirementKey = entry.getKey();
1527 requirementMap = requirementSubstitutionMapping.get(requirementKey);
1528 requirementMapping.put(requirementKey, requirementMap);
1532 mdcDataDebugMessage.debugExitMessage(null, null);
1533 return requirementMapping;
1536 private static Map<String, List<String>> manageCapabilityMapping(
1537 Map<String, CapabilityDefinition> capabilities,
1538 Map<String, List<String>> capabilitySubstitutionMapping) {
1539 mdcDataDebugMessage.debugEntryMessage(null, null);
1541 if (capabilities == null) {
1542 mdcDataDebugMessage.debugExitMessage(null, null);
1546 Map<String, List<String>> capabilityMapping = new HashMap<>();
1547 String capabilityKey;
1548 List<String> capabilityMap;
1549 for (Map.Entry<String, CapabilityDefinition> entry : capabilities.entrySet()) {
1550 capabilityKey = entry.getKey();
1551 capabilityMap = capabilitySubstitutionMapping.get(capabilityKey);
1552 capabilityMapping.put(capabilityKey, capabilityMap);
1555 mdcDataDebugMessage.debugExitMessage(null, null);
1556 return capabilityMapping;
1559 public static void addSubstitutionNodeTypeRequirements(NodeType substitutionNodeType,
1560 List<Map<String, RequirementDefinition>>
1562 String templateName) {
1563 mdcDataDebugMessage.debugEntryMessage(null, null);
1565 if (requirementsList == null || requirementsList.size() == 0) {
1569 if (substitutionNodeType.getRequirements() == null) {
1570 substitutionNodeType.setRequirements(new ArrayList<>());
1573 for (Map<String, RequirementDefinition> requirementDef : requirementsList) {
1574 for (Map.Entry<String, RequirementDefinition> entry : requirementDef.entrySet()) {
1575 Map<String, RequirementDefinition> requirementMap = new HashMap<>();
1576 requirementMap.put(entry.getKey() + "_" + templateName, entry.getValue().clone());
1577 substitutionNodeType.getRequirements().add(requirementMap);
1581 mdcDataDebugMessage.debugExitMessage(null, null);
1584 public static boolean isNodeTemplateSectionMissingFromServiceTemplate(ServiceTemplate serviceTemplate){
1585 return Objects.isNull(serviceTemplate.getTopology_template() )
1586 || MapUtils.isEmpty(serviceTemplate.getTopology_template().getNode_templates());