aboutsummaryrefslogtreecommitdiffstats
path: root/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/ToscaSolConverterPnf.java
blob: a3935ee7359da90754326299f02c977b19db7fde (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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*
 * -
 *  * ============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.impl;

import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.CONSTRAINTS;
import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.DEFAULT_VALUE;
import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.DESCRIPTION;
import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.ENTRY_SCHEMA;
import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.GET_INPUT;
import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.PROPERTIES;
import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.REQUIRED;
import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.TYPE;

import java.util.AbstractMap;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import org.apache.commons.collections.MapUtils;
import org.onap.sdc.tosca.datatypes.model.Constraint;
import org.onap.sdc.tosca.datatypes.model.EntrySchema;
import org.onap.sdc.tosca.datatypes.model.NodeTemplate;
import org.onap.sdc.tosca.datatypes.model.ParameterDefinition;
import org.onap.sdc.tosca.datatypes.model.ServiceTemplate;
import org.onap.sdc.tosca.datatypes.model.TopologyTemplate;
import org.openecomp.core.converter.ServiceTemplateReaderService;
import org.openecomp.sdc.tosca.services.DataModelUtil;

public class ToscaSolConverterPnf extends AbstractToscaSolConverter {

    private static final String PNF_EXT_CP_TYPE = "tosca.nodes.nfv.PnfExtCp";
    private static final String EXT_CP_TYPE = "org.openecomp.resource.cp.v2.extCP";
    private static final String LAYER_PROTOCOLS = "layer_protocols";
    private static final String ASSIGNMENT_METHOD = "assingment_method";
    private static final String DHCP = "dhcp";
    private static final String IP_V4 = "ipv4";
    private static final String IP_V6 = "ipv6";
    private static final String IP_VERSION = "ip_version";
    private static final String IP_REQUIREMENTS = "ip_requirements";
    private static final String IP_REQUIREMENTS_TYPE = "org.openecomp.datatypes.network.IpRequirements";
    private ServiceTemplate serviceTemplate;
    private ServiceTemplateReaderService readerService;

    /**
     * For PNF the node templates are converted ETSI node types to ecomp node types. All other data i.e. inputs,
     * substitution mappings and outputs are simply dropped at this stage. The equivalent ecomp data will be added when
     * the vsp is imported into the catalog.
     *
     * @param serviceTemplate - the service template
     * @param readerService - the reader service
     */
    @Override
    public void convertTopologyTemplate(final ServiceTemplate serviceTemplate,
            final ServiceTemplateReaderService readerService) {
        this.serviceTemplate = serviceTemplate;
        this.readerService = readerService;
        convertNodeTemplatesToEcompTypes();
        addEmptyNodeTemplatesIfNoneDefined();
    }

    /**
     * Copies a input from the reader (input) to the service template (output)
     * @param inputName     the name of the input to copy
     */
    private void copyTopologyTemplateInput(final String inputName) {
        final Map<String, Object> inputMap = readerService.getInputs();
        if (MapUtils.isEmpty(inputMap)) {
            return;
        }
        final Map propertyMap = (Map) inputMap.get(inputName);
        final Object requiredObj = propertyMap.get(REQUIRED.getElementName());
        final Object constraintsObj = propertyMap.get(CONSTRAINTS.getElementName());
        final Object entrySchemaObj = propertyMap.get(ENTRY_SCHEMA.getElementName());

        EntrySchema entrySchema = null;
        if (entrySchemaObj instanceof Map) {
            final Map entrySchemaMap = ((Map) entrySchemaObj);
            entrySchema = parseEntrySchema(entrySchemaMap);
        }
        final ParameterDefinition parameterDefinition =
            DataModelUtil.createParameterDefinition(
                (String) propertyMap.get(TYPE.getElementName()),
                (String) propertyMap.get(DESCRIPTION.getElementName()),
                requiredObj instanceof Boolean ? (Boolean) requiredObj : null,
                constraintsObj instanceof List ? (List<Constraint>) constraintsObj : null,
                entrySchema,
                propertyMap.get(DEFAULT_VALUE.getElementName()));

        DataModelUtil
            .addInputParameterToTopologyTemplate(serviceTemplate, inputName, parameterDefinition);
    }

    /**
     * PNF only has nfv.PNF and nfv.PnfExtCp types defined in ETSI SOL001 v2.5.1. - The PNF is mapped to the outer
     * Abstract PNF container in ecomp model and hence nfv.PNF is dropped here. - nfv.PnfExtCp is mapped to ecomp
     * v2.extCp type.
     */
    private void convertNodeTemplatesToEcompTypes() {
        final Map<String, Object> nodeTemplates = readerService.getNodeTemplates();
        if (MapUtils.isEmpty(nodeTemplates)) {
            return;
        }

        nodeTemplates.entrySet().stream()
            .filter(nodeTemplateEntry ->
                PNF_EXT_CP_TYPE.equals(((Map<String, Object>) nodeTemplateEntry.getValue()).get(TYPE.getElementName())))
            .forEach(nodeTemplateEntry ->
                DataModelUtil.addNodeTemplate(serviceTemplate, nodeTemplateEntry.getKey(),
                    convertToEcompConnectionPointNodeType((Map<String, Object>) nodeTemplateEntry.getValue())));
    }

    /**
     * Converts from the ETSI PnfExtCp node type to ecomp v2.extCP node type The following properties are mapped -
     * layer_protocols is mapped to ip_requirements if it contains the values ipv4 and/or ipv6. All other data e.g.
     * remaining properties, requirements, capabilities are not mapped over to ecomp equivalent
     *
     * @param pnfExtCp - the ETSI PnfExtCp map
     * @return ecomp v2.extCP node type
     */
    private NodeTemplate convertToEcompConnectionPointNodeType(final Map<String, Object> pnfExtCp) {
        final NodeTemplate nodeTemplate = new NodeTemplate();
        nodeTemplate.setType(EXT_CP_TYPE);
        final Map<String, Object> properties = (Map<String, Object>) pnfExtCp.get(PROPERTIES.getElementName());

        properties.entrySet().stream()
            .filter(propertyMap -> LAYER_PROTOCOLS.equals(propertyMap.getKey()))
            .forEach(propertyMap -> {
                final Object propertyValue = propertyMap.getValue();
                if (propertyValue instanceof List) {
                    // layer_protocols: [ ipv4, ipv6, ... ]
                    final List<Map<String, Object>> ipRequirements =
                        convertToIpRequirementsProperty((List<String>) propertyValue);
                    if (!ipRequirements.isEmpty()) {
                        final Map<String, Object> convertedProperties = new HashMap<>();
                        convertedProperties.put(IP_REQUIREMENTS, ipRequirements);
                        nodeTemplate.setProperties(convertedProperties);
                    }
                } else if (propertyValue instanceof AbstractMap) {
                    final Map propertyValueMap = (Map) propertyValue;
                    if (propertyValueMap.containsKey(GET_INPUT.getElementName())) {
                        // layer_protocols: {get_input: anInputName}
                        final Map<String, Object> convertedProperties = new HashMap<>();
                        convertedProperties.put(IP_REQUIREMENTS, propertyValueMap);
                        nodeTemplate.setProperties(convertedProperties);
                        final String getInputValue = (String) propertyValueMap.get(GET_INPUT.getElementName());
                        if (!isInputAlreadyAdded(getInputValue)) {
                            copyTopologyTemplateInput(getInputValue);
                            parseLayerProtocolsInputToIpRequirements(getInputValue);
                        }
                    }
                }
            });
        return nodeTemplate;
    }

    /**
     * Checks if a topology template input was already added
     *
     * @param inputName     The name of the input to check
     * @return
     *  {@code true} if the input was found in the topology template structure
     */
    private boolean isInputAlreadyAdded(final String inputName) {
        final TopologyTemplate topologyTemplate = serviceTemplate.getTopology_template();
        if (topologyTemplate == null) {
            return false;
        }

        final Map<String, ParameterDefinition> inputMap = topologyTemplate.getInputs();
        if (MapUtils.isNotEmpty(inputMap)) {
            return inputMap.keySet().contains(inputName);
        }

        return false;
    }

    /**
     * Parses a layer_protocol input to org.openecomp.datatypes.network.IpRequirements ecomp type.
     *
     * @param inputName     The name of the input to parse
     *
     */
    private void parseLayerProtocolsInputToIpRequirements(final String inputName) {
        final TopologyTemplate topologyTemplate = serviceTemplate.getTopology_template();
        final ParameterDefinition layerProtocolsInput = topologyTemplate.getInputs().get(inputName);
        final EntrySchema entrySchema = layerProtocolsInput.getEntry_schema();
        entrySchema.setType(IP_REQUIREMENTS_TYPE);
        final List<String> defaultLayerProtocolList = (List<String>) layerProtocolsInput.get_default();
        layerProtocolsInput.set_default(convertToIpRequirementsProperty(defaultLayerProtocolList));
    }

    /**
     * Converts each layer_protocols entry that is either {@link #IP_V4} or {@link #IP_V6} to the ecomp
     * {@link #IP_REQUIREMENTS_TYPE}, ignoring other entry types.
     *
     * @param layerProtocols    the PnfExtCp layer_protocols list
     * @return
     *  A list of map representing a {@link #IP_REQUIREMENTS_TYPE} ecomp type
     */
    private List<Map<String, Object>> convertToIpRequirementsProperty(final List<String> layerProtocols) {
        return layerProtocols.stream()
            .filter(layerProtocol -> IP_V4.equals(layerProtocol) || IP_V6.equals(layerProtocol))
            .map(this::createIpRequirementsEntry)
            .collect(Collectors.toList());
    }

    /**
     * Creates a {@link #IP_REQUIREMENTS_TYPE} based on the ip version
     * @param ipVersion  the provided ip version, either {@link #IP_V4} or {@link #IP_V6}
     * @return
     *  A map representing an {@link #IP_REQUIREMENTS_TYPE} ecomp type
     */
    private Map<String, Object> createIpRequirementsEntry(final String ipVersion) {
        final int version = IP_V4.equals(ipVersion) ? 4 : 6;
        final Map<String, Object> result = new HashMap<>();
        result.put(IP_VERSION, version);
        result.put(ASSIGNMENT_METHOD, DHCP);
        return result;
    }

    /**
     * Fills missing required entries in the service template. Checks for topology_template entry and
     * topology_template->node_templates entry.
     */
    private void addEmptyNodeTemplatesIfNoneDefined() {
        TopologyTemplate topologyTemplate = serviceTemplate.getTopology_template();
        if (Objects.isNull(topologyTemplate)) {
            topologyTemplate = new TopologyTemplate();
            serviceTemplate.setTopology_template(topologyTemplate);
        }
        if (topologyTemplate.getNode_templates() == null) {
            topologyTemplate.setNode_templates(new HashMap<>());
        }
    }

    /**
     * Parses an input entry schema
     *
     * @param entrySchemaMap    the descriptor input entry schema map
     * @return
     *  A parsed entry schema based on the provided map
     */
    private EntrySchema parseEntrySchema(Map entrySchemaMap) {
        return DataModelUtil.createEntrySchema((String) entrySchemaMap.get(TYPE.getElementName())
            , (String) entrySchemaMap.get(DESCRIPTION.getElementName())
            , (List<Constraint>) entrySchemaMap.get(CONSTRAINTS.getElementName())
        );
    }

}