386bbfb5581ba4e357980a21b9487a3925050ab3
[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 static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.DERIVED_FROM;
23
24 import java.util.Collections;
25 import java.util.HashMap;
26 import java.util.Map;
27 import java.util.Optional;
28 import java.util.Set;
29 import java.util.stream.Collectors;
30 import org.onap.sdc.tosca.datatypes.model.NodeType;
31 import org.openecomp.core.converter.pnfd.model.ConversionDefinition;
32 import org.openecomp.core.converter.pnfd.model.Transformation;
33 import org.openecomp.core.converter.pnfd.model.TransformationPropertyType;
34 import org.openecomp.core.converter.pnfd.strategy.PnfdConversionStrategy;
35 import org.openecomp.sdc.tosca.services.DataModelUtil;
36
37 public class PnfdNodeTypeBlockParser extends AbstractPnfdBlockParser {
38
39     public PnfdNodeTypeBlockParser(final Transformation transformation) {
40         super(transformation);
41     }
42
43     @Override
44     protected Set<Map<String, Object>> findBlocksToParse() {
45         final Map<String, Object> customNodeTypeMap = fetchCustomNodeType();
46         if (customNodeTypeMap.isEmpty()) {
47             return Collections.emptySet();
48         }
49
50         final String nodeNamePrefix =
51             transformation.getPropertyValue(TransformationPropertyType.NODE_NAME_PREFIX, String.class)
52                 .orElse("");
53
54         return customNodeTypeMap.entrySet().parallelStream()
55             .map(nodeEntry -> {
56                 final Map<String, Object> map = new HashMap<>();
57                 map.put(nodeNamePrefix.concat(nodeEntry.getKey()), nodeEntry.getValue());
58                 return map;
59             }).collect(Collectors.toSet());
60     }
61
62     @Override
63     protected Optional<Map<String, Object>> buildParsedBlock(final Map<String, Object> attributeQuery,
64                                                              final Map<String, Object> fromNodeTemplateAttributeMap,
65                                                              final ConversionDefinition conversionDefinition) {
66         //cannot query for more than one attribute
67         if (attributeQuery.keySet().size() > 1) {
68             return Optional.empty();
69         }
70         final String attribute = attributeQuery.keySet().iterator().next();
71         final Object queryValue = attributeQuery.get(attribute);
72         final Object attributeValueToConvert = fromNodeTemplateAttributeMap.get(attribute);
73         final Map<String, Object> parsedNodeTemplate = new HashMap<>();
74         if (queryValue == null) {
75             PnfdConversionStrategy pnfdConversionStrategy = conversionDefinition.getPnfdConversionStrategy();
76             final Optional convertedAttribute = pnfdConversionStrategy
77                 .convert(DERIVED_FROM.getElementName()
78                     .equalsIgnoreCase(attribute) ? attributeValueToBeConverted : attributeValueToConvert);
79             if (convertedAttribute.isPresent()) {
80                 parsedNodeTemplate.put(conversionDefinition.getToAttributeName(), convertedAttribute.get());
81             }
82         }
83         return parsedNodeTemplate.isEmpty() ? Optional.empty() : Optional.of(parsedNodeTemplate);
84     }
85
86     @Override
87     protected void write(final String blockName, final Map<String, Object> parsedBlockYamlObject) {
88         if (!parsedBlockYamlObject.isEmpty()) {
89             final NodeType nodeTypeYamlParser = NodeTypeYamlParser.parse(parsedBlockYamlObject);
90             DataModelUtil.addNodeType(templateTo, blockName, nodeTypeYamlParser);
91         }
92     }
93
94 }