aboutsummaryrefslogtreecommitdiffstats
path: root/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/PnfdNodeTemplateTransformationEngine.java
blob: b2038a00ddf1ca49c1c962e9800eb6949d22bafc (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
/*
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2019 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.core.converter.impl.pnfd;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.onap.sdc.tosca.datatypes.model.ServiceTemplate;
import org.onap.sdc.tosca.datatypes.model.TopologyTemplate;
import org.openecomp.core.converter.ServiceTemplateReaderService;
import org.openecomp.core.converter.impl.pnfd.factory.PnfdBlockParserFactory;
import org.openecomp.core.converter.impl.pnfd.parser.ConversionQueryYamlParser;
import org.openecomp.core.converter.pnfd.model.Transformation;
import org.openecomp.core.converter.pnfd.model.TransformationBlock;

/**
 * Engine that manages the PNF Descriptor transformation process for the NodeTemplate block.
 */
public class PnfdNodeTemplateTransformationEngine extends AbstractPnfdTransformationEngine {

    public PnfdNodeTemplateTransformationEngine(final ServiceTemplateReaderService templateFrom,
                                                final ServiceTemplate templateTo) {
        super(templateFrom, templateTo);
    }

    //used for tests purposes
    PnfdNodeTemplateTransformationEngine(final ServiceTemplateReaderService templateFrom,
                                                final ServiceTemplate templateTo,
                                                final String descriptorResourcePath) {
        super(templateFrom, templateTo, descriptorResourcePath);
    }

    /**
     * Runs the transformation process.
     */
    public void transform() {
        readDefinition();
        initializeTopologyTemplate();
        executeTransformations();
    }

    /**
     * Initializes the topology template and its node template set.
     */
    private void initializeTopologyTemplate() {
        TopologyTemplate topologyTemplate = templateTo.getTopology_template();
        if (topologyTemplate == null) {
            topologyTemplate = new TopologyTemplate();
            templateTo.setTopology_template(topologyTemplate);
        }
        if (topologyTemplate.getNode_templates() == null) {
            topologyTemplate.setNode_templates(new HashMap<>());
        }
    }


    /**
     * Execute all transformations specified in the descriptor.
     */
    @Override
    protected void executeTransformations() {
        final Set<Transformation> transformationSet = transformationDescription.getTransformationSet();
        if (CollectionUtils.isEmpty(transformationSet)) {
            return;
        }
        transformationGroupByBlockMap = transformationSet.stream()
            .filter(Transformation::isValid)
            .collect(Collectors.groupingBy(Transformation::getBlock));
        executeCustomTypeTransformations();
        final Map<String, String> inputsToConvertMap = executeNodeTemplateTransformations();
        executeGetInputFunctionTransformations(inputsToConvertMap);
    }

    /**
     * Parses all topology_template node_template.
     * @return
     *  A map containing any input that was called with a get_input TOSCA function and its getInputFunction
     *  transformation name
     */
    private Map<String, String> executeNodeTemplateTransformations() {
        final List<Transformation> transformationList = transformationGroupByBlockMap
            .get(TransformationBlock.NODE_TEMPLATE);
        if (CollectionUtils.isEmpty(transformationList)) {
            return Collections.emptyMap();
        }

        final Map<String, String> inputsToConvertMap = new HashMap<>();
        transformationList.forEach(transformation ->
            PnfdBlockParserFactory.getInstance().get(transformation).ifPresent(pnfParser -> {
                pnfParser.parse(templateFrom, templateTo);
                if (pnfParser.getInputAndTransformationNameMap().isPresent()) {
                    inputsToConvertMap.putAll(pnfParser.getInputAndTransformationNameMap().get());
                }
            }));
        return inputsToConvertMap;
    }

    /**
     * Parses all topology_template inputs called with a get_input TOSCA function.
     * @param inputsToConvertMap    A map containing the topology_template input name and its conversion definition name
     */
    private void executeGetInputFunctionTransformations(final Map<String, String> inputsToConvertMap) {
        final List<Transformation> transformationListOfGetInputFunction = transformationGroupByBlockMap
            .get(TransformationBlock.GET_INPUT_FUNCTION);

        if(MapUtils.isEmpty(inputsToConvertMap) || CollectionUtils.isEmpty(transformationListOfGetInputFunction)) {
            return;
        }

        final Map<String, List<Transformation>> transformationByName = transformationListOfGetInputFunction.stream()
            .collect(Collectors.groupingBy(Transformation::getName));

        inputsToConvertMap.forEach((inputName, transformationName) -> {
            final List<Transformation> transformationList = transformationByName.get(transformationName);
            if (!CollectionUtils.isEmpty(transformationList)) {
                final Transformation transformation = transformationList.stream()
                    .findFirst().orElse(null);
                if (transformation != null) {
                    final Map<String, Object> conversionQueryMap = new HashMap<>();
                    conversionQueryMap.put(inputName, null);
                    transformation.setConversionQuery(
                        ConversionQueryYamlParser.parse(conversionQueryMap).orElse(null)
                    );
                    PnfdBlockParserFactory.getInstance().get(transformation)
                        .ifPresent(pnfParser -> pnfParser.parse(templateFrom, templateTo));
                }
            }
        });
    }

    /**
     * Parses a Customized Node Type that extend from a valid ONAP NodeType.
     */
    private void executeCustomTypeTransformations() {
        final List<Transformation> transformationList =  transformationGroupByBlockMap
            .get((TransformationBlock.CUSTOM_NODE_TYPE));
        if (CollectionUtils.isEmpty(transformationList)) {
            return;
        }
        transformationList.forEach(transformation ->
            PnfdBlockParserFactory.getInstance().get(transformation).ifPresent(pnfdBlockParser ->
                pnfdBlockParser.parse(templateFrom, templateTo)));
    }

}