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.MdcDataErrorMessage;
31 import org.openecomp.sdc.logging.types.LoggerConstants;
32 import org.openecomp.sdc.logging.types.LoggerErrorCode;
33 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
34 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
35 import org.openecomp.sdc.tosca.datatypes.ToscaCapabilityType;
36 import org.openecomp.sdc.tosca.datatypes.ToscaFunctions;
37 import org.openecomp.sdc.tosca.datatypes.ToscaRelationshipType;
38 import org.openecomp.sdc.tosca.datatypes.model.AttributeDefinition;
39 import org.openecomp.sdc.tosca.datatypes.model.CapabilityAssignment;
40 import org.openecomp.sdc.tosca.datatypes.model.CapabilityDefinition;
41 import org.openecomp.sdc.tosca.datatypes.model.Constraint;
42 import org.openecomp.sdc.tosca.datatypes.model.EntrySchema;
43 import org.openecomp.sdc.tosca.datatypes.model.GroupDefinition;
44 import org.openecomp.sdc.tosca.datatypes.model.Import;
45 import org.openecomp.sdc.tosca.datatypes.model.NodeTemplate;
46 import org.openecomp.sdc.tosca.datatypes.model.NodeType;
47 import org.openecomp.sdc.tosca.datatypes.model.ParameterDefinition;
48 import org.openecomp.sdc.tosca.datatypes.model.PolicyDefinition;
49 import org.openecomp.sdc.tosca.datatypes.model.PropertyDefinition;
50 import org.openecomp.sdc.tosca.datatypes.model.RelationshipTemplate;
51 import org.openecomp.sdc.tosca.datatypes.model.RequirementAssignment;
52 import org.openecomp.sdc.tosca.datatypes.model.RequirementDefinition;
53 import org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate;
54 import org.openecomp.sdc.tosca.datatypes.model.Status;
55 import org.openecomp.sdc.tosca.datatypes.model.SubstitutionMapping;
56 import org.openecomp.sdc.tosca.datatypes.model.TopologyTemplate;
57 import org.openecomp.sdc.tosca.datatypes.model.heatextend.ParameterDefinitionExt;
58 import org.openecomp.sdc.tosca.errors.InvalidAddActionNullEntityErrorBuilder;
59 import org.openecomp.sdc.tosca.errors.InvalidRequirementAssignmentErrorBuilder;
60 import org.openecomp.sdc.tosca.services.impl.ToscaAnalyzerServiceImpl;
62 import java.io.ByteArrayInputStream;
63 import java.io.ByteArrayOutputStream;
64 import java.io.IOException;
65 import java.io.NotSerializableException;
66 import java.io.ObjectInputStream;
67 import java.io.ObjectOutputStream;
68 import java.util.ArrayList;
69 import java.util.Collections;
70 import java.util.HashMap;
71 import java.util.List;
72 import java.util.ListIterator;
74 import java.util.Objects;
75 import java.util.Optional;
78 * The type Data model util.
80 public class DataModelUtil {
83 * Add substitution mapping.
85 private static final Logger logger = LoggerFactory.getLogger(DataModelUtil.class);
86 private static final String SERVICE_TEMPLATE = "Service Template";
87 private static final String NODE_TYPE = "Node Type";
90 * Add substitution mapping.
92 * @param serviceTemplate the service template
93 * @param substitutionMapping the substitution mapping
95 public static void addSubstitutionMapping(ServiceTemplate serviceTemplate,
96 SubstitutionMapping substitutionMapping) {
97 if (serviceTemplate == null) {
98 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
99 LoggerTragetServiceName.ADD_ENTITIES_TO_TOSCA, ErrorLevel.ERROR.name(),
100 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.INVALID_ADD_ACTION);
101 throw new CoreException(
102 new InvalidAddActionNullEntityErrorBuilder("Substitution Mapping", SERVICE_TEMPLATE)
106 if (serviceTemplate.getTopology_template() == null) {
107 serviceTemplate.setTopology_template(new TopologyTemplate());
109 serviceTemplate.getTopology_template().setSubstitution_mappings(substitutionMapping);
112 public static List<String> getDirectives(NodeTemplate nodeTemplate) {
113 if (Objects.isNull(nodeTemplate)
114 || Objects.isNull(nodeTemplate.getDirectives())) {
115 return Collections.emptyList();
118 return nodeTemplate.getDirectives();
122 * Add substitution mapping req.
124 * @param serviceTemplate the service template
125 * @param substitutionMappingRequirementId the substitution mapping requirement id
126 * @param substitutionMappingRequirementList the substitution mapping requirement list
128 public static void addSubstitutionMappingReq(ServiceTemplate serviceTemplate,
129 String substitutionMappingRequirementId,
130 List<String> substitutionMappingRequirementList) {
131 if (serviceTemplate == null) {
132 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
133 LoggerTragetServiceName.ADD_ENTITIES_TO_TOSCA, ErrorLevel.ERROR.name(),
134 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.INVALID_ADD_ACTION);
135 throw new CoreException(
136 new InvalidAddActionNullEntityErrorBuilder("Substitution Mapping Requirements",
137 SERVICE_TEMPLATE).build());
140 if (serviceTemplate.getTopology_template() == null) {
141 serviceTemplate.setTopology_template(new TopologyTemplate());
143 if (serviceTemplate.getTopology_template().getSubstitution_mappings() == null) {
144 serviceTemplate.getTopology_template().setSubstitution_mappings(new SubstitutionMapping());
146 if (serviceTemplate.getTopology_template().getSubstitution_mappings().getRequirements()
148 serviceTemplate.getTopology_template().getSubstitution_mappings()
149 .setRequirements(new HashMap<>());
152 serviceTemplate.getTopology_template().getSubstitution_mappings().getRequirements()
153 .put(substitutionMappingRequirementId, substitutionMappingRequirementList);
157 * Add substitution mapping capability.
159 * @param serviceTemplate the service template
160 * @param substitutionMappingCapabilityId the substitution mapping capability id
161 * @param substitutionMappingCapabilityList the substitution mapping capability list
163 public static void addSubstitutionMappingCapability(ServiceTemplate serviceTemplate,
164 String substitutionMappingCapabilityId,
165 List<String> substitutionMappingCapabilityList) {
166 if (serviceTemplate == null) {
167 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
168 LoggerTragetServiceName.ADD_ENTITIES_TO_TOSCA, ErrorLevel.ERROR.name(),
169 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.INVALID_ADD_ACTION);
170 throw new CoreException(
171 new InvalidAddActionNullEntityErrorBuilder("Substitution Mapping Capabilities",
172 SERVICE_TEMPLATE).build());
175 if (serviceTemplate.getTopology_template() == null) {
176 serviceTemplate.setTopology_template(new TopologyTemplate());
178 if (serviceTemplate.getTopology_template().getSubstitution_mappings() == null) {
179 serviceTemplate.getTopology_template().setSubstitution_mappings(new SubstitutionMapping());
181 if (serviceTemplate.getTopology_template().getSubstitution_mappings().getCapabilities()
183 serviceTemplate.getTopology_template().getSubstitution_mappings()
184 .setCapabilities(new HashMap<>());
187 serviceTemplate.getTopology_template().getSubstitution_mappings().getCapabilities()
188 .putIfAbsent(substitutionMappingCapabilityId, substitutionMappingCapabilityList);
191 public static Map<String, NodeTemplate> getNodeTemplates(ServiceTemplate serviceTemplate) {
192 if (Objects.isNull(serviceTemplate)
193 || Objects.isNull(serviceTemplate.getTopology_template())
194 || MapUtils.isEmpty(serviceTemplate.getTopology_template().getNode_templates())) {
195 return new HashMap<>();
198 return serviceTemplate.getTopology_template().getNode_templates();
204 * @param serviceTemplate the service template
205 * @param nodeTemplateId the node template id
206 * @param nodeTemplate the node template
208 public static void addNodeTemplate(ServiceTemplate serviceTemplate, String nodeTemplateId,
209 NodeTemplate nodeTemplate) {
210 if (serviceTemplate == null) {
211 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
212 LoggerTragetServiceName.ADD_ENTITIES_TO_TOSCA, ErrorLevel.ERROR.name(),
213 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.INVALID_ADD_ACTION);
214 throw new CoreException(
215 new InvalidAddActionNullEntityErrorBuilder("Node Template", SERVICE_TEMPLATE).build());
217 TopologyTemplate topologyTemplate = serviceTemplate.getTopology_template();
218 if (Objects.isNull(topologyTemplate)) {
219 topologyTemplate = new TopologyTemplate();
220 serviceTemplate.setTopology_template(topologyTemplate);
222 if (topologyTemplate.getNode_templates() == null) {
223 topologyTemplate.setNode_templates(new HashMap<>());
225 topologyTemplate.getNode_templates().put(nodeTemplateId, nodeTemplate);
229 * Add capabilities def to node type.
231 * @param nodeType the node type
232 * @param capabilities the capability definitions
234 public static void addNodeTypeCapabilitiesDef(NodeType nodeType,
235 Map<String, CapabilityDefinition> capabilities) {
236 if (MapUtils.isEmpty(capabilities) || capabilities.entrySet().isEmpty()) {
240 if (nodeType == null) {
241 throw new CoreException(
242 new InvalidAddActionNullEntityErrorBuilder("Capability Definition", NODE_TYPE).build());
245 if (MapUtils.isEmpty(nodeType.getCapabilities())) {
246 nodeType.setCapabilities(new HashMap<>());
248 if (capabilities.size() > 0) {
249 nodeType.setCapabilities(new HashMap<>());
251 for (Map.Entry<String, CapabilityDefinition> entry : capabilities.entrySet()) {
252 nodeType.getCapabilities().put(entry.getKey(), entry.getValue());
257 * Add policy definition.
259 * @param serviceTemplate the service template
260 * @param policyId the policy id
261 * @param policyDefinition the policy definition
263 public static void addPolicyDefinition(ServiceTemplate serviceTemplate, String policyId,
264 PolicyDefinition policyDefinition) {
265 if (serviceTemplate == null) {
266 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
267 LoggerTragetServiceName.ADD_ENTITIES_TO_TOSCA, ErrorLevel.ERROR.name(),
268 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.INVALID_ADD_ACTION);
269 throw new CoreException(
270 new InvalidAddActionNullEntityErrorBuilder("Policy Definition", SERVICE_TEMPLATE)
273 TopologyTemplate topologyTemplate = serviceTemplate.getTopology_template();
274 if (Objects.isNull(topologyTemplate)) {
275 topologyTemplate = new TopologyTemplate();
276 serviceTemplate.setTopology_template(topologyTemplate);
278 if (topologyTemplate.getPolicies() == null) {
279 topologyTemplate.setPolicies(new HashMap<>());
281 topologyTemplate.getPolicies().put(policyId, policyDefinition);
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,
293 if (serviceTemplate == null) {
294 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
295 LoggerTragetServiceName.ADD_ENTITIES_TO_TOSCA, ErrorLevel.ERROR.name(),
296 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.INVALID_ADD_ACTION);
297 throw new CoreException(
298 new InvalidAddActionNullEntityErrorBuilder(NODE_TYPE, SERVICE_TEMPLATE).build());
300 if (serviceTemplate.getNode_types() == null) {
301 serviceTemplate.setNode_types(new HashMap<>());
303 serviceTemplate.getNode_types().put(nodeTypeId, nodeType);
306 public static void removeNodeType(ServiceTemplate serviceTemplate,
308 if (serviceTemplate == null) {
309 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
310 LoggerTragetServiceName.ADD_ENTITIES_TO_TOSCA, ErrorLevel.ERROR.name(),
311 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.INVALID_ADD_ACTION);
312 throw new CoreException(
313 new InvalidAddActionNullEntityErrorBuilder(NODE_TYPE, SERVICE_TEMPLATE).build());
315 if (serviceTemplate.getNode_types() == null) {
316 serviceTemplate.setNode_types(new HashMap<>());
318 serviceTemplate.getNode_types().remove(nodeTypeId);
322 * Add relationship template.
324 * @param serviceTemplate the service template
325 * @param relationshipTemplateId the relationship template id
326 * @param relationshipTemplate the relationship template
328 public static void addRelationshipTemplate(ServiceTemplate serviceTemplate,
329 String relationshipTemplateId,
330 RelationshipTemplate relationshipTemplate) {
331 if (serviceTemplate == null) {
332 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
333 LoggerTragetServiceName.ADD_ENTITIES_TO_TOSCA, ErrorLevel.ERROR.name(),
334 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.INVALID_ADD_ACTION);
335 throw new CoreException(
336 new InvalidAddActionNullEntityErrorBuilder("Relationship Template", SERVICE_TEMPLATE)
339 if (serviceTemplate.getTopology_template() == null) {
340 serviceTemplate.setTopology_template(new TopologyTemplate());
342 if (serviceTemplate.getTopology_template().getRelationship_templates() == null) {
343 serviceTemplate.getTopology_template().setRelationship_templates(new HashMap<>());
345 serviceTemplate.getTopology_template().getRelationship_templates()
346 .put(relationshipTemplateId, relationshipTemplate);
350 * Add requirement assignment.
352 * @param nodeTemplate the node template
353 * @param requirementId the requirement id
354 * @param requirementAssignment the requirement assignment
356 public static void addRequirementAssignment(NodeTemplate nodeTemplate, String requirementId,
357 RequirementAssignment requirementAssignment) {
358 if (nodeTemplate == null) {
359 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
360 LoggerTragetServiceName.ADD_ENTITIES_TO_TOSCA, ErrorLevel.ERROR.name(),
361 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.INVALID_ADD_ACTION);
362 throw new CoreException(
363 new InvalidAddActionNullEntityErrorBuilder("Requirement Assignment", "Node Template")
366 if (requirementAssignment.getNode() == 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(new InvalidRequirementAssignmentErrorBuilder(requirementId).build());
373 if (nodeTemplate.getRequirements() == null) {
374 nodeTemplate.setRequirements(new ArrayList<>());
376 Map<String, RequirementAssignment> requirement = new HashMap<>();
377 requirement.put(requirementId, requirementAssignment);
378 nodeTemplate.getRequirements().add(requirement);
382 * Gets node template.
384 * @param serviceTemplate the service template
385 * @param nodeTemplateId the node template id
386 * @return the node template
388 public static NodeTemplate getNodeTemplate(ServiceTemplate serviceTemplate,
389 String nodeTemplateId) {
390 if (serviceTemplate == null
391 || serviceTemplate.getTopology_template() == null
392 || serviceTemplate.getTopology_template().getNode_templates() == null) {
395 return serviceTemplate.getTopology_template().getNode_templates().get(nodeTemplateId);
401 * @param serviceTemplate the service template
402 * @param nodeTypeId the node type id
403 * @return the node type
405 public static NodeType getNodeType(ServiceTemplate serviceTemplate, String nodeTypeId) {
406 if (serviceTemplate == null || serviceTemplate.getNode_types() == null) {
409 return serviceTemplate.getNode_types().get(nodeTypeId);
413 * Gets requirement definition.
415 * @param nodeType the node type
416 * @param requirementDefinitionId the requirement definition id
417 * @return the requirement definition
419 public static Optional<RequirementDefinition> getRequirementDefinition(
421 String requirementDefinitionId) {
422 if (nodeType == null || nodeType.getRequirements() == null || requirementDefinitionId == null) {
423 return Optional.empty();
425 for (Map<String, RequirementDefinition> reqMap : nodeType.getRequirements()) {
426 if (reqMap.containsKey(requirementDefinitionId)) {
427 return Optional.of(reqMap.get(requirementDefinitionId));
430 return Optional.empty();
434 * get requirement definition from requirement definition list by req key.
436 * @param requirementsDefinitionList requirement definition list
437 * @param requirementKey requirement key
439 public static Optional<RequirementDefinition> getRequirementDefinition(
440 List<Map<String, RequirementDefinition>> requirementsDefinitionList,
441 String requirementKey) {
442 if (CollectionUtils.isEmpty(requirementsDefinitionList)) {
443 return Optional.empty();
446 for (Map<String, RequirementDefinition> requirementMap : requirementsDefinitionList) {
447 if (requirementMap.containsKey(requirementKey)) {
448 return Optional.of(requirementMap.get(requirementKey));
451 return Optional.empty();
455 * Gets capability definition.
457 * @param nodeType the node type
458 * @param capabilityDefinitionId the capability definition id
459 * @return the capability definition
461 public static Optional<CapabilityDefinition> getCapabilityDefinition(
463 String capabilityDefinitionId) {
464 if (nodeType == null || nodeType.getCapabilities() == null || capabilityDefinitionId == null) {
465 return Optional.empty();
467 return Optional.ofNullable(nodeType.getCapabilities().get(capabilityDefinitionId));
471 * Add group definition to topology template.
473 * @param serviceTemplate the service template
474 * @param groupName the group name
475 * @param group the group
477 public static void addGroupDefinitionToTopologyTemplate(ServiceTemplate serviceTemplate,
478 String groupName, GroupDefinition group) {
479 if (serviceTemplate == null) {
480 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
481 LoggerTragetServiceName.ADD_ENTITIES_TO_TOSCA, ErrorLevel.ERROR.name(),
482 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.INVALID_ADD_ACTION);
483 throw new CoreException(
484 new InvalidAddActionNullEntityErrorBuilder("Group Definition", SERVICE_TEMPLATE)
488 TopologyTemplate topologyTemplate = serviceTemplate.getTopology_template();
489 if (Objects.isNull(topologyTemplate)) {
490 topologyTemplate = new TopologyTemplate();
491 serviceTemplate.setTopology_template(topologyTemplate);
493 if (topologyTemplate.getGroups() == null) {
494 topologyTemplate.setGroups(new HashMap<>());
496 if (serviceTemplate.getTopology_template().getGroups() == null) {
497 Map<String, GroupDefinition> groups = new HashMap<>();
498 serviceTemplate.getTopology_template().setGroups(groups);
501 serviceTemplate.getTopology_template().getGroups().put(groupName, group);
505 * Create parameter definition property definition.
507 * @param type the type
508 * @param description the description
509 * @param value the value
510 * @param required the required
511 * @param constraints the constraints
512 * @param status the status
513 * @param entrySchema the entry schema
514 * @param defaultVal the default val
515 * @return the property definition
517 public static ParameterDefinition createParameterDefinition(String type, String description,
518 Object value, boolean required,
519 List<Constraint> constraints,
521 EntrySchema entrySchema,
523 ParameterDefinition paramDef = new ParameterDefinition();
524 paramDef.setType(type);
525 paramDef.setDescription(description);
526 paramDef.setValue(value);
527 paramDef.setRequired(required);
528 paramDef.setConstraints(constraints);
529 if (status != null) {
530 paramDef.setStatus(status);
532 paramDef.setEntry_schema(entrySchema == null ? null : entrySchema.clone());
533 paramDef.set_default(defaultVal);
538 * Create requirement requirement definition.
540 * @param capability the capability
541 * @param node the node
542 * @param relationship the relationship
543 * @param occurrences the occurrences
544 * @return the requirement definition
546 public static RequirementDefinition createRequirement(String capability, String node,
547 String relationship, Object[] occurrences) {
548 RequirementDefinition requirementDefinition = new RequirementDefinition();
549 requirementDefinition.setCapability(capability);
550 requirementDefinition.setNode(node);
551 requirementDefinition.setRelationship(relationship);
552 if (occurrences != null) {
553 requirementDefinition.setOccurrences(occurrences);
555 return requirementDefinition;
559 * Create entry schema entry schema.
561 * @param type the type
562 * @param description the description
563 * @param constraints the constraints
564 * @return the entry schema
566 public static EntrySchema createEntrySchema(String type, String description,
567 List<Constraint> constraints) {
568 if (Objects.isNull(type) && Objects.isNull(description) &&
569 CollectionUtils.isEmpty(constraints)) {
573 EntrySchema entrySchema = new EntrySchema();
574 entrySchema.setType(type);
575 entrySchema.setDescription(description);
576 entrySchema.setConstraints(constraints);
581 * Create get input property value from list parameter map.
583 * @param inputPropertyListName the input property list name
584 * @param indexInTheList the index in the list
585 * @param nestedPropertyName the nested property name
588 public static Map createGetInputPropertyValueFromListParameter(String inputPropertyListName,
590 String... nestedPropertyName) {
591 List propertyList = new ArrayList<>();
592 propertyList.add(inputPropertyListName);
593 propertyList.add(indexInTheList);
594 if (nestedPropertyName != null) {
595 Collections.addAll(propertyList, nestedPropertyName);
597 Map getInputProperty = new HashMap<>();
598 getInputProperty.put(ToscaFunctions.GET_INPUT.getDisplayName(), propertyList);
599 return getInputProperty;
603 * Convert property def to parameter def parameter definition ext.
605 * @param propertyDefinition the property definition
606 * @return the parameter definition ext
608 public static ParameterDefinitionExt convertPropertyDefToParameterDef(
609 PropertyDefinition propertyDefinition) {
610 if (propertyDefinition == null) {
614 ParameterDefinitionExt parameterDefinition = new ParameterDefinitionExt();
615 parameterDefinition.setType(propertyDefinition.getType());
616 parameterDefinition.setDescription(propertyDefinition.getDescription());
617 parameterDefinition.setRequired(propertyDefinition.getRequired());
618 parameterDefinition.set_default(propertyDefinition.get_default());
619 parameterDefinition.setStatus(propertyDefinition.getStatus());
620 parameterDefinition.setConstraints(propertyDefinition.getConstraints());
621 parameterDefinition.setEntry_schema(Objects.isNull(propertyDefinition.getEntry_schema()) ? null
622 : propertyDefinition.getEntry_schema().clone());
623 parameterDefinition.setHidden(false);
624 parameterDefinition.setImmutable(false);
625 return parameterDefinition;
629 * Convert attribute def to parameter def parameter definition ext.
631 * @param attributeDefinition the attribute definition
632 * @param outputValue the output value
633 * @return the parameter definition ext
635 public static ParameterDefinitionExt convertAttributeDefToParameterDef(
636 AttributeDefinition attributeDefinition, Map<String, List> outputValue) {
637 if (attributeDefinition == null) {
640 ParameterDefinitionExt parameterDefinition = new ParameterDefinitionExt();
641 parameterDefinition.setDescription(attributeDefinition.getDescription());
642 parameterDefinition.setValue(outputValue);
643 return parameterDefinition;
646 public static boolean isNodeTemplate(String entryId, ServiceTemplate serviceTemplate) {
647 return serviceTemplate.getTopology_template().getNode_templates() != null
648 && serviceTemplate.getTopology_template().getNode_templates().get(entryId) != null;
652 * Add Input parameter.
654 * @param serviceTemplate the service template
655 * @param parameterDefinitionId the parameter definition id
656 * @param parameterDefinition the parameter definition
658 public static void addInputParameterToTopologyTemplate(ServiceTemplate serviceTemplate,
659 String parameterDefinitionId,
660 ParameterDefinition parameterDefinition) {
661 if (Objects.isNull(serviceTemplate)) {
662 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
663 LoggerTragetServiceName.ADD_ENTITIES_TO_TOSCA, ErrorLevel.ERROR.name(),
664 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.INVALID_ADD_ACTION);
665 throw new CoreException(
666 new InvalidAddActionNullEntityErrorBuilder("Topology Template Input Parameter",
667 SERVICE_TEMPLATE).build());
669 TopologyTemplate topologyTemplate = serviceTemplate.getTopology_template();
670 if (Objects.isNull(topologyTemplate)) {
671 topologyTemplate = new TopologyTemplate();
672 serviceTemplate.setTopology_template(topologyTemplate);
674 if (topologyTemplate.getInputs() == null) {
675 topologyTemplate.setInputs(new HashMap<>());
677 topologyTemplate.getInputs().put(parameterDefinitionId, parameterDefinition);
681 * Add Output parameter.
683 * @param serviceTemplate the service template
684 * @param parameterDefinitionId the parameter definition id
685 * @param parameterDefinition the parameter definition
687 public static void addOutputParameterToTopologyTemplate(ServiceTemplate serviceTemplate,
688 String parameterDefinitionId,
689 ParameterDefinition parameterDefinition) {
690 if (Objects.isNull(serviceTemplate)) {
691 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
692 LoggerTragetServiceName.ADD_ENTITIES_TO_TOSCA, ErrorLevel.ERROR.name(),
693 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.INVALID_ADD_ACTION);
694 throw new CoreException(
695 new InvalidAddActionNullEntityErrorBuilder("Topology Template Output Parameter",
696 SERVICE_TEMPLATE).build());
698 TopologyTemplate topologyTemplate = serviceTemplate.getTopology_template();
699 if (Objects.isNull(topologyTemplate)) {
700 topologyTemplate = new TopologyTemplate();
701 serviceTemplate.setTopology_template(topologyTemplate);
703 if (topologyTemplate.getOutputs() == null) {
704 topologyTemplate.setOutputs(new HashMap<>());
706 topologyTemplate.getOutputs().put(parameterDefinitionId, parameterDefinition);
710 * Add requirement def to requirement def list.
712 * @param requirementList requirement list
713 * @param requirementDef added requirement def
715 public static void addRequirementToList(List<Map<String, RequirementDefinition>> requirementList,
716 Map<String, RequirementDefinition> requirementDef) {
717 if (requirementDef == null) {
720 if (requirementList == null) {
721 requirementList = new ArrayList<>();
724 for (Map.Entry<String, RequirementDefinition> entry : requirementDef.entrySet()) {
725 CommonMethods.mergeEntryInList(entry.getKey(), entry.getValue(), requirementList);
730 * get node template requirement.
732 * @param nodeTemplate node template
734 public static Map<String, RequirementAssignment> getNodeTemplateRequirements(
735 NodeTemplate nodeTemplate) {
736 if (Objects.isNull(nodeTemplate)) {
739 List<Map<String, RequirementAssignment>> templateRequirements = nodeTemplate.getRequirements();
741 Map<String, RequirementAssignment> nodeTemplateRequirementsAssignment = new HashMap<>();
742 if (CollectionUtils.isEmpty(templateRequirements)) {
743 return nodeTemplateRequirementsAssignment;
745 YamlUtil yamlUtil = new YamlUtil();
746 for (Map<String, RequirementAssignment> requirementAssignmentMap : templateRequirements) {
747 for (Map.Entry<String, RequirementAssignment> requirementEntry : requirementAssignmentMap
749 RequirementAssignment requirementAssignment = (yamlUtil
750 .yamlToObject(yamlUtil.objectToYaml(requirementEntry.getValue()),
751 RequirementAssignment.class));
752 nodeTemplateRequirementsAssignment
753 .put(requirementEntry.getKey(), requirementAssignment);
756 return nodeTemplateRequirementsAssignment;
760 * Gets the list of requirements for the node template.
762 * @param nodeTemplate the node template
763 * @return the node template requirement list and null if the node has no requirements
765 public static List<Map<String, RequirementAssignment>> getNodeTemplateRequirementList(
766 NodeTemplate nodeTemplate) {
767 ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
768 //Creating concrete objects
769 List<Map<String, RequirementAssignment>> requirements = nodeTemplate.getRequirements();
770 List<Map<String, RequirementAssignment>> concreteRequirementList = null;
771 if (requirements != null) {
772 concreteRequirementList = new ArrayList<>();
773 ListIterator<Map<String, RequirementAssignment>> reqListIterator = requirements
775 while (reqListIterator.hasNext()) {
776 Map<String, RequirementAssignment> requirement = reqListIterator.next();
777 Map<String, RequirementAssignment> concreteRequirement = new HashMap<>();
778 for (Map.Entry<String, RequirementAssignment> reqEntry : requirement.entrySet()) {
779 RequirementAssignment requirementAssignment = (toscaExtensionYamlUtil
780 .yamlToObject(toscaExtensionYamlUtil.objectToYaml(reqEntry.getValue()),
781 RequirementAssignment.class));
782 concreteRequirement.put(reqEntry.getKey(), requirementAssignment);
783 concreteRequirementList.add(concreteRequirement);
784 reqListIterator.remove();
787 requirements.clear();
788 requirements.addAll(concreteRequirementList);
789 nodeTemplate.setRequirements(requirements);
791 return concreteRequirementList;
795 * get requirement assignment from requirement assignment list by req key.
797 * @param requirementsAssignmentList requirement definition list
798 * @param requirementKey requirement key
800 public static Optional<List<RequirementAssignment>> getRequirementAssignment(
801 List<Map<String, RequirementAssignment>> requirementsAssignmentList,
802 String requirementKey) {
803 if (CollectionUtils.isEmpty(requirementsAssignmentList)) {
804 return Optional.empty();
807 List<RequirementAssignment> matchRequirementAssignmentList = new ArrayList<>();
808 for (Map<String, RequirementAssignment> requirementMap : requirementsAssignmentList) {
809 if (requirementMap.containsKey(requirementKey)) {
810 YamlUtil yamlUtil = new YamlUtil();
811 RequirementAssignment requirementAssignment = (yamlUtil
812 .yamlToObject(yamlUtil.objectToYaml(requirementMap.get(requirementKey)),
813 RequirementAssignment.class));
814 matchRequirementAssignmentList.add(requirementAssignment);
817 return Optional.of(matchRequirementAssignmentList);
821 * remove requirement definition from requirement definition list by req key.
823 * @param requirementsDefinitionList requirement definition list
824 * @param requirementKey requirement key
826 public static void removeRequirementsDefinition(
827 List<Map<String, RequirementDefinition>> requirementsDefinitionList,
828 String requirementKey) {
829 if (requirementsDefinitionList == null) {
833 List<Map<String, RequirementDefinition>> mapToBeRemoved = new ArrayList<>();
834 for (Map<String, RequirementDefinition> reqMap : requirementsDefinitionList) {
835 reqMap.remove(requirementKey);
836 if (reqMap.isEmpty()) {
837 mapToBeRemoved.add(reqMap);
840 for (Map<String, RequirementDefinition> removeMap : mapToBeRemoved) {
841 requirementsDefinitionList.remove(removeMap);
846 * remove requirement assignment from requirement definition list by req key.
848 * @param requirementsAssignmentList requirement Assignment list
849 * @param requirementKey requirement key
851 public static void removeRequirementsAssignment(
852 List<Map<String, RequirementAssignment>> requirementsAssignmentList,
853 String requirementKey) {
854 if (requirementsAssignmentList == null) {
858 List<Map<String, RequirementAssignment>> mapToBeRemoved = new ArrayList<>();
859 for (Map<String, RequirementAssignment> reqMap : requirementsAssignmentList) {
860 reqMap.remove(requirementKey);
861 if (reqMap.isEmpty()) {
862 mapToBeRemoved.add(reqMap);
865 for (Map<String, RequirementAssignment> removeMap : mapToBeRemoved) {
866 requirementsAssignmentList.remove(removeMap);
872 * Remove requirement assignment.
874 * @param nodeTemplate the node template
875 * @param requirementKey the requirement key
876 * @param requirementAssignmentToBeDeleted the requirement assignment to be deleted
878 public static void removeRequirementAssignment(
879 NodeTemplate nodeTemplate,
880 String requirementKey,
881 RequirementAssignment requirementAssignmentToBeDeleted) {
882 ToscaAnalyzerService toscaAnalyzerService = new ToscaAnalyzerServiceImpl();
883 List<Map<String, RequirementAssignment>> nodeTemplateRequirements = nodeTemplate
885 if (nodeTemplateRequirements == null) {
889 ListIterator<Map<String, RequirementAssignment>> iter = nodeTemplateRequirements.listIterator();
890 while (iter.hasNext()) {
891 Map<String, RequirementAssignment> reqMap = iter.next();
892 RequirementAssignment requirementAssignment = reqMap.get(requirementKey);
893 if (requirementAssignment != null) {
894 boolean isDesiredRequirementAssignment = toscaAnalyzerService
895 .isDesiredRequirementAssignment(requirementAssignment,
896 requirementAssignmentToBeDeleted.getCapability(),
897 requirementAssignmentToBeDeleted.getNode(),
898 requirementAssignmentToBeDeleted.getRelationship());
899 if (isDesiredRequirementAssignment) {
907 * Return the suffix of the input namespace For an exampale - for abc.sdf.vsrx, return vsrx
909 * @param namespace namespace
910 * @return String namespace suffix
912 public static String getNamespaceSuffix(String namespace) {
913 if (namespace == null) {
916 String delimiterChar = ".";
917 if (namespace.contains(delimiterChar)) {
918 return namespace.substring(namespace.lastIndexOf(delimiterChar) + 1);
924 * Return true if the input import exist in the input imports list.
926 * @param imports namespace
927 * @param importId namespace
928 * @return true if exist, false if not exist
930 public static boolean isImportAddedToServiceTemplate(List<Map<String, Import>> imports,
932 for (Map<String, Import> anImport : imports) {
933 if (anImport.containsKey(importId)) {
941 * Get output parameter according to the input outputParameterId.
943 * @param serviceTemplate service template
944 * @param outputParameterId output parameter id
945 * @return ParameterDefinition - output parameter
947 public static ParameterDefinition getOuputParameter(ServiceTemplate serviceTemplate,
948 String outputParameterId) {
949 if (serviceTemplate == null
950 || serviceTemplate.getTopology_template() == null
951 || serviceTemplate.getTopology_template().getOutputs() == null) {
954 return serviceTemplate.getTopology_template().getOutputs().get(outputParameterId);
958 * Gets input parameters in a service template.
960 * @param serviceTemplate the service template
961 * @return the input parameters
963 public static Map<String, ParameterDefinition> getInputParameters(ServiceTemplate
965 if (serviceTemplate == null
966 || serviceTemplate.getTopology_template() == null
967 || serviceTemplate.getTopology_template().getInputs() == null) {
970 return serviceTemplate.getTopology_template().getInputs();
974 * Gets relationship templates in a service template.
976 * @param serviceTemplate the service template
977 * @return the relationship template
979 public static Map<String, RelationshipTemplate> getRelationshipTemplates(ServiceTemplate
981 if (serviceTemplate == null
982 || serviceTemplate.getTopology_template() == null
983 || serviceTemplate.getTopology_template().getRelationship_templates() == null) {
986 return serviceTemplate.getTopology_template().getRelationship_templates();
990 * Get property value according to the input propertyId.
992 * @param nodeTemplate node template
993 * @param propertyId property id
994 * @return Object property Value
996 public static Object getPropertyValue(NodeTemplate nodeTemplate,
998 if (nodeTemplate == null
999 || nodeTemplate.getProperties() == null) {
1002 return nodeTemplate.getProperties().get(propertyId);
1006 * Get node template properties according to the input node template id.
1008 * @param serviceTemplate service template
1009 * @param nodeTemplateId node template id
1010 * @return node template properties
1012 public static Map<String, Object> getNodeTemplateProperties(ServiceTemplate serviceTemplate,
1013 String nodeTemplateId) {
1014 if (serviceTemplate == null
1015 || serviceTemplate.getTopology_template() == null
1016 || serviceTemplate.getTopology_template().getNode_templates() == null
1017 || serviceTemplate.getTopology_template().getNode_templates().get(nodeTemplateId) == null) {
1020 return serviceTemplate.getTopology_template().getNode_templates().get(nodeTemplateId)
1025 * Gets substitution mappings in a service template.
1027 * @param serviceTemplate the service template
1028 * @return the substitution mappings
1030 public static SubstitutionMapping getSubstitutionMappings(ServiceTemplate serviceTemplate) {
1031 if (serviceTemplate == null
1032 || serviceTemplate.getTopology_template() == null
1033 || serviceTemplate.getTopology_template().getSubstitution_mappings() == null) {
1036 return serviceTemplate.getTopology_template().getSubstitution_mappings();
1041 * Compare two requirement assignment objects for equality.
1043 * @param first the first requirement assignment object
1044 * @param second the second requirement assignment object
1045 * @return true if objects are equal and false otherwise
1047 public static boolean compareRequirementAssignment(RequirementAssignment first,
1048 RequirementAssignment second) {
1049 return (first.getCapability().equals(second.getCapability())
1050 && first.getNode().equals(second.getNode())
1051 && first.getRelationship().equals(second.getRelationship()));
1055 * Gets a deep copy clone of the input object.
1057 * @param <T> the type parameter
1058 * @param objectValue the object value
1059 * @param clazz the clazz
1060 * @return the cloned object
1062 public static <T> Object getClonedObject(Object objectValue, Class<T> clazz) {
1063 YamlUtil yamlUtil = new ToscaExtensionYamlUtil();
1064 Object clonedObjectValue;
1065 String objectToYaml = yamlUtil.objectToYaml(objectValue);
1066 clonedObjectValue = yamlUtil.yamlToObject(objectToYaml, clazz);
1067 return clonedObjectValue;
1071 * Gets a deep copy clone of the input object.
1073 * @param obj the object to be cloned
1074 * @return the cloned object
1076 public static Object getClonedObject(Object obj) {
1077 Object clonedObjectValue;
1080 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
1081 ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
1082 objectOutputStream.writeObject(obj);
1083 //Deserialize object
1084 ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream
1086 ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
1087 clonedObjectValue = objectInputStream.readObject();
1088 } catch (NotSerializableException ex) {
1089 logger.debug(ex.getMessage(), ex);
1090 return getClonedObject(obj, obj.getClass());
1091 } catch (IOException | ClassNotFoundException ex) {
1092 logger.debug(ex.getMessage(), ex);
1095 return clonedObjectValue;
1099 * Add substitution filtering property.
1101 * @param templateName the substitution service template name
1102 * @param nodeTemplate the node template
1103 * @param count the count
1105 public static void addSubstitutionFilteringProperty(String templateName,
1106 NodeTemplate nodeTemplate, int count) {
1107 Map<String, Object> serviceTemplateFilterPropertyValue = new HashMap<>();
1108 Map<String, Object> properties = nodeTemplate.getProperties();
1109 serviceTemplateFilterPropertyValue.put(ToscaConstants
1110 .SUBSTITUTE_SERVICE_TEMPLATE_PROPERTY_NAME, templateName);
1111 serviceTemplateFilterPropertyValue.put(ToscaConstants.COUNT_PROPERTY_NAME, count);
1112 properties.put(ToscaConstants.SERVICE_TEMPLATE_FILTER_PROPERTY_NAME,
1113 serviceTemplateFilterPropertyValue);
1114 nodeTemplate.setProperties(properties);
1118 * Adding binding requirement from port node template to compute node template.
1120 * @param computeNodeTemplateId compute node template id
1121 * @param portNodeTemplate port node template
1123 public static void addBindingReqFromPortToCompute(String computeNodeTemplateId,
1124 NodeTemplate portNodeTemplate) {
1125 RequirementAssignment requirementAssignment = new RequirementAssignment();
1126 requirementAssignment.setCapability(ToscaCapabilityType.NATIVE_NETWORK_BINDABLE);
1127 requirementAssignment.setRelationship(ToscaRelationshipType.NATIVE_NETWORK_BINDS_TO);
1128 requirementAssignment.setNode(computeNodeTemplateId);
1129 addRequirementAssignment(portNodeTemplate, ToscaConstants.BINDING_REQUIREMENT_ID,
1130 requirementAssignment);
1133 public static SubstitutionMapping createSubstitutionTemplateSubMapping(
1135 NodeType substitutionNodeType,
1136 Map<String, Map<String, List<String>>> mapping) {
1137 SubstitutionMapping substitutionMapping = new SubstitutionMapping();
1138 substitutionMapping.setNode_type(nodeTypeKey);
1139 substitutionMapping.setCapabilities(
1140 manageCapabilityMapping(substitutionNodeType.getCapabilities(), mapping.get("capability")));
1141 substitutionMapping.setRequirements(
1142 manageRequirementMapping(substitutionNodeType.getRequirements(),
1143 mapping.get("requirement")));
1144 return substitutionMapping;
1148 * Add node template capability.
1150 * @param nodeTemplate the node template
1151 * @param capabilityId the capability id
1152 * @param capabilityProperties the capability properties
1153 * @param capabilityAttributes the capability attributes
1155 public static void addNodeTemplateCapability(NodeTemplate nodeTemplate, String capabilityId,
1156 Map<String, Object> capabilityProperties,
1157 Map<String, Object> capabilityAttributes) {
1158 List<Map<String, CapabilityAssignment>> capabilities = nodeTemplate.getCapabilities();
1159 if (Objects.isNull(capabilities)) {
1160 capabilities = new ArrayList<>();
1162 CapabilityAssignment capabilityAssignment = new CapabilityAssignment();
1163 capabilityAssignment.setProperties(capabilityProperties);
1164 capabilityAssignment.setAttributes(capabilityAttributes);
1165 Map<String, CapabilityAssignment> nodeTemplateCapability = new HashMap<>();
1166 nodeTemplateCapability.put(capabilityId, capabilityAssignment);
1167 capabilities.add(nodeTemplateCapability);
1168 nodeTemplate.setCapabilities(capabilities);
1171 private static Map<String, List<String>> manageRequirementMapping(
1172 List<Map<String, RequirementDefinition>> requirementList,
1173 Map<String, List<String>> requirementSubstitutionMapping) {
1174 if (requirementList == null) {
1177 Map<String, List<String>> requirementMapping = new HashMap<>();
1178 String requirementKey;
1179 List<String> requirementMap;
1180 for (Map<String, RequirementDefinition> requirementDefMap : requirementList) {
1181 for (Map.Entry<String, RequirementDefinition> entry : requirementDefMap.entrySet()) {
1182 requirementKey = entry.getKey();
1183 requirementMap = requirementSubstitutionMapping.get(requirementKey);
1184 requirementMapping.put(requirementKey, requirementMap);
1187 return requirementMapping;
1190 private static Map<String, List<String>> manageCapabilityMapping(
1191 Map<String, CapabilityDefinition> capabilities,
1192 Map<String, List<String>> capabilitySubstitutionMapping) {
1193 if (capabilities == null) {
1197 Map<String, List<String>> capabilityMapping = new HashMap<>();
1198 String capabilityKey;
1199 List<String> capabilityMap;
1200 for (Map.Entry<String, CapabilityDefinition> entry : capabilities.entrySet()) {
1201 capabilityKey = entry.getKey();
1202 capabilityMap = capabilitySubstitutionMapping.get(capabilityKey);
1203 capabilityMapping.put(capabilityKey, capabilityMap);
1205 return capabilityMapping;
1208 public static void addSubstitutionNodeTypeRequirements(NodeType substitutionNodeType,
1209 List<Map<String, RequirementDefinition>>
1211 String templateName) {
1212 if (CollectionUtils.isEmpty(requirementsList)) {
1216 if (substitutionNodeType.getRequirements() == null) {
1217 substitutionNodeType.setRequirements(new ArrayList<>());
1220 for (Map<String, RequirementDefinition> requirementDef : requirementsList) {
1221 for (Map.Entry<String, RequirementDefinition> entry : requirementDef.entrySet()) {
1222 Map<String, RequirementDefinition> requirementMap = new HashMap<>();
1223 requirementMap.put(entry.getKey() + "_" + templateName, entry.getValue().clone());
1224 substitutionNodeType.getRequirements().add(requirementMap);
1229 public static boolean isNodeTemplateSectionMissingFromServiceTemplate(
1230 ServiceTemplate serviceTemplate) {
1231 return Objects.isNull(serviceTemplate.getTopology_template())
1232 || MapUtils.isEmpty(serviceTemplate.getTopology_template().getNode_templates());