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