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 static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.DERIVED_FROM;
24 import java.util.Collections;
25 import java.util.HashMap;
27 import java.util.Optional;
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;
37 public class PnfdNodeTypeBlockParser extends AbstractPnfdBlockParser {
39 public PnfdNodeTypeBlockParser(final Transformation transformation) {
40 super(transformation);
44 protected Set<Map<String, Object>> findBlocksToParse() {
45 final Map<String, Object> customNodeTypeMap = fetchCustomNodeType();
46 if (customNodeTypeMap.isEmpty()) {
47 return Collections.emptySet();
50 final String nodeNamePrefix =
51 transformation.getPropertyValue(TransformationPropertyType.NODE_NAME_PREFIX, String.class)
54 return customNodeTypeMap.entrySet().parallelStream()
56 final Map<String, Object> map = new HashMap<>();
57 map.put(nodeNamePrefix.concat(nodeEntry.getKey()), nodeEntry.getValue());
59 }).collect(Collectors.toSet());
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();
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());
83 return parsedNodeTemplate.isEmpty() ? Optional.empty() : Optional.of(parsedNodeTemplate);
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);