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