b2038a00ddf1ca49c1c962e9800eb6949d22bafc
[sdc.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation
4  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  *
16  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  */
19
20 package org.openecomp.core.converter.impl.pnfd;
21
22 import java.util.Collections;
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Set;
27 import java.util.stream.Collectors;
28 import org.apache.commons.collections.CollectionUtils;
29 import org.apache.commons.collections.MapUtils;
30 import org.onap.sdc.tosca.datatypes.model.ServiceTemplate;
31 import org.onap.sdc.tosca.datatypes.model.TopologyTemplate;
32 import org.openecomp.core.converter.ServiceTemplateReaderService;
33 import org.openecomp.core.converter.impl.pnfd.factory.PnfdBlockParserFactory;
34 import org.openecomp.core.converter.impl.pnfd.parser.ConversionQueryYamlParser;
35 import org.openecomp.core.converter.pnfd.model.Transformation;
36 import org.openecomp.core.converter.pnfd.model.TransformationBlock;
37
38 /**
39  * Engine that manages the PNF Descriptor transformation process for the NodeTemplate block.
40  */
41 public class PnfdNodeTemplateTransformationEngine extends AbstractPnfdTransformationEngine {
42
43     public PnfdNodeTemplateTransformationEngine(final ServiceTemplateReaderService templateFrom,
44                                                 final ServiceTemplate templateTo) {
45         super(templateFrom, templateTo);
46     }
47
48     //used for tests purposes
49     PnfdNodeTemplateTransformationEngine(final ServiceTemplateReaderService templateFrom,
50                                                 final ServiceTemplate templateTo,
51                                                 final String descriptorResourcePath) {
52         super(templateFrom, templateTo, descriptorResourcePath);
53     }
54
55     /**
56      * Runs the transformation process.
57      */
58     public void transform() {
59         readDefinition();
60         initializeTopologyTemplate();
61         executeTransformations();
62     }
63
64     /**
65      * Initializes the topology template and its node template set.
66      */
67     private void initializeTopologyTemplate() {
68         TopologyTemplate topologyTemplate = templateTo.getTopology_template();
69         if (topologyTemplate == null) {
70             topologyTemplate = new TopologyTemplate();
71             templateTo.setTopology_template(topologyTemplate);
72         }
73         if (topologyTemplate.getNode_templates() == null) {
74             topologyTemplate.setNode_templates(new HashMap<>());
75         }
76     }
77
78
79     /**
80      * Execute all transformations specified in the descriptor.
81      */
82     @Override
83     protected void executeTransformations() {
84         final Set<Transformation> transformationSet = transformationDescription.getTransformationSet();
85         if (CollectionUtils.isEmpty(transformationSet)) {
86             return;
87         }
88         transformationGroupByBlockMap = transformationSet.stream()
89             .filter(Transformation::isValid)
90             .collect(Collectors.groupingBy(Transformation::getBlock));
91         executeCustomTypeTransformations();
92         final Map<String, String> inputsToConvertMap = executeNodeTemplateTransformations();
93         executeGetInputFunctionTransformations(inputsToConvertMap);
94     }
95
96     /**
97      * Parses all topology_template node_template.
98      * @return
99      *  A map containing any input that was called with a get_input TOSCA function and its getInputFunction
100      *  transformation name
101      */
102     private Map<String, String> executeNodeTemplateTransformations() {
103         final List<Transformation> transformationList = transformationGroupByBlockMap
104             .get(TransformationBlock.NODE_TEMPLATE);
105         if (CollectionUtils.isEmpty(transformationList)) {
106             return Collections.emptyMap();
107         }
108
109         final Map<String, String> inputsToConvertMap = new HashMap<>();
110         transformationList.forEach(transformation ->
111             PnfdBlockParserFactory.getInstance().get(transformation).ifPresent(pnfParser -> {
112                 pnfParser.parse(templateFrom, templateTo);
113                 if (pnfParser.getInputAndTransformationNameMap().isPresent()) {
114                     inputsToConvertMap.putAll(pnfParser.getInputAndTransformationNameMap().get());
115                 }
116             }));
117         return inputsToConvertMap;
118     }
119
120     /**
121      * Parses all topology_template inputs called with a get_input TOSCA function.
122      * @param inputsToConvertMap    A map containing the topology_template input name and its conversion definition name
123      */
124     private void executeGetInputFunctionTransformations(final Map<String, String> inputsToConvertMap) {
125         final List<Transformation> transformationListOfGetInputFunction = transformationGroupByBlockMap
126             .get(TransformationBlock.GET_INPUT_FUNCTION);
127
128         if(MapUtils.isEmpty(inputsToConvertMap) || CollectionUtils.isEmpty(transformationListOfGetInputFunction)) {
129             return;
130         }
131
132         final Map<String, List<Transformation>> transformationByName = transformationListOfGetInputFunction.stream()
133             .collect(Collectors.groupingBy(Transformation::getName));
134
135         inputsToConvertMap.forEach((inputName, transformationName) -> {
136             final List<Transformation> transformationList = transformationByName.get(transformationName);
137             if (!CollectionUtils.isEmpty(transformationList)) {
138                 final Transformation transformation = transformationList.stream()
139                     .findFirst().orElse(null);
140                 if (transformation != null) {
141                     final Map<String, Object> conversionQueryMap = new HashMap<>();
142                     conversionQueryMap.put(inputName, null);
143                     transformation.setConversionQuery(
144                         ConversionQueryYamlParser.parse(conversionQueryMap).orElse(null)
145                     );
146                     PnfdBlockParserFactory.getInstance().get(transformation)
147                         .ifPresent(pnfParser -> pnfParser.parse(templateFrom, templateTo));
148                 }
149             }
150         });
151     }
152
153     /**
154      * Parses a Customized Node Type that extend from a valid ONAP NodeType.
155      */
156     private void executeCustomTypeTransformations() {
157         final List<Transformation> transformationList =  transformationGroupByBlockMap
158             .get((TransformationBlock.CUSTOM_NODE_TYPE));
159         if (CollectionUtils.isEmpty(transformationList)) {
160             return;
161         }
162         transformationList.forEach(transformation ->
163             PnfdBlockParserFactory.getInstance().get(transformation).ifPresent(pnfdBlockParser ->
164                 pnfdBlockParser.parse(templateFrom, templateTo)));
165     }
166
167 }