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.be.tosca;
23 import com.fasterxml.jackson.databind.ObjectMapper;
24 import com.fasterxml.jackson.databind.SerializationFeature;
25 import fj.data.Either;
26 import org.apache.commons.collections.CollectionUtils;
27 import org.apache.commons.collections.MapUtils;
28 import org.apache.commons.lang.StringUtils;
29 import org.apache.commons.lang3.tuple.ImmutablePair;
30 import org.apache.commons.lang3.tuple.ImmutableTriple;
31 import org.apache.commons.lang3.tuple.Triple;
32 import org.onap.sdc.tosca.services.YamlUtil;
33 import org.openecomp.sdc.be.components.impl.exceptions.SdcResourceNotFoundException;
34 import org.openecomp.sdc.be.config.ConfigurationManager;
35 import org.openecomp.sdc.be.dao.titan.TitanOperationStatus;
36 import org.openecomp.sdc.be.datatypes.components.ResourceMetadataDataDefinition;
37 import org.openecomp.sdc.be.datatypes.elements.*;
38 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
39 import org.openecomp.sdc.be.datatypes.enums.OriginTypeEnum;
40 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
41 import org.openecomp.sdc.be.model.*;
42 import org.openecomp.sdc.be.model.cache.ApplicationDataTypeCache;
43 import org.openecomp.sdc.be.model.category.CategoryDefinition;
44 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
45 import org.openecomp.sdc.be.model.jsontitan.utils.ModelConverter;
46 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
47 import org.openecomp.sdc.be.model.operations.impl.InterfaceLifecycleOperation;
48 import org.openecomp.sdc.be.model.tosca.converters.ToscaValueBaseConverter;
49 import org.openecomp.sdc.be.tosca.model.*;
50 import org.openecomp.sdc.be.tosca.utils.ForwardingPathToscaUtil;
51 import org.openecomp.sdc.be.tosca.utils.InputConverter;
52 import org.openecomp.sdc.be.tosca.utils.InterfacesOperationsToscaUtil;
53 import org.openecomp.sdc.common.log.wrappers.Logger;
54 import org.openecomp.sdc.externalupload.utils.ServiceUtils;
55 import org.springframework.beans.factory.annotation.Autowired;
56 import org.yaml.snakeyaml.DumperOptions;
57 import org.yaml.snakeyaml.DumperOptions.FlowStyle;
58 import org.yaml.snakeyaml.Yaml;
59 import org.yaml.snakeyaml.introspector.BeanAccess;
60 import org.yaml.snakeyaml.introspector.Property;
61 import org.yaml.snakeyaml.introspector.PropertyUtils;
62 import org.yaml.snakeyaml.nodes.MappingNode;
63 import org.yaml.snakeyaml.nodes.Node;
64 import org.yaml.snakeyaml.nodes.NodeTuple;
65 import org.yaml.snakeyaml.nodes.Tag;
66 import org.yaml.snakeyaml.representer.Represent;
67 import org.yaml.snakeyaml.representer.Representer;
69 import java.beans.IntrospectionException;
71 import java.util.Map.Entry;
72 import java.util.function.Supplier;
73 import java.util.stream.Collectors;
75 import static org.apache.commons.collections.CollectionUtils.isEmpty;
76 import static org.apache.commons.collections.CollectionUtils.isNotEmpty;
77 import static org.apache.commons.collections.MapUtils.isNotEmpty;
78 import static org.apache.commons.lang.StringUtils.isNotEmpty;
79 import static org.openecomp.sdc.be.tosca.utils.InterfacesOperationsToscaUtil.addInterfaceDefinitionElement;
80 import static org.openecomp.sdc.be.tosca.utils.InterfacesOperationsToscaUtil.addInterfaceTypeElement;
82 @org.springframework.stereotype.Component("tosca-export-handler")
83 public class ToscaExportHandler {
85 private ApplicationDataTypeCache dataTypeCache;
86 private ToscaOperationFacade toscaOperationFacade;
87 private CapabilityRequirementConverter capabilityRequirementConverter;
88 private PolicyExportParser policyExportParser;
89 private GroupExportParser groupExportParser;
90 private PropertyConvertor propertyConvertor;
91 private InputConverter inputConverter;
92 private InterfaceLifecycleOperation interfaceLifecycleOperation;
95 public ToscaExportHandler(ApplicationDataTypeCache dataTypeCache, ToscaOperationFacade toscaOperationFacade,
96 CapabilityRequirementConverter capabilityRequirementConverter, PolicyExportParser policyExportParser,
97 GroupExportParser groupExportParser, InputConverter inputConverter, InterfaceLifecycleOperation interfaceLifecycleOperation) {
98 this.dataTypeCache = dataTypeCache;
99 this.toscaOperationFacade = toscaOperationFacade;
100 this.capabilityRequirementConverter = capabilityRequirementConverter;
101 this.policyExportParser = policyExportParser;
102 this.groupExportParser = groupExportParser;
103 this.propertyConvertor = PropertyConvertor.getInstance();
104 this.inputConverter = inputConverter;
105 this.interfaceLifecycleOperation = interfaceLifecycleOperation;
109 private static final Logger log = Logger.getLogger(ToscaExportHandler.class);
111 private static final String TOSCA_VERSION = "tosca_simple_yaml_1_1";
112 private static final String SERVICE_NODE_TYPE_PREFIX = "org.openecomp.service.";
113 private static final String IMPORTS_FILE_KEY = "file";
114 private static final String TOSCA_INTERFACE_NAME = "-interface.yml";
115 public static final String ASSET_TOSCA_TEMPLATE = "assettoscatemplate";
116 private static final String FAILED_TO_GET_DEFAULT_IMPORTS_CONFIGURATION = "convertToToscaTemplate - failed to get Default Imports section from configuration";
117 private static final String NOT_SUPPORTED_COMPONENT_TYPE = "Not supported component type {}";
118 private static final List<Map<String, Map<String, String>>> DEFAULT_IMPORTS = ConfigurationManager
119 .getConfigurationManager().getConfiguration().getDefaultImports();
120 private static YamlUtil yamlUtil = new YamlUtil();
122 public ToscaExportHandler(){}
124 public Either<ToscaRepresentation, ToscaError> exportComponent(Component component) {
126 Either<ToscaTemplate, ToscaError> toscaTemplateRes = convertToToscaTemplate(component);
127 if (toscaTemplateRes.isRight()) {
128 return Either.right(toscaTemplateRes.right().value());
131 ToscaTemplate toscaTemplate = toscaTemplateRes.left().value();
132 ToscaRepresentation toscaRepresentation = this.createToscaRepresentation(toscaTemplate);
133 return Either.left(toscaRepresentation);
136 public Either<ToscaRepresentation, ToscaError> exportComponentInterface(Component component,
137 boolean isAssociatedResourceComponent) {
138 if (null == DEFAULT_IMPORTS) {
139 log.debug(FAILED_TO_GET_DEFAULT_IMPORTS_CONFIGURATION);
140 return Either.right(ToscaError.GENERAL_ERROR);
143 ToscaTemplate toscaTemplate = new ToscaTemplate(TOSCA_VERSION);
144 toscaTemplate.setImports(new ArrayList<>(DEFAULT_IMPORTS));
145 Map<String, ToscaNodeType> nodeTypes = new HashMap<>();
146 Either<ToscaTemplate, ToscaError> toscaTemplateRes = convertInterfaceNodeType(new HashMap<>(), component,
147 toscaTemplate, nodeTypes, isAssociatedResourceComponent);
148 if (toscaTemplateRes.isRight()) {
149 return Either.right(toscaTemplateRes.right().value());
152 toscaTemplate = toscaTemplateRes.left().value();
153 ToscaRepresentation toscaRepresentation = this.createToscaRepresentation(toscaTemplate);
154 return Either.left(toscaRepresentation);
157 public ToscaRepresentation createToscaRepresentation(ToscaTemplate toscaTemplate) {
158 CustomRepresenter representer = new CustomRepresenter();
159 DumperOptions options = new DumperOptions();
160 options.setAllowReadOnlyProperties(false);
161 options.setPrettyFlow(true);
163 options.setDefaultFlowStyle(FlowStyle.FLOW);
164 options.setCanonical(false);
166 representer.addClassTag(toscaTemplate.getClass(), Tag.MAP);
168 representer.setPropertyUtils(new UnsortedPropertyUtils());
169 Yaml yaml = new Yaml(representer, options);
171 String yamlAsString = yaml.dumpAsMap(toscaTemplate);
173 StringBuilder sb = new StringBuilder();
174 sb.append(ConfigurationManager.getConfigurationManager().getConfiguration().getHeatEnvArtifactHeader());
175 sb.append(yamlAsString);
176 sb.append(ConfigurationManager.getConfigurationManager().getConfiguration().getHeatEnvArtifactFooter());
178 ToscaRepresentation toscaRepresentation = new ToscaRepresentation();
179 toscaRepresentation.setMainYaml(sb.toString());
180 toscaRepresentation.setDependencies(toscaTemplate.getDependencies());
182 return toscaRepresentation;
185 public Either<ToscaTemplate, ToscaError> getDependencies(Component component) {
186 ToscaTemplate toscaTemplate = new ToscaTemplate(null);
187 Either<ImmutablePair<ToscaTemplate, Map<String, Component>>, ToscaError> fillImports = fillImports(component,
189 if (fillImports.isRight()) {
190 return Either.right(fillImports.right().value());
192 return Either.left(fillImports.left().value().left);
195 private Either<ToscaTemplate, ToscaError> convertToToscaTemplate(Component component) {
196 if (null == DEFAULT_IMPORTS) {
197 log.debug(FAILED_TO_GET_DEFAULT_IMPORTS_CONFIGURATION);
198 return Either.right(ToscaError.GENERAL_ERROR);
201 log.trace("start tosca export for {}", component.getUniqueId());
202 ToscaTemplate toscaTemplate = new ToscaTemplate(TOSCA_VERSION);
204 toscaTemplate.setMetadata(convertMetadata(component));
205 toscaTemplate.setImports(new ArrayList<>(DEFAULT_IMPORTS));
206 Map<String, ToscaNodeType> nodeTypes = new HashMap<>();
207 if (ModelConverter.isAtomicComponent(component)) {
208 log.trace("convert component as node type");
209 return convertNodeType(new HashMap<>(), component, toscaTemplate, nodeTypes);
211 log.trace("convert component as topology template");
212 return convertToscaTemplate(component, toscaTemplate);
217 private Either<ToscaTemplate, ToscaError> convertToscaTemplate(Component component, ToscaTemplate toscaNode) {
219 Either<ImmutablePair<ToscaTemplate, Map<String, Component>>, ToscaError> importsRes = fillImports(component,
221 if (importsRes.isRight()) {
222 return Either.right(importsRes.right().value());
224 toscaNode = importsRes.left().value().left;
225 Map<String, Component> componentCache = importsRes.left().value().right;
226 Either<Map<String, ToscaNodeType>, ToscaError> nodeTypesMapEither = createProxyNodeTypes(componentCache , component );
227 if (nodeTypesMapEither.isRight()) {
228 log.debug("Failed to fetch normative service proxy resource by tosca name, error {}",
229 nodeTypesMapEither.right().value());
230 return Either.right(nodeTypesMapEither.right().value());
232 Map<String, ToscaNodeType> nodeTypesMap = nodeTypesMapEither.left().value();
233 if (nodeTypesMap != null && !nodeTypesMap.isEmpty()) {
234 toscaNode.setNode_types(nodeTypesMap);
238 Either<Map<String, DataTypeDefinition>, TitanOperationStatus> dataTypesEither = dataTypeCache.getAll();
239 if (dataTypesEither.isRight()) {
240 log.debug("Failed to retrieve all data types {}", dataTypesEither.right().value());
241 return Either.right(ToscaError.GENERAL_ERROR);
243 Map<String, DataTypeDefinition> dataTypes = dataTypesEither.left().value();
244 ToscaTopolgyTemplate topologyTemplate = new ToscaTopolgyTemplate();
245 List<InputDefinition> inputDef = component.getInputs();
246 Map<String, ToscaProperty> inputs = inputConverter.convertInputs(inputDef, dataTypes);
248 if (!inputs.isEmpty()) {
249 topologyTemplate.setInputs(inputs);
252 List<ComponentInstance> componentInstances = component.getComponentInstances();
253 Map<String, List<ComponentInstanceProperty>> componentInstancesProperties =
254 component.getComponentInstancesProperties();
255 Map<String, List<ComponentInstanceInterface>> componentInstanceInterfaces =
256 component.getComponentInstancesInterfaces();
257 if (componentInstances != null && !componentInstances.isEmpty()) {
259 Either<Map<String, ToscaNodeTemplate>, ToscaError> nodeTemplates =
260 convertNodeTemplates(component, componentInstances,
261 componentInstancesProperties, componentInstanceInterfaces,
262 componentCache, dataTypes, topologyTemplate);
263 if (nodeTemplates.isRight()) {
264 return Either.right(nodeTemplates.right().value());
266 log.debug("node templates converted");
268 topologyTemplate.setNode_templates(nodeTemplates.left().value());
272 addGroupsToTopologyTemplate(component, topologyTemplate);
275 addPoliciesToTopologyTemplate(component, topologyTemplate);
276 } catch (SdcResourceNotFoundException e) {
277 log.debug("Fail to add policies to topology template:",e);
278 return Either.right(ToscaError.GENERAL_ERROR);
282 SubstitutionMapping substitutionMapping = new SubstitutionMapping();
283 String toscaResourceName;
284 switch (component.getComponentType()) {
286 toscaResourceName = ((ResourceMetadataDataDefinition) component.getComponentMetadataDefinition()
287 .getMetadataDataDefinition()).getToscaResourceName();
290 toscaResourceName = SERVICE_NODE_TYPE_PREFIX
291 + component.getComponentMetadataDefinition().getMetadataDataDefinition().getSystemName();
294 log.debug(NOT_SUPPORTED_COMPONENT_TYPE, component.getComponentType());
295 return Either.right(ToscaError.NOT_SUPPORTED_TOSCA_TYPE);
297 substitutionMapping.setNode_type(toscaResourceName);
299 Either<SubstitutionMapping, ToscaError> capabilities = convertCapabilities(component, substitutionMapping, componentCache);
300 if (capabilities.isRight()) {
301 return Either.right(capabilities.right().value());
303 substitutionMapping = capabilities.left().value();
305 Either<SubstitutionMapping, ToscaError> requirements = capabilityRequirementConverter
306 .convertSubstitutionMappingRequirements(componentCache, component, substitutionMapping);
307 if (requirements.isRight()) {
308 return Either.right(requirements.right().value());
310 substitutionMapping = requirements.left().value();
312 topologyTemplate.setSubstitution_mappings(substitutionMapping);
314 toscaNode.setTopology_template(topologyTemplate);
315 return Either.left(toscaNode);
318 private void addGroupsToTopologyTemplate(Component component, ToscaTopolgyTemplate topologyTemplate) {
321 Map<String, ToscaGroupTemplate> groups = groupExportParser.getGroups(component);
323 topologyTemplate.addGroups(groups);
327 private void addPoliciesToTopologyTemplate(Component component, ToscaTopolgyTemplate topologyTemplate)
328 throws SdcResourceNotFoundException {
329 Map<String, ToscaPolicyTemplate> policies = policyExportParser.getPolicies(component);
330 if(policies!= null) {
331 topologyTemplate.addPolicies(policies);
335 private ToscaMetadata convertMetadata(Component component) {
336 return convertMetadata(component, false, null);
339 private ToscaMetadata convertMetadata(Component component, boolean isInstance,
340 ComponentInstance componentInstance) {
341 ToscaMetadata toscaMetadata = new ToscaMetadata();
342 toscaMetadata.setInvariantUUID(component.getInvariantUUID());
343 toscaMetadata.setUUID(component.getUUID());
344 toscaMetadata.setDescription(component.getDescription());
345 toscaMetadata.setName(component.getComponentMetadataDefinition().getMetadataDataDefinition().getName());
347 List<CategoryDefinition> categories = component.getCategories();
348 CategoryDefinition categoryDefinition = categories.get(0);
349 toscaMetadata.setCategory(categoryDefinition.getName());
352 toscaMetadata.setVersion(component.getVersion());
353 toscaMetadata.setCustomizationUUID(componentInstance.getCustomizationUUID());
354 if (componentInstance.getSourceModelInvariant() != null
355 && !componentInstance.getSourceModelInvariant().isEmpty()) {
356 toscaMetadata.setVersion(componentInstance.getComponentVersion());
357 toscaMetadata.setSourceModelInvariant(componentInstance.getSourceModelInvariant());
358 toscaMetadata.setSourceModelUuid(componentInstance.getSourceModelUuid());
359 toscaMetadata.setSourceModelName(componentInstance.getSourceModelName());
360 toscaMetadata.setName(
361 componentInstance.getSourceModelName() + " " + OriginTypeEnum.ServiceProxy.getDisplayValue());
362 toscaMetadata.setDescription(componentInstance.getDescription());
366 switch (component.getComponentType()) {
368 Resource resource = (Resource) component;
370 if (isInstance && componentInstance.getOriginType() == OriginTypeEnum.ServiceProxy) {
371 toscaMetadata.setType(componentInstance.getOriginType().getDisplayValue());
373 toscaMetadata.setType(resource.getResourceType().name());
375 toscaMetadata.setSubcategory(categoryDefinition.getSubcategories().get(0).getName());
376 toscaMetadata.setResourceVendor(resource.getVendorName());
377 toscaMetadata.setResourceVendorRelease(resource.getVendorRelease());
378 toscaMetadata.setResourceVendorModelNumber(resource.getResourceVendorModelNumber());
381 Service service = (Service) component;
382 toscaMetadata.setType(component.getComponentType().getValue());
383 toscaMetadata.setServiceType(service.getServiceType());
384 toscaMetadata.setServiceRole(service.getServiceRole());
385 toscaMetadata.setEnvironmentContext(service.getEnvironmentContext());
386 resolveInstantiationTypeAndSetItToToscaMetaData(toscaMetadata, service);
389 toscaMetadata.setServiceEcompNaming(((Service) component).isEcompGeneratedNaming());
390 toscaMetadata.setEcompGeneratedNaming(((Service) component).isEcompGeneratedNaming());
391 toscaMetadata.setNamingPolicy(((Service) component).getNamingPolicy());
395 log.debug(NOT_SUPPORTED_COMPONENT_TYPE, component.getComponentType());
397 return toscaMetadata;
400 private void resolveInstantiationTypeAndSetItToToscaMetaData(ToscaMetadata toscaMetadata, Service service) {
401 if (service.getInstantiationType() != null) {
402 toscaMetadata.setInstantiationType(service.getInstantiationType());
405 toscaMetadata.setInstantiationType(StringUtils.EMPTY);
409 private Either<ImmutablePair<ToscaTemplate, Map<String, Component>>, ToscaError> fillImports(Component component,
410 ToscaTemplate toscaTemplate) {
412 if (null == DEFAULT_IMPORTS) {
413 log.debug(FAILED_TO_GET_DEFAULT_IMPORTS_CONFIGURATION);
414 return Either.right(ToscaError.GENERAL_ERROR);
416 Map<String, Component> componentCache = new HashMap<>();
418 if (!ModelConverter.isAtomicComponent(component)) {
419 List<Map<String, Map<String, String>>> additionalImports = toscaTemplate.getImports() == null
420 ? new ArrayList<>(DEFAULT_IMPORTS) : new ArrayList<>(toscaTemplate.getImports());
422 List<Triple<String, String, Component>> dependecies = new ArrayList<>();
424 Map<String, ArtifactDefinition> toscaArtifacts = component.getToscaArtifacts();
425 if (isNotEmpty(toscaArtifacts)) {
426 ArtifactDefinition artifactDefinition = toscaArtifacts.get(ToscaExportHandler.ASSET_TOSCA_TEMPLATE);
427 if(artifactDefinition != null) {
428 Map<String, Map<String, String>> importsListMember = new HashMap<>();
429 Map<String, String> interfaceFiles = new HashMap<>();
430 interfaceFiles.put(IMPORTS_FILE_KEY, getInterfaceFilename(artifactDefinition.getArtifactName()));
431 StringBuilder keyNameBuilder = new StringBuilder();
432 keyNameBuilder.append(component.getComponentType().toString().toLowerCase()).append("-")
433 .append(component.getName()).append("-interface");
434 importsListMember.put(keyNameBuilder.toString(), interfaceFiles);
435 additionalImports.add(importsListMember);
438 List<ComponentInstance> componentInstances = component.getComponentInstances();
439 if (componentInstances != null && !componentInstances.isEmpty()) {
440 componentInstances.forEach(ci -> createDependency(componentCache, additionalImports, dependecies, ci));
442 toscaTemplate.setDependencies(dependecies);
443 toscaTemplate.setImports(additionalImports);
445 log.debug("currently imports supported for VF and service only");
447 return Either.left(new ImmutablePair<>(toscaTemplate, componentCache));
450 private void createDependency(Map<String, Component> componentCache, List<Map<String, Map<String, String>>> imports,
451 List<Triple<String, String, Component>> dependecies, ComponentInstance ci) {
452 Map<String, String> files = new HashMap<>();
453 Map<String, Map<String, String>> importsListMember = new HashMap<>();
454 StringBuilder keyNameBuilder;
456 Component componentRI = componentCache.get(ci.getComponentUid());
457 if (componentRI == null) {
458 // all resource must be only once!
459 Either<Component, StorageOperationStatus> resource = toscaOperationFacade
460 .getToscaFullElement(ci.getComponentUid());
461 if ((resource.isRight()) && (log.isDebugEnabled())) {
462 log.debug("Failed to fetch resource with id {} for instance {}",ci.getComponentUid() ,ci.getUniqueId());
466 Component fetchedComponent = resource.left().value();
467 componentCache.put(fetchedComponent.getUniqueId(), fetchedComponent);
469 if (ci.getOriginType() == OriginTypeEnum.ServiceProxy){
470 Either<Component, StorageOperationStatus> sourceService = toscaOperationFacade
471 .getToscaFullElement(ci.getSourceModelUid());
472 if (sourceService.isRight() && (log.isDebugEnabled())) {
473 log.debug("Failed to fetch source service with id {} for proxy {}", ci.getSourceModelUid(), ci.getUniqueId());
475 Component fetchedSource = sourceService.left().value();
476 componentCache.put(fetchedSource.getUniqueId(), fetchedSource);
479 componentRI = fetchedComponent;
481 Map<String, ArtifactDefinition> toscaArtifacts = componentRI.getToscaArtifacts();
482 ArtifactDefinition artifactDefinition = toscaArtifacts.get(ASSET_TOSCA_TEMPLATE);
483 if (artifactDefinition != null) {
484 String artifactName = artifactDefinition.getArtifactName();
485 files.put(IMPORTS_FILE_KEY, artifactName);
486 keyNameBuilder = new StringBuilder();
487 keyNameBuilder.append(fetchedComponent.getComponentType().toString().toLowerCase());
488 keyNameBuilder.append("-");
489 keyNameBuilder.append(ci.getComponentName());
490 importsListMember.put(keyNameBuilder.toString(), files);
491 imports.add(importsListMember);
492 dependecies.add(new ImmutableTriple<>(artifactName,
493 artifactDefinition.getEsId(), fetchedComponent));
495 if (!ModelConverter.isAtomicComponent(componentRI)) {
496 importsListMember = new HashMap<>();
497 Map<String, String> interfaceFiles = new HashMap<>();
498 interfaceFiles.put(IMPORTS_FILE_KEY, getInterfaceFilename(artifactName));
499 keyNameBuilder.append("-interface");
500 importsListMember.put(keyNameBuilder.toString(), interfaceFiles);
501 imports.add(importsListMember);
507 public static String getInterfaceFilename(String artifactName) {
508 return artifactName.substring(0, artifactName.lastIndexOf('.')) + ToscaExportHandler.TOSCA_INTERFACE_NAME;
511 private Either<ToscaTemplate, ToscaError> convertNodeType(Map<String, Component> componentsCache, Component component, ToscaTemplate toscaNode,
512 Map<String, ToscaNodeType> nodeTypes) {
513 log.debug("start convert node type for {}", component.getUniqueId());
514 ToscaNodeType toscaNodeType = createNodeType(component);
516 Either<Map<String, DataTypeDefinition>, TitanOperationStatus> dataTypesEither = dataTypeCache.getAll();
517 if (dataTypesEither.isRight()) {
518 log.debug("Failed to fetch all data types :", dataTypesEither.right().value());
519 return Either.right(ToscaError.GENERAL_ERROR);
522 Map<String, DataTypeDefinition> dataTypes = dataTypesEither.left().value();
523 Either<ToscaNodeType, ToscaError> properties = propertyConvertor.convertProperties(component, toscaNodeType,
525 if (properties.isRight()) {
526 return Either.right(properties.right().value());
528 toscaNodeType = properties.left().value();
529 log.debug("Properties converted for {}", component.getUniqueId());
531 // Extracted to method for code reuse
532 return convertReqCapAndTypeName(componentsCache, component, toscaNode, nodeTypes, toscaNodeType, dataTypes);
535 private Either<ToscaTemplate, ToscaError> convertInterfaceNodeType(Map<String, Component> componentsCache,
536 Component component, ToscaTemplate toscaNode,
537 Map<String, ToscaNodeType> nodeTypes,
538 boolean isAssociatedResourceComponent) {
539 log.debug("start convert node type for {}", component.getUniqueId());
540 ToscaNodeType toscaNodeType = createNodeType(component);
542 Either<Map<String, InterfaceDefinition>, StorageOperationStatus> lifecycleTypeEither =
543 interfaceLifecycleOperation.getAllInterfaceLifecycleTypes();
544 if(lifecycleTypeEither.isRight()){
545 log.debug("Failed to fetch all interface types :", lifecycleTypeEither.right().value());
546 return Either.right(ToscaError.GENERAL_ERROR);
548 List<String> allGlobalInterfaceTypes = lifecycleTypeEither.left().value()
551 .map(interfaceDef -> interfaceDef.getType())
552 .collect(Collectors.toList());
553 toscaNode.setInterface_types(addInterfaceTypeElement(component, allGlobalInterfaceTypes));
555 Either<Map<String, DataTypeDefinition>, TitanOperationStatus> dataTypesEither = dataTypeCache.getAll();
556 if (dataTypesEither.isRight()) {
557 log.debug("Failed to fetch all data types :", dataTypesEither.right().value());
558 return Either.right(ToscaError.GENERAL_ERROR);
561 Map<String, DataTypeDefinition> dataTypes = dataTypesEither.left().value();
563 List<InputDefinition> inputDef = component.getInputs();
564 Map<String, ToscaProperty> mergedProperties = new HashMap<>();
565 addInterfaceDefinitionElement(component, toscaNodeType, dataTypes, isAssociatedResourceComponent);
566 if (inputDef != null) {
567 addInputsToProperties(dataTypes, inputDef, mergedProperties);
570 if(CollectionUtils.isNotEmpty(component.getProperties())) {
571 List<PropertyDefinition> properties = component.getProperties();
572 mergedProperties = properties.stream().collect(Collectors.toMap(
573 PropertyDataDefinition::getName,
574 property -> propertyConvertor.convertProperty(dataTypes, property, PropertyConvertor.PropertyType.PROPERTY)));
576 if (!mergedProperties.isEmpty()) {
577 toscaNodeType.setProperties(mergedProperties);
579 // Extracted to method for code reuse
580 return convertReqCapAndTypeName(componentsCache, component, toscaNode, nodeTypes, toscaNodeType, dataTypes);
583 private void addInputsToProperties(Map<String, DataTypeDefinition> dataTypes,
584 List<InputDefinition> inputDef,
585 Map<String, ToscaProperty> mergedProperties) {
586 for(InputDefinition input : inputDef) {
587 ToscaProperty property = propertyConvertor.convertProperty(dataTypes, input, PropertyConvertor.PropertyType.INPUT);
588 mergedProperties.put(input.getName(), property);
592 private Either<ToscaTemplate, ToscaError> convertReqCapAndTypeName(Map<String, Component> componentsCache, Component component, ToscaTemplate toscaNode,
593 Map<String, ToscaNodeType> nodeTypes, ToscaNodeType toscaNodeType,
594 Map<String, DataTypeDefinition> dataTypes) {
595 Either<ToscaNodeType, ToscaError> capabilities = convertCapabilities(componentsCache, component, toscaNodeType, dataTypes);
596 if (capabilities.isRight()) {
597 return Either.right(capabilities.right().value());
599 toscaNodeType = capabilities.left().value();
600 log.debug("Capabilities converted for {}", component.getUniqueId());
602 Either<ToscaNodeType, ToscaError> requirements = capabilityRequirementConverter.convertRequirements(componentsCache, component,
604 if (requirements.isRight()) {
605 return Either.right(requirements.right().value());
607 toscaNodeType = requirements.left().value();
608 log.debug("Requirements converted for {}", component.getUniqueId());
610 String toscaResourceName;
611 switch (component.getComponentType()) {
613 toscaResourceName = ((ResourceMetadataDataDefinition) component.getComponentMetadataDefinition()
614 .getMetadataDataDefinition()).getToscaResourceName();
617 toscaResourceName = SERVICE_NODE_TYPE_PREFIX
618 + component.getComponentMetadataDefinition().getMetadataDataDefinition().getSystemName();
621 log.debug(NOT_SUPPORTED_COMPONENT_TYPE, component.getComponentType());
622 return Either.right(ToscaError.NOT_SUPPORTED_TOSCA_TYPE);
625 nodeTypes.put(toscaResourceName, toscaNodeType);
626 toscaNode.setNode_types(nodeTypes);
627 log.debug("finish convert node type for {}", component.getUniqueId());
628 return Either.left(toscaNode);
631 protected Either<Map<String, ToscaNodeTemplate>, ToscaError> convertNodeTemplates(
633 List<ComponentInstance> componentInstances,
634 Map<String, List<ComponentInstanceProperty>> componentInstancesProperties,
635 Map<String, List<ComponentInstanceInterface>> componentInstanceInterfaces,
636 Map<String, Component> componentCache, Map<String, DataTypeDefinition> dataTypes,
637 ToscaTopolgyTemplate topologyTemplate) {
639 Either<Map<String, ToscaNodeTemplate>, ToscaError> convertNodeTemplatesRes = null;
640 log.debug("start convert topology template for {} for type {}", component.getUniqueId(),
641 component.getComponentType());
642 Map<String, ToscaNodeTemplate> nodeTemplates = new HashMap<>();
643 Map<String, List<ComponentInstanceInput>> componentInstancesInputs = component.getComponentInstancesInputs();
645 Map<String, ToscaGroupTemplate> groupsMap = null;
646 for (ComponentInstance componentInstance : componentInstances) {
647 ToscaNodeTemplate nodeTemplate = new ToscaNodeTemplate();
648 nodeTemplate.setType(componentInstance.getToscaComponentName());
649 nodeTemplate.setDirectives(componentInstance.getDirectives());
650 nodeTemplate.setNode_filter(convertToNodeTemplateNodeFilterComponent(componentInstance.getNodeFilter()));
652 Either<Component, Boolean> originComponentRes = capabilityRequirementConverter
653 .getOriginComponent(componentCache, componentInstance);
654 if (originComponentRes.isRight()) {
655 convertNodeTemplatesRes = Either.right(ToscaError.NODE_TYPE_REQUIREMENT_ERROR);
658 Either<ToscaNodeTemplate, ToscaError> requirements = convertComponentInstanceRequirements(component,
659 componentInstance, component.getComponentInstancesRelations(), nodeTemplate,
660 originComponentRes.left().value(), componentCache);
661 if (requirements.isRight()) {
662 convertNodeTemplatesRes = Either.right(requirements.right().value());
665 String instanceUniqueId = componentInstance.getUniqueId();
666 log.debug("Component instance Requirements converted for instance {}", instanceUniqueId);
668 nodeTemplate = requirements.left().value();
670 Component originalComponent = componentCache.get(componentInstance.getActualComponentUid());
672 if (componentInstance.getOriginType() == OriginTypeEnum.ServiceProxy){
673 Component componentOfProxy = componentCache.get(componentInstance.getComponentUid());
674 nodeTemplate.setMetadata(convertMetadata(componentOfProxy, true, componentInstance));
676 nodeTemplate.setMetadata(convertMetadata(originalComponent, true, componentInstance));
679 Either<ToscaNodeTemplate, ToscaError> capabilities = capabilityRequirementConverter
680 .convertComponentInstanceCapabilities(componentInstance, dataTypes, nodeTemplate);
681 if (capabilities.isRight()) {
682 convertNodeTemplatesRes = Either.right(capabilities.right().value());
685 log.debug("Component instance Capabilities converted for instance {}", instanceUniqueId);
687 nodeTemplate = capabilities.left().value();
688 Map<String, Object> props = new HashMap<>();
690 if (originalComponent.getComponentType() == ComponentTypeEnum.RESOURCE) {
691 // Adds the properties of parent component to map
692 addPropertiesOfParentComponent(dataTypes, originalComponent, props);
695 if (null != componentInstancesProperties && componentInstancesProperties.containsKey(instanceUniqueId)) {
696 addPropertiesOfComponentInstance(componentInstancesProperties, dataTypes,
697 instanceUniqueId, props);
700 if (componentInstancesInputs != null && componentInstancesInputs.containsKey(instanceUniqueId)) {
701 addComponentInstanceInputs(dataTypes, componentInstancesInputs, instanceUniqueId,
704 //M3[00001] - NODE TEMPLATE INTERFACES - START
705 handleInstanceInterfaces(componentInstanceInterfaces, componentInstance, dataTypes, nodeTemplate,
706 instanceUniqueId, component);
707 //M3[00001] - NODE TEMPLATE INTERFACES - END
708 if (props != null && !props.isEmpty()) {
709 nodeTemplate.setProperties(props);
712 List<GroupInstance> groupInstances = componentInstance.getGroupInstances();
713 if (groupInstances != null) {
714 if (groupsMap == null) {
715 groupsMap = new HashMap<>();
717 for (GroupInstance groupInst : groupInstances) {
718 boolean addToTosca = true;
720 List<String> artifacts = groupInst.getArtifacts();
721 if (artifacts == null || artifacts.isEmpty()) {
726 ToscaGroupTemplate toscaGroup = groupExportParser.getToscaGroupTemplate(groupInst, componentInstance.getInvariantName());
727 groupsMap.put(groupInst.getName(), toscaGroup);
732 nodeTemplates.put(componentInstance.getName(), nodeTemplate);
734 if (groupsMap != null) {
735 log.debug("instance groups added");
736 topologyTemplate.addGroups(groupsMap);
738 if (component.getComponentType() == ComponentTypeEnum.SERVICE && isNotEmpty(((Service) component).getForwardingPaths())) {
739 log.debug("Starting converting paths for component {}, name {}", component.getUniqueId(),
740 component.getName());
741 ForwardingPathToscaUtil.addForwardingPaths((Service) component, nodeTemplates, capabilityRequirementConverter, componentCache, toscaOperationFacade);
742 log.debug("Finished converting paths for component {}, name {}", component.getUniqueId(),
743 component.getName());
745 if (convertNodeTemplatesRes == null) {
746 convertNodeTemplatesRes = Either.left(nodeTemplates);
748 log.debug("finish convert topology template for {} for type {}", component.getUniqueId(),
749 component.getComponentType());
750 return convertNodeTemplatesRes;
753 private void handleInstanceInterfaces(
754 Map<String, List<ComponentInstanceInterface>> componentInstanceInterfaces,
755 ComponentInstance componentInstance, Map<String, DataTypeDefinition> dataTypes, ToscaNodeTemplate nodeTemplate,
756 String instanceUniqueId,
757 Component parentComponent) {
759 Map<String, Object> interfaces;
761 // we need to handle service proxy interfaces
762 if(isComponentOfTypeServiceProxy(componentInstance)) {
763 if(MapUtils.isEmpty(componentInstanceInterfaces)
764 || !componentInstanceInterfaces.containsKey(instanceUniqueId)) {
767 List<ComponentInstanceInterface> currServiceInterfaces =
768 componentInstanceInterfaces.get(instanceUniqueId);
770 Map<String, InterfaceDefinition> tmpInterfaces = new HashMap<>();
771 currServiceInterfaces.forEach(instInterface -> tmpInterfaces.put(instInterface
772 .getUniqueId(), instInterface));
774 interfaces = InterfacesOperationsToscaUtil
775 .getInterfacesMap(parentComponent, tmpInterfaces, dataTypes, true, true);
779 getComponentInstanceInterfaceInstances(componentInstanceInterfaces,
780 componentInstance, instanceUniqueId);
782 nodeTemplate.setInterfaces(interfaces);
785 private boolean isComponentOfTypeServiceProxy(ComponentInstance componentInstance) {
786 return Objects.nonNull(componentInstance.getOriginType())
787 && componentInstance.getOriginType().getValue().equals("Service Proxy");
790 //M3[00001] - NODE TEMPLATE INTERFACES - START
791 private Map<String, Object> getComponentInstanceInterfaceInstances(Map<String, List<ComponentInstanceInterface>> componentInstancesInterfaces,
792 ComponentInstance componentInstance,
793 String instanceUniqueId) {
794 if(MapUtils.isEmpty(componentInstancesInterfaces)) {
798 List<ComponentInstanceInterface> componentInstanceInterfaces =
799 componentInstancesInterfaces.get(instanceUniqueId);
801 if(CollectionUtils.isEmpty(componentInstanceInterfaces)) {
805 Map<String, Object> interfaces = new HashMap<>();
806 for(ComponentInstanceInterface componentInstanceInterface : componentInstanceInterfaces) {
807 interfaces.put(componentInstanceInterface.getInterfaceId(),
808 removeOperationsKeyFromInterface(componentInstanceInterface.getInterfaceInstanceDataDefinition()));
811 componentInstance.setInterfaces(interfaces);
816 private void addComponentInstanceInputs(Map<String, DataTypeDefinition> dataTypes,
817 Map<String, List<ComponentInstanceInput>> componentInstancesInputs,
818 String instanceUniqueId, Map<String, Object> props) {
820 List<ComponentInstanceInput> instanceInputsList = componentInstancesInputs.get(instanceUniqueId);
821 if (instanceInputsList != null) {
822 instanceInputsList.forEach(input -> {
824 Supplier<String> supplier = () -> input.getValue() != null && !input.getValue().isEmpty()
825 ? input.getValue() : input.getDefaultValue();
826 propertyConvertor.convertAndAddValue(dataTypes, props, input, supplier);
831 private void addPropertiesOfComponentInstance(
832 Map<String, List<ComponentInstanceProperty>> componentInstancesProperties,
833 Map<String, DataTypeDefinition> dataTypes, String instanceUniqueId,
834 Map<String, Object> props) {
836 if (isNotEmpty(componentInstancesProperties)) {
837 componentInstancesProperties.get(instanceUniqueId)
838 // Converts and adds each value to property map
839 .forEach(prop -> propertyConvertor.convertAndAddValue(dataTypes, props, prop,
844 private void addPropertiesOfParentComponent(Map<String, DataTypeDefinition> dataTypes,
845 Component componentOfInstance, Map<String, Object> props) {
847 List<PropertyDefinition> componentProperties = ((Resource) componentOfInstance).getProperties();
848 if (isNotEmpty(componentProperties)) {
849 componentProperties.stream()
850 // Filters out properties with empty default values
851 .filter(prop -> isNotEmpty(prop.getDefaultValue()))
852 // Converts and adds each value to property map
853 .forEach(prop -> propertyConvertor.convertAndAddValue(dataTypes, props, prop,
854 prop::getDefaultValue));
860 * @param componentInstance
865 private void convertAndAddValue(Map<String, DataTypeDefinition> dataTypes, ComponentInstance componentInstance,
866 Map<String, Object> props, PropertyDefinition prop, Supplier<String> supplier) {
867 Object convertedValue = convertValue(dataTypes, componentInstance, prop, supplier);
868 if (!ToscaValueBaseConverter.isEmptyObjectValue(convertedValue)) {
869 props.put(prop.getName(), convertedValue);
873 private <T extends PropertyDefinition> Object convertValue(Map<String, DataTypeDefinition> dataTypes,
874 ComponentInstance componentInstance, T input, Supplier<String> supplier) {
875 log.debug("Convert property or input value {} for instance {}", input.getName(),
876 componentInstance.getUniqueId());
877 String propertyType = input.getType();
878 String innerType = null;
879 if (input.getSchema() != null && input.getSchema().getProperty() != null) {
880 innerType = input.getSchema().getProperty().getType();
882 return propertyConvertor.convertToToscaObject(propertyType, supplier.get(), innerType,
886 private ToscaNodeType createNodeType(Component component) {
887 ToscaNodeType toscaNodeType = new ToscaNodeType();
888 if (ModelConverter.isAtomicComponent(component)) {
889 if (((Resource) component).getDerivedFrom() != null) {
890 toscaNodeType.setDerived_from(((Resource) component).getDerivedFrom().get(0));
892 toscaNodeType.setDescription(component.getDescription());
894 String derivedFrom = null != component.getDerivedFromGenericType() ? component.getDerivedFromGenericType()
895 : "tosca.nodes.Root";
896 toscaNodeType.setDerived_from(derivedFrom);
898 return toscaNodeType;
901 private Either<Map<String, ToscaNodeType>, ToscaError> createProxyNodeTypes(Map<String, Component> componentCache ,Component container ) {
903 Map<String, ToscaNodeType> nodeTypesMap = new HashMap<>();
904 Either<Map<String, ToscaNodeType>, ToscaError> res = Either.left(nodeTypesMap);
906 List<ComponentInstance> componentInstances = container.getComponentInstances();
908 if (componentInstances == null || componentInstances.isEmpty()) {
911 Map<String, ComponentInstance> serviceProxyInstanceList = new HashMap<>();
912 List<ComponentInstance> proxyInst = componentInstances.stream()
913 .filter(p -> p.getOriginType().name().equals(OriginTypeEnum.ServiceProxy.name()))
914 .collect(Collectors.toList());
915 if (proxyInst != null && !proxyInst.isEmpty()) {
916 for (ComponentInstance inst : proxyInst) {
917 serviceProxyInstanceList.put(inst.getToscaComponentName(), inst);
921 if (serviceProxyInstanceList.isEmpty()) {
924 ComponentParametersView filter = new ComponentParametersView(true);
925 filter.setIgnoreCapabilities(false);
926 filter.setIgnoreComponentInstances(false);
927 Either<Resource, StorageOperationStatus> serviceProxyOrigin = toscaOperationFacade
928 .getLatestByName("serviceProxy");
929 if (serviceProxyOrigin.isRight()) {
930 log.debug("Failed to fetch normative service proxy resource by tosca name, error {}",
931 serviceProxyOrigin.right().value());
932 return Either.right(ToscaError.NOT_SUPPORTED_TOSCA_TYPE);
934 Component origComponent = serviceProxyOrigin.left().value();
936 for (Entry<String, ComponentInstance> entryProxy : serviceProxyInstanceList.entrySet()) {
937 Component serviceComponent = null;
938 ComponentParametersView componentParametersView = new ComponentParametersView();
939 componentParametersView.disableAll();
940 componentParametersView.setIgnoreCategories(false);
941 Either<Component, StorageOperationStatus> service = toscaOperationFacade
942 .getToscaElement(entryProxy.getValue().getSourceModelUid(), componentParametersView);
943 if (service.isRight()) {
944 log.debug("Failed to fetch resource with id {} for instance {}", entryProxy.getValue().getSourceModelUid(), entryProxy.getValue().getName());
946 serviceComponent = service.left().value();
949 ToscaNodeType toscaNodeType = createProxyNodeType(componentCache , origComponent, serviceComponent, entryProxy.getValue());
950 nodeTypesMap.put(entryProxy.getKey(), toscaNodeType);
953 return Either.left(nodeTypesMap);
956 private ToscaNodeType createProxyNodeType(Map<String, Component> componentCache , Component origComponent, Component proxyComponent,
957 ComponentInstance instance) {
958 ToscaNodeType toscaNodeType = new ToscaNodeType();
959 String derivedFrom = ((Resource) origComponent).getToscaResourceName();
961 toscaNodeType.setDerived_from(derivedFrom);
962 Either<Map<String, DataTypeDefinition>, TitanOperationStatus> dataTypesEither = dataTypeCache.getAll();
963 if (dataTypesEither.isRight()) {
964 log.debug("Failed to retrieve all data types {}", dataTypesEither.right().value());
966 Map<String, DataTypeDefinition> dataTypes = dataTypesEither.left().value();
967 Map<String, ToscaCapability> capabilities = this.capabilityRequirementConverter
968 .convertProxyCapabilities( componentCache ,origComponent, proxyComponent, instance, dataTypes);
970 toscaNodeType.setCapabilities(capabilities);
972 return toscaNodeType;
975 private Either<ToscaNodeTemplate, ToscaError> convertComponentInstanceRequirements(Component component,
976 ComponentInstance componentInstance, List<RequirementCapabilityRelDef> relations,
977 ToscaNodeTemplate nodeTypeTemplate, Component originComponent, Map<String, Component> componentCache) {
979 List<Map<String, ToscaTemplateRequirement>> toscaRequirements = new ArrayList<>();
980 if (!addRequirements(component, componentInstance, relations, originComponent, toscaRequirements, componentCache)) {
981 log.debug("Failed to convert component instance requirements for the component instance {}. ",
982 componentInstance.getName());
983 return Either.right(ToscaError.NODE_TYPE_REQUIREMENT_ERROR);
985 if (!toscaRequirements.isEmpty()) {
986 nodeTypeTemplate.setRequirements(toscaRequirements);
988 log.debug("Finished to convert requirements for the node type {} ", componentInstance.getName());
989 return Either.left(nodeTypeTemplate);
992 private boolean addRequirements(Component component, ComponentInstance componentInstance,
993 List<RequirementCapabilityRelDef> relations, Component originComponent,
994 List<Map<String, ToscaTemplateRequirement>> toscaRequirements, Map<String, Component> componentCache) {
995 List<RequirementCapabilityRelDef> filteredRelations = relations.stream()
996 .filter(p -> componentInstance.getUniqueId().equals(p.getFromNode())).collect(Collectors.toList());
997 return isEmpty(filteredRelations) ||
998 filteredRelations.stream()
999 .allMatch(rel -> addRequirement(componentInstance, originComponent, component.getComponentInstances(), rel, toscaRequirements, componentCache));
1002 private boolean addRequirement(ComponentInstance fromInstance, Component fromOriginComponent,
1003 List<ComponentInstance> instancesList, RequirementCapabilityRelDef rel,
1004 List<Map<String, ToscaTemplateRequirement>> toscaRequirements, Map<String, Component> componentCache) {
1006 boolean result = true;
1007 Map<String, List<RequirementDefinition>> reqMap = fromOriginComponent.getRequirements();
1008 RelationshipInfo reqAndRelationshipPair = rel.getRelationships().get(0).getRelation();
1009 Either<Component, StorageOperationStatus> getOriginRes = null;
1010 Optional<RequirementDefinition> reqOpt = Optional.empty();
1011 Component toOriginComponent = null;
1012 Optional<CapabilityDefinition> capOpt = Optional.empty();
1014 ComponentInstance toInstance = instancesList.stream().filter(i -> rel.getToNode().equals(i.getUniqueId()))
1015 .findFirst().orElse(null);
1016 if (toInstance == null) {
1017 log.debug("Failed to find a relation from the node {} to the node {}", fromInstance.getName(),
1022 reqOpt = findRequirement(fromOriginComponent, reqMap, reqAndRelationshipPair, fromInstance.getUniqueId());
1023 if (!reqOpt.isPresent()) {
1024 log.debug("Failed to find a requirement with uniqueId {} on a component with uniqueId {}",
1025 reqAndRelationshipPair.getRequirementUid(), fromOriginComponent.getUniqueId());
1030 ComponentParametersView filter = new ComponentParametersView(true);
1031 filter.setIgnoreComponentInstances(false);
1032 filter.setIgnoreCapabilities(false);
1033 filter.setIgnoreGroups(false);
1034 getOriginRes = toscaOperationFacade.getToscaElement(toInstance.getActualComponentUid(), filter);
1035 if (getOriginRes.isRight()) {
1036 log.debug("Failed to build substituted name for the requirement {}. Failed to get an origin component with uniqueId {}",
1037 reqOpt.get().getName(), toInstance.getActualComponentUid());
1042 toOriginComponent = getOriginRes.left().value();
1043 capOpt = toOriginComponent.getCapabilities().get(reqOpt.get().getCapability()).stream()
1044 .filter(c -> isCapabilityBelongToRelation(reqAndRelationshipPair, c)).findFirst();
1045 if (!capOpt.isPresent()) {
1046 capOpt = findCapability(reqAndRelationshipPair, toOriginComponent, fromOriginComponent, reqOpt.get());
1047 if(!capOpt.isPresent()){
1049 log.debug("Failed to find a capability with name {} on a component with uniqueId {}",
1050 reqAndRelationshipPair.getCapability(), fromOriginComponent.getUniqueId());
1055 result = buildAndAddRequirement(toscaRequirements, fromOriginComponent, toOriginComponent, capOpt.get(),
1056 reqOpt.get(), reqAndRelationshipPair, toInstance, componentCache);
1061 private boolean isCapabilityBelongToRelation(RelationshipInfo reqAndRelationshipPair, CapabilityDefinition capability) {
1062 return capability.getName().equals(reqAndRelationshipPair.getCapability()) && (capability.getOwnerId() !=null && capability.getOwnerId().equals(reqAndRelationshipPair.getCapabilityOwnerId()));
1065 private Optional<CapabilityDefinition> findCapability(RelationshipInfo reqAndRelationshipPair, Component toOriginComponent, Component fromOriginComponent, RequirementDefinition requirement) {
1066 Optional<CapabilityDefinition> cap = toOriginComponent.getCapabilities().get(requirement.getCapability()).stream().filter(c -> c.getType().equals(requirement.getCapability())).findFirst();
1067 if (!cap.isPresent()) {
1068 log.debug("Failed to find a capability with name {} on a component with uniqueId {}", reqAndRelationshipPair.getCapability(), fromOriginComponent.getUniqueId());
1073 private boolean buildAndAddRequirement(List<Map<String, ToscaTemplateRequirement>> toscaRequirements, Component fromOriginComponent, Component toOriginComponent, CapabilityDefinition capability, RequirementDefinition requirement, RelationshipInfo reqAndRelationshipPair, ComponentInstance toInstance, Map<String, Component> componentCache) {
1074 List<String> reducedPath = capability.getPath();
1075 if(capability.getOwnerId() !=null){
1076 reducedPath = capabilityRequirementConverter.getReducedPathByOwner(capability.getPath() , capability.getOwnerId() );
1078 Either<String, Boolean> buildCapNameRes = capabilityRequirementConverter.buildSubstitutedName(componentCache,
1079 toOriginComponent, reducedPath, reqAndRelationshipPair.getCapability(), capability.getPreviousName());
1080 if (buildCapNameRes.isRight()) {
1082 "Failed to build a substituted capability name for the capability with name {} on a component with uniqueId {}",
1083 reqAndRelationshipPair.getCapability(), fromOriginComponent.getUniqueId());
1086 Either<String, Boolean> buildReqNameRes = capabilityRequirementConverter.buildSubstitutedName(componentCache, fromOriginComponent,
1087 requirement.getPath(), reqAndRelationshipPair.getRequirement(), requirement.getPreviousName());
1088 if (buildReqNameRes.isRight()) {
1090 "Failed to build a substituted requirement name for the requirement with name {} on a component with uniqueId {}",
1091 reqAndRelationshipPair.getRequirement(), fromOriginComponent.getUniqueId());
1094 ToscaTemplateRequirement toscaRequirement = new ToscaTemplateRequirement();
1095 Map<String, ToscaTemplateRequirement> toscaReqMap = new HashMap<>();
1096 toscaRequirement.setNode(toInstance.getName());
1097 toscaRequirement.setCapability(buildCapNameRes.left().value());
1098 toscaReqMap.put(buildReqNameRes.left().value(), toscaRequirement);
1099 toscaRequirements.add(toscaReqMap);
1103 private Optional<RequirementDefinition> findRequirement(Component fromOriginComponent, Map<String, List<RequirementDefinition>> reqMap, RelationshipInfo reqAndRelationshipPair, String fromInstanceId) {
1104 for(List<RequirementDefinition> reqList: reqMap.values()){
1105 Optional<RequirementDefinition> reqOpt = reqList.stream().filter(r -> isRequirementBelongToRelation(fromOriginComponent, reqAndRelationshipPair, r, fromInstanceId)).findFirst();
1106 if(reqOpt.isPresent()){
1110 return Optional.empty();
1114 * Allows detecting the requirement belonging to the received relationship
1115 * The detection logic is: A requirement belongs to a relationship IF 1.The
1116 * name of the requirement equals to the "requirement" field of the
1117 * relation; AND 2. In case of a non-atomic resource, OwnerId of the
1118 * requirement equals to requirementOwnerId of the relation OR uniqueId of
1119 * toInstance equals to capabilityOwnerId of the relation
1121 private boolean isRequirementBelongToRelation(Component originComponent, RelationshipInfo reqAndRelationshipPair, RequirementDefinition requirement, String fromInstanceId) {
1122 if (!StringUtils.equals(requirement.getName(), reqAndRelationshipPair.getRequirement())) {
1123 log.debug("Failed to find a requirement with name {} and reqAndRelationshipPair {}",
1124 requirement.getName(), reqAndRelationshipPair.getRequirement());
1127 return ModelConverter.isAtomicComponent(originComponent) ||
1128 isRequirementBelongToOwner(reqAndRelationshipPair, requirement, fromInstanceId, originComponent);
1131 private boolean isRequirementBelongToOwner(RelationshipInfo reqAndRelationshipPair, RequirementDefinition requirement, String fromInstanceId, Component originComponent) {
1132 return StringUtils.equals(requirement.getOwnerId(), reqAndRelationshipPair.getRequirementOwnerId()) || (isCvfc(originComponent) && StringUtils.equals(fromInstanceId, reqAndRelationshipPair.getRequirementOwnerId()));
1135 private boolean isCvfc(Component component) {
1136 return component.getComponentType() == ComponentTypeEnum.RESOURCE &&
1137 ((Resource) component).getResourceType() == ResourceTypeEnum.CVFC;
1140 private Either<SubstitutionMapping, ToscaError> convertCapabilities(Component component,
1141 SubstitutionMapping substitutionMappings, Map<String, Component> componentCache) {
1143 Either<SubstitutionMapping, ToscaError> result = Either.left(substitutionMappings);
1144 Either<Map<String, String[]>, ToscaError> toscaCapabilitiesRes = capabilityRequirementConverter
1145 .convertSubstitutionMappingCapabilities(componentCache, component);
1146 if (toscaCapabilitiesRes.isRight()) {
1147 result = Either.right(toscaCapabilitiesRes.right().value());
1148 log.debug("Failed convert capabilities for the component {}. ", component.getName());
1149 } else if (isNotEmpty(toscaCapabilitiesRes.left().value())) {
1150 substitutionMappings.setCapabilities(toscaCapabilitiesRes.left().value());
1151 log.debug("Finish convert capabilities for the component {}. ", component.getName());
1153 log.debug("Finished to convert capabilities for the component {}. ", component.getName());
1157 private Either<ToscaNodeType, ToscaError> convertCapabilities(Map<String, Component> componentsCache, Component component, ToscaNodeType nodeType,
1158 Map<String, DataTypeDefinition> dataTypes) {
1159 Map<String, ToscaCapability> toscaCapabilities = capabilityRequirementConverter.convertCapabilities(componentsCache, component,
1161 if (!toscaCapabilities.isEmpty()) {
1162 nodeType.setCapabilities(toscaCapabilities);
1164 log.debug("Finish convert Capabilities for node type");
1166 return Either.left(nodeType);
1170 protected NodeFilter convertToNodeTemplateNodeFilterComponent(CINodeFilterDataDefinition inNodeFilter) {
1171 if (inNodeFilter == null){
1174 NodeFilter nodeFilter = new NodeFilter();
1176 ListDataDefinition<RequirementNodeFilterCapabilityDataDefinition> origCapabilities =
1177 inNodeFilter.getCapabilities();
1179 ListDataDefinition<RequirementNodeFilterPropertyDataDefinition> origProperties = inNodeFilter.getProperties();
1181 List<Map<String, CapabilityFilter>> capabilitiesCopy = new ArrayList<>();
1182 List<Map<String, List<Object>>> propertiesCopy = new ArrayList<>();
1184 copyNodeFilterCapabilitiesTemplate(origCapabilities, capabilitiesCopy);
1185 copyNodeFilterProperties(origProperties, propertiesCopy);
1187 if(CollectionUtils.isNotEmpty(capabilitiesCopy)) {
1188 nodeFilter.setCapabilities(capabilitiesCopy);
1191 if(CollectionUtils.isNotEmpty(propertiesCopy)) {
1192 nodeFilter.setProperties(propertiesCopy);
1195 nodeFilter.setTosca_id(cloneToscaId(inNodeFilter.getTosca_id()));
1198 nodeFilter = (NodeFilter) cloneObjectFromYml(nodeFilter, NodeFilter.class);
1203 private Object cloneToscaId(Object toscaId) {
1204 return Objects.isNull(toscaId) ? null
1205 : cloneObjectFromYml(toscaId, toscaId.getClass());
1209 private Object cloneObjectFromYml(Object objToClone, Class classOfObj) {
1210 String objectAsYml = yamlUtil.objectToYaml(objToClone);
1211 return yamlUtil.yamlToObject(objectAsYml, classOfObj);
1213 private void copyNodeFilterCapabilitiesTemplate(
1214 ListDataDefinition<RequirementNodeFilterCapabilityDataDefinition> origCapabilities,
1215 List<Map<String, CapabilityFilter>> capabilitiesCopy) {
1216 if(origCapabilities == null || origCapabilities.getListToscaDataDefinition() == null ||
1217 origCapabilities.getListToscaDataDefinition().isEmpty() ) {
1220 for(RequirementNodeFilterCapabilityDataDefinition capability : origCapabilities.getListToscaDataDefinition()) {
1221 Map<String, CapabilityFilter> capabilityFilterCopyMap = new HashMap<>();
1222 CapabilityFilter capabilityFilter = new CapabilityFilter();
1223 List<Map<String, List<Object>>> propertiesCopy = new ArrayList<>();
1224 copyNodeFilterProperties(capability.getProperties(), propertiesCopy);
1225 capabilityFilter.setProperties(propertiesCopy);
1226 capabilityFilterCopyMap.put(capability.getName(), capabilityFilter);
1227 capabilitiesCopy.add(capabilityFilterCopyMap);
1231 private List<Object> copyNodeFilterProperty(List<Object> propertyList) {
1232 String listAsString = yamlUtil.objectToYaml(propertyList);
1233 return yamlUtil.yamlToObject(listAsString, List.class);
1237 private void copyNodeFilterProperties(
1238 ListDataDefinition<RequirementNodeFilterPropertyDataDefinition> origProperties,
1239 List<Map<String, List<Object>>> propertiesCopy) {
1240 if(origProperties == null || origProperties.getListToscaDataDefinition() == null ||
1241 origProperties.isEmpty()) {
1244 for(RequirementNodeFilterPropertyDataDefinition propertyDataDefinition : origProperties.getListToscaDataDefinition()) {
1245 Map<String, List<Object>> propertyMapCopy = new HashMap<>();
1246 for(String propertyInfoEntry : propertyDataDefinition.getConstraints()) {
1247 Map propertyValObj = new YamlUtil().yamlToObject(propertyInfoEntry, Map.class);
1248 if (propertyMapCopy.containsKey(propertyDataDefinition.getName())){
1249 propertyMapCopy.get(propertyDataDefinition.getName()).add(propertyValObj);
1251 if (propertyDataDefinition.getName() != null) {
1252 List propsList =new ArrayList();
1253 propsList.add(propertyValObj);
1254 propertyMapCopy.put(propertyDataDefinition.getName(), propsList);
1256 propertyMapCopy.putAll(propertyValObj);
1260 propertiesCopy.add(propertyMapCopy);
1265 private static class CustomRepresenter extends Representer {
1266 public CustomRepresenter() {
1268 // null representer is exceptional and it is stored as an instance
1270 this.nullRepresenter = new RepresentNull();
1275 protected NodeTuple representJavaBeanProperty(Object javaBean, Property property, Object propertyValue,
1277 if (propertyValue == null) {
1280 // skip not relevant for Tosca property
1281 if ("dependencies".equals(property.getName())) {
1284 NodeTuple defaultNode = super.representJavaBeanProperty(javaBean, property, propertyValue, customTag);
1286 return "_defaultp_".equals(property.getName())
1287 ? new NodeTuple(representData("default"), defaultNode.getValueNode()) : defaultNode;
1292 protected MappingNode representJavaBean(Set<Property> properties, Object javaBean) {
1293 // remove the bean type from the output yaml (!! ...)
1294 if (!classTags.containsKey(javaBean.getClass())) {
1295 addClassTag(javaBean.getClass(), Tag.MAP);
1298 return super.representJavaBean(properties, javaBean);
1301 private class RepresentNull implements Represent {
1303 public Node representData(Object data) {
1304 // possible values are here http://yaml.org/type/null.html
1305 return representScalar(Tag.NULL, "");
1310 private static class UnsortedPropertyUtils extends PropertyUtils {
1312 protected Set<Property> createPropertySet(Class type, BeanAccess bAccess)
1313 throws IntrospectionException {
1314 Collection<Property> fields = getPropertiesMap(type, BeanAccess.FIELD).values();
1315 return new LinkedHashSet<>(fields);
1319 private Object removeOperationsKeyFromInterface(Object interfaceInstanceDataDefinition) {
1320 ObjectMapper objectMapper = new ObjectMapper();
1321 objectMapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false);
1323 Map<String, Object> interfaceAsMap = ServiceUtils.getObjectAsMap(interfaceInstanceDataDefinition);
1324 Map<String, Object> operations = (Map<String, Object>) interfaceAsMap.remove("operations");
1325 interfaceAsMap.remove("empty");
1327 if(MapUtils.isNotEmpty(operations)) {
1328 interfaceAsMap.putAll(operations);
1331 Object interfaceObject = objectMapper.convertValue(interfaceAsMap, Object.class);
1333 return interfaceObject;