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.impl;
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.context.impl.MdcDataDebugMessage;
29 import org.openecomp.sdc.logging.context.impl.MdcDataErrorMessage;
30 import org.openecomp.sdc.logging.types.LoggerConstants;
31 import org.openecomp.sdc.logging.types.LoggerErrorCode;
32 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
33 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
34 import org.openecomp.sdc.tosca.datatypes.ToscaElementTypes;
35 import org.openecomp.sdc.tosca.datatypes.ToscaNodeType;
36 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
37 import org.openecomp.sdc.tosca.datatypes.model.AttributeDefinition;
38 import org.openecomp.sdc.tosca.datatypes.model.CapabilityDefinition;
39 import org.openecomp.sdc.tosca.datatypes.model.CapabilityType;
40 import org.openecomp.sdc.tosca.datatypes.model.Import;
41 import org.openecomp.sdc.tosca.datatypes.model.NodeTemplate;
42 import org.openecomp.sdc.tosca.datatypes.model.NodeType;
43 import org.openecomp.sdc.tosca.datatypes.model.ParameterDefinition;
44 import org.openecomp.sdc.tosca.datatypes.model.PropertyDefinition;
45 import org.openecomp.sdc.tosca.datatypes.model.PropertyType;
46 import org.openecomp.sdc.tosca.datatypes.model.RequirementAssignment;
47 import org.openecomp.sdc.tosca.datatypes.model.RequirementDefinition;
48 import org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate;
49 import org.openecomp.sdc.tosca.errors.ToscaInvalidEntryNotFoundErrorBuilder;
50 import org.openecomp.sdc.tosca.errors.ToscaInvalidSubstituteNodeTemplatePropertiesErrorBuilder;
51 import org.openecomp.sdc.tosca.errors.ToscaInvalidSubstitutionServiceTemplateErrorBuilder;
52 import org.openecomp.sdc.tosca.errors.ToscaNodeTypeNotFoundErrorBuilder;
53 import org.openecomp.sdc.tosca.services.DataModelUtil;
54 import org.openecomp.sdc.tosca.services.ToscaAnalyzerService;
55 import org.openecomp.sdc.tosca.services.ToscaConstants;
56 import org.openecomp.sdc.tosca.services.ToscaUtil;
57 import org.openecomp.sdc.tosca.services.ToscaExtensionYamlUtil;
59 import java.util.ArrayList;
60 import java.util.Collection;
61 import java.util.HashMap;
62 import java.util.HashSet;
63 import java.util.List;
65 import java.util.Objects;
66 import java.util.Optional;
69 public class ToscaAnalyzerServiceImpl implements ToscaAnalyzerService {
71 protected static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
73 public List<Map<String, RequirementDefinition>> calculateExposedRequirements(
74 List<Map<String, RequirementDefinition>> nodeTypeRequirementsDefinitionList,
75 Map<String, RequirementAssignment> nodeTemplateRequirementsAssignment) {
76 mdcDataDebugMessage.debugEntryMessage(null, null);
78 if (nodeTypeRequirementsDefinitionList == null) {
81 for (Map.Entry<String, RequirementAssignment> entry : nodeTemplateRequirementsAssignment
83 if (entry.getValue().getNode() != null) {
84 Optional<RequirementDefinition> requirementDefinition =
85 DataModelUtil.getRequirementDefinition(nodeTypeRequirementsDefinitionList, entry
87 RequirementDefinition cloneRequirementDefinition;
88 if (requirementDefinition.isPresent()) {
89 cloneRequirementDefinition = requirementDefinition.get().clone();
90 if (!evaluateRequirementFulfillment(cloneRequirementDefinition)) {
91 CommonMethods.mergeEntryInList(entry.getKey(), cloneRequirementDefinition,
92 nodeTypeRequirementsDefinitionList);
94 DataModelUtil.removeRequirementsDefinition(nodeTypeRequirementsDefinitionList, entry
99 for (Map<String, RequirementDefinition> nodeTypeRequirementsMap :
100 nodeTypeRequirementsDefinitionList) {
101 Object max = nodeTypeRequirementsMap.get(entry.getKey()).getOccurrences() != null
102 && nodeTypeRequirementsMap.get(entry.getKey()).getOccurrences().length > 0
103 ? nodeTypeRequirementsMap.get(entry.getKey()).getOccurrences()[1] : 1;
104 Object min = nodeTypeRequirementsMap.get(entry.getKey()).getOccurrences() != null
105 && nodeTypeRequirementsMap.get(entry.getKey()).getOccurrences().length > 0
106 ? nodeTypeRequirementsMap.get(entry.getKey()).getOccurrences()[0] : 1;
107 nodeTypeRequirementsMap.get(entry.getKey()).setOccurrences(new Object[]{min, max});
112 mdcDataDebugMessage.debugExitMessage(null, null);
113 return nodeTypeRequirementsDefinitionList;
116 private static boolean evaluateRequirementFulfillment(RequirementDefinition
117 requirementDefinition) {
118 Object[] occurrences = requirementDefinition.getOccurrences();
119 if (occurrences == null) {
120 requirementDefinition.setOccurrences(new Object[]{1, 1});
123 if (occurrences[1].equals(ToscaConstants.UNBOUNDED)) {
127 if (occurrences[1].equals(1)) {
130 occurrences[1] = (Integer) occurrences[1] - 1;
134 public Map<String, CapabilityDefinition> calculateExposedCapabilities(
135 Map<String, CapabilityDefinition> nodeTypeCapabilitiesDefinition,
136 Map<String, Map<String, RequirementAssignment>> fullFilledRequirementsDefinitionMap) {
139 mdcDataDebugMessage.debugEntryMessage(null, null);
141 String capabilityKey;
144 for (Map.Entry<String, Map<String, RequirementAssignment>> entry :
145 fullFilledRequirementsDefinitionMap.entrySet()) {
146 for (Map.Entry<String, RequirementAssignment> fullFilledEntry : entry.getValue().entrySet()) {
148 capability = fullFilledEntry.getValue().getCapability();
149 fullFilledEntry.getValue().getOccurrences();
150 node = fullFilledEntry.getValue().getNode();
151 capabilityKey = capability + "_" + node;
152 CapabilityDefinition capabilityDefinition = nodeTypeCapabilitiesDefinition.get(
154 if (capabilityDefinition != null) {
155 CapabilityDefinition clonedCapabilityDefinition = capabilityDefinition.clone();
156 nodeTypeCapabilitiesDefinition.put(capabilityKey, capabilityDefinition.clone());
157 if (evaluateCapabilityFulfillment(clonedCapabilityDefinition)) {
158 nodeTypeCapabilitiesDefinition.remove(capabilityKey);
160 nodeTypeCapabilitiesDefinition.put(capabilityKey, clonedCapabilityDefinition);
166 Map<String, CapabilityDefinition> exposedCapabilitiesDefinition = new HashMap<>();
167 for (Map.Entry<String, CapabilityDefinition> entry : nodeTypeCapabilitiesDefinition
169 exposedCapabilitiesDefinition.put(entry.getKey(), entry.getValue());
172 mdcDataDebugMessage.debugExitMessage(null, null);
173 return exposedCapabilitiesDefinition;
176 private static boolean evaluateCapabilityFulfillment(CapabilityDefinition capabilityDefinition) {
178 Object[] occurrences = capabilityDefinition.getOccurrences();
179 if (occurrences == null) {
180 capabilityDefinition.setOccurrences(new Object[]{1, ToscaConstants.UNBOUNDED});
183 if (occurrences[1].equals(ToscaConstants.UNBOUNDED)) {
187 if (occurrences[1].equals(1)) {
190 occurrences[1] = (Integer) occurrences[1] - 1;
195 node template with type equal to node type or derived from node type
198 public Map<String, NodeTemplate> getNodeTemplatesByType(ServiceTemplate serviceTemplate,
200 ToscaServiceModel toscaServiceModel) {
201 Map<String, NodeTemplate> nodeTemplates = new HashMap<>();
203 if (Objects.nonNull(serviceTemplate.getTopology_template())
204 && MapUtils.isNotEmpty(serviceTemplate.getTopology_template().getNode_templates())) {
205 for (Map.Entry<String, NodeTemplate> nodeTemplateEntry : serviceTemplate
206 .getTopology_template().getNode_templates().entrySet()) {
207 if (isTypeOf(nodeTemplateEntry.getValue(), nodeType, serviceTemplate, toscaServiceModel)) {
208 nodeTemplates.put(nodeTemplateEntry.getKey(), nodeTemplateEntry.getValue());
213 return nodeTemplates;
217 public Optional<NodeType> fetchNodeType(String nodeTypeKey, Collection<ServiceTemplate>
219 Optional<Map<String, NodeType>> nodeTypeMap = serviceTemplates.stream()
220 .map(st -> st.getNode_types())
221 .filter(nodeTypes -> Objects.nonNull(nodeTypes) && nodeTypes.containsKey(nodeTypeKey))
223 if (nodeTypeMap.isPresent()) {
224 return Optional.ofNullable(nodeTypeMap.get().get(nodeTypeKey));
226 return Optional.empty();
230 public boolean isTypeOf(NodeTemplate nodeTemplate, String nodeType,
231 ServiceTemplate serviceTemplate, ToscaServiceModel toscaServiceModel) {
232 if (nodeTemplate == null) {
236 if (isNodeTemplateOfTypeNodeType(nodeTemplate, nodeType)) {
240 Optional<Boolean> nodeTypeExistInServiceTemplateHierarchy =
241 isNodeTypeExistInServiceTemplateHierarchy(nodeType, nodeTemplate.getType(), serviceTemplate,
242 toscaServiceModel, null);
243 return nodeTypeExistInServiceTemplateHierarchy.orElseThrow(() -> new CoreException(
244 new ToscaNodeTypeNotFoundErrorBuilder(nodeTemplate.getType()).build()));
248 public List<RequirementAssignment> getRequirements(NodeTemplate nodeTemplate,
249 String requirementId) {
250 List<RequirementAssignment> requirements = new ArrayList<>();
251 List<Map<String, RequirementAssignment>> requirementList = nodeTemplate.getRequirements();
252 if (requirementList != null) {
253 requirementList.stream().filter(reqMap -> reqMap.get(requirementId) != null)
255 ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
256 RequirementAssignment reqAssignment = toscaExtensionYamlUtil
257 .yamlToObject(toscaExtensionYamlUtil.objectToYaml(reqMap.get(requirementId)),
258 RequirementAssignment.class);
259 requirements.add(reqAssignment);
266 public Optional<NodeTemplate> getNodeTemplateById(ServiceTemplate serviceTemplate,
267 String nodeTemplateId) {
268 if ((serviceTemplate.getTopology_template() != null)
269 && (serviceTemplate.getTopology_template().getNode_templates() != null)
270 && (serviceTemplate.getTopology_template().getNode_templates().get(nodeTemplateId)
273 .of(serviceTemplate.getTopology_template().getNode_templates().get(nodeTemplateId));
275 return Optional.empty();
279 public Optional<String> getSubstituteServiceTemplateName(String substituteNodeTemplateId,
280 NodeTemplate substitutableNodeTemplate) {
281 if (!isSubstitutableNodeTemplate(substitutableNodeTemplate)) {
282 return Optional.empty();
285 if (substitutableNodeTemplate.getProperties() != null
286 && substitutableNodeTemplate.getProperties()
287 .get(ToscaConstants.SERVICE_TEMPLATE_FILTER_PROPERTY_NAME) != null) {
288 Object serviceTemplateFilter = substitutableNodeTemplate.getProperties()
289 .get(ToscaConstants.SERVICE_TEMPLATE_FILTER_PROPERTY_NAME);
290 if (serviceTemplateFilter != null && serviceTemplateFilter instanceof Map) {
291 Object substituteServiceTemplate = ((Map) serviceTemplateFilter)
292 .get(ToscaConstants.SUBSTITUTE_SERVICE_TEMPLATE_PROPERTY_NAME);
293 if (substituteServiceTemplate == null) {
294 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
295 LoggerTragetServiceName.ADD_ENTITIES_TO_TOSCA, ErrorLevel.ERROR.name(),
296 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.INVALID_PROPERTY);
297 throw new CoreException(
298 new ToscaInvalidSubstituteNodeTemplatePropertiesErrorBuilder(substituteNodeTemplateId)
301 return Optional.of(substituteServiceTemplate.toString());
304 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
305 LoggerTragetServiceName.ADD_ENTITIES_TO_TOSCA, ErrorLevel.ERROR.name(),
306 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.INVALID_PROPERTY);
307 throw new CoreException(
308 new ToscaInvalidSubstituteNodeTemplatePropertiesErrorBuilder(substituteNodeTemplateId)
313 public Map<String, NodeTemplate> getSubstitutableNodeTemplates(ServiceTemplate serviceTemplate) {
314 Map<String, NodeTemplate> substitutableNodeTemplates = new HashMap<>();
316 if (serviceTemplate == null
317 || serviceTemplate.getTopology_template() == null
318 || serviceTemplate.getTopology_template().getNode_templates() == null) {
319 return substitutableNodeTemplates;
322 Map<String, NodeTemplate> nodeTemplates =
323 serviceTemplate.getTopology_template().getNode_templates();
324 for (String nodeTemplateId : nodeTemplates.keySet()) {
325 NodeTemplate nodeTemplate = nodeTemplates.get(nodeTemplateId);
326 if (isSubstitutableNodeTemplate(nodeTemplate)) {
327 substitutableNodeTemplates.put(nodeTemplateId, nodeTemplate);
331 return substitutableNodeTemplates;
335 public Optional<Map.Entry<String, NodeTemplate>> getSubstitutionMappedNodeTemplateByExposedReq(
336 String substituteServiceTemplateFileName, ServiceTemplate substituteServiceTemplate,
337 String requirementId) {
338 if (isSubstitutionServiceTemplate(substituteServiceTemplateFileName,
339 substituteServiceTemplate)) {
340 Map<String, List<String>> substitutionMappingRequirements =
341 substituteServiceTemplate.getTopology_template().getSubstitution_mappings()
343 if (substitutionMappingRequirements != null) {
344 List<String> requirementMapping = substitutionMappingRequirements.get(requirementId);
345 if (requirementMapping != null && !requirementMapping.isEmpty()) {
346 String mappedNodeTemplateId = requirementMapping.get(0);
347 Optional<NodeTemplate> mappedNodeTemplate =
348 getNodeTemplateById(substituteServiceTemplate, mappedNodeTemplateId);
349 mappedNodeTemplate.orElseThrow(() -> new CoreException(
350 new ToscaInvalidEntryNotFoundErrorBuilder("Node Template", mappedNodeTemplateId)
352 Map.Entry<String, NodeTemplate> mappedNodeTemplateEntry =
353 new Map.Entry<String, NodeTemplate>() {
355 public String getKey() {
356 return mappedNodeTemplateId;
360 public NodeTemplate getValue() {
361 return mappedNodeTemplate.get();
365 public NodeTemplate setValue(NodeTemplate value) {
369 return Optional.of(mappedNodeTemplateEntry);
373 return Optional.empty();
377 match only for the input which is not null
380 public boolean isDesiredRequirementAssignment(RequirementAssignment requirementAssignment,
381 String capability, String node,
382 String relationship) {
383 if (capability != null) {
384 if (requirementAssignment.getCapability() == null
385 || !requirementAssignment.getCapability().equals(capability)) {
391 if (requirementAssignment.getNode() == null
392 || !requirementAssignment.getNode().equals(node)) {
397 if (relationship != null) {
398 if (requirementAssignment.getRelationship() == null
399 || !requirementAssignment.getRelationship().equals(relationship)) {
404 return !(capability == null && node == null && relationship == null);
409 public Object getFlatEntity(ToscaElementTypes elementType, String typeId,
410 ServiceTemplate serviceTemplate, ToscaServiceModel toscaModel) {
413 switch (elementType) {
414 case CAPABILITY_TYPE:
415 returnEntity = new CapabilityType();
418 returnEntity = new NodeType();
421 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
422 LoggerTragetServiceName.ADD_ENTITIES_TO_TOSCA, ErrorLevel.ERROR.name(),
423 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.UNSUPPORTED_ENTITY);
424 throw new RuntimeException(
425 "Entity[" + elementType + "] id[" + typeId + "] flat not supported");
428 scanAnFlatEntity(elementType, typeId, returnEntity, serviceTemplate, toscaModel,
429 new ArrayList<String>(), 0);
436 public boolean isSubstitutableNodeTemplate(NodeTemplate nodeTemplate) {
437 return nodeTemplate.getDirectives() != null
438 && nodeTemplate.getDirectives().contains(ToscaConstants
439 .NODE_TEMPLATE_DIRECTIVE_SUBSTITUTABLE);
442 private Optional<Boolean> isNodeTypeExistInServiceTemplateHierarchy(
443 String nodeTypeToMatch,
444 String nodeTypeToSearch,
445 ServiceTemplate serviceTemplate,
446 ToscaServiceModel toscaServiceModel,
447 Set<String> analyzedImportFiles) {
448 Map<String, NodeType> searchableNodeTypes = serviceTemplate.getNode_types();
449 if (!MapUtils.isEmpty(searchableNodeTypes)) {
450 NodeType nodeType = searchableNodeTypes.get(nodeTypeToSearch);
451 if (Objects.nonNull(nodeType)) {
452 if (Objects.equals(nodeType.getDerived_from(), nodeTypeToMatch)) {
453 return Optional.of(true);
454 } else if (isNodeTypeIsToscaRoot(nodeType)) {
455 return Optional.of(false);
457 return isNodeTypeExistInServiceTemplateHierarchy(nodeTypeToMatch,
458 nodeType.getDerived_from(), serviceTemplate, toscaServiceModel, null);
461 return isNodeTypeExistInImports(nodeTypeToMatch, nodeTypeToSearch, serviceTemplate,
462 toscaServiceModel, analyzedImportFiles);
465 return isNodeTypeExistInImports(nodeTypeToMatch, nodeTypeToSearch, serviceTemplate,
466 toscaServiceModel, analyzedImportFiles);
470 private Optional<Boolean> isNodeTypeExistInImports(String nodeTypeToMatch,
471 String nodeTypeToSearch,
472 ServiceTemplate serviceTemplate,
473 ToscaServiceModel toscaServiceModel,
474 Set<String> filesScanned) {
475 List<Map<String, Import>> imports = serviceTemplate.getImports();
476 if (CollectionUtils.isEmpty(imports)) {
477 return Optional.empty();
480 filesScanned = createFilesScannedSet(filesScanned);
482 for (Map<String, Import> map : imports) {
483 ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
484 Import anImport = toscaExtensionYamlUtil
485 .yamlToObject(toscaExtensionYamlUtil.objectToYaml(map.values().iterator().next()),
487 if (Objects.isNull(anImport) || Objects.isNull(anImport.getFile())) {
488 throw new RuntimeException("import without file entry");
490 String importFile = anImport.getFile();
491 ServiceTemplate template =
492 toscaServiceModel.getServiceTemplates().get(fetchFileNameForImport(importFile,
493 serviceTemplate.getMetadata() == null ? null
494 : serviceTemplate.getMetadata().get("filename")));
495 if (filesScanned.contains(ToscaUtil.getServiceTemplateFileName(template))) {
498 filesScanned.add(ToscaUtil.getServiceTemplateFileName(template));
500 Optional<Boolean> nodeTypeExistInServiceTemplateHierarchy =
501 isNodeTypeExistInServiceTemplateHierarchy(nodeTypeToMatch, nodeTypeToSearch, template,
502 toscaServiceModel, filesScanned);
503 if (nodeTypeExistInServiceTemplateHierarchy.isPresent()) {
504 if (nodeTypeExistInServiceTemplateHierarchy.get()) {
505 filesScanned.clear();
506 return Optional.of(true);
511 return Optional.of(false);
514 private Set<String> addImportFileToAnalyzedImportFilesSet(Set<String> analyzedImportFiles,
516 analyzedImportFiles.add(importFile);
517 return analyzedImportFiles;
520 private Set<String> createFilesScannedSet(Set<String> filesScanned) {
521 if (Objects.isNull(filesScanned)) {
522 filesScanned = new HashSet<>();
527 private boolean isNodeTypeIsToscaRoot(NodeType stNodeType) {
528 return Objects.equals(stNodeType.getDerived_from(), ToscaNodeType.NATIVE_ROOT);
531 private boolean isNodeTemplateOfTypeNodeType(NodeTemplate nodeTemplate, String nodeType) {
532 return Objects.equals(nodeTemplate.getType(), nodeType);
535 private boolean isSubstitutionServiceTemplate(String substituteServiceTemplateFileName,
536 ServiceTemplate substituteServiceTemplate) {
537 if (substituteServiceTemplate != null
538 && substituteServiceTemplate.getTopology_template() != null
539 && substituteServiceTemplate.getTopology_template().getSubstitution_mappings() != null) {
540 if (substituteServiceTemplate.getTopology_template().getSubstitution_mappings()
541 .getNode_type() == null) {
542 throw new CoreException(new ToscaInvalidSubstitutionServiceTemplateErrorBuilder(
543 substituteServiceTemplateFileName).build());
551 private boolean scanAnFlatEntity(ToscaElementTypes elementType, String typeId, Object entity,
552 ServiceTemplate serviceTemplate, ToscaServiceModel toscaModel,
553 List<String> filesScanned, int rootScanStartInx) {
556 boolean entityFound =
557 enrichEntityFromCurrentServiceTemplate(elementType, typeId, entity, serviceTemplate,
558 toscaModel, filesScanned, rootScanStartInx);
560 List<Map<String, Import>> imports = serviceTemplate.getImports();
561 if (CollectionUtils.isEmpty(imports)) {
564 ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
565 boolean found = false;
566 for (Map<String, Import> importMap : imports) {
570 String filename = "";
571 for (Object importObject : importMap.values()) {
572 Import importServiceTemplate = toscaExtensionYamlUtil
573 .yamlToObject(toscaExtensionYamlUtil.objectToYaml(importObject), Import.class);
574 filename = fetchFileNameForImport(importServiceTemplate.getFile(),
575 serviceTemplate.getMetadata() == null ? null : serviceTemplate.getMetadata().get
577 if (filesScanned.contains(filename)) {
580 filesScanned.add(filename);
582 ServiceTemplate template =
583 toscaModel.getServiceTemplates()
586 scanAnFlatEntity(elementType, typeId, entity, template, toscaModel, filesScanned,
587 filesScanned.size());
595 private String fetchFileNameForImport(String importServiceTemplateFile,
596 String currentMetadatafileName) {
597 if (importServiceTemplateFile.contains("../")) {
598 return importServiceTemplateFile.replace("../", "");
599 } else if (importServiceTemplateFile.contains("/")) {
600 return importServiceTemplateFile;
601 } else if (currentMetadatafileName != null) {
602 return currentMetadatafileName.substring(0, currentMetadatafileName.indexOf("/")) + "/" +
603 importServiceTemplateFile;
605 return importServiceTemplateFile;
610 private boolean enrichEntityFromCurrentServiceTemplate(ToscaElementTypes elementType,
611 String typeId, Object entity,
612 ServiceTemplate serviceTemplate,
613 ToscaServiceModel toscaModel,
614 List<String> filesScanned,
615 int rootScanStartInx) {
617 switch (elementType) {
618 case CAPABILITY_TYPE:
619 if (serviceTemplate.getCapability_types() != null
620 && serviceTemplate.getCapability_types().containsKey(typeId)) {
622 filesScanned.clear();
623 CapabilityType targetCapabilityType = ((CapabilityType) entity);
624 CapabilityType sourceCapabilityType = serviceTemplate.getCapability_types().get(typeId);
625 derivedFrom = sourceCapabilityType.getDerived_from();
626 if (derivedFrom != null) {
627 scanAnFlatEntity(elementType, derivedFrom, entity, serviceTemplate, toscaModel,
628 filesScanned, rootScanStartInx);
630 combineCapabilityTypeInfo(sourceCapabilityType, targetCapabilityType);
636 if (serviceTemplate.getNode_types() != null
637 && serviceTemplate.getNode_types().containsKey(typeId)) {
639 filesScanned.clear();
640 NodeType targetNodeType = ((NodeType) entity);
641 NodeType sourceNodeType = serviceTemplate.getNode_types().get(typeId);
642 derivedFrom = sourceNodeType.getDerived_from();
643 if (derivedFrom != null) {
644 scanAnFlatEntity(elementType, derivedFrom, entity, serviceTemplate, toscaModel,
645 filesScanned, rootScanStartInx);
647 combineNodeTypeInfo(sourceNodeType, targetNodeType);
653 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
654 LoggerTragetServiceName.ADD_ENTITIES_TO_TOSCA, ErrorLevel.ERROR.name(),
655 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.UNSUPPORTED_ENTITY);
656 throw new RuntimeException(
657 "Entity[" + elementType + "] id[" + typeId + "] flat not supported");
665 private void combineNodeTypeInfo(NodeType sourceNodeType, NodeType targetNodeType) {
666 targetNodeType.setDerived_from(sourceNodeType.getDerived_from());
667 targetNodeType.setDescription(sourceNodeType.getDescription());
668 targetNodeType.setVersion(sourceNodeType.getVersion());
669 targetNodeType.setProperties(
670 CommonMethods.mergeMaps(targetNodeType.getProperties(), sourceNodeType.getProperties()));
671 targetNodeType.setInterfaces(
672 CommonMethods.mergeMaps(targetNodeType.getInterfaces(), sourceNodeType.getInterfaces()));
673 targetNodeType.setArtifacts(
674 CommonMethods.mergeMaps(targetNodeType.getArtifacts(), sourceNodeType.getArtifacts()));
675 targetNodeType.setAttributes(
676 CommonMethods.mergeMaps(targetNodeType.getAttributes(), sourceNodeType.getAttributes()));
677 targetNodeType.setCapabilities(CommonMethods
678 .mergeMaps(targetNodeType.getCapabilities(), sourceNodeType.getCapabilities()));
679 targetNodeType.setRequirements(CommonMethods
680 .mergeListsOfMap(targetNodeType.getRequirements(), sourceNodeType.getRequirements()));
685 private void combineCapabilityTypeInfo(CapabilityType sourceCapabilityType,
686 CapabilityType targetCapabilityType) {
688 targetCapabilityType.setAttributes(CommonMethods
689 .mergeMaps(targetCapabilityType.getAttributes(), sourceCapabilityType.getAttributes()));
690 targetCapabilityType.setProperties(CommonMethods
691 .mergeMaps(targetCapabilityType.getProperties(), sourceCapabilityType.getProperties()));
692 targetCapabilityType.setValid_source_types(CommonMethods
693 .mergeLists(targetCapabilityType.getValid_source_types(),
694 sourceCapabilityType.getValid_source_types()));
696 if (!CommonMethods.isEmpty(sourceCapabilityType.getDerived_from())) {
697 targetCapabilityType.setDerived_from(sourceCapabilityType.getDerived_from());
699 if (!CommonMethods.isEmpty(sourceCapabilityType.getDescription())) {
700 targetCapabilityType.setDescription(sourceCapabilityType.getDescription());
702 if (!CommonMethods.isEmpty(sourceCapabilityType.getVersion())) {
703 targetCapabilityType.setVersion(sourceCapabilityType.getVersion());
711 * Create node type according to the input substitution service template, while the substitution
712 * service template can be mappted to this node type, for substitution mapping.
714 * @param substitutionServiceTemplate substitution serivce template
715 * @param nodeTypeDerivedFromValue derived from value for the created node type
716 * @return the node type
719 public NodeType createInitSubstitutionNodeType(ServiceTemplate substitutionServiceTemplate,
720 String nodeTypeDerivedFromValue) {
721 NodeType substitutionNodeType = new NodeType();
722 substitutionNodeType.setDerived_from(nodeTypeDerivedFromValue);
723 substitutionNodeType.setDescription(substitutionServiceTemplate.getDescription());
725 .setProperties(manageSubstitutionNodeTypeProperties(substitutionServiceTemplate));
727 .setAttributes(manageSubstitutionNodeTypeAttributes(substitutionServiceTemplate));
728 return substitutionNodeType;
731 public Map<String, PropertyDefinition> manageSubstitutionNodeTypeProperties(
732 ServiceTemplate substitutionServiceTemplate) {
733 Map<String, PropertyDefinition> substitutionNodeTypeProperties = new HashMap<>();
734 Map<String, ParameterDefinition> properties =
735 substitutionServiceTemplate.getTopology_template().getInputs();
736 if (properties == null) {
740 PropertyDefinition propertyDefinition;
741 String toscaPropertyName;
742 for (Map.Entry<String, ParameterDefinition> entry : properties.entrySet()) {
743 toscaPropertyName = entry.getKey();
744 propertyDefinition = new PropertyDefinition();
745 ParameterDefinition parameterDefinition =
746 substitutionServiceTemplate.getTopology_template().getInputs().get(toscaPropertyName);
747 propertyDefinition.setType(parameterDefinition.getType());
748 propertyDefinition.setDescription(parameterDefinition.getDescription());
749 propertyDefinition.set_default(parameterDefinition.get_default());
750 if (parameterDefinition.getRequired() != null) {
751 propertyDefinition.setRequired(parameterDefinition.getRequired());
753 if (propertyDefinition.get_default() != null) {
754 propertyDefinition.setRequired(false);
756 if (!CollectionUtils.isEmpty(parameterDefinition.getConstraints())) {
757 propertyDefinition.setConstraints(parameterDefinition.getConstraints());
759 propertyDefinition.setEntry_schema(parameterDefinition.getEntry_schema());
760 if (parameterDefinition.getStatus() != null) {
761 propertyDefinition.setStatus(parameterDefinition.getStatus());
763 substitutionNodeTypeProperties.put(toscaPropertyName, propertyDefinition);
765 return substitutionNodeTypeProperties;
768 private Map<String, AttributeDefinition> manageSubstitutionNodeTypeAttributes(
769 ServiceTemplate substitutionServiceTemplate) {
770 Map<String, AttributeDefinition> substitutionNodeTypeAttributes = new HashMap<>();
771 Map<String, ParameterDefinition> attributes =
772 substitutionServiceTemplate.getTopology_template().getOutputs();
773 if (attributes == null) {
776 AttributeDefinition attributeDefinition;
777 String toscaAttributeName;
779 for (Map.Entry<String, ParameterDefinition> entry : attributes.entrySet()) {
780 attributeDefinition = new AttributeDefinition();
781 toscaAttributeName = entry.getKey();
782 ParameterDefinition parameterDefinition =
783 substitutionServiceTemplate.getTopology_template().getOutputs().get(toscaAttributeName);
784 if (parameterDefinition.getType() != null && !parameterDefinition.getType().isEmpty()) {
785 attributeDefinition.setType(parameterDefinition.getType());
787 attributeDefinition.setType(PropertyType.STRING.getDisplayName());
789 attributeDefinition.setDescription(parameterDefinition.getDescription());
790 attributeDefinition.set_default(parameterDefinition.get_default());
791 attributeDefinition.setEntry_schema(parameterDefinition.getEntry_schema());
792 if (Objects.nonNull(parameterDefinition.getStatus())) {
793 attributeDefinition.setStatus(parameterDefinition.getStatus());
795 substitutionNodeTypeAttributes.put(toscaAttributeName, attributeDefinition);
797 return substitutionNodeTypeAttributes;
801 * Checks if the requirement exists in the node template.
803 * @param nodeTemplate the node template
804 * @param requirementId the requirement id
805 * @param requirementAssignment the requirement assignment
806 * @return true if the requirement already exists and false otherwise
808 public boolean isRequirementExistInNodeTemplate(NodeTemplate nodeTemplate,
809 String requirementId,
810 RequirementAssignment requirementAssignment) {
811 boolean result = false;
812 List<Map<String, RequirementAssignment>> nodeTemplateRequirements = nodeTemplate
814 if (nodeTemplateRequirements != null) {
815 for (Map<String, RequirementAssignment> requirement : nodeTemplateRequirements) {
816 if (requirement.containsKey(requirementId)) {
817 result = DataModelUtil.compareRequirementAssignment(requirementAssignment,
818 requirement.get(requirementId));