3efde199b493627e6f2a7bc9fd7a691c1f67229b
[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 java.util.Collections;
23 import java.util.HashMap;
24 import java.util.Map;
25 import java.util.Optional;
26 import java.util.Set;
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;
34
35 public class PnfdCustomNodeTypeBlockParser extends AbstractPnfdBlockParser {
36
37     public PnfdCustomNodeTypeBlockParser(final Transformation transformation) {
38         super(transformation);
39     }
40
41     @Override
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();
47         }
48         return customNodeTypeMap.entrySet().stream()
49             .map(customNode -> {
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()));
55                 return map;
56             }).collect(Collectors.toSet());
57     }
58
59     @Override
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();
66         }
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());
75             }
76         }
77         return parsedNodeTemplate.isEmpty() ? Optional.empty() : Optional.of(parsedNodeTemplate);
78     }
79
80     @Override
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);
85         }
86     }
87
88 }