0258b42a3d9d62980b411772b9ed2aa0c9b634ce
[sdc.git] /
1 /*
2  * -
3  *  * ============LICENSE_START=======================================================
4  *  *  Copyright (C) 2019 Nordix Foundation.
5  *  * ================================================================================
6  *  * Licensed under the Apache License, Version 2.0 (the "License");
7  *  * you may not use this file except in compliance with the License.
8  *  * You may obtain a copy of the License at
9  *  *
10  *  *      http://www.apache.org/licenses/LICENSE-2.0
11  *  *
12  *  * Unless required by applicable law or agreed to in writing, software
13  *  * distributed under the License is distributed on an "AS IS" BASIS,
14  *  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  * See the License for the specific language governing permissions and
16  *  * limitations under the License.
17  *  *
18  *  * SPDX-License-Identifier: Apache-2.0
19  *  * ============LICENSE_END=========================================================
20  *
21  */
22
23 package org.openecomp.core.impl;
24
25 import org.apache.commons.collections.MapUtils;
26 import org.onap.sdc.tosca.datatypes.model.NodeTemplate;
27 import org.onap.sdc.tosca.datatypes.model.ServiceTemplate;
28 import org.onap.sdc.tosca.datatypes.model.TopologyTemplate;
29 import org.openecomp.core.converter.ServiceTemplateReaderService;
30 import org.openecomp.sdc.tosca.services.DataModelUtil;
31
32 import java.util.HashMap;
33 import java.util.List;
34 import java.util.Map;
35 import java.util.Objects;
36 import java.util.stream.Collectors;
37
38 public class ToscaSolConverterPnf extends AbstractToscaSolConverter {
39
40     private static final String PNF_EXT_CP_TYPE = "tosca.nodes.nfv.PnfExtCp";
41     private static final String EXT_CP_TYPE = "org.openecomp.resource.cp.v2.extCP";
42     private static final String LAYER_PROTOCOLS = "layer_protocols";
43     private static final String IP_V4 = "ipv4";
44     private static final String IP_V6 = "ipv6";
45     private static final String IP_VERSION = "ip_version";
46     private static final String ASSIGNMENT_METHOD = "assingment_method";
47     private static final String DHCP = "dhcp";
48
49     /**
50      * For PNF the node templates are converted ETSI node types to ecomp node types
51      * All other data i.e. inputs, substitution mappings and outputs are simply dropped at this stage. The equivalent
52      * ecomp data will be added when the vsp is imported into the catalog.
53      * @param serviceTemplate
54      * @param readerService
55      */
56     @Override
57     public void convertTopologyTemplate(ServiceTemplate serviceTemplate, ServiceTemplateReaderService readerService) {
58         convertNodeTemplatesToEcompTypes(serviceTemplate, readerService);
59         addEmptyNodeTemplatesIfNoneDefined(serviceTemplate);
60     }
61
62     /**
63      * PNF only has nfv.PNF and nfv.PnfExtCp types defined in ETSI SOL001 v2.5.1.
64      * - The PNF is mapped to the outer Abstract PNF container in ecomp model and hence nfv.PNF is dropped here.
65      * - nfv.PnfExtCp is mapped to ecomp v2.extCp type.
66      *
67      * @param serviceTemplate
68      * @param readerService
69      */
70     private void convertNodeTemplatesToEcompTypes(ServiceTemplate serviceTemplate,
71                                                   ServiceTemplateReaderService readerService) {
72         Map<String, Object> nodeTemplates = readerService.getNodeTemplates();
73         if (MapUtils.isEmpty(nodeTemplates)) {
74             return;
75         }
76
77         for (Map.Entry<String, Object> nodeTemplateEntry : nodeTemplates.entrySet()) {
78             Map<String, Object> inputNodeTemplate = (Map<String, Object>) nodeTemplateEntry.getValue();
79             if (PNF_EXT_CP_TYPE.equals((String) inputNodeTemplate.get("type"))) {
80                 NodeTemplate nodeTemplate = convertToEcompConnectionPointNodeType(inputNodeTemplate);
81                 DataModelUtil.addNodeTemplate(serviceTemplate, nodeTemplateEntry.getKey(), nodeTemplate);
82             }
83         }
84         addEmptyNodeTemplatesIfNoneDefined(serviceTemplate);
85     }
86
87
88     /**
89      * Converts from the ETSI PnfExtCp node type to ecomp v2.extCP node type
90      * The following properties are mapped
91      * - layer_protocols is mapped to ip_requirements if it contains the values ipv4 and/or ipv6
92      * <p>
93      * All other data e.g. remaining properties, requirements, capabilities are
94      * not mapped over to ecomp equivalent
95      *
96      * @param pnfExtCp
97      * @return ecomp v2.extCP node type
98      */
99     private NodeTemplate convertToEcompConnectionPointNodeType(Map<String, Object> pnfExtCp) {
100         NodeTemplate nodeTemplate = new NodeTemplate();
101         nodeTemplate.setType(EXT_CP_TYPE);
102         Map<String, Object> properties = (Map<String, Object>) pnfExtCp.get("properties");
103         for (Map.Entry<String, Object> property : properties.entrySet()) {
104             final String propertyName = property.getKey();
105             if (LAYER_PROTOCOLS.equals(propertyName)) {
106                 List<Map<String, Object>> ipRequirements = convertToIpRequirementsProperty((List<String>) property.getValue());
107                 if (isNotEmpty(ipRequirements)) {
108                     Map<String, Object> convertedProperties = new HashMap<>();
109                     convertedProperties.put("ip_requirements", ipRequirements);
110                     nodeTemplate.setProperties(convertedProperties);
111                 }
112             }
113         }
114         return nodeTemplate;
115     }
116
117     private List<Map<String, Object>> convertToIpRequirementsProperty(List<String> layerProtocols) {
118         return layerProtocols.stream()
119                 .filter(layerProtocol -> IP_V4.equals(layerProtocol) || IP_V6.equals(layerProtocol))
120                 .map(this::createIpPropertyElement)
121                 .collect(Collectors.toList());
122     }
123
124     private Map<String, Object> createIpPropertyElement(String ipVersion) {
125         final int version = ipVersion.equals(IP_V4) ? 4 : 6;
126         Map<String, Object> result = new HashMap<>();
127         result.put(IP_VERSION, version);
128         result.put(ASSIGNMENT_METHOD, DHCP);
129         return result;
130     }
131
132     private boolean isNotEmpty(List<?> list) {
133         return !list.isEmpty();
134     }
135
136     private void addEmptyNodeTemplatesIfNoneDefined(ServiceTemplate serviceTemplate) {
137         TopologyTemplate topologyTemplate = serviceTemplate.getTopology_template();
138         if (Objects.isNull(topologyTemplate)) {
139             topologyTemplate = new TopologyTemplate();
140             serviceTemplate.setTopology_template(topologyTemplate);
141         }
142         if (topologyTemplate.getNode_templates() == null) {
143             topologyTemplate.setNode_templates(new HashMap<>());
144         }
145     }
146
147 }