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.model.ConversionDefinition;
31 import org.openecomp.core.converter.pnfd.model.Transformation;
32 import org.openecomp.core.converter.pnfd.strategy.PnfdConversionStrategy;
33 import org.openecomp.sdc.tosca.services.DataModelUtil;
35 public class PnfdCustomNodeTypeBlockParser extends AbstractPnfdBlockParser {
37 public PnfdCustomNodeTypeBlockParser(final Transformation transformation) {
38 super(transformation);
42 protected Set<Map<String, Object>> findBlocksToParse() {
43 final Map<String, Object> nodeTemplateMap = templateFrom.getNodeTemplates();
44 final Map<String, Object> customNodeTypeMap = fetchCustomNodeType();
45 if (customNodeTypeMap.isEmpty() || MapUtils.isEmpty(nodeTemplateMap)) {
46 return Collections.emptySet();
48 return customNodeTypeMap.entrySet().stream()
50 final Map<String, Object> map = new HashMap<>();
51 nodeTemplateMap.entrySet().stream()
52 .filter(nodeTemplate ->
53 extractObjectValue(nodeTemplate.getValue()).equalsIgnoreCase(customNode.getKey()))
54 .forEach(nodeType -> map.put(nodeType.getKey(), nodeType.getValue()));
56 }).collect(Collectors.toSet());
60 protected Optional<Map<String, Object>> buildParsedBlock(final Map<String, Object> attributeQuery,
61 final Map<String, Object> fromNodeTemplateAttributeMap,
62 final ConversionDefinition conversionDefinition) {
63 //cannot query for more than one attribute
64 if (attributeQuery.keySet().size() > 1) {
65 return Optional.empty();
67 final String attribute = attributeQuery.keySet().iterator().next();
68 final Object queryValue = attributeQuery.get(attribute);
69 final Map<String, Object> parsedNodeTemplate = new HashMap<>();
70 if (queryValue == null) {
71 final PnfdConversionStrategy pnfdConversionStrategy = conversionDefinition.getPnfdConversionStrategy();
72 final Optional convertedAttribute = pnfdConversionStrategy.convert(attributeValueToBeConverted);
73 if (convertedAttribute.isPresent()) {
74 parsedNodeTemplate.put(conversionDefinition.getToAttributeName(), convertedAttribute.get());
77 return parsedNodeTemplate.isEmpty() ? Optional.empty() : Optional.of(parsedNodeTemplate);
81 protected void write(final String nodeTemplateName, final Map<String, Object> parsedTemplateMap) {
82 if (!parsedTemplateMap.isEmpty()) {
83 final NodeTemplate parsedNodeTemplate = NodeTemplateYamlParser.parse(parsedTemplateMap);
84 DataModelUtil.addNodeTemplate(templateTo, nodeTemplateName, parsedNodeTemplate);