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.yaml.snakeyaml.Yaml;
26 import java.util.NoSuchElementException;
29 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.child;
30 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.childElement;
33 * Transforms a CBAM package into an ONAP package
35 public class OnapVnfdBuilder {
37 private String buildHeader(JsonObject toplogyTemplate) {
38 JsonObject properties = child(child(toplogyTemplate, "substitution_mappings"), "properties");
39 String descriptor_version = properties.get("descriptor_version").getAsString();
40 return "tosca_definitions_version: tosca_simple_yaml_1_0\n" +
44 " csarVersion: " + descriptor_version + "\n" +
45 " csarProvider: " + properties.get("provider").getAsString() + "\n" +
47 " version: " + properties.get("software_version").getAsString() + "\n" +
49 " name: " + properties.get("product_name").getAsString() + "\n" +
50 " vnfdVersion: " + descriptor_version + "\n\n" +
51 "topology_template:\n" +
55 private JsonElement get(String name, Set<Map.Entry<String, JsonElement>> nodes) {
56 for (Map.Entry<String, JsonElement> node : nodes) {
57 if (name.equals(node.getKey())) {
58 return node.getValue();
61 throw new NoSuchElementException("The VNFD does not have a node called " + name + " but required by an other node");
64 private String buildVdu(String name, JsonObject vdu, Set<Map.Entry<String, JsonElement>> nodes) {
65 String memorySize = "";
67 StringBuilder body = new StringBuilder();
68 JsonArray vduRequirements = childElement(vdu.getAsJsonObject(), "requirements").getAsJsonArray();
69 for (int i = 0; i < vduRequirements.size(); i++) {
70 JsonObject requirement = vduRequirements.get(i).getAsJsonObject();
71 Map.Entry<String, JsonElement> next = requirement.entrySet().iterator().next();
72 switch (next.getKey()) {
73 case "virtual_compute":
74 JsonObject virtualCompute = get(next.getValue().getAsString(), nodes).getAsJsonObject();
75 cpuCount = childElement(child(child(virtualCompute, "properties"), "virtual_cpu"), "num_virtual_cpu").getAsString();
76 memorySize = childElement(child(child(virtualCompute, "properties"), "virtual_memory"), "virtual_mem_size").getAsString();
78 case "virtual_storage":
80 " - virtual_storage:\n" +
81 " capability: tosca.capabilities.nfv.VirtualStorage\n" +
82 " node: " + next.getValue().getAsString() + "\n";
88 String header = " " + name + ":\n" +
89 " type: tosca.nodes.nfv.VDU.Compute\n" +
91 " virtual_compute:\n" +
93 " virtual_memory:\n" +
94 " virtual_mem_size: " + memorySize + "\n" +
96 " num_virtual_cpu: " + cpuCount + "\n" +
98 return header + body.toString();
102 * @param cbamVnfd the CBAM VNFD
103 * @return the converted ONAP VNFD
105 public String toOnapVnfd(String cbamVnfd) {
106 JsonObject root = new Gson().toJsonTree(new Yaml().load(cbamVnfd)).getAsJsonObject();
107 JsonObject topology_template = child(root, "topology_template");
108 if (topology_template.has("node_templates")) {
109 Set<Map.Entry<String, JsonElement>> node_templates = child(topology_template, "node_templates").entrySet();
110 StringBuilder body = new StringBuilder();
111 for (Map.Entry<String, JsonElement> node : node_templates) {
112 String type = childElement(node.getValue().getAsJsonObject(), "type").getAsString();
114 case "tosca.nodes.nfv.VDU":
115 body.append(buildVdu(node.getKey(), node.getValue().getAsJsonObject(), node_templates));
117 case "tosca.nodes.nfv.VirtualStorage":
118 body.append(buildVolume(node.getKey(), node.getValue().getAsJsonObject()));
120 case "tosca.nodes.nfv.VL":
121 body.append(buildVl(node.getKey()));
123 case "tosca.nodes.nfv.ICP":
124 body.append(buildIcp(node.getKey(), node.getValue().getAsJsonObject()));
126 case "tosca.nodes.nfv.ECP":
127 body.append(buildEcp(node.getKey(), node.getValue(), node_templates));
131 return buildHeader(topology_template) + body.toString();
133 return buildHeader(topology_template);
136 private String buildEcp(String name, JsonElement ecp, Set<Map.Entry<String, JsonElement>> nodes) {
137 if (ecp.getAsJsonObject().has("requirements")) {
138 JsonArray requirements = ecp.getAsJsonObject().get("requirements").getAsJsonArray();
139 String icpName = null;
140 for (int i = 0; i < requirements.size(); i++) {
141 JsonElement requirement = requirements.get(i);
142 Map.Entry<String, JsonElement> next = requirement.getAsJsonObject().entrySet().iterator().next();
143 switch (next.getKey()) {
144 case "internal_connection_point":
145 icpName = next.getValue().getAsString();
149 if (icpName != null) {
150 JsonObject icpNode = get(icpName, nodes).getAsJsonObject();
152 if (icpNode.has("requirements")) {
153 requirements = icpNode.getAsJsonObject().get("requirements").getAsJsonArray();
154 for (int i = 0; i < requirements.size(); i++) {
155 JsonElement requirement = requirements.get(i);
156 Map.Entry<String, JsonElement> next = requirement.getAsJsonObject().entrySet().iterator().next();
157 switch (next.getKey()) {
158 case "virtual_binding":
159 vdu = next.getValue().getAsString();
163 JsonObject properties = child(icpNode, "properties");
164 return " " + name + ":\n" +
165 " type: tosca.nodes.nfv.VduCpd\n" +
167 " layer_protocol: " + childElement(properties, "layer_protocol").getAsString() + "\n" +
169 (properties.has("description") ?
170 " description: " + childElement(properties, "description").getAsString() + "\n" : "") +
172 " - virtual_binding: " + vdu + "\n";
180 private String buildIcp(String name, JsonObject icp) {
181 if (icp.has("requirements")) {
182 JsonArray requirements = icp.get("requirements").getAsJsonArray();
185 for (int i = 0; i < requirements.size(); i++) {
186 JsonElement requirement = requirements.get(i);
187 Map.Entry<String, JsonElement> next = requirement.getAsJsonObject().entrySet().iterator().next();
188 switch (next.getKey()) {
189 case "virtual_binding":
190 vdu = next.getValue().getAsString();
192 vl = next.getValue().getAsString();
196 if (vdu != null && vl != null) {
197 JsonObject properties = child(icp, "properties");
198 return " " + name + ":\n" +
199 " type: tosca.nodes.nfv.VduCpd\n" +
201 " layer_protocol: " + childElement(properties, "layer_protocol").getAsString() + "\n" +
202 " role: leaf\n" + (properties.has("description") ?
203 " description: " + childElement(properties, "description").getAsString() + "\n" : "") +
205 " - virtual_binding: " + vdu + "\n" +
206 " - virtual_link: " + vl + "\n";
212 private String buildVolume(String nodeName, JsonObject volume) {
213 return " " + nodeName + ":\n" +
214 " type: tosca.nodes.nfv.VDU.VirtualStorage\n" +
216 " id: " + nodeName + "\n" +
217 " type_of_storage: volume\n" +
218 " size_of_storage: " + childElement(child(volume, "properties"), "size_of_storage").getAsString() + "\n";
221 private String buildVl(String name) {
222 return " " + name + ":\n" +
223 " type: tosca.nodes.nfv.VnfVirtualLinkDesc\n" +
227 " flavourId: notUsed\n";