3c996462fee59b36fd9c9e0a5fa19ed34d3af47d
[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         if (transformationDescription == null) {
85             return;
86         }
87         final Set<Transformation> transformationSet = transformationDescription.getTransformationSet();
88         if (CollectionUtils.isEmpty(transformationSet)) {
89             return;
90         }
91         transformationGroupByBlockMap = transformationSet.stream()
92             .filter(Transformation::isValid)
93             .collect(Collectors.groupingBy(Transformation::getBlock));
94         executeCustomTypeTransformations();
95         final Map<String, String> inputsToConvertMap = executeNodeTemplateTransformations();
96         executeGetInputFunctionTransformations(inputsToConvertMap);
97     }
98
99     /**
100      * Parses all topology_template node_template.
101      * @return
102      *  A map containing any input that was called with a get_input TOSCA function and its getInputFunction
103      *  transformation name
104      */
105     private Map<String, String> executeNodeTemplateTransformations() {
106         final List<Transformation> transformationList = transformationGroupByBlockMap
107             .get(TransformationBlock.NODE_TEMPLATE);
108         if (CollectionUtils.isEmpty(transformationList)) {
109             return Collections.emptyMap();
110         }
111
112         final Map<String, String> inputsToConvertMap = new HashMap<>();
113         transformationList.forEach(transformation ->
114             PnfdBlockParserFactory.getInstance().get(transformation).ifPresent(pnfParser -> {
115                 pnfParser.parse(templateFrom, templateTo);
116                 if (pnfParser.getInputAndTransformationNameMap().isPresent()) {
117                     inputsToConvertMap.putAll(pnfParser.getInputAndTransformationNameMap().get());
118                 }
119             }));
120         return inputsToConvertMap;
121     }
122
123     /**
124      * Parses all topology_template inputs called with a get_input TOSCA function.
125      * @param inputsToConvertMap    A map containing the topology_template input name and its conversion definition name
126      */
127     private void executeGetInputFunctionTransformations(final Map<String, String> inputsToConvertMap) {
128         final List<Transformation> transformationListOfGetInputFunction = transformationGroupByBlockMap
129             .get(TransformationBlock.GET_INPUT_FUNCTION);
130
131         if(MapUtils.isEmpty(inputsToConvertMap) || CollectionUtils.isEmpty(transformationListOfGetInputFunction)) {
132             return;
133         }
134
135         final Map<String, List<Transformation>> transformationByName = transformationListOfGetInputFunction.stream()
136             .collect(Collectors.groupingBy(Transformation::getName));
137
138         inputsToConvertMap.forEach((inputName, transformationName) -> {
139             final List<Transformation> transformationList = transformationByName.get(transformationName);
140             if (!CollectionUtils.isEmpty(transformationList)) {
141                 final Transformation transformation = transformationList.stream()
142                     .findFirst().orElse(null);
143                 if (transformation != null) {
144                     final Map<String, Object> conversionQueryMap = new HashMap<>();
145                     conversionQueryMap.put(inputName, null);
146                     transformation.setConversionQuery(
147                         ConversionQueryYamlParser.parse(conversionQueryMap).orElse(null)
148                     );
149                     PnfdBlockParserFactory.getInstance().get(transformation)
150                         .ifPresent(pnfParser -> pnfParser.parse(templateFrom, templateTo));
151                 }
152             }
153         });
154     }
155
156     /**
157      * Parses a Customized Node Type that extend from a valid ONAP NodeType.
158      */
159     private void executeCustomTypeTransformations() {
160         final List<Transformation> transformationList =  transformationGroupByBlockMap
161             .get((TransformationBlock.CUSTOM_NODE_TYPE));
162         if (CollectionUtils.isEmpty(transformationList)) {
163             return;
164         }
165         transformationList.forEach(transformation ->
166             PnfdBlockParserFactory.getInstance().get(transformation).ifPresent(pnfdBlockParser ->
167                 pnfdBlockParser.parse(templateFrom, templateTo)));
168     }
169
170 }