summaryrefslogtreecommitdiffstats
path: root/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/converter/impl/pnfd/parser/AbstractPnfdBlockParser.java
blob: 056d07aed01948f5471e0f6de268ea8412acc987 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2019 Nordix Foundation
 *  ================================================================================
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *  SPDX-License-Identifier: Apache-2.0
 *  ============LICENSE_END=========================================================
 */

package org.openecomp.core.converter.impl.pnfd.parser;

import com.google.common.collect.ImmutableMap;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import org.apache.commons.collections.MapUtils;
import org.onap.sdc.tosca.datatypes.model.ServiceTemplate;
import org.openecomp.core.converter.ServiceTemplateReaderService;
import org.openecomp.core.converter.impl.pnfd.PnfdQueryExecutor;
import org.openecomp.core.converter.pnfd.model.ConversionDefinition;
import org.openecomp.core.converter.pnfd.model.ConversionQuery;
import org.openecomp.core.converter.pnfd.model.PnfTransformationToken;
import org.openecomp.core.converter.pnfd.model.Transformation;
import org.openecomp.core.converter.pnfd.parser.PnfdBlockParser;

public abstract class AbstractPnfdBlockParser implements PnfdBlockParser {

    protected final Transformation transformation;
    protected ServiceTemplateReaderService templateFrom;
    protected ServiceTemplate templateTo;
    protected Object attributeValueToBeConverted = "";

    public AbstractPnfdBlockParser(final Transformation transformation) {
        this.transformation = transformation;
    }

    /**
     * Parses a PNFD block based on the {@link Transformation} provided during the {@link PnfdBlockParser}
     * instantiation.
     *
     * @param templateFrom the original PNFD template
     * @param templateTo the resulting PNFD template
     */
    public void parse(final ServiceTemplateReaderService templateFrom, final ServiceTemplate templateTo) {
        this.templateFrom = templateFrom;
        this.templateTo = templateTo;
        final Set<Map<String, Object>> blockToParseSet = findBlocksToParse();
        if (!blockToParseSet.isEmpty()) {
            blockToParseSet.forEach(this::parse);
        }
    }

    /**
     * Applies all specified conversions in {@link Transformation#getConversionDefinitionList()} for the given
     * blockYamlObject.
     *
     * @param blockYamlObject the block content as a YAML object
     */
    protected void parse(final Map<String, Object> blockYamlObject) {
        if (MapUtils.isEmpty(blockYamlObject)) {
            return;
        }
        final List<ConversionDefinition> conversionDefinitionList = transformation.getConversionDefinitionList();
        final Map<String, Object> parsedBlockYamlObject = new HashMap<>();
        final String blockName = blockYamlObject.keySet().iterator().next();
        conversionDefinitionList.stream()
            .filter(conversionDefinition -> conversionDefinition.getConversionQuery().isValidAttributeQuery())
            .forEach(conversionDefinition -> {
                final Map<String, Object> query =
                    (Map<String, Object>) conversionDefinition.getConversionQuery().getQuery();
                final Map<String, Object> blockAttributeMap = (Map<String, Object>) blockYamlObject.get(blockName);
                final Optional<Map<String, Object>> parsedBlockAttributeMap = buildParsedBlock(query, blockAttributeMap
                    , conversionDefinition);
                parsedBlockAttributeMap.ifPresent(convertedNodeTemplateAttributeMap1 ->
                    mergeYamlObjects(parsedBlockYamlObject, convertedNodeTemplateAttributeMap1)
                );
            });

        write(blockName, parsedBlockYamlObject);
    }

    /**
     * Writes the block in the resulting {@link ServiceTemplate} {@link #templateTo}.
     *
     * @param blockName the name of the block
     * @param parsedBlockYamlObject the block content as a YAML object
     */
    protected abstract void write(final String blockName, final Map<String, Object> parsedBlockYamlObject);

    /**
     * Uses the provided attribute query to find a attribute in the original YAML object and apply the provided
     * conversion.
     *
     * @param attributeQuery the attribute query
     * @param fromNodeTemplateAttributeMap the original YAML object
     * @param conversionDefinition the conversion
     * @return the rebuilt original YAML object with the converted attribute
     */
    protected abstract Optional<Map<String, Object>> buildParsedBlock(final Map<String, Object> attributeQuery,
                                                                      final Map<String, Object> fromNodeTemplateAttributeMap,
                                                                      final ConversionDefinition conversionDefinition);

    /**
     * Merges two YAML objects.
     *
     * @param originalMap original YAML object
     * @param toBeMergedMap YAML object to be merged
     * @return the new YAML object representing the merge result.
     */
    protected Map<String, Object> mergeYamlObjects(final Map<String, Object> originalMap,
        final Map<String, Object> toBeMergedMap) {
        toBeMergedMap.forEach(
            (key, value) -> originalMap.merge(key, value,
                (toBeMergedValue, originalValue) -> {
                    if (originalValue instanceof Map) {
                        return mergeYamlObjects((Map) originalValue, (Map) toBeMergedValue);
                    }
                    return originalValue;
                })
        );

        return originalMap;
    }

    /**
     * Executes the provided {@link #transformation getConversionQuery} YAML query to find the blocks to be parsed in
     * {@link #templateFrom}.
     *
     * @return The YAML blocks found
     */
    protected abstract Set<Map<String, Object>> findBlocksToParse();

    /**
     * Checks if the YAML object is a TOSCA get_input call
     *
     * @param yamlObject the YAML object
     * @return {@code true} if the YAML object is a TOSCA get_input call, {@code false} otherwise
     */
    protected boolean isGetInputFunction(final Object yamlObject) {
        if (yamlObject instanceof Map) {
            final Map<String, Object> yamlMap = (Map<String, Object>) yamlObject;
            return yamlMap.containsKey(PnfTransformationToken.GET_INPUT.getName());
        }

        return false;
    }

    /**
     * Extracts the value from an YAML Object.
     * @param yamlObject
     * @return The Object value from the yamlObject parameter.
     */
    protected String extractObjectValue(final Object yamlObject) {
        if (yamlObject instanceof Map) {
            final Map<String, Object> yamlMap = (Map<String, Object>) yamlObject;
            return (String) yamlMap.values().stream().findFirst().orElse(null);
        }

        return null;
    }

    /**
     * Gets the stored input names called with TOSCA get_input function and its transformation configured in {@link
     * ConversionDefinition#getToGetInput()}
     */
    public Optional<Map<String, String>> getInputAndTransformationNameMap() {
        return Optional.empty();
    }

    /**
     * Finds all the derived node types from the provided node types.
     *
     * @param rootNodeTypeMap a map with the root node types to find the derived ones
     * @param derivedNodeTypeMap a map that will be filled with the derived node types
     */
    private void findAllDerivedNodeType(final Map<String, Object> rootNodeTypeMap,
                                          final Map<String, Object> derivedNodeTypeMap) {
        templateFrom.getNodeTypes().entrySet().stream()
            .filter(nodeEntry -> rootNodeTypeMap.containsKey(extractObjectValue(nodeEntry.getValue())))
            .forEach(nodeEntry -> {
                if (!derivedNodeTypeMap.containsKey(nodeEntry.getKey())) {
                    derivedNodeTypeMap.put(nodeEntry.getKey(), nodeEntry.getValue());
                    final ImmutableMap<String, Object> newRootNodeTypeMap = ImmutableMap
                        .of(nodeEntry.getKey(), nodeEntry.getValue());
                    findAllDerivedNodeType(newRootNodeTypeMap, derivedNodeTypeMap);
                }
            });
    }

    /**
     * Fetches all Custom NodeTypes based on the query result.
     * @return a map with all custom Node Types that matches with the query result.
     */
    protected Map<String, Object> fetchCustomNodeType() {
        final Map<String, Object> nodeTypesMap = templateFrom.getNodeTypes();
        if (MapUtils.isEmpty(nodeTypesMap)) {
            return Collections.emptyMap();
        }
        final ConversionQuery conversionQuery = transformation.getConversionQuery();
        final Map<String, Object> customNodeTypesMap = new HashMap<>();
        nodeTypesMap.entrySet().stream()
            .filter(nodeEntry -> PnfdQueryExecutor.find(conversionQuery, nodeEntry.getValue()))
            .forEach(customNode -> {
                attributeValueToBeConverted = extractObjectValue(customNode.getValue());
                customNodeTypesMap.put(customNode.getKey(), customNode.getValue());
            });
        final Map<String, Object> childNodeTypeMap = new HashMap<>();
        findAllDerivedNodeType(customNodeTypesMap, childNodeTypeMap);
        customNodeTypesMap.putAll(childNodeTypeMap);
        return customNodeTypesMap;
    }

}