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