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
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.
16 * SPDX-License-Identifier: Apache-2.0
17 * ============LICENSE_END=========================================================
20 package org.openecomp.core.converter.impl.pnfd;
22 import java.util.Collections;
23 import java.util.HashMap;
24 import java.util.List;
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;
39 * Engine that manages the PNF Descriptor transformation process for the NodeTemplate block.
41 public class PnfdNodeTemplateTransformationEngine extends AbstractPnfdTransformationEngine {
43 public PnfdNodeTemplateTransformationEngine(final ServiceTemplateReaderService templateFrom,
44 final ServiceTemplate templateTo) {
45 super(templateFrom, templateTo);
48 //used for tests purposes
49 PnfdNodeTemplateTransformationEngine(final ServiceTemplateReaderService templateFrom,
50 final ServiceTemplate templateTo,
51 final String descriptorResourcePath) {
52 super(templateFrom, templateTo, descriptorResourcePath);
56 * Runs the transformation process.
58 public void transform() {
60 initializeTopologyTemplate();
61 executeTransformations();
65 * Initializes the topology template and its node template set.
67 private void initializeTopologyTemplate() {
68 TopologyTemplate topologyTemplate = templateTo.getTopology_template();
69 if (topologyTemplate == null) {
70 topologyTemplate = new TopologyTemplate();
71 templateTo.setTopology_template(topologyTemplate);
73 if (topologyTemplate.getNode_templates() == null) {
74 topologyTemplate.setNode_templates(new HashMap<>());
80 * Execute all transformations specified in the descriptor.
83 protected void executeTransformations() {
84 if (transformationDescription == null) {
87 final Set<Transformation> transformationSet = transformationDescription.getTransformationSet();
88 if (CollectionUtils.isEmpty(transformationSet)) {
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);
100 * Parses all topology_template node_template.
102 * A map containing any input that was called with a get_input TOSCA function and its getInputFunction
103 * transformation name
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();
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());
120 return inputsToConvertMap;
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
127 private void executeGetInputFunctionTransformations(final Map<String, String> inputsToConvertMap) {
128 final List<Transformation> transformationListOfGetInputFunction = transformationGroupByBlockMap
129 .get(TransformationBlock.GET_INPUT_FUNCTION);
131 if(MapUtils.isEmpty(inputsToConvertMap) || CollectionUtils.isEmpty(transformationListOfGetInputFunction)) {
135 final Map<String, List<Transformation>> transformationByName = transformationListOfGetInputFunction.stream()
136 .collect(Collectors.groupingBy(Transformation::getName));
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)
149 PnfdBlockParserFactory.getInstance().get(transformation)
150 .ifPresent(pnfParser -> pnfParser.parse(templateFrom, templateTo));
157 * Parses a Customized Node Type that extend from a valid ONAP NodeType.
159 private void executeCustomTypeTransformations() {
160 final List<Transformation> transformationList = transformationGroupByBlockMap
161 .get((TransformationBlock.CUSTOM_NODE_TYPE));
162 if (CollectionUtils.isEmpty(transformationList)) {
165 transformationList.forEach(transformation ->
166 PnfdBlockParserFactory.getInstance().get(transformation).ifPresent(pnfdBlockParser ->
167 pnfdBlockParser.parse(templateFrom, templateTo)));