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;
24 import org.jetbrains.annotations.Nullable;
25 import org.yaml.snakeyaml.Yaml;
28 import java.util.NoSuchElementException;
30 import java.util.regex.Pattern;
32 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.child;
33 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.childElement;
36 * Transforms a CBAM package into an ONAP package
38 public class OnapVnfdBuilder {
40 public static final String DESCRIPTION = "description";
41 public static final String PROPERTIES = "properties";
42 public static final String REQUIREMENTS = "requirements";
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");
55 * @param cbamVnfd the CBAM VNFD
56 * @return the converted ONAP VNFD
58 public String toOnapVnfd(String cbamVnfd) {
59 JsonObject root = new Gson().toJsonTree(new Yaml().load(cbamVnfd)).getAsJsonObject();
60 JsonObject topologyTemplate = child(root, "topology_template");
61 if (topologyTemplate.has("node_templates")) {
62 Set<Map.Entry<String, JsonElement>> nodeTemplates = child(topologyTemplate, "node_templates").entrySet();
63 StringBuilder body = new StringBuilder();
64 for (Map.Entry<String, JsonElement> node : nodeTemplates) {
65 String type = childElement(node.getValue().getAsJsonObject(), "type").getAsString();
67 case "tosca.nodes.nfv.VDU":
68 body.append(buildVdu(node.getKey(), node.getValue().getAsJsonObject(), nodeTemplates));
70 case "tosca.nodes.nfv.VirtualStorage":
71 body.append(buildVolume(node.getKey(), node.getValue().getAsJsonObject()));
73 case "tosca.nodes.nfv.VL":
74 body.append(buildVl(node.getKey()));
76 case "tosca.nodes.nfv.ICP":
77 body.append(buildIcp(node.getKey(), node.getValue().getAsJsonObject()));
79 case "tosca.nodes.nfv.ECP":
80 body.append(buildEcp(node.getKey(), node.getValue(), nodeTemplates));
86 return buildHeader(topologyTemplate) + body.toString();
88 return buildHeader(topologyTemplate);
91 private String buildHeader(JsonObject toplogyTemplate) {
92 JsonObject properties = child(child(toplogyTemplate, "substitution_mappings"), PROPERTIES);
93 String descriptorVersion = properties.get("descriptor_version").getAsString();
94 return "tosca_definitions_version: tosca_simple_yaml_1_0\n" +
98 " csarVersion: " + descriptorVersion + "\n" +
99 " csarProvider: " + properties.get("provider").getAsString() + "\n" +
101 " version: " + properties.get("software_version").getAsString() + "\n" +
102 " csarType: NFAR\n" +
103 " name: " + properties.get("product_name").getAsString() + "\n" +
104 " vnfdVersion: " + descriptorVersion + "\n\n" +
105 "topology_template:\n" +
106 " node_templates:\n";
109 private JsonElement get(String name, Set<Map.Entry<String, JsonElement>> nodes) {
110 for (Map.Entry<String, JsonElement> node : nodes) {
111 if (name.equals(node.getKey())) {
112 return node.getValue();
115 throw new NoSuchElementException("The VNFD does not have a node called " + name + " but required by an other node");
118 private String buildVdu(String name, JsonObject vdu, Set<Map.Entry<String, JsonElement>> nodes) {
119 String memorySize = "";
120 String cpuCount = "";
121 StringBuilder body = new StringBuilder();
122 JsonArray vduRequirements = childElement(vdu.getAsJsonObject(), REQUIREMENTS).getAsJsonArray();
123 for (int i = 0; i < vduRequirements.size(); i++) {
124 JsonObject requirement = vduRequirements.get(i).getAsJsonObject();
125 Map.Entry<String, JsonElement> next = requirement.entrySet().iterator().next();
126 String s = next.getKey();
127 if ("virtual_compute".equals(s)) {
128 JsonObject virtualCompute = get(next.getValue().getAsString(), nodes).getAsJsonObject();
129 cpuCount = childElement(child(child(virtualCompute, PROPERTIES), "virtual_cpu"), "num_virtual_cpu").getAsString();
130 memorySize = childElement(child(child(virtualCompute, PROPERTIES), "virtual_memory"), "virtual_mem_size").getAsString();
132 } else if ("virtual_storage".equals(s)) {
133 String item = indent(
134 "- virtual_storage:\n" +
135 " capability: tosca.capabilities.nfv.VirtualStorage\n" +
136 " node: " + next.getValue().getAsString() + "\n", 4);
142 String header = indent(name + ":\n" +
143 " type: tosca.nodes.nfv.VDU.Compute\n" +
145 " virtual_compute:\n" +
148 " virtual_memory:\n" +
149 " virtual_mem_size: " + memorySize + "\n" +
151 " num_virtual_cpu: " + cpuCount + "\n", 3) +
152 " " + REQUIREMENTS + ":\n", 2);
153 return header + body.toString();
156 private String buildEcp(String name, JsonElement ecp, Set<Map.Entry<String, JsonElement>> nodes) {
157 if (ecp.getAsJsonObject().has(REQUIREMENTS)) {
158 String icpName = getIcpName(ecp.getAsJsonObject().get(REQUIREMENTS).getAsJsonArray());
159 if (icpName != null) {
160 return buildIcp(name, icpName, nodes);
166 private String buildIcp(String name, String icpName, Set<Map.Entry<String, JsonElement>> nodes) {
167 JsonObject icpNode = get(icpName, nodes).getAsJsonObject();
168 if (icpNode.has(REQUIREMENTS)) {
169 String vdu = getVdu(icpNode.getAsJsonObject().get(REQUIREMENTS).getAsJsonArray());
171 return buildVduCpd(name, vdu, child(icpNode, PROPERTIES));
178 private String getVdu(JsonArray requirements) {
180 for (int i = 0; i < requirements.size(); i++) {
181 JsonElement requirement = requirements.get(i);
182 Map.Entry<String, JsonElement> next = requirement.getAsJsonObject().entrySet().iterator().next();
183 String s = next.getKey();
184 if ("virtual_binding".equals(s)) {
185 vdu = next.getValue().getAsString();
192 private String getIcpName(JsonArray requirements) {
193 String icpName = null;
194 for (int i = 0; i < requirements.size(); i++) {
195 JsonElement requirement = requirements.get(i);
196 Map.Entry<String, JsonElement> next = requirement.getAsJsonObject().entrySet().iterator().next();
197 String s = next.getKey();
198 if ("internal_connection_point".equals(s)) {
199 icpName = next.getValue().getAsString();
205 private String buildVduCpd(String name, String vdu, JsonObject properties) {
206 return indent(name + ":\n" +
207 " type: tosca.nodes.nfv.VduCpd\n" +
208 " " + PROPERTIES + ":\n" +
209 " layer_protocol: " + childElement(properties, "layer_protocol").getAsString() + "\n" +
211 (properties.has(DESCRIPTION) ?
212 " description: " + childElement(properties, DESCRIPTION).getAsString() + "\n" : "") +
214 " - virtual_binding: " + vdu + "\n", 2);
217 private String buildIcp(String name, JsonObject icp) {
218 if (icp.has(REQUIREMENTS)) {
219 JsonArray requirements = icp.get(REQUIREMENTS).getAsJsonArray();
222 for (int i = 0; i < requirements.size(); i++) {
223 JsonElement requirement = requirements.get(i);
224 Map.Entry<String, JsonElement> next = requirement.getAsJsonObject().entrySet().iterator().next();
225 String s = next.getKey();
226 if ("virtual_binding".equals(s)) {
227 vdu = next.getValue().getAsString();
229 } else if ("virtual_link".equals(s)) {
230 vl = next.getValue().getAsString();
233 if (vdu != null && vl != null) {
234 JsonObject properties = child(icp, PROPERTIES);
235 return " " + name + ":\n" +
236 " type: tosca.nodes.nfv.VduCpd\n" +
237 " " + PROPERTIES + ":\n" +
238 " layer_protocol: " + childElement(properties, "layer_protocol").getAsString() + "\n" +
239 " role: leaf\n" + (properties.has(DESCRIPTION) ?
240 " description: " + childElement(properties, DESCRIPTION).getAsString() + "\n" : "") +
242 " - virtual_binding: " + vdu + "\n" +
243 " - virtual_link: " + vl + "\n";
249 private String buildVolume(String nodeName, JsonObject volume) {
250 return " " + nodeName + ":\n" +
251 " type: tosca.nodes.nfv.VDU.VirtualStorage\n" +
253 " id: " + nodeName + "\n" +
254 " type_of_storage: volume\n" +
255 " size_of_storage: " + childElement(child(volume, PROPERTIES), "size_of_storage").getAsString() + "\n";
258 private String buildVl(String name) {
259 return " " + name + ":\n" +
260 " type: tosca.nodes.nfv.VnfVirtualLinkDesc\n" +
264 " flavourId: notUsed\n";