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
10 * * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * * SPDX-License-Identifier: Apache-2.0
19 * * ============LICENSE_END=========================================================
23 package org.openecomp.core.impl;
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;
32 import java.util.HashMap;
33 import java.util.List;
35 import java.util.Objects;
36 import java.util.stream.Collectors;
38 public class ToscaSolConverterPnf extends AbstractToscaSolConverter {
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";
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
57 public void convertTopologyTemplate(ServiceTemplate serviceTemplate, ServiceTemplateReaderService readerService) {
58 convertNodeTemplatesToEcompTypes(serviceTemplate, readerService);
59 addEmptyNodeTemplatesIfNoneDefined(serviceTemplate);
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.
67 * @param serviceTemplate
68 * @param readerService
70 private void convertNodeTemplatesToEcompTypes(ServiceTemplate serviceTemplate,
71 ServiceTemplateReaderService readerService) {
72 Map<String, Object> nodeTemplates = readerService.getNodeTemplates();
73 if (MapUtils.isEmpty(nodeTemplates)) {
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);
84 addEmptyNodeTemplatesIfNoneDefined(serviceTemplate);
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
93 * All other data e.g. remaining properties, requirements, capabilities are
94 * not mapped over to ecomp equivalent
97 * @return ecomp v2.extCP node type
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);
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());
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);
132 private boolean isNotEmpty(List<?> list) {
133 return !list.isEmpty();
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);
142 if (topologyTemplate.getNode_templates() == null) {
143 topologyTemplate.setNode_templates(new HashMap<>());