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.slf4j.Logger;
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;
34 import static org.slf4j.LoggerFactory.getLogger;
37 * Transforms a CBAM package into an ONAP package
39 public class OnapVnfdBuilder {
40 public static final String DESCRIPTION = "description";
41 public static final String PROPERTIES = "properties";
42 public static final String REQUIREMENTS = "requirements";
43 private static Logger logger = getLogger(OnapVnfdBuilder.class);
46 static String indent(String content, int prefixSize) {
47 StringBuilder sb = new StringBuilder();
48 for (int i = 0; i < prefixSize; i++) {
51 Pattern pattern = Pattern.compile("^(.*)$", Pattern.MULTILINE);
52 return pattern.matcher(content).replaceAll(sb.toString() + "$1");
56 * @param cbamVnfd the CBAM VNFD
57 * @return the converted ONAP VNFD
59 public String toOnapVnfd(String cbamVnfd) {
60 JsonObject root = new Gson().toJsonTree(new Yaml().load(cbamVnfd)).getAsJsonObject();
61 JsonObject topologyTemplate = child(root, "topology_template");
62 if (topologyTemplate.has("node_templates")) {
63 Set<Map.Entry<String, JsonElement>> nodeTemplates = child(topologyTemplate, "node_templates").entrySet();
64 StringBuilder body = new StringBuilder();
65 for (Map.Entry<String, JsonElement> node : nodeTemplates) {
66 String type = childElement(node.getValue().getAsJsonObject(), "type").getAsString();
68 case "tosca.nodes.nfv.VDU":
69 body.append(buildVdu(node.getKey(), node.getValue().getAsJsonObject(), nodeTemplates));
71 case "tosca.nodes.nfv.VirtualStorage":
72 body.append(buildVolume(node.getKey(), node.getValue().getAsJsonObject()));
74 case "tosca.nodes.nfv.VL":
75 body.append(buildVl(node.getKey()));
77 case "tosca.nodes.nfv.ICP":
78 body.append(buildIcp(node.getKey(), node.getValue().getAsJsonObject()));
80 case "tosca.nodes.nfv.ECP":
81 body.append(buildEcp(node.getKey(), node.getValue(), nodeTemplates));
87 return buildHeader(topologyTemplate) + body.toString();
89 return buildHeader(topologyTemplate);
92 private String buildHeader(JsonObject toplogyTemplate) {
93 JsonObject properties = child(child(toplogyTemplate, "substitution_mappings"), PROPERTIES);
94 String descriptorVersion = properties.get("descriptor_version").getAsString();
95 return "tosca_definitions_version: tosca_simple_yaml_1_0\n" +
99 " csarVersion: " + descriptorVersion + "\n" +
100 " csarProvider: " + properties.get("provider").getAsString() + "\n" +
102 " version: " + properties.get("software_version").getAsString() + "\n" +
103 " csarType: NFAR\n" +
104 " name: " + properties.get("product_name").getAsString() + "\n" +
105 " vnfdVersion: " + descriptorVersion + "\n\n" +
106 "topology_template:\n" +
107 " node_templates:\n";
110 private JsonElement get(String name, Set<Map.Entry<String, JsonElement>> nodes) {
111 for (Map.Entry<String, JsonElement> node : nodes) {
112 if (name.equals(node.getKey())) {
113 return node.getValue();
116 throw new NoSuchElementException("The VNFD does not have a node called " + name + " but required by an other node");
119 private String buildVdu(String name, JsonObject vdu, Set<Map.Entry<String, JsonElement>> nodes) {
120 String memorySize = "";
121 String cpuCount = "";
122 StringBuilder body = new StringBuilder();
123 JsonArray vduRequirements = childElement(vdu.getAsJsonObject(), REQUIREMENTS).getAsJsonArray();
124 for (int i = 0; i < vduRequirements.size(); i++) {
125 JsonObject requirement = vduRequirements.get(i).getAsJsonObject();
126 Map.Entry<String, JsonElement> next = requirement.entrySet().iterator().next();
127 String s = next.getKey();
128 if ("virtual_compute".equals(s)) {
129 JsonObject virtualCompute = get(next.getValue().getAsString(), nodes).getAsJsonObject();
130 cpuCount = childElement(child(child(virtualCompute, PROPERTIES), "virtual_cpu"), "num_virtual_cpu").getAsString();
131 memorySize = childElement(child(child(virtualCompute, PROPERTIES), "virtual_memory"), "virtual_mem_size").getAsString();
133 } else if ("virtual_storage".equals(s)) {
134 String item = indent(
135 "- virtual_storage:\n" +
136 " capability: tosca.capabilities.nfv.VirtualStorage\n" +
137 " node: " + next.getValue().getAsString() + "\n", 4);
143 String header = indent(name + ":\n" +
144 " type: tosca.nodes.nfv.VDU.Compute\n" +
146 " virtual_compute:\n" +
149 " virtual_memory:\n" +
150 " virtual_mem_size: " + memorySize + "\n" +
152 " num_virtual_cpu: " + cpuCount + "\n", 3) +
153 " " + REQUIREMENTS + ":\n", 2);
154 return header + body.toString();
157 private String buildEcp(String name, JsonElement ecp, Set<Map.Entry<String, JsonElement>> nodes) {
158 if (ecp.getAsJsonObject().has(REQUIREMENTS)) {
159 String icpName = getIcpName(ecp.getAsJsonObject().get(REQUIREMENTS).getAsJsonArray());
160 if (icpName != null) {
161 return buildEcpInternal(name, icpName, nodes);
163 logger.warn("The {} ecp does not have an internal connection point", name);
166 logger.warn("The {} ecp does not have an requirements section", name);
171 private String buildEcpInternal(String ecpName, String icpName, Set<Map.Entry<String, JsonElement>> nodes) {
172 JsonObject icpNode = get(icpName, nodes).getAsJsonObject();
173 if (icpNode.has(REQUIREMENTS)) {
174 String vdu = getVduOfIcp(icpNode.getAsJsonObject().get(REQUIREMENTS).getAsJsonArray());
175 //internal connection point is bound to VDU
177 return buildVduCpd(ecpName, vdu, child(icpNode, PROPERTIES));
179 logger.warn("The {} internal connection point of the {} ecp does not have a VDU", icpName, ecpName);
182 logger.warn("The {} internal connection point of the {} ecp does not have a requirements section", icpName, ecpName);
187 private String getVduOfIcp(JsonArray icpRequirements) {
189 for (int i = 0; i < icpRequirements.size(); i++) {
190 JsonElement requirement = icpRequirements.get(i);
191 Map.Entry<String, JsonElement> next = requirement.getAsJsonObject().entrySet().iterator().next();
192 String s = next.getKey();
193 if ("virtual_binding".equals(s)) {
194 vdu = next.getValue().getAsString();
200 private String getIcpName(JsonArray requirements) {
201 String icpName = null;
202 for (int i = 0; i < requirements.size(); i++) {
203 JsonElement requirement = requirements.get(i);
204 Map.Entry<String, JsonElement> next = requirement.getAsJsonObject().entrySet().iterator().next();
205 String s = next.getKey();
206 if ("internal_connection_point".equals(s)) {
207 icpName = next.getValue().getAsString();
213 private String buildIcp(String name, JsonObject icp) {
214 if (icp.has(REQUIREMENTS)) {
215 JsonArray requirements = icp.get(REQUIREMENTS).getAsJsonArray();
218 for (int i = 0; i < requirements.size(); i++) {
219 JsonElement requirement = requirements.get(i);
220 Map.Entry<String, JsonElement> next = requirement.getAsJsonObject().entrySet().iterator().next();
221 String s = next.getKey();
222 if ("virtual_binding".equals(s)) {
223 vdu = next.getValue().getAsString();
224 } else if ("virtual_link".equals(s)) {
225 vl = next.getValue().getAsString();
229 logger.warn("The {} internal connection point does not have a VDU", name);
230 } else if (vl == null) {
231 logger.warn("The {} internal connection point does not have a VL", name);
233 JsonObject properties = child(icp, PROPERTIES);
234 return indent(name + ":\n" +
235 " type: tosca.nodes.nfv.VduCpd\n" +
236 " " + PROPERTIES + ":\n" +
237 " layer_protocol: " + childElement(properties, "layer_protocol").getAsString() + "\n" +
238 " role: leaf\n" + (properties.has(DESCRIPTION) ?
239 " description: " + childElement(properties, DESCRIPTION).getAsString() + "\n" : "") +
241 " - virtual_binding: " + vdu + "\n" +
242 " - virtual_link: " + vl + "\n", 2);
245 logger.warn("The {} internal connection point does not have a requirements section", name);
250 private String buildVduCpd(String name, String vdu, JsonObject properties) {
251 return indent(name + ":\n" +
252 " type: tosca.nodes.nfv.VduCpd\n" +
253 " " + PROPERTIES + ":\n" +
254 " layer_protocol: " + childElement(properties, "layer_protocol").getAsString() + "\n" +
256 (properties.has(DESCRIPTION) ?
257 " description: " + childElement(properties, DESCRIPTION).getAsString() + "\n" : "") +
259 " - virtual_binding: " + vdu + "\n", 2);
262 private String buildVolume(String nodeName, JsonObject volume) {
263 return indent(nodeName + ":\n" +
264 " type: tosca.nodes.nfv.VDU.VirtualStorage\n" +
266 " id: " + nodeName + "\n" +
267 " type_of_storage: volume\n" +
268 " size_of_storage: " + childElement(child(volume, PROPERTIES), "size_of_storage").getAsString() + "\n", 2);
271 private String buildVl(String name) {
272 return indent(name + ":\n" +
273 " type: tosca.nodes.nfv.VnfVirtualLinkDesc\n" +
277 " flavourId: notUsed\n", 2);