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 static org.apache.commons.collections.CollectionUtils.isEmpty;
24 import static org.apache.commons.collections.CollectionUtils.isNotEmpty;
25 import static org.apache.commons.collections.MapUtils.isNotEmpty;
26 import static org.openecomp.sdc.be.components.utils.PropertiesUtils.resolvePropertyValueFromInput;
27 import static org.openecomp.sdc.be.tosca.InterfacesOperationsConverter.addInterfaceTypeElement;
28 import java.beans.IntrospectionException;
29 import java.util.ArrayList;
30 import java.util.Collection;
31 import java.util.HashMap;
32 import java.util.Iterator;
33 import java.util.LinkedHashSet;
34 import java.util.List;
36 import java.util.Map.Entry;
37 import java.util.Objects;
38 import java.util.Optional;
40 import java.util.function.Supplier;
41 import java.util.stream.Collectors;
42 import org.apache.commons.collections.CollectionUtils;
43 import org.apache.commons.collections.MapUtils;
44 import org.apache.commons.lang.StringUtils;
45 import org.apache.commons.lang3.tuple.ImmutablePair;
46 import org.apache.commons.lang3.tuple.ImmutableTriple;
47 import org.apache.commons.lang3.tuple.Triple;
48 import org.onap.sdc.tosca.services.YamlUtil;
49 import org.openecomp.sdc.be.components.impl.exceptions.SdcResourceNotFoundException;
50 import org.openecomp.sdc.be.config.ConfigurationManager;
51 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
52 import org.openecomp.sdc.be.datatypes.components.ResourceMetadataDataDefinition;
53 import org.openecomp.sdc.be.datatypes.elements.AttributeDataDefinition;
54 import org.openecomp.sdc.be.datatypes.elements.CINodeFilterDataDefinition;
55 import org.openecomp.sdc.be.datatypes.elements.InterfaceDataDefinition;
56 import org.openecomp.sdc.be.datatypes.elements.ListDataDefinition;
57 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
58 import org.openecomp.sdc.be.datatypes.elements.RequirementNodeFilterCapabilityDataDefinition;
59 import org.openecomp.sdc.be.datatypes.elements.RequirementNodeFilterPropertyDataDefinition;
60 import org.openecomp.sdc.be.datatypes.elements.RequirementSubstitutionFilterPropertyDataDefinition;
61 import org.openecomp.sdc.be.datatypes.elements.SubstitutionFilterDataDefinition;
62 import org.openecomp.sdc.be.datatypes.elements.ToscaArtifactDataDefinition;
63 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
64 import org.openecomp.sdc.be.datatypes.enums.OriginTypeEnum;
65 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
66 import org.openecomp.sdc.be.model.ArtifactDefinition;
67 import org.openecomp.sdc.be.model.CapabilityDefinition;
68 import org.openecomp.sdc.be.model.Component;
69 import org.openecomp.sdc.be.model.ComponentInstance;
70 import org.openecomp.sdc.be.model.ComponentInstanceInput;
71 import org.openecomp.sdc.be.model.ComponentInstanceInterface;
72 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
73 import org.openecomp.sdc.be.model.ComponentParametersView;
74 import org.openecomp.sdc.be.model.DataTypeDefinition;
75 import org.openecomp.sdc.be.model.GroupInstance;
76 import org.openecomp.sdc.be.model.InputDefinition;
77 import org.openecomp.sdc.be.model.InterfaceDefinition;
78 import org.openecomp.sdc.be.model.PropertyDefinition;
79 import org.openecomp.sdc.be.model.RelationshipInfo;
80 import org.openecomp.sdc.be.model.RequirementCapabilityRelDef;
81 import org.openecomp.sdc.be.model.RequirementDefinition;
82 import org.openecomp.sdc.be.model.Resource;
83 import org.openecomp.sdc.be.model.Service;
84 import org.openecomp.sdc.be.model.cache.ApplicationDataTypeCache;
85 import org.openecomp.sdc.be.model.category.CategoryDefinition;
86 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
87 import org.openecomp.sdc.be.model.jsonjanusgraph.utils.ModelConverter;
88 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
89 import org.openecomp.sdc.be.model.operations.impl.InterfaceLifecycleOperation;
90 import org.openecomp.sdc.be.model.tosca.converters.ToscaValueBaseConverter;
91 import org.openecomp.sdc.be.tosca.model.CapabilityFilter;
92 import org.openecomp.sdc.be.tosca.model.NodeFilter;
93 import org.openecomp.sdc.be.tosca.model.SubstitutionMapping;
94 import org.openecomp.sdc.be.tosca.model.ToscaCapability;
95 import org.openecomp.sdc.be.tosca.model.ToscaDataType;
96 import org.openecomp.sdc.be.tosca.model.ToscaGroupTemplate;
97 import org.openecomp.sdc.be.tosca.model.ToscaMetadata;
98 import org.openecomp.sdc.be.tosca.model.ToscaNodeTemplate;
99 import org.openecomp.sdc.be.tosca.model.ToscaNodeType;
100 import org.openecomp.sdc.be.tosca.model.ToscaPolicyTemplate;
101 import org.openecomp.sdc.be.tosca.model.ToscaProperty;
102 import org.openecomp.sdc.be.tosca.model.ToscaRequirement;
103 import org.openecomp.sdc.be.tosca.model.ToscaTemplate;
104 import org.openecomp.sdc.be.tosca.model.ToscaTemplateArtifact;
105 import org.openecomp.sdc.be.tosca.model.ToscaTemplateRequirement;
106 import org.openecomp.sdc.be.tosca.model.ToscaTopolgyTemplate;
107 import org.openecomp.sdc.be.tosca.utils.ForwardingPathToscaUtil;
108 import org.openecomp.sdc.be.tosca.utils.InputConverter;
109 import org.openecomp.sdc.common.log.wrappers.Logger;
110 import org.openecomp.sdc.externalupload.utils.ServiceUtils;
111 import org.springframework.beans.factory.annotation.Autowired;
112 import org.yaml.snakeyaml.DumperOptions;
113 import org.yaml.snakeyaml.DumperOptions.FlowStyle;
114 import org.yaml.snakeyaml.Yaml;
115 import org.yaml.snakeyaml.introspector.BeanAccess;
116 import org.yaml.snakeyaml.introspector.Property;
117 import org.yaml.snakeyaml.introspector.PropertyUtils;
118 import org.yaml.snakeyaml.nodes.MappingNode;
119 import org.yaml.snakeyaml.nodes.Node;
120 import org.yaml.snakeyaml.nodes.NodeTuple;
121 import org.yaml.snakeyaml.nodes.Tag;
122 import org.yaml.snakeyaml.representer.Represent;
123 import org.yaml.snakeyaml.representer.Representer;
124 import com.fasterxml.jackson.databind.ObjectMapper;
125 import com.fasterxml.jackson.databind.SerializationFeature;
126 import fj.data.Either;
128 @org.springframework.stereotype.Component("tosca-export-handler")
129 public class ToscaExportHandler {
131 private static final Logger log = Logger.getLogger(ToscaExportHandler.class);
133 private ApplicationDataTypeCache dataTypeCache;
134 private ToscaOperationFacade toscaOperationFacade;
135 private CapabilityRequirementConverter capabilityRequirementConverter;
136 private PolicyExportParser policyExportParser;
137 private GroupExportParser groupExportParser;
138 private PropertyConvertor propertyConvertor;
139 private InputConverter inputConverter;
140 private InterfaceLifecycleOperation interfaceLifecycleOperation;
141 private InterfacesOperationsConverter interfacesOperationsConverter;
144 public ToscaExportHandler(ApplicationDataTypeCache dataTypeCache, ToscaOperationFacade toscaOperationFacade,
145 CapabilityRequirementConverter capabilityRequirementConverter,
146 PolicyExportParser policyExportParser,
147 GroupExportParser groupExportParser, PropertyConvertor propertyConvertor,
148 InputConverter inputConverter,
149 InterfaceLifecycleOperation interfaceLifecycleOperation,
150 InterfacesOperationsConverter interfacesOperationsConverter) {
151 this.dataTypeCache = dataTypeCache;
152 this.toscaOperationFacade = toscaOperationFacade;
153 this.capabilityRequirementConverter = capabilityRequirementConverter;
154 this.policyExportParser = policyExportParser;
155 this.groupExportParser = groupExportParser;
156 this.propertyConvertor = propertyConvertor;
157 this.inputConverter = inputConverter;
158 this.interfaceLifecycleOperation = interfaceLifecycleOperation;
159 this.interfacesOperationsConverter = interfacesOperationsConverter;
162 private static final String TOSCA_VERSION = "tosca_simple_yaml_1_1";
163 private static final String SERVICE_NODE_TYPE_PREFIX = "org.openecomp.service.";
164 private static final String IMPORTS_FILE_KEY = "file";
165 private static final String TOSCA_INTERFACE_NAME = "-interface.yml";
166 public static final String ASSET_TOSCA_TEMPLATE = "assettoscatemplate";
167 private static final String FAILED_TO_GET_DEFAULT_IMPORTS_CONFIGURATION = "convertToToscaTemplate - failed to get Default Imports section from configuration";
168 private static final String NOT_SUPPORTED_COMPONENT_TYPE = "Not supported component type {}";
169 private static final List<Map<String, Map<String, String>>> DEFAULT_IMPORTS = ConfigurationManager
170 .getConfigurationManager().getConfiguration().getDefaultImports();
171 private static final String NATIVE_ROOT = "tosca.nodes.Root";
172 private static YamlUtil yamlUtil = new YamlUtil();
174 public ToscaExportHandler() {
177 public Either<ToscaRepresentation, ToscaError> exportComponent(Component component) {
178 return convertToToscaTemplate(component).left().map(this::createToscaRepresentation);
181 public Either<ToscaRepresentation, ToscaError> exportComponentInterface(final Component component,
182 final boolean isAssociatedComponent) {
183 if (null == DEFAULT_IMPORTS) {
184 log.debug(FAILED_TO_GET_DEFAULT_IMPORTS_CONFIGURATION);
185 return Either.right(ToscaError.GENERAL_ERROR);
188 String toscaVersion = null;
189 if (component instanceof Resource) {
190 toscaVersion = ((Resource) component).getToscaVersion();
192 ToscaTemplate toscaTemplate = new ToscaTemplate(toscaVersion != null ? toscaVersion : TOSCA_VERSION);
193 toscaTemplate.setImports(new ArrayList<>(DEFAULT_IMPORTS));
194 final Map<String, ToscaNodeType> nodeTypes = new HashMap<>();
195 final Either<ToscaTemplate, ToscaError> toscaTemplateRes =
196 convertInterfaceNodeType(new HashMap<>(), component, toscaTemplate, nodeTypes, isAssociatedComponent);
197 if (toscaTemplateRes.isRight()) {
198 return Either.right(toscaTemplateRes.right().value());
201 toscaTemplate = toscaTemplateRes.left().value();
202 ToscaRepresentation toscaRepresentation = this.createToscaRepresentation(toscaTemplate);
203 return Either.left(toscaRepresentation);
206 public ToscaRepresentation createToscaRepresentation(ToscaTemplate toscaTemplate) {
207 CustomRepresenter representer = new CustomRepresenter();
208 DumperOptions options = new DumperOptions();
209 options.setAllowReadOnlyProperties(false);
210 options.setPrettyFlow(true);
212 options.setDefaultFlowStyle(FlowStyle.FLOW);
213 options.setCanonical(false);
215 representer.addClassTag(toscaTemplate.getClass(), Tag.MAP);
217 representer.setPropertyUtils(new UnsortedPropertyUtils());
218 Yaml yaml = new Yaml(representer, options);
220 String yamlAsString = yaml.dumpAsMap(toscaTemplate);
222 StringBuilder sb = new StringBuilder();
223 sb.append(ConfigurationManager.getConfigurationManager().getConfiguration().getHeatEnvArtifactHeader());
224 sb.append(yamlAsString);
225 sb.append(ConfigurationManager.getConfigurationManager().getConfiguration().getHeatEnvArtifactFooter());
227 return ToscaRepresentation.make(sb.toString().getBytes(), toscaTemplate);
230 public Either<ToscaTemplate, ToscaError> getDependencies(Component component) {
231 ToscaTemplate toscaTemplate = new ToscaTemplate(null);
232 Either<ImmutablePair<ToscaTemplate, Map<String, Component>>, ToscaError> fillImports = fillImports(component,
234 if (fillImports.isRight()) {
235 return Either.right(fillImports.right().value());
237 return Either.left(fillImports.left().value().left);
240 public Either<ToscaTemplate, ToscaError> convertToToscaTemplate(final Component component) {
241 if (null == DEFAULT_IMPORTS) {
242 log.debug(FAILED_TO_GET_DEFAULT_IMPORTS_CONFIGURATION);
243 return Either.right(ToscaError.GENERAL_ERROR);
245 log.trace("start tosca export for {}", component.getUniqueId());
246 String toscaVersion = null;
247 if (component instanceof Resource) {
248 toscaVersion = ((Resource) component).getToscaVersion();
250 final ToscaTemplate toscaTemplate = new ToscaTemplate(toscaVersion != null ? toscaVersion : TOSCA_VERSION);
251 toscaTemplate.setMetadata(convertMetadata(component));
252 toscaTemplate.setImports(new ArrayList<>(DEFAULT_IMPORTS));
253 final Map<String, ToscaNodeType> nodeTypes = new HashMap<>();
254 if (ModelConverter.isAtomicComponent(component)) {
255 log.trace("convert component as node type");
256 return convertNodeType(new HashMap<>(), component, toscaTemplate, nodeTypes);
258 log.trace("convert component as topology template");
259 return convertToscaTemplate(component, toscaTemplate);
264 private Either<ToscaTemplate, ToscaError> convertToscaTemplate(Component component, ToscaTemplate toscaNode) {
266 Either<ImmutablePair<ToscaTemplate, Map<String, Component>>, ToscaError> importsRes = fillImports(component,
268 if (importsRes.isRight()) {
269 return Either.right(importsRes.right().value());
271 toscaNode = importsRes.left().value().left;
272 Map<String, Component> componentCache = importsRes.left().value().right;
273 Either<Map<String, ToscaNodeType>, ToscaError> nodeTypesMapEither = createProxyNodeTypes(componentCache,
275 if (nodeTypesMapEither.isRight()) {
276 log.debug("Failed to fetch normative service proxy resource by tosca name, error {}",
277 nodeTypesMapEither.right().value());
278 return Either.right(nodeTypesMapEither.right().value());
280 Map<String, ToscaNodeType> nodeTypesMap = nodeTypesMapEither.left().value();
281 if (nodeTypesMap != null && !nodeTypesMap.isEmpty()) {
282 toscaNode.setNode_types(nodeTypesMap);
285 createServiceSubstitutionNodeTypes(componentCache, component, toscaNode);
287 Either<Map<String, Object>, ToscaError> proxyInterfaceTypesEither = createProxyInterfaceTypes(component);
288 if (proxyInterfaceTypesEither.isRight()) {
289 log.debug("Failed to populate service proxy local interface types in tosca, error {}",
290 nodeTypesMapEither.right().value());
291 return Either.right(proxyInterfaceTypesEither.right().value());
293 Map<String, Object> proxyInterfaceTypes = proxyInterfaceTypesEither.left().value();
294 if (MapUtils.isNotEmpty(proxyInterfaceTypes)) {
295 toscaNode.setInterface_types(proxyInterfaceTypes);
298 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> dataTypesEither = dataTypeCache.getAll();
299 if (dataTypesEither.isRight()) {
300 log.debug("Failed to retrieve all data types {}", dataTypesEither.right().value());
301 return Either.right(ToscaError.GENERAL_ERROR);
303 Map<String, DataTypeDefinition> dataTypes = dataTypesEither.left().value();
304 ToscaTopolgyTemplate topologyTemplate = new ToscaTopolgyTemplate();
305 List<InputDefinition> inputDef = component.getInputs();
306 Map<String, ToscaProperty> inputs = inputConverter.convertInputs(inputDef, dataTypes);
308 if (!inputs.isEmpty()) {
309 topologyTemplate.setInputs(inputs);
312 final List<ComponentInstance> componentInstances = component.getComponentInstances();
313 Map<String, List<ComponentInstanceProperty>> componentInstancesProperties =
314 component.getComponentInstancesProperties();
315 Map<String, List<ComponentInstanceInterface>> componentInstanceInterfaces =
316 component.getComponentInstancesInterfaces();
317 SubstitutionMapping substitutionMapping = new SubstitutionMapping();
318 if (CollectionUtils.isNotEmpty(componentInstances)) {
319 final Either<Map<String, ToscaNodeTemplate>, ToscaError> nodeTemplates =
320 convertNodeTemplates(component, componentInstances,
321 componentInstancesProperties, componentInstanceInterfaces,
322 componentCache, dataTypes, topologyTemplate);
323 if (nodeTemplates.isRight()) {
324 return Either.right(nodeTemplates.right().value());
326 log.debug("node templates converted");
327 topologyTemplate.setNode_templates(nodeTemplates.left().value());
329 convertSubstitutionMappingFilter(componentInstances, substitutionMapping);
332 addGroupsToTopologyTemplate(component, topologyTemplate);
335 addPoliciesToTopologyTemplate(component, topologyTemplate);
336 } catch (SdcResourceNotFoundException e) {
337 log.debug("Fail to add policies to topology template:", e);
338 return Either.right(ToscaError.GENERAL_ERROR);
341 String toscaResourceName;
342 switch (component.getComponentType()) {
344 toscaResourceName = ((ResourceMetadataDataDefinition) component.getComponentMetadataDefinition()
345 .getMetadataDataDefinition()).getToscaResourceName();
348 toscaResourceName = SERVICE_NODE_TYPE_PREFIX
349 + component.getComponentMetadataDefinition().getMetadataDataDefinition().getSystemName();
352 log.debug(NOT_SUPPORTED_COMPONENT_TYPE, component.getComponentType());
353 return Either.right(ToscaError.NOT_SUPPORTED_TOSCA_TYPE);
355 substitutionMapping.setNode_type(toscaResourceName);
356 Either<SubstitutionMapping, ToscaError> capabilities = convertCapabilities(component, substitutionMapping,
358 if (capabilities.isRight()) {
359 return Either.right(capabilities.right().value());
361 substitutionMapping = capabilities.left().value();
363 Either<SubstitutionMapping, ToscaError> requirements = capabilityRequirementConverter
364 .convertSubstitutionMappingRequirements(componentCache, component, substitutionMapping);
365 if (requirements.isRight()) {
366 return Either.right(requirements.right().value());
368 substitutionMapping = requirements.left().value();
370 final Optional<Map<String, ToscaProperty>> proxyInputProperties = getProxyNodeTypeInputProperties(component, dataTypes);
371 proxyInputProperties.ifPresent(substitutionMapping::setProperties);
373 topologyTemplate.setSubstitution_mappings(substitutionMapping);
375 toscaNode.setTopology_template(topologyTemplate);
377 return Either.left(toscaNode);
380 private void convertSubstitutionMappingFilter(final List<ComponentInstance> componentInstances,
381 final SubstitutionMapping substitutionMapping) {
382 componentInstances.stream()
383 .filter(componentInstance -> hasSubstitutionFilterDataDefinition(componentInstance.getSubstitutionFilter()))
384 .forEach(componentInstance -> substitutionMapping
385 .setSubstitution_filter(convertToSubstitutionFilterComponent(componentInstance.getSubstitutionFilter())));
388 private boolean hasSubstitutionFilterDataDefinition(
389 final SubstitutionFilterDataDefinition substitutionFilterDataDefinition) {
391 return substitutionFilterDataDefinition != null && substitutionFilterDataDefinition.getProperties() != null
392 && CollectionUtils.isNotEmpty(substitutionFilterDataDefinition.getProperties().getListToscaDataDefinition());
395 private void addGroupsToTopologyTemplate(Component component, ToscaTopolgyTemplate topologyTemplate) {
396 Map<String, ToscaGroupTemplate> groups = groupExportParser.getGroups(component);
397 if (groups != null) {
398 topologyTemplate.addGroups(groups);
402 private void addPoliciesToTopologyTemplate(Component component, ToscaTopolgyTemplate topologyTemplate)
403 throws SdcResourceNotFoundException {
404 Map<String, ToscaPolicyTemplate> policies = policyExportParser.getPolicies(component);
405 if (policies != null) {
406 topologyTemplate.addPolicies(policies);
410 private ToscaMetadata convertMetadata(Component component) {
411 return convertMetadata(component, false, null);
414 private ToscaMetadata convertMetadata(Component component, boolean isInstance,
415 ComponentInstance componentInstance) {
416 ToscaMetadata toscaMetadata = new ToscaMetadata();
417 toscaMetadata.setInvariantUUID(component.getInvariantUUID());
418 toscaMetadata.setUUID(component.getUUID());
419 toscaMetadata.setDescription(component.getDescription());
420 toscaMetadata.setName(component.getComponentMetadataDefinition().getMetadataDataDefinition().getName());
422 List<CategoryDefinition> categories = component.getCategories();
423 CategoryDefinition categoryDefinition = categories.get(0);
424 toscaMetadata.setCategory(categoryDefinition.getName());
427 toscaMetadata.setVersion(component.getVersion());
428 toscaMetadata.setCustomizationUUID(componentInstance.getCustomizationUUID());
429 if (componentInstance.getSourceModelInvariant() != null
430 && !componentInstance.getSourceModelInvariant().isEmpty()) {
431 toscaMetadata.setVersion(componentInstance.getComponentVersion());
432 toscaMetadata.setSourceModelInvariant(componentInstance.getSourceModelInvariant());
433 toscaMetadata.setSourceModelUuid(componentInstance.getSourceModelUuid());
434 toscaMetadata.setSourceModelName(componentInstance.getSourceModelName());
435 if (componentInstance.getOriginType() == OriginTypeEnum.ServiceProxy) {
436 toscaMetadata.setName(componentInstance.getSourceModelName() + " " + OriginTypeEnum.ServiceProxy.getDisplayValue());
437 } else if (componentInstance.getOriginType() == OriginTypeEnum.ServiceSubstitution) {
438 toscaMetadata.setName(componentInstance.getSourceModelName() + " " + OriginTypeEnum.ServiceSubstitution.getDisplayValue());
440 toscaMetadata.setDescription(componentInstance.getDescription());
444 switch (component.getComponentType()) {
446 Resource resource = (Resource) component;
448 if (isInstance && (componentInstance.getOriginType() == OriginTypeEnum.ServiceProxy || componentInstance.getOriginType() == OriginTypeEnum.ServiceSubstitution)) {
449 toscaMetadata.setType(componentInstance.getOriginType().getDisplayValue());
451 toscaMetadata.setType(resource.getResourceType().name());
453 toscaMetadata.setSubcategory(categoryDefinition.getSubcategories().get(0).getName());
454 toscaMetadata.setResourceVendor(resource.getVendorName());
455 toscaMetadata.setResourceVendorRelease(resource.getVendorRelease());
456 toscaMetadata.setResourceVendorModelNumber(resource.getResourceVendorModelNumber());
459 Service service = (Service) component;
460 toscaMetadata.setType(component.getComponentType().getValue());
461 toscaMetadata.setServiceType(service.getServiceType());
462 toscaMetadata.setServiceRole(service.getServiceRole());
463 toscaMetadata.setServiceFunction(service.getServiceFunction());
464 toscaMetadata.setEnvironmentContext(service.getEnvironmentContext());
465 resolveInstantiationTypeAndSetItToToscaMetaData(toscaMetadata, service);
468 toscaMetadata.setServiceEcompNaming(((Service) component).isEcompGeneratedNaming());
469 toscaMetadata.setEcompGeneratedNaming(((Service) component).isEcompGeneratedNaming());
470 toscaMetadata.setNamingPolicy(((Service) component).getNamingPolicy());
474 log.debug(NOT_SUPPORTED_COMPONENT_TYPE, component.getComponentType());
476 return toscaMetadata;
479 private void resolveInstantiationTypeAndSetItToToscaMetaData(ToscaMetadata toscaMetadata, Service service) {
480 if (service.getInstantiationType() != null) {
481 toscaMetadata.setInstantiationType(service.getInstantiationType());
483 toscaMetadata.setInstantiationType(StringUtils.EMPTY);
487 private Either<ImmutablePair<ToscaTemplate, Map<String, Component>>, ToscaError> fillImports(Component component,
488 ToscaTemplate toscaTemplate) {
490 if (null == DEFAULT_IMPORTS) {
491 log.debug(FAILED_TO_GET_DEFAULT_IMPORTS_CONFIGURATION);
492 return Either.right(ToscaError.GENERAL_ERROR);
494 Map<String, Component> componentCache = new HashMap<>();
496 if (!ModelConverter.isAtomicComponent(component)) {
497 List<Map<String, Map<String, String>>> additionalImports = toscaTemplate.getImports() == null
498 ? new ArrayList<>(DEFAULT_IMPORTS) : new ArrayList<>(toscaTemplate.getImports());
500 List<Triple<String, String, Component>> dependecies = new ArrayList<>();
502 Map<String, ArtifactDefinition> toscaArtifacts = component.getToscaArtifacts();
503 if (isNotEmpty(toscaArtifacts)) {
504 ArtifactDefinition artifactDefinition = toscaArtifacts.get(ToscaExportHandler.ASSET_TOSCA_TEMPLATE);
505 if (artifactDefinition != null) {
506 Map<String, Map<String, String>> importsListMember = new HashMap<>();
507 Map<String, String> interfaceFiles = new HashMap<>();
508 interfaceFiles.put(IMPORTS_FILE_KEY, getInterfaceFilename(artifactDefinition.getArtifactName()));
509 StringBuilder keyNameBuilder = new StringBuilder();
510 keyNameBuilder.append(component.getComponentType().toString().toLowerCase()).append("-")
511 .append(component.getName()).append("-interface");
512 importsListMember.put(keyNameBuilder.toString(), interfaceFiles);
513 additionalImports.add(importsListMember);
516 List<ComponentInstance> componentInstances = component.getComponentInstances();
517 if (componentInstances != null && !componentInstances.isEmpty()) {
518 componentInstances.forEach(ci -> createDependency(componentCache, additionalImports, dependecies, ci));
520 toscaTemplate.setDependencies(dependecies);
521 toscaTemplate.setImports(additionalImports);
523 log.debug("currently imports supported for VF and service only");
525 return Either.left(new ImmutablePair<>(toscaTemplate, componentCache));
528 private void createDependency(final Map<String, Component> componentCache,
529 final List<Map<String, Map<String, String>>> imports,
530 final List<Triple<String, String, Component>> dependencies,
531 final ComponentInstance componentInstance) {
532 log.debug("createDependency componentCache {}", componentCache);
533 final Component componentRI = componentCache.get(componentInstance.getComponentUid());
534 if (componentRI == null) {
535 // all resource must be only once!
536 final Either<Component, StorageOperationStatus> resource = toscaOperationFacade
537 .getToscaFullElement(componentInstance.getComponentUid());
538 if ((resource.isRight()) && (log.isDebugEnabled())) {
539 log.debug("Failed to fetch resource with id {} for instance {}", componentInstance.getComponentUid(),
540 componentInstance.getUniqueId());
543 final Component fetchedComponent = resource.left().value();
544 setComponentCache(componentCache, componentInstance, fetchedComponent);
545 addDependencies(imports, dependencies, fetchedComponent);
550 * Sets a componentCache from the given component/resource.
552 private void setComponentCache(final Map<String, Component> componentCache,
553 final ComponentInstance componentInstance,
554 final Component fetchedComponent) {
555 componentCache.put(fetchedComponent.getUniqueId(), fetchedComponent);
556 if (componentInstance.getOriginType() == OriginTypeEnum.ServiceProxy || componentInstance.getOriginType() == OriginTypeEnum.ServiceSubstitution) {
557 final Either<Component, StorageOperationStatus> sourceService = toscaOperationFacade
558 .getToscaFullElement(componentInstance.getSourceModelUid());
559 if (sourceService.isRight() && (log.isDebugEnabled())) {
560 log.debug("Failed to fetch source service with id {} for proxy {}",
561 componentInstance.getSourceModelUid(), componentInstance.getUniqueId());
563 final Component fetchedSource = sourceService.left().value();
564 componentCache.put(fetchedSource.getUniqueId(), fetchedSource);
569 * Retrieves all derived_from nodes and stores it in a predictable order.
571 private void addDependencies(final List<Map<String, Map<String, String>>> imports,
572 final List<Triple<String, String, Component>> dependencies,
573 final Component fetchedComponent) {
574 final Set<Component> componentsList = new LinkedHashSet<>();
575 if (fetchedComponent instanceof Resource) {
576 log.debug("fetchedComponent is a resource {}", fetchedComponent);
578 final Optional<Map<String, String>> derivedFromMapOfIdToName = getDerivedFromMapOfIdToName(fetchedComponent,
580 if (derivedFromMapOfIdToName.isPresent()) {
581 derivedFromMapOfIdToName.get().entrySet().forEach(entry -> {
582 log.debug("Started entry.getValue() : {}", entry.getValue());
583 if (!NATIVE_ROOT.equals(entry.getValue())) {
584 Either<Resource, StorageOperationStatus> resourcefetched = toscaOperationFacade
585 .getToscaElement(entry.getKey());
586 if (resourcefetched != null && resourcefetched.isLeft()) {
587 componentsList.add(resourcefetched.left().value());
592 setImports(imports, dependencies, componentsList);
597 * Returns all derived_from nodes found.
599 private Optional<Map<String, String>> getDerivedFromMapOfIdToName(final Component fetchedComponent,
600 final Set<Component> componentsList) {
601 final Resource parentResource = (Resource) fetchedComponent;
602 Map<String, String> derivedFromMapOfIdToName = new HashMap<>();
603 if (CollectionUtils.isNotEmpty(parentResource.getComponentInstances())) {
604 componentsList.add(fetchedComponent);
605 for (final ComponentInstance componentInstance : parentResource.getComponentInstances()) {
606 final Either<Resource, StorageOperationStatus> resourcefetched = toscaOperationFacade
607 .getToscaElement(componentInstance.getComponentUid());
608 if (resourcefetched != null && resourcefetched.isLeft()) {
609 final Map<String, String> derivedWithId = resourcefetched.left().value()
610 .getDerivedFromMapOfIdToName();
611 if (MapUtils.isNotEmpty(derivedWithId)) {
612 derivedFromMapOfIdToName.putAll(derivedWithId);
617 derivedFromMapOfIdToName = parentResource.getDerivedFromMapOfIdToName();
619 log.debug("Started derivedFromMapOfIdToName: {}", derivedFromMapOfIdToName);
620 return Optional.ofNullable(derivedFromMapOfIdToName);
624 * Creates a resource map and adds it to the import list.
626 private void setImports(final List<Map<String, Map<String, String>>> imports,
627 final List<Triple<String, String, Component>> dependencies,
628 final Set<Component> componentsList) {
629 componentsList.forEach(component -> {
630 final Map<String, ArtifactDefinition> toscaArtifacts = component.getToscaArtifacts();
631 final ArtifactDefinition artifactDefinition = toscaArtifacts.get(ASSET_TOSCA_TEMPLATE);
632 if (artifactDefinition != null) {
633 final Map<String, String> files = new HashMap<>();
634 final String artifactName = artifactDefinition.getArtifactName();
635 files.put(IMPORTS_FILE_KEY, artifactName);
636 final StringBuilder keyNameBuilder = new StringBuilder();
637 keyNameBuilder.append(component.getComponentType().toString().toLowerCase());
638 keyNameBuilder.append("-");
639 keyNameBuilder.append(component.getName());
640 addImports(imports, keyNameBuilder, files);
642 .add(new ImmutableTriple<String, String, Component>(artifactName, artifactDefinition.getEsId(),
645 if (!ModelConverter.isAtomicComponent(component)) {
646 final Map<String, String> interfaceFiles = new HashMap<>();
647 interfaceFiles.put(IMPORTS_FILE_KEY, getInterfaceFilename(artifactName));
648 keyNameBuilder.append("-interface");
649 addImports(imports, keyNameBuilder, interfaceFiles);
656 * Adds the found resource to the import definition list.
658 private void addImports(final List<Map<String, Map<String, String>>> imports,
659 final StringBuilder keyNameBuilder,
660 final Map<String, String> files) {
661 final String mapKey = keyNameBuilder.toString();
662 if (imports.stream().allMatch(stringMapMap -> stringMapMap.get(mapKey) == null)) {
663 final Map<String, Map<String, String>> importsListMember = new HashMap<>();
664 importsListMember.put(keyNameBuilder.toString(), files);
665 imports.add(importsListMember);
669 public static String getInterfaceFilename(String artifactName) {
670 return artifactName.substring(0, artifactName.lastIndexOf('.')) + ToscaExportHandler.TOSCA_INTERFACE_NAME;
673 private Either<ToscaTemplate, ToscaError> convertNodeType(Map<String, Component> componentsCache,
674 Component component, ToscaTemplate toscaNode,
675 Map<String, ToscaNodeType> nodeTypes) {
676 return convertInterfaceNodeType(componentsCache, component, toscaNode, nodeTypes, false);
679 public Either<ToscaTemplate, ToscaError> convertInterfaceNodeType(Map<String, Component> componentsCache,
680 Component component, ToscaTemplate toscaNode,
681 Map<String, ToscaNodeType> nodeTypes,
682 boolean isAssociatedComponent) {
683 log.debug("start convert node type for {}", component.getUniqueId());
684 ToscaNodeType toscaNodeType = createNodeType(component);
686 Either<Map<String, InterfaceDefinition>, StorageOperationStatus> lifecycleTypeEither =
687 interfaceLifecycleOperation.getAllInterfaceLifecycleTypes();
688 if (lifecycleTypeEither.isRight()) {
689 log.debug("Failed to fetch all interface types :", lifecycleTypeEither.right().value());
690 return Either.right(ToscaError.GENERAL_ERROR);
692 List<String> allGlobalInterfaceTypes = lifecycleTypeEither.left().value()
695 .map(InterfaceDataDefinition::getType)
696 .collect(Collectors.toList());
697 toscaNode.setInterface_types(addInterfaceTypeElement(component, allGlobalInterfaceTypes));
699 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> dataTypesEither = dataTypeCache.getAll();
700 if (dataTypesEither.isRight()) {
701 log.debug("Failed to fetch all data types :", dataTypesEither.right().value());
702 return Either.right(ToscaError.GENERAL_ERROR);
705 Map<String, DataTypeDefinition> dataTypes = dataTypesEither.left().value();
707 List<InputDefinition> inputDef = component.getInputs();
708 Map<String, ToscaProperty> mergedProperties = new HashMap<>();
709 interfacesOperationsConverter
710 .addInterfaceDefinitionElement(component, toscaNodeType, dataTypes, isAssociatedComponent);
711 addInputsToProperties(dataTypes, inputDef, mergedProperties);
713 if (CollectionUtils.isNotEmpty(component.getProperties())) {
714 List<PropertyDefinition> properties = component.getProperties();
715 Map<String, ToscaProperty> convertedProperties = properties.stream()
716 .map(propertyDefinition -> resolvePropertyValueFromInput(propertyDefinition, component.getInputs()))
717 .collect(Collectors.toMap(PropertyDataDefinition::getName,
718 property -> propertyConvertor.convertProperty(dataTypes, property,
719 PropertyConvertor.PropertyType.PROPERTY)));
720 // merge component properties and inputs properties
721 mergedProperties.putAll(convertedProperties);
723 if (MapUtils.isNotEmpty(mergedProperties)) {
724 toscaNodeType.setProperties(mergedProperties);
727 /* convert private data_types */
728 List<DataTypeDefinition> privateDataTypes = component.getDataTypes();
729 if (CollectionUtils.isNotEmpty(privateDataTypes)) {
730 Map<String, ToscaDataType> toscaDataTypeMap = new HashMap<>();
731 for (DataTypeDefinition dataType : privateDataTypes) {
732 log.debug("Emitting private data type: component.name={} dataType.name={}",
733 component.getNormalizedName(), dataType.getName());
734 ToscaDataType toscaDataType = new ToscaDataType();
735 toscaDataType.setDerived_from(dataType.getDerivedFromName());
736 toscaDataType.setDescription(dataType.getDescription());
737 toscaDataType.setVersion(dataType.getVersion());
738 if (CollectionUtils.isNotEmpty(dataType.getProperties())) {
739 toscaDataType.setProperties(dataType.getProperties().stream()
740 .collect(Collectors.toMap(
742 s -> propertyConvertor
743 .convertProperty(dataTypes, s, PropertyConvertor.PropertyType.PROPERTY)
746 toscaDataTypeMap.put(dataType.getName(), toscaDataType);
748 toscaNode.setData_types(toscaDataTypeMap);
751 // Extracted to method for code reuse
752 return convertReqCapAndTypeName(componentsCache, component, toscaNode, nodeTypes, toscaNodeType, dataTypes);
755 private Either<ToscaTemplate, ToscaError> convertReqCapAndTypeName(Map<String, Component> componentsCache,
756 Component component, ToscaTemplate toscaNode,
757 Map<String, ToscaNodeType> nodeTypes,
758 ToscaNodeType toscaNodeType,
759 Map<String, DataTypeDefinition> dataTypes) {
760 Either<ToscaNodeType, ToscaError> capabilities = convertCapabilities(componentsCache, component, toscaNodeType,
762 if (capabilities.isRight()) {
763 return Either.right(capabilities.right().value());
765 toscaNodeType = capabilities.left().value();
766 log.debug("Capabilities converted for {}", component.getUniqueId());
768 Either<ToscaNodeType, ToscaError> requirements = capabilityRequirementConverter
769 .convertRequirements(componentsCache, component,
771 if (requirements.isRight()) {
772 return Either.right(requirements.right().value());
774 toscaNodeType = requirements.left().value();
775 log.debug("Requirements converted for {}", component.getUniqueId());
777 String toscaResourceName;
778 switch (component.getComponentType()) {
780 toscaResourceName = ((ResourceMetadataDataDefinition) component.getComponentMetadataDefinition()
781 .getMetadataDataDefinition()).getToscaResourceName();
784 toscaResourceName = SERVICE_NODE_TYPE_PREFIX
785 + component.getComponentMetadataDefinition().getMetadataDataDefinition().getSystemName();
788 log.debug(NOT_SUPPORTED_COMPONENT_TYPE, component.getComponentType());
789 return Either.right(ToscaError.NOT_SUPPORTED_TOSCA_TYPE);
792 nodeTypes.put(toscaResourceName, toscaNodeType);
793 toscaNode.setNode_types(nodeTypes);
794 log.debug("finish convert node type for {}", component.getUniqueId());
795 return Either.left(toscaNode);
798 protected Either<Map<String, ToscaNodeTemplate>, ToscaError> convertNodeTemplates(
800 List<ComponentInstance> componentInstances,
801 Map<String, List<ComponentInstanceProperty>> componentInstancesProperties,
802 Map<String, List<ComponentInstanceInterface>> componentInstanceInterfaces,
803 Map<String, Component> componentCache, Map<String, DataTypeDefinition> dataTypes,
804 ToscaTopolgyTemplate topologyTemplate) {
806 Either<Map<String, ToscaNodeTemplate>, ToscaError> convertNodeTemplatesRes = null;
807 log.debug("start convert topology template for {} for type {}", component.getUniqueId(),
808 component.getComponentType());
809 Map<String, ToscaNodeTemplate> nodeTemplates = new HashMap<>();
810 Map<String, List<ComponentInstanceInput>> componentInstancesInputs = component.getComponentInstancesInputs();
812 Map<String, ToscaGroupTemplate> groupsMap = null;
813 for (ComponentInstance componentInstance : componentInstances) {
814 ToscaNodeTemplate nodeTemplate = new ToscaNodeTemplate();
815 if (MapUtils.isNotEmpty(componentInstance.getToscaArtifacts())) {
816 nodeTemplate.setArtifacts(convertToNodeTemplateArtifacts(componentInstance.getToscaArtifacts()));
818 nodeTemplate.setType(componentInstance.getToscaComponentName());
819 nodeTemplate.setDirectives(componentInstance.getDirectives());
820 nodeTemplate.setNode_filter(convertToNodeTemplateNodeFilterComponent(componentInstance.getNodeFilter()));
822 Either<Component, Boolean> originComponentRes = capabilityRequirementConverter
823 .getOriginComponent(componentCache, componentInstance);
824 if (originComponentRes.isRight()) {
825 convertNodeTemplatesRes = Either.right(ToscaError.NODE_TYPE_REQUIREMENT_ERROR);
828 Either<ToscaNodeTemplate, ToscaError> requirements = convertComponentInstanceRequirements(component,
829 componentInstance, component.getComponentInstancesRelations(), nodeTemplate,
830 originComponentRes.left().value(), componentCache);
831 if (requirements.isRight()) {
832 convertNodeTemplatesRes = Either.right(requirements.right().value());
835 String instanceUniqueId = componentInstance.getUniqueId();
836 log.debug("Component instance Requirements converted for instance {}", instanceUniqueId);
838 nodeTemplate = requirements.left().value();
840 Component originalComponent = componentCache.get(componentInstance.getActualComponentUid());
842 if (componentInstance.getOriginType() == OriginTypeEnum.ServiceProxy) {
843 Component componentOfProxy = componentCache.get(componentInstance.getComponentUid());
844 nodeTemplate.setMetadata(convertMetadata(componentOfProxy, true, componentInstance));
846 nodeTemplate.setMetadata(convertMetadata(originalComponent, true, componentInstance));
849 Either<ToscaNodeTemplate, ToscaError> capabilities = capabilityRequirementConverter
850 .convertComponentInstanceCapabilities(componentInstance, dataTypes, nodeTemplate);
851 if (capabilities.isRight()) {
852 convertNodeTemplatesRes = Either.right(capabilities.right().value());
855 log.debug("Component instance Capabilities converted for instance {}", instanceUniqueId);
857 nodeTemplate = capabilities.left().value();
858 Map<String, Object> props = new HashMap<>();
860 if (originalComponent.getComponentType() == ComponentTypeEnum.RESOURCE) {
861 // Adds the properties of parent component to map
862 addPropertiesOfParentComponent(dataTypes, originalComponent, props);
865 if (null != componentInstancesProperties && componentInstancesProperties.containsKey(instanceUniqueId)) {
866 addPropertiesOfComponentInstance(componentInstancesProperties, dataTypes, instanceUniqueId,
870 if (componentInstancesInputs != null && componentInstancesInputs.containsKey(instanceUniqueId)
871 && !isComponentOfTypeServiceProxy(componentInstance)) {
872 //For service proxy the inputs are already handled under instance properties above
873 addComponentInstanceInputs(dataTypes, componentInstancesInputs, instanceUniqueId,
876 //M3[00001] - NODE TEMPLATE INTERFACES - START
877 handleInstanceInterfaces(componentInstanceInterfaces, componentInstance, dataTypes, nodeTemplate,
878 instanceUniqueId, component);
879 //M3[00001] - NODE TEMPLATE INTERFACES - END
880 if (props != null && !props.isEmpty()) {
881 nodeTemplate.setProperties(props);
884 List<GroupInstance> groupInstances = componentInstance.getGroupInstances();
885 if (groupInstances != null) {
886 if (groupsMap == null) {
887 groupsMap = new HashMap<>();
889 for (GroupInstance groupInst : groupInstances) {
890 boolean addToTosca = true;
892 List<String> artifacts = groupInst.getArtifacts();
893 if (artifacts == null || artifacts.isEmpty()) {
898 ToscaGroupTemplate toscaGroup = groupExportParser
899 .getToscaGroupTemplate(groupInst, componentInstance.getInvariantName());
900 groupsMap.put(groupInst.getName(), toscaGroup);
905 nodeTemplates.put(componentInstance.getName(), nodeTemplate);
907 if (groupsMap != null) {
908 log.debug("instance groups added");
909 topologyTemplate.addGroups(groupsMap);
911 if (component.getComponentType() == ComponentTypeEnum.SERVICE && isNotEmpty(
912 ((Service) component).getForwardingPaths())) {
913 log.debug("Starting converting paths for component {}, name {}", component.getUniqueId(),
914 component.getName());
915 ForwardingPathToscaUtil
916 .addForwardingPaths((Service) component, nodeTemplates, capabilityRequirementConverter, componentCache,
917 toscaOperationFacade);
918 log.debug("Finished converting paths for component {}, name {}", component.getUniqueId(),
919 component.getName());
921 if (convertNodeTemplatesRes == null) {
922 convertNodeTemplatesRes = Either.left(nodeTemplates);
924 log.debug("finish convert topology template for {} for type {}", component.getUniqueId(),
925 component.getComponentType());
926 return convertNodeTemplatesRes;
929 private void handleInstanceInterfaces(
930 Map<String, List<ComponentInstanceInterface>> componentInstanceInterfaces,
931 ComponentInstance componentInstance, Map<String, DataTypeDefinition> dataTypes, ToscaNodeTemplate nodeTemplate,
932 String instanceUniqueId,
933 Component parentComponent) {
935 Map<String, Object> interfaces;
937 // we need to handle service proxy interfaces
938 if (isComponentOfTypeServiceProxy(componentInstance)) {
939 if (MapUtils.isEmpty(componentInstanceInterfaces)
940 || !componentInstanceInterfaces.containsKey(instanceUniqueId)) {
943 List<ComponentInstanceInterface> currServiceInterfaces =
944 componentInstanceInterfaces.get(instanceUniqueId);
946 Map<String, InterfaceDefinition> tmpInterfaces = new HashMap<>();
947 currServiceInterfaces.forEach(instInterface -> tmpInterfaces.put(instInterface
948 .getUniqueId(), instInterface));
950 interfaces = interfacesOperationsConverter
951 .getInterfacesMap(parentComponent, componentInstance, tmpInterfaces, dataTypes, true, true);
955 getComponentInstanceInterfaceInstances(componentInstanceInterfaces,
956 componentInstance, instanceUniqueId);
958 nodeTemplate.setInterfaces(interfaces);
961 private boolean isComponentOfTypeServiceProxy(ComponentInstance componentInstance) {
962 return Objects.nonNull(componentInstance.getOriginType())
963 && componentInstance.getOriginType().getValue().equals("Service Proxy");
966 //M3[00001] - NODE TEMPLATE INTERFACES - START
967 private Map<String, Object> getComponentInstanceInterfaceInstances(
968 Map<String, List<ComponentInstanceInterface>> componentInstancesInterfaces,
969 ComponentInstance componentInstance,
970 String instanceUniqueId) {
971 if (MapUtils.isEmpty(componentInstancesInterfaces)) {
975 List<ComponentInstanceInterface> componentInstanceInterfaces =
976 componentInstancesInterfaces.get(instanceUniqueId);
978 if (CollectionUtils.isEmpty(componentInstanceInterfaces)) {
982 Map<String, Object> interfaces = new HashMap<>();
983 for (ComponentInstanceInterface componentInstanceInterface : componentInstanceInterfaces) {
984 interfaces.put(componentInstanceInterface.getInterfaceId(),
985 removeOperationsKeyFromInterface(componentInstanceInterface.getInterfaceInstanceDataDefinition()));
988 componentInstance.setInterfaces(interfaces);
993 private void addComponentInstanceInputs(Map<String, DataTypeDefinition> dataTypes,
994 Map<String, List<ComponentInstanceInput>> componentInstancesInputs,
995 String instanceUniqueId, Map<String, Object> props) {
997 List<ComponentInstanceInput> instanceInputsList = componentInstancesInputs.get(instanceUniqueId);
998 if (instanceInputsList != null) {
999 instanceInputsList.forEach(input -> {
1001 Supplier<String> supplier = () -> input.getValue() != null && !Objects.isNull(input.getValue())
1002 ? input.getValue() : input.getDefaultValue();
1003 propertyConvertor.convertAndAddValue(dataTypes, props, input, supplier);
1008 private void addPropertiesOfComponentInstance(
1009 Map<String, List<ComponentInstanceProperty>> componentInstancesProperties,
1010 Map<String, DataTypeDefinition> dataTypes, String instanceUniqueId,
1011 Map<String, Object> props) {
1013 if (isNotEmpty(componentInstancesProperties)) {
1014 componentInstancesProperties.get(instanceUniqueId)
1015 // Converts and adds each value to property map
1016 .forEach(prop -> propertyConvertor.convertAndAddValue(dataTypes, props, prop,
1021 private void addPropertiesOfParentComponent(Map<String, DataTypeDefinition> dataTypes,
1022 Component componentOfInstance, Map<String, Object> props) {
1024 List<PropertyDefinition> componentProperties = componentOfInstance.getProperties();
1025 if (isNotEmpty(componentProperties)) {
1026 componentProperties.stream()
1027 // Filters out properties with empty default values
1028 .filter(prop -> StringUtils.isNotEmpty(prop.getDefaultValue()))
1029 // Converts and adds each value to property map
1030 .forEach(prop -> propertyConvertor.convertAndAddValue(dataTypes, props, prop,
1031 prop::getDefaultValue));
1038 private void convertAndAddValue(Map<String, DataTypeDefinition> dataTypes, ComponentInstance componentInstance,
1039 Map<String, Object> props, PropertyDefinition prop, Supplier<String> supplier) {
1040 Object convertedValue = convertValue(dataTypes, componentInstance, prop, supplier);
1041 if (!ToscaValueBaseConverter.isEmptyObjectValue(convertedValue)) {
1042 props.put(prop.getName(), convertedValue);
1046 private <T extends PropertyDefinition> Object convertValue(Map<String, DataTypeDefinition> dataTypes,
1047 ComponentInstance componentInstance, T input,
1048 Supplier<String> supplier) {
1049 log.debug("Convert property or input value {} for instance {}", input.getName(),
1050 componentInstance.getUniqueId());
1051 String propertyType = input.getType();
1052 String innerType = null;
1053 if (input.getSchema() != null && input.getSchema().getProperty() != null) {
1054 innerType = input.getSchema().getProperty().getType();
1056 return propertyConvertor.convertToToscaObject(input, supplier.get(), dataTypes, true);
1059 private ToscaNodeType createNodeType(Component component) {
1060 ToscaNodeType toscaNodeType = new ToscaNodeType();
1061 if (ModelConverter.isAtomicComponent(component)) {
1062 if (((Resource) component).getDerivedFrom() != null) {
1063 toscaNodeType.setDerived_from(((Resource) component).getDerivedFrom().get(0));
1065 toscaNodeType.setDescription(component.getDescription());
1067 String derivedFrom = null != component.getDerivedFromGenericType() ? component.getDerivedFromGenericType()
1069 toscaNodeType.setDerived_from(derivedFrom);
1071 if (component instanceof Resource) {
1072 final List<AttributeDataDefinition> attributes = ((Resource) component).getAttributes();
1073 if (CollectionUtils.isNotEmpty(attributes)) {
1074 final Map<String, AttributeDataDefinition> attributeDataDefinitionMap
1075 = attributes.stream().collect(Collectors.toMap(AttributeDataDefinition::getName, a -> a));
1076 toscaNodeType.setAttributes(attributeDataDefinitionMap);
1079 return toscaNodeType;
1082 private Either<Map<String, Object>, ToscaError> createProxyInterfaceTypes(Component container) {
1084 Map<String, Object> proxyInterfaceTypes = new HashMap<>();
1085 Either<Map<String, Object>, ToscaError> res = Either.left(proxyInterfaceTypes);
1086 List<ComponentInstance> componentInstances = container.getComponentInstances();
1087 if (CollectionUtils.isEmpty(componentInstances)) {
1090 Map<String, ComponentInstance> serviceProxyInstanceList = new HashMap<>();
1091 componentInstances.stream()
1092 .filter(this::isComponentOfTypeServiceProxy)
1093 .forEach(inst -> serviceProxyInstanceList.put(inst.getToscaComponentName(), inst));
1094 if (MapUtils.isEmpty(serviceProxyInstanceList)) {
1097 for (Entry<String, ComponentInstance> entryProxy : serviceProxyInstanceList.entrySet()) {
1098 Component serviceComponent;
1099 ComponentParametersView componentParametersView = new ComponentParametersView();
1100 componentParametersView.disableAll();
1101 componentParametersView.setIgnoreInterfaces(false);
1102 Either<Component, StorageOperationStatus> service = toscaOperationFacade
1103 .getToscaElement(entryProxy.getValue().getSourceModelUid(), componentParametersView);
1104 if (service.isRight()) {
1105 log.debug("Failed to fetch original service component with id {} for instance {}",
1106 entryProxy.getValue().getSourceModelUid(), entryProxy.getValue().getName());
1107 return Either.right(ToscaError.GENERAL_ERROR);
1109 serviceComponent = service.left().value();
1112 Either<Map<String, InterfaceDefinition>, StorageOperationStatus> lifecycleTypeEither =
1113 interfaceLifecycleOperation.getAllInterfaceLifecycleTypes();
1114 if (lifecycleTypeEither.isRight()) {
1115 log.debug("Failed to retrieve global interface types :", lifecycleTypeEither.right().value());
1116 return Either.right(ToscaError.GENERAL_ERROR);
1119 List<String> allGlobalInterfaceTypes = lifecycleTypeEither.left().value().values().stream()
1120 .map(InterfaceDataDefinition::getType)
1121 .collect(Collectors.toList());
1122 //Add interface types for local interfaces in the original service component for proxy
1123 Map<String, Object> localInterfaceTypes = addInterfaceTypeElement(serviceComponent,
1124 allGlobalInterfaceTypes);
1125 if (MapUtils.isNotEmpty(localInterfaceTypes)) {
1126 proxyInterfaceTypes.putAll(localInterfaceTypes);
1130 return Either.left(proxyInterfaceTypes);
1133 private Either<Map<String, ToscaNodeType>, ToscaError> createProxyNodeTypes(Map<String, Component> componentCache,
1134 Component container) {
1136 Map<String, ToscaNodeType> nodeTypesMap = new HashMap<>();
1137 Either<Map<String, ToscaNodeType>, ToscaError> res = Either.left(nodeTypesMap);
1139 List<ComponentInstance> componentInstances = container.getComponentInstances();
1141 if (componentInstances == null || componentInstances.isEmpty()) {
1144 Map<String, ComponentInstance> serviceProxyInstanceList = new HashMap<>();
1145 List<ComponentInstance> proxyInst = componentInstances.stream()
1146 .filter(p -> p.getOriginType().name().equals(OriginTypeEnum.ServiceProxy.name()))
1147 .collect(Collectors.toList());
1148 if (proxyInst != null && !proxyInst.isEmpty()) {
1149 for (ComponentInstance inst : proxyInst) {
1150 serviceProxyInstanceList.put(inst.getToscaComponentName(), inst);
1154 if (serviceProxyInstanceList.isEmpty()) {
1157 Either<Resource, StorageOperationStatus> serviceProxyOrigin = toscaOperationFacade
1158 .getLatestByName("serviceProxy");
1159 if (serviceProxyOrigin.isRight()) {
1160 log.debug("Failed to fetch normative service proxy resource by tosca name, error {}",
1161 serviceProxyOrigin.right().value());
1162 return Either.right(ToscaError.NOT_SUPPORTED_TOSCA_TYPE);
1164 Component origComponent = serviceProxyOrigin.left().value();
1166 for (Entry<String, ComponentInstance> entryProxy : serviceProxyInstanceList.entrySet()) {
1167 Component serviceComponent = null;
1168 ComponentParametersView componentParametersView = new ComponentParametersView();
1169 componentParametersView.disableAll();
1170 componentParametersView.setIgnoreCategories(false);
1171 componentParametersView.setIgnoreProperties(false);
1172 componentParametersView.setIgnoreInputs(false);
1173 componentParametersView.setIgnoreInterfaces(false);
1174 componentParametersView.setIgnoreRequirements(false);
1175 Either<Component, StorageOperationStatus> service = toscaOperationFacade
1176 .getToscaElement(entryProxy.getValue().getSourceModelUid(), componentParametersView);
1177 if (service.isRight()) {
1178 log.debug("Failed to fetch resource with id {} for instance {}",
1179 entryProxy.getValue().getSourceModelUid(), entryProxy.getValue().getName());
1181 serviceComponent = service.left().value();
1184 ToscaNodeType toscaNodeType = createProxyNodeType(componentCache, origComponent, serviceComponent,
1185 entryProxy.getValue());
1186 nodeTypesMap.put(entryProxy.getKey(), toscaNodeType);
1189 return Either.left(nodeTypesMap);
1192 private void createServiceSubstitutionNodeTypes(final Map<String, Component> componentCache,
1193 final Component container, final ToscaTemplate toscaNode) {
1194 final List<ComponentInstance> componentInstances = container.getComponentInstances();
1196 if (CollectionUtils.isEmpty(componentInstances)) {
1199 final List<ComponentInstance> serviceSubstitutionInstanceList = componentInstances.stream()
1200 .filter(p -> p.getOriginType().name().equals(OriginTypeEnum.ServiceSubstitution.name()))
1201 .collect(Collectors.toList());
1202 if (CollectionUtils.isNotEmpty(serviceSubstitutionInstanceList)) {
1203 for (ComponentInstance inst : serviceSubstitutionInstanceList) {
1204 final Map<String, ToscaNodeType> nodeTypes = toscaNode.getNode_types() == null ? new HashMap<>() : toscaNode.getNode_types();
1205 convertInterfaceNodeType(new HashMap<>(), componentCache.get(inst.getSourceModelUid()), toscaNode, nodeTypes, true);
1210 private ToscaNodeType createProxyNodeType(Map<String, Component> componentCache, Component origComponent,
1211 Component proxyComponent, ComponentInstance instance) {
1212 ToscaNodeType toscaNodeType = new ToscaNodeType();
1213 String derivedFrom = ((Resource) origComponent).getToscaResourceName();
1215 toscaNodeType.setDerived_from(derivedFrom);
1216 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> dataTypesEither = dataTypeCache.getAll();
1217 if (dataTypesEither.isRight()) {
1218 log.debug("Failed to retrieve all data types {}", dataTypesEither.right().value());
1220 Map<String, DataTypeDefinition> dataTypes = dataTypesEither.left().value();
1221 Map<String, ToscaCapability> capabilities = this.capabilityRequirementConverter
1222 .convertProxyCapabilities(componentCache, instance, dataTypes);
1224 if (MapUtils.isNotEmpty(capabilities)) {
1225 toscaNodeType.setCapabilities(capabilities);
1227 List<Map<String, ToscaRequirement>> proxyNodeTypeRequirements = this.capabilityRequirementConverter
1228 .convertProxyRequirements(componentCache, instance);
1229 if (CollectionUtils.isNotEmpty(proxyNodeTypeRequirements)) {
1230 toscaNodeType.setRequirements(proxyNodeTypeRequirements);
1232 Optional<Map<String, ToscaProperty>> proxyProperties = getProxyNodeTypeProperties(proxyComponent, dataTypes);
1233 proxyProperties.ifPresent(toscaNodeType::setProperties);
1235 Optional<Map<String, Object>> proxyInterfaces = getProxyNodeTypeInterfaces(proxyComponent, dataTypes);
1236 proxyInterfaces.ifPresent(toscaNodeType::setInterfaces);
1238 return toscaNodeType;
1241 private Either<ToscaNodeTemplate, ToscaError> convertComponentInstanceRequirements(Component component,
1242 ComponentInstance componentInstance,
1243 List<RequirementCapabilityRelDef> relations,
1244 ToscaNodeTemplate nodeTypeTemplate,
1245 Component originComponent,
1246 Map<String, Component> componentCache) {
1248 List<Map<String, ToscaTemplateRequirement>> toscaRequirements = new ArrayList<>();
1249 if (!addRequirements(component, componentInstance, relations, originComponent, toscaRequirements,
1251 log.debug("Failed to convert component instance requirements for the component instance {}. ",
1252 componentInstance.getName());
1253 return Either.right(ToscaError.NODE_TYPE_REQUIREMENT_ERROR);
1255 if (!toscaRequirements.isEmpty()) {
1256 nodeTypeTemplate.setRequirements(toscaRequirements);
1258 log.debug("Finished to convert requirements for the node type {} ", componentInstance.getName());
1259 return Either.left(nodeTypeTemplate);
1262 private boolean addRequirements(Component component, ComponentInstance componentInstance,
1263 List<RequirementCapabilityRelDef> relations, Component originComponent,
1264 List<Map<String, ToscaTemplateRequirement>> toscaRequirements,
1265 Map<String, Component> componentCache) {
1266 List<RequirementCapabilityRelDef> filteredRelations = relations.stream()
1267 .filter(p -> componentInstance.getUniqueId().equals(p.getFromNode())).collect(Collectors.toList());
1268 return isEmpty(filteredRelations) ||
1269 filteredRelations.stream()
1271 rel -> addRequirement(componentInstance, originComponent, component.getComponentInstances(), rel,
1272 toscaRequirements, componentCache));
1275 private boolean addRequirement(ComponentInstance fromInstance, Component fromOriginComponent,
1276 List<ComponentInstance> instancesList, RequirementCapabilityRelDef rel,
1277 List<Map<String, ToscaTemplateRequirement>> toscaRequirements,
1278 Map<String, Component> componentCache) {
1280 boolean result = true;
1281 Map<String, List<RequirementDefinition>> reqMap = fromOriginComponent.getRequirements();
1282 RelationshipInfo reqAndRelationshipPair = rel.getRelationships().get(0).getRelation();
1283 Either<Component, StorageOperationStatus> getOriginRes = null;
1284 Optional<RequirementDefinition> reqOpt = Optional.empty();
1285 Component toOriginComponent = null;
1286 Optional<CapabilityDefinition> capOpt = Optional.empty();
1288 ComponentInstance toInstance = instancesList.stream().filter(i -> rel.getToNode().equals(i.getUniqueId()))
1289 .findFirst().orElse(null);
1290 if (toInstance == null) {
1291 log.debug("Failed to find a relation from the node {} to the node {}", fromInstance.getName(),
1296 reqOpt = findRequirement(fromOriginComponent, reqMap, reqAndRelationshipPair, fromInstance.getUniqueId());
1297 if (!reqOpt.isPresent()) {
1298 log.debug("Failed to find a requirement with uniqueId {} on a component with uniqueId {}",
1299 reqAndRelationshipPair.getRequirementUid(), fromOriginComponent.getUniqueId());
1304 ComponentParametersView filter = new ComponentParametersView(true);
1305 filter.setIgnoreComponentInstances(false);
1306 filter.setIgnoreCapabilities(false);
1307 filter.setIgnoreGroups(false);
1308 getOriginRes = toscaOperationFacade.getToscaElement(toInstance.getActualComponentUid(), filter);
1309 if (getOriginRes.isRight()) {
1311 "Failed to build substituted name for the requirement {}. Failed to get an origin component with uniqueId {}",
1312 reqOpt.get().getName(), toInstance.getActualComponentUid());
1317 toOriginComponent = getOriginRes.left().value();
1318 capOpt = toOriginComponent.getCapabilities().get(reqOpt.get().getCapability()).stream()
1319 .filter(c -> isCapabilityBelongToRelation(reqAndRelationshipPair, c)).findFirst();
1320 if (!capOpt.isPresent()) {
1321 capOpt = findCapability(reqAndRelationshipPair, toOriginComponent, fromOriginComponent, reqOpt.get());
1322 if (!capOpt.isPresent()) {
1324 log.debug("Failed to find a capability with name {} on a component with uniqueId {}",
1325 reqAndRelationshipPair.getCapability(), fromOriginComponent.getUniqueId());
1330 result = buildAndAddRequirement(toscaRequirements, fromOriginComponent, toOriginComponent, capOpt.get(),
1331 reqOpt.get(), reqAndRelationshipPair, toInstance, componentCache);
1336 private boolean isCapabilityBelongToRelation(RelationshipInfo reqAndRelationshipPair,
1337 CapabilityDefinition capability) {
1338 return capability.getName().equals(reqAndRelationshipPair.getCapability()) && (capability.getOwnerId() != null
1339 && capability.getOwnerId().equals(reqAndRelationshipPair.getCapabilityOwnerId()));
1342 private Optional<CapabilityDefinition> findCapability(RelationshipInfo reqAndRelationshipPair,
1343 Component toOriginComponent, Component fromOriginComponent,
1344 RequirementDefinition requirement) {
1345 Optional<CapabilityDefinition> cap = toOriginComponent.getCapabilities().get(requirement.getCapability())
1346 .stream().filter(c -> c.getType().equals(requirement.getCapability())).findFirst();
1347 if (!cap.isPresent()) {
1348 log.debug("Failed to find a capability with name {} on a component with uniqueId {}",
1349 reqAndRelationshipPair.getCapability(), fromOriginComponent.getUniqueId());
1354 private boolean buildAndAddRequirement(List<Map<String, ToscaTemplateRequirement>> toscaRequirements,
1355 Component fromOriginComponent, Component toOriginComponent,
1356 CapabilityDefinition capability, RequirementDefinition requirement,
1357 RelationshipInfo reqAndRelationshipPair, ComponentInstance toInstance,
1358 Map<String, Component> componentCache) {
1359 List<String> reducedPath = capability.getPath();
1360 if (capability.getOwnerId() != null) {
1361 reducedPath = capabilityRequirementConverter
1362 .getReducedPathByOwner(capability.getPath(), capability.getOwnerId());
1364 Either<String, Boolean> buildCapNameRes = capabilityRequirementConverter.buildSubstitutedName(componentCache,
1365 toOriginComponent, reducedPath, reqAndRelationshipPair.getCapability(), capability.getPreviousName());
1366 if (buildCapNameRes.isRight()) {
1368 "Failed to build a substituted capability name for the capability with name {} on a component with uniqueId {}",
1369 reqAndRelationshipPair.getCapability(), fromOriginComponent.getUniqueId());
1372 Either<String, Boolean> buildReqNameRes = capabilityRequirementConverter
1373 .buildSubstitutedName(componentCache, fromOriginComponent,
1374 requirement.getPath(), reqAndRelationshipPair.getRequirement(), requirement.getPreviousName());
1375 if (buildReqNameRes.isRight()) {
1377 "Failed to build a substituted requirement name for the requirement with name {} on a component with uniqueId {}",
1378 reqAndRelationshipPair.getRequirement(), fromOriginComponent.getUniqueId());
1381 ToscaTemplateRequirement toscaRequirement = new ToscaTemplateRequirement();
1382 Map<String, ToscaTemplateRequirement> toscaReqMap = new HashMap<>();
1383 toscaRequirement.setNode(toInstance.getName());
1384 toscaRequirement.setCapability(buildCapNameRes.left().value());
1385 toscaReqMap.put(buildReqNameRes.left().value(), toscaRequirement);
1386 toscaRequirements.add(toscaReqMap);
1390 private Optional<RequirementDefinition> findRequirement(Component fromOriginComponent,
1391 Map<String, List<RequirementDefinition>> reqMap,
1392 RelationshipInfo reqAndRelationshipPair,
1393 String fromInstanceId) {
1394 for (List<RequirementDefinition> reqList : reqMap.values()) {
1395 Optional<RequirementDefinition> reqOpt = reqList.stream().filter(
1396 r -> isRequirementBelongToRelation(fromOriginComponent, reqAndRelationshipPair, r, fromInstanceId))
1398 if (reqOpt.isPresent()) {
1402 return Optional.empty();
1406 * Allows detecting the requirement belonging to the received relationship The detection logic is: A requirement
1407 * belongs to a relationship IF 1.The name of the requirement equals to the "requirement" field of the relation; AND
1408 * 2. In case of a non-atomic resource, OwnerId of the requirement equals to requirementOwnerId of the relation OR
1409 * uniqueId of toInstance equals to capabilityOwnerId of the relation
1411 private boolean isRequirementBelongToRelation(Component originComponent, RelationshipInfo reqAndRelationshipPair,
1412 RequirementDefinition requirement, String fromInstanceId) {
1413 if (!StringUtils.equals(requirement.getName(), reqAndRelationshipPair.getRequirement())) {
1414 log.debug("Failed to find a requirement with name {} and reqAndRelationshipPair {}",
1415 requirement.getName(), reqAndRelationshipPair.getRequirement());
1418 return ModelConverter.isAtomicComponent(originComponent) ||
1419 isRequirementBelongToOwner(reqAndRelationshipPair, requirement, fromInstanceId, originComponent);
1422 private boolean isRequirementBelongToOwner(RelationshipInfo reqAndRelationshipPair,
1423 RequirementDefinition requirement, String fromInstanceId,
1424 Component originComponent) {
1425 return StringUtils.equals(requirement.getOwnerId(), reqAndRelationshipPair.getRequirementOwnerId())
1426 || (isCvfc(originComponent) && StringUtils
1427 .equals(fromInstanceId, reqAndRelationshipPair.getRequirementOwnerId())
1428 || StringUtils.equals(requirement.getOwnerId(), originComponent.getUniqueId()));
1431 private boolean isCvfc(Component component) {
1432 return component.getComponentType() == ComponentTypeEnum.RESOURCE &&
1433 ((Resource) component).getResourceType() == ResourceTypeEnum.CVFC;
1436 private Either<SubstitutionMapping, ToscaError> convertCapabilities(Component component,
1437 SubstitutionMapping substitutionMappings,
1438 Map<String, Component> componentCache) {
1440 Either<SubstitutionMapping, ToscaError> result = Either.left(substitutionMappings);
1441 Either<Map<String, String[]>, ToscaError> toscaCapabilitiesRes = capabilityRequirementConverter
1442 .convertSubstitutionMappingCapabilities(componentCache, component);
1443 if (toscaCapabilitiesRes.isRight()) {
1444 result = Either.right(toscaCapabilitiesRes.right().value());
1445 log.debug("Failed convert capabilities for the component {}. ", component.getName());
1446 } else if (isNotEmpty(toscaCapabilitiesRes.left().value())) {
1447 substitutionMappings.setCapabilities(toscaCapabilitiesRes.left().value());
1448 log.debug("Finish convert capabilities for the component {}. ", component.getName());
1450 log.debug("Finished to convert capabilities for the component {}. ", component.getName());
1454 private Either<ToscaNodeType, ToscaError> convertCapabilities(Map<String, Component> componentsCache,
1455 Component component, ToscaNodeType nodeType,
1456 Map<String, DataTypeDefinition> dataTypes) {
1457 Map<String, ToscaCapability> toscaCapabilities = capabilityRequirementConverter
1458 .convertCapabilities(componentsCache, component,
1460 if (!toscaCapabilities.isEmpty()) {
1461 nodeType.setCapabilities(toscaCapabilities);
1463 log.debug("Finish convert Capabilities for node type");
1465 return Either.left(nodeType);
1468 private Map<String, ToscaTemplateArtifact> convertToNodeTemplateArtifacts(
1469 Map<String, ToscaArtifactDataDefinition> artifacts) {
1470 if (artifacts == null) {
1473 Map<String, ToscaTemplateArtifact> arts = new HashMap<>();
1474 for (Map.Entry<String, ToscaArtifactDataDefinition> entry : artifacts.entrySet()) {
1475 ToscaTemplateArtifact artifact = new ToscaTemplateArtifact();
1476 artifact.setFile(entry.getValue().getFile());
1477 artifact.setType(entry.getValue().getType());
1478 arts.put(entry.getKey(), artifact);
1483 protected NodeFilter convertToNodeTemplateNodeFilterComponent(CINodeFilterDataDefinition inNodeFilter) {
1484 if (inNodeFilter == null) {
1487 NodeFilter nodeFilter = new NodeFilter();
1489 ListDataDefinition<RequirementNodeFilterCapabilityDataDefinition> origCapabilities =
1490 inNodeFilter.getCapabilities();
1492 ListDataDefinition<RequirementNodeFilterPropertyDataDefinition> origProperties = inNodeFilter.getProperties();
1494 List<Map<String, CapabilityFilter>> capabilitiesCopy = new ArrayList<>();
1495 List<Map<String, List<Object>>> propertiesCopy = new ArrayList<>();
1497 copyNodeFilterCapabilitiesTemplate(origCapabilities, capabilitiesCopy);
1498 copyNodeFilterProperties(origProperties, propertiesCopy);
1500 if (CollectionUtils.isNotEmpty(capabilitiesCopy)) {
1501 nodeFilter.setCapabilities(capabilitiesCopy);
1504 if (CollectionUtils.isNotEmpty(propertiesCopy)) {
1505 nodeFilter.setProperties(propertiesCopy);
1508 nodeFilter.setTosca_id(cloneToscaId(inNodeFilter.getTosca_id()));
1510 nodeFilter = (NodeFilter) cloneObjectFromYml(nodeFilter, NodeFilter.class);
1515 private NodeFilter convertToSubstitutionFilterComponent(
1516 final SubstitutionFilterDataDefinition substitutionFilterDataDefinition) {
1518 NodeFilter nodeFilter = new NodeFilter();
1520 final List<Map<String, List<Object>>> propertiesCopy = new ArrayList<>();
1521 copySubstitutionFilterProperties(substitutionFilterDataDefinition.getProperties(), propertiesCopy);
1523 if (CollectionUtils.isNotEmpty(propertiesCopy)) {
1524 nodeFilter.setProperties(propertiesCopy);
1526 nodeFilter.setTosca_id(cloneToscaId(substitutionFilterDataDefinition.getTosca_id()));
1527 nodeFilter = (NodeFilter) cloneObjectFromYml(nodeFilter, NodeFilter.class);
1532 private Object cloneToscaId(Object toscaId) {
1533 return Objects.isNull(toscaId) ? null
1534 : cloneObjectFromYml(toscaId, toscaId.getClass());
1537 private Object cloneObjectFromYml(Object objToClone, Class classOfObj) {
1538 String objectAsYml = yamlUtil.objectToYaml(objToClone);
1539 return yamlUtil.yamlToObject(objectAsYml, classOfObj);
1542 private void copyNodeFilterCapabilitiesTemplate(
1543 ListDataDefinition<RequirementNodeFilterCapabilityDataDefinition> origCapabilities,
1544 List<Map<String, CapabilityFilter>> capabilitiesCopy) {
1545 if (origCapabilities == null || origCapabilities.getListToscaDataDefinition() == null ||
1546 origCapabilities.getListToscaDataDefinition().isEmpty()) {
1549 for (RequirementNodeFilterCapabilityDataDefinition capability : origCapabilities.getListToscaDataDefinition()) {
1550 Map<String, CapabilityFilter> capabilityFilterCopyMap = new HashMap<>();
1551 CapabilityFilter capabilityFilter = new CapabilityFilter();
1552 List<Map<String, List<Object>>> propertiesCopy = new ArrayList<>();
1553 copyNodeFilterProperties(capability.getProperties(), propertiesCopy);
1554 capabilityFilter.setProperties(propertiesCopy);
1555 capabilityFilterCopyMap.put(capability.getName(), capabilityFilter);
1556 capabilitiesCopy.add(capabilityFilterCopyMap);
1560 private void copyNodeFilterProperties(
1561 ListDataDefinition<RequirementNodeFilterPropertyDataDefinition> origProperties,
1562 List<Map<String, List<Object>>> propertiesCopy) {
1563 if (origProperties == null || origProperties.getListToscaDataDefinition() == null ||
1564 origProperties.isEmpty()) {
1567 Map<String, List<Object>> propertyMapCopy = new HashMap<>();
1568 for (RequirementNodeFilterPropertyDataDefinition propertyDataDefinition : origProperties
1569 .getListToscaDataDefinition()) {
1570 for (String propertyInfoEntry : propertyDataDefinition.getConstraints()) {
1571 Map propertyValObj = new YamlUtil().yamlToObject(propertyInfoEntry, Map.class);
1572 String propertyName = propertyDataDefinition.getName();
1573 if (propertyMapCopy.containsKey(propertyName)) {
1574 addPropertyConstraintValueToList(propertyName, propertyValObj, propertyMapCopy.get(propertyName));
1576 if (propertyName != null) {
1577 List propsList = new ArrayList();
1578 addPropertyConstraintValueToList(propertyName, propertyValObj, propsList);
1579 propertyMapCopy.put(propertyName, propsList);
1581 propertyMapCopy.putAll(propertyValObj);
1586 propertyMapCopy.entrySet().stream().forEach(entry ->
1587 addCalculatedConstraintsIntoPropertiesList(propertiesCopy, entry));
1590 private void copySubstitutionFilterProperties(
1591 final ListDataDefinition<RequirementSubstitutionFilterPropertyDataDefinition> origProperties,
1592 final List<Map<String, List<Object>>> propertiesCopy) {
1593 if (origProperties == null || origProperties.getListToscaDataDefinition() == null ||
1594 origProperties.isEmpty()) {
1597 final Map<String, List<Object>> propertyMapCopy = new HashMap<>();
1598 for (final RequirementSubstitutionFilterPropertyDataDefinition propertyDataDefinition : origProperties
1599 .getListToscaDataDefinition()) {
1600 for (final String propertyInfoEntry : propertyDataDefinition.getConstraints()) {
1601 final Map<String, List<Object>> propertyValObj = new YamlUtil().yamlToObject(propertyInfoEntry, Map.class);
1602 final String propertyName = propertyDataDefinition.getName();
1603 if (propertyMapCopy.containsKey(propertyName)) {
1604 addPropertyConstraintValueToList(propertyName, propertyValObj, propertyMapCopy.get(propertyName));
1606 if (propertyName != null) {
1607 final List<Object> propsList = new ArrayList();
1608 addPropertyConstraintValueToList(propertyName, propertyValObj, propsList);
1609 propertyMapCopy.put(propertyName, propsList);
1611 propertyMapCopy.putAll(propertyValObj);
1616 propertyMapCopy.entrySet().forEach(entry ->
1617 addCalculatedConstraintsIntoPropertiesList(propertiesCopy, entry));
1620 private void addPropertyConstraintValueToList(String propertyName, Map propertyValObj, List propsList) {
1621 if (propertyValObj.containsKey(propertyName)) {
1622 propsList.add(propertyValObj.get(propertyName));
1624 propsList.add(propertyValObj);
1628 private void addCalculatedConstraintsIntoPropertiesList(List<Map<String, List<Object>>> propertiesCopy,
1629 Entry<String, List<Object>> entry) {
1630 Map<String, List<Object>> tempMap = new HashMap<>();
1631 tempMap.put(entry.getKey(), entry.getValue());
1632 propertiesCopy.add(tempMap);
1635 private static class CustomRepresenter extends Representer {
1637 CustomRepresenter() {
1639 // null representer is exceptional and it is stored as an instance
1641 this.nullRepresenter = new RepresentNull();
1646 protected NodeTuple representJavaBeanProperty(Object javaBean, Property property, Object propertyValue,
1648 if (propertyValue == null) {
1651 // skip not relevant for Tosca property
1652 if ("dependencies".equals(property.getName())) {
1655 removeDefaultP(propertyValue);
1656 NodeTuple defaultNode = super.representJavaBeanProperty(javaBean, property, propertyValue, customTag);
1658 return "_defaultp_".equals(property.getName())
1659 ? new NodeTuple(representData("default"), defaultNode.getValueNode()) : defaultNode;
1662 private void removeDefaultP(final Object propertyValue) {
1663 if (propertyValue instanceof Map) {
1664 final Map mapPropertyValue = ((Map) propertyValue);
1666 final Iterator<Entry> iter = mapPropertyValue.entrySet().iterator();
1667 Object defaultValue = null;
1668 while (iter.hasNext()) {
1669 final Map.Entry entry = iter.next();
1671 if ("_defaultp_".equals(entry.getKey())) {
1672 defaultValue = entry.getValue();
1674 } else if (entry.getValue() instanceof Map) {
1675 removeDefaultP(entry.getValue());
1678 if (defaultValue != null) {
1679 mapPropertyValue.putIfAbsent("default", defaultValue);
1685 protected MappingNode representJavaBean(Set<Property> properties, Object javaBean) {
1686 // remove the bean type from the output yaml (!! ...)
1687 if (!classTags.containsKey(javaBean.getClass())) {
1688 addClassTag(javaBean.getClass(), Tag.MAP);
1691 return super.representJavaBean(properties, javaBean);
1694 private class RepresentNull implements Represent {
1697 public Node representData(Object data) {
1698 // possible values are here http://yaml.org/type/null.html
1699 return representScalar(Tag.NULL, "");
1704 private static class UnsortedPropertyUtils extends PropertyUtils {
1707 protected Set<Property> createPropertySet(Class type, BeanAccess bAccess)
1708 throws IntrospectionException {
1709 Collection<Property> fields = getPropertiesMap(type, BeanAccess.FIELD).values();
1710 return new LinkedHashSet<>(fields);
1714 private Object removeOperationsKeyFromInterface(Object interfaceInstanceDataDefinition) {
1715 ObjectMapper objectMapper = new ObjectMapper();
1716 objectMapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false);
1718 Map<String, Object> interfaceAsMap = ServiceUtils.getObjectAsMap(interfaceInstanceDataDefinition);
1719 Map<String, Object> operations = (Map<String, Object>) interfaceAsMap.remove("operations");
1720 interfaceAsMap.remove("empty");
1722 if (MapUtils.isNotEmpty(operations)) {
1723 interfaceAsMap.putAll(operations);
1726 Object interfaceObject = objectMapper.convertValue(interfaceAsMap, Object.class);
1728 return interfaceObject;
1732 private Optional<Map<String, ToscaProperty>> getProxyNodeTypeInputProperties(final Component proxyComponent,
1733 final Map<String, DataTypeDefinition> dataTypes) {
1734 if (Objects.isNull(proxyComponent)) {
1735 return Optional.empty();
1737 final Map<String, ToscaProperty> proxyInputProperties = new HashMap<>();
1738 addInputsToProperties(dataTypes, proxyComponent.getInputs(), proxyInputProperties);
1739 return MapUtils.isNotEmpty(proxyInputProperties) ? Optional.of(proxyInputProperties) : Optional.empty();
1742 Optional<Map<String, ToscaProperty>> getProxyNodeTypeProperties(Component proxyComponent,
1743 Map<String, DataTypeDefinition>
1745 if (Objects.isNull(proxyComponent)) {
1746 return Optional.empty();
1748 Map<String, ToscaProperty> proxyProperties = new HashMap<>();
1749 addInputsToProperties(dataTypes, proxyComponent.getInputs(), proxyProperties);
1750 if (CollectionUtils.isNotEmpty(proxyComponent.getProperties())) {
1751 proxyProperties.putAll(proxyComponent.getProperties().stream()
1752 .map(propertyDefinition -> resolvePropertyValueFromInput(propertyDefinition,
1753 proxyComponent.getInputs()))
1754 .collect(Collectors.toMap(PropertyDataDefinition::getName,
1755 property -> propertyConvertor.convertProperty(dataTypes, property,
1756 PropertyConvertor.PropertyType.PROPERTY))));
1758 return MapUtils.isNotEmpty(proxyProperties) ? Optional.of(proxyProperties) : Optional.empty();
1761 void addInputsToProperties(Map<String, DataTypeDefinition> dataTypes,
1762 List<InputDefinition> componentInputs,
1763 Map<String, ToscaProperty> mergedProperties) {
1764 if (CollectionUtils.isEmpty(componentInputs)) {
1767 for (InputDefinition input : componentInputs) {
1768 ToscaProperty property = propertyConvertor.convertProperty(dataTypes, input,
1769 PropertyConvertor.PropertyType.INPUT);
1770 mergedProperties.put(input.getName(), property);
1774 Optional<Map<String, Object>> getProxyNodeTypeInterfaces(Component proxyComponent,
1775 Map<String, DataTypeDefinition> dataTypes) {
1776 if (Objects.isNull(proxyComponent) || MapUtils.isEmpty(proxyComponent.getInterfaces())) {
1777 return Optional.empty();
1779 Map<String, InterfaceDefinition> proxyComponentInterfaces = proxyComponent.getInterfaces();
1780 //Unset artifact path for operation implementation for proxy node types as for operations with artifacts it is
1781 // always available in the proxy node template
1782 removeOperationImplementationForProxyNodeType(proxyComponentInterfaces);
1783 return Optional.ofNullable(interfacesOperationsConverter
1784 .getInterfacesMap(proxyComponent, null, proxyComponentInterfaces, dataTypes,
1788 private static void removeOperationImplementationForProxyNodeType(
1789 Map<String, InterfaceDefinition> proxyComponentInterfaces) {
1790 if (MapUtils.isEmpty(proxyComponentInterfaces)) {
1793 proxyComponentInterfaces.values().stream().map(InterfaceDataDefinition::getOperations)
1794 .filter(MapUtils::isNotEmpty)
1795 .forEach(operations -> operations.values().forEach(operation -> operation.setImplementation(null)));