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.parser;
22 import java.util.Collections;
23 import java.util.HashMap;
25 import java.util.Optional;
27 import java.util.stream.Collectors;
28 import org.apache.commons.collections.MapUtils;
29 import org.onap.sdc.tosca.datatypes.model.NodeTemplate;
30 import org.openecomp.core.converter.pnfd.parser.AbstractPnfdBlockParser;
31 import org.openecomp.core.converter.impl.pnfd.PnfdQueryExecutor;
32 import org.openecomp.core.converter.pnfd.model.ConversionDefinition;
33 import org.openecomp.core.converter.pnfd.model.ConversionQuery;
34 import org.openecomp.core.converter.pnfd.model.Transformation;
35 import org.openecomp.core.converter.impl.pnfd.strategy.CopyConversionStrategy;
36 import org.openecomp.core.converter.pnfd.strategy.PnfdConversionStrategy;
37 import org.openecomp.sdc.tosca.services.DataModelUtil;
39 public class PnfdNodeTemplateBlockParser extends AbstractPnfdBlockParser {
41 private Map<String, String> inputNameToConvertMap = new HashMap<>();
43 public PnfdNodeTemplateBlockParser(final Transformation transformation) {
44 super(transformation);
47 public Optional<Map<String, Object>> buildParsedBlock(final Map<String, Object> attributeQuery,
48 final Map<String, Object> fromNodeTemplateAttributeMap,
49 final ConversionDefinition conversionDefinition) {
50 //cannot query for more than one attribute
51 if (attributeQuery.keySet().size() > 1) {
52 return Optional.empty();
54 final String attribute = attributeQuery.keySet().iterator().next();
55 final Object queryValue = attributeQuery.get(attribute);
56 final Object attributeValueToConvert = fromNodeTemplateAttributeMap.get(attribute);
57 if (queryValue == null) {
58 PnfdConversionStrategy pnfdConversionStrategy = conversionDefinition.getPnfdConversionStrategy();
59 if (isGetInputFunction(attributeValueToConvert)) {
60 inputNameToConvertMap.put(extractGetInputFunctionValue(attributeValueToConvert)
61 , conversionDefinition.getToGetInput()
63 pnfdConversionStrategy = new CopyConversionStrategy();
65 final Map<String, Object> parsedNodeTemplate = new HashMap<>();
66 final Optional convertedAttribute = pnfdConversionStrategy.convert(attributeValueToConvert);
67 if (convertedAttribute.isPresent()) {
68 parsedNodeTemplate.put(conversionDefinition.getToAttributeName(), convertedAttribute.get());
71 return parsedNodeTemplate.isEmpty() ? Optional.empty() : Optional.of(parsedNodeTemplate);
73 if (!(queryValue instanceof Map) || !(attributeValueToConvert instanceof Map)) {
74 return Optional.empty();
76 final Map<String, Object> parsedNodeTemplate = new HashMap<>();
77 final Optional<Map<String, Object>> builtNodeTemplate = buildParsedBlock(
78 (Map<String, Object>) queryValue,
79 (Map<String, Object>) attributeValueToConvert, conversionDefinition);
80 builtNodeTemplate.ifPresent(builtNodeTemplate1 -> parsedNodeTemplate.put(attribute, builtNodeTemplate1));
82 return parsedNodeTemplate.isEmpty() ? Optional.empty() : Optional.of(parsedNodeTemplate);
86 protected Set<Map<String, Object>> findBlocksToParse() {
87 final ConversionQuery conversionQuery = transformation.getConversionQuery();
88 final Map<String, Object> nodeTemplateMap = templateFrom.getNodeTemplates();
89 if (MapUtils.isEmpty(nodeTemplateMap)) {
90 return Collections.emptySet();
93 return nodeTemplateMap.entrySet().stream()
94 .filter(mapEntry -> PnfdQueryExecutor.find(conversionQuery, mapEntry.getValue()))
95 .map(stringObjectEntry -> {
96 final Map<String, Object> map = new HashMap<>();
97 map.put(stringObjectEntry.getKey(), stringObjectEntry.getValue());
99 }).collect(Collectors.toSet());
103 public void write(final String nodeTemplateName, final Map<String, Object> parsedNodeTemplateMap) {
104 if (!parsedNodeTemplateMap.isEmpty()) {
105 final NodeTemplate parsedNodeTemplate = NodeTemplateYamlParser.parse(parsedNodeTemplateMap);
106 DataModelUtil.addNodeTemplate(templateTo, nodeTemplateName, parsedNodeTemplate);
111 public Optional<Map<String, String>> getInputAndTransformationNameMap() {
112 return Optional.of(inputNameToConvertMap);