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