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 com.google.common.collect.ImmutableMap;
23 import java.util.Collections;
24 import java.util.HashMap;
26 import java.util.Optional;
28 import java.util.stream.Collectors;
29 import org.apache.commons.collections.MapUtils;
30 import org.onap.sdc.tosca.datatypes.model.ParameterDefinition;
31 import org.openecomp.core.converter.pnfd.parser.AbstractPnfdBlockParser;
32 import org.openecomp.core.converter.impl.pnfd.PnfdQueryExecutor;
33 import org.openecomp.core.converter.pnfd.model.ConversionDefinition;
34 import org.openecomp.core.converter.pnfd.model.ConversionQuery;
35 import org.openecomp.core.converter.pnfd.model.Transformation;
36 import org.openecomp.core.converter.pnfd.strategy.PnfdConversionStrategy;
37 import org.openecomp.sdc.tosca.services.DataModelUtil;
39 public class PnfdInputBlockParser extends AbstractPnfdBlockParser {
41 public PnfdInputBlockParser(final Transformation transformation) {
42 super(transformation);
46 protected Optional<Map<String, Object>> buildParsedBlock(final Map<String, Object> attributeQuery,
47 final Map<String, Object> originalAttributeMap, final ConversionDefinition conversionDefinition) {
48 //cannot query for more than one attribute
49 if (attributeQuery.keySet().size() > 1) {
50 return Optional.empty();
52 final String attribute = attributeQuery.keySet().iterator().next();
53 final Map<String, Object> parsedInput = new HashMap<>();
54 if (attributeQuery.get(attribute) == null) {
55 final PnfdConversionStrategy pnfdConversionStrategy = conversionDefinition.getPnfdConversionStrategy();
56 final Optional convertedAttribute = pnfdConversionStrategy.convert(originalAttributeMap.get(attribute));
57 convertedAttribute.ifPresent(convertedAttribute1 -> parsedInput.put(conversionDefinition.getToAttributeName(), convertedAttribute1));
59 final Optional<Map<String, Object>> builtInput = buildParsedBlock((Map<String, Object>) attributeQuery.get(attribute),
60 (Map<String, Object>) originalAttributeMap.get(attribute), conversionDefinition);
61 builtInput.ifPresent(builtInput1 -> parsedInput.put(attribute, builtInput1));
64 return parsedInput.isEmpty() ? Optional.empty() : Optional.of(parsedInput);
68 protected void write(final String blockName, final Map<String, Object> parsedBlockYamlObject) {
69 if (!parsedBlockYamlObject.isEmpty()) {
70 final ParameterDefinition parameterDefinition = ParameterDefinitionYamlParser.parse(parsedBlockYamlObject);
71 DataModelUtil.addInputParameterToTopologyTemplate(templateTo, blockName, parameterDefinition);
76 protected Set<Map<String, Object>> findBlocksToParse() {
77 final ConversionQuery conversionQuery = transformation.getConversionQuery();
78 final Map<String, Object> inputsMap = templateFrom.getInputs();
79 if (MapUtils.isEmpty(inputsMap)) {
80 return Collections.emptySet();
83 return inputsMap.entrySet().stream()
84 .filter(inputMapEntry -> PnfdQueryExecutor
85 .find(conversionQuery, ImmutableMap.of(inputMapEntry.getKey(), inputMapEntry.getValue()))
87 .map(inputMapEntry -> {
88 final Map<String, Object> map = new HashMap<>();
89 map.put(inputMapEntry.getKey(), inputMapEntry.getValue());
91 }).collect(Collectors.toSet());