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