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.CapabilityAssignment;
41 import org.openecomp.sdc.tosca.datatypes.model.CapabilityDefinition;
42 import org.openecomp.sdc.tosca.datatypes.model.CapabilityType;
43 import org.openecomp.sdc.tosca.datatypes.model.Constraint;
44 import org.openecomp.sdc.tosca.datatypes.model.EntrySchema;
45 import org.openecomp.sdc.tosca.datatypes.model.GroupDefinition;
46 import org.openecomp.sdc.tosca.datatypes.model.Import;
47 import org.openecomp.sdc.tosca.datatypes.model.NodeTemplate;
48 import org.openecomp.sdc.tosca.datatypes.model.NodeType;
49 import org.openecomp.sdc.tosca.datatypes.model.ParameterDefinition;
50 import org.openecomp.sdc.tosca.datatypes.model.PolicyDefinition;
51 import org.openecomp.sdc.tosca.datatypes.model.PropertyDefinition;
52 import org.openecomp.sdc.tosca.datatypes.model.RelationshipTemplate;
53 import org.openecomp.sdc.tosca.datatypes.model.RequirementAssignment;
54 import org.openecomp.sdc.tosca.datatypes.model.RequirementDefinition;
55 import org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate;
56 import org.openecomp.sdc.tosca.datatypes.model.Status;
57 import org.openecomp.sdc.tosca.datatypes.model.SubstitutionMapping;
58 import org.openecomp.sdc.tosca.datatypes.model.TopologyTemplate;
59 import org.openecomp.sdc.tosca.datatypes.model.heatextend.ParameterDefinitionExt;
60 import org.openecomp.sdc.tosca.errors.InvalidAddActionNullEntityErrorBuilder;
61 import org.openecomp.sdc.tosca.errors.InvalidRequirementAssignmentErrorBuilder;
62 import org.openecomp.sdc.tosca.services.impl.ToscaAnalyzerServiceImpl;
64 import java.io.ByteArrayInputStream;
65 import java.io.ByteArrayOutputStream;
66 import java.io.IOException;
67 import java.io.NotSerializableException;
68 import java.io.ObjectInputStream;
69 import java.io.ObjectOutputStream;
70 import java.util.ArrayList;
71 import java.util.Arrays;
72 import java.util.Collections;
73 import java.util.HashMap;
74 import java.util.List;
75 import java.util.ListIterator;
77 import java.util.Objects;
78 import java.util.Optional;
81 * The type Data model util.
83 public class DataModelUtil {
86 * Add substitution mapping.
89 private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
90 private static final Logger logger = LoggerFactory.getLogger(DataModelUtil.class);
93 * Add substitution mapping.
95 * @param serviceTemplate the service template
96 * @param substitutionMapping the substitution mapping
98 public static void addSubstitutionMapping(ServiceTemplate serviceTemplate,
99 SubstitutionMapping substitutionMapping) {
100 mdcDataDebugMessage.debugEntryMessage(null, null);
102 if (serviceTemplate == null) {
103 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
104 LoggerTragetServiceName.ADD_ENTITIES_TO_TOSCA, ErrorLevel.ERROR.name(),
105 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.INVALID_ADD_ACTION);
106 throw new CoreException(
107 new InvalidAddActionNullEntityErrorBuilder("Substitution Mapping", "Service Template")
111 if (serviceTemplate.getTopology_template() == null) {
112 serviceTemplate.setTopology_template(new TopologyTemplate());
114 serviceTemplate.getTopology_template().setSubstitution_mappings(substitutionMapping);
116 mdcDataDebugMessage.debugExitMessage(null, null);
120 * Add substitution mapping req.
122 * @param serviceTemplate the service template
123 * @param substitutionMappingRequirementId the substitution mapping requirement id
124 * @param substitutionMappingRequirementList the substitution mapping requirement list
126 public static void addSubstitutionMappingReq(ServiceTemplate serviceTemplate,
127 String substitutionMappingRequirementId,
128 List<String> substitutionMappingRequirementList) {
131 mdcDataDebugMessage.debugEntryMessage(null, null);
133 if (serviceTemplate == null) {
134 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
135 LoggerTragetServiceName.ADD_ENTITIES_TO_TOSCA, ErrorLevel.ERROR.name(),
136 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.INVALID_ADD_ACTION);
137 throw new CoreException(
138 new InvalidAddActionNullEntityErrorBuilder("Substitution Mapping Requirements",
139 "Service Template").build());
142 if (serviceTemplate.getTopology_template() == null) {
143 serviceTemplate.setTopology_template(new TopologyTemplate());
145 if (serviceTemplate.getTopology_template().getSubstitution_mappings() == null) {
146 serviceTemplate.getTopology_template().setSubstitution_mappings(new SubstitutionMapping());
148 if (serviceTemplate.getTopology_template().getSubstitution_mappings().getRequirements()
150 serviceTemplate.getTopology_template().getSubstitution_mappings()
151 .setRequirements(new HashMap<>());
154 serviceTemplate.getTopology_template().getSubstitution_mappings().getRequirements()
155 .put(substitutionMappingRequirementId, substitutionMappingRequirementList);
157 mdcDataDebugMessage.debugExitMessage(null, null);
161 * Add substitution mapping capability.
163 * @param serviceTemplate the service template
164 * @param substitutionMappingCapabilityId the substitution mapping capability id
165 * @param substitutionMappingCapabilityList the substitution mapping capability list
167 public static void addSubstitutionMappingCapability(ServiceTemplate serviceTemplate,
168 String substitutionMappingCapabilityId,
169 List<String> substitutionMappingCapabilityList) {
172 mdcDataDebugMessage.debugEntryMessage(null, null);
174 if (serviceTemplate == null) {
175 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
176 LoggerTragetServiceName.ADD_ENTITIES_TO_TOSCA, ErrorLevel.ERROR.name(),
177 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.INVALID_ADD_ACTION);
178 throw new CoreException(
179 new InvalidAddActionNullEntityErrorBuilder("Substitution Mapping Capabilities",
180 "Service Template").build());
183 if (serviceTemplate.getTopology_template() == null) {
184 serviceTemplate.setTopology_template(new TopologyTemplate());
186 if (serviceTemplate.getTopology_template().getSubstitution_mappings() == null) {
187 serviceTemplate.getTopology_template().setSubstitution_mappings(new SubstitutionMapping());
189 if (serviceTemplate.getTopology_template().getSubstitution_mappings().getCapabilities()
191 serviceTemplate.getTopology_template().getSubstitution_mappings()
192 .setCapabilities(new HashMap<>());
195 serviceTemplate.getTopology_template().getSubstitution_mappings().getCapabilities()
196 .putIfAbsent(substitutionMappingCapabilityId, substitutionMappingCapabilityList);
198 mdcDataDebugMessage.debugExitMessage(null, null);
201 public static Map<String, NodeTemplate> getNodeTemplates(ServiceTemplate serviceTemplate){
202 if (Objects.isNull(serviceTemplate)
203 || Objects.isNull(serviceTemplate.getTopology_template())
204 || MapUtils.isEmpty(serviceTemplate.getTopology_template().getNode_templates())){
205 return new HashMap<>();
208 return serviceTemplate.getTopology_template().getNode_templates();
214 * @param serviceTemplate the service template
215 * @param nodeTemplateId the node template id
216 * @param nodeTemplate the node template
218 public static void addNodeTemplate(ServiceTemplate serviceTemplate, String nodeTemplateId,
219 NodeTemplate nodeTemplate) {
222 mdcDataDebugMessage.debugEntryMessage(null, null);
224 if (serviceTemplate == null) {
225 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
226 LoggerTragetServiceName.ADD_ENTITIES_TO_TOSCA, ErrorLevel.ERROR.name(),
227 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.INVALID_ADD_ACTION);
228 throw new CoreException(
229 new InvalidAddActionNullEntityErrorBuilder("Node Template", "Service Template").build());
231 TopologyTemplate topologyTemplate = serviceTemplate.getTopology_template();
232 if (Objects.isNull(topologyTemplate)) {
233 topologyTemplate = new TopologyTemplate();
234 serviceTemplate.setTopology_template(topologyTemplate);
236 if (topologyTemplate.getNode_templates() == null) {
237 topologyTemplate.setNode_templates(new HashMap<>());
239 topologyTemplate.getNode_templates().put(nodeTemplateId, nodeTemplate);
241 mdcDataDebugMessage.debugExitMessage(null, null);
246 * Add capability def.
248 * @param nodeType the node type
249 * @param capabilityId the capability id
250 * @param capabilityDefinition the capability definition
252 public static void addCapabilityDef(NodeType nodeType, String capabilityId,
253 CapabilityDefinition capabilityDefinition) {
256 mdcDataDebugMessage.debugEntryMessage(null, null);
258 if (nodeType == null) {
259 throw new CoreException(
260 new InvalidAddActionNullEntityErrorBuilder("Capability Definition", "Node Type").build());
262 if (Objects.isNull(nodeType.getCapabilities())) {
263 nodeType.setCapabilities(new HashMap<>());
265 nodeType.getCapabilities().put(capabilityId, capabilityDefinition);
267 mdcDataDebugMessage.debugExitMessage(null, null);
271 * Add capabilities def to node type.
273 * @param nodeType the node type
274 * @param capabilities the capability definitions
276 public static void addNodeTypeCapabilitiesDef(NodeType nodeType,
277 Map<String, CapabilityDefinition> capabilities) {
278 mdcDataDebugMessage.debugEntryMessage(null, null);
280 if (capabilities == null || capabilities.entrySet().size() == 0) {
284 if (nodeType == null) {
285 throw new CoreException(
286 new InvalidAddActionNullEntityErrorBuilder("Capability Definition", "Node Type").build());
289 if (MapUtils.isEmpty(nodeType.getCapabilities())) {
290 nodeType.setCapabilities(new HashMap<>());
292 if (capabilities.size() > 0) {
293 nodeType.setCapabilities(new HashMap<>());
295 for (Map.Entry<String, CapabilityDefinition> entry : capabilities.entrySet()) {
296 nodeType.getCapabilities().put(entry.getKey(), entry.getValue());
299 mdcDataDebugMessage.debugExitMessage(null, null);
303 * Add policy definition.
305 * @param serviceTemplate the service template
306 * @param policyId the policy id
307 * @param policyDefinition the policy definition
309 public static void addPolicyDefinition(ServiceTemplate serviceTemplate, String policyId,
310 PolicyDefinition policyDefinition) {
313 mdcDataDebugMessage.debugEntryMessage(null, null);
315 if (serviceTemplate == null) {
316 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
317 LoggerTragetServiceName.ADD_ENTITIES_TO_TOSCA, ErrorLevel.ERROR.name(),
318 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.INVALID_ADD_ACTION);
319 throw new CoreException(
320 new InvalidAddActionNullEntityErrorBuilder("Policy Definition", "Service Template")
323 TopologyTemplate topologyTemplate = serviceTemplate.getTopology_template();
324 if (Objects.isNull(topologyTemplate)) {
325 topologyTemplate = new TopologyTemplate();
326 serviceTemplate.setTopology_template(topologyTemplate);
328 if (topologyTemplate.getPolicies() == null) {
329 topologyTemplate.setPolicies(new HashMap<>());
331 topologyTemplate.getPolicies().put(policyId, policyDefinition);
333 mdcDataDebugMessage.debugExitMessage(null, null);
339 * @param serviceTemplate the service template
340 * @param nodeTypeId the node type id
341 * @param nodeType the node type
343 public static void addNodeType(ServiceTemplate serviceTemplate, String nodeTypeId,
347 mdcDataDebugMessage.debugEntryMessage(null, null);
349 if (serviceTemplate == null) {
350 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
351 LoggerTragetServiceName.ADD_ENTITIES_TO_TOSCA, ErrorLevel.ERROR.name(),
352 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.INVALID_ADD_ACTION);
353 throw new CoreException(
354 new InvalidAddActionNullEntityErrorBuilder("Node Type", "Service Template").build());
356 if (serviceTemplate.getNode_types() == null) {
357 serviceTemplate.setNode_types(new HashMap<>());
359 serviceTemplate.getNode_types().put(nodeTypeId, nodeType);
361 mdcDataDebugMessage.debugExitMessage(null, null);
364 public static void removeNodeType(ServiceTemplate serviceTemplate,
366 if (serviceTemplate == null) {
367 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
368 LoggerTragetServiceName.ADD_ENTITIES_TO_TOSCA, ErrorLevel.ERROR.name(),
369 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.INVALID_ADD_ACTION);
370 throw new CoreException(
371 new InvalidAddActionNullEntityErrorBuilder("Node Type", "Service Template").build());
373 if (serviceTemplate.getNode_types() == null) {
374 serviceTemplate.setNode_types(new HashMap<>());
376 serviceTemplate.getNode_types().remove(nodeTypeId);
378 mdcDataDebugMessage.debugExitMessage(null, null);
382 * Add relationship template.
384 * @param serviceTemplate the service template
385 * @param relationshipTemplateId the relationship template id
386 * @param relationshipTemplate the relationship template
388 public static void addRelationshipTemplate(ServiceTemplate serviceTemplate,
389 String relationshipTemplateId,
390 RelationshipTemplate relationshipTemplate) {
393 mdcDataDebugMessage.debugEntryMessage(null, null);
395 if (serviceTemplate == null) {
396 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
397 LoggerTragetServiceName.ADD_ENTITIES_TO_TOSCA, ErrorLevel.ERROR.name(),
398 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.INVALID_ADD_ACTION);
399 throw new CoreException(
400 new InvalidAddActionNullEntityErrorBuilder("Relationship Template", "Service Template")
403 if (serviceTemplate.getTopology_template() == null) {
404 serviceTemplate.setTopology_template(new TopologyTemplate());
406 if (serviceTemplate.getTopology_template().getRelationship_templates() == null) {
407 serviceTemplate.getTopology_template().setRelationship_templates(new HashMap<>());
409 serviceTemplate.getTopology_template().getRelationship_templates()
410 .put(relationshipTemplateId, relationshipTemplate);
412 mdcDataDebugMessage.debugExitMessage(null, null);
416 * Add requirement assignment.
418 * @param nodeTemplate the node template
419 * @param requirementId the requirement id
420 * @param requirementAssignment the requirement assignment
422 public static void addRequirementAssignment(NodeTemplate nodeTemplate, String requirementId,
423 RequirementAssignment requirementAssignment) {
426 mdcDataDebugMessage.debugEntryMessage(null, null);
428 if (nodeTemplate == null) {
429 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
430 LoggerTragetServiceName.ADD_ENTITIES_TO_TOSCA, ErrorLevel.ERROR.name(),
431 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.INVALID_ADD_ACTION);
432 throw new CoreException(
433 new InvalidAddActionNullEntityErrorBuilder("Requirement Assignment", "Node Template")
436 if (requirementAssignment.getNode() == null) {
437 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
438 LoggerTragetServiceName.ADD_ENTITIES_TO_TOSCA, ErrorLevel.ERROR.name(),
439 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.INVALID_ADD_ACTION);
440 throw new CoreException(new InvalidRequirementAssignmentErrorBuilder(requirementId).build());
443 if (nodeTemplate.getRequirements() == null) {
444 nodeTemplate.setRequirements(new ArrayList<>());
446 Map<String, RequirementAssignment> requirement = new HashMap<>();
447 requirement.put(requirementId, requirementAssignment);
448 nodeTemplate.getRequirements().add(requirement);
450 mdcDataDebugMessage.debugExitMessage(null, null);
454 * Gets node template.
456 * @param serviceTemplate the service template
457 * @param nodeTemplateId the node template id
458 * @return the node template
460 public static NodeTemplate getNodeTemplate(ServiceTemplate serviceTemplate,
461 String nodeTemplateId) {
463 mdcDataDebugMessage.debugEntryMessage(null, null);
465 if (serviceTemplate == null
466 || serviceTemplate.getTopology_template() == null
467 || serviceTemplate.getTopology_template().getNode_templates() == null) {
471 mdcDataDebugMessage.debugExitMessage(null, null);
472 return serviceTemplate.getTopology_template().getNode_templates().get(nodeTemplateId);
478 * @param serviceTemplate the service template
479 * @param nodeTypeId the node type id
480 * @return the node type
482 public static NodeType getNodeType(ServiceTemplate serviceTemplate, String nodeTypeId) {
485 mdcDataDebugMessage.debugEntryMessage(null, null);
486 if (serviceTemplate == null || serviceTemplate.getNode_types() == null) {
490 mdcDataDebugMessage.debugExitMessage(null, null);
491 return serviceTemplate.getNode_types().get(nodeTypeId);
495 * Gets requirement definition.
497 * @param nodeType the node type
498 * @param requirementDefinitionId the requirement definition id
499 * @return the requirement definition
501 public static Optional<RequirementDefinition> getRequirementDefinition(
503 String requirementDefinitionId) {
506 mdcDataDebugMessage.debugEntryMessage(null, null);
508 if (nodeType == null || nodeType.getRequirements() == null || requirementDefinitionId == null) {
509 return Optional.empty();
511 for (Map<String, RequirementDefinition> reqMap : nodeType.getRequirements()) {
512 if (reqMap.containsKey(requirementDefinitionId)) {
513 return Optional.of(reqMap.get(requirementDefinitionId));
517 mdcDataDebugMessage.debugExitMessage(null, null);
518 return Optional.empty();
522 * get requirement defenition from requirement defenition list by req key.
524 * @param requirementsDefinitionList requirement defenition list
525 * @param requirementKey requirement key
527 public static Optional<RequirementDefinition> getRequirementDefinition(
528 List<Map<String, RequirementDefinition>> requirementsDefinitionList,
529 String requirementKey) {
530 mdcDataDebugMessage.debugEntryMessage(null, null);
531 if (CollectionUtils.isEmpty(requirementsDefinitionList)) {
532 return Optional.empty();
535 for (Map<String, RequirementDefinition> requirementMap : requirementsDefinitionList) {
536 if (requirementMap.containsKey(requirementKey)) {
537 mdcDataDebugMessage.debugExitMessage(null, null);
538 return Optional.of(requirementMap.get(requirementKey));
542 mdcDataDebugMessage.debugExitMessage(null, null);
543 return Optional.empty();
547 * Gets capability definition.
549 * @param nodeType the node type
550 * @param capabilityDefinitionId the capability definition id
551 * @return the capability definition
553 public static Optional<CapabilityDefinition> getCapabilityDefinition(
555 String capabilityDefinitionId) {
558 mdcDataDebugMessage.debugEntryMessage(null, null);
560 if (nodeType == null || nodeType.getCapabilities() == null || capabilityDefinitionId == null) {
561 return Optional.empty();
564 mdcDataDebugMessage.debugExitMessage(null, null);
565 return Optional.ofNullable(nodeType.getCapabilities().get(capabilityDefinitionId));
569 * Add group definition to topology template.
571 * @param serviceTemplate the service template
572 * @param groupName the group name
573 * @param group the group
575 public static void addGroupDefinitionToTopologyTemplate(ServiceTemplate serviceTemplate,
576 String groupName, GroupDefinition group) {
579 mdcDataDebugMessage.debugEntryMessage(null, null);
581 if (serviceTemplate == null) {
582 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
583 LoggerTragetServiceName.ADD_ENTITIES_TO_TOSCA, ErrorLevel.ERROR.name(),
584 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.INVALID_ADD_ACTION);
585 throw new CoreException(
586 new InvalidAddActionNullEntityErrorBuilder("Group Definition", "Service Template")
590 TopologyTemplate topologyTemplate = serviceTemplate.getTopology_template();
591 if (Objects.isNull(topologyTemplate)) {
592 topologyTemplate = new TopologyTemplate();
593 serviceTemplate.setTopology_template(topologyTemplate);
595 if (topologyTemplate.getGroups() == null) {
596 topologyTemplate.setGroups(new HashMap<>());
598 if (serviceTemplate.getTopology_template().getGroups() == null) {
599 Map<String, GroupDefinition> groups = new HashMap<>();
600 serviceTemplate.getTopology_template().setGroups(groups);
603 serviceTemplate.getTopology_template().getGroups().put(groupName, group);
604 mdcDataDebugMessage.debugExitMessage(null, null);
608 * Create property definition property definition.
610 * @param type the type
611 * @param description the description
612 * @param required the required
613 * @param constraints the constraints
614 * @param status the status
615 * @param entrySchema the entry schema
616 * @param defaultVal the default val
617 * @return the property definition
619 public static PropertyDefinition createPropertyDefinition(String type, String description,
621 List<Constraint> constraints,
623 EntrySchema entrySchema,
627 mdcDataDebugMessage.debugEntryMessage(null, null);
629 PropertyDefinition propDef = new PropertyDefinition();
630 propDef.setType(type);
631 propDef.setDescription(description);
632 propDef.setRequired(required);
633 propDef.setConstraints(constraints);
634 if (status != null) {
635 propDef.setStatus(status);
637 propDef.setEntry_schema(entrySchema);
638 propDef.set_default(defaultVal);
640 mdcDataDebugMessage.debugExitMessage(null, null);
645 * Create parameter definition property definition.
647 * @param type the type
648 * @param description the description
649 * @param value the value
650 * @param required the required
651 * @param constraints the constraints
652 * @param status the status
653 * @param entrySchema the entry schema
654 * @param defaultVal the default val
655 * @return the property definition
657 public static ParameterDefinition createParameterDefinition(String type, String description,
658 Object value, boolean required,
659 List<Constraint> constraints,
661 EntrySchema entrySchema,
665 mdcDataDebugMessage.debugEntryMessage(null, null);
667 ParameterDefinition paramDef = new ParameterDefinition();
668 paramDef.setType(type);
669 paramDef.setDescription(description);
670 paramDef.setValue(value);
671 paramDef.setRequired(required);
672 paramDef.setConstraints(constraints);
673 if (status != null) {
674 paramDef.setStatus(status);
676 paramDef.setEntry_schema(entrySchema == null ? null : entrySchema.clone());
677 paramDef.set_default(defaultVal);
679 mdcDataDebugMessage.debugExitMessage(null, null);
684 * Create requirement requirement definition.
686 * @param capability the capability
687 * @param node the node
688 * @param relationship the relationship
689 * @param occurrences the occurrences
690 * @return the requirement definition
692 public static RequirementDefinition createRequirement(String capability, String node,
693 String relationship, Object[] occurrences) {
696 mdcDataDebugMessage.debugEntryMessage(null, null);
698 RequirementDefinition requirementDefinition = new RequirementDefinition();
699 requirementDefinition.setCapability(capability);
700 requirementDefinition.setNode(node);
701 requirementDefinition.setRelationship(relationship);
702 if (occurrences != null) {
703 requirementDefinition.setOccurrences(occurrences);
706 mdcDataDebugMessage.debugExitMessage(null, null);
707 return requirementDefinition;
711 * Create attribute definition attribute definition.
713 * @param type the type
714 * @param description the description
715 * @param status the status
716 * @param entrySchema the entry schema
717 * @param defaultVal the default val
718 * @return the attribute definition
720 public static AttributeDefinition createAttributeDefinition(String type, String description,
722 EntrySchema entrySchema,
726 mdcDataDebugMessage.debugEntryMessage(null, null);
728 AttributeDefinition attributeDef = new AttributeDefinition();
729 attributeDef.setType(type);
731 if (description != null) {
732 attributeDef.setDescription(description);
734 if (status != null) {
735 attributeDef.setStatus(status);
737 attributeDef.setEntry_schema(entrySchema);
738 attributeDef.set_default(defaultVal);
740 mdcDataDebugMessage.debugExitMessage(null, null);
745 * Create valid values constraint constraint.
747 * @param values the values
748 * @return the constraint
750 public static Constraint createValidValuesConstraint(Object... values) {
753 mdcDataDebugMessage.debugEntryMessage(null, null);
755 Constraint validValues = new Constraint();
756 for (Object value : values) {
757 validValues.addValidValue(value);
760 mdcDataDebugMessage.debugExitMessage(null, null);
765 * Create metadata metadata.
767 * @param templateName the template name
768 * @param templateVersion the template version
769 * @param templateAuthor the template author
770 * @return the metadata
772 public static Map<String, String> createMetadata(String templateName, String templateVersion,
773 String templateAuthor) {
776 mdcDataDebugMessage.debugEntryMessage(null, null);
777 Map<String, String> metadata = new HashMap<>();
778 metadata.put(ToscaConstants.ST_METADATA_TEMPLATE_NAME, templateName);
779 metadata.put("template_version", templateVersion);
780 metadata.put("template_author", templateAuthor);
782 mdcDataDebugMessage.debugExitMessage(null, null);
787 * Create entry schema entry schema.
789 * @param type the type
790 * @param description the description
791 * @param constraints the constraints
792 * @return the entry schema
794 public static EntrySchema createEntrySchema(String type, String description,
795 List<Constraint> constraints) {
798 mdcDataDebugMessage.debugEntryMessage(null, null);
800 if(Objects.isNull(type) && Objects.isNull(description) && CollectionUtils.isEmpty(constraints)){
804 EntrySchema entrySchema = new EntrySchema();
805 entrySchema.setType(type);
806 entrySchema.setDescription(description);
807 entrySchema.setConstraints(constraints);
809 mdcDataDebugMessage.debugExitMessage(null, null);
814 * Create valid values constraints list list.
816 * @param values the values
819 public static List<Constraint> createValidValuesConstraintsList(String... values) {
822 mdcDataDebugMessage.debugEntryMessage(null, null);
824 List<Constraint> constraints;
825 Constraint validValues;
826 constraints = new ArrayList<>();
827 validValues = DataModelUtil.createValidValuesConstraint(values);
828 constraints.add(validValues);
830 mdcDataDebugMessage.debugExitMessage(null, null);
835 * Create greater or equal constrain constraint.
837 * @param value the value
838 * @return the constraint
840 public static Constraint createGreaterOrEqualConstrain(Object value) {
841 mdcDataDebugMessage.debugEntryMessage(null, null);
843 Constraint constraint = new Constraint();
844 constraint.setGreater_or_equal(value);
846 mdcDataDebugMessage.debugExitMessage(null, null);
851 * Gets constrain list.
853 * @param constrains the constrains
854 * @return the constrain list
856 public static List<Constraint> getConstrainList(Constraint... constrains) {
857 return Arrays.asList(constrains);
862 * Create get input property value from list parameter map.
864 * @param inputPropertyListName the input property list name
865 * @param indexInTheList the index in the list
866 * @param nestedPropertyName the nested property name
869 public static Map createGetInputPropertyValueFromListParameter(String inputPropertyListName,
871 String... nestedPropertyName) {
874 mdcDataDebugMessage.debugEntryMessage(null, null);
876 List propertyList = new ArrayList<>();
877 propertyList.add(inputPropertyListName);
878 propertyList.add(indexInTheList);
879 if (nestedPropertyName != null) {
880 Collections.addAll(propertyList, nestedPropertyName);
882 Map getInputProperty = new HashMap<>();
883 getInputProperty.put(ToscaFunctions.GET_INPUT.getDisplayName(), propertyList);
885 mdcDataDebugMessage.debugExitMessage(null, null);
886 return getInputProperty;
890 * Convert property def to parameter def parameter definition ext.
892 * @param propertyDefinition the property definition
893 * @return the parameter definition ext
895 public static ParameterDefinitionExt convertPropertyDefToParameterDef(
896 PropertyDefinition propertyDefinition) {
899 mdcDataDebugMessage.debugEntryMessage(null, null);
901 if (propertyDefinition == null) {
905 ParameterDefinitionExt parameterDefinition = new ParameterDefinitionExt();
906 parameterDefinition.setType(propertyDefinition.getType());
907 parameterDefinition.setDescription(propertyDefinition.getDescription());
908 parameterDefinition.setRequired(propertyDefinition.getRequired());
909 parameterDefinition.set_default(propertyDefinition.get_default());
910 parameterDefinition.setStatus(propertyDefinition.getStatus());
911 parameterDefinition.setConstraints(propertyDefinition.getConstraints());
912 parameterDefinition.setEntry_schema(Objects.isNull(propertyDefinition.getEntry_schema()) ? null
913 : propertyDefinition.getEntry_schema().clone());
914 parameterDefinition.setHidden(false);
915 parameterDefinition.setImmutable(false);
917 mdcDataDebugMessage.debugExitMessage(null, null);
918 return parameterDefinition;
922 * Convert attribute def to parameter def parameter definition ext.
924 * @param attributeDefinition the attribute definition
925 * @param outputValue the output value
926 * @return the parameter definition ext
928 public static ParameterDefinitionExt convertAttributeDefToParameterDef(
929 AttributeDefinition attributeDefinition, Map<String, List> outputValue) {
932 mdcDataDebugMessage.debugEntryMessage(null, null);
934 if (attributeDefinition == null) {
937 ParameterDefinitionExt parameterDefinition = new ParameterDefinitionExt();
938 parameterDefinition.setDescription(attributeDefinition.getDescription());
939 parameterDefinition.setValue(outputValue);
941 mdcDataDebugMessage.debugExitMessage(null, null);
942 return parameterDefinition;
946 * Convert capability type to capability definition capability definition.
948 * @param capabilityTypeId the capability type id
949 * @param capabilityType the capability type
950 * @param properties the properties
951 * @return the capability definition
953 public static CapabilityDefinition convertCapabilityTypeToCapabilityDefinition(
954 String capabilityTypeId, CapabilityType capabilityType, Map<String, Object> properties) {
957 mdcDataDebugMessage.debugEntryMessage(null, null);
959 CapabilityDefinition capabilityDefinition = new CapabilityDefinition();
960 capabilityDefinition.setAttributes(cloneAttributeDefinitionMap(capabilityType.getAttributes()));
961 capabilityDefinition.setProperties(clonePropertyDefinitionMap(capabilityType.getProperties()));
962 capabilityDefinition.setDescription(capabilityType.getDescription());
963 capabilityDefinition.setType(capabilityTypeId);
965 capabilityDefinition.getProperties()
968 .filter(entry -> properties.containsKey(entry.getKey()))
969 .forEach(entry -> entry.getValue()
970 .set_default(properties.get(entry.getKey())));
972 mdcDataDebugMessage.debugExitMessage(null, null);
973 return capabilityDefinition;
977 * Clone property definition map map.
979 * @param propertyDefinitionMap the property definition map
982 public static Map clonePropertyDefinitionMap(
983 Map<String, PropertyDefinition> propertyDefinitionMap) {
986 mdcDataDebugMessage.debugEntryMessage(null, null);
988 Map outMap = new HashMap<>();
989 for (String propertyDefKey : propertyDefinitionMap.keySet()) {
990 PropertyDefinition propertyDefValue = propertyDefinitionMap.get(propertyDefKey);
991 outMap.put(new String(propertyDefKey), propertyDefValue.clone());
994 mdcDataDebugMessage.debugExitMessage(null, null);
999 * Clone attribute definition map map.
1001 * @param attributeDefinitionMap the attribute definition map
1004 public static Map cloneAttributeDefinitionMap(
1005 Map<String, AttributeDefinition> attributeDefinitionMap) {
1008 mdcDataDebugMessage.debugEntryMessage(null, null);
1010 Map outMap = new HashMap<>();
1011 for (String attributeDefKey : attributeDefinitionMap.keySet()) {
1012 AttributeDefinition attributeDefinition = attributeDefinitionMap.get(attributeDefKey);
1013 outMap.put(new String(attributeDefKey), attributeDefinition.clone());
1016 mdcDataDebugMessage.debugExitMessage(null, null);
1020 public static boolean isNodeTemplate(String entryId, ServiceTemplate serviceTemplate) {
1021 return serviceTemplate.getTopology_template().getNode_templates() != null
1022 && serviceTemplate.getTopology_template().getNode_templates().get(entryId) != null;
1026 * Add Input parameter.
1028 * @param serviceTemplate the service template
1029 * @param parameterDefinitionId the parameter definition id
1030 * @param parameterDefinition the parameter definition
1032 public static void addInputParameterToTopologyTemplate(ServiceTemplate serviceTemplate,
1033 String parameterDefinitionId,
1034 ParameterDefinition parameterDefinition) {
1037 mdcDataDebugMessage.debugEntryMessage(null, null);
1039 if (Objects.isNull(serviceTemplate)) {
1040 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
1041 LoggerTragetServiceName.ADD_ENTITIES_TO_TOSCA, ErrorLevel.ERROR.name(),
1042 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.INVALID_ADD_ACTION);
1043 throw new CoreException(
1044 new InvalidAddActionNullEntityErrorBuilder("Topology Template Input Parameter",
1045 "Service Template").build());
1047 TopologyTemplate topologyTemplate = serviceTemplate.getTopology_template();
1048 if (Objects.isNull(topologyTemplate)) {
1049 topologyTemplate = new TopologyTemplate();
1050 serviceTemplate.setTopology_template(topologyTemplate);
1052 if (topologyTemplate.getInputs() == null) {
1053 topologyTemplate.setInputs(new HashMap<>());
1055 topologyTemplate.getInputs().put(parameterDefinitionId, parameterDefinition);
1057 mdcDataDebugMessage.debugExitMessage(null, null);
1062 * Add Output parameter.
1064 * @param serviceTemplate the service template
1065 * @param parameterDefinitionId the parameter definition id
1066 * @param parameterDefinition the parameter definition
1068 public static void addOutputParameterToTopologyTemplate(ServiceTemplate serviceTemplate,
1069 String parameterDefinitionId,
1070 ParameterDefinition parameterDefinition) {
1073 mdcDataDebugMessage.debugEntryMessage(null, null);
1075 if (Objects.isNull(serviceTemplate)) {
1076 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
1077 LoggerTragetServiceName.ADD_ENTITIES_TO_TOSCA, ErrorLevel.ERROR.name(),
1078 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.INVALID_ADD_ACTION);
1079 throw new CoreException(
1080 new InvalidAddActionNullEntityErrorBuilder("Topology Template Ouput Parameter",
1081 "Service Template").build());
1083 TopologyTemplate topologyTemplate = serviceTemplate.getTopology_template();
1084 if (Objects.isNull(topologyTemplate)) {
1085 topologyTemplate = new TopologyTemplate();
1086 serviceTemplate.setTopology_template(topologyTemplate);
1088 if (topologyTemplate.getOutputs() == null) {
1089 topologyTemplate.setOutputs(new HashMap<>());
1091 topologyTemplate.getOutputs().put(parameterDefinitionId, parameterDefinition);
1093 mdcDataDebugMessage.debugExitMessage(null, null);
1098 * Add requirement def to requirement def list.
1100 * @param requirementList requirement list
1101 * @param requirementDef added requirement def
1103 public static void addRequirementToList(List<Map<String, RequirementDefinition>> requirementList,
1104 Map<String, RequirementDefinition> requirementDef) {
1105 if (requirementDef == null) {
1108 if (requirementList == null) {
1109 requirementList = new ArrayList<Map<String, RequirementDefinition>>();
1112 for (Map.Entry<String, RequirementDefinition> entry : requirementDef.entrySet()) {
1113 CommonMethods.mergeEntryInList(entry.getKey(), entry.getValue(), requirementList);
1118 * get node template requirement.
1120 * @param nodeTemplate node template
1122 public static Map<String, RequirementAssignment> getNodeTemplateRequirements(
1123 NodeTemplate nodeTemplate) {
1124 mdcDataDebugMessage.debugEntryMessage(null, null);
1126 if (Objects.isNull(nodeTemplate)) {
1129 List<Map<String, RequirementAssignment>> templateRequirements = nodeTemplate.getRequirements();
1131 Map<String, RequirementAssignment> nodeTemplateRequirementsAssignment = new HashMap<>();
1132 if (CollectionUtils.isEmpty(templateRequirements)) {
1133 return nodeTemplateRequirementsAssignment;
1135 YamlUtil yamlUtil = new YamlUtil();
1136 for (Map<String, RequirementAssignment> requirementAssignmentMap : templateRequirements) {
1137 for (Map.Entry<String, RequirementAssignment> requirementEntry : requirementAssignmentMap
1139 RequirementAssignment requirementAssignment = (yamlUtil
1140 .yamlToObject(yamlUtil.objectToYaml(requirementEntry.getValue()),
1141 RequirementAssignment.class));
1142 nodeTemplateRequirementsAssignment
1143 .put(requirementEntry.getKey(), requirementAssignment);
1147 mdcDataDebugMessage.debugExitMessage(null, null);
1148 return nodeTemplateRequirementsAssignment;
1152 * Gets the list of requirements for the node template.
1154 * @param nodeTemplate the node template
1155 * @return the node template requirement list and null if the node has no requirements
1157 public static List<Map<String, RequirementAssignment>> getNodeTemplateRequirementList(
1158 NodeTemplate nodeTemplate) {
1159 mdcDataDebugMessage.debugEntryMessage(null, null);
1160 ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
1161 //Creating concrete objects
1162 List<Map<String, RequirementAssignment>> requirements = nodeTemplate.getRequirements();
1163 List<Map<String, RequirementAssignment>> concreteRequirementList = null;
1164 if (requirements != null) {
1165 concreteRequirementList = new ArrayList<>();
1166 ListIterator<Map<String, RequirementAssignment>> reqListIterator = requirements
1168 while (reqListIterator.hasNext()) {
1169 Map<String, RequirementAssignment> requirement = reqListIterator.next();
1170 Map<String, RequirementAssignment> concreteRequirement = new HashMap<>();
1171 for (Map.Entry<String, RequirementAssignment> reqEntry : requirement.entrySet()) {
1172 RequirementAssignment requirementAssignment = (toscaExtensionYamlUtil
1173 .yamlToObject(toscaExtensionYamlUtil.objectToYaml(reqEntry.getValue()),
1174 RequirementAssignment.class));
1175 concreteRequirement.put(reqEntry.getKey(), requirementAssignment);
1176 concreteRequirementList.add(concreteRequirement);
1177 reqListIterator.remove();
1180 requirements.clear();
1181 requirements.addAll(concreteRequirementList);
1182 nodeTemplate.setRequirements(requirements);
1184 mdcDataDebugMessage.debugExitMessage(null, null);
1185 return concreteRequirementList;
1189 * get requirement assignment from requirement assignment list by req key.
1191 * @param requirementsAssignmentList requirement defenition list
1192 * @param requirementKey requirement key
1194 public static Optional<List<RequirementAssignment>> getRequirementAssignment(
1195 List<Map<String, RequirementAssignment>> requirementsAssignmentList,
1196 String requirementKey) {
1198 mdcDataDebugMessage.debugEntryMessage(null, null);
1199 if (CollectionUtils.isEmpty(requirementsAssignmentList)) {
1200 return Optional.empty();
1203 List<RequirementAssignment> matchRequirementAssignmentList = new ArrayList<>();
1204 for (Map<String, RequirementAssignment> requirementMap : requirementsAssignmentList) {
1205 if (requirementMap.containsKey(requirementKey)) {
1206 YamlUtil yamlUtil = new YamlUtil();
1207 RequirementAssignment requirementAssignment = (yamlUtil
1208 .yamlToObject(yamlUtil.objectToYaml(requirementMap.get(requirementKey)),
1209 RequirementAssignment.class));
1210 matchRequirementAssignmentList.add(requirementAssignment);
1214 mdcDataDebugMessage.debugExitMessage(null, null);
1215 return Optional.ofNullable(matchRequirementAssignmentList);
1219 * remove requirement defenition from requirement defenition list by req key.
1221 * @param requirementsDefinitionList requirement defenition list
1222 * @param requirementKey requirement key
1224 public static void removeRequirementsDefinition(
1225 List<Map<String, RequirementDefinition>> requirementsDefinitionList,
1226 String requirementKey) {
1227 mdcDataDebugMessage.debugEntryMessage(null, null);
1228 if (requirementsDefinitionList == null) {
1232 List<Map<String, RequirementDefinition>> mapToBeRemoved = new ArrayList<>();
1233 for (Map<String, RequirementDefinition> reqMap : requirementsDefinitionList) {
1234 reqMap.remove(requirementKey);
1235 if (reqMap.isEmpty()) {
1236 mapToBeRemoved.add(reqMap);
1239 for (Map<String, RequirementDefinition> removeMap : mapToBeRemoved) {
1240 requirementsDefinitionList.remove(removeMap);
1243 mdcDataDebugMessage.debugExitMessage(null, null);
1247 * remove requirement assignment from requirement defenition list by req key.
1249 * @param requirementsAssignmentList requirement Assignment list
1250 * @param requirementKey requirement key
1252 public static void removeRequirementsAssignment(
1253 List<Map<String, RequirementAssignment>> requirementsAssignmentList,
1254 String requirementKey) {
1255 mdcDataDebugMessage.debugEntryMessage(null, null);
1256 if (requirementsAssignmentList == null) {
1260 List<Map<String, RequirementAssignment>> mapToBeRemoved = new ArrayList<>();
1261 for (Map<String, RequirementAssignment> reqMap : requirementsAssignmentList) {
1262 reqMap.remove(requirementKey);
1263 if (reqMap.isEmpty()) {
1264 mapToBeRemoved.add(reqMap);
1267 for (Map<String, RequirementAssignment> removeMap : mapToBeRemoved) {
1268 requirementsAssignmentList.remove(removeMap);
1271 mdcDataDebugMessage.debugExitMessage(null, null);
1276 * Remove requirement assignment.
1278 * @param nodeTemplate the node template
1279 * @param requirementKey the requirement key
1280 * @param requirementAssignmentToBeDeleted the requirement assignment to be deleted
1282 public static void removeRequirementAssignment(
1283 NodeTemplate nodeTemplate,
1284 String requirementKey,
1285 RequirementAssignment requirementAssignmentToBeDeleted) {
1286 mdcDataDebugMessage.debugEntryMessage(null, null);
1287 ToscaAnalyzerService toscaAnalyzerService = new ToscaAnalyzerServiceImpl();
1288 List<Map<String, RequirementAssignment>> nodeTemplateRequirements = nodeTemplate
1290 if (nodeTemplateRequirements == null) {
1294 Map<String, RequirementAssignment> mapToBeRemoved = new HashMap<>();
1295 ListIterator<Map<String, RequirementAssignment>> iter = nodeTemplateRequirements.listIterator();
1296 while (iter.hasNext()) {
1297 Map<String, RequirementAssignment> reqMap = iter.next();
1298 RequirementAssignment requirementAssignment = reqMap.get(requirementKey);
1299 if (requirementAssignment != null) {
1300 boolean isDesiredRequirementAssignment = toscaAnalyzerService
1301 .isDesiredRequirementAssignment(requirementAssignment,
1302 requirementAssignmentToBeDeleted.getCapability(),
1303 requirementAssignmentToBeDeleted.getNode(),
1304 requirementAssignmentToBeDeleted.getRelationship());
1305 if (isDesiredRequirementAssignment) {
1311 mdcDataDebugMessage.debugExitMessage(null, null);
1315 * Return the suffix of the input namespace
1316 * For an exmpale - for abc.sdf.vsrx, retrun vsrx
1318 * @param namespace namespace
1319 * @return String namespace suffix
1321 public static String getNamespaceSuffix(String namespace) {
1322 if (namespace == null) {
1325 String delimiterChar = ".";
1326 if (namespace.contains(delimiterChar)) {
1327 return namespace.substring(namespace.lastIndexOf(delimiterChar) + 1);
1333 * Return true if the input import exist in the input imports list.
1335 * @param imports namespace
1336 * @param importId namespace
1337 * @return true if exist, flase if not exist
1339 public static boolean isImportAddedToServiceTemplate(List<Map<String, Import>> imports,
1341 for (Map<String, Import> anImport : imports) {
1342 if (anImport.containsKey(importId)) {
1350 * Get output parameter according to the input outputParameterId.
1352 * @param serviceTemplate service template
1353 * @param outputParameterId output parameter id
1354 * @return ParameterDefinition - output parameter
1356 public static ParameterDefinition getOuputParameter(ServiceTemplate serviceTemplate,
1357 String outputParameterId) {
1358 mdcDataDebugMessage.debugEntryMessage(null, null);
1360 if (serviceTemplate == null
1361 || serviceTemplate.getTopology_template() == null
1362 || serviceTemplate.getTopology_template().getOutputs() == null) {
1366 mdcDataDebugMessage.debugExitMessage(null, null);
1367 return serviceTemplate.getTopology_template().getOutputs().get(outputParameterId);
1371 * Gets input parameters in a service template.
1373 * @param serviceTemplate the service template
1374 * @return the input parameters
1376 public static Map<String, ParameterDefinition> getInputParameters(ServiceTemplate
1378 if (serviceTemplate == null
1379 || serviceTemplate.getTopology_template() == null
1380 || serviceTemplate.getTopology_template().getInputs() == null) {
1383 return serviceTemplate.getTopology_template().getInputs();
1387 * Gets relationship templates in a service template.
1389 * @param serviceTemplate the service template
1390 * @return the relationship template
1392 public static Map<String, RelationshipTemplate> getRelationshipTemplates(ServiceTemplate
1394 if (serviceTemplate == null
1395 || serviceTemplate.getTopology_template() == null
1396 || serviceTemplate.getTopology_template().getRelationship_templates() == null) {
1399 return serviceTemplate.getTopology_template().getRelationship_templates();
1403 * Get property value according to the input propertyId.
1405 * @param nodeTemplate node template
1406 * @param propertyId property id
1407 * @return Object property Value
1409 public static Object getPropertyValue(NodeTemplate nodeTemplate,
1410 String propertyId) {
1411 mdcDataDebugMessage.debugEntryMessage(null, null);
1413 if (nodeTemplate == null
1414 || nodeTemplate.getProperties() == null) {
1418 mdcDataDebugMessage.debugExitMessage(null, null);
1419 return nodeTemplate.getProperties().get(propertyId);
1423 * Get node template properties according to the input node template id.
1425 * @param serviceTemplate service template
1426 * @param nodeTemplateId node template id
1427 * @return node template properties
1429 public static Map<String, Object> getNodeTemplateProperties(ServiceTemplate serviceTemplate,
1430 String nodeTemplateId) {
1431 mdcDataDebugMessage.debugEntryMessage(null, null);
1433 if (serviceTemplate == null
1434 || serviceTemplate.getTopology_template() == null
1435 || serviceTemplate.getTopology_template().getNode_templates() == null
1436 || serviceTemplate.getTopology_template().getNode_templates().get(nodeTemplateId) == null) {
1440 mdcDataDebugMessage.debugExitMessage(null, null);
1441 return serviceTemplate.getTopology_template().getNode_templates().get(nodeTemplateId)
1446 * Gets substitution mappings in a service template.
1448 * @param serviceTemplate the service template
1449 * @return the substitution mappings
1451 public static SubstitutionMapping getSubstitutionMappings(ServiceTemplate serviceTemplate) {
1452 mdcDataDebugMessage.debugEntryMessage(null, null);
1454 if (serviceTemplate == null
1455 || serviceTemplate.getTopology_template() == null
1456 || serviceTemplate.getTopology_template().getSubstitution_mappings() == null) {
1460 mdcDataDebugMessage.debugExitMessage(null, null);
1461 return serviceTemplate.getTopology_template().getSubstitution_mappings();
1466 * Compare two requirement assignment objects for equality.
1468 * @param first the first requirement assignement object
1469 * @param second the second requirement assignement object
1470 * @return true if objects are equal and false otherwise
1472 public static boolean compareRequirementAssignment(RequirementAssignment first,
1473 RequirementAssignment second) {
1474 if (first.getCapability().equals(second.getCapability())
1475 && first.getNode().equals(second.getNode())
1476 && first.getRelationship().equals(second.getRelationship())) {
1483 * Gets a deep copy clone of the input object.
1485 * @param <T> the type parameter
1486 * @param objectValue the object value
1487 * @param clazz the clazz
1488 * @return the cloned object
1490 public static <T> Object getClonedObject(Object objectValue, Class<T> clazz) {
1491 YamlUtil yamlUtil = new ToscaExtensionYamlUtil();
1492 Object clonedObjectValue;
1493 String objectToYaml = yamlUtil.objectToYaml(objectValue);
1494 clonedObjectValue = yamlUtil.yamlToObject(objectToYaml, clazz);
1495 return clonedObjectValue;
1499 * Gets a deep copy clone of the input object.
1501 * @param obj the object to be cloned
1502 * @return the cloned object
1504 public static Object getClonedObject(Object obj) {
1505 Object clonedObjectValue;
1508 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
1509 ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
1510 objectOutputStream.writeObject(obj);
1511 //Deserialize object
1512 ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream
1514 ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
1515 clonedObjectValue = objectInputStream.readObject();
1516 } catch (NotSerializableException ex) {
1517 logger.debug(ex.getMessage(), ex);
1518 return getClonedObject(obj, obj.getClass());
1519 } catch (IOException | ClassNotFoundException ex) {
1520 logger.debug(ex.getMessage(), ex);
1523 return clonedObjectValue;
1527 * Add substitution filtering property.
1529 * @param templateName the substitution service template name
1530 * @param nodeTemplate the node template
1531 * @param count the count
1533 public static void addSubstitutionFilteringProperty(String templateName,
1534 NodeTemplate nodeTemplate, int count) {
1535 Map<String, Object> serviceTemplateFilterPropertyValue = new HashMap<>();
1536 Map<String, Object> properties = nodeTemplate.getProperties();
1537 serviceTemplateFilterPropertyValue.put(ToscaConstants
1538 .SUBSTITUTE_SERVICE_TEMPLATE_PROPERTY_NAME, templateName);
1539 serviceTemplateFilterPropertyValue.put(ToscaConstants.COUNT_PROPERTY_NAME, count);
1540 properties.put(ToscaConstants.SERVICE_TEMPLATE_FILTER_PROPERTY_NAME,
1541 serviceTemplateFilterPropertyValue);
1542 nodeTemplate.setProperties(properties);
1546 * Adding binding requirement from port node template to compute node template.
1548 * @param computeNodeTemplateId compute node template id
1549 * @param portNodeTemplate port node template
1551 public static void addBindingReqFromPortToCompute(String computeNodeTemplateId,
1552 NodeTemplate portNodeTemplate) {
1555 mdcDataDebugMessage.debugEntryMessage(null, null);
1556 RequirementAssignment requirementAssignment = new RequirementAssignment();
1557 requirementAssignment.setCapability(ToscaCapabilityType.NATIVE_NETWORK_BINDABLE);
1558 requirementAssignment.setRelationship(ToscaRelationshipType.NATIVE_NETWORK_BINDS_TO);
1559 requirementAssignment.setNode(computeNodeTemplateId);
1560 addRequirementAssignment(portNodeTemplate, ToscaConstants.BINDING_REQUIREMENT_ID,
1561 requirementAssignment);
1562 mdcDataDebugMessage.debugExitMessage(null, null);
1565 public static SubstitutionMapping createSubstitutionTemplateSubMapping(
1567 NodeType substitutionNodeType,
1568 Map<String, Map<String, List<String>>> mapping) {
1569 mdcDataDebugMessage.debugEntryMessage(null, null);
1570 SubstitutionMapping substitutionMapping = new SubstitutionMapping();
1571 substitutionMapping.setNode_type(nodeTypeKey);
1572 substitutionMapping.setCapabilities(
1573 manageCapabilityMapping(substitutionNodeType.getCapabilities(), mapping.get("capability")));
1574 substitutionMapping.setRequirements(
1575 manageRequirementMapping(substitutionNodeType.getRequirements(),
1576 mapping.get("requirement")));
1578 mdcDataDebugMessage.debugExitMessage(null, null);
1579 return substitutionMapping;
1583 * Add node template capability.
1585 * @param nodeTemplate the node template
1586 * @param capabilityId the capability id
1587 * @param capabilityProperties the capability properties
1588 * @param capabilityAttributes the capability attributes
1590 public static void addNodeTemplateCapability(NodeTemplate nodeTemplate, String capabilityId,
1591 Map<String, Object> capabilityProperties,
1592 Map<String, Object> capabilityAttributes) {
1593 List<Map<String, CapabilityAssignment>> capabilities = nodeTemplate.getCapabilities();
1594 if (Objects.isNull(capabilities)) {
1595 capabilities = new ArrayList<>();
1597 CapabilityAssignment capabilityAssignment = new CapabilityAssignment();
1598 capabilityAssignment.setProperties(capabilityProperties);
1599 capabilityAssignment.setAttributes(capabilityAttributes);
1600 Map<String, CapabilityAssignment> nodeTemplateCapability = new HashMap<>();
1601 nodeTemplateCapability.put(capabilityId, capabilityAssignment);
1602 capabilities.add(nodeTemplateCapability);
1603 nodeTemplate.setCapabilities(capabilities);
1606 private static Map<String, List<String>> manageRequirementMapping(
1607 List<Map<String, RequirementDefinition>> requirementList,
1608 Map<String, List<String>> requirementSubstitutionMapping) {
1609 mdcDataDebugMessage.debugEntryMessage(null, null);
1611 if (requirementList == null) {
1614 Map<String, List<String>> requirementMapping = new HashMap<>();
1615 String requirementKey;
1616 List<String> requirementMap;
1617 for (Map<String, RequirementDefinition> requirementDefMap : requirementList) {
1618 for (Map.Entry<String, RequirementDefinition> entry : requirementDefMap.entrySet()) {
1619 requirementKey = entry.getKey();
1620 requirementMap = requirementSubstitutionMapping.get(requirementKey);
1621 requirementMapping.put(requirementKey, requirementMap);
1625 mdcDataDebugMessage.debugExitMessage(null, null);
1626 return requirementMapping;
1629 private static Map<String, List<String>> manageCapabilityMapping(
1630 Map<String, CapabilityDefinition> capabilities,
1631 Map<String, List<String>> capabilitySubstitutionMapping) {
1632 mdcDataDebugMessage.debugEntryMessage(null, null);
1634 if (capabilities == null) {
1635 mdcDataDebugMessage.debugExitMessage(null, null);
1639 Map<String, List<String>> capabilityMapping = new HashMap<>();
1640 String capabilityKey;
1641 List<String> capabilityMap;
1642 for (Map.Entry<String, CapabilityDefinition> entry : capabilities.entrySet()) {
1643 capabilityKey = entry.getKey();
1644 capabilityMap = capabilitySubstitutionMapping.get(capabilityKey);
1645 capabilityMapping.put(capabilityKey, capabilityMap);
1648 mdcDataDebugMessage.debugExitMessage(null, null);
1649 return capabilityMapping;
1652 public static void addSubstitutionNodeTypeRequirements(NodeType substitutionNodeType,
1653 List<Map<String, RequirementDefinition>>
1655 String templateName) {
1656 mdcDataDebugMessage.debugEntryMessage(null, null);
1658 if (requirementsList == null || requirementsList.size() == 0) {
1662 if (substitutionNodeType.getRequirements() == null) {
1663 substitutionNodeType.setRequirements(new ArrayList<>());
1666 for (Map<String, RequirementDefinition> requirementDef : requirementsList) {
1667 for (Map.Entry<String, RequirementDefinition> entry : requirementDef.entrySet()) {
1668 Map<String, RequirementDefinition> requirementMap = new HashMap<>();
1669 requirementMap.put(entry.getKey() + "_" + templateName, entry.getValue().clone());
1670 substitutionNodeType.getRequirements().add(requirementMap);
1674 mdcDataDebugMessage.debugExitMessage(null, null);
1677 public static boolean isNodeTemplateSectionMissingFromServiceTemplate(ServiceTemplate serviceTemplate){
1678 return Objects.isNull(serviceTemplate.getTopology_template() )
1679 || MapUtils.isEmpty(serviceTemplate.getTopology_template().getNode_templates());