4ce7b2e1c77733d4ca3543b8c8989d49e09a5d41
[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.parser;
21
22 import com.google.common.collect.ImmutableMap;
23 import java.util.Collections;
24 import java.util.HashMap;
25 import java.util.Map;
26 import java.util.Optional;
27 import java.util.Set;
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.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.pnfd.strategy.PnfdConversionStrategy;
36 import org.openecomp.sdc.tosca.services.DataModelUtil;
37
38 public class PnfdInputBlockParser extends AbstractPnfdBlockParser {
39
40     public PnfdInputBlockParser(final Transformation transformation) {
41         super(transformation);
42     }
43
44     @Override
45     protected Optional<Map<String, Object>> buildParsedBlock(final Map<String, Object> attributeQuery,
46         final Map<String, Object> originalAttributeMap, final ConversionDefinition conversionDefinition) {
47         //cannot query for more than one attribute
48         if (attributeQuery.keySet().size() > 1) {
49             return Optional.empty();
50         }
51         final String attribute = attributeQuery.keySet().iterator().next();
52         final Map<String, Object> parsedInput = new HashMap<>();
53         if (attributeQuery.get(attribute) == null) {
54             final PnfdConversionStrategy pnfdConversionStrategy = conversionDefinition.getPnfdConversionStrategy();
55             final Optional convertedAttribute = pnfdConversionStrategy.convert(originalAttributeMap.get(attribute));
56             convertedAttribute.ifPresent(convertedAttribute1 -> parsedInput.put(conversionDefinition.getToAttributeName(), convertedAttribute1));
57         } else {
58             final Optional<Map<String, Object>> builtInput = buildParsedBlock((Map<String, Object>) attributeQuery.get(attribute),
59                 (Map<String, Object>) originalAttributeMap.get(attribute), conversionDefinition);
60             builtInput.ifPresent(builtInput1 -> parsedInput.put(attribute, builtInput1));
61         }
62
63         return parsedInput.isEmpty() ? Optional.empty() : Optional.of(parsedInput);
64     }
65
66     @Override
67     protected void write(final String blockName, final Map<String, Object> parsedBlockYamlObject) {
68         if (!parsedBlockYamlObject.isEmpty()) {
69             final ParameterDefinition parameterDefinition = ParameterDefinitionYamlParser.parse(parsedBlockYamlObject);
70             DataModelUtil.addInputParameterToTopologyTemplate(templateTo, blockName, parameterDefinition);
71         }
72     }
73
74     @Override
75     protected Set<Map<String, Object>> findBlocksToParse() {
76         final ConversionQuery conversionQuery = transformation.getConversionQuery();
77         final Map<String, Object> inputsMap = templateFrom.getInputs();
78         if (MapUtils.isEmpty(inputsMap)) {
79             return Collections.emptySet();
80         }
81
82         return inputsMap.entrySet().stream()
83             .filter(inputMapEntry -> PnfdQueryExecutor
84                 .find(conversionQuery, ImmutableMap.of(inputMapEntry.getKey(), inputMapEntry.getValue()))
85             )
86             .map(inputMapEntry -> {
87                 final Map<String, Object> map = new HashMap<>();
88                 map.put(inputMapEntry.getKey(), inputMapEntry.getValue());
89                 return map;
90             }).collect(Collectors.toSet());
91     }
92
93 }