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