aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-be-plugins/etsi-nfv-nsd-csar-plugin/src/main/java/org/openecomp/sdc/be/plugins/etsi/nfv/nsd/generator/NsDescriptorGeneratorImpl.java
blob: f024d8f52a20c57f8180c1981a7f4f9af151a9cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
 
/*
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2020 Nordix Foundation
 *  ================================================================================
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *  SPDX-License-Identifier: Apache-2.0
 *  ============LICENSE_END=========================================================
 */
package org.openecomp.sdc.be.plugins.etsi.nfv.nsd.generator;

import com.google.common.collect.ImmutableMap;
import fj.data.Either;
import groovy.util.MapEntry;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.stream.Collectors;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.openecomp.sdc.be.config.ConfigurationManager;
import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
import org.openecomp.sdc.be.model.Component;
import org.openecomp.sdc.be.model.tosca.constraints.ConstraintType;
import org.openecomp.sdc.be.plugins.etsi.nfv.nsd.exception.NsdException;
import org.openecomp.sdc.be.plugins.etsi.nfv.nsd.model.Nsd;
import org.openecomp.sdc.be.plugins.etsi.nfv.nsd.model.VnfDescriptor;
import org.openecomp.sdc.be.plugins.etsi.nfv.nsd.tosca.yaml.ToscaTemplateYamlGenerator;
import org.openecomp.sdc.be.tosca.ToscaError;
import org.openecomp.sdc.be.tosca.ToscaExportHandler;
import org.openecomp.sdc.be.tosca.model.SubstitutionMapping;
import org.openecomp.sdc.be.tosca.model.ToscaNodeTemplate;
import org.openecomp.sdc.be.tosca.model.ToscaNodeType;
import org.openecomp.sdc.be.tosca.model.ToscaProperty;
import org.openecomp.sdc.be.tosca.model.ToscaPropertyConstraint;
import org.openecomp.sdc.be.tosca.model.ToscaPropertyConstraintValidValues;
import org.openecomp.sdc.be.tosca.model.ToscaRequirement;
import org.openecomp.sdc.be.tosca.model.ToscaTemplate;
import org.openecomp.sdc.be.tosca.model.ToscaTemplateRequirement;
import org.openecomp.sdc.be.tosca.model.ToscaTopolgyTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;

@org.springframework.stereotype.Component("nsDescriptorGenerator")
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class NsDescriptorGeneratorImpl implements NsDescriptorGenerator {

    private static final Logger LOGGER = LoggerFactory.getLogger(NsDescriptorGeneratorImpl.class);
    private static final String TOSCA_VERSION = "tosca_simple_yaml_1_1";
    private static final String NS_TOSCA_TYPE = "tosca.nodes.nfv.NS";
    private static final List<Map<String, Map<String, String>>> DEFAULT_IMPORTS = ConfigurationManager.getConfigurationManager().getConfiguration()
        .getDefaultImports();
    private static final List<String> PROPERTIES_TO_EXCLUDE_FROM_ETSI_SOL_NSD_NS_NODE_TYPE = Arrays
        .asList("cds_model_name", "cds_model_version", "skip_post_instantiation_configuration", "controller_actor");
    private static final List<String> PROPERTIES_TO_EXCLUDE_FROM_ETSI_SOL_NSD_NS_NODE_TEMPLATE = Arrays
        .asList("nf_function", "nf_role", "nf_naming_code", "nf_type", "nf_naming", "availability_zone_max_count", "min_instances", "max_instances",
            "multi_stage_design", "sdnc_model_name", "sdnc_model_version", "sdnc_artifact_name", "skip_post_instantiation_configuration",
            "controller_actor");
    private final ToscaExportHandler toscaExportHandler;
    private final ObjectProvider<ToscaTemplateYamlGenerator> toscaTemplateYamlGeneratorProvider;

    public NsDescriptorGeneratorImpl(final ToscaExportHandler toscaExportHandler,
                                     final ObjectProvider<ToscaTemplateYamlGenerator> toscaTemplateYamlGeneratorProvider) {
        this.toscaExportHandler = toscaExportHandler;
        this.toscaTemplateYamlGeneratorProvider = toscaTemplateYamlGeneratorProvider;
    }

    public Optional<Nsd> generate(final Component component, final List<VnfDescriptor> vnfDescriptorList) throws NsdException {
        if (!ComponentTypeEnum.SERVICE.equals(component.getComponentType())) {
            return Optional.empty();
        }
        final ToscaTemplate toscaTemplate = createNetworkServiceDescriptor(component, vnfDescriptorList);
        final ToscaNodeType nsNodeType = toscaTemplate.getNode_types().values().stream()
            .filter(toscaNodeType -> NS_TOSCA_TYPE.equals(toscaNodeType.getDerived_from())).findFirst().orElse(null);
        if (nsNodeType == null) {
            return Optional.empty();
        }
        return Optional.of(buildNsd(toscaTemplate, nsNodeType));
    }

    private Nsd buildNsd(final ToscaTemplate toscaTemplate, final ToscaNodeType nsNodeType) {
        final Nsd nsd = new Nsd();
        nsd.setDesigner(getProperty(nsNodeType, Nsd.DESIGNER_PROPERTY));
        nsd.setVersion(getProperty(nsNodeType, Nsd.VERSION_PROPERTY));
        nsd.setName(getProperty(nsNodeType, Nsd.NAME_PROPERTY));
        nsd.setInvariantId(getProperty(nsNodeType, Nsd.INVARIANT_ID_PROPERTY));
        final ToscaTemplateYamlGenerator yamlParserProvider = toscaTemplateYamlGeneratorProvider.getObject(toscaTemplate);
        final byte[] contents = yamlParserProvider.parseToYamlString().getBytes();
        nsd.setContents(contents);
        final List<String> interfaceImplementations = getInterfaceImplementations(toscaTemplate);
        nsd.setArtifactReferences(interfaceImplementations);
        return nsd;
    }

    private List<String> getInterfaceImplementations(final ToscaTemplate template) {
        if (template.getTopology_template().getNode_templates() == null) {
            return Collections.emptyList();
        }
        final List<String> interfaceImplementations = new ArrayList<>();
        final Collection<ToscaNodeTemplate> nodeTemplates = template.getTopology_template().getNode_templates().values();
        nodeTemplates.stream().filter(toscaNodeTemplate -> toscaNodeTemplate.getInterfaces() != null).forEach(
            toscaNodeTemplate -> toscaNodeTemplate.getInterfaces().values()
                .forEach(interfaceInstance -> interfaceImplementations.addAll(getInterfaceImplementations(interfaceInstance))));
        return interfaceImplementations;
    }

    private Collection<String> getInterfaceImplementations(final Object interfaceInstance) {
        final Collection<String> interfaceImplementations = new ArrayList<>();
        if (interfaceInstance instanceof Map) {
            for (final Object value : ((Map<?, ?>) interfaceInstance).values()) {
                if (value instanceof Map && ((Map<?, ?>) value).get("implementation") != null) {
                    interfaceImplementations.add(((Map<?, ?>) value).get("implementation").toString());
                }
            }
        }
        return interfaceImplementations;
    }

    private String getProperty(final ToscaNodeType nodeType, final String propertyName) {
        final ToscaProperty toscaProperty = nodeType.getProperties().get(propertyName);
        final String errorMsg = String.format("Property '%s' must be defined and must have a valid values constraint", propertyName);
        final String returnValueOnError = "unknown";
        if (toscaProperty == null || CollectionUtils.isEmpty(toscaProperty.getConstraints())) {
            LOGGER.error(errorMsg);
            return returnValueOnError;
        }
        final ToscaPropertyConstraint toscaPropertyConstraint = toscaProperty.getConstraints().get(0);
        if (ConstraintType.VALID_VALUES != toscaPropertyConstraint.getConstraintType()) {
            LOGGER.error(errorMsg);
            return returnValueOnError;
        }
        final ToscaPropertyConstraintValidValues validValuesConstraint = (ToscaPropertyConstraintValidValues) toscaPropertyConstraint;
        final List<String> validValues = validValuesConstraint.getValidValues();
        if (CollectionUtils.isEmpty(validValues)) {
            LOGGER.error(errorMsg);
            return returnValueOnError;
        }
        return validValues.get(0);
    }

    private ToscaTemplate createNetworkServiceDescriptor(final Component component, final List<VnfDescriptor> vnfDescriptorList) throws NsdException {
        final ToscaTemplate componentToscaTemplate = parseToToscaTemplate(component);
        final ToscaTemplate componentToscaTemplateInterface = exportComponentInterfaceAsToscaTemplate(component);
        final Entry<String, ToscaNodeType> firstNodeTypeEntry = componentToscaTemplateInterface.getNode_types().entrySet().stream().findFirst()
            .orElse(null);
        if (firstNodeTypeEntry == null) {
            throw new NsdException("Could not find abstract Service type");
        }
        final String nsNodeTypeName = firstNodeTypeEntry.getKey();
        final ToscaNodeType nsNodeType = firstNodeTypeEntry.getValue();
        final Map<String, ToscaNodeType> nodeTypeMap = new HashMap<>();
        nodeTypeMap.put(nsNodeTypeName, createEtsiSolNsNodeType(nsNodeType, componentToscaTemplate));
        if (componentToscaTemplate.getNode_types() == null) {
            componentToscaTemplate.setNode_types(nodeTypeMap);
        } else {
            componentToscaTemplate.getNode_types().putAll(nodeTypeMap);
        }
        handleNodeTemplates(componentToscaTemplate);
        removeOnapPropertiesFromInputs(componentToscaTemplate);
        handleSubstitutionMappings(componentToscaTemplate, nsNodeTypeName);
        final Map<String, ToscaNodeTemplate> nodeTemplates = new HashMap<>();
        nodeTemplates.put(nsNodeTypeName,
            createNodeTemplateForNsNodeType(nsNodeTypeName, componentToscaTemplateInterface.getNode_types().get(nsNodeTypeName)));
        if (componentToscaTemplate.getTopology_template().getNode_templates() == null) {
            componentToscaTemplate.getTopology_template().setNode_templates(nodeTemplates);
        } else {
            setNodeTemplateTypesForVnfs(componentToscaTemplate, vnfDescriptorList);
            componentToscaTemplate.getTopology_template().getNode_templates().putAll(nodeTemplates);
        }
        removeOnapMetaData(componentToscaTemplate);
        setDefaultImportsForEtsiSolNsNsd(componentToscaTemplate, vnfDescriptorList);
        return componentToscaTemplate;
    }

    private void handleSubstitutionMappings(final ToscaTemplate componentToscaTemplate, final String nsNodeTypeName) {
        final SubstitutionMapping substitutionMapping = new SubstitutionMapping();
        substitutionMapping.setNode_type(nsNodeTypeName);
        final SubstitutionMapping onapSubstitutionMapping = componentToscaTemplate.getTopology_template().getSubstitution_mappings();
        if (onapSubstitutionMapping != null && onapSubstitutionMapping.getRequirements() != null) {
            substitutionMapping.setRequirements(adjustRequirementNamesToMatchVnfd(onapSubstitutionMapping.getRequirements()));
        }
        componentToscaTemplate.getTopology_template().setSubstitution_mappings(substitutionMapping);
    }
    
    private Map<String, String[]> adjustRequirementNamesToMatchVnfd(final Map<String, String[]> requirements) {
        for (final Map.Entry<String, String[]> entry : requirements.entrySet()) {
            try {
                final String[] adjustedValue = {entry.getValue()[0], entry.getValue()[1].substring(entry.getValue()[1].lastIndexOf('.') + 1)};
                entry.setValue(adjustedValue);
            } catch (final ArrayIndexOutOfBoundsException exception) {
                LOGGER.error("Malformed requirement: {}", entry);
            }
        }
        return requirements;
    }

    private void setNodeTemplateTypesForVnfs(final ToscaTemplate template, final List<VnfDescriptor> vnfDescriptorList) {
        if (CollectionUtils.isEmpty(vnfDescriptorList)) {
            return;
        }
        final Map<String, ToscaNodeTemplate> nodeTemplateMap = template.getTopology_template().getNode_templates();
        if (MapUtils.isEmpty(nodeTemplateMap)) {
            return;
        }
        nodeTemplateMap.forEach(
            (key, toscaNodeTemplate) -> vnfDescriptorList.stream().filter(vnfDescriptor -> key.equals(vnfDescriptor.getName())).findFirst()
                .ifPresent(vnfDescriptor -> toscaNodeTemplate.setType(vnfDescriptor.getNodeType())));
    }

    private void handleNodeTemplates(final ToscaTemplate template) {
        final Map<String, ToscaNodeTemplate> nodeTemplateMap = template.getTopology_template().getNode_templates();
        if (MapUtils.isEmpty(nodeTemplateMap)) {
            return;
        }
        for (final Entry<String, ToscaNodeTemplate> nodeTemplate : nodeTemplateMap.entrySet()) {
            setPropertiesForNodeTemplate(nodeTemplate);
            setRequirementsForNodeTemplate(nodeTemplate);
            removeCapabilitiesFromNodeTemplate(nodeTemplate);
        }
    }
    
    private void setPropertiesForNodeTemplate(final Entry<String, ToscaNodeTemplate> nodeTemplate) {
        final Map<String, Object> propertyMap = nodeTemplate.getValue().getProperties();
        if (MapUtils.isEmpty(propertyMap)) {
            nodeTemplate.getValue().setProperties(null);
            return;
        }
        final Map<String, Object> editedPropertyMap = new HashMap<>();
        for (final Entry<String, Object> property : propertyMap.entrySet()) {
            if (!PROPERTIES_TO_EXCLUDE_FROM_ETSI_SOL_NSD_NS_NODE_TEMPLATE.contains(property.getKey()) && propertyIsDefinedInNodeType(
                property.getKey())) {
                editedPropertyMap.put(property.getKey().substring(property.getKey().indexOf('_') + 1), property.getValue());
            }
        }
        if (editedPropertyMap.isEmpty()) {
            nodeTemplate.getValue().setProperties(null);
        } else {
            nodeTemplate.getValue().setProperties(editedPropertyMap);
        }
    }

    private void setRequirementsForNodeTemplate(final Entry<String, ToscaNodeTemplate> nodeTemplateMap) {
        final List<Map<String,ToscaTemplateRequirement>> requirementAssignments = nodeTemplateMap.getValue().getRequirements();
        if (requirementAssignments != null) {  
            final List<Map<String,ToscaTemplateRequirement>> requirementAssignmentsMatchingVnfdRequirements = new ArrayList<>();
            for (final Map<String, ToscaTemplateRequirement> requirementAssignment: requirementAssignments) {
                final Map<String, ToscaTemplateRequirement> requirementAssignmentMatchingVnfd = 
                        requirementAssignment.entrySet().stream().collect(Collectors.toMap(entry -> entry.getKey().substring(entry.getKey().lastIndexOf('.') + 1), Map.Entry::getValue));
                requirementAssignmentsMatchingVnfdRequirements.add(requirementAssignmentMatchingVnfd);
            }
            nodeTemplateMap.getValue().setRequirements(requirementAssignmentsMatchingVnfdRequirements);
        }
    }


    private void removeCapabilitiesFromNodeTemplate(final Entry<String, ToscaNodeTemplate> nodeTemplate) {
        nodeTemplate.getValue().setCapabilities(null);
    }

    private void removeOnapPropertiesFromInputs(final ToscaTemplate template) {
        final ToscaTopolgyTemplate topologyTemplate = template.getTopology_template();
        final Map<String, ToscaProperty> inputMap = topologyTemplate.getInputs();
        if (MapUtils.isNotEmpty(inputMap)) {
            inputMap.entrySet().removeIf(entry -> PROPERTIES_TO_EXCLUDE_FROM_ETSI_SOL_NSD_NS_NODE_TYPE.contains(entry.getKey()));
        }
        if (MapUtils.isEmpty(inputMap)) {
            topologyTemplate.setInputs(null);
        }
    }

    private void removeOnapMetaData(final ToscaTemplate template) {
        template.setMetadata(null);
        final Map<String, ToscaNodeTemplate> nodeTemplateMap = template.getTopology_template().getNode_templates();
        if (MapUtils.isEmpty(nodeTemplateMap)) {
            return;
        }
        nodeTemplateMap.values().forEach(toscaNodeTemplate -> toscaNodeTemplate.setMetadata(null));
    }

    private void setDefaultImportsForEtsiSolNsNsd(final ToscaTemplate template, final List<VnfDescriptor> vnfDescriptorList) {
        final List<Map<String, Map<String, String>>> importEntryMap = new ArrayList<>();
        final Map<String, Map<String, String>> defaultImportEntryMap = generateDefaultImportEntry();
        if (MapUtils.isNotEmpty(defaultImportEntryMap)) {
            importEntryMap.add(defaultImportEntryMap);
        }
        if (CollectionUtils.isNotEmpty(vnfDescriptorList)) {
            for (final VnfDescriptor vnfDescriptor : vnfDescriptorList) {
                final Map<String, String> vnfImportChildEntry = new HashMap<>();
                vnfImportChildEntry.put("file", vnfDescriptor.getVnfdFileName());
                final Map<String, Map<String, String>> vnfdImportVnfdEntry = new HashMap<>();
                vnfdImportVnfdEntry.put(vnfDescriptor.getName(), vnfImportChildEntry);
                importEntryMap.add(vnfdImportVnfdEntry);
            }
        }
        template.setImports(importEntryMap);
    }

    private Map<String, Map<String, String>> generateDefaultImportEntry() {
        return ImmutableMap.of("etsi_nfv_sol001_nsd_types", ImmutableMap.of("file", "etsi_nfv_sol001_nsd_types.yaml"));
    }

    private ToscaNodeType createEtsiSolNsNodeType(final ToscaNodeType nsNodeType, final ToscaTemplate componentToscaTemplate) {
        final ToscaNodeType toscaNodeType = new ToscaNodeType();
        toscaNodeType.setDerived_from(NS_TOSCA_TYPE);
        final Map<String, ToscaProperty> propertiesInNsNodeType = nsNodeType.getProperties();
        for (final Entry<String, ToscaProperty> property : propertiesInNsNodeType.entrySet()) {
            final ToscaProperty toscaProperty = property.getValue();
            if (toscaProperty.getDefaultp() != null) {
                final ToscaPropertyConstraintValidValues constraint = new ToscaPropertyConstraintValidValues(
                    Collections.singletonList(toscaProperty.getDefaultp().toString()));
                toscaProperty.setConstraints(Collections.singletonList(constraint));
            }
        }
        propertiesInNsNodeType.entrySet().removeIf(entry -> PROPERTIES_TO_EXCLUDE_FROM_ETSI_SOL_NSD_NS_NODE_TYPE.contains(entry.getKey()));
        toscaNodeType.setProperties(propertiesInNsNodeType);
        
        final List<Map<String, ToscaRequirement>> requirementsInNsNodeType = getRequirementsForNsNodeType(nsNodeType.getRequirements(), componentToscaTemplate);
        if (!requirementsInNsNodeType.isEmpty()) {
            toscaNodeType.setRequirements(requirementsInNsNodeType);  
        }

        return toscaNodeType;
    }
    
    private List<Map<String,ToscaRequirement>> getRequirementsForNsNodeType(final List<Map<String,ToscaRequirement>> requirements, final ToscaTemplate componentToscaTemplate) {
        final Map<String, String[]> requirementsInSubstitutionMapping = componentToscaTemplate.getTopology_template().getSubstitution_mappings().getRequirements();
        if (requirements == null || MapUtils.isEmpty(requirementsInSubstitutionMapping)) {
            return Collections.emptyList();
        }
        final List<Map<String,ToscaRequirement>> requirementsToAdd = new ArrayList<>();
        for (final Map<String,ToscaRequirement> requirementMap : requirements) {
            final Map<String,ToscaRequirement> neededRequirements = requirementMap.entrySet().stream().filter(entry -> requirementsInSubstitutionMapping.containsKey(entry.getKey())).collect(Collectors.toMap(Entry::getKey, Entry::getValue));
            if (!neededRequirements.isEmpty()) {
                requirementsToAdd.add(neededRequirements);
            }
        }
        return requirementsToAdd;
       
    }

    private boolean propertyIsDefinedInNodeType(final String propertyName) {
        // This will achieve what we want for now, but will look into a more generic solution which would involve

        // checking the node_type definition in the VNFD
        return !propertyName.equals("additional_parameters");
    }

    private ToscaNodeTemplate createNodeTemplateForNsNodeType(final String nodeType, final ToscaNodeType toscaNodeType) {
        final ToscaNodeTemplate nodeTemplate = new ToscaNodeTemplate();
        nodeTemplate.setType(nodeType);
        final Map<String, ToscaProperty> properties = toscaNodeType.getProperties();
        final Map<String, Object> nodeTemplateProperties = new HashMap<>();
        for (final Entry<String, ToscaProperty> property : properties.entrySet()) {
            nodeTemplateProperties.put(property.getKey(), property.getValue().getDefaultp());
        }
        if (!nodeTemplateProperties.isEmpty()) {
            nodeTemplate.setProperties(nodeTemplateProperties);
        }
        final Map<String, Object> interfaces = toscaNodeType.getInterfaces();
        if (interfaces != null) {
            for (final Entry<String, Object> nodeInterface : interfaces.entrySet()) {
                if ("Nslcm".equals(nodeInterface.getKey()) && nodeInterface.getValue() instanceof Map) {
                    ((Map<?, ?>) nodeInterface.getValue()).remove("type");
                }
            }
            nodeTemplate.setInterfaces(interfaces);
        }
        return nodeTemplate;
    }

    private ToscaTemplate parseToToscaTemplate(final Component component) throws NsdException {
        final Either<ToscaTemplate, ToscaError> toscaTemplateRes = toscaExportHandler.convertToToscaTemplate(component);
        if (toscaTemplateRes.isRight()) {
            String errorMsg = String
                .format("Could not parse component '%s' to tosca template. Error '%s'", component.getName(), toscaTemplateRes.right().value().name());
            throw new NsdException(errorMsg);
        }
        return toscaTemplateRes.left().value();
    }

    private ToscaTemplate exportComponentInterfaceAsToscaTemplate(final Component component) throws NsdException {
        if (null == DEFAULT_IMPORTS) {
            throw new NsdException("Could not load default CSAR imports from configuration");
        }
        final ToscaTemplate toscaTemplate = new ToscaTemplate(TOSCA_VERSION);
        toscaTemplate.setImports(new ArrayList<>(DEFAULT_IMPORTS));
        final Either<ToscaTemplate, ToscaError> toscaTemplateRes = toscaExportHandler
            .convertInterfaceNodeType(new HashMap<>(), component, toscaTemplate, new HashMap<>(), false);
        if (toscaTemplateRes.isRight()) {
            throw new NsdException(String.format("Could not create abstract service from component '%s'", component.getName()));
        }
        return toscaTemplateRes.left().value();
    }
}