2 * Copyright 2016-2017, Nokia Corporation
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * 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.
17 package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.packagetransformer;
19 import com.google.common.annotations.VisibleForTesting;
20 import com.google.gson.Gson;
21 import com.google.gson.JsonArray;
22 import com.google.gson.JsonElement;
23 import com.google.gson.JsonObject;
25 import java.util.NoSuchElementException;
27 import java.util.regex.Pattern;
28 import org.slf4j.Logger;
29 import org.yaml.snakeyaml.Yaml;
31 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.child;
32 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.childElement;
33 import static org.slf4j.LoggerFactory.getLogger;
36 * Transforms a CBAM package into an ONAP package
38 public class OnapVnfdBuilder {
39 public static final String DESCRIPTION = "description";
40 public static final String PROPERTIES = "properties";
41 public static final String REQUIREMENTS = "requirements";
42 private static Logger logger = getLogger(OnapVnfdBuilder.class);
45 static String indent(String content, int prefixSize) {
46 StringBuilder sb = new StringBuilder();
47 for (int i = 0; i < prefixSize; i++) {
50 Pattern pattern = Pattern.compile("^(.*)$", Pattern.MULTILINE);
51 return pattern.matcher(content).replaceAll(sb.toString() + "$1");
54 private static String trimUnit(String data) {
55 //FIXME the unit should not be trimmed VF-C bug
56 return data.trim().replaceAll("[^0-9]", "");
60 * @param cbamVnfd the CBAM VNFD
61 * @return the converted ONAP VNFD
63 public String toOnapVnfd(String cbamVnfd) {
64 JsonObject root = new Gson().toJsonTree(new Yaml().load(cbamVnfd)).getAsJsonObject();
65 JsonObject topologyTemplate = child(root, "topology_template");
66 if (topologyTemplate.has("node_templates")) {
67 Set<Map.Entry<String, JsonElement>> nodeTemplates = child(topologyTemplate, "node_templates").entrySet();
68 StringBuilder body = new StringBuilder();
69 for (Map.Entry<String, JsonElement> node : nodeTemplates) {
70 String type = childElement(node.getValue().getAsJsonObject(), "type").getAsString();
71 if ("tosca.nodes.nfv.VDU".equals(type)) {
72 body.append(buildVdu(node.getKey(), node.getValue().getAsJsonObject(), nodeTemplates));
73 } else if ("tosca.nodes.nfv.VirtualStorage".equals(type)) {
74 body.append(buildVolume(node.getKey(), node.getValue().getAsJsonObject()));
75 } else if ("tosca.nodes.nfv.VL".equals(type)) {
76 body.append(buildVl(node.getKey()));
77 } else if ("tosca.nodes.nfv.ICP".equals(type)) {
78 body.append(buildIcp(node.getKey(), node.getValue().getAsJsonObject()));
79 } else if ("tosca.nodes.nfv.ECP".equals(type)) {
80 body.append(buildEcp(node.getKey(), node.getValue(), nodeTemplates));
82 logger.warn("The {} type is not converted", type);
85 return buildHeader(topologyTemplate) + body.toString();
87 return buildHeader(topologyTemplate);
90 private String buildHeader(JsonObject toplogyTemplate) {
91 JsonObject properties = child(child(toplogyTemplate, "substitution_mappings"), PROPERTIES);
92 String descriptorVersion = properties.get("descriptor_version").getAsString();
93 return "tosca_definitions_version: tosca_simple_yaml_1_0\n" +
97 " csarVersion: " + descriptorVersion + "\n" +
98 " csarProvider: " + properties.get("provider").getAsString() + "\n" +
100 " version: " + properties.get("software_version").getAsString() + "\n" +
101 " csarType: NFAR\n" +
102 " name: " + properties.get("product_name").getAsString() + "\n" +
103 " vnfdVersion: " + descriptorVersion + "\n\n" +
104 "topology_template:\n" +
105 " node_templates:\n";
108 private JsonElement get(String name, Set<Map.Entry<String, JsonElement>> nodes) {
109 for (Map.Entry<String, JsonElement> node : nodes) {
110 if (name.equals(node.getKey())) {
111 return node.getValue();
114 throw new NoSuchElementException("The VNFD does not have a node called " + name + " but required by an other node");
117 private String buildVdu(String name, JsonObject vdu, Set<Map.Entry<String, JsonElement>> nodes) {
118 String memorySize = "";
119 String cpuCount = "";
120 StringBuilder body = new StringBuilder();
121 JsonArray vduRequirements = childElement(vdu.getAsJsonObject(), REQUIREMENTS).getAsJsonArray();
122 for (int i = 0; i < vduRequirements.size(); i++) {
123 JsonObject requirement = vduRequirements.get(i).getAsJsonObject();
124 Map.Entry<String, JsonElement> next = requirement.entrySet().iterator().next();
125 String s = next.getKey();
126 if ("virtual_compute".equals(s)) {
127 JsonObject virtualCompute = get(next.getValue().getAsString(), nodes).getAsJsonObject();
128 cpuCount = childElement(child(child(virtualCompute, PROPERTIES), "virtual_cpu"), "num_virtual_cpu").getAsString();
129 memorySize = trimUnit(childElement(child(child(virtualCompute, PROPERTIES), "virtual_memory"), "virtual_mem_size").getAsString());
130 } else if ("virtual_storage".equals(s)) {
131 String item = indent(
132 "- virtual_storage:\n" +
133 " capability: tosca.capabilities.nfv.VirtualStorage\n" +
134 " node: " + next.getValue().getAsString() + "\n", 4);
140 String header = indent(name + ":\n" +
141 " type: tosca.nodes.nfv.VDU.Compute\n" +
143 " virtual_compute:\n" +
146 " virtual_memory:\n" +
147 " virtual_mem_size: " + trimUnit(memorySize) + "\n" +
149 " num_virtual_cpu: " + cpuCount + "\n", 3) +
150 " " + REQUIREMENTS + ":\n", 2);
151 return header + body.toString();
154 private String buildEcp(String name, JsonElement ecp, Set<Map.Entry<String, JsonElement>> nodes) {
155 if (ecp.getAsJsonObject().has(REQUIREMENTS)) {
156 String icpName = getIcpName(ecp.getAsJsonObject().get(REQUIREMENTS).getAsJsonArray());
157 if (icpName != null) {
158 return buildEcpInternal(name, icpName, nodes);
160 logger.warn("The {} ecp does not have an internal connection point", name);
163 logger.warn("The {} ecp does not have an requirements section", name);
168 private String buildEcpInternal(String ecpName, String icpName, Set<Map.Entry<String, JsonElement>> nodes) {
169 JsonObject icpNode = get(icpName, nodes).getAsJsonObject();
170 if (icpNode.has(REQUIREMENTS)) {
171 String vdu = getVduOfIcp(icpNode.getAsJsonObject().get(REQUIREMENTS).getAsJsonArray());
172 //internal connection point is bound to VDU
174 return buildVduCpd(ecpName, vdu, child(icpNode, PROPERTIES));
176 logger.warn("The {} internal connection point of the {} ecp does not have a VDU", icpName, ecpName);
179 logger.warn("The {} internal connection point of the {} ecp does not have a requirements section", icpName, ecpName);
184 private String getVduOfIcp(JsonArray icpRequirements) {
186 for (int i = 0; i < icpRequirements.size(); i++) {
187 JsonElement requirement = icpRequirements.get(i);
188 Map.Entry<String, JsonElement> next = requirement.getAsJsonObject().entrySet().iterator().next();
189 String s = next.getKey();
190 if ("virtual_binding".equals(s)) {
191 vdu = next.getValue().getAsString();
197 private String getIcpName(JsonArray requirements) {
198 String icpName = null;
199 for (int i = 0; i < requirements.size(); i++) {
200 JsonElement requirement = requirements.get(i);
201 Map.Entry<String, JsonElement> next = requirement.getAsJsonObject().entrySet().iterator().next();
202 String s = next.getKey();
203 if ("internal_connection_point".equals(s)) {
204 icpName = next.getValue().getAsString();
210 private String buildIcp(String name, JsonObject icp) {
211 if (icp.has(REQUIREMENTS)) {
212 JsonArray requirements = icp.get(REQUIREMENTS).getAsJsonArray();
215 for (int i = 0; i < requirements.size(); i++) {
216 JsonElement requirement = requirements.get(i);
217 Map.Entry<String, JsonElement> next = requirement.getAsJsonObject().entrySet().iterator().next();
218 String s = next.getKey();
219 if ("virtual_binding".equals(s)) {
220 vdu = next.getValue().getAsString();
221 } else if ("virtual_link".equals(s)) {
222 vl = next.getValue().getAsString();
226 logger.warn("The {} internal connection point does not have a VDU", name);
227 } else if (vl == null) {
228 logger.warn("The {} internal connection point does not have a VL", name);
230 JsonObject properties = child(icp, PROPERTIES);
231 return indent(name + ":\n" +
232 " type: tosca.nodes.nfv.VduCpd\n" +
233 " " + PROPERTIES + ":\n" +
234 " layer_protocol: " + childElement(properties, "layer_protocol").getAsString() + "\n" +
235 " role: leaf\n" + (properties.has(DESCRIPTION) ?
236 " description: " + childElement(properties, DESCRIPTION).getAsString() + "\n" : "") +
238 " - virtual_binding: " + vdu + "\n" +
239 " - virtual_link: " + vl + "\n", 2);
242 logger.warn("The {} internal connection point does not have a requirements section", name);
247 private String buildVduCpd(String name, String vdu, JsonObject properties) {
248 return indent(name + ":\n" +
249 " type: tosca.nodes.nfv.VduCpd\n" +
250 " " + PROPERTIES + ":\n" +
251 " layer_protocol: " + childElement(properties, "layer_protocol").getAsString() + "\n" +
253 (properties.has(DESCRIPTION) ?
254 " description: " + childElement(properties, DESCRIPTION).getAsString() + "\n" : "") +
256 " - virtual_binding: " + vdu + "\n", 2);
259 private String buildVolume(String nodeName, JsonObject volume) {
260 return indent(nodeName + ":\n" +
261 " type: tosca.nodes.nfv.VDU.VirtualStorage\n" +
263 " id: " + nodeName + "\n" +
264 " type_of_storage: volume\n" +
265 " size_of_storage: " + trimUnit(childElement(child(volume, PROPERTIES), "size_of_storage").getAsString()) + "\n", 2);
268 private String buildVl(String name) {
269 return indent(name + ":\n" +
270 " type: tosca.nodes.nfv.VnfVirtualLinkDesc\n" +
274 " flavourId: notUsed\n", 2);