Fix import VFC with attributes
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / tosca / ToscaExportHandler.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.openecomp.sdc.be.tosca;
22
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
29 import com.fasterxml.jackson.databind.ObjectMapper;
30 import com.fasterxml.jackson.databind.SerializationFeature;
31 import fj.data.Either;
32 import java.beans.IntrospectionException;
33 import java.util.ArrayList;
34 import java.util.Collection;
35 import java.util.Collections;
36 import java.util.HashMap;
37 import java.util.Iterator;
38 import java.util.LinkedHashSet;
39 import java.util.List;
40 import java.util.Map;
41 import java.util.Map.Entry;
42 import java.util.Objects;
43 import java.util.Optional;
44 import java.util.Set;
45 import java.util.function.Supplier;
46 import java.util.stream.Collectors;
47 import org.apache.commons.collections.CollectionUtils;
48 import org.apache.commons.collections.MapUtils;
49 import org.apache.commons.lang.StringUtils;
50 import org.apache.commons.lang3.tuple.ImmutablePair;
51 import org.apache.commons.lang3.tuple.ImmutableTriple;
52 import org.apache.commons.lang3.tuple.Triple;
53 import org.onap.sdc.tosca.datatypes.model.AttributeDefinition;
54 import org.onap.sdc.tosca.services.YamlUtil;
55 import org.openecomp.sdc.be.components.impl.exceptions.SdcResourceNotFoundException;
56 import org.openecomp.sdc.be.config.ConfigurationManager;
57 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
58 import org.openecomp.sdc.be.datatypes.components.ResourceMetadataDataDefinition;
59 import org.openecomp.sdc.be.datatypes.elements.AttributeDataDefinition;
60 import org.openecomp.sdc.be.datatypes.elements.CINodeFilterDataDefinition;
61 import org.openecomp.sdc.be.datatypes.elements.InterfaceDataDefinition;
62 import org.openecomp.sdc.be.datatypes.elements.ListDataDefinition;
63 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
64 import org.openecomp.sdc.be.datatypes.elements.RequirementNodeFilterCapabilityDataDefinition;
65 import org.openecomp.sdc.be.datatypes.elements.RequirementNodeFilterPropertyDataDefinition;
66 import org.openecomp.sdc.be.datatypes.elements.RequirementSubstitutionFilterPropertyDataDefinition;
67 import org.openecomp.sdc.be.datatypes.elements.SubstitutionFilterDataDefinition;
68 import org.openecomp.sdc.be.datatypes.elements.ToscaArtifactDataDefinition;
69 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
70 import org.openecomp.sdc.be.datatypes.enums.OriginTypeEnum;
71 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
72 import org.openecomp.sdc.be.model.ArtifactDefinition;
73 import org.openecomp.sdc.be.model.CapabilityDefinition;
74 import org.openecomp.sdc.be.model.Component;
75 import org.openecomp.sdc.be.model.ComponentInstance;
76 import org.openecomp.sdc.be.model.ComponentInstanceInput;
77 import org.openecomp.sdc.be.model.ComponentInstanceInterface;
78 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
79 import org.openecomp.sdc.be.model.ComponentParametersView;
80 import org.openecomp.sdc.be.model.DataTypeDefinition;
81 import org.openecomp.sdc.be.model.GroupInstance;
82 import org.openecomp.sdc.be.model.InputDefinition;
83 import org.openecomp.sdc.be.model.InterfaceDefinition;
84 import org.openecomp.sdc.be.model.PropertyDefinition;
85 import org.openecomp.sdc.be.model.RelationshipInfo;
86 import org.openecomp.sdc.be.model.RequirementCapabilityRelDef;
87 import org.openecomp.sdc.be.model.RequirementDefinition;
88 import org.openecomp.sdc.be.model.Resource;
89 import org.openecomp.sdc.be.model.Service;
90 import org.openecomp.sdc.be.model.cache.ApplicationDataTypeCache;
91 import org.openecomp.sdc.be.model.category.CategoryDefinition;
92 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
93 import org.openecomp.sdc.be.model.jsonjanusgraph.utils.ModelConverter;
94 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
95 import org.openecomp.sdc.be.model.operations.impl.InterfaceLifecycleOperation;
96 import org.openecomp.sdc.be.model.tosca.converters.ToscaValueBaseConverter;
97 import org.openecomp.sdc.be.tosca.model.CapabilityFilter;
98 import org.openecomp.sdc.be.tosca.model.NodeFilter;
99 import org.openecomp.sdc.be.tosca.model.SubstitutionMapping;
100 import org.openecomp.sdc.be.tosca.model.ToscaCapability;
101 import org.openecomp.sdc.be.tosca.model.ToscaDataType;
102 import org.openecomp.sdc.be.tosca.model.ToscaGroupTemplate;
103 import org.openecomp.sdc.be.tosca.model.ToscaMetadata;
104 import org.openecomp.sdc.be.tosca.model.ToscaNodeTemplate;
105 import org.openecomp.sdc.be.tosca.model.ToscaNodeType;
106 import org.openecomp.sdc.be.tosca.model.ToscaPolicyTemplate;
107 import org.openecomp.sdc.be.tosca.model.ToscaProperty;
108 import org.openecomp.sdc.be.tosca.model.ToscaRequirement;
109 import org.openecomp.sdc.be.tosca.model.ToscaTemplate;
110 import org.openecomp.sdc.be.tosca.model.ToscaTemplateArtifact;
111 import org.openecomp.sdc.be.tosca.model.ToscaTemplateRequirement;
112 import org.openecomp.sdc.be.tosca.model.ToscaTopolgyTemplate;
113 import org.openecomp.sdc.be.tosca.utils.ForwardingPathToscaUtil;
114 import org.openecomp.sdc.be.tosca.utils.InputConverter;
115 import org.openecomp.sdc.common.log.wrappers.Logger;
116 import org.openecomp.sdc.externalupload.utils.ServiceUtils;
117 import org.springframework.beans.factory.annotation.Autowired;
118 import org.yaml.snakeyaml.DumperOptions;
119 import org.yaml.snakeyaml.DumperOptions.FlowStyle;
120 import org.yaml.snakeyaml.Yaml;
121 import org.yaml.snakeyaml.introspector.BeanAccess;
122 import org.yaml.snakeyaml.introspector.Property;
123 import org.yaml.snakeyaml.introspector.PropertyUtils;
124 import org.yaml.snakeyaml.nodes.MappingNode;
125 import org.yaml.snakeyaml.nodes.Node;
126 import org.yaml.snakeyaml.nodes.NodeTuple;
127 import org.yaml.snakeyaml.nodes.Tag;
128 import org.yaml.snakeyaml.representer.Represent;
129 import org.yaml.snakeyaml.representer.Representer;
130
131 @org.springframework.stereotype.Component("tosca-export-handler")
132 public class ToscaExportHandler {
133
134     private static final Logger log = Logger.getLogger(ToscaExportHandler.class);
135
136     private ApplicationDataTypeCache dataTypeCache;
137     private ToscaOperationFacade toscaOperationFacade;
138     private CapabilityRequirementConverter capabilityRequirementConverter;
139     private PolicyExportParser policyExportParser;
140     private GroupExportParser groupExportParser;
141     private PropertyConvertor propertyConvertor;
142     private InputConverter inputConverter;
143     private InterfaceLifecycleOperation interfaceLifecycleOperation;
144     private InterfacesOperationsConverter interfacesOperationsConverter;
145
146     @Autowired
147     public ToscaExportHandler(ApplicationDataTypeCache dataTypeCache, ToscaOperationFacade toscaOperationFacade,
148                               CapabilityRequirementConverter capabilityRequirementConverter,
149                               PolicyExportParser policyExportParser,
150                               GroupExportParser groupExportParser, PropertyConvertor propertyConvertor,
151                               InputConverter inputConverter,
152                               InterfaceLifecycleOperation interfaceLifecycleOperation,
153                               InterfacesOperationsConverter interfacesOperationsConverter) {
154         this.dataTypeCache = dataTypeCache;
155         this.toscaOperationFacade = toscaOperationFacade;
156         this.capabilityRequirementConverter = capabilityRequirementConverter;
157         this.policyExportParser = policyExportParser;
158         this.groupExportParser = groupExportParser;
159         this.propertyConvertor = propertyConvertor;
160         this.inputConverter = inputConverter;
161         this.interfaceLifecycleOperation = interfaceLifecycleOperation;
162         this.interfacesOperationsConverter = interfacesOperationsConverter;
163     }
164
165     private static final String TOSCA_VERSION = "tosca_simple_yaml_1_1";
166     private static final String SERVICE_NODE_TYPE_PREFIX = "org.openecomp.service.";
167     private static final String IMPORTS_FILE_KEY = "file";
168     private static final String TOSCA_INTERFACE_NAME = "-interface.yml";
169     public static final String ASSET_TOSCA_TEMPLATE = "assettoscatemplate";
170     private static final String FAILED_TO_GET_DEFAULT_IMPORTS_CONFIGURATION = "convertToToscaTemplate - failed to get Default Imports section from configuration";
171     private static final String NOT_SUPPORTED_COMPONENT_TYPE = "Not supported component type {}";
172     private static final String NATIVE_ROOT = "tosca.nodes.Root";
173     private static YamlUtil yamlUtil = new YamlUtil();
174
175     public ToscaExportHandler() {
176     }
177
178     public Either<ToscaRepresentation, ToscaError> exportComponent(Component component) {
179         return convertToToscaTemplate(component).left().map(this::createToscaRepresentation);
180     }
181
182     public Either<ToscaRepresentation, ToscaError> exportComponentInterface(final Component component,
183                                                                             final boolean isAssociatedComponent) {
184         final List<Map<String, Map<String, String>>> defaultToscaImportConfig = getDefaultToscaImportConfig();
185         if (CollectionUtils.isEmpty(defaultToscaImportConfig)) {
186             log.debug(FAILED_TO_GET_DEFAULT_IMPORTS_CONFIGURATION);
187             return Either.right(ToscaError.GENERAL_ERROR);
188         }
189
190         String toscaVersion = null;
191         if (component instanceof Resource) {
192             toscaVersion = ((Resource) component).getToscaVersion();
193         }
194         ToscaTemplate toscaTemplate = new ToscaTemplate(toscaVersion != null ? toscaVersion : TOSCA_VERSION);
195         toscaTemplate.setImports(new ArrayList<>(defaultToscaImportConfig));
196         final Map<String, ToscaNodeType> nodeTypes = new HashMap<>();
197         final Either<ToscaTemplate, ToscaError> toscaTemplateRes =
198             convertInterfaceNodeType(new HashMap<>(), component, toscaTemplate, nodeTypes, isAssociatedComponent);
199         if (toscaTemplateRes.isRight()) {
200             return Either.right(toscaTemplateRes.right().value());
201         }
202
203         toscaTemplate = toscaTemplateRes.left().value();
204         ToscaRepresentation toscaRepresentation = this.createToscaRepresentation(toscaTemplate);
205         return Either.left(toscaRepresentation);
206     }
207
208     public ToscaRepresentation createToscaRepresentation(ToscaTemplate toscaTemplate) {
209         CustomRepresenter representer = new CustomRepresenter();
210         DumperOptions options = new DumperOptions();
211         options.setAllowReadOnlyProperties(false);
212         options.setPrettyFlow(true);
213
214         options.setDefaultFlowStyle(FlowStyle.FLOW);
215         options.setCanonical(false);
216
217         representer.addClassTag(toscaTemplate.getClass(), Tag.MAP);
218
219         representer.setPropertyUtils(new UnsortedPropertyUtils());
220         Yaml yaml = new Yaml(representer, options);
221
222         String yamlAsString = yaml.dumpAsMap(toscaTemplate);
223
224         StringBuilder sb = new StringBuilder();
225         sb.append(ConfigurationManager.getConfigurationManager().getConfiguration().getHeatEnvArtifactHeader());
226         sb.append(yamlAsString);
227         sb.append(ConfigurationManager.getConfigurationManager().getConfiguration().getHeatEnvArtifactFooter());
228
229         return ToscaRepresentation.make(sb.toString().getBytes(), toscaTemplate);
230     }
231
232     public Either<ToscaTemplate, ToscaError> getDependencies(Component component) {
233         ToscaTemplate toscaTemplate = new ToscaTemplate(null);
234         Either<ImmutablePair<ToscaTemplate, Map<String, Component>>, ToscaError> fillImports = fillImports(component,
235             toscaTemplate);
236         if (fillImports.isRight()) {
237             return Either.right(fillImports.right().value());
238         }
239         return Either.left(fillImports.left().value().left);
240     }
241
242     public Either<ToscaTemplate, ToscaError> convertToToscaTemplate(final Component component) {
243         final List<Map<String, Map<String, String>>> defaultToscaImportConfig = getDefaultToscaImportConfig();
244         if (CollectionUtils.isEmpty(defaultToscaImportConfig)) {
245             log.debug(FAILED_TO_GET_DEFAULT_IMPORTS_CONFIGURATION);
246             return Either.right(ToscaError.GENERAL_ERROR);
247         }
248         log.trace("start tosca export for {}", component.getUniqueId());
249         String toscaVersion = null;
250         if (component instanceof Resource) {
251             toscaVersion = ((Resource) component).getToscaVersion();
252         }
253         final ToscaTemplate toscaTemplate = new ToscaTemplate(toscaVersion != null ? toscaVersion : TOSCA_VERSION);
254         toscaTemplate.setMetadata(convertMetadata(component));
255         toscaTemplate.setImports(new ArrayList<>(defaultToscaImportConfig));
256         final Map<String, ToscaNodeType> nodeTypes = new HashMap<>();
257         if (ModelConverter.isAtomicComponent(component)) {
258             log.trace("convert component as node type");
259             return convertNodeType(new HashMap<>(), component, toscaTemplate, nodeTypes);
260         } else {
261             log.trace("convert component as topology template");
262             return convertToscaTemplate(component, toscaTemplate);
263         }
264
265     }
266
267     private Either<ToscaTemplate, ToscaError> convertToscaTemplate(Component component, ToscaTemplate toscaNode) {
268
269         Either<ImmutablePair<ToscaTemplate, Map<String, Component>>, ToscaError> importsRes = fillImports(component,
270             toscaNode);
271         if (importsRes.isRight()) {
272             return Either.right(importsRes.right().value());
273         }
274         toscaNode = importsRes.left().value().left;
275         Map<String, Component> componentCache = importsRes.left().value().right;
276         Either<Map<String, ToscaNodeType>, ToscaError> nodeTypesMapEither = createProxyNodeTypes(componentCache,
277             component);
278         if (nodeTypesMapEither.isRight()) {
279             log.debug("Failed to fetch normative service proxy resource by tosca name, error {}",
280                 nodeTypesMapEither.right().value());
281             return Either.right(nodeTypesMapEither.right().value());
282         }
283         Map<String, ToscaNodeType> nodeTypesMap = nodeTypesMapEither.left().value();
284         if (nodeTypesMap != null && !nodeTypesMap.isEmpty()) {
285             toscaNode.setNode_types(nodeTypesMap);
286         }
287         
288         createServiceSubstitutionNodeTypes(componentCache, component, toscaNode);
289
290         Either<Map<String, Object>, ToscaError> proxyInterfaceTypesEither = createProxyInterfaceTypes(component);
291         if (proxyInterfaceTypesEither.isRight()) {
292             log.debug("Failed to populate service proxy local interface types in tosca, error {}",
293                 nodeTypesMapEither.right().value());
294             return Either.right(proxyInterfaceTypesEither.right().value());
295         }
296         Map<String, Object> proxyInterfaceTypes = proxyInterfaceTypesEither.left().value();
297         if (MapUtils.isNotEmpty(proxyInterfaceTypes)) {
298             toscaNode.setInterface_types(proxyInterfaceTypes);
299         }
300
301         Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> dataTypesEither = dataTypeCache.getAll();
302         if (dataTypesEither.isRight()) {
303             log.debug("Failed to retrieve all data types {}", dataTypesEither.right().value());
304             return Either.right(ToscaError.GENERAL_ERROR);
305         }
306         Map<String, DataTypeDefinition> dataTypes = dataTypesEither.left().value();
307         ToscaTopolgyTemplate topologyTemplate = new ToscaTopolgyTemplate();
308         List<InputDefinition> inputDef = component.getInputs();
309         Map<String, ToscaProperty> inputs = inputConverter.convertInputs(inputDef, dataTypes);
310
311         if (!inputs.isEmpty()) {
312             topologyTemplate.setInputs(inputs);
313         }
314
315         final List<ComponentInstance> componentInstances = component.getComponentInstances();
316         Map<String, List<ComponentInstanceProperty>> componentInstancesProperties =
317             component.getComponentInstancesProperties();
318         Map<String, List<ComponentInstanceInterface>> componentInstanceInterfaces =
319             component.getComponentInstancesInterfaces();
320         if (CollectionUtils.isNotEmpty(componentInstances)) {
321             final Either<Map<String, ToscaNodeTemplate>, ToscaError> nodeTemplates =
322                 convertNodeTemplates(component, componentInstances,
323                     componentInstancesProperties, componentInstanceInterfaces,
324                     componentCache, dataTypes, topologyTemplate);
325             if (nodeTemplates.isRight()) {
326                 return Either.right(nodeTemplates.right().value());
327             }
328             log.debug("node templates converted");
329             topologyTemplate.setNode_templates(nodeTemplates.left().value());
330         }
331
332         SubstitutionMapping substitutionMapping = new SubstitutionMapping();
333         convertSubstitutionMappingFilter(component, substitutionMapping);
334
335         addGroupsToTopologyTemplate(component, topologyTemplate);
336
337         try {
338             addPoliciesToTopologyTemplate(component, topologyTemplate);
339         } catch (SdcResourceNotFoundException e) {
340             log.debug("Fail to add policies to topology template:", e);
341             return Either.right(ToscaError.GENERAL_ERROR);
342         }
343
344         String toscaResourceName;
345         switch (component.getComponentType()) {
346             case RESOURCE:
347                 toscaResourceName = ((ResourceMetadataDataDefinition) component.getComponentMetadataDefinition()
348                     .getMetadataDataDefinition()).getToscaResourceName();
349                 break;
350             case SERVICE:
351                 toscaResourceName = SERVICE_NODE_TYPE_PREFIX
352                     + component.getComponentMetadataDefinition().getMetadataDataDefinition().getSystemName();
353                 break;
354             default:
355                 log.debug(NOT_SUPPORTED_COMPONENT_TYPE, component.getComponentType());
356                 return Either.right(ToscaError.NOT_SUPPORTED_TOSCA_TYPE);
357         }
358         substitutionMapping.setNode_type(toscaResourceName);
359         Either<SubstitutionMapping, ToscaError> capabilities = convertCapabilities(component, substitutionMapping,
360             componentCache);
361         if (capabilities.isRight()) {
362             return Either.right(capabilities.right().value());
363         }
364         substitutionMapping = capabilities.left().value();
365
366         Either<SubstitutionMapping, ToscaError> requirements = capabilityRequirementConverter
367             .convertSubstitutionMappingRequirements(componentCache, component, substitutionMapping);
368         if (requirements.isRight()) {
369             return Either.right(requirements.right().value());
370         }
371         substitutionMapping = requirements.left().value();
372         final Map<String, String[]> propertyMappingMap = buildSubstitutionMappingPropertyMapping(component);
373         if (!propertyMappingMap.isEmpty()) {
374             substitutionMapping.setProperties(propertyMappingMap);
375         }
376
377         final Map<String, String[]> attributesMappingMap = buildSubstitutionMappingAttributesMapping(component);
378         if (!attributesMappingMap.isEmpty()) {
379             substitutionMapping.setAttributes(attributesMappingMap);
380         }
381
382         topologyTemplate.setSubstitution_mappings(substitutionMapping);
383
384         toscaNode.setTopology_template(topologyTemplate);
385
386         return Either.left(toscaNode);
387     }
388
389     private void convertSubstitutionMappingFilter(final Component component,
390                                                   final SubstitutionMapping substitutionMapping) {
391         if(component.getSubstitutionFilter() != null
392                 && (component.getSubstitutionFilter().getProperties()).getListToscaDataDefinition() != null) {
393             substitutionMapping
394                     .setSubstitution_filter(convertToSubstitutionFilterComponent(component.getSubstitutionFilter()));
395         }
396     }
397
398     private void addGroupsToTopologyTemplate(Component component, ToscaTopolgyTemplate topologyTemplate) {
399         Map<String, ToscaGroupTemplate> groups = groupExportParser.getGroups(component);
400         if (groups != null) {
401             topologyTemplate.addGroups(groups);
402         }
403     }
404
405     private void addPoliciesToTopologyTemplate(Component component, ToscaTopolgyTemplate topologyTemplate)
406         throws SdcResourceNotFoundException {
407         Map<String, ToscaPolicyTemplate> policies = policyExportParser.getPolicies(component);
408         if (policies != null) {
409             topologyTemplate.addPolicies(policies);
410         }
411     }
412
413     private ToscaMetadata convertMetadata(Component component) {
414         return convertMetadata(component, false, null);
415     }
416
417     private ToscaMetadata convertMetadata(Component component, boolean isInstance,
418                                           ComponentInstance componentInstance) {
419         ToscaMetadata toscaMetadata = new ToscaMetadata();
420         toscaMetadata.setInvariantUUID(component.getInvariantUUID());
421         toscaMetadata.setUUID(component.getUUID());
422         toscaMetadata.setDescription(component.getDescription());
423         toscaMetadata.setName(component.getComponentMetadataDefinition().getMetadataDataDefinition().getName());
424
425         List<CategoryDefinition> categories = component.getCategories();
426         CategoryDefinition categoryDefinition = categories.get(0);
427         toscaMetadata.setCategory(categoryDefinition.getName());
428
429         if (isInstance) {
430             toscaMetadata.setVersion(component.getVersion());
431             toscaMetadata.setCustomizationUUID(componentInstance.getCustomizationUUID());
432             if (componentInstance.getSourceModelInvariant() != null
433                 && !componentInstance.getSourceModelInvariant().isEmpty()) {
434                 toscaMetadata.setVersion(componentInstance.getComponentVersion());
435                 toscaMetadata.setSourceModelInvariant(componentInstance.getSourceModelInvariant());
436                 toscaMetadata.setSourceModelUuid(componentInstance.getSourceModelUuid());
437                 toscaMetadata.setSourceModelName(componentInstance.getSourceModelName());
438                 if (componentInstance.getOriginType() == OriginTypeEnum.ServiceProxy) {
439                         toscaMetadata.setName(componentInstance.getSourceModelName() + " " + OriginTypeEnum.ServiceProxy.getDisplayValue());
440                 } else if (componentInstance.getOriginType() == OriginTypeEnum.ServiceSubstitution) {
441                     toscaMetadata.setName(componentInstance.getSourceModelName() + " " + OriginTypeEnum.ServiceSubstitution.getDisplayValue());
442                 }
443                 toscaMetadata.setDescription(componentInstance.getDescription());
444             }
445
446         }
447         switch (component.getComponentType()) {
448             case RESOURCE:
449                 Resource resource = (Resource) component;
450
451             if (isInstance && (componentInstance.getOriginType() == OriginTypeEnum.ServiceProxy || componentInstance.getOriginType() == OriginTypeEnum.ServiceSubstitution)) {
452                     toscaMetadata.setType(componentInstance.getOriginType().getDisplayValue());
453                 } else {
454                     toscaMetadata.setType(resource.getResourceType().name());
455                 }
456                 toscaMetadata.setSubcategory(categoryDefinition.getSubcategories().get(0).getName());
457                 toscaMetadata.setResourceVendor(resource.getVendorName());
458                 toscaMetadata.setResourceVendorRelease(resource.getVendorRelease());
459                 toscaMetadata.setResourceVendorModelNumber(resource.getResourceVendorModelNumber());
460                 break;
461             case SERVICE:
462                 Service service = (Service) component;
463                 toscaMetadata.setType(component.getComponentType().getValue());
464                 toscaMetadata.setServiceType(service.getServiceType());
465                 toscaMetadata.setServiceRole(service.getServiceRole());
466                 toscaMetadata.setServiceFunction(service.getServiceFunction());
467                 toscaMetadata.setEnvironmentContext(service.getEnvironmentContext());
468                 resolveInstantiationTypeAndSetItToToscaMetaData(toscaMetadata, service);
469                 if (!isInstance) {
470                     // DE268546
471                     toscaMetadata.setServiceEcompNaming(((Service) component).isEcompGeneratedNaming());
472                     toscaMetadata.setEcompGeneratedNaming(((Service) component).isEcompGeneratedNaming());
473                     toscaMetadata.setNamingPolicy(((Service) component).getNamingPolicy());
474                 }
475                 break;
476             default:
477                 log.debug(NOT_SUPPORTED_COMPONENT_TYPE, component.getComponentType());
478         }
479         return toscaMetadata;
480     }
481
482     private void resolveInstantiationTypeAndSetItToToscaMetaData(ToscaMetadata toscaMetadata, Service service) {
483         if (service.getInstantiationType() != null) {
484             toscaMetadata.setInstantiationType(service.getInstantiationType());
485         } else {
486             toscaMetadata.setInstantiationType(StringUtils.EMPTY);
487         }
488     }
489
490     private Either<ImmutablePair<ToscaTemplate, Map<String, Component>>, ToscaError> fillImports(Component component,
491                                                                                                  ToscaTemplate toscaTemplate) {
492
493         final List<Map<String, Map<String, String>>> defaultToscaImportConfig = getDefaultToscaImportConfig();
494         if (CollectionUtils.isEmpty(defaultToscaImportConfig)) {
495             log.debug(FAILED_TO_GET_DEFAULT_IMPORTS_CONFIGURATION);
496             return Either.right(ToscaError.GENERAL_ERROR);
497         }
498         Map<String, Component> componentCache = new HashMap<>();
499
500         if (!ModelConverter.isAtomicComponent(component)) {
501             final List<Map<String, Map<String, String>>> additionalImports =
502                 toscaTemplate.getImports() == null ? new ArrayList<>(defaultToscaImportConfig)
503                     : new ArrayList<>(toscaTemplate.getImports());
504
505             List<Triple<String, String, Component>> dependecies = new ArrayList<>();
506
507             Map<String, ArtifactDefinition> toscaArtifacts = component.getToscaArtifacts();
508             if (isNotEmpty(toscaArtifacts)) {
509                 ArtifactDefinition artifactDefinition = toscaArtifacts.get(ToscaExportHandler.ASSET_TOSCA_TEMPLATE);
510                 if (artifactDefinition != null) {
511                     Map<String, Map<String, String>> importsListMember = new HashMap<>();
512                     Map<String, String> interfaceFiles = new HashMap<>();
513                     interfaceFiles.put(IMPORTS_FILE_KEY, getInterfaceFilename(artifactDefinition.getArtifactName()));
514                     StringBuilder keyNameBuilder = new StringBuilder();
515                     keyNameBuilder.append(component.getComponentType().toString().toLowerCase()).append("-")
516                         .append(component.getName()).append("-interface");
517                     importsListMember.put(keyNameBuilder.toString(), interfaceFiles);
518                     additionalImports.add(importsListMember);
519                 }
520             }
521             List<ComponentInstance> componentInstances = component.getComponentInstances();
522             if (componentInstances != null && !componentInstances.isEmpty()) {
523                 componentInstances.forEach(ci -> createDependency(componentCache, additionalImports, dependecies, ci));
524             }
525             toscaTemplate.setDependencies(dependecies);
526             toscaTemplate.setImports(additionalImports);
527         } else {
528             log.debug("currently imports supported for VF and service only");
529         }
530         return Either.left(new ImmutablePair<>(toscaTemplate, componentCache));
531     }
532
533     private List<Map<String, Map<String, String>>> getDefaultToscaImportConfig() {
534         return ConfigurationManager.getConfigurationManager().getConfiguration().getDefaultImports();
535     }
536
537     private void createDependency(final Map<String, Component> componentCache,
538                                   final List<Map<String, Map<String, String>>> imports,
539                                   final List<Triple<String, String, Component>> dependencies,
540                                   final ComponentInstance componentInstance) {
541         log.debug("createDependency componentCache {}", componentCache);
542         final Component componentRI = componentCache.get(componentInstance.getComponentUid());
543         if (componentRI == null) {
544             // all resource must be only once!
545             final Either<Component, StorageOperationStatus> resource = toscaOperationFacade
546                 .getToscaFullElement(componentInstance.getComponentUid());
547             if ((resource.isRight()) && (log.isDebugEnabled())) {
548                 log.debug("Failed to fetch resource with id {} for instance {}", componentInstance.getComponentUid(),
549                     componentInstance.getUniqueId());
550                 return;
551             }
552             final Component fetchedComponent = resource.left().value();
553             setComponentCache(componentCache, componentInstance, fetchedComponent);
554             addDependencies(imports, dependencies, fetchedComponent);
555         }
556     }
557
558     /**
559      * Sets a componentCache from the given component/resource.
560      */
561     private void setComponentCache(final Map<String, Component> componentCache,
562                                    final ComponentInstance componentInstance,
563                                    final Component fetchedComponent) {
564         componentCache.put(fetchedComponent.getUniqueId(), fetchedComponent);
565         if (componentInstance.getOriginType() == OriginTypeEnum.ServiceProxy || componentInstance.getOriginType() == OriginTypeEnum.ServiceSubstitution) {
566             final Either<Component, StorageOperationStatus> sourceService = toscaOperationFacade
567                 .getToscaFullElement(componentInstance.getSourceModelUid());
568             if (sourceService.isRight() && (log.isDebugEnabled())) {
569                 log.debug("Failed to fetch source service with id {} for proxy {}",
570                     componentInstance.getSourceModelUid(), componentInstance.getUniqueId());
571             }
572             final Component fetchedSource = sourceService.left().value();
573             componentCache.put(fetchedSource.getUniqueId(), fetchedSource);
574         }
575     }
576
577     /**
578      * Retrieves all derived_from nodes and stores it in a predictable order.
579      */
580     private void addDependencies(final List<Map<String, Map<String, String>>> imports,
581                                  final List<Triple<String, String, Component>> dependencies,
582                                  final Component fetchedComponent) {
583         final Set<Component> componentsList = new LinkedHashSet<>();
584         if (fetchedComponent instanceof Resource) {
585             log.debug("fetchedComponent is a resource {}", fetchedComponent);
586
587             final Optional<Map<String, String>> derivedFromMapOfIdToName = getDerivedFromMapOfIdToName(fetchedComponent,
588                 componentsList);
589             if (derivedFromMapOfIdToName.isPresent()) {
590                 derivedFromMapOfIdToName.get().entrySet().forEach(entry -> {
591                     log.debug("Started entry.getValue() : {}", entry.getValue());
592                     if (!NATIVE_ROOT.equals(entry.getValue())) {
593                         Either<Resource, StorageOperationStatus> resourcefetched = toscaOperationFacade
594                             .getToscaElement(entry.getKey());
595                         if (resourcefetched != null && resourcefetched.isLeft()) {
596                             componentsList.add(resourcefetched.left().value());
597                         }
598                     }
599                 });
600             }
601             setImports(imports, dependencies, componentsList);
602         }
603     }
604
605     /**
606      * Returns all derived_from nodes found.
607      */
608     private Optional<Map<String, String>> getDerivedFromMapOfIdToName(final Component fetchedComponent,
609                                                                       final Set<Component> componentsList) {
610         final Resource parentResource = (Resource) fetchedComponent;
611         Map<String, String> derivedFromMapOfIdToName = new HashMap<>();
612         if (CollectionUtils.isNotEmpty(parentResource.getComponentInstances())) {
613             componentsList.add(fetchedComponent);
614             for (final ComponentInstance componentInstance : parentResource.getComponentInstances()) {
615                 final Either<Resource, StorageOperationStatus> resourcefetched = toscaOperationFacade
616                     .getToscaElement(componentInstance.getComponentUid());
617                 if (resourcefetched != null && resourcefetched.isLeft()) {
618                     final Map<String, String> derivedWithId = resourcefetched.left().value()
619                         .getDerivedFromMapOfIdToName();
620                     if (MapUtils.isNotEmpty(derivedWithId)) {
621                         derivedFromMapOfIdToName.putAll(derivedWithId);
622                     }
623                 }
624             }
625         } else {
626             derivedFromMapOfIdToName = parentResource.getDerivedFromMapOfIdToName();
627         }
628         log.debug("Started derivedFromMapOfIdToName: {}", derivedFromMapOfIdToName);
629         return Optional.ofNullable(derivedFromMapOfIdToName);
630     }
631
632     /**
633      * Creates a resource map and adds it to the import list.
634      */
635     private void setImports(final List<Map<String, Map<String, String>>> imports,
636                             final List<Triple<String, String, Component>> dependencies,
637                             final Set<Component> componentsList) {
638         componentsList.forEach(component -> {
639             final Map<String, ArtifactDefinition> toscaArtifacts = component.getToscaArtifacts();
640             final ArtifactDefinition artifactDefinition = toscaArtifacts.get(ASSET_TOSCA_TEMPLATE);
641             if (artifactDefinition != null) {
642                 final Map<String, String> files = new HashMap<>();
643                 final String artifactName = artifactDefinition.getArtifactName();
644                 files.put(IMPORTS_FILE_KEY, artifactName);
645                 final StringBuilder keyNameBuilder = new StringBuilder();
646                 keyNameBuilder.append(component.getComponentType().toString().toLowerCase());
647                 keyNameBuilder.append("-");
648                 keyNameBuilder.append(component.getName());
649                 addImports(imports, keyNameBuilder, files);
650                 dependencies
651                     .add(new ImmutableTriple<String, String, Component>(artifactName, artifactDefinition.getEsId(),
652                         component));
653
654                 if (!ModelConverter.isAtomicComponent(component)) {
655                     final Map<String, String> interfaceFiles = new HashMap<>();
656                     interfaceFiles.put(IMPORTS_FILE_KEY, getInterfaceFilename(artifactName));
657                     keyNameBuilder.append("-interface");
658                     addImports(imports, keyNameBuilder, interfaceFiles);
659                 }
660             }
661         });
662     }
663
664     /**
665      * Adds the found resource to the import definition list.
666      */
667     private void addImports(final List<Map<String, Map<String, String>>> imports,
668                             final StringBuilder keyNameBuilder,
669                             final Map<String, String> files) {
670         final String mapKey = keyNameBuilder.toString();
671         if (imports.stream().allMatch(stringMapMap -> stringMapMap.get(mapKey) == null)) {
672             final Map<String, Map<String, String>> importsListMember = new HashMap<>();
673             importsListMember.put(keyNameBuilder.toString(), files);
674             imports.add(importsListMember);
675         }
676     }
677
678     public static String getInterfaceFilename(String artifactName) {
679         return artifactName.substring(0, artifactName.lastIndexOf('.')) + ToscaExportHandler.TOSCA_INTERFACE_NAME;
680     }
681
682     private Either<ToscaTemplate, ToscaError> convertNodeType(Map<String, Component> componentsCache,
683                                                               Component component, ToscaTemplate toscaNode,
684                                                               Map<String, ToscaNodeType> nodeTypes) {
685         return convertInterfaceNodeType(componentsCache, component, toscaNode, nodeTypes, false);
686     }
687
688     public Either<ToscaTemplate, ToscaError> convertInterfaceNodeType(Map<String, Component> componentsCache,
689                                                                       Component component, ToscaTemplate toscaNode,
690                                                                       Map<String, ToscaNodeType> nodeTypes,
691                                                                       boolean isAssociatedComponent) {
692         log.debug("start convert node type for {}", component.getUniqueId());
693         ToscaNodeType toscaNodeType = createNodeType(component);
694
695         Either<Map<String, InterfaceDefinition>, StorageOperationStatus> lifecycleTypeEither =
696             interfaceLifecycleOperation.getAllInterfaceLifecycleTypes();
697         if (lifecycleTypeEither.isRight()) {
698             log.debug("Failed to fetch all interface types :", lifecycleTypeEither.right().value());
699             return Either.right(ToscaError.GENERAL_ERROR);
700         }
701         List<String> allGlobalInterfaceTypes = lifecycleTypeEither.left().value()
702             .values()
703             .stream()
704             .map(InterfaceDataDefinition::getType)
705             .collect(Collectors.toList());
706         toscaNode.setInterface_types(addInterfaceTypeElement(component, allGlobalInterfaceTypes));
707
708         Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> dataTypesEither = dataTypeCache.getAll();
709         if (dataTypesEither.isRight()) {
710             log.debug("Failed to fetch all data types :", dataTypesEither.right().value());
711             return Either.right(ToscaError.GENERAL_ERROR);
712         }
713
714         Map<String, DataTypeDefinition> dataTypes = dataTypesEither.left().value();
715
716         List<InputDefinition> inputDef = component.getInputs();
717         Map<String, ToscaProperty> mergedProperties = new HashMap<>();
718         interfacesOperationsConverter
719             .addInterfaceDefinitionElement(component, toscaNodeType, dataTypes, isAssociatedComponent);
720         addInputsToProperties(dataTypes, inputDef, mergedProperties);
721
722         if (CollectionUtils.isNotEmpty(component.getProperties())) {
723             List<PropertyDefinition> properties = component.getProperties();
724             Map<String, ToscaProperty> convertedProperties = properties.stream()
725                 .map(propertyDefinition -> resolvePropertyValueFromInput(propertyDefinition, component.getInputs()))
726                 .collect(Collectors.toMap(PropertyDataDefinition::getName,
727                     property -> propertyConvertor.convertProperty(dataTypes, property,
728                         PropertyConvertor.PropertyType.PROPERTY)));
729             // merge component properties and inputs properties
730             mergedProperties.putAll(convertedProperties);
731         }
732         if (MapUtils.isNotEmpty(mergedProperties)) {
733             toscaNodeType.setProperties(mergedProperties);
734         }
735
736         /* convert private data_types */
737         List<DataTypeDefinition> privateDataTypes = component.getDataTypes();
738         if (CollectionUtils.isNotEmpty(privateDataTypes)) {
739             Map<String, ToscaDataType> toscaDataTypeMap = new HashMap<>();
740             for (DataTypeDefinition dataType : privateDataTypes) {
741                 log.debug("Emitting private data type: component.name={} dataType.name={}",
742                     component.getNormalizedName(), dataType.getName());
743                 ToscaDataType toscaDataType = new ToscaDataType();
744                 toscaDataType.setDerived_from(dataType.getDerivedFromName());
745                 toscaDataType.setDescription(dataType.getDescription());
746                 toscaDataType.setVersion(dataType.getVersion());
747                 if (CollectionUtils.isNotEmpty(dataType.getProperties())) {
748                     toscaDataType.setProperties(dataType.getProperties().stream()
749                         .collect(Collectors.toMap(
750                             s -> s.getName(),
751                             s -> propertyConvertor
752                                 .convertProperty(dataTypes, s, PropertyConvertor.PropertyType.PROPERTY)
753                         )));
754                 }
755                 toscaDataTypeMap.put(dataType.getName(), toscaDataType);
756             }
757             toscaNode.setData_types(toscaDataTypeMap);
758         }
759
760         // Extracted to method for code reuse
761         return convertReqCapAndTypeName(componentsCache, component, toscaNode, nodeTypes, toscaNodeType, dataTypes);
762     }
763
764     private Either<ToscaTemplate, ToscaError> convertReqCapAndTypeName(Map<String, Component> componentsCache,
765                                                                        Component component, ToscaTemplate toscaNode,
766                                                                        Map<String, ToscaNodeType> nodeTypes,
767                                                                        ToscaNodeType toscaNodeType,
768                                                                        Map<String, DataTypeDefinition> dataTypes) {
769         Either<ToscaNodeType, ToscaError> capabilities = convertCapabilities(componentsCache, component, toscaNodeType,
770             dataTypes);
771         if (capabilities.isRight()) {
772             return Either.right(capabilities.right().value());
773         }
774         toscaNodeType = capabilities.left().value();
775         log.debug("Capabilities converted for {}", component.getUniqueId());
776
777         Either<ToscaNodeType, ToscaError> requirements = capabilityRequirementConverter
778             .convertRequirements(componentsCache, component,
779                 toscaNodeType);
780         if (requirements.isRight()) {
781             return Either.right(requirements.right().value());
782         }
783         toscaNodeType = requirements.left().value();
784         log.debug("Requirements converted for {}", component.getUniqueId());
785
786         String toscaResourceName;
787         switch (component.getComponentType()) {
788             case RESOURCE:
789                 toscaResourceName = ((ResourceMetadataDataDefinition) component.getComponentMetadataDefinition()
790                     .getMetadataDataDefinition()).getToscaResourceName();
791                 break;
792             case SERVICE:
793                 toscaResourceName = SERVICE_NODE_TYPE_PREFIX
794                     + component.getComponentMetadataDefinition().getMetadataDataDefinition().getSystemName();
795                 break;
796             default:
797                 log.debug(NOT_SUPPORTED_COMPONENT_TYPE, component.getComponentType());
798                 return Either.right(ToscaError.NOT_SUPPORTED_TOSCA_TYPE);
799         }
800
801         nodeTypes.put(toscaResourceName, toscaNodeType);
802         toscaNode.setNode_types(nodeTypes);
803         log.debug("finish convert node type for {}", component.getUniqueId());
804         return Either.left(toscaNode);
805     }
806
807     protected Either<Map<String, ToscaNodeTemplate>, ToscaError> convertNodeTemplates(
808         Component component,
809         List<ComponentInstance> componentInstances,
810         Map<String, List<ComponentInstanceProperty>> componentInstancesProperties,
811         Map<String, List<ComponentInstanceInterface>> componentInstanceInterfaces,
812         Map<String, Component> componentCache, Map<String, DataTypeDefinition> dataTypes,
813         ToscaTopolgyTemplate topologyTemplate) {
814
815         Either<Map<String, ToscaNodeTemplate>, ToscaError> convertNodeTemplatesRes = null;
816         log.debug("start convert topology template for {} for type {}", component.getUniqueId(),
817             component.getComponentType());
818         Map<String, ToscaNodeTemplate> nodeTemplates = new HashMap<>();
819         Map<String, List<ComponentInstanceInput>> componentInstancesInputs = component.getComponentInstancesInputs();
820
821         Map<String, ToscaGroupTemplate> groupsMap = null;
822         for (ComponentInstance componentInstance : componentInstances) {
823             ToscaNodeTemplate nodeTemplate = new ToscaNodeTemplate();
824             if (MapUtils.isNotEmpty(componentInstance.getToscaArtifacts())) {
825                 nodeTemplate.setArtifacts(convertToNodeTemplateArtifacts(componentInstance.getToscaArtifacts()));
826             }
827             nodeTemplate.setType(componentInstance.getToscaComponentName());
828             nodeTemplate.setDirectives(componentInstance.getDirectives());
829             nodeTemplate.setNode_filter(convertToNodeTemplateNodeFilterComponent(componentInstance.getNodeFilter()));
830
831             Either<Component, Boolean> originComponentRes = capabilityRequirementConverter
832                 .getOriginComponent(componentCache, componentInstance);
833             if (originComponentRes.isRight()) {
834                 convertNodeTemplatesRes = Either.right(ToscaError.NODE_TYPE_REQUIREMENT_ERROR);
835                 break;
836             }
837             Either<ToscaNodeTemplate, ToscaError> requirements = convertComponentInstanceRequirements(component,
838                 componentInstance, component.getComponentInstancesRelations(), nodeTemplate,
839                 originComponentRes.left().value(), componentCache);
840             if (requirements.isRight()) {
841                 convertNodeTemplatesRes = Either.right(requirements.right().value());
842                 break;
843             }
844             String instanceUniqueId = componentInstance.getUniqueId();
845             log.debug("Component instance Requirements converted for instance {}", instanceUniqueId);
846
847             nodeTemplate = requirements.left().value();
848
849             Component originalComponent = componentCache.get(componentInstance.getActualComponentUid());
850
851             if (componentInstance.getOriginType() == OriginTypeEnum.ServiceProxy) {
852                 Component componentOfProxy = componentCache.get(componentInstance.getComponentUid());
853                 nodeTemplate.setMetadata(convertMetadata(componentOfProxy, true, componentInstance));
854             } else {
855                 nodeTemplate.setMetadata(convertMetadata(originalComponent, true, componentInstance));
856             }
857
858             Either<ToscaNodeTemplate, ToscaError> capabilities = capabilityRequirementConverter
859                 .convertComponentInstanceCapabilities(componentInstance, dataTypes, nodeTemplate);
860             if (capabilities.isRight()) {
861                 convertNodeTemplatesRes = Either.right(capabilities.right().value());
862                 break;
863             }
864             log.debug("Component instance Capabilities converted for instance {}", instanceUniqueId);
865
866             nodeTemplate = capabilities.left().value();
867             Map<String, Object> props = new HashMap<>();
868
869             if (originalComponent.getComponentType() == ComponentTypeEnum.RESOURCE) {
870                 // Adds the properties of parent component to map
871                 addPropertiesOfParentComponent(dataTypes, originalComponent, props);
872             }
873
874             if (null != componentInstancesProperties && componentInstancesProperties.containsKey(instanceUniqueId)) {
875                 addPropertiesOfComponentInstance(componentInstancesProperties, dataTypes, instanceUniqueId,
876                     props);
877             }
878
879             if (componentInstancesInputs != null && componentInstancesInputs.containsKey(instanceUniqueId)
880                 && !isComponentOfTypeServiceProxy(componentInstance)) {
881                 //For service proxy the inputs are already handled under instance properties above
882                 addComponentInstanceInputs(dataTypes, componentInstancesInputs, instanceUniqueId,
883                     props);
884             }
885             //M3[00001] - NODE TEMPLATE INTERFACES  - START
886             handleInstanceInterfaces(componentInstanceInterfaces, componentInstance, dataTypes, nodeTemplate,
887                 instanceUniqueId, component);
888             //M3[00001] - NODE TEMPLATE INTERFACES  - END
889             if (props != null && !props.isEmpty()) {
890                 nodeTemplate.setProperties(props);
891             }
892
893             List<GroupInstance> groupInstances = componentInstance.getGroupInstances();
894             if (groupInstances != null) {
895                 if (groupsMap == null) {
896                     groupsMap = new HashMap<>();
897                 }
898                 for (GroupInstance groupInst : groupInstances) {
899                     boolean addToTosca = true;
900
901                     List<String> artifacts = groupInst.getArtifacts();
902                     if (artifacts == null || artifacts.isEmpty()) {
903                         addToTosca = false;
904                     }
905
906                     if (addToTosca) {
907                         ToscaGroupTemplate toscaGroup = groupExportParser
908                             .getToscaGroupTemplate(groupInst, componentInstance.getInvariantName());
909                         groupsMap.put(groupInst.getName(), toscaGroup);
910                     }
911                 }
912             }
913
914             nodeTemplates.put(componentInstance.getName(), nodeTemplate);
915         }
916         if (groupsMap != null) {
917             log.debug("instance groups added");
918             topologyTemplate.addGroups(groupsMap);
919         }
920         if (component.getComponentType() == ComponentTypeEnum.SERVICE && isNotEmpty(
921             ((Service) component).getForwardingPaths())) {
922             log.debug("Starting converting paths for component {}, name {}", component.getUniqueId(),
923                 component.getName());
924             ForwardingPathToscaUtil
925                 .addForwardingPaths((Service) component, nodeTemplates, capabilityRequirementConverter, componentCache,
926                     toscaOperationFacade);
927             log.debug("Finished converting paths for component {}, name {}", component.getUniqueId(),
928                 component.getName());
929         }
930         if (convertNodeTemplatesRes == null) {
931             convertNodeTemplatesRes = Either.left(nodeTemplates);
932         }
933         log.debug("finish convert topology template for {} for type {}", component.getUniqueId(),
934             component.getComponentType());
935         return convertNodeTemplatesRes;
936     }
937
938     private void handleInstanceInterfaces(
939         Map<String, List<ComponentInstanceInterface>> componentInstanceInterfaces,
940         ComponentInstance componentInstance, Map<String, DataTypeDefinition> dataTypes, ToscaNodeTemplate nodeTemplate,
941         String instanceUniqueId,
942         Component parentComponent) {
943
944         Map<String, Object> interfaces;
945
946         // we need to handle service proxy interfaces
947         if (isComponentOfTypeServiceProxy(componentInstance)) {
948             if (MapUtils.isEmpty(componentInstanceInterfaces)
949                 || !componentInstanceInterfaces.containsKey(instanceUniqueId)) {
950                 interfaces = null;
951             } else {
952                 List<ComponentInstanceInterface> currServiceInterfaces =
953                     componentInstanceInterfaces.get(instanceUniqueId);
954
955                 Map<String, InterfaceDefinition> tmpInterfaces = new HashMap<>();
956                 currServiceInterfaces.forEach(instInterface -> tmpInterfaces.put(instInterface
957                     .getUniqueId(), instInterface));
958
959                 interfaces = interfacesOperationsConverter
960                     .getInterfacesMap(parentComponent, componentInstance, tmpInterfaces, dataTypes, true, true);
961             }
962         } else {
963             interfaces =
964                 getComponentInstanceInterfaceInstances(componentInstanceInterfaces,
965                     componentInstance, instanceUniqueId);
966         }
967         nodeTemplate.setInterfaces(interfaces);
968     }
969
970     private boolean isComponentOfTypeServiceProxy(ComponentInstance componentInstance) {
971         return Objects.nonNull(componentInstance.getOriginType())
972             && componentInstance.getOriginType().getValue().equals("Service Proxy");
973     }
974
975     //M3[00001] - NODE TEMPLATE INTERFACES  - START
976     private Map<String, Object> getComponentInstanceInterfaceInstances(
977         Map<String, List<ComponentInstanceInterface>> componentInstancesInterfaces,
978         ComponentInstance componentInstance,
979         String instanceUniqueId) {
980         if (MapUtils.isEmpty(componentInstancesInterfaces)) {
981             return null;
982         }
983
984         List<ComponentInstanceInterface> componentInstanceInterfaces =
985             componentInstancesInterfaces.get(instanceUniqueId);
986
987         if (CollectionUtils.isEmpty(componentInstanceInterfaces)) {
988             return null;
989         }
990
991         Map<String, Object> interfaces = new HashMap<>();
992         for (ComponentInstanceInterface componentInstanceInterface : componentInstanceInterfaces) {
993             interfaces.put(componentInstanceInterface.getInterfaceId(),
994                 removeOperationsKeyFromInterface(componentInstanceInterface.getInterfaceInstanceDataDefinition()));
995         }
996
997         componentInstance.setInterfaces(interfaces);
998
999         return interfaces;
1000     }
1001
1002     private void addComponentInstanceInputs(Map<String, DataTypeDefinition> dataTypes,
1003                                             Map<String, List<ComponentInstanceInput>> componentInstancesInputs,
1004                                             String instanceUniqueId, Map<String, Object> props) {
1005
1006         List<ComponentInstanceInput> instanceInputsList = componentInstancesInputs.get(instanceUniqueId);
1007         if (instanceInputsList != null) {
1008             instanceInputsList.forEach(input -> {
1009
1010                 Supplier<String> supplier = () -> input.getValue() != null && !Objects.isNull(input.getValue())
1011                     ? input.getValue() : input.getDefaultValue();
1012                 propertyConvertor.convertAndAddValue(dataTypes, props, input, supplier);
1013             });
1014         }
1015     }
1016
1017     private void addPropertiesOfComponentInstance(
1018         Map<String, List<ComponentInstanceProperty>> componentInstancesProperties,
1019         Map<String, DataTypeDefinition> dataTypes, String instanceUniqueId,
1020         Map<String, Object> props) {
1021
1022         if (isNotEmpty(componentInstancesProperties)) {
1023             componentInstancesProperties.get(instanceUniqueId)
1024                 // Converts and adds each value to property map
1025                 .forEach(prop -> propertyConvertor.convertAndAddValue(dataTypes, props, prop,
1026                     prop::getValue));
1027         }
1028     }
1029
1030     private void addPropertiesOfParentComponent(Map<String, DataTypeDefinition> dataTypes,
1031                                                 Component componentOfInstance, Map<String, Object> props) {
1032
1033         List<PropertyDefinition> componentProperties = componentOfInstance.getProperties();
1034         if (isNotEmpty(componentProperties)) {
1035             componentProperties.stream()
1036                 // Filters out properties with empty default values
1037                 .filter(prop -> StringUtils.isNotEmpty(prop.getDefaultValue()))
1038                 // Converts and adds each value to property map
1039                 .forEach(prop -> propertyConvertor.convertAndAddValue(dataTypes, props, prop,
1040                     prop::getDefaultValue));
1041         }
1042     }
1043
1044     /**
1045      *
1046      */
1047     private void convertAndAddValue(Map<String, DataTypeDefinition> dataTypes, ComponentInstance componentInstance,
1048                                     Map<String, Object> props, PropertyDefinition prop, Supplier<String> supplier) {
1049         Object convertedValue = convertValue(dataTypes, componentInstance, prop, supplier);
1050         if (!ToscaValueBaseConverter.isEmptyObjectValue(convertedValue)) {
1051             props.put(prop.getName(), convertedValue);
1052         }
1053     }
1054
1055     private <T extends PropertyDefinition> Object convertValue(Map<String, DataTypeDefinition> dataTypes,
1056                                                                ComponentInstance componentInstance, T input,
1057                                                                Supplier<String> supplier) {
1058         log.debug("Convert property or input value {} for instance {}", input.getName(),
1059             componentInstance.getUniqueId());
1060         String propertyType = input.getType();
1061         String innerType = null;
1062         if (input.getSchema() != null && input.getSchema().getProperty() != null) {
1063             innerType = input.getSchema().getProperty().getType();
1064         }
1065         return propertyConvertor.convertToToscaObject(input, supplier.get(), dataTypes, true);
1066     }
1067
1068     private ToscaNodeType createNodeType(Component component) {
1069         ToscaNodeType toscaNodeType = new ToscaNodeType();
1070         if (ModelConverter.isAtomicComponent(component)) {
1071             if (((Resource) component).getDerivedFrom() != null) {
1072                 toscaNodeType.setDerived_from(((Resource) component).getDerivedFrom().get(0));
1073             }
1074             toscaNodeType.setDescription(component.getDescription());
1075         } else {
1076             String derivedFrom = null != component.getDerivedFromGenericType() ? component.getDerivedFromGenericType()
1077                 : NATIVE_ROOT;
1078             toscaNodeType.setDerived_from(derivedFrom);
1079         }
1080         if (component instanceof Resource) {
1081             final List<AttributeDataDefinition> attributes = ((Resource) component).getAttributes();
1082             if (CollectionUtils.isNotEmpty(attributes)) {
1083                 final Map<String, Object> attributeDataDefinitionMap = new HashMap<>();
1084                 attributes.forEach(attributeDataDefinition ->
1085                     buildAttributeData(attributeDataDefinition, attributeDataDefinitionMap));
1086
1087                 toscaNodeType.setAttributes(attributeDataDefinitionMap);
1088             }
1089         }
1090         return toscaNodeType;
1091     }
1092
1093     private void buildAttributeData(final AttributeDataDefinition originalAttributeDataDefinition,
1094                                     final Map<String, Object> attributeDataDefinitionMap) {
1095
1096         attributeDataDefinitionMap.put(originalAttributeDataDefinition.getName(),
1097             new ObjectMapper().convertValue(new AttributeDefinition(
1098                 originalAttributeDataDefinition.getType(),
1099                 originalAttributeDataDefinition.getDescription(),
1100                 originalAttributeDataDefinition.get_default(),
1101                 originalAttributeDataDefinition.getStatus(),
1102                 originalAttributeDataDefinition.getEntry_schema()), Object.class));
1103     }
1104
1105     private Either<Map<String, Object>, ToscaError> createProxyInterfaceTypes(Component container) {
1106
1107         Map<String, Object> proxyInterfaceTypes = new HashMap<>();
1108         Either<Map<String, Object>, ToscaError> res = Either.left(proxyInterfaceTypes);
1109         List<ComponentInstance> componentInstances = container.getComponentInstances();
1110         if (CollectionUtils.isEmpty(componentInstances)) {
1111             return res;
1112         }
1113         Map<String, ComponentInstance> serviceProxyInstanceList = new HashMap<>();
1114         componentInstances.stream()
1115             .filter(this::isComponentOfTypeServiceProxy)
1116             .forEach(inst -> serviceProxyInstanceList.put(inst.getToscaComponentName(), inst));
1117         if (MapUtils.isEmpty(serviceProxyInstanceList)) {
1118             return res;
1119         }
1120         for (Entry<String, ComponentInstance> entryProxy : serviceProxyInstanceList.entrySet()) {
1121             Component serviceComponent;
1122             ComponentParametersView componentParametersView = new ComponentParametersView();
1123             componentParametersView.disableAll();
1124             componentParametersView.setIgnoreInterfaces(false);
1125             Either<Component, StorageOperationStatus> service = toscaOperationFacade
1126                 .getToscaElement(entryProxy.getValue().getSourceModelUid(), componentParametersView);
1127             if (service.isRight()) {
1128                 log.debug("Failed to fetch original service component with id {} for instance {}",
1129                     entryProxy.getValue().getSourceModelUid(), entryProxy.getValue().getName());
1130                 return Either.right(ToscaError.GENERAL_ERROR);
1131             } else {
1132                 serviceComponent = service.left().value();
1133             }
1134
1135             Either<Map<String, InterfaceDefinition>, StorageOperationStatus> lifecycleTypeEither =
1136                 interfaceLifecycleOperation.getAllInterfaceLifecycleTypes();
1137             if (lifecycleTypeEither.isRight()) {
1138                 log.debug("Failed to retrieve global interface types :", lifecycleTypeEither.right().value());
1139                 return Either.right(ToscaError.GENERAL_ERROR);
1140             }
1141
1142             List<String> allGlobalInterfaceTypes = lifecycleTypeEither.left().value().values().stream()
1143                 .map(InterfaceDataDefinition::getType)
1144                 .collect(Collectors.toList());
1145             //Add interface types for local interfaces in the original service component for proxy
1146             Map<String, Object> localInterfaceTypes = addInterfaceTypeElement(serviceComponent,
1147                 allGlobalInterfaceTypes);
1148             if (MapUtils.isNotEmpty(localInterfaceTypes)) {
1149                 proxyInterfaceTypes.putAll(localInterfaceTypes);
1150             }
1151
1152         }
1153         return Either.left(proxyInterfaceTypes);
1154     }
1155
1156     private Either<Map<String, ToscaNodeType>, ToscaError> createProxyNodeTypes(Map<String, Component> componentCache,
1157                                                                                 Component container) {
1158
1159         Map<String, ToscaNodeType> nodeTypesMap = new HashMap<>();
1160         Either<Map<String, ToscaNodeType>, ToscaError> res = Either.left(nodeTypesMap);
1161
1162         List<ComponentInstance> componentInstances = container.getComponentInstances();
1163
1164         if (componentInstances == null || componentInstances.isEmpty()) {
1165             return res;
1166         }
1167         Map<String, ComponentInstance> serviceProxyInstanceList = new HashMap<>();
1168         List<ComponentInstance> proxyInst = componentInstances.stream()
1169             .filter(p -> p.getOriginType().name().equals(OriginTypeEnum.ServiceProxy.name()))
1170             .collect(Collectors.toList());
1171         if (proxyInst != null && !proxyInst.isEmpty()) {
1172             for (ComponentInstance inst : proxyInst) {
1173                 serviceProxyInstanceList.put(inst.getToscaComponentName(), inst);
1174             }
1175         }
1176
1177         if (serviceProxyInstanceList.isEmpty()) {
1178             return res;
1179         }
1180         Either<Resource, StorageOperationStatus> serviceProxyOrigin = toscaOperationFacade
1181             .getLatestByName("serviceProxy");
1182         if (serviceProxyOrigin.isRight()) {
1183             log.debug("Failed to fetch normative service proxy resource by tosca name, error {}",
1184                 serviceProxyOrigin.right().value());
1185             return Either.right(ToscaError.NOT_SUPPORTED_TOSCA_TYPE);
1186         }
1187         Component origComponent = serviceProxyOrigin.left().value();
1188
1189         for (Entry<String, ComponentInstance> entryProxy : serviceProxyInstanceList.entrySet()) {
1190             Component serviceComponent = null;
1191             ComponentParametersView componentParametersView = new ComponentParametersView();
1192             componentParametersView.disableAll();
1193             componentParametersView.setIgnoreCategories(false);
1194             componentParametersView.setIgnoreProperties(false);
1195             componentParametersView.setIgnoreInputs(false);
1196             componentParametersView.setIgnoreInterfaces(false);
1197             componentParametersView.setIgnoreRequirements(false);
1198             Either<Component, StorageOperationStatus> service = toscaOperationFacade
1199                 .getToscaElement(entryProxy.getValue().getSourceModelUid(), componentParametersView);
1200             if (service.isRight()) {
1201                 log.debug("Failed to fetch resource with id {} for instance {}",
1202                     entryProxy.getValue().getSourceModelUid(), entryProxy.getValue().getName());
1203             } else {
1204                 serviceComponent = service.left().value();
1205             }
1206
1207             ToscaNodeType toscaNodeType = createProxyNodeType(componentCache, origComponent, serviceComponent,
1208                 entryProxy.getValue());
1209             nodeTypesMap.put(entryProxy.getKey(), toscaNodeType);
1210         }
1211
1212         return Either.left(nodeTypesMap);
1213     }
1214     
1215     private void createServiceSubstitutionNodeTypes(final Map<String, Component> componentCache,
1216             final Component container, final ToscaTemplate toscaNode) {
1217         final List<ComponentInstance> componentInstances = container.getComponentInstances();
1218
1219         if (CollectionUtils.isEmpty(componentInstances)) {
1220             return;
1221         }
1222         final List<ComponentInstance> serviceSubstitutionInstanceList = componentInstances.stream()
1223                .filter(p -> p.getOriginType().name().equals(OriginTypeEnum.ServiceSubstitution.name()))
1224                .collect(Collectors.toList());
1225         if (CollectionUtils.isNotEmpty(serviceSubstitutionInstanceList)) {
1226             for (ComponentInstance inst : serviceSubstitutionInstanceList) {
1227                 final Map<String, ToscaNodeType> nodeTypes = toscaNode.getNode_types() == null ? new HashMap<>() : toscaNode.getNode_types();
1228                 convertInterfaceNodeType(new HashMap<>(), componentCache.get(inst.getSourceModelUid()), toscaNode, nodeTypes, true);
1229             }
1230         }  
1231     }
1232
1233     private ToscaNodeType createProxyNodeType(Map<String, Component> componentCache, Component origComponent,
1234                                               Component proxyComponent, ComponentInstance instance) {
1235         ToscaNodeType toscaNodeType = new ToscaNodeType();
1236         String derivedFrom = ((Resource) origComponent).getToscaResourceName();
1237
1238         toscaNodeType.setDerived_from(derivedFrom);
1239         Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> dataTypesEither = dataTypeCache.getAll();
1240         if (dataTypesEither.isRight()) {
1241             log.debug("Failed to retrieve all data types {}", dataTypesEither.right().value());
1242         }
1243         Map<String, DataTypeDefinition> dataTypes = dataTypesEither.left().value();
1244         Map<String, ToscaCapability> capabilities = this.capabilityRequirementConverter
1245             .convertProxyCapabilities(componentCache, instance, dataTypes);
1246
1247         if (MapUtils.isNotEmpty(capabilities)) {
1248             toscaNodeType.setCapabilities(capabilities);
1249         }
1250         List<Map<String, ToscaRequirement>> proxyNodeTypeRequirements = this.capabilityRequirementConverter
1251             .convertProxyRequirements(componentCache, instance);
1252         if (CollectionUtils.isNotEmpty(proxyNodeTypeRequirements)) {
1253             toscaNodeType.setRequirements(proxyNodeTypeRequirements);
1254         }
1255         Optional<Map<String, ToscaProperty>> proxyProperties = getProxyNodeTypeProperties(proxyComponent, dataTypes);
1256         proxyProperties.ifPresent(toscaNodeType::setProperties);
1257
1258         Optional<Map<String, Object>> proxyInterfaces = getProxyNodeTypeInterfaces(proxyComponent, dataTypes);
1259         proxyInterfaces.ifPresent(toscaNodeType::setInterfaces);
1260
1261         return toscaNodeType;
1262     }
1263
1264     private Either<ToscaNodeTemplate, ToscaError> convertComponentInstanceRequirements(Component component,
1265                                                                                        ComponentInstance componentInstance,
1266                                                                                        List<RequirementCapabilityRelDef> relations,
1267                                                                                        ToscaNodeTemplate nodeTypeTemplate,
1268                                                                                        Component originComponent,
1269                                                                                        Map<String, Component> componentCache) {
1270
1271         List<Map<String, ToscaTemplateRequirement>> toscaRequirements = new ArrayList<>();
1272         if (!addRequirements(component, componentInstance, relations, originComponent, toscaRequirements,
1273             componentCache)) {
1274             log.debug("Failed to convert component instance requirements for the component instance {}. ",
1275                 componentInstance.getName());
1276             return Either.right(ToscaError.NODE_TYPE_REQUIREMENT_ERROR);
1277         }
1278         if (!toscaRequirements.isEmpty()) {
1279             nodeTypeTemplate.setRequirements(toscaRequirements);
1280         }
1281         log.debug("Finished to convert requirements for the node type {} ", componentInstance.getName());
1282         return Either.left(nodeTypeTemplate);
1283     }
1284
1285     private boolean addRequirements(Component component, ComponentInstance componentInstance,
1286                                     List<RequirementCapabilityRelDef> relations, Component originComponent,
1287                                     List<Map<String, ToscaTemplateRequirement>> toscaRequirements,
1288                                     Map<String, Component> componentCache) {
1289         List<RequirementCapabilityRelDef> filteredRelations = relations.stream()
1290             .filter(p -> componentInstance.getUniqueId().equals(p.getFromNode())).collect(Collectors.toList());
1291         return isEmpty(filteredRelations) ||
1292             filteredRelations.stream()
1293                 .allMatch(
1294                     rel -> addRequirement(componentInstance, originComponent, component.getComponentInstances(), rel,
1295                         toscaRequirements, componentCache));
1296     }
1297
1298     private boolean addRequirement(ComponentInstance fromInstance, Component fromOriginComponent,
1299                                    List<ComponentInstance> instancesList, RequirementCapabilityRelDef rel,
1300                                    List<Map<String, ToscaTemplateRequirement>> toscaRequirements,
1301                                    Map<String, Component> componentCache) {
1302
1303         boolean result = true;
1304         Map<String, List<RequirementDefinition>> reqMap = fromOriginComponent.getRequirements();
1305         RelationshipInfo reqAndRelationshipPair = rel.getRelationships().get(0).getRelation();
1306         Either<Component, StorageOperationStatus> getOriginRes = null;
1307         Optional<RequirementDefinition> reqOpt = Optional.empty();
1308         Component toOriginComponent = null;
1309         Optional<CapabilityDefinition> capOpt = Optional.empty();
1310
1311         ComponentInstance toInstance = instancesList.stream().filter(i -> rel.getToNode().equals(i.getUniqueId()))
1312             .findFirst().orElse(null);
1313         if (toInstance == null) {
1314             log.debug("Failed to find a relation from the node {} to the node {}", fromInstance.getName(),
1315                 rel.getToNode());
1316             result = false;
1317         }
1318         if (result) {
1319             reqOpt = findRequirement(fromOriginComponent, reqMap, reqAndRelationshipPair, fromInstance.getUniqueId());
1320             if (!reqOpt.isPresent()) {
1321                 log.debug("Failed to find a requirement with uniqueId {} on a component with uniqueId {}",
1322                     reqAndRelationshipPair.getRequirementUid(), fromOriginComponent.getUniqueId());
1323                 result = false;
1324             }
1325         }
1326         if (result) {
1327             ComponentParametersView filter = new ComponentParametersView(true);
1328             filter.setIgnoreComponentInstances(false);
1329             filter.setIgnoreCapabilities(false);
1330             filter.setIgnoreGroups(false);
1331             getOriginRes = toscaOperationFacade.getToscaElement(toInstance.getActualComponentUid(), filter);
1332             if (getOriginRes.isRight()) {
1333                 log.debug(
1334                     "Failed to build substituted name for the requirement {}. Failed to get an origin component with uniqueId {}",
1335                     reqOpt.get().getName(), toInstance.getActualComponentUid());
1336                 result = false;
1337             }
1338         }
1339         if (result) {
1340             toOriginComponent = getOriginRes.left().value();
1341             capOpt = toOriginComponent.getCapabilities().get(reqOpt.get().getCapability()).stream()
1342                 .filter(c -> isCapabilityBelongToRelation(reqAndRelationshipPair, c)).findFirst();
1343             if (!capOpt.isPresent()) {
1344                 capOpt = findCapability(reqAndRelationshipPair, toOriginComponent, fromOriginComponent, reqOpt.get());
1345                 if (!capOpt.isPresent()) {
1346                     result = false;
1347                     log.debug("Failed to find a capability with name {} on a component with uniqueId {}",
1348                         reqAndRelationshipPair.getCapability(), fromOriginComponent.getUniqueId());
1349                 }
1350             }
1351         }
1352         if (result) {
1353             result = buildAndAddRequirement(toscaRequirements, fromOriginComponent, toOriginComponent, capOpt.get(),
1354                 reqOpt.get(), reqAndRelationshipPair, toInstance, componentCache);
1355         }
1356         return result;
1357     }
1358
1359     private boolean isCapabilityBelongToRelation(RelationshipInfo reqAndRelationshipPair,
1360                                                  CapabilityDefinition capability) {
1361         return capability.getName().equals(reqAndRelationshipPair.getCapability()) && (capability.getOwnerId() != null
1362             && capability.getOwnerId().equals(reqAndRelationshipPair.getCapabilityOwnerId()));
1363     }
1364
1365     private Optional<CapabilityDefinition> findCapability(RelationshipInfo reqAndRelationshipPair,
1366                                                           Component toOriginComponent, Component fromOriginComponent,
1367                                                           RequirementDefinition requirement) {
1368         Optional<CapabilityDefinition> cap = toOriginComponent.getCapabilities().get(requirement.getCapability())
1369             .stream().filter(c -> c.getType().equals(requirement.getCapability())).findFirst();
1370         if (!cap.isPresent()) {
1371             log.debug("Failed to find a capability with name {} on a component with uniqueId {}",
1372                 reqAndRelationshipPair.getCapability(), fromOriginComponent.getUniqueId());
1373         }
1374         return cap;
1375     }
1376
1377     private boolean buildAndAddRequirement(List<Map<String, ToscaTemplateRequirement>> toscaRequirements,
1378                                            Component fromOriginComponent, Component toOriginComponent,
1379                                            CapabilityDefinition capability, RequirementDefinition requirement,
1380                                            RelationshipInfo reqAndRelationshipPair, ComponentInstance toInstance,
1381                                            Map<String, Component> componentCache) {
1382         List<String> reducedPath = capability.getPath();
1383         if (capability.getOwnerId() != null) {
1384             reducedPath = capabilityRequirementConverter
1385                 .getReducedPathByOwner(capability.getPath(), capability.getOwnerId());
1386         }
1387         Either<String, Boolean> buildCapNameRes = capabilityRequirementConverter.buildSubstitutedName(componentCache,
1388             toOriginComponent, reducedPath, reqAndRelationshipPair.getCapability(), capability.getPreviousName());
1389         if (buildCapNameRes.isRight()) {
1390             log.debug(
1391                 "Failed to build a substituted capability name for the capability with name {} on a component with uniqueId {}",
1392                 reqAndRelationshipPair.getCapability(), fromOriginComponent.getUniqueId());
1393             return false;
1394         }
1395         Either<String, Boolean> buildReqNameRes = capabilityRequirementConverter
1396             .buildSubstitutedName(componentCache, fromOriginComponent,
1397                 requirement.getPath(), reqAndRelationshipPair.getRequirement(), requirement.getPreviousName());
1398         if (buildReqNameRes.isRight()) {
1399             log.debug(
1400                 "Failed to build a substituted requirement name for the requirement with name {} on a component with uniqueId {}",
1401                 reqAndRelationshipPair.getRequirement(), fromOriginComponent.getUniqueId());
1402             return false;
1403         }
1404         ToscaTemplateRequirement toscaRequirement = new ToscaTemplateRequirement();
1405         Map<String, ToscaTemplateRequirement> toscaReqMap = new HashMap<>();
1406         toscaRequirement.setNode(toInstance.getName());
1407         toscaRequirement.setCapability(buildCapNameRes.left().value());
1408         toscaReqMap.put(buildReqNameRes.left().value(), toscaRequirement);
1409         toscaRequirements.add(toscaReqMap);
1410         return true;
1411     }
1412
1413     private Optional<RequirementDefinition> findRequirement(Component fromOriginComponent,
1414                                                             Map<String, List<RequirementDefinition>> reqMap,
1415                                                             RelationshipInfo reqAndRelationshipPair,
1416                                                             String fromInstanceId) {
1417         for (List<RequirementDefinition> reqList : reqMap.values()) {
1418             Optional<RequirementDefinition> reqOpt = reqList.stream().filter(
1419                 r -> isRequirementBelongToRelation(fromOriginComponent, reqAndRelationshipPair, r, fromInstanceId))
1420                 .findFirst();
1421             if (reqOpt.isPresent()) {
1422                 return reqOpt;
1423             }
1424         }
1425         return Optional.empty();
1426     }
1427
1428     /**
1429      * Allows detecting the requirement belonging to the received relationship The detection logic is: A requirement
1430      * belongs to a relationship IF 1.The name of the requirement equals to the "requirement" field of the relation; AND
1431      * 2. In case of a non-atomic resource, OwnerId of the requirement equals to requirementOwnerId of the relation OR
1432      * uniqueId of toInstance equals to capabilityOwnerId of the relation
1433      */
1434     private boolean isRequirementBelongToRelation(Component originComponent, RelationshipInfo reqAndRelationshipPair,
1435                                                   RequirementDefinition requirement, String fromInstanceId) {
1436         if (!StringUtils.equals(requirement.getName(), reqAndRelationshipPair.getRequirement())) {
1437             log.debug("Failed to find a requirement with name {} and  reqAndRelationshipPair {}",
1438                 requirement.getName(), reqAndRelationshipPair.getRequirement());
1439             return false;
1440         }
1441         return ModelConverter.isAtomicComponent(originComponent) ||
1442             isRequirementBelongToOwner(reqAndRelationshipPair, requirement, fromInstanceId, originComponent);
1443     }
1444
1445     private boolean isRequirementBelongToOwner(RelationshipInfo reqAndRelationshipPair,
1446                                                RequirementDefinition requirement, String fromInstanceId,
1447                                                Component originComponent) {
1448         return StringUtils.equals(requirement.getOwnerId(), reqAndRelationshipPair.getRequirementOwnerId())
1449             || (isCvfc(originComponent) && StringUtils
1450             .equals(fromInstanceId, reqAndRelationshipPair.getRequirementOwnerId())
1451             || StringUtils.equals(requirement.getOwnerId(), originComponent.getUniqueId()));
1452     }
1453
1454     private boolean isCvfc(Component component) {
1455         return component.getComponentType() == ComponentTypeEnum.RESOURCE &&
1456             ((Resource) component).getResourceType() == ResourceTypeEnum.CVFC;
1457     }
1458
1459     private Either<SubstitutionMapping, ToscaError> convertCapabilities(Component component,
1460                                                                         SubstitutionMapping substitutionMappings,
1461                                                                         Map<String, Component> componentCache) {
1462
1463         Either<SubstitutionMapping, ToscaError> result = Either.left(substitutionMappings);
1464         Either<Map<String, String[]>, ToscaError> toscaCapabilitiesRes = capabilityRequirementConverter
1465             .convertSubstitutionMappingCapabilities(componentCache, component);
1466         if (toscaCapabilitiesRes.isRight()) {
1467             result = Either.right(toscaCapabilitiesRes.right().value());
1468             log.debug("Failed convert capabilities for the component {}. ", component.getName());
1469         } else if (isNotEmpty(toscaCapabilitiesRes.left().value())) {
1470             substitutionMappings.setCapabilities(toscaCapabilitiesRes.left().value());
1471             log.debug("Finish convert capabilities for the component {}. ", component.getName());
1472         }
1473         log.debug("Finished to convert capabilities for the component {}. ", component.getName());
1474         return result;
1475     }
1476
1477     private Either<ToscaNodeType, ToscaError> convertCapabilities(Map<String, Component> componentsCache,
1478                                                                   Component component, ToscaNodeType nodeType,
1479                                                                   Map<String, DataTypeDefinition> dataTypes) {
1480         Map<String, ToscaCapability> toscaCapabilities = capabilityRequirementConverter
1481             .convertCapabilities(componentsCache, component,
1482                 dataTypes);
1483         if (!toscaCapabilities.isEmpty()) {
1484             nodeType.setCapabilities(toscaCapabilities);
1485         }
1486         log.debug("Finish convert Capabilities for node type");
1487
1488         return Either.left(nodeType);
1489     }
1490
1491     private Map<String, ToscaTemplateArtifact> convertToNodeTemplateArtifacts(
1492         Map<String, ToscaArtifactDataDefinition> artifacts) {
1493         if (artifacts == null) {
1494             return null;
1495         }
1496         Map<String, ToscaTemplateArtifact> arts = new HashMap<>();
1497         for (Map.Entry<String, ToscaArtifactDataDefinition> entry : artifacts.entrySet()) {
1498             ToscaTemplateArtifact artifact = new ToscaTemplateArtifact();
1499             artifact.setFile(entry.getValue().getFile());
1500             artifact.setType(entry.getValue().getType());
1501             arts.put(entry.getKey(), artifact);
1502         }
1503         return arts;
1504     }
1505
1506     protected NodeFilter convertToNodeTemplateNodeFilterComponent(CINodeFilterDataDefinition inNodeFilter) {
1507         if (inNodeFilter == null) {
1508             return null;
1509         }
1510         NodeFilter nodeFilter = new NodeFilter();
1511
1512         ListDataDefinition<RequirementNodeFilterCapabilityDataDefinition> origCapabilities =
1513             inNodeFilter.getCapabilities();
1514
1515         ListDataDefinition<RequirementNodeFilterPropertyDataDefinition> origProperties = inNodeFilter.getProperties();
1516
1517         List<Map<String, CapabilityFilter>> capabilitiesCopy = new ArrayList<>();
1518         List<Map<String, List<Object>>> propertiesCopy = new ArrayList<>();
1519
1520         copyNodeFilterCapabilitiesTemplate(origCapabilities, capabilitiesCopy);
1521         copyNodeFilterProperties(origProperties, propertiesCopy);
1522
1523         if (CollectionUtils.isNotEmpty(capabilitiesCopy)) {
1524             nodeFilter.setCapabilities(capabilitiesCopy);
1525         }
1526
1527         if (CollectionUtils.isNotEmpty(propertiesCopy)) {
1528             nodeFilter.setProperties(propertiesCopy);
1529         }
1530
1531         nodeFilter.setTosca_id(cloneToscaId(inNodeFilter.getTosca_id()));
1532
1533         nodeFilter = (NodeFilter) cloneObjectFromYml(nodeFilter, NodeFilter.class);
1534
1535         return nodeFilter;
1536     }
1537
1538     private NodeFilter convertToSubstitutionFilterComponent(
1539         final SubstitutionFilterDataDefinition substitutionFilterDataDefinition) {
1540
1541         if (substitutionFilterDataDefinition == null) {
1542             return null;
1543         }
1544         NodeFilter nodeFilter = new NodeFilter();
1545
1546         ListDataDefinition<RequirementSubstitutionFilterPropertyDataDefinition> origProperties =
1547                 substitutionFilterDataDefinition.getProperties();
1548         List<Map<String, List<Object>>> propertiesCopy = new ArrayList<>();
1549
1550         copySubstitutionFilterProperties(origProperties, propertiesCopy);
1551
1552         if (CollectionUtils.isNotEmpty(propertiesCopy)) {
1553             nodeFilter.setProperties(propertiesCopy);
1554         }
1555         nodeFilter.setTosca_id(cloneToscaId(substitutionFilterDataDefinition.getTosca_id()));
1556
1557         return (NodeFilter) cloneObjectFromYml(nodeFilter, NodeFilter.class);
1558     }
1559
1560     private Object cloneToscaId(Object toscaId) {
1561         return Objects.isNull(toscaId) ? null
1562             : cloneObjectFromYml(toscaId, toscaId.getClass());
1563     }
1564
1565     private Object cloneObjectFromYml(Object objToClone, Class classOfObj) {
1566         String objectAsYml = yamlUtil.objectToYaml(objToClone);
1567         return yamlUtil.yamlToObject(objectAsYml, classOfObj);
1568     }
1569
1570     private void copyNodeFilterCapabilitiesTemplate(
1571         ListDataDefinition<RequirementNodeFilterCapabilityDataDefinition> origCapabilities,
1572         List<Map<String, CapabilityFilter>> capabilitiesCopy) {
1573         if (origCapabilities == null || origCapabilities.getListToscaDataDefinition() == null ||
1574             origCapabilities.getListToscaDataDefinition().isEmpty()) {
1575             return;
1576         }
1577         for (RequirementNodeFilterCapabilityDataDefinition capability : origCapabilities.getListToscaDataDefinition()) {
1578             Map<String, CapabilityFilter> capabilityFilterCopyMap = new HashMap<>();
1579             CapabilityFilter capabilityFilter = new CapabilityFilter();
1580             List<Map<String, List<Object>>> propertiesCopy = new ArrayList<>();
1581             copyNodeFilterProperties(capability.getProperties(), propertiesCopy);
1582             capabilityFilter.setProperties(propertiesCopy);
1583             capabilityFilterCopyMap.put(capability.getName(), capabilityFilter);
1584             capabilitiesCopy.add(capabilityFilterCopyMap);
1585         }
1586     }
1587
1588     private void copyNodeFilterProperties(
1589         ListDataDefinition<RequirementNodeFilterPropertyDataDefinition> origProperties,
1590         List<Map<String, List<Object>>> propertiesCopy) {
1591         if (origProperties == null || origProperties.getListToscaDataDefinition() == null ||
1592             origProperties.isEmpty()) {
1593             return;
1594         }
1595         Map<String, List<Object>> propertyMapCopy = new HashMap<>();
1596         for (RequirementNodeFilterPropertyDataDefinition propertyDataDefinition : origProperties
1597             .getListToscaDataDefinition()) {
1598             for (String propertyInfoEntry : propertyDataDefinition.getConstraints()) {
1599                 Map propertyValObj = new YamlUtil().yamlToObject(propertyInfoEntry, Map.class);
1600                 String propertyName = propertyDataDefinition.getName();
1601                 if (propertyMapCopy.containsKey(propertyName)) {
1602                     addPropertyConstraintValueToList(propertyName, propertyValObj, propertyMapCopy.get(propertyName));
1603                 } else {
1604                     if (propertyName != null) {
1605                         List propsList = new ArrayList();
1606                         addPropertyConstraintValueToList(propertyName, propertyValObj, propsList);
1607                         propertyMapCopy.put(propertyName, propsList);
1608                     } else {
1609                         propertyMapCopy.putAll(propertyValObj);
1610                     }
1611                 }
1612             }
1613         }
1614         propertyMapCopy.entrySet().stream().forEach(entry ->
1615             addCalculatedConstraintsIntoPropertiesList(propertiesCopy, entry));
1616     }
1617
1618     private void copySubstitutionFilterProperties(
1619         final ListDataDefinition<RequirementSubstitutionFilterPropertyDataDefinition> origProperties,
1620         final List<Map<String, List<Object>>> propertiesCopy) {
1621         if (origProperties == null || origProperties.getListToscaDataDefinition() == null ||
1622             origProperties.isEmpty()) {
1623             return;
1624         }
1625         final Map<String, List<Object>> propertyMapCopy = new HashMap<>();
1626         for (final RequirementSubstitutionFilterPropertyDataDefinition propertyDataDefinition : origProperties
1627             .getListToscaDataDefinition()) {
1628             for (final String propertyInfoEntry : propertyDataDefinition.getConstraints()) {
1629                 final Map<String, List<Object>> propertyValObj = new YamlUtil().yamlToObject(propertyInfoEntry, Map.class);
1630                 final String propertyName = propertyDataDefinition.getName();
1631                 if (propertyMapCopy.containsKey(propertyName)) {
1632                     addPropertyConstraintValueToList(propertyName, propertyValObj, propertyMapCopy.get(propertyName));
1633                 } else {
1634                     if (propertyName != null) {
1635                         final List<Object> propsList = new ArrayList();
1636                         addPropertyConstraintValueToList(propertyName, propertyValObj, propsList);
1637                         propertyMapCopy.put(propertyName, propsList);
1638                     } else {
1639                         propertyMapCopy.putAll(propertyValObj);
1640                     }
1641                 }
1642             }
1643         }
1644         propertyMapCopy.entrySet().forEach(entry ->
1645             addCalculatedConstraintsIntoPropertiesList(propertiesCopy, entry));
1646     }
1647
1648     private void addPropertyConstraintValueToList(String propertyName, Map propertyValObj, List propsList) {
1649         if (propertyValObj.containsKey(propertyName)) {
1650             propsList.add(propertyValObj.get(propertyName));
1651         } else {
1652             propsList.add(propertyValObj);
1653         }
1654     }
1655
1656     private void addCalculatedConstraintsIntoPropertiesList(List<Map<String, List<Object>>> propertiesCopy,
1657                                                             Entry<String, List<Object>> entry) {
1658         Map<String, List<Object>> tempMap = new HashMap<>();
1659         tempMap.put(entry.getKey(), entry.getValue());
1660         propertiesCopy.add(tempMap);
1661     }
1662
1663     private static class CustomRepresenter extends Representer {
1664
1665         CustomRepresenter() {
1666             super();
1667             // null representer is exceptional and it is stored as an instance
1668             // variable.
1669             this.nullRepresenter = new RepresentNull();
1670
1671         }
1672
1673         @Override
1674         protected NodeTuple representJavaBeanProperty(Object javaBean, Property property, Object propertyValue,
1675                                                       Tag customTag) {
1676             if (propertyValue == null) {
1677                 return null;
1678             }
1679             // skip not relevant for Tosca property
1680             if ("dependencies".equals(property.getName())) {
1681                 return null;
1682             }
1683             removeDefaultP(propertyValue);
1684             NodeTuple defaultNode = super.representJavaBeanProperty(javaBean, property, propertyValue, customTag);
1685
1686             return "_defaultp_".equals(property.getName())
1687                 ? new NodeTuple(representData("default"), defaultNode.getValueNode()) : defaultNode;
1688         }
1689
1690         private void removeDefaultP(final Object propertyValue) {
1691             if (propertyValue instanceof Map) {
1692                 final Map mapPropertyValue = ((Map) propertyValue);
1693
1694                 final Iterator<Entry> iter = mapPropertyValue.entrySet().iterator();
1695                 Object defaultValue = null;
1696                 while (iter.hasNext()) {
1697                     final Map.Entry entry = iter.next();
1698
1699                     if ("_defaultp_".equals(entry.getKey())) {
1700                         defaultValue = entry.getValue();
1701                         iter.remove();
1702                     } else if (entry.getValue() instanceof Map) {
1703                         removeDefaultP(entry.getValue());
1704                     }
1705                 }
1706                 if (defaultValue != null) {
1707                     mapPropertyValue.putIfAbsent("default", defaultValue);
1708                 }
1709             }
1710         }
1711
1712         @Override
1713         protected MappingNode representJavaBean(Set<Property> properties, Object javaBean) {
1714             // remove the bean type from the output yaml (!! ...)
1715             if (!classTags.containsKey(javaBean.getClass())) {
1716                 addClassTag(javaBean.getClass(), Tag.MAP);
1717             }
1718
1719             return super.representJavaBean(properties, javaBean);
1720         }
1721
1722         private class RepresentNull implements Represent {
1723
1724             @Override
1725             public Node representData(Object data) {
1726                 // possible values are here http://yaml.org/type/null.html
1727                 return representScalar(Tag.NULL, "");
1728             }
1729         }
1730     }
1731
1732     private static class UnsortedPropertyUtils extends PropertyUtils {
1733
1734         @Override
1735         protected Set<Property> createPropertySet(Class type, BeanAccess bAccess)
1736             throws IntrospectionException {
1737             Collection<Property> fields = getPropertiesMap(type, BeanAccess.FIELD).values();
1738             return new LinkedHashSet<>(fields);
1739         }
1740     }
1741
1742     private Object removeOperationsKeyFromInterface(Object interfaceInstanceDataDefinition) {
1743         ObjectMapper objectMapper = new ObjectMapper();
1744         objectMapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false);
1745
1746         Map<String, Object> interfaceAsMap = ServiceUtils.getObjectAsMap(interfaceInstanceDataDefinition);
1747         Map<String, Object> operations = (Map<String, Object>) interfaceAsMap.remove("operations");
1748         interfaceAsMap.remove("empty");
1749
1750         if (MapUtils.isNotEmpty(operations)) {
1751             interfaceAsMap.putAll(operations);
1752         }
1753
1754         Object interfaceObject = objectMapper.convertValue(interfaceAsMap, Object.class);
1755
1756         return interfaceObject;
1757
1758     }
1759
1760     private Map<String, String[]> buildSubstitutionMappingPropertyMapping(final Component component) {
1761         if (component == null || CollectionUtils.isEmpty(component.getInputs())) {
1762             return Collections.emptyMap();
1763         }
1764         return component.getInputs().stream()
1765             .map(PropertyDataDefinition::getName)
1766             .collect(
1767                 Collectors.toMap(
1768                     inputName -> inputName,
1769                     inputName -> new String[]{inputName},
1770                     (inputName1, inputName2) -> inputName1)
1771             );
1772     }
1773
1774     private Map<String, String[]> buildSubstitutionMappingAttributesMapping(final Component component) {
1775         if (component == null || CollectionUtils.isEmpty(component.getOutputs())) {
1776             return Collections.emptyMap();
1777         }
1778         return component.getOutputs().stream()
1779             .map(PropertyDataDefinition::getName)
1780             .collect(
1781                 Collectors.toMap(
1782                     outputName -> outputName,
1783                     outputName -> new String[]{outputName},
1784                     (outputName1, outputName2) -> outputName1)
1785             );
1786     }
1787
1788     Optional<Map<String, ToscaProperty>> getProxyNodeTypeProperties(Component proxyComponent,
1789                                                                     Map<String, DataTypeDefinition>
1790                                                                         dataTypes) {
1791         if (Objects.isNull(proxyComponent)) {
1792             return Optional.empty();
1793         }
1794         Map<String, ToscaProperty> proxyProperties = new HashMap<>();
1795         addInputsToProperties(dataTypes, proxyComponent.getInputs(), proxyProperties);
1796         if (CollectionUtils.isNotEmpty(proxyComponent.getProperties())) {
1797             proxyProperties.putAll(proxyComponent.getProperties().stream()
1798                 .map(propertyDefinition -> resolvePropertyValueFromInput(propertyDefinition,
1799                     proxyComponent.getInputs()))
1800                 .collect(Collectors.toMap(PropertyDataDefinition::getName,
1801                     property -> propertyConvertor.convertProperty(dataTypes, property,
1802                         PropertyConvertor.PropertyType.PROPERTY))));
1803         }
1804         return MapUtils.isNotEmpty(proxyProperties) ? Optional.of(proxyProperties) : Optional.empty();
1805     }
1806
1807     void addInputsToProperties(Map<String, DataTypeDefinition> dataTypes,
1808                                List<InputDefinition> componentInputs,
1809                                Map<String, ToscaProperty> mergedProperties) {
1810         if (CollectionUtils.isEmpty(componentInputs)) {
1811             return;
1812         }
1813         for (InputDefinition input : componentInputs) {
1814             ToscaProperty property = propertyConvertor.convertProperty(dataTypes, input,
1815                 PropertyConvertor.PropertyType.INPUT);
1816             mergedProperties.put(input.getName(), property);
1817         }
1818     }
1819
1820     Optional<Map<String, Object>> getProxyNodeTypeInterfaces(Component proxyComponent,
1821                                                              Map<String, DataTypeDefinition> dataTypes) {
1822         if (Objects.isNull(proxyComponent) || MapUtils.isEmpty(proxyComponent.getInterfaces())) {
1823             return Optional.empty();
1824         }
1825         Map<String, InterfaceDefinition> proxyComponentInterfaces = proxyComponent.getInterfaces();
1826         //Unset artifact path for operation implementation for proxy node types as for operations with artifacts it is
1827         // always available in the proxy node template
1828         removeOperationImplementationForProxyNodeType(proxyComponentInterfaces);
1829         return Optional.ofNullable(interfacesOperationsConverter
1830             .getInterfacesMap(proxyComponent, null, proxyComponentInterfaces, dataTypes,
1831                 false, false));
1832     }
1833
1834     private static void removeOperationImplementationForProxyNodeType(
1835         Map<String, InterfaceDefinition> proxyComponentInterfaces) {
1836         if (MapUtils.isEmpty(proxyComponentInterfaces)) {
1837             return;
1838         }
1839         proxyComponentInterfaces.values().stream().map(InterfaceDataDefinition::getOperations)
1840             .filter(MapUtils::isNotEmpty)
1841             .forEach(operations -> operations.values().forEach(operation -> operation.setImplementation(null)));
1842     }
1843 }