Add dependent child service to service
[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.converters.ToscaValueBaseConverter;
49 import org.openecomp.sdc.be.tosca.model.*;
50 import org.openecomp.sdc.be.tosca.utils.ForwardingPathToscaUtil;
51 import org.openecomp.sdc.be.tosca.utils.InputConverter;
52 import org.openecomp.sdc.be.tosca.utils.InterfacesOperationsToscaUtil;
53 import org.openecomp.sdc.common.log.wrappers.Logger;
54 import org.openecomp.sdc.externalupload.utils.ServiceUtils;
55 import org.springframework.beans.factory.annotation.Autowired;
56 import org.yaml.snakeyaml.DumperOptions;
57 import org.yaml.snakeyaml.DumperOptions.FlowStyle;
58 import org.yaml.snakeyaml.Yaml;
59 import org.yaml.snakeyaml.introspector.BeanAccess;
60 import org.yaml.snakeyaml.introspector.Property;
61 import org.yaml.snakeyaml.introspector.PropertyUtils;
62 import org.yaml.snakeyaml.nodes.MappingNode;
63 import org.yaml.snakeyaml.nodes.Node;
64 import org.yaml.snakeyaml.nodes.NodeTuple;
65 import org.yaml.snakeyaml.nodes.Tag;
66 import org.yaml.snakeyaml.representer.Represent;
67 import org.yaml.snakeyaml.representer.Representer;
68
69 import java.beans.IntrospectionException;
70 import java.util.*;
71 import java.util.Map.Entry;
72 import java.util.function.Supplier;
73 import java.util.stream.Collectors;
74
75 import static org.apache.commons.collections.CollectionUtils.isEmpty;
76 import static org.apache.commons.collections.CollectionUtils.isNotEmpty;
77 import static org.apache.commons.collections.MapUtils.isNotEmpty;
78 import static org.apache.commons.lang.StringUtils.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 isAssociatedResourceComponent) {
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, isAssociatedResourceComponent);
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         return Either.left(toscaNode);
316     }
317
318   private void addGroupsToTopologyTemplate(Component component, ToscaTopolgyTemplate topologyTemplate) {
319
320
321         Map<String, ToscaGroupTemplate> groups = groupExportParser.getGroups(component);
322         if(groups!= null) {
323             topologyTemplate.addGroups(groups);
324         }
325     }
326
327     private void addPoliciesToTopologyTemplate(Component component, ToscaTopolgyTemplate topologyTemplate)
328             throws SdcResourceNotFoundException {
329         Map<String, ToscaPolicyTemplate> policies = policyExportParser.getPolicies(component);
330         if(policies!= null) {
331             topologyTemplate.addPolicies(policies);
332         }
333     }
334
335     private ToscaMetadata convertMetadata(Component component) {
336         return convertMetadata(component, false, null);
337     }
338
339     private ToscaMetadata convertMetadata(Component component, boolean isInstance,
340             ComponentInstance componentInstance) {
341         ToscaMetadata toscaMetadata = new ToscaMetadata();
342         toscaMetadata.setInvariantUUID(component.getInvariantUUID());
343         toscaMetadata.setUUID(component.getUUID());
344         toscaMetadata.setDescription(component.getDescription());
345         toscaMetadata.setName(component.getComponentMetadataDefinition().getMetadataDataDefinition().getName());
346
347         List<CategoryDefinition> categories = component.getCategories();
348         CategoryDefinition categoryDefinition = categories.get(0);
349         toscaMetadata.setCategory(categoryDefinition.getName());
350
351         if (isInstance) {
352             toscaMetadata.setVersion(component.getVersion());
353             toscaMetadata.setCustomizationUUID(componentInstance.getCustomizationUUID());
354             if (componentInstance.getSourceModelInvariant() != null
355                         && !componentInstance.getSourceModelInvariant().isEmpty()) {
356                 toscaMetadata.setVersion(componentInstance.getComponentVersion());
357                 toscaMetadata.setSourceModelInvariant(componentInstance.getSourceModelInvariant());
358                 toscaMetadata.setSourceModelUuid(componentInstance.getSourceModelUuid());
359                 toscaMetadata.setSourceModelName(componentInstance.getSourceModelName());
360                 toscaMetadata.setName(
361                         componentInstance.getSourceModelName() + " " + OriginTypeEnum.ServiceProxy.getDisplayValue());
362                 toscaMetadata.setDescription(componentInstance.getDescription());
363             }
364
365         }
366         switch (component.getComponentType()) {
367             case RESOURCE:
368                 Resource resource = (Resource) component;
369
370                 if (isInstance && componentInstance.getOriginType() == OriginTypeEnum.ServiceProxy) {
371                     toscaMetadata.setType(componentInstance.getOriginType().getDisplayValue());
372                 } else {
373                     toscaMetadata.setType(resource.getResourceType().name());
374                 }
375                 toscaMetadata.setSubcategory(categoryDefinition.getSubcategories().get(0).getName());
376                 toscaMetadata.setResourceVendor(resource.getVendorName());
377                 toscaMetadata.setResourceVendorRelease(resource.getVendorRelease());
378                 toscaMetadata.setResourceVendorModelNumber(resource.getResourceVendorModelNumber());
379                 break;
380             case SERVICE:
381                 Service service = (Service) component;
382                 toscaMetadata.setType(component.getComponentType().getValue());
383                 toscaMetadata.setServiceType(service.getServiceType());
384                 toscaMetadata.setServiceRole(service.getServiceRole());
385                 toscaMetadata.setEnvironmentContext(service.getEnvironmentContext());
386                 resolveInstantiationTypeAndSetItToToscaMetaData(toscaMetadata, service);
387                 if (!isInstance) {
388                     // DE268546
389                     toscaMetadata.setServiceEcompNaming(((Service) component).isEcompGeneratedNaming());
390                     toscaMetadata.setEcompGeneratedNaming(((Service) component).isEcompGeneratedNaming());
391                     toscaMetadata.setNamingPolicy(((Service) component).getNamingPolicy());
392                 }
393                 break;
394             default:
395                 log.debug(NOT_SUPPORTED_COMPONENT_TYPE, component.getComponentType());
396         }
397         return toscaMetadata;
398     }
399
400     private void resolveInstantiationTypeAndSetItToToscaMetaData(ToscaMetadata toscaMetadata, Service service) {
401         if (service.getInstantiationType() != null) {
402             toscaMetadata.setInstantiationType(service.getInstantiationType());
403         }
404         else {
405             toscaMetadata.setInstantiationType(StringUtils.EMPTY);
406         }
407     }
408
409     private Either<ImmutablePair<ToscaTemplate, Map<String, Component>>, ToscaError> fillImports(Component component,
410             ToscaTemplate toscaTemplate) {
411
412         if (null == DEFAULT_IMPORTS) {
413             log.debug(FAILED_TO_GET_DEFAULT_IMPORTS_CONFIGURATION);
414             return Either.right(ToscaError.GENERAL_ERROR);
415         }
416         Map<String, Component> componentCache = new HashMap<>();
417
418         if (!ModelConverter.isAtomicComponent(component)) {
419             List<Map<String, Map<String, String>>> additionalImports = toscaTemplate.getImports() == null
420                                                                                ? new ArrayList<>(DEFAULT_IMPORTS) : new ArrayList<>(toscaTemplate.getImports());
421
422             List<Triple<String, String, Component>> dependecies = new ArrayList<>();
423
424             Map<String, ArtifactDefinition> toscaArtifacts = component.getToscaArtifacts();
425             if (isNotEmpty(toscaArtifacts)) {
426                 ArtifactDefinition artifactDefinition = toscaArtifacts.get(ToscaExportHandler.ASSET_TOSCA_TEMPLATE);
427                 if(artifactDefinition != null) {
428                     Map<String, Map<String, String>> importsListMember = new HashMap<>();
429                     Map<String, String> interfaceFiles = new HashMap<>();
430                     interfaceFiles.put(IMPORTS_FILE_KEY, getInterfaceFilename(artifactDefinition.getArtifactName()));
431                     StringBuilder keyNameBuilder = new StringBuilder();
432                     keyNameBuilder.append(component.getComponentType().toString().toLowerCase()).append("-")
433                                   .append(component.getName()).append("-interface");
434                     importsListMember.put(keyNameBuilder.toString(), interfaceFiles);
435                     additionalImports.add(importsListMember);
436                 }
437             }
438             List<ComponentInstance> componentInstances = component.getComponentInstances();
439             if (componentInstances != null && !componentInstances.isEmpty()) {
440                 componentInstances.forEach(ci -> createDependency(componentCache, additionalImports, dependecies, ci));
441             }
442             toscaTemplate.setDependencies(dependecies);
443             toscaTemplate.setImports(additionalImports);
444         } else {
445             log.debug("currently imports supported for VF and service only");
446         }
447         return Either.left(new ImmutablePair<>(toscaTemplate, componentCache));
448     }
449
450     private void createDependency(Map<String, Component> componentCache, List<Map<String, Map<String, String>>> imports,
451             List<Triple<String, String, Component>> dependecies, ComponentInstance ci) {
452         Map<String, String> files = new HashMap<>();
453         Map<String, Map<String, String>> importsListMember = new HashMap<>();
454         StringBuilder keyNameBuilder;
455
456         Component componentRI = componentCache.get(ci.getComponentUid());
457         if (componentRI == null) {
458             // all resource must be only once!
459             Either<Component, StorageOperationStatus> resource = toscaOperationFacade
460                                                                          .getToscaFullElement(ci.getComponentUid());
461             if ((resource.isRight()) && (log.isDebugEnabled())) {
462                 log.debug("Failed to fetch resource with id {} for instance {}",ci.getComponentUid() ,ci.getUniqueId());
463                 return ;
464             }
465
466             Component fetchedComponent = resource.left().value();
467             componentCache.put(fetchedComponent.getUniqueId(), fetchedComponent);
468
469             if (ci.getOriginType() == OriginTypeEnum.ServiceProxy){
470                 Either<Component, StorageOperationStatus> sourceService = toscaOperationFacade
471                                                                                   .getToscaFullElement(ci.getSourceModelUid());
472                 if (sourceService.isRight() && (log.isDebugEnabled())) {
473                     log.debug("Failed to fetch source service with id {} for proxy {}", ci.getSourceModelUid(), ci.getUniqueId());
474                 }
475                 Component fetchedSource = sourceService.left().value();
476                 componentCache.put(fetchedSource.getUniqueId(), fetchedSource);
477             }
478
479             componentRI = fetchedComponent;
480
481             Map<String, ArtifactDefinition> toscaArtifacts = componentRI.getToscaArtifacts();
482             ArtifactDefinition artifactDefinition = toscaArtifacts.get(ASSET_TOSCA_TEMPLATE);
483             if (artifactDefinition != null) {
484                 String artifactName = artifactDefinition.getArtifactName();
485                 files.put(IMPORTS_FILE_KEY, artifactName);
486                 keyNameBuilder = new StringBuilder();
487                 keyNameBuilder.append(fetchedComponent.getComponentType().toString().toLowerCase());
488                 keyNameBuilder.append("-");
489                 keyNameBuilder.append(ci.getComponentName());
490                 importsListMember.put(keyNameBuilder.toString(), files);
491                 imports.add(importsListMember);
492                 dependecies.add(new ImmutableTriple<>(artifactName,
493                         artifactDefinition.getEsId(), fetchedComponent));
494
495                 if (!ModelConverter.isAtomicComponent(componentRI)) {
496                     importsListMember = new HashMap<>();
497                     Map<String, String> interfaceFiles = new HashMap<>();
498                     interfaceFiles.put(IMPORTS_FILE_KEY, getInterfaceFilename(artifactName));
499                     keyNameBuilder.append("-interface");
500                     importsListMember.put(keyNameBuilder.toString(), interfaceFiles);
501                     imports.add(importsListMember);
502                 }
503             }
504         }
505     }
506
507     public static String getInterfaceFilename(String artifactName) {
508         return artifactName.substring(0, artifactName.lastIndexOf('.')) + ToscaExportHandler.TOSCA_INTERFACE_NAME;
509     }
510
511     private Either<ToscaTemplate, ToscaError> convertNodeType(Map<String, Component> componentsCache, Component component, ToscaTemplate toscaNode,
512             Map<String, ToscaNodeType> nodeTypes) {
513         log.debug("start convert node type for {}", component.getUniqueId());
514         ToscaNodeType toscaNodeType = createNodeType(component);
515
516         Either<Map<String, DataTypeDefinition>, TitanOperationStatus> dataTypesEither = dataTypeCache.getAll();
517         if (dataTypesEither.isRight()) {
518             log.debug("Failed to fetch all data types :", dataTypesEither.right().value());
519             return Either.right(ToscaError.GENERAL_ERROR);
520         }
521
522         Map<String, DataTypeDefinition> dataTypes = dataTypesEither.left().value();
523         Either<ToscaNodeType, ToscaError> properties = propertyConvertor.convertProperties(component, toscaNodeType,
524                 dataTypes);
525         if (properties.isRight()) {
526             return Either.right(properties.right().value());
527         }
528         toscaNodeType = properties.left().value();
529         log.debug("Properties converted for {}", component.getUniqueId());
530
531         // Extracted to method for code reuse
532         return convertReqCapAndTypeName(componentsCache, component, toscaNode, nodeTypes, toscaNodeType, dataTypes);
533     }
534
535     private Either<ToscaTemplate, ToscaError> convertInterfaceNodeType(Map<String, Component> componentsCache,
536             Component component, ToscaTemplate toscaNode,
537             Map<String, ToscaNodeType> nodeTypes,
538             boolean isAssociatedResourceComponent) {
539         log.debug("start convert node type for {}", component.getUniqueId());
540         ToscaNodeType toscaNodeType = createNodeType(component);
541
542         Either<Map<String, InterfaceDefinition>, StorageOperationStatus> lifecycleTypeEither =
543                 interfaceLifecycleOperation.getAllInterfaceLifecycleTypes();
544         if(lifecycleTypeEither.isRight()){
545             log.debug("Failed to fetch all interface types :", lifecycleTypeEither.right().value());
546             return Either.right(ToscaError.GENERAL_ERROR);
547         }
548         List<String> allGlobalInterfaceTypes = lifecycleTypeEither.left().value()
549                                                                   .values()
550                                                                   .stream()
551                                                                   .map(interfaceDef -> interfaceDef.getType())
552                                                                   .collect(Collectors.toList());
553         toscaNode.setInterface_types(addInterfaceTypeElement(component, allGlobalInterfaceTypes));
554
555         Either<Map<String, DataTypeDefinition>, TitanOperationStatus> dataTypesEither = dataTypeCache.getAll();
556         if (dataTypesEither.isRight()) {
557             log.debug("Failed to fetch all data types :", dataTypesEither.right().value());
558             return Either.right(ToscaError.GENERAL_ERROR);
559         }
560
561         Map<String, DataTypeDefinition> dataTypes = dataTypesEither.left().value();
562
563         List<InputDefinition> inputDef = component.getInputs();
564         Map<String, ToscaProperty> mergedProperties = new HashMap<>();
565         addInterfaceDefinitionElement(component, toscaNodeType, dataTypes, isAssociatedResourceComponent);
566         if (inputDef != null) {
567             addInputsToProperties(dataTypes, inputDef, mergedProperties);
568         }
569
570         if(CollectionUtils.isNotEmpty(component.getProperties())) {
571             List<PropertyDefinition> properties = component.getProperties();
572             mergedProperties = properties.stream().collect(Collectors.toMap(
573                 PropertyDataDefinition::getName,
574                 property -> propertyConvertor.convertProperty(dataTypes, property, PropertyConvertor.PropertyType.PROPERTY)));
575         }
576         if (!mergedProperties.isEmpty()) {
577             toscaNodeType.setProperties(mergedProperties);
578         }
579         // Extracted to method for code reuse
580         return convertReqCapAndTypeName(componentsCache, component, toscaNode, nodeTypes, toscaNodeType, dataTypes);
581     }
582
583   private void addInputsToProperties(Map<String, DataTypeDefinition> dataTypes,
584                                      List<InputDefinition> inputDef,
585                                      Map<String, ToscaProperty> mergedProperties) {
586     for(InputDefinition input : inputDef) {
587       ToscaProperty property = propertyConvertor.convertProperty(dataTypes, input, PropertyConvertor.PropertyType.INPUT);
588       mergedProperties.put(input.getName(), property);
589     }
590   }
591
592     private Either<ToscaTemplate, ToscaError> convertReqCapAndTypeName(Map<String, Component> componentsCache, Component component, ToscaTemplate toscaNode,
593             Map<String, ToscaNodeType> nodeTypes, ToscaNodeType toscaNodeType,
594             Map<String, DataTypeDefinition> dataTypes) {
595         Either<ToscaNodeType, ToscaError> capabilities = convertCapabilities(componentsCache, component, toscaNodeType, dataTypes);
596         if (capabilities.isRight()) {
597             return Either.right(capabilities.right().value());
598         }
599         toscaNodeType = capabilities.left().value();
600         log.debug("Capabilities converted for {}", component.getUniqueId());
601
602         Either<ToscaNodeType, ToscaError> requirements = capabilityRequirementConverter.convertRequirements(componentsCache, component,
603                 toscaNodeType);
604         if (requirements.isRight()) {
605             return Either.right(requirements.right().value());
606         }
607         toscaNodeType = requirements.left().value();
608         log.debug("Requirements converted for {}", component.getUniqueId());
609
610         String toscaResourceName;
611         switch (component.getComponentType()) {
612             case RESOURCE:
613                 toscaResourceName = ((ResourceMetadataDataDefinition) component.getComponentMetadataDefinition()
614                                                                                .getMetadataDataDefinition()).getToscaResourceName();
615                 break;
616             case SERVICE:
617                 toscaResourceName = SERVICE_NODE_TYPE_PREFIX
618                                             + component.getComponentMetadataDefinition().getMetadataDataDefinition().getSystemName();
619                 break;
620             default:
621                 log.debug(NOT_SUPPORTED_COMPONENT_TYPE, component.getComponentType());
622                 return Either.right(ToscaError.NOT_SUPPORTED_TOSCA_TYPE);
623         }
624
625         nodeTypes.put(toscaResourceName, toscaNodeType);
626         toscaNode.setNode_types(nodeTypes);
627         log.debug("finish convert node type for {}", component.getUniqueId());
628         return Either.left(toscaNode);
629     }
630
631     protected Either<Map<String, ToscaNodeTemplate>, ToscaError> convertNodeTemplates(
632             Component component,
633             List<ComponentInstance> componentInstances,
634             Map<String, List<ComponentInstanceProperty>> componentInstancesProperties,
635             Map<String, List<ComponentInstanceInterface>> componentInstanceInterfaces,
636             Map<String, Component> componentCache, Map<String, DataTypeDefinition> dataTypes,
637             ToscaTopolgyTemplate topologyTemplate) {
638
639         Either<Map<String, ToscaNodeTemplate>, ToscaError> convertNodeTemplatesRes = null;
640         log.debug("start convert topology template for {} for type {}", component.getUniqueId(),
641                 component.getComponentType());
642         Map<String, ToscaNodeTemplate> nodeTemplates = new HashMap<>();
643         Map<String, List<ComponentInstanceInput>> componentInstancesInputs = component.getComponentInstancesInputs();
644
645         Map<String, ToscaGroupTemplate> groupsMap = null;
646         for (ComponentInstance componentInstance : componentInstances) {
647             ToscaNodeTemplate nodeTemplate = new ToscaNodeTemplate();
648             nodeTemplate.setType(componentInstance.getToscaComponentName());
649             nodeTemplate.setDirectives(componentInstance.getDirectives());
650             nodeTemplate.setNode_filter(convertToNodeTemplateNodeFilterComponent(componentInstance.getNodeFilter()));
651
652             Either<Component, Boolean> originComponentRes = capabilityRequirementConverter
653                                                                     .getOriginComponent(componentCache, componentInstance);
654             if (originComponentRes.isRight()) {
655                 convertNodeTemplatesRes = Either.right(ToscaError.NODE_TYPE_REQUIREMENT_ERROR);
656                 break;
657             }
658             Either<ToscaNodeTemplate, ToscaError> requirements = convertComponentInstanceRequirements(component,
659                     componentInstance, component.getComponentInstancesRelations(), nodeTemplate,
660                     originComponentRes.left().value(), componentCache);
661             if (requirements.isRight()) {
662                 convertNodeTemplatesRes = Either.right(requirements.right().value());
663                 break;
664             }
665             String instanceUniqueId = componentInstance.getUniqueId();
666             log.debug("Component instance Requirements converted for instance {}", instanceUniqueId);
667
668             nodeTemplate = requirements.left().value();
669
670             Component originalComponent = componentCache.get(componentInstance.getActualComponentUid());
671
672             if (componentInstance.getOriginType() == OriginTypeEnum.ServiceProxy){
673                 Component componentOfProxy = componentCache.get(componentInstance.getComponentUid());
674                 nodeTemplate.setMetadata(convertMetadata(componentOfProxy, true, componentInstance));
675             } else {
676                 nodeTemplate.setMetadata(convertMetadata(originalComponent, true, componentInstance));
677             }
678
679             Either<ToscaNodeTemplate, ToscaError> capabilities = capabilityRequirementConverter
680                                                                          .convertComponentInstanceCapabilities(componentInstance, dataTypes, nodeTemplate);
681             if (capabilities.isRight()) {
682                 convertNodeTemplatesRes = Either.right(capabilities.right().value());
683                 break;
684             }
685             log.debug("Component instance Capabilities converted for instance {}", instanceUniqueId);
686
687             nodeTemplate = capabilities.left().value();
688             Map<String, Object> props = new HashMap<>();
689
690             if (originalComponent.getComponentType() == ComponentTypeEnum.RESOURCE) {
691                 // Adds the properties of parent component to map
692                 addPropertiesOfParentComponent(dataTypes, originalComponent, props);
693             }
694
695             if (null != componentInstancesProperties && componentInstancesProperties.containsKey(instanceUniqueId)) {
696                 addPropertiesOfComponentInstance(componentInstancesProperties, dataTypes,
697                         instanceUniqueId, props);
698             }
699
700             if (componentInstancesInputs != null && componentInstancesInputs.containsKey(instanceUniqueId)) {
701                 addComponentInstanceInputs(dataTypes, componentInstancesInputs, instanceUniqueId,
702                         props);
703             }
704             //M3[00001] - NODE TEMPLATE INTERFACES  - START
705             handleInstanceInterfaces(componentInstanceInterfaces, componentInstance, dataTypes, nodeTemplate,
706                     instanceUniqueId, component);
707             //M3[00001] - NODE TEMPLATE INTERFACES  - END
708             if (props != null && !props.isEmpty()) {
709                 nodeTemplate.setProperties(props);
710             }
711
712             List<GroupInstance> groupInstances = componentInstance.getGroupInstances();
713             if (groupInstances != null) {
714                 if (groupsMap == null) {
715                     groupsMap = new HashMap<>();
716                 }
717                 for (GroupInstance groupInst : groupInstances) {
718                     boolean addToTosca = true;
719
720                     List<String> artifacts = groupInst.getArtifacts();
721                     if (artifacts == null || artifacts.isEmpty()) {
722                         addToTosca = false;
723                     }
724
725                     if (addToTosca) {
726                         ToscaGroupTemplate toscaGroup = groupExportParser.getToscaGroupTemplate(groupInst, componentInstance.getInvariantName());
727                         groupsMap.put(groupInst.getName(), toscaGroup);
728                     }
729                 }
730             }
731
732             nodeTemplates.put(componentInstance.getName(), nodeTemplate);
733         }
734         if (groupsMap != null) {
735             log.debug("instance groups added");
736             topologyTemplate.addGroups(groupsMap);
737         }
738         if (component.getComponentType() == ComponentTypeEnum.SERVICE && isNotEmpty(((Service) component).getForwardingPaths())) {
739             log.debug("Starting converting paths for component {}, name {}", component.getUniqueId(),
740                     component.getName());
741             ForwardingPathToscaUtil.addForwardingPaths((Service) component, nodeTemplates, capabilityRequirementConverter, componentCache, toscaOperationFacade);
742             log.debug("Finished converting paths for component {}, name {}", component.getUniqueId(),
743                     component.getName());
744         }
745         if (convertNodeTemplatesRes == null) {
746             convertNodeTemplatesRes = Either.left(nodeTemplates);
747         }
748         log.debug("finish convert topology template for {} for type {}", component.getUniqueId(),
749                 component.getComponentType());
750         return convertNodeTemplatesRes;
751     }
752
753     private void handleInstanceInterfaces(
754             Map<String, List<ComponentInstanceInterface>> componentInstanceInterfaces,
755             ComponentInstance componentInstance, Map<String, DataTypeDefinition> dataTypes, ToscaNodeTemplate nodeTemplate,
756             String instanceUniqueId,
757             Component parentComponent) {
758
759         Map<String, Object> interfaces;
760
761         // we need to handle service proxy interfaces
762         if(isComponentOfTypeServiceProxy(componentInstance)) {
763             if(MapUtils.isEmpty(componentInstanceInterfaces)
764                 || !componentInstanceInterfaces.containsKey(instanceUniqueId)) {
765                 interfaces = null;
766             } else {
767                 List<ComponentInstanceInterface> currServiceInterfaces =
768                     componentInstanceInterfaces.get(instanceUniqueId);
769
770                 Map<String, InterfaceDefinition> tmpInterfaces = new HashMap<>();
771                 currServiceInterfaces.forEach(instInterface -> tmpInterfaces.put(instInterface
772                     .getUniqueId(), instInterface));
773
774                 interfaces = InterfacesOperationsToscaUtil
775                         .getInterfacesMap(parentComponent, tmpInterfaces, dataTypes, true, true);
776             }
777         } else {
778             interfaces =
779                 getComponentInstanceInterfaceInstances(componentInstanceInterfaces,
780                     componentInstance, instanceUniqueId);
781         }
782         nodeTemplate.setInterfaces(interfaces);
783     }
784
785     private boolean isComponentOfTypeServiceProxy(ComponentInstance componentInstance) {
786         return Objects.nonNull(componentInstance.getOriginType())
787             && componentInstance.getOriginType().getValue().equals("Service Proxy");
788     }
789
790     //M3[00001] - NODE TEMPLATE INTERFACES  - START
791     private Map<String, Object> getComponentInstanceInterfaceInstances(Map<String, List<ComponentInstanceInterface>> componentInstancesInterfaces,
792                                                                         ComponentInstance componentInstance,
793                                                                        String instanceUniqueId) {
794         if(MapUtils.isEmpty(componentInstancesInterfaces)) {
795             return null;
796         }
797
798         List<ComponentInstanceInterface> componentInstanceInterfaces =
799             componentInstancesInterfaces.get(instanceUniqueId);
800
801         if(CollectionUtils.isEmpty(componentInstanceInterfaces)) {
802           return null;
803         }
804
805         Map<String, Object> interfaces = new HashMap<>();
806         for(ComponentInstanceInterface componentInstanceInterface : componentInstanceInterfaces) {
807             interfaces.put(componentInstanceInterface.getInterfaceId(),
808                 removeOperationsKeyFromInterface(componentInstanceInterface.getInterfaceInstanceDataDefinition()));
809         }
810
811         componentInstance.setInterfaces(interfaces);
812
813         return interfaces;
814     }
815
816     private void addComponentInstanceInputs(Map<String, DataTypeDefinition> dataTypes,
817             Map<String, List<ComponentInstanceInput>> componentInstancesInputs,
818             String instanceUniqueId, Map<String, Object> props) {
819
820         List<ComponentInstanceInput> instanceInputsList = componentInstancesInputs.get(instanceUniqueId);
821         if (instanceInputsList != null) {
822             instanceInputsList.forEach(input -> {
823
824                 Supplier<String> supplier = () -> input.getValue() != null && !input.getValue().isEmpty()
825                                                           ? input.getValue() : input.getDefaultValue();
826                 propertyConvertor.convertAndAddValue(dataTypes, props, input, supplier);
827             });
828         }
829     }
830
831     private void addPropertiesOfComponentInstance(
832             Map<String, List<ComponentInstanceProperty>> componentInstancesProperties,
833             Map<String, DataTypeDefinition> dataTypes, String instanceUniqueId,
834             Map<String, Object> props) {
835
836         if (isNotEmpty(componentInstancesProperties)) {
837             componentInstancesProperties.get(instanceUniqueId)
838                                         // Converts and adds each value to property map
839                                         .forEach(prop -> propertyConvertor.convertAndAddValue(dataTypes, props, prop,
840                                                 prop::getValue));
841         }
842     }
843
844     private void addPropertiesOfParentComponent(Map<String, DataTypeDefinition> dataTypes,
845             Component componentOfInstance, Map<String, Object> props) {
846
847         List<PropertyDefinition> componentProperties = ((Resource) componentOfInstance).getProperties();
848         if (isNotEmpty(componentProperties)) {
849             componentProperties.stream()
850                                // Filters out properties with empty default values
851                                .filter(prop -> isNotEmpty(prop.getDefaultValue()))
852                                // Converts and adds each value to property map
853                                .forEach(prop -> propertyConvertor.convertAndAddValue(dataTypes, props, prop,
854                                        prop::getDefaultValue));
855         }
856     }
857
858     /**
859      * @param dataTypes
860      * @param componentInstance
861      * @param props
862      * @param prop
863      * @param supplier
864      */
865     private void convertAndAddValue(Map<String, DataTypeDefinition> dataTypes, ComponentInstance componentInstance,
866             Map<String, Object> props, PropertyDefinition prop, Supplier<String> supplier) {
867         Object convertedValue = convertValue(dataTypes, componentInstance, prop, supplier);
868         if (!ToscaValueBaseConverter.isEmptyObjectValue(convertedValue)) {
869             props.put(prop.getName(), convertedValue);
870         }
871     }
872
873     private <T extends PropertyDefinition> Object convertValue(Map<String, DataTypeDefinition> dataTypes,
874             ComponentInstance componentInstance, T input, Supplier<String> supplier) {
875         log.debug("Convert property or input value {} for instance {}", input.getName(),
876                 componentInstance.getUniqueId());
877         String propertyType = input.getType();
878         String innerType = null;
879         if (input.getSchema() != null && input.getSchema().getProperty() != null) {
880             innerType = input.getSchema().getProperty().getType();
881         }
882         return propertyConvertor.convertToToscaObject(propertyType, supplier.get(), innerType,
883             dataTypes, true);
884     }
885
886     private ToscaNodeType createNodeType(Component component) {
887         ToscaNodeType toscaNodeType = new ToscaNodeType();
888         if (ModelConverter.isAtomicComponent(component)) {
889             if (((Resource) component).getDerivedFrom() != null) {
890                 toscaNodeType.setDerived_from(((Resource) component).getDerivedFrom().get(0));
891             }
892             toscaNodeType.setDescription(component.getDescription());
893         } else {
894             String derivedFrom = null != component.getDerivedFromGenericType() ? component.getDerivedFromGenericType()
895                                          : "tosca.nodes.Root";
896             toscaNodeType.setDerived_from(derivedFrom);
897         }
898         return toscaNodeType;
899     }
900
901     private Either<Map<String, ToscaNodeType>, ToscaError> createProxyNodeTypes(Map<String, Component> componentCache ,Component container  ) {
902
903         Map<String, ToscaNodeType> nodeTypesMap = new HashMap<>();
904         Either<Map<String, ToscaNodeType>, ToscaError> res = Either.left(nodeTypesMap);
905
906         List<ComponentInstance> componentInstances = container.getComponentInstances();
907
908         if (componentInstances == null || componentInstances.isEmpty()) {
909             return res;
910         }
911         Map<String, ComponentInstance> serviceProxyInstanceList = new HashMap<>();
912         List<ComponentInstance> proxyInst = componentInstances.stream()
913                                                               .filter(p -> p.getOriginType().name().equals(OriginTypeEnum.ServiceProxy.name()))
914                                                               .collect(Collectors.toList());
915         if (proxyInst != null && !proxyInst.isEmpty()) {
916             for (ComponentInstance inst : proxyInst) {
917                 serviceProxyInstanceList.put(inst.getToscaComponentName(), inst);
918             }
919         }
920
921         if (serviceProxyInstanceList.isEmpty()) {
922             return res;
923         }
924         ComponentParametersView filter = new ComponentParametersView(true);
925         filter.setIgnoreCapabilities(false);
926         filter.setIgnoreComponentInstances(false);
927         Either<Resource, StorageOperationStatus> serviceProxyOrigin = toscaOperationFacade
928                                                                               .getLatestByName("serviceProxy");
929         if (serviceProxyOrigin.isRight()) {
930             log.debug("Failed to fetch normative service proxy resource by tosca name, error {}",
931                     serviceProxyOrigin.right().value());
932             return Either.right(ToscaError.NOT_SUPPORTED_TOSCA_TYPE);
933         }
934         Component origComponent = serviceProxyOrigin.left().value();
935
936         for (Entry<String, ComponentInstance> entryProxy : serviceProxyInstanceList.entrySet()) {
937             Component serviceComponent = null;
938             ComponentParametersView componentParametersView = new ComponentParametersView();
939             componentParametersView.disableAll();
940             componentParametersView.setIgnoreCategories(false);
941             Either<Component, StorageOperationStatus> service = toscaOperationFacade
942                                                                         .getToscaElement(entryProxy.getValue().getSourceModelUid(), componentParametersView);
943             if (service.isRight()) {
944                 log.debug("Failed to fetch resource with id {} for instance {}", entryProxy.getValue().getSourceModelUid(),  entryProxy.getValue().getName());
945             } else {
946                 serviceComponent = service.left().value();
947             }
948
949             ToscaNodeType toscaNodeType = createProxyNodeType(componentCache , origComponent, serviceComponent, entryProxy.getValue());
950             nodeTypesMap.put(entryProxy.getKey(), toscaNodeType);
951         }
952
953         return Either.left(nodeTypesMap);
954     }
955
956     private ToscaNodeType createProxyNodeType(Map<String, Component> componentCache , Component origComponent, Component proxyComponent,
957             ComponentInstance instance) {
958         ToscaNodeType toscaNodeType = new ToscaNodeType();
959         String derivedFrom = ((Resource) origComponent).getToscaResourceName();
960
961         toscaNodeType.setDerived_from(derivedFrom);
962         Either<Map<String, DataTypeDefinition>, TitanOperationStatus> dataTypesEither = dataTypeCache.getAll();
963         if (dataTypesEither.isRight()) {
964             log.debug("Failed to retrieve all data types {}", dataTypesEither.right().value());
965         }
966         Map<String, DataTypeDefinition> dataTypes = dataTypesEither.left().value();
967         Map<String, ToscaCapability> capabilities = this.capabilityRequirementConverter
968                                                             .convertProxyCapabilities( componentCache ,origComponent, proxyComponent, instance, dataTypes);
969
970         toscaNodeType.setCapabilities(capabilities);
971
972         return toscaNodeType;
973     }
974
975     private Either<ToscaNodeTemplate, ToscaError> convertComponentInstanceRequirements(Component component,
976             ComponentInstance componentInstance, List<RequirementCapabilityRelDef> relations,
977             ToscaNodeTemplate nodeTypeTemplate, Component originComponent, Map<String, Component> componentCache) {
978
979         List<Map<String, ToscaTemplateRequirement>> toscaRequirements = new ArrayList<>();
980         if (!addRequirements(component, componentInstance, relations, originComponent, toscaRequirements, componentCache)) {
981             log.debug("Failed to convert component instance requirements for the component instance {}. ",
982                     componentInstance.getName());
983             return Either.right(ToscaError.NODE_TYPE_REQUIREMENT_ERROR);
984         }
985         if (!toscaRequirements.isEmpty()) {
986             nodeTypeTemplate.setRequirements(toscaRequirements);
987         }
988         log.debug("Finished to convert requirements for the node type {} ", componentInstance.getName());
989         return Either.left(nodeTypeTemplate);
990     }
991
992     private boolean addRequirements(Component component, ComponentInstance componentInstance,
993             List<RequirementCapabilityRelDef> relations, Component originComponent,
994             List<Map<String, ToscaTemplateRequirement>> toscaRequirements, Map<String, Component> componentCache) {
995         List<RequirementCapabilityRelDef> filteredRelations = relations.stream()
996                                                                        .filter(p -> componentInstance.getUniqueId().equals(p.getFromNode())).collect(Collectors.toList());
997         return isEmpty(filteredRelations) ||
998                        filteredRelations.stream()
999                                         .allMatch(rel -> addRequirement(componentInstance, originComponent, component.getComponentInstances(), rel, toscaRequirements, componentCache));
1000     }
1001
1002     private boolean addRequirement(ComponentInstance fromInstance, Component fromOriginComponent,
1003             List<ComponentInstance> instancesList, RequirementCapabilityRelDef rel,
1004             List<Map<String, ToscaTemplateRequirement>> toscaRequirements, Map<String, Component> componentCache) {
1005
1006         boolean result = true;
1007         Map<String, List<RequirementDefinition>> reqMap = fromOriginComponent.getRequirements();
1008         RelationshipInfo reqAndRelationshipPair = rel.getRelationships().get(0).getRelation();
1009         Either<Component, StorageOperationStatus> getOriginRes = null;
1010         Optional<RequirementDefinition> reqOpt = Optional.empty();
1011         Component toOriginComponent = null;
1012         Optional<CapabilityDefinition> capOpt = Optional.empty();
1013
1014         ComponentInstance toInstance = instancesList.stream().filter(i -> rel.getToNode().equals(i.getUniqueId()))
1015                                                     .findFirst().orElse(null);
1016         if (toInstance == null) {
1017             log.debug("Failed to find a relation from the node {} to the node {}", fromInstance.getName(),
1018                     rel.getToNode());
1019             result = false;
1020         }
1021         if (result) {
1022             reqOpt = findRequirement(fromOriginComponent, reqMap, reqAndRelationshipPair, fromInstance.getUniqueId());
1023             if (!reqOpt.isPresent()) {
1024                 log.debug("Failed to find a requirement with uniqueId {} on a component with uniqueId {}",
1025                         reqAndRelationshipPair.getRequirementUid(), fromOriginComponent.getUniqueId());
1026                 result = false;
1027             }
1028         }
1029         if (result) {
1030             ComponentParametersView filter = new ComponentParametersView(true);
1031             filter.setIgnoreComponentInstances(false);
1032             filter.setIgnoreCapabilities(false);
1033             filter.setIgnoreGroups(false);
1034             getOriginRes = toscaOperationFacade.getToscaElement(toInstance.getActualComponentUid(), filter);
1035             if (getOriginRes.isRight()) {
1036                 log.debug("Failed to build substituted name for the requirement {}. Failed to get an origin component with uniqueId {}",
1037                         reqOpt.get().getName(), toInstance.getActualComponentUid());
1038                 result = false;
1039             }
1040         }
1041         if (result) {
1042             toOriginComponent = getOriginRes.left().value();
1043             capOpt = toOriginComponent.getCapabilities().get(reqOpt.get().getCapability()).stream()
1044                                       .filter(c -> isCapabilityBelongToRelation(reqAndRelationshipPair, c)).findFirst();
1045             if (!capOpt.isPresent()) {
1046                 capOpt = findCapability(reqAndRelationshipPair, toOriginComponent, fromOriginComponent, reqOpt.get());
1047                 if(!capOpt.isPresent()){
1048                     result = false;
1049                     log.debug("Failed to find a capability with name {} on a component with uniqueId {}",
1050                             reqAndRelationshipPair.getCapability(), fromOriginComponent.getUniqueId());
1051                 }
1052             }
1053         }
1054         if (result) {
1055             result = buildAndAddRequirement(toscaRequirements, fromOriginComponent, toOriginComponent, capOpt.get(),
1056                     reqOpt.get(), reqAndRelationshipPair, toInstance, componentCache);
1057         }
1058         return result;
1059     }
1060
1061     private boolean isCapabilityBelongToRelation(RelationshipInfo reqAndRelationshipPair, CapabilityDefinition capability) {
1062         return capability.getName().equals(reqAndRelationshipPair.getCapability()) && (capability.getOwnerId() !=null && capability.getOwnerId().equals(reqAndRelationshipPair.getCapabilityOwnerId()));
1063     }
1064
1065     private Optional<CapabilityDefinition> findCapability(RelationshipInfo reqAndRelationshipPair, Component toOriginComponent, Component fromOriginComponent, RequirementDefinition requirement) {
1066         Optional<CapabilityDefinition> cap = toOriginComponent.getCapabilities().get(requirement.getCapability()).stream().filter(c -> c.getType().equals(requirement.getCapability())).findFirst();
1067         if (!cap.isPresent()) {
1068             log.debug("Failed to find a capability with name {} on a component with uniqueId {}", reqAndRelationshipPair.getCapability(), fromOriginComponent.getUniqueId());
1069         }
1070         return cap;
1071     }
1072
1073     private boolean buildAndAddRequirement(List<Map<String, ToscaTemplateRequirement>> toscaRequirements, Component fromOriginComponent, Component toOriginComponent, CapabilityDefinition capability, RequirementDefinition requirement, RelationshipInfo reqAndRelationshipPair, ComponentInstance toInstance, Map<String, Component> componentCache) {
1074         List<String> reducedPath = capability.getPath();
1075         if(capability.getOwnerId() !=null){
1076             reducedPath =   capabilityRequirementConverter.getReducedPathByOwner(capability.getPath() , capability.getOwnerId() );
1077         }
1078         Either<String, Boolean> buildCapNameRes = capabilityRequirementConverter.buildSubstitutedName(componentCache,
1079                 toOriginComponent, reducedPath, reqAndRelationshipPair.getCapability(), capability.getPreviousName());
1080         if (buildCapNameRes.isRight()) {
1081             log.debug(
1082                     "Failed to build a substituted capability name for the capability with name {} on a component with uniqueId {}",
1083                     reqAndRelationshipPair.getCapability(), fromOriginComponent.getUniqueId());
1084             return false;
1085         }
1086         Either<String, Boolean> buildReqNameRes  = capabilityRequirementConverter.buildSubstitutedName(componentCache, fromOriginComponent,
1087                 requirement.getPath(), reqAndRelationshipPair.getRequirement(), requirement.getPreviousName());
1088         if (buildReqNameRes.isRight()) {
1089             log.debug(
1090                     "Failed to build a substituted requirement name for the requirement with name {} on a component with uniqueId {}",
1091                     reqAndRelationshipPair.getRequirement(), fromOriginComponent.getUniqueId());
1092             return false;
1093         }
1094         ToscaTemplateRequirement toscaRequirement = new ToscaTemplateRequirement();
1095         Map<String, ToscaTemplateRequirement> toscaReqMap = new HashMap<>();
1096         toscaRequirement.setNode(toInstance.getName());
1097         toscaRequirement.setCapability(buildCapNameRes.left().value());
1098         toscaReqMap.put(buildReqNameRes.left().value(), toscaRequirement);
1099         toscaRequirements.add(toscaReqMap);
1100         return true;
1101     }
1102
1103     private Optional<RequirementDefinition> findRequirement(Component fromOriginComponent, Map<String, List<RequirementDefinition>> reqMap, RelationshipInfo reqAndRelationshipPair,  String fromInstanceId) {
1104         for(List<RequirementDefinition> reqList: reqMap.values()){
1105             Optional<RequirementDefinition> reqOpt = reqList.stream().filter(r -> isRequirementBelongToRelation(fromOriginComponent, reqAndRelationshipPair, r, fromInstanceId)).findFirst();
1106             if(reqOpt.isPresent()){
1107                 return reqOpt;
1108             }
1109         }
1110         return Optional.empty();
1111     }
1112
1113     /**
1114      * Allows detecting the requirement belonging to the received relationship
1115      * The detection logic is: A requirement belongs to a relationship IF 1.The
1116      * name of the requirement equals to the "requirement" field of the
1117      * relation; AND 2. In case of a non-atomic resource, OwnerId of the
1118      * requirement equals to requirementOwnerId of the relation OR uniqueId of
1119      * toInstance equals to capabilityOwnerId of the relation
1120      */
1121     private boolean isRequirementBelongToRelation(Component originComponent, RelationshipInfo reqAndRelationshipPair, RequirementDefinition requirement, String fromInstanceId) {
1122         if (!StringUtils.equals(requirement.getName(), reqAndRelationshipPair.getRequirement())) {
1123             log.debug("Failed to find a requirement with name {} and  reqAndRelationshipPair {}",
1124                     requirement.getName(), reqAndRelationshipPair.getRequirement());
1125             return false;
1126         }
1127         return ModelConverter.isAtomicComponent(originComponent) ||
1128                        isRequirementBelongToOwner(reqAndRelationshipPair, requirement, fromInstanceId, originComponent);
1129     }
1130
1131     private boolean isRequirementBelongToOwner(RelationshipInfo reqAndRelationshipPair, RequirementDefinition requirement, String fromInstanceId, Component originComponent) {
1132         return StringUtils.equals(requirement.getOwnerId(), reqAndRelationshipPair.getRequirementOwnerId()) || (isCvfc(originComponent) && StringUtils.equals(fromInstanceId, reqAndRelationshipPair.getRequirementOwnerId()));
1133     }
1134
1135     private boolean isCvfc(Component component) {
1136         return component.getComponentType() == ComponentTypeEnum.RESOURCE &&
1137                        ((Resource) component).getResourceType() == ResourceTypeEnum.CVFC;
1138     }
1139
1140     private Either<SubstitutionMapping, ToscaError> convertCapabilities(Component component,
1141             SubstitutionMapping substitutionMappings, Map<String, Component> componentCache) {
1142
1143         Either<SubstitutionMapping, ToscaError> result = Either.left(substitutionMappings);
1144         Either<Map<String, String[]>, ToscaError> toscaCapabilitiesRes = capabilityRequirementConverter
1145                                                                                  .convertSubstitutionMappingCapabilities(componentCache, component);
1146         if (toscaCapabilitiesRes.isRight()) {
1147             result = Either.right(toscaCapabilitiesRes.right().value());
1148             log.debug("Failed convert capabilities for the component {}. ", component.getName());
1149         } else if (isNotEmpty(toscaCapabilitiesRes.left().value())) {
1150             substitutionMappings.setCapabilities(toscaCapabilitiesRes.left().value());
1151             log.debug("Finish convert capabilities for the component {}. ", component.getName());
1152         }
1153         log.debug("Finished to convert capabilities for the component {}. ", component.getName());
1154         return result;
1155     }
1156
1157     private Either<ToscaNodeType, ToscaError> convertCapabilities(Map<String, Component> componentsCache, Component component, ToscaNodeType nodeType,
1158             Map<String, DataTypeDefinition> dataTypes) {
1159         Map<String, ToscaCapability> toscaCapabilities = capabilityRequirementConverter.convertCapabilities(componentsCache, component,
1160                 dataTypes);
1161         if (!toscaCapabilities.isEmpty()) {
1162             nodeType.setCapabilities(toscaCapabilities);
1163         }
1164         log.debug("Finish convert Capabilities for node type");
1165
1166         return Either.left(nodeType);
1167     }
1168
1169
1170     protected NodeFilter convertToNodeTemplateNodeFilterComponent(CINodeFilterDataDefinition inNodeFilter) {
1171         if (inNodeFilter == null){
1172             return null;
1173         }
1174         NodeFilter nodeFilter = new NodeFilter();
1175
1176         ListDataDefinition<RequirementNodeFilterCapabilityDataDefinition> origCapabilities =
1177                 inNodeFilter.getCapabilities();
1178
1179         ListDataDefinition<RequirementNodeFilterPropertyDataDefinition> origProperties = inNodeFilter.getProperties();
1180
1181         List<Map<String, CapabilityFilter>> capabilitiesCopy = new ArrayList<>();
1182         List<Map<String, List<Object>>> propertiesCopy = new ArrayList<>();
1183
1184         copyNodeFilterCapabilitiesTemplate(origCapabilities, capabilitiesCopy);
1185         copyNodeFilterProperties(origProperties, propertiesCopy);
1186
1187         if(CollectionUtils.isNotEmpty(capabilitiesCopy)) {
1188             nodeFilter.setCapabilities(capabilitiesCopy);
1189         }
1190
1191         if(CollectionUtils.isNotEmpty(propertiesCopy)) {
1192             nodeFilter.setProperties(propertiesCopy);
1193         }
1194
1195         nodeFilter.setTosca_id(cloneToscaId(inNodeFilter.getTosca_id()));
1196
1197
1198         nodeFilter = (NodeFilter) cloneObjectFromYml(nodeFilter, NodeFilter.class);
1199
1200         return nodeFilter;
1201     }
1202
1203     private Object cloneToscaId(Object toscaId) {
1204         return Objects.isNull(toscaId) ? null
1205                        : cloneObjectFromYml(toscaId, toscaId.getClass());
1206     }
1207
1208
1209     private Object cloneObjectFromYml(Object objToClone, Class classOfObj) {
1210         String objectAsYml = yamlUtil.objectToYaml(objToClone);
1211         return yamlUtil.yamlToObject(objectAsYml, classOfObj);
1212     }
1213     private void copyNodeFilterCapabilitiesTemplate(
1214             ListDataDefinition<RequirementNodeFilterCapabilityDataDefinition> origCapabilities,
1215             List<Map<String, CapabilityFilter>> capabilitiesCopy) {
1216         if(origCapabilities == null || origCapabilities.getListToscaDataDefinition() == null ||
1217                    origCapabilities.getListToscaDataDefinition().isEmpty() ) {
1218             return;
1219         }
1220         for(RequirementNodeFilterCapabilityDataDefinition capability : origCapabilities.getListToscaDataDefinition()) {
1221             Map<String, CapabilityFilter> capabilityFilterCopyMap = new HashMap<>();
1222             CapabilityFilter capabilityFilter = new CapabilityFilter();
1223             List<Map<String, List<Object>>> propertiesCopy = new ArrayList<>();
1224             copyNodeFilterProperties(capability.getProperties(), propertiesCopy);
1225             capabilityFilter.setProperties(propertiesCopy);
1226             capabilityFilterCopyMap.put(capability.getName(), capabilityFilter);
1227             capabilitiesCopy.add(capabilityFilterCopyMap);
1228         }
1229     }
1230
1231     private List<Object> copyNodeFilterProperty(List<Object> propertyList) {
1232         String listAsString = yamlUtil.objectToYaml(propertyList);
1233         return yamlUtil.yamlToObject(listAsString, List.class);
1234     }
1235
1236
1237     private void copyNodeFilterProperties(
1238             ListDataDefinition<RequirementNodeFilterPropertyDataDefinition> origProperties,
1239             List<Map<String, List<Object>>> propertiesCopy) {
1240         if(origProperties == null || origProperties.getListToscaDataDefinition() == null ||
1241                    origProperties.isEmpty()) {
1242             return;
1243         }
1244         for(RequirementNodeFilterPropertyDataDefinition propertyDataDefinition : origProperties.getListToscaDataDefinition()) {
1245             Map<String, List<Object>> propertyMapCopy = new HashMap<>();
1246             for(String propertyInfoEntry : propertyDataDefinition.getConstraints()) {
1247                 Map propertyValObj =  new YamlUtil().yamlToObject(propertyInfoEntry, Map.class);
1248                 if (propertyMapCopy.containsKey(propertyDataDefinition.getName())){
1249                     propertyMapCopy.get(propertyDataDefinition.getName()).add(propertyValObj);
1250                 } else {
1251                     if (propertyDataDefinition.getName() != null) {
1252                         List propsList =new ArrayList();
1253                         propsList.add(propertyValObj);
1254                         propertyMapCopy.put(propertyDataDefinition.getName(), propsList);
1255                     } else {
1256                         propertyMapCopy.putAll(propertyValObj);
1257                     }
1258                 }
1259             }
1260             propertiesCopy.add(propertyMapCopy);
1261         }
1262
1263     }
1264
1265     private static class CustomRepresenter extends Representer {
1266         public CustomRepresenter() {
1267             super();
1268             // null representer is exceptional and it is stored as an instance
1269             // variable.
1270             this.nullRepresenter = new RepresentNull();
1271
1272         }
1273
1274         @Override
1275         protected NodeTuple representJavaBeanProperty(Object javaBean, Property property, Object propertyValue,
1276                 Tag customTag) {
1277             if (propertyValue == null) {
1278                 return null;
1279             } else {
1280                 // skip not relevant for Tosca property
1281                 if ("dependencies".equals(property.getName())) {
1282                     return null;
1283                 }
1284                 NodeTuple defaultNode = super.representJavaBeanProperty(javaBean, property, propertyValue, customTag);
1285
1286                 return "_defaultp_".equals(property.getName())
1287                                ? new NodeTuple(representData("default"), defaultNode.getValueNode()) : defaultNode;
1288             }
1289         }
1290
1291         @Override
1292         protected MappingNode representJavaBean(Set<Property> properties, Object javaBean) {
1293             // remove the bean type from the output yaml (!! ...)
1294             if (!classTags.containsKey(javaBean.getClass())) {
1295                 addClassTag(javaBean.getClass(), Tag.MAP);
1296             }
1297
1298             return super.representJavaBean(properties, javaBean);
1299         }
1300
1301         private class RepresentNull implements Represent {
1302             @Override
1303             public Node representData(Object data) {
1304                 // possible values are here http://yaml.org/type/null.html
1305                 return representScalar(Tag.NULL, "");
1306             }
1307         }
1308     }
1309
1310     private static class UnsortedPropertyUtils extends PropertyUtils {
1311         @Override
1312         protected Set<Property> createPropertySet(Class type, BeanAccess bAccess)
1313                 throws IntrospectionException {
1314             Collection<Property> fields = getPropertiesMap(type, BeanAccess.FIELD).values();
1315             return new LinkedHashSet<>(fields);
1316         }
1317     }
1318
1319     private Object removeOperationsKeyFromInterface(Object interfaceInstanceDataDefinition) {
1320         ObjectMapper objectMapper = new ObjectMapper();
1321         objectMapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false);
1322
1323             Map<String, Object> interfaceAsMap = ServiceUtils.getObjectAsMap(interfaceInstanceDataDefinition);
1324             Map<String, Object> operations = (Map<String, Object>) interfaceAsMap.remove("operations");
1325             interfaceAsMap.remove("empty");
1326
1327             if(MapUtils.isNotEmpty(operations)) {
1328                 interfaceAsMap.putAll(operations);
1329             }
1330
1331             Object interfaceObject = objectMapper.convertValue(interfaceAsMap, Object.class);
1332
1333             return interfaceObject;
1334
1335     }
1336 }
1337