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.gson.Gson;
20 import com.google.gson.JsonArray;
21 import com.google.gson.JsonElement;
22 import com.google.gson.JsonObject;
23 import org.jetbrains.annotations.Nullable;
24 import org.yaml.snakeyaml.Yaml;
27 import java.util.NoSuchElementException;
30 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.child;
31 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.childElement;
34 * Transforms a CBAM package into an ONAP package
36 public class OnapVnfdBuilder {
38 public static final String DESCRIPTION = "description";
39 public static final String PROPERTIES = "properties";
40 public static final String REQUIREMENTS = "requirements";
43 * @param cbamVnfd the CBAM VNFD
44 * @return the converted ONAP VNFD
46 public String toOnapVnfd(String cbamVnfd) {
47 JsonObject root = new Gson().toJsonTree(new Yaml().load(cbamVnfd)).getAsJsonObject();
48 JsonObject topologyTemplate = child(root, "topology_template");
49 if (topologyTemplate.has("node_templates")) {
50 Set<Map.Entry<String, JsonElement>> nodeTemplates = child(topologyTemplate, "node_templates").entrySet();
51 StringBuilder body = new StringBuilder();
52 for (Map.Entry<String, JsonElement> node : nodeTemplates) {
53 String type = childElement(node.getValue().getAsJsonObject(), "type").getAsString();
55 case "tosca.nodes.nfv.VDU":
56 body.append(buildVdu(node.getKey(), node.getValue().getAsJsonObject(), nodeTemplates));
58 case "tosca.nodes.nfv.VirtualStorage":
59 body.append(buildVolume(node.getKey(), node.getValue().getAsJsonObject()));
61 case "tosca.nodes.nfv.VL":
62 body.append(buildVl(node.getKey()));
64 case "tosca.nodes.nfv.ICP":
65 body.append(buildIcp(node.getKey(), node.getValue().getAsJsonObject()));
67 case "tosca.nodes.nfv.ECP":
68 body.append(buildEcp(node.getKey(), node.getValue(), nodeTemplates));
74 return buildHeader(topologyTemplate) + body.toString();
76 return buildHeader(topologyTemplate);
79 private String buildHeader(JsonObject toplogyTemplate) {
80 JsonObject properties = child(child(toplogyTemplate, "substitution_mappings"), PROPERTIES);
81 String descriptorVersion = properties.get("descriptor_version").getAsString();
82 return "tosca_definitions_version: tosca_simple_yaml_1_0\n" +
86 " csarVersion: " + descriptorVersion + "\n" +
87 " csarProvider: " + properties.get("provider").getAsString() + "\n" +
89 " version: " + properties.get("software_version").getAsString() + "\n" +
91 " name: " + properties.get("product_name").getAsString() + "\n" +
92 " vnfdVersion: " + descriptorVersion + "\n\n" +
93 "topology_template:\n" +
97 private JsonElement get(String name, Set<Map.Entry<String, JsonElement>> nodes) {
98 for (Map.Entry<String, JsonElement> node : nodes) {
99 if (name.equals(node.getKey())) {
100 return node.getValue();
103 throw new NoSuchElementException("The VNFD does not have a node called " + name + " but required by an other node");
106 private String buildVdu(String name, JsonObject vdu, Set<Map.Entry<String, JsonElement>> nodes) {
107 String memorySize = "";
108 String cpuCount = "";
109 StringBuilder body = new StringBuilder();
110 JsonArray vduRequirements = childElement(vdu.getAsJsonObject(), REQUIREMENTS).getAsJsonArray();
111 for (int i = 0; i < vduRequirements.size(); i++) {
112 JsonObject requirement = vduRequirements.get(i).getAsJsonObject();
113 Map.Entry<String, JsonElement> next = requirement.entrySet().iterator().next();
114 String s = next.getKey();
115 if ("virtual_compute".equals(s)) {
116 JsonObject virtualCompute = get(next.getValue().getAsString(), nodes).getAsJsonObject();
117 cpuCount = childElement(child(child(virtualCompute, PROPERTIES), "virtual_cpu"), "num_virtual_cpu").getAsString();
118 memorySize = childElement(child(child(virtualCompute, PROPERTIES), "virtual_memory"), "virtual_mem_size").getAsString();
120 } else if ("virtual_storage".equals(s)) {
122 " - virtual_storage:\n" +
123 " capability: tosca.capabilities.nfv.VirtualStorage\n" +
124 " node: " + next.getValue().getAsString() + "\n";
130 String header = " " + name + ":\n" +
131 " type: tosca.nodes.nfv.VDU.Compute\n" +
133 " virtual_compute:\n" +
135 " virtual_memory:\n" +
136 " virtual_mem_size: " + memorySize + "\n" +
138 " num_virtual_cpu: " + cpuCount + "\n" +
139 " " + REQUIREMENTS + ":\n";
140 return header + body.toString();
143 private String buildEcp(String name, JsonElement ecp, Set<Map.Entry<String, JsonElement>> nodes) {
144 if (ecp.getAsJsonObject().has(REQUIREMENTS)) {
145 JsonArray requirements = ecp.getAsJsonObject().get(REQUIREMENTS).getAsJsonArray();
146 String icpName = getIcpName(requirements);
147 if (icpName != null) {
148 JsonObject icpNode = get(icpName, nodes).getAsJsonObject();
149 if (icpNode.has(REQUIREMENTS)) {
150 requirements = icpNode.getAsJsonObject().get(REQUIREMENTS).getAsJsonArray();
151 String vdu = getVdu(requirements);
153 JsonObject properties = child(icpNode, PROPERTIES);
154 return buildVduCpd(name, vdu, properties);
163 private String getVdu(JsonArray requirements) {
165 for (int i = 0; i < requirements.size(); i++) {
166 JsonElement requirement = requirements.get(i);
167 Map.Entry<String, JsonElement> next = requirement.getAsJsonObject().entrySet().iterator().next();
168 String s = next.getKey();
169 if ("virtual_binding".equals(s)) {
170 vdu = next.getValue().getAsString();
177 private String getIcpName(JsonArray requirements) {
178 String icpName = null;
179 for (int i = 0; i < requirements.size(); i++) {
180 JsonElement requirement = requirements.get(i);
181 Map.Entry<String, JsonElement> next = requirement.getAsJsonObject().entrySet().iterator().next();
182 String s = next.getKey();
183 if ("internal_connection_point".equals(s)) {
184 icpName = next.getValue().getAsString();
190 private String buildVduCpd(String name, String vdu, JsonObject properties) {
191 return " " + name + ":\n" +
192 " type: tosca.nodes.nfv.VduCpd\n" +
193 " " + PROPERTIES + ":\n" +
194 " layer_protocol: " + childElement(properties, "layer_protocol").getAsString() + "\n" +
196 (properties.has(DESCRIPTION) ?
197 " description: " + childElement(properties, DESCRIPTION).getAsString() + "\n" : "") +
199 " - virtual_binding: " + vdu + "\n";
202 private String buildIcp(String name, JsonObject icp) {
203 if (icp.has(REQUIREMENTS)) {
204 JsonArray requirements = icp.get(REQUIREMENTS).getAsJsonArray();
207 for (int i = 0; i < requirements.size(); i++) {
208 JsonElement requirement = requirements.get(i);
209 Map.Entry<String, JsonElement> next = requirement.getAsJsonObject().entrySet().iterator().next();
210 String s = next.getKey();
211 if ("virtual_binding".equals(s)) {
212 vdu = next.getValue().getAsString();
214 } else if ("virtual_link".equals(s)) {
215 vl = next.getValue().getAsString();
218 if (vdu != null && vl != null) {
219 JsonObject properties = child(icp, PROPERTIES);
220 return " " + name + ":\n" +
221 " type: tosca.nodes.nfv.VduCpd\n" +
222 " " + PROPERTIES + ":\n" +
223 " layer_protocol: " + childElement(properties, "layer_protocol").getAsString() + "\n" +
224 " role: leaf\n" + (properties.has(DESCRIPTION) ?
225 " description: " + childElement(properties, DESCRIPTION).getAsString() + "\n" : "") +
227 " - virtual_binding: " + vdu + "\n" +
228 " - virtual_link: " + vl + "\n";
234 private String buildVolume(String nodeName, JsonObject volume) {
235 return " " + nodeName + ":\n" +
236 " type: tosca.nodes.nfv.VDU.VirtualStorage\n" +
238 " id: " + nodeName + "\n" +
239 " type_of_storage: volume\n" +
240 " size_of_storage: " + childElement(child(volume, PROPERTIES), "size_of_storage").getAsString() + "\n";
243 private String buildVl(String name) {
244 return " " + name + ":\n" +
245 " type: tosca.nodes.nfv.VnfVirtualLinkDesc\n" +
249 " flavourId: notUsed\n";