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();
67 if ("tosca.nodes.nfv.VDU".equals(type)) {
68 body.append(buildVdu(node.getKey(), node.getValue().getAsJsonObject(), nodeTemplates));
69 } else if ("tosca.nodes.nfv.VirtualStorage".equals(type)) {
70 body.append(buildVolume(node.getKey(), node.getValue().getAsJsonObject()));
71 } else if ("tosca.nodes.nfv.VL".equals(type)) {
72 body.append(buildVl(node.getKey()));
73 } else if ("tosca.nodes.nfv.ICP".equals(type)) {
74 body.append(buildIcp(node.getKey(), node.getValue().getAsJsonObject()));
75 } else if ("tosca.nodes.nfv.ECP".equals(type)) {
76 body.append(buildEcp(node.getKey(), node.getValue(), nodeTemplates));
78 logger.warn("The {} type is not converted", type);
81 return buildHeader(topologyTemplate) + body.toString();
83 return buildHeader(topologyTemplate);
86 private String buildHeader(JsonObject toplogyTemplate) {
87 JsonObject properties = child(child(toplogyTemplate, "substitution_mappings"), PROPERTIES);
88 String descriptorVersion = properties.get("descriptor_version").getAsString();
89 return "tosca_definitions_version: tosca_simple_yaml_1_0\n" +
93 " csarVersion: " + descriptorVersion + "\n" +
94 " csarProvider: " + properties.get("provider").getAsString() + "\n" +
96 " version: " + properties.get("software_version").getAsString() + "\n" +
98 " name: " + properties.get("product_name").getAsString() + "\n" +
99 " vnfdVersion: " + descriptorVersion + "\n\n" +
100 "topology_template:\n" +
101 " node_templates:\n";
104 private JsonElement get(String name, Set<Map.Entry<String, JsonElement>> nodes) {
105 for (Map.Entry<String, JsonElement> node : nodes) {
106 if (name.equals(node.getKey())) {
107 return node.getValue();
110 throw new NoSuchElementException("The VNFD does not have a node called " + name + " but required by an other node");
113 private String buildVdu(String name, JsonObject vdu, Set<Map.Entry<String, JsonElement>> nodes) {
114 String memorySize = "";
115 String cpuCount = "";
116 StringBuilder body = new StringBuilder();
117 JsonArray vduRequirements = childElement(vdu.getAsJsonObject(), REQUIREMENTS).getAsJsonArray();
118 for (int i = 0; i < vduRequirements.size(); i++) {
119 JsonObject requirement = vduRequirements.get(i).getAsJsonObject();
120 Map.Entry<String, JsonElement> next = requirement.entrySet().iterator().next();
121 String s = next.getKey();
122 if ("virtual_compute".equals(s)) {
123 JsonObject virtualCompute = get(next.getValue().getAsString(), nodes).getAsJsonObject();
124 cpuCount = childElement(child(child(virtualCompute, PROPERTIES), "virtual_cpu"), "num_virtual_cpu").getAsString();
125 memorySize = childElement(child(child(virtualCompute, PROPERTIES), "virtual_memory"), "virtual_mem_size").getAsString();
127 } else if ("virtual_storage".equals(s)) {
128 String item = indent(
129 "- virtual_storage:\n" +
130 " capability: tosca.capabilities.nfv.VirtualStorage\n" +
131 " node: " + next.getValue().getAsString() + "\n", 4);
137 String header = indent(name + ":\n" +
138 " type: tosca.nodes.nfv.VDU.Compute\n" +
140 " virtual_compute:\n" +
143 " virtual_memory:\n" +
144 " virtual_mem_size: " + memorySize + "\n" +
146 " num_virtual_cpu: " + cpuCount + "\n", 3) +
147 " " + REQUIREMENTS + ":\n", 2);
148 return header + body.toString();
151 private String buildEcp(String name, JsonElement ecp, Set<Map.Entry<String, JsonElement>> nodes) {
152 if (ecp.getAsJsonObject().has(REQUIREMENTS)) {
153 String icpName = getIcpName(ecp.getAsJsonObject().get(REQUIREMENTS).getAsJsonArray());
154 if (icpName != null) {
155 return buildEcpInternal(name, icpName, nodes);
157 logger.warn("The {} ecp does not have an internal connection point", name);
160 logger.warn("The {} ecp does not have an requirements section", name);
165 private String buildEcpInternal(String ecpName, String icpName, Set<Map.Entry<String, JsonElement>> nodes) {
166 JsonObject icpNode = get(icpName, nodes).getAsJsonObject();
167 if (icpNode.has(REQUIREMENTS)) {
168 String vdu = getVduOfIcp(icpNode.getAsJsonObject().get(REQUIREMENTS).getAsJsonArray());
169 //internal connection point is bound to VDU
171 return buildVduCpd(ecpName, vdu, child(icpNode, PROPERTIES));
173 logger.warn("The {} internal connection point of the {} ecp does not have a VDU", icpName, ecpName);
176 logger.warn("The {} internal connection point of the {} ecp does not have a requirements section", icpName, ecpName);
181 private String getVduOfIcp(JsonArray icpRequirements) {
183 for (int i = 0; i < icpRequirements.size(); i++) {
184 JsonElement requirement = icpRequirements.get(i);
185 Map.Entry<String, JsonElement> next = requirement.getAsJsonObject().entrySet().iterator().next();
186 String s = next.getKey();
187 if ("virtual_binding".equals(s)) {
188 vdu = next.getValue().getAsString();
194 private String getIcpName(JsonArray requirements) {
195 String icpName = null;
196 for (int i = 0; i < requirements.size(); i++) {
197 JsonElement requirement = requirements.get(i);
198 Map.Entry<String, JsonElement> next = requirement.getAsJsonObject().entrySet().iterator().next();
199 String s = next.getKey();
200 if ("internal_connection_point".equals(s)) {
201 icpName = next.getValue().getAsString();
207 private String buildIcp(String name, JsonObject icp) {
208 if (icp.has(REQUIREMENTS)) {
209 JsonArray requirements = icp.get(REQUIREMENTS).getAsJsonArray();
212 for (int i = 0; i < requirements.size(); i++) {
213 JsonElement requirement = requirements.get(i);
214 Map.Entry<String, JsonElement> next = requirement.getAsJsonObject().entrySet().iterator().next();
215 String s = next.getKey();
216 if ("virtual_binding".equals(s)) {
217 vdu = next.getValue().getAsString();
218 } else if ("virtual_link".equals(s)) {
219 vl = next.getValue().getAsString();
223 logger.warn("The {} internal connection point does not have a VDU", name);
224 } else if (vl == null) {
225 logger.warn("The {} internal connection point does not have a VL", name);
227 JsonObject properties = child(icp, PROPERTIES);
228 return indent(name + ":\n" +
229 " type: tosca.nodes.nfv.VduCpd\n" +
230 " " + PROPERTIES + ":\n" +
231 " layer_protocol: " + childElement(properties, "layer_protocol").getAsString() + "\n" +
232 " role: leaf\n" + (properties.has(DESCRIPTION) ?
233 " description: " + childElement(properties, DESCRIPTION).getAsString() + "\n" : "") +
235 " - virtual_binding: " + vdu + "\n" +
236 " - virtual_link: " + vl + "\n", 2);
239 logger.warn("The {} internal connection point does not have a requirements section", name);
244 private String buildVduCpd(String name, String vdu, JsonObject properties) {
245 return indent(name + ":\n" +
246 " type: tosca.nodes.nfv.VduCpd\n" +
247 " " + PROPERTIES + ":\n" +
248 " layer_protocol: " + childElement(properties, "layer_protocol").getAsString() + "\n" +
250 (properties.has(DESCRIPTION) ?
251 " description: " + childElement(properties, DESCRIPTION).getAsString() + "\n" : "") +
253 " - virtual_binding: " + vdu + "\n", 2);
256 private String buildVolume(String nodeName, JsonObject volume) {
257 return indent(nodeName + ":\n" +
258 " type: tosca.nodes.nfv.VDU.VirtualStorage\n" +
260 " id: " + nodeName + "\n" +
261 " type_of_storage: volume\n" +
262 " size_of_storage: " + childElement(child(volume, PROPERTIES), "size_of_storage").getAsString() + "\n", 2);
265 private String buildVl(String name) {
266 return indent(name + ":\n" +
267 " type: tosca.nodes.nfv.VnfVirtualLinkDesc\n" +
271 " flavourId: notUsed\n", 2);