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.janusgraph.JanusGraphOperationStatus;
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.jsonjanusgraph.operations.ToscaOperationFacade;
45 import org.openecomp.sdc.be.model.jsonjanusgraph.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.openecomp.sdc.be.components.utils.PropertiesUtils.resolvePropertyValueFromInput;
79 import static org.openecomp.sdc.be.tosca.utils.InterfacesOperationsToscaUtil.addInterfaceDefinitionElement;
80 import static org.openecomp.sdc.be.tosca.utils.InterfacesOperationsToscaUtil.addInterfaceTypeElement;
81 import static org.openecomp.sdc.be.tosca.utils.ToscaExportUtils.addInputsToProperties;
82 import static org.openecomp.sdc.be.tosca.utils.ToscaExportUtils.getProxyNodeTypeInterfaces;
83 import static org.openecomp.sdc.be.tosca.utils.ToscaExportUtils.getProxyNodeTypeProperties;
85 @org.springframework.stereotype.Component("tosca-export-handler")
86 public class ToscaExportHandler {
88 private ApplicationDataTypeCache dataTypeCache;
89 private ToscaOperationFacade toscaOperationFacade;
90 private CapabilityRequirementConverter capabilityRequirementConverter;
91 private PolicyExportParser policyExportParser;
92 private GroupExportParser groupExportParser;
93 private PropertyConvertor propertyConvertor;
94 private InputConverter inputConverter;
95 private InterfaceLifecycleOperation interfaceLifecycleOperation;
98 public ToscaExportHandler(ApplicationDataTypeCache dataTypeCache, ToscaOperationFacade toscaOperationFacade,
99 CapabilityRequirementConverter capabilityRequirementConverter, PolicyExportParser policyExportParser,
100 GroupExportParser groupExportParser, InputConverter inputConverter, InterfaceLifecycleOperation interfaceLifecycleOperation) {
101 this.dataTypeCache = dataTypeCache;
102 this.toscaOperationFacade = toscaOperationFacade;
103 this.capabilityRequirementConverter = capabilityRequirementConverter;
104 this.policyExportParser = policyExportParser;
105 this.groupExportParser = groupExportParser;
106 this.propertyConvertor = PropertyConvertor.getInstance();
107 this.inputConverter = inputConverter;
108 this.interfaceLifecycleOperation = interfaceLifecycleOperation;
112 private static final Logger log = Logger.getLogger(ToscaExportHandler.class);
114 private static final String TOSCA_VERSION = "tosca_simple_yaml_1_1";
115 private static final String SERVICE_NODE_TYPE_PREFIX = "org.openecomp.service.";
116 private static final String IMPORTS_FILE_KEY = "file";
117 private static final String TOSCA_INTERFACE_NAME = "-interface.yml";
118 public static final String ASSET_TOSCA_TEMPLATE = "assettoscatemplate";
119 private static final String FAILED_TO_GET_DEFAULT_IMPORTS_CONFIGURATION = "convertToToscaTemplate - failed to get Default Imports section from configuration";
120 private static final String NOT_SUPPORTED_COMPONENT_TYPE = "Not supported component type {}";
121 private static final List<Map<String, Map<String, String>>> DEFAULT_IMPORTS = ConfigurationManager
122 .getConfigurationManager().getConfiguration().getDefaultImports();
123 private static YamlUtil yamlUtil = new YamlUtil();
125 public ToscaExportHandler(){}
127 public Either<ToscaRepresentation, ToscaError> exportComponent(Component component) {
129 Either<ToscaTemplate, ToscaError> toscaTemplateRes = convertToToscaTemplate(component);
130 if (toscaTemplateRes.isRight()) {
131 return Either.right(toscaTemplateRes.right().value());
134 ToscaTemplate toscaTemplate = toscaTemplateRes.left().value();
135 ToscaRepresentation toscaRepresentation = this.createToscaRepresentation(toscaTemplate);
136 return Either.left(toscaRepresentation);
139 public Either<ToscaRepresentation, ToscaError> exportComponentInterface(Component component,
140 boolean isAssociatedComponent) {
141 if (null == DEFAULT_IMPORTS) {
142 log.debug(FAILED_TO_GET_DEFAULT_IMPORTS_CONFIGURATION);
143 return Either.right(ToscaError.GENERAL_ERROR);
146 ToscaTemplate toscaTemplate = new ToscaTemplate(TOSCA_VERSION);
147 toscaTemplate.setImports(new ArrayList<>(DEFAULT_IMPORTS));
148 Map<String, ToscaNodeType> nodeTypes = new HashMap<>();
149 Either<ToscaTemplate, ToscaError> toscaTemplateRes = convertInterfaceNodeType(new HashMap<>(), component,
150 toscaTemplate, nodeTypes, isAssociatedComponent);
151 if (toscaTemplateRes.isRight()) {
152 return Either.right(toscaTemplateRes.right().value());
155 toscaTemplate = toscaTemplateRes.left().value();
156 ToscaRepresentation toscaRepresentation = this.createToscaRepresentation(toscaTemplate);
157 return Either.left(toscaRepresentation);
160 public ToscaRepresentation createToscaRepresentation(ToscaTemplate toscaTemplate) {
161 CustomRepresenter representer = new CustomRepresenter();
162 DumperOptions options = new DumperOptions();
163 options.setAllowReadOnlyProperties(false);
164 options.setPrettyFlow(true);
166 options.setDefaultFlowStyle(FlowStyle.FLOW);
167 options.setCanonical(false);
169 representer.addClassTag(toscaTemplate.getClass(), Tag.MAP);
171 representer.setPropertyUtils(new UnsortedPropertyUtils());
172 Yaml yaml = new Yaml(representer, options);
174 String yamlAsString = yaml.dumpAsMap(toscaTemplate);
176 StringBuilder sb = new StringBuilder();
177 sb.append(ConfigurationManager.getConfigurationManager().getConfiguration().getHeatEnvArtifactHeader());
178 sb.append(yamlAsString);
179 sb.append(ConfigurationManager.getConfigurationManager().getConfiguration().getHeatEnvArtifactFooter());
181 ToscaRepresentation toscaRepresentation = new ToscaRepresentation();
182 toscaRepresentation.setMainYaml(sb.toString());
183 toscaRepresentation.setDependencies(toscaTemplate.getDependencies());
185 return toscaRepresentation;
188 public Either<ToscaTemplate, ToscaError> getDependencies(Component component) {
189 ToscaTemplate toscaTemplate = new ToscaTemplate(null);
190 Either<ImmutablePair<ToscaTemplate, Map<String, Component>>, ToscaError> fillImports = fillImports(component,
192 if (fillImports.isRight()) {
193 return Either.right(fillImports.right().value());
195 return Either.left(fillImports.left().value().left);
198 private Either<ToscaTemplate, ToscaError> convertToToscaTemplate(Component component) {
199 if (null == DEFAULT_IMPORTS) {
200 log.debug(FAILED_TO_GET_DEFAULT_IMPORTS_CONFIGURATION);
201 return Either.right(ToscaError.GENERAL_ERROR);
204 log.trace("start tosca export for {}", component.getUniqueId());
205 ToscaTemplate toscaTemplate = new ToscaTemplate(TOSCA_VERSION);
207 toscaTemplate.setMetadata(convertMetadata(component));
208 toscaTemplate.setImports(new ArrayList<>(DEFAULT_IMPORTS));
209 Map<String, ToscaNodeType> nodeTypes = new HashMap<>();
210 if (ModelConverter.isAtomicComponent(component)) {
211 log.trace("convert component as node type");
212 return convertNodeType(new HashMap<>(), component, toscaTemplate, nodeTypes);
214 log.trace("convert component as topology template");
215 return convertToscaTemplate(component, toscaTemplate);
220 private Either<ToscaTemplate, ToscaError> convertToscaTemplate(Component component, ToscaTemplate toscaNode) {
222 Either<ImmutablePair<ToscaTemplate, Map<String, Component>>, ToscaError> importsRes = fillImports(component,
224 if (importsRes.isRight()) {
225 return Either.right(importsRes.right().value());
227 toscaNode = importsRes.left().value().left;
228 Map<String, Component> componentCache = importsRes.left().value().right;
229 Either<Map<String, ToscaNodeType>, ToscaError> nodeTypesMapEither = createProxyNodeTypes(componentCache,
231 if (nodeTypesMapEither.isRight()) {
232 log.debug("Failed to fetch normative service proxy resource by tosca name, error {}",
233 nodeTypesMapEither.right().value());
234 return Either.right(nodeTypesMapEither.right().value());
236 Map<String, ToscaNodeType> nodeTypesMap = nodeTypesMapEither.left().value();
237 if (nodeTypesMap != null && !nodeTypesMap.isEmpty()) {
238 toscaNode.setNode_types(nodeTypesMap);
241 Either<Map<String, Object>, ToscaError> proxyInterfaceTypesEither = createProxyInterfaceTypes(component);
242 if (proxyInterfaceTypesEither.isRight()) {
243 log.debug("Failed to populate service proxy local interface types in tosca, error {}",
244 nodeTypesMapEither.right().value());
245 return Either.right(proxyInterfaceTypesEither.right().value());
247 Map<String, Object> proxyInterfaceTypes = proxyInterfaceTypesEither.left().value();
248 if (MapUtils.isNotEmpty(proxyInterfaceTypes)) {
249 toscaNode.setInterface_types(proxyInterfaceTypes);
252 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> dataTypesEither = dataTypeCache.getAll();
253 if (dataTypesEither.isRight()) {
254 log.debug("Failed to retrieve all data types {}", dataTypesEither.right().value());
255 return Either.right(ToscaError.GENERAL_ERROR);
257 Map<String, DataTypeDefinition> dataTypes = dataTypesEither.left().value();
258 ToscaTopolgyTemplate topologyTemplate = new ToscaTopolgyTemplate();
259 List<InputDefinition> inputDef = component.getInputs();
260 Map<String, ToscaProperty> inputs = inputConverter.convertInputs(inputDef, dataTypes);
262 if (!inputs.isEmpty()) {
263 topologyTemplate.setInputs(inputs);
266 List<ComponentInstance> componentInstances = component.getComponentInstances();
267 Map<String, List<ComponentInstanceProperty>> componentInstancesProperties =
268 component.getComponentInstancesProperties();
269 Map<String, List<ComponentInstanceInterface>> componentInstanceInterfaces =
270 component.getComponentInstancesInterfaces();
271 if (componentInstances != null && !componentInstances.isEmpty()) {
273 Either<Map<String, ToscaNodeTemplate>, ToscaError> nodeTemplates =
274 convertNodeTemplates(component, componentInstances,
275 componentInstancesProperties, componentInstanceInterfaces,
276 componentCache, dataTypes, topologyTemplate);
277 if (nodeTemplates.isRight()) {
278 return Either.right(nodeTemplates.right().value());
280 log.debug("node templates converted");
282 topologyTemplate.setNode_templates(nodeTemplates.left().value());
286 addGroupsToTopologyTemplate(component, topologyTemplate);
289 addPoliciesToTopologyTemplate(component, topologyTemplate);
290 } catch (SdcResourceNotFoundException e) {
291 log.debug("Fail to add policies to topology template:",e);
292 return Either.right(ToscaError.GENERAL_ERROR);
296 SubstitutionMapping substitutionMapping = new SubstitutionMapping();
297 String toscaResourceName;
298 switch (component.getComponentType()) {
300 toscaResourceName = ((ResourceMetadataDataDefinition) component.getComponentMetadataDefinition()
301 .getMetadataDataDefinition()).getToscaResourceName();
304 toscaResourceName = SERVICE_NODE_TYPE_PREFIX
305 + component.getComponentMetadataDefinition().getMetadataDataDefinition().getSystemName();
308 log.debug(NOT_SUPPORTED_COMPONENT_TYPE, component.getComponentType());
309 return Either.right(ToscaError.NOT_SUPPORTED_TOSCA_TYPE);
311 substitutionMapping.setNode_type(toscaResourceName);
313 Either<SubstitutionMapping, ToscaError> capabilities = convertCapabilities(component, substitutionMapping, componentCache);
314 if (capabilities.isRight()) {
315 return Either.right(capabilities.right().value());
317 substitutionMapping = capabilities.left().value();
319 Either<SubstitutionMapping, ToscaError> requirements = capabilityRequirementConverter
320 .convertSubstitutionMappingRequirements(componentCache, component, substitutionMapping);
321 if (requirements.isRight()) {
322 return Either.right(requirements.right().value());
324 substitutionMapping = requirements.left().value();
326 topologyTemplate.setSubstitution_mappings(substitutionMapping);
328 toscaNode.setTopology_template(topologyTemplate);
330 return Either.left(toscaNode);
333 private void addGroupsToTopologyTemplate(Component component, ToscaTopolgyTemplate topologyTemplate) {
336 Map<String, ToscaGroupTemplate> groups = groupExportParser.getGroups(component);
338 topologyTemplate.addGroups(groups);
342 private void addPoliciesToTopologyTemplate(Component component, ToscaTopolgyTemplate topologyTemplate)
343 throws SdcResourceNotFoundException {
344 Map<String, ToscaPolicyTemplate> policies = policyExportParser.getPolicies(component);
345 if(policies!= null) {
346 topologyTemplate.addPolicies(policies);
350 private ToscaMetadata convertMetadata(Component component) {
351 return convertMetadata(component, false, null);
354 private ToscaMetadata convertMetadata(Component component, boolean isInstance,
355 ComponentInstance componentInstance) {
356 ToscaMetadata toscaMetadata = new ToscaMetadata();
357 toscaMetadata.setInvariantUUID(component.getInvariantUUID());
358 toscaMetadata.setUUID(component.getUUID());
359 toscaMetadata.setDescription(component.getDescription());
360 toscaMetadata.setName(component.getComponentMetadataDefinition().getMetadataDataDefinition().getName());
362 List<CategoryDefinition> categories = component.getCategories();
363 CategoryDefinition categoryDefinition = categories.get(0);
364 toscaMetadata.setCategory(categoryDefinition.getName());
367 toscaMetadata.setVersion(component.getVersion());
368 toscaMetadata.setCustomizationUUID(componentInstance.getCustomizationUUID());
369 if (componentInstance.getSourceModelInvariant() != null
370 && !componentInstance.getSourceModelInvariant().isEmpty()) {
371 toscaMetadata.setVersion(componentInstance.getComponentVersion());
372 toscaMetadata.setSourceModelInvariant(componentInstance.getSourceModelInvariant());
373 toscaMetadata.setSourceModelUuid(componentInstance.getSourceModelUuid());
374 toscaMetadata.setSourceModelName(componentInstance.getSourceModelName());
375 toscaMetadata.setName(
376 componentInstance.getSourceModelName() + " " + OriginTypeEnum.ServiceProxy.getDisplayValue());
377 toscaMetadata.setDescription(componentInstance.getDescription());
381 switch (component.getComponentType()) {
383 Resource resource = (Resource) component;
385 if (isInstance && componentInstance.getOriginType() == OriginTypeEnum.ServiceProxy) {
386 toscaMetadata.setType(componentInstance.getOriginType().getDisplayValue());
388 toscaMetadata.setType(resource.getResourceType().name());
390 toscaMetadata.setSubcategory(categoryDefinition.getSubcategories().get(0).getName());
391 toscaMetadata.setResourceVendor(resource.getVendorName());
392 toscaMetadata.setResourceVendorRelease(resource.getVendorRelease());
393 toscaMetadata.setResourceVendorModelNumber(resource.getResourceVendorModelNumber());
396 Service service = (Service) component;
397 toscaMetadata.setType(component.getComponentType().getValue());
398 toscaMetadata.setServiceType(service.getServiceType());
399 toscaMetadata.setServiceRole(service.getServiceRole());
400 toscaMetadata.setEnvironmentContext(service.getEnvironmentContext());
401 resolveInstantiationTypeAndSetItToToscaMetaData(toscaMetadata, service);
404 toscaMetadata.setServiceEcompNaming(((Service) component).isEcompGeneratedNaming());
405 toscaMetadata.setEcompGeneratedNaming(((Service) component).isEcompGeneratedNaming());
406 toscaMetadata.setNamingPolicy(((Service) component).getNamingPolicy());
410 log.debug(NOT_SUPPORTED_COMPONENT_TYPE, component.getComponentType());
412 return toscaMetadata;
415 private void resolveInstantiationTypeAndSetItToToscaMetaData(ToscaMetadata toscaMetadata, Service service) {
416 if (service.getInstantiationType() != null) {
417 toscaMetadata.setInstantiationType(service.getInstantiationType());
420 toscaMetadata.setInstantiationType(StringUtils.EMPTY);
424 private Either<ImmutablePair<ToscaTemplate, Map<String, Component>>, ToscaError> fillImports(Component component,
425 ToscaTemplate toscaTemplate) {
427 if (null == DEFAULT_IMPORTS) {
428 log.debug(FAILED_TO_GET_DEFAULT_IMPORTS_CONFIGURATION);
429 return Either.right(ToscaError.GENERAL_ERROR);
431 Map<String, Component> componentCache = new HashMap<>();
433 if (!ModelConverter.isAtomicComponent(component)) {
434 List<Map<String, Map<String, String>>> additionalImports = toscaTemplate.getImports() == null
435 ? new ArrayList<>(DEFAULT_IMPORTS) : new ArrayList<>(toscaTemplate.getImports());
437 List<Triple<String, String, Component>> dependecies = new ArrayList<>();
439 Map<String, ArtifactDefinition> toscaArtifacts = component.getToscaArtifacts();
440 if (isNotEmpty(toscaArtifacts)) {
441 ArtifactDefinition artifactDefinition = toscaArtifacts.get(ToscaExportHandler.ASSET_TOSCA_TEMPLATE);
442 if(artifactDefinition != null) {
443 Map<String, Map<String, String>> importsListMember = new HashMap<>();
444 Map<String, String> interfaceFiles = new HashMap<>();
445 interfaceFiles.put(IMPORTS_FILE_KEY, getInterfaceFilename(artifactDefinition.getArtifactName()));
446 StringBuilder keyNameBuilder = new StringBuilder();
447 keyNameBuilder.append(component.getComponentType().toString().toLowerCase()).append("-")
448 .append(component.getName()).append("-interface");
449 importsListMember.put(keyNameBuilder.toString(), interfaceFiles);
450 additionalImports.add(importsListMember);
453 List<ComponentInstance> componentInstances = component.getComponentInstances();
454 if (componentInstances != null && !componentInstances.isEmpty()) {
455 componentInstances.forEach(ci -> createDependency(componentCache, additionalImports, dependecies, ci));
457 toscaTemplate.setDependencies(dependecies);
458 toscaTemplate.setImports(additionalImports);
460 log.debug("currently imports supported for VF and service only");
462 return Either.left(new ImmutablePair<>(toscaTemplate, componentCache));
465 private void createDependency(Map<String, Component> componentCache, List<Map<String, Map<String, String>>> imports,
466 List<Triple<String, String, Component>> dependecies, ComponentInstance ci) {
467 Map<String, String> files = new HashMap<>();
468 Map<String, Map<String, String>> importsListMember = new HashMap<>();
469 StringBuilder keyNameBuilder;
471 Component componentRI = componentCache.get(ci.getComponentUid());
472 if (componentRI == null) {
473 // all resource must be only once!
474 Either<Component, StorageOperationStatus> resource = toscaOperationFacade
475 .getToscaFullElement(ci.getComponentUid());
476 if ((resource.isRight()) && (log.isDebugEnabled())) {
477 log.debug("Failed to fetch resource with id {} for instance {}",ci.getComponentUid() ,ci.getUniqueId());
481 Component fetchedComponent = resource.left().value();
482 componentCache.put(fetchedComponent.getUniqueId(), fetchedComponent);
484 if (ci.getOriginType() == OriginTypeEnum.ServiceProxy){
485 Either<Component, StorageOperationStatus> sourceService = toscaOperationFacade
486 .getToscaFullElement(ci.getSourceModelUid());
487 if (sourceService.isRight() && (log.isDebugEnabled())) {
488 log.debug("Failed to fetch source service with id {} for proxy {}", ci.getSourceModelUid(), ci.getUniqueId());
490 Component fetchedSource = sourceService.left().value();
491 componentCache.put(fetchedSource.getUniqueId(), fetchedSource);
494 componentRI = fetchedComponent;
496 Map<String, ArtifactDefinition> toscaArtifacts = componentRI.getToscaArtifacts();
497 ArtifactDefinition artifactDefinition = toscaArtifacts.get(ASSET_TOSCA_TEMPLATE);
498 if (artifactDefinition != null) {
499 String artifactName = artifactDefinition.getArtifactName();
500 files.put(IMPORTS_FILE_KEY, artifactName);
501 keyNameBuilder = new StringBuilder();
502 keyNameBuilder.append(fetchedComponent.getComponentType().toString().toLowerCase());
503 keyNameBuilder.append("-");
504 keyNameBuilder.append(ci.getComponentName());
505 importsListMember.put(keyNameBuilder.toString(), files);
506 imports.add(importsListMember);
507 dependecies.add(new ImmutableTriple<>(artifactName,
508 artifactDefinition.getEsId(), fetchedComponent));
510 if (!ModelConverter.isAtomicComponent(componentRI)) {
511 importsListMember = new HashMap<>();
512 Map<String, String> interfaceFiles = new HashMap<>();
513 interfaceFiles.put(IMPORTS_FILE_KEY, getInterfaceFilename(artifactName));
514 keyNameBuilder.append("-interface");
515 importsListMember.put(keyNameBuilder.toString(), interfaceFiles);
516 imports.add(importsListMember);
522 public static String getInterfaceFilename(String artifactName) {
523 return artifactName.substring(0, artifactName.lastIndexOf('.')) + ToscaExportHandler.TOSCA_INTERFACE_NAME;
526 private Either<ToscaTemplate, ToscaError> convertNodeType(Map<String, Component> componentsCache, Component component, ToscaTemplate toscaNode,
527 Map<String, ToscaNodeType> nodeTypes) {
528 return convertInterfaceNodeType(componentsCache, component, toscaNode, nodeTypes, false);
531 private Either<ToscaTemplate, ToscaError> convertInterfaceNodeType(Map<String, Component> componentsCache,
532 Component component, ToscaTemplate toscaNode,
533 Map<String, ToscaNodeType> nodeTypes,
534 boolean isAssociatedComponent) {
535 log.debug("start convert node type for {}", component.getUniqueId());
536 ToscaNodeType toscaNodeType = createNodeType(component);
538 Either<Map<String, InterfaceDefinition>, StorageOperationStatus> lifecycleTypeEither =
539 interfaceLifecycleOperation.getAllInterfaceLifecycleTypes();
540 if(lifecycleTypeEither.isRight()){
541 log.debug("Failed to fetch all interface types :", lifecycleTypeEither.right().value());
542 return Either.right(ToscaError.GENERAL_ERROR);
544 List<String> allGlobalInterfaceTypes = lifecycleTypeEither.left().value()
547 .map(InterfaceDataDefinition::getType)
548 .collect(Collectors.toList());
549 toscaNode.setInterface_types(addInterfaceTypeElement(component, allGlobalInterfaceTypes));
551 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> dataTypesEither = dataTypeCache.getAll();
552 if (dataTypesEither.isRight()) {
553 log.debug("Failed to fetch all data types :", dataTypesEither.right().value());
554 return Either.right(ToscaError.GENERAL_ERROR);
557 Map<String, DataTypeDefinition> dataTypes = dataTypesEither.left().value();
559 List<InputDefinition> inputDef = component.getInputs();
560 Map<String, ToscaProperty> mergedProperties = new HashMap<>();
561 addInterfaceDefinitionElement(component, toscaNodeType, dataTypes, isAssociatedComponent);
562 addInputsToProperties(dataTypes, inputDef, mergedProperties);
564 if(CollectionUtils.isNotEmpty(component.getProperties())) {
565 List<PropertyDefinition> properties = component.getProperties();
566 Map<String, ToscaProperty> convertedProperties = properties.stream()
567 .map(propertyDefinition -> resolvePropertyValueFromInput(propertyDefinition, component.getInputs()))
568 .collect(Collectors.toMap(PropertyDataDefinition::getName,
569 property -> propertyConvertor.convertProperty(dataTypes, property,
570 PropertyConvertor.PropertyType.PROPERTY)));
571 // merge component properties and inputs properties
572 mergedProperties.putAll(convertedProperties);
574 if (MapUtils.isNotEmpty(mergedProperties)) {
575 toscaNodeType.setProperties(mergedProperties);
578 /* convert private data_types */
579 List<DataTypeDefinition> privateDataTypes = component.getDataTypes();
580 if (CollectionUtils.isNotEmpty(privateDataTypes) ) {
581 Map<String, ToscaDataType> toscaDataTypeMap = new HashMap<>();
582 for (DataTypeDefinition dataType: privateDataTypes) {
583 log.debug("Emitting private data type: component.name={} dataType.name={}",
584 component.getNormalizedName(), dataType.getName());
585 ToscaDataType toscaDataType = new ToscaDataType();
586 toscaDataType.setDerived_from(dataType.getDerivedFromName());
587 toscaDataType.setDescription(dataType.getDescription());
588 toscaDataType.setVersion(dataType.getVersion());
589 if (CollectionUtils.isNotEmpty(dataType.getProperties())) {
590 toscaDataType.setProperties(dataType.getProperties().stream()
591 .collect(Collectors.toMap(
593 s -> propertyConvertor.convertProperty(dataTypes, s, PropertyConvertor.PropertyType.PROPERTY)
596 toscaDataTypeMap.put(dataType.getName(), toscaDataType);
598 toscaNode.setData_types(toscaDataTypeMap);
601 // Extracted to method for code reuse
602 return convertReqCapAndTypeName(componentsCache, component, toscaNode, nodeTypes, toscaNodeType, dataTypes);
605 private Either<ToscaTemplate, ToscaError> convertReqCapAndTypeName(Map<String, Component> componentsCache, Component component, ToscaTemplate toscaNode,
606 Map<String, ToscaNodeType> nodeTypes, ToscaNodeType toscaNodeType,
607 Map<String, DataTypeDefinition> dataTypes) {
608 Either<ToscaNodeType, ToscaError> capabilities = convertCapabilities(componentsCache, component, toscaNodeType, dataTypes);
609 if (capabilities.isRight()) {
610 return Either.right(capabilities.right().value());
612 toscaNodeType = capabilities.left().value();
613 log.debug("Capabilities converted for {}", component.getUniqueId());
615 Either<ToscaNodeType, ToscaError> requirements = capabilityRequirementConverter.convertRequirements(componentsCache, component,
617 if (requirements.isRight()) {
618 return Either.right(requirements.right().value());
620 toscaNodeType = requirements.left().value();
621 log.debug("Requirements converted for {}", component.getUniqueId());
623 String toscaResourceName;
624 switch (component.getComponentType()) {
626 toscaResourceName = ((ResourceMetadataDataDefinition) component.getComponentMetadataDefinition()
627 .getMetadataDataDefinition()).getToscaResourceName();
630 toscaResourceName = SERVICE_NODE_TYPE_PREFIX
631 + component.getComponentMetadataDefinition().getMetadataDataDefinition().getSystemName();
634 log.debug(NOT_SUPPORTED_COMPONENT_TYPE, component.getComponentType());
635 return Either.right(ToscaError.NOT_SUPPORTED_TOSCA_TYPE);
638 nodeTypes.put(toscaResourceName, toscaNodeType);
639 toscaNode.setNode_types(nodeTypes);
640 log.debug("finish convert node type for {}", component.getUniqueId());
641 return Either.left(toscaNode);
644 protected Either<Map<String, ToscaNodeTemplate>, ToscaError> convertNodeTemplates(
646 List<ComponentInstance> componentInstances,
647 Map<String, List<ComponentInstanceProperty>> componentInstancesProperties,
648 Map<String, List<ComponentInstanceInterface>> componentInstanceInterfaces,
649 Map<String, Component> componentCache, Map<String, DataTypeDefinition> dataTypes,
650 ToscaTopolgyTemplate topologyTemplate) {
652 Either<Map<String, ToscaNodeTemplate>, ToscaError> convertNodeTemplatesRes = null;
653 log.debug("start convert topology template for {} for type {}", component.getUniqueId(),
654 component.getComponentType());
655 Map<String, ToscaNodeTemplate> nodeTemplates = new HashMap<>();
656 Map<String, List<ComponentInstanceInput>> componentInstancesInputs = component.getComponentInstancesInputs();
658 Map<String, ToscaGroupTemplate> groupsMap = null;
659 for (ComponentInstance componentInstance : componentInstances) {
660 ToscaNodeTemplate nodeTemplate = new ToscaNodeTemplate();
661 nodeTemplate.setType(componentInstance.getToscaComponentName());
662 nodeTemplate.setDirectives(componentInstance.getDirectives());
663 nodeTemplate.setNode_filter(convertToNodeTemplateNodeFilterComponent(componentInstance.getNodeFilter()));
665 Either<Component, Boolean> originComponentRes = capabilityRequirementConverter
666 .getOriginComponent(componentCache, componentInstance);
667 if (originComponentRes.isRight()) {
668 convertNodeTemplatesRes = Either.right(ToscaError.NODE_TYPE_REQUIREMENT_ERROR);
671 Either<ToscaNodeTemplate, ToscaError> requirements = convertComponentInstanceRequirements(component,
672 componentInstance, component.getComponentInstancesRelations(), nodeTemplate,
673 originComponentRes.left().value(), componentCache);
674 if (requirements.isRight()) {
675 convertNodeTemplatesRes = Either.right(requirements.right().value());
678 String instanceUniqueId = componentInstance.getUniqueId();
679 log.debug("Component instance Requirements converted for instance {}", instanceUniqueId);
681 nodeTemplate = requirements.left().value();
683 Component originalComponent = componentCache.get(componentInstance.getActualComponentUid());
685 if (componentInstance.getOriginType() == OriginTypeEnum.ServiceProxy){
686 Component componentOfProxy = componentCache.get(componentInstance.getComponentUid());
687 nodeTemplate.setMetadata(convertMetadata(componentOfProxy, true, componentInstance));
689 nodeTemplate.setMetadata(convertMetadata(originalComponent, true, componentInstance));
692 Either<ToscaNodeTemplate, ToscaError> capabilities = capabilityRequirementConverter
693 .convertComponentInstanceCapabilities(componentInstance, dataTypes, nodeTemplate);
694 if (capabilities.isRight()) {
695 convertNodeTemplatesRes = Either.right(capabilities.right().value());
698 log.debug("Component instance Capabilities converted for instance {}", instanceUniqueId);
700 nodeTemplate = capabilities.left().value();
701 Map<String, Object> props = new HashMap<>();
703 if (originalComponent.getComponentType() == ComponentTypeEnum.RESOURCE) {
704 // Adds the properties of parent component to map
705 addPropertiesOfParentComponent(dataTypes, originalComponent, props);
708 if (null != componentInstancesProperties && componentInstancesProperties.containsKey(instanceUniqueId)) {
709 addPropertiesOfComponentInstance(componentInstancesProperties, dataTypes, instanceUniqueId,
713 if (componentInstancesInputs != null && componentInstancesInputs.containsKey(instanceUniqueId)
714 && !isComponentOfTypeServiceProxy(componentInstance)) {
715 //For service proxy the inputs are already handled under instance properties above
716 addComponentInstanceInputs(dataTypes, componentInstancesInputs, instanceUniqueId,
719 //M3[00001] - NODE TEMPLATE INTERFACES - START
720 handleInstanceInterfaces(componentInstanceInterfaces, componentInstance, dataTypes, nodeTemplate,
721 instanceUniqueId, component);
722 //M3[00001] - NODE TEMPLATE INTERFACES - END
723 if (props != null && !props.isEmpty()) {
724 nodeTemplate.setProperties(props);
727 List<GroupInstance> groupInstances = componentInstance.getGroupInstances();
728 if (groupInstances != null) {
729 if (groupsMap == null) {
730 groupsMap = new HashMap<>();
732 for (GroupInstance groupInst : groupInstances) {
733 boolean addToTosca = true;
735 List<String> artifacts = groupInst.getArtifacts();
736 if (artifacts == null || artifacts.isEmpty()) {
741 ToscaGroupTemplate toscaGroup = groupExportParser.getToscaGroupTemplate(groupInst, componentInstance.getInvariantName());
742 groupsMap.put(groupInst.getName(), toscaGroup);
747 nodeTemplates.put(componentInstance.getName(), nodeTemplate);
749 if (groupsMap != null) {
750 log.debug("instance groups added");
751 topologyTemplate.addGroups(groupsMap);
753 if (component.getComponentType() == ComponentTypeEnum.SERVICE && isNotEmpty(((Service) component).getForwardingPaths())) {
754 log.debug("Starting converting paths for component {}, name {}", component.getUniqueId(),
755 component.getName());
756 ForwardingPathToscaUtil.addForwardingPaths((Service) component, nodeTemplates, capabilityRequirementConverter, componentCache, toscaOperationFacade);
757 log.debug("Finished converting paths for component {}, name {}", component.getUniqueId(),
758 component.getName());
760 if (convertNodeTemplatesRes == null) {
761 convertNodeTemplatesRes = Either.left(nodeTemplates);
763 log.debug("finish convert topology template for {} for type {}", component.getUniqueId(),
764 component.getComponentType());
765 return convertNodeTemplatesRes;
768 private void handleInstanceInterfaces(
769 Map<String, List<ComponentInstanceInterface>> componentInstanceInterfaces,
770 ComponentInstance componentInstance, Map<String, DataTypeDefinition> dataTypes, ToscaNodeTemplate nodeTemplate,
771 String instanceUniqueId,
772 Component parentComponent) {
774 Map<String, Object> interfaces;
776 // we need to handle service proxy interfaces
777 if(isComponentOfTypeServiceProxy(componentInstance)) {
778 if(MapUtils.isEmpty(componentInstanceInterfaces)
779 || !componentInstanceInterfaces.containsKey(instanceUniqueId)) {
782 List<ComponentInstanceInterface> currServiceInterfaces =
783 componentInstanceInterfaces.get(instanceUniqueId);
785 Map<String, InterfaceDefinition> tmpInterfaces = new HashMap<>();
786 currServiceInterfaces.forEach(instInterface -> tmpInterfaces.put(instInterface
787 .getUniqueId(), instInterface));
789 interfaces = InterfacesOperationsToscaUtil
790 .getInterfacesMap(parentComponent, componentInstance, tmpInterfaces, dataTypes, true, true);
794 getComponentInstanceInterfaceInstances(componentInstanceInterfaces,
795 componentInstance, instanceUniqueId);
797 nodeTemplate.setInterfaces(interfaces);
800 private boolean isComponentOfTypeServiceProxy(ComponentInstance componentInstance) {
801 return Objects.nonNull(componentInstance.getOriginType())
802 && componentInstance.getOriginType().getValue().equals("Service Proxy");
805 //M3[00001] - NODE TEMPLATE INTERFACES - START
806 private Map<String, Object> getComponentInstanceInterfaceInstances(Map<String, List<ComponentInstanceInterface>> componentInstancesInterfaces,
807 ComponentInstance componentInstance,
808 String instanceUniqueId) {
809 if(MapUtils.isEmpty(componentInstancesInterfaces)) {
813 List<ComponentInstanceInterface> componentInstanceInterfaces =
814 componentInstancesInterfaces.get(instanceUniqueId);
816 if(CollectionUtils.isEmpty(componentInstanceInterfaces)) {
820 Map<String, Object> interfaces = new HashMap<>();
821 for(ComponentInstanceInterface componentInstanceInterface : componentInstanceInterfaces) {
822 interfaces.put(componentInstanceInterface.getInterfaceId(),
823 removeOperationsKeyFromInterface(componentInstanceInterface.getInterfaceInstanceDataDefinition()));
826 componentInstance.setInterfaces(interfaces);
831 private void addComponentInstanceInputs(Map<String, DataTypeDefinition> dataTypes,
832 Map<String, List<ComponentInstanceInput>> componentInstancesInputs,
833 String instanceUniqueId, Map<String, Object> props) {
835 List<ComponentInstanceInput> instanceInputsList = componentInstancesInputs.get(instanceUniqueId);
836 if (instanceInputsList != null) {
837 instanceInputsList.forEach(input -> {
839 Supplier<String> supplier = () -> input.getValue() != null && !Objects.isNull(input.getValue())
840 ? input.getValue() : input.getDefaultValue();
841 propertyConvertor.convertAndAddValue(dataTypes, props, input, supplier);
846 private void addPropertiesOfComponentInstance(
847 Map<String, List<ComponentInstanceProperty>> componentInstancesProperties,
848 Map<String, DataTypeDefinition> dataTypes, String instanceUniqueId,
849 Map<String, Object> props) {
851 if (isNotEmpty(componentInstancesProperties)) {
852 componentInstancesProperties.get(instanceUniqueId)
853 // Converts and adds each value to property map
854 .forEach(prop -> propertyConvertor.convertAndAddValue(dataTypes, props, prop,
859 private void addPropertiesOfParentComponent(Map<String, DataTypeDefinition> dataTypes,
860 Component componentOfInstance, Map<String, Object> props) {
862 List<PropertyDefinition> componentProperties = componentOfInstance.getProperties();
863 if (isNotEmpty(componentProperties)) {
864 componentProperties.stream()
865 // Filters out properties with empty default values
866 .filter(prop -> StringUtils.isNotEmpty(prop.getDefaultValue()))
867 // Converts and adds each value to property map
868 .forEach(prop -> propertyConvertor.convertAndAddValue(dataTypes, props, prop,
869 prop::getDefaultValue));
875 * @param componentInstance
880 private void convertAndAddValue(Map<String, DataTypeDefinition> dataTypes, ComponentInstance componentInstance,
881 Map<String, Object> props, PropertyDefinition prop, Supplier<String> supplier) {
882 Object convertedValue = convertValue(dataTypes, componentInstance, prop, supplier);
883 if (!ToscaValueBaseConverter.isEmptyObjectValue(convertedValue)) {
884 props.put(prop.getName(), convertedValue);
888 private <T extends PropertyDefinition> Object convertValue(Map<String, DataTypeDefinition> dataTypes,
889 ComponentInstance componentInstance, T input, Supplier<String> supplier) {
890 log.debug("Convert property or input value {} for instance {}", input.getName(),
891 componentInstance.getUniqueId());
892 String propertyType = input.getType();
893 String innerType = null;
894 if (input.getSchema() != null && input.getSchema().getProperty() != null) {
895 innerType = input.getSchema().getProperty().getType();
897 return propertyConvertor.convertToToscaObject(propertyType, supplier.get(), innerType,
901 private ToscaNodeType createNodeType(Component component) {
902 ToscaNodeType toscaNodeType = new ToscaNodeType();
903 if (ModelConverter.isAtomicComponent(component)) {
904 if (((Resource) component).getDerivedFrom() != null) {
905 toscaNodeType.setDerived_from(((Resource) component).getDerivedFrom().get(0));
907 toscaNodeType.setDescription(component.getDescription());
909 String derivedFrom = null != component.getDerivedFromGenericType() ? component.getDerivedFromGenericType()
910 : "tosca.nodes.Root";
911 toscaNodeType.setDerived_from(derivedFrom);
913 return toscaNodeType;
916 private Either<Map<String, Object>, ToscaError> createProxyInterfaceTypes(Component container) {
918 Map<String, Object> proxyInterfaceTypes = new HashMap<>();
919 Either<Map<String, Object>, ToscaError> res = Either.left(proxyInterfaceTypes);
920 List<ComponentInstance> componentInstances = container.getComponentInstances();
921 if (CollectionUtils.isEmpty(componentInstances)) {
924 Map<String, ComponentInstance> serviceProxyInstanceList = new HashMap<>();
925 componentInstances.stream()
926 .filter(this::isComponentOfTypeServiceProxy)
927 .forEach(inst -> serviceProxyInstanceList.put(inst.getToscaComponentName(), inst));
928 if (MapUtils.isEmpty(serviceProxyInstanceList)) {
931 for (Entry<String, ComponentInstance> entryProxy : serviceProxyInstanceList.entrySet()) {
932 Component serviceComponent;
933 ComponentParametersView componentParametersView = new ComponentParametersView();
934 componentParametersView.disableAll();
935 componentParametersView.setIgnoreInterfaces(false);
936 Either<Component, StorageOperationStatus> service = toscaOperationFacade
937 .getToscaElement(entryProxy.getValue().getSourceModelUid(), componentParametersView);
938 if (service.isRight()) {
939 log.debug("Failed to fetch original service component with id {} for instance {}",
940 entryProxy.getValue().getSourceModelUid(), entryProxy.getValue().getName());
941 return Either.right(ToscaError.GENERAL_ERROR);
943 serviceComponent = service.left().value();
946 Either<Map<String, InterfaceDefinition>, StorageOperationStatus> lifecycleTypeEither =
947 interfaceLifecycleOperation.getAllInterfaceLifecycleTypes();
948 if(lifecycleTypeEither.isRight()){
949 log.debug("Failed to retrieve global interface types :", lifecycleTypeEither.right().value());
950 return Either.right(ToscaError.GENERAL_ERROR);
953 List<String> allGlobalInterfaceTypes = lifecycleTypeEither.left().value().values().stream()
954 .map(InterfaceDataDefinition::getType)
955 .collect(Collectors.toList());
956 //Add interface types for local interfaces in the original service component for proxy
957 Map<String, Object> localInterfaceTypes = addInterfaceTypeElement(serviceComponent,
958 allGlobalInterfaceTypes);
959 if (MapUtils.isNotEmpty(localInterfaceTypes)) {
960 proxyInterfaceTypes.putAll(localInterfaceTypes);
964 return Either.left(proxyInterfaceTypes);
967 private Either<Map<String, ToscaNodeType>, ToscaError> createProxyNodeTypes(Map<String, Component> componentCache,
968 Component container) {
970 Map<String, ToscaNodeType> nodeTypesMap = new HashMap<>();
971 Either<Map<String, ToscaNodeType>, ToscaError> res = Either.left(nodeTypesMap);
973 List<ComponentInstance> componentInstances = container.getComponentInstances();
975 if (componentInstances == null || componentInstances.isEmpty()) {
978 Map<String, ComponentInstance> serviceProxyInstanceList = new HashMap<>();
979 List<ComponentInstance> proxyInst = componentInstances.stream()
980 .filter(p -> p.getOriginType().name().equals(OriginTypeEnum.ServiceProxy.name()))
981 .collect(Collectors.toList());
982 if (proxyInst != null && !proxyInst.isEmpty()) {
983 for (ComponentInstance inst : proxyInst) {
984 serviceProxyInstanceList.put(inst.getToscaComponentName(), inst);
988 if (serviceProxyInstanceList.isEmpty()) {
991 Either<Resource, StorageOperationStatus> serviceProxyOrigin = toscaOperationFacade
992 .getLatestByName("serviceProxy");
993 if (serviceProxyOrigin.isRight()) {
994 log.debug("Failed to fetch normative service proxy resource by tosca name, error {}",
995 serviceProxyOrigin.right().value());
996 return Either.right(ToscaError.NOT_SUPPORTED_TOSCA_TYPE);
998 Component origComponent = serviceProxyOrigin.left().value();
1000 for (Entry<String, ComponentInstance> entryProxy : serviceProxyInstanceList.entrySet()) {
1001 Component serviceComponent = null;
1002 ComponentParametersView componentParametersView = new ComponentParametersView();
1003 componentParametersView.disableAll();
1004 componentParametersView.setIgnoreCategories(false);
1005 componentParametersView.setIgnoreProperties(false);
1006 componentParametersView.setIgnoreInputs(false);
1007 componentParametersView.setIgnoreInterfaces(false);
1008 componentParametersView.setIgnoreRequirements(false);
1009 Either<Component, StorageOperationStatus> service = toscaOperationFacade
1010 .getToscaElement(entryProxy.getValue().getSourceModelUid(), componentParametersView);
1011 if (service.isRight()) {
1012 log.debug("Failed to fetch resource with id {} for instance {}", entryProxy.getValue().getSourceModelUid(), entryProxy.getValue().getName());
1014 serviceComponent = service.left().value();
1017 ToscaNodeType toscaNodeType = createProxyNodeType(componentCache, origComponent, serviceComponent,
1018 entryProxy.getValue());
1019 nodeTypesMap.put(entryProxy.getKey(), toscaNodeType);
1022 return Either.left(nodeTypesMap);
1025 private ToscaNodeType createProxyNodeType(Map<String, Component> componentCache , Component origComponent,
1026 Component proxyComponent, ComponentInstance instance) {
1027 ToscaNodeType toscaNodeType = new ToscaNodeType();
1028 String derivedFrom = ((Resource) origComponent).getToscaResourceName();
1030 toscaNodeType.setDerived_from(derivedFrom);
1031 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> dataTypesEither = dataTypeCache.getAll();
1032 if (dataTypesEither.isRight()) {
1033 log.debug("Failed to retrieve all data types {}", dataTypesEither.right().value());
1035 Map<String, DataTypeDefinition> dataTypes = dataTypesEither.left().value();
1036 Map<String, ToscaCapability> capabilities = this.capabilityRequirementConverter
1037 .convertProxyCapabilities(componentCache, instance, dataTypes);
1039 if (MapUtils.isNotEmpty(capabilities)) {
1040 toscaNodeType.setCapabilities(capabilities);
1042 List<Map<String, ToscaRequirement>> proxyNodeTypeRequirements = this.capabilityRequirementConverter
1043 .convertProxyRequirements(componentCache, instance);
1044 if (CollectionUtils.isNotEmpty(proxyNodeTypeRequirements)) {
1045 toscaNodeType.setRequirements(proxyNodeTypeRequirements);
1047 Optional<Map<String, ToscaProperty>> proxyProperties = getProxyNodeTypeProperties(proxyComponent, dataTypes);
1048 proxyProperties.ifPresent(toscaNodeType::setProperties);
1050 Optional<Map<String, Object>> proxyInterfaces = getProxyNodeTypeInterfaces(proxyComponent, dataTypes);
1051 proxyInterfaces.ifPresent(toscaNodeType::setInterfaces);
1053 return toscaNodeType;
1056 private Either<ToscaNodeTemplate, ToscaError> convertComponentInstanceRequirements(Component component,
1057 ComponentInstance componentInstance, List<RequirementCapabilityRelDef> relations,
1058 ToscaNodeTemplate nodeTypeTemplate, Component originComponent, Map<String, Component> componentCache) {
1060 List<Map<String, ToscaTemplateRequirement>> toscaRequirements = new ArrayList<>();
1061 if (!addRequirements(component, componentInstance, relations, originComponent, toscaRequirements, componentCache)) {
1062 log.debug("Failed to convert component instance requirements for the component instance {}. ",
1063 componentInstance.getName());
1064 return Either.right(ToscaError.NODE_TYPE_REQUIREMENT_ERROR);
1066 if (!toscaRequirements.isEmpty()) {
1067 nodeTypeTemplate.setRequirements(toscaRequirements);
1069 log.debug("Finished to convert requirements for the node type {} ", componentInstance.getName());
1070 return Either.left(nodeTypeTemplate);
1073 private boolean addRequirements(Component component, ComponentInstance componentInstance,
1074 List<RequirementCapabilityRelDef> relations, Component originComponent,
1075 List<Map<String, ToscaTemplateRequirement>> toscaRequirements, Map<String, Component> componentCache) {
1076 List<RequirementCapabilityRelDef> filteredRelations = relations.stream()
1077 .filter(p -> componentInstance.getUniqueId().equals(p.getFromNode())).collect(Collectors.toList());
1078 return isEmpty(filteredRelations) ||
1079 filteredRelations.stream()
1080 .allMatch(rel -> addRequirement(componentInstance, originComponent, component.getComponentInstances(), rel, toscaRequirements, componentCache));
1083 private boolean addRequirement(ComponentInstance fromInstance, Component fromOriginComponent,
1084 List<ComponentInstance> instancesList, RequirementCapabilityRelDef rel,
1085 List<Map<String, ToscaTemplateRequirement>> toscaRequirements, Map<String, Component> componentCache) {
1087 boolean result = true;
1088 Map<String, List<RequirementDefinition>> reqMap = fromOriginComponent.getRequirements();
1089 RelationshipInfo reqAndRelationshipPair = rel.getRelationships().get(0).getRelation();
1090 Either<Component, StorageOperationStatus> getOriginRes = null;
1091 Optional<RequirementDefinition> reqOpt = Optional.empty();
1092 Component toOriginComponent = null;
1093 Optional<CapabilityDefinition> capOpt = Optional.empty();
1095 ComponentInstance toInstance = instancesList.stream().filter(i -> rel.getToNode().equals(i.getUniqueId()))
1096 .findFirst().orElse(null);
1097 if (toInstance == null) {
1098 log.debug("Failed to find a relation from the node {} to the node {}", fromInstance.getName(),
1103 reqOpt = findRequirement(fromOriginComponent, reqMap, reqAndRelationshipPair, fromInstance.getUniqueId());
1104 if (!reqOpt.isPresent()) {
1105 log.debug("Failed to find a requirement with uniqueId {} on a component with uniqueId {}",
1106 reqAndRelationshipPair.getRequirementUid(), fromOriginComponent.getUniqueId());
1111 ComponentParametersView filter = new ComponentParametersView(true);
1112 filter.setIgnoreComponentInstances(false);
1113 filter.setIgnoreCapabilities(false);
1114 filter.setIgnoreGroups(false);
1115 getOriginRes = toscaOperationFacade.getToscaElement(toInstance.getActualComponentUid(), filter);
1116 if (getOriginRes.isRight()) {
1117 log.debug("Failed to build substituted name for the requirement {}. Failed to get an origin component with uniqueId {}",
1118 reqOpt.get().getName(), toInstance.getActualComponentUid());
1123 toOriginComponent = getOriginRes.left().value();
1124 capOpt = toOriginComponent.getCapabilities().get(reqOpt.get().getCapability()).stream()
1125 .filter(c -> isCapabilityBelongToRelation(reqAndRelationshipPair, c)).findFirst();
1126 if (!capOpt.isPresent()) {
1127 capOpt = findCapability(reqAndRelationshipPair, toOriginComponent, fromOriginComponent, reqOpt.get());
1128 if(!capOpt.isPresent()){
1130 log.debug("Failed to find a capability with name {} on a component with uniqueId {}",
1131 reqAndRelationshipPair.getCapability(), fromOriginComponent.getUniqueId());
1136 result = buildAndAddRequirement(toscaRequirements, fromOriginComponent, toOriginComponent, capOpt.get(),
1137 reqOpt.get(), reqAndRelationshipPair, toInstance, componentCache);
1142 private boolean isCapabilityBelongToRelation(RelationshipInfo reqAndRelationshipPair, CapabilityDefinition capability) {
1143 return capability.getName().equals(reqAndRelationshipPair.getCapability()) && (capability.getOwnerId() !=null && capability.getOwnerId().equals(reqAndRelationshipPair.getCapabilityOwnerId()));
1146 private Optional<CapabilityDefinition> findCapability(RelationshipInfo reqAndRelationshipPair, Component toOriginComponent, Component fromOriginComponent, RequirementDefinition requirement) {
1147 Optional<CapabilityDefinition> cap = toOriginComponent.getCapabilities().get(requirement.getCapability()).stream().filter(c -> c.getType().equals(requirement.getCapability())).findFirst();
1148 if (!cap.isPresent()) {
1149 log.debug("Failed to find a capability with name {} on a component with uniqueId {}", reqAndRelationshipPair.getCapability(), fromOriginComponent.getUniqueId());
1154 private boolean buildAndAddRequirement(List<Map<String, ToscaTemplateRequirement>> toscaRequirements, Component fromOriginComponent, Component toOriginComponent, CapabilityDefinition capability, RequirementDefinition requirement, RelationshipInfo reqAndRelationshipPair, ComponentInstance toInstance, Map<String, Component> componentCache) {
1155 List<String> reducedPath = capability.getPath();
1156 if(capability.getOwnerId() !=null){
1157 reducedPath = capabilityRequirementConverter.getReducedPathByOwner(capability.getPath() , capability.getOwnerId() );
1159 Either<String, Boolean> buildCapNameRes = capabilityRequirementConverter.buildSubstitutedName(componentCache,
1160 toOriginComponent, reducedPath, reqAndRelationshipPair.getCapability(), capability.getPreviousName());
1161 if (buildCapNameRes.isRight()) {
1163 "Failed to build a substituted capability name for the capability with name {} on a component with uniqueId {}",
1164 reqAndRelationshipPair.getCapability(), fromOriginComponent.getUniqueId());
1167 Either<String, Boolean> buildReqNameRes = capabilityRequirementConverter.buildSubstitutedName(componentCache, fromOriginComponent,
1168 requirement.getPath(), reqAndRelationshipPair.getRequirement(), requirement.getPreviousName());
1169 if (buildReqNameRes.isRight()) {
1171 "Failed to build a substituted requirement name for the requirement with name {} on a component with uniqueId {}",
1172 reqAndRelationshipPair.getRequirement(), fromOriginComponent.getUniqueId());
1175 ToscaTemplateRequirement toscaRequirement = new ToscaTemplateRequirement();
1176 Map<String, ToscaTemplateRequirement> toscaReqMap = new HashMap<>();
1177 toscaRequirement.setNode(toInstance.getName());
1178 toscaRequirement.setCapability(buildCapNameRes.left().value());
1179 toscaReqMap.put(buildReqNameRes.left().value(), toscaRequirement);
1180 toscaRequirements.add(toscaReqMap);
1184 private Optional<RequirementDefinition> findRequirement(Component fromOriginComponent, Map<String, List<RequirementDefinition>> reqMap, RelationshipInfo reqAndRelationshipPair, String fromInstanceId) {
1185 for(List<RequirementDefinition> reqList: reqMap.values()){
1186 Optional<RequirementDefinition> reqOpt = reqList.stream().filter(r -> isRequirementBelongToRelation(fromOriginComponent, reqAndRelationshipPair, r, fromInstanceId)).findFirst();
1187 if(reqOpt.isPresent()){
1191 return Optional.empty();
1195 * Allows detecting the requirement belonging to the received relationship
1196 * The detection logic is: A requirement belongs to a relationship IF 1.The
1197 * name of the requirement equals to the "requirement" field of the
1198 * relation; AND 2. In case of a non-atomic resource, OwnerId of the
1199 * requirement equals to requirementOwnerId of the relation OR uniqueId of
1200 * toInstance equals to capabilityOwnerId of the relation
1202 private boolean isRequirementBelongToRelation(Component originComponent, RelationshipInfo reqAndRelationshipPair, RequirementDefinition requirement, String fromInstanceId) {
1203 if (!StringUtils.equals(requirement.getName(), reqAndRelationshipPair.getRequirement())) {
1204 log.debug("Failed to find a requirement with name {} and reqAndRelationshipPair {}",
1205 requirement.getName(), reqAndRelationshipPair.getRequirement());
1208 return ModelConverter.isAtomicComponent(originComponent) ||
1209 isRequirementBelongToOwner(reqAndRelationshipPair, requirement, fromInstanceId, originComponent);
1212 private boolean isRequirementBelongToOwner(RelationshipInfo reqAndRelationshipPair, RequirementDefinition requirement, String fromInstanceId, Component originComponent) {
1213 return StringUtils.equals(requirement.getOwnerId(), reqAndRelationshipPair.getRequirementOwnerId())
1214 || (isCvfc(originComponent) && StringUtils.equals(fromInstanceId, reqAndRelationshipPair.getRequirementOwnerId())
1215 || StringUtils.equals(requirement.getOwnerId(), originComponent.getUniqueId()));
1218 private boolean isCvfc(Component component) {
1219 return component.getComponentType() == ComponentTypeEnum.RESOURCE &&
1220 ((Resource) component).getResourceType() == ResourceTypeEnum.CVFC;
1223 private Either<SubstitutionMapping, ToscaError> convertCapabilities(Component component,
1224 SubstitutionMapping substitutionMappings, Map<String, Component> componentCache) {
1226 Either<SubstitutionMapping, ToscaError> result = Either.left(substitutionMappings);
1227 Either<Map<String, String[]>, ToscaError> toscaCapabilitiesRes = capabilityRequirementConverter
1228 .convertSubstitutionMappingCapabilities(componentCache, component);
1229 if (toscaCapabilitiesRes.isRight()) {
1230 result = Either.right(toscaCapabilitiesRes.right().value());
1231 log.debug("Failed convert capabilities for the component {}. ", component.getName());
1232 } else if (isNotEmpty(toscaCapabilitiesRes.left().value())) {
1233 substitutionMappings.setCapabilities(toscaCapabilitiesRes.left().value());
1234 log.debug("Finish convert capabilities for the component {}. ", component.getName());
1236 log.debug("Finished to convert capabilities for the component {}. ", component.getName());
1240 private Either<ToscaNodeType, ToscaError> convertCapabilities(Map<String, Component> componentsCache, Component component, ToscaNodeType nodeType,
1241 Map<String, DataTypeDefinition> dataTypes) {
1242 Map<String, ToscaCapability> toscaCapabilities = capabilityRequirementConverter.convertCapabilities(componentsCache, component,
1244 if (!toscaCapabilities.isEmpty()) {
1245 nodeType.setCapabilities(toscaCapabilities);
1247 log.debug("Finish convert Capabilities for node type");
1249 return Either.left(nodeType);
1253 protected NodeFilter convertToNodeTemplateNodeFilterComponent(CINodeFilterDataDefinition inNodeFilter) {
1254 if (inNodeFilter == null){
1257 NodeFilter nodeFilter = new NodeFilter();
1259 ListDataDefinition<RequirementNodeFilterCapabilityDataDefinition> origCapabilities =
1260 inNodeFilter.getCapabilities();
1262 ListDataDefinition<RequirementNodeFilterPropertyDataDefinition> origProperties = inNodeFilter.getProperties();
1264 List<Map<String, CapabilityFilter>> capabilitiesCopy = new ArrayList<>();
1265 List<Map<String, List<Object>>> propertiesCopy = new ArrayList<>();
1267 copyNodeFilterCapabilitiesTemplate(origCapabilities, capabilitiesCopy);
1268 copyNodeFilterProperties(origProperties, propertiesCopy);
1270 if(CollectionUtils.isNotEmpty(capabilitiesCopy)) {
1271 nodeFilter.setCapabilities(capabilitiesCopy);
1274 if(CollectionUtils.isNotEmpty(propertiesCopy)) {
1275 nodeFilter.setProperties(propertiesCopy);
1278 nodeFilter.setTosca_id(cloneToscaId(inNodeFilter.getTosca_id()));
1281 nodeFilter = (NodeFilter) cloneObjectFromYml(nodeFilter, NodeFilter.class);
1286 private Object cloneToscaId(Object toscaId) {
1287 return Objects.isNull(toscaId) ? null
1288 : cloneObjectFromYml(toscaId, toscaId.getClass());
1292 private Object cloneObjectFromYml(Object objToClone, Class classOfObj) {
1293 String objectAsYml = yamlUtil.objectToYaml(objToClone);
1294 return yamlUtil.yamlToObject(objectAsYml, classOfObj);
1296 private void copyNodeFilterCapabilitiesTemplate(
1297 ListDataDefinition<RequirementNodeFilterCapabilityDataDefinition> origCapabilities,
1298 List<Map<String, CapabilityFilter>> capabilitiesCopy) {
1299 if(origCapabilities == null || origCapabilities.getListToscaDataDefinition() == null ||
1300 origCapabilities.getListToscaDataDefinition().isEmpty() ) {
1303 for(RequirementNodeFilterCapabilityDataDefinition capability : origCapabilities.getListToscaDataDefinition()) {
1304 Map<String, CapabilityFilter> capabilityFilterCopyMap = new HashMap<>();
1305 CapabilityFilter capabilityFilter = new CapabilityFilter();
1306 List<Map<String, List<Object>>> propertiesCopy = new ArrayList<>();
1307 copyNodeFilterProperties(capability.getProperties(), propertiesCopy);
1308 capabilityFilter.setProperties(propertiesCopy);
1309 capabilityFilterCopyMap.put(capability.getName(), capabilityFilter);
1310 capabilitiesCopy.add(capabilityFilterCopyMap);
1314 private List<Object> copyNodeFilterProperty(List<Object> propertyList) {
1315 String listAsString = yamlUtil.objectToYaml(propertyList);
1316 return yamlUtil.yamlToObject(listAsString, List.class);
1320 private void copyNodeFilterProperties(
1321 ListDataDefinition<RequirementNodeFilterPropertyDataDefinition> origProperties,
1322 List<Map<String, List<Object>>> propertiesCopy) {
1323 if(origProperties == null || origProperties.getListToscaDataDefinition() == null ||
1324 origProperties.isEmpty()) {
1327 for(RequirementNodeFilterPropertyDataDefinition propertyDataDefinition : origProperties.getListToscaDataDefinition()) {
1328 Map<String, List<Object>> propertyMapCopy = new HashMap<>();
1329 for(String propertyInfoEntry : propertyDataDefinition.getConstraints()) {
1330 Map propertyValObj = new YamlUtil().yamlToObject(propertyInfoEntry, Map.class);
1331 if (propertyMapCopy.containsKey(propertyDataDefinition.getName())){
1332 propertyMapCopy.get(propertyDataDefinition.getName()).add(propertyValObj);
1334 if (propertyDataDefinition.getName() != null) {
1335 List propsList =new ArrayList();
1336 propsList.add(propertyValObj);
1337 propertyMapCopy.put(propertyDataDefinition.getName(), propsList);
1339 propertyMapCopy.putAll(propertyValObj);
1343 propertiesCopy.add(propertyMapCopy);
1348 private static class CustomRepresenter extends Representer {
1349 public CustomRepresenter() {
1351 // null representer is exceptional and it is stored as an instance
1353 this.nullRepresenter = new RepresentNull();
1358 protected NodeTuple representJavaBeanProperty(Object javaBean, Property property, Object propertyValue,
1360 if (propertyValue == null) {
1363 // skip not relevant for Tosca property
1364 if ("dependencies".equals(property.getName())) {
1367 NodeTuple defaultNode = super.representJavaBeanProperty(javaBean, property, propertyValue, customTag);
1369 return "_defaultp_".equals(property.getName())
1370 ? new NodeTuple(representData("default"), defaultNode.getValueNode()) : defaultNode;
1375 protected MappingNode representJavaBean(Set<Property> properties, Object javaBean) {
1376 // remove the bean type from the output yaml (!! ...)
1377 if (!classTags.containsKey(javaBean.getClass())) {
1378 addClassTag(javaBean.getClass(), Tag.MAP);
1381 return super.representJavaBean(properties, javaBean);
1384 private class RepresentNull implements Represent {
1386 public Node representData(Object data) {
1387 // possible values are here http://yaml.org/type/null.html
1388 return representScalar(Tag.NULL, "");
1393 private static class UnsortedPropertyUtils extends PropertyUtils {
1395 protected Set<Property> createPropertySet(Class type, BeanAccess bAccess)
1396 throws IntrospectionException {
1397 Collection<Property> fields = getPropertiesMap(type, BeanAccess.FIELD).values();
1398 return new LinkedHashSet<>(fields);
1402 private Object removeOperationsKeyFromInterface(Object interfaceInstanceDataDefinition) {
1403 ObjectMapper objectMapper = new ObjectMapper();
1404 objectMapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false);
1406 Map<String, Object> interfaceAsMap = ServiceUtils.getObjectAsMap(interfaceInstanceDataDefinition);
1407 Map<String, Object> operations = (Map<String, Object>) interfaceAsMap.remove("operations");
1408 interfaceAsMap.remove("empty");
1410 if(MapUtils.isNotEmpty(operations)) {
1411 interfaceAsMap.putAll(operations);
1414 Object interfaceObject = objectMapper.convertValue(interfaceAsMap, Object.class);
1416 return interfaceObject;