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