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