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;
25 import java.util.NoSuchElementException;
27 import java.util.regex.Pattern;
28 import org.slf4j.Logger;
29 import org.yaml.snakeyaml.Yaml;
31 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.child;
32 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.childElement;
33 import static org.slf4j.LoggerFactory.getLogger;
36 * Transforms a CBAM package into an ONAP package
38 public class OnapVnfdBuilder {
39 public static final String DESCRIPTION = "description";
40 public static final String PROPERTIES = "properties";
41 public static final String REQUIREMENTS = "requirements";
42 private static Logger logger = getLogger(OnapVnfdBuilder.class);
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");
54 private static String trimUnit(String data) {
55 //FIXME the unit should not be trimmed VF-C bug
56 return data.trim().replaceAll("[^0-9]", "");
59 public static String getRequirement(JsonArray requirements, String key) {
60 for (int i = 0; i < requirements.size(); i++) {
61 JsonElement requirement = requirements.get(i);
62 Map.Entry<String, JsonElement> next = requirement.getAsJsonObject().entrySet().iterator().next();
63 String s = next.getKey();
65 return next.getValue().getAsString();
72 * @param cbamVnfd the CBAM VNFD
73 * @return the converted ONAP VNFD
75 public String toOnapVnfd(String cbamVnfd) {
76 JsonObject root = new Gson().toJsonTree(new Yaml().load(cbamVnfd)).getAsJsonObject();
77 JsonObject topologyTemplate = child(root, "topology_template");
78 if (topologyTemplate.has("node_templates")) {
79 Set<Map.Entry<String, JsonElement>> nodeTemplates = child(topologyTemplate, "node_templates").entrySet();
80 StringBuilder body = new StringBuilder();
81 for (Map.Entry<String, JsonElement> node : nodeTemplates) {
82 String type = childElement(node.getValue().getAsJsonObject(), "type").getAsString();
83 if ("tosca.nodes.nfv.VDU".equals(type)) {
84 body.append(buildVdu(node.getKey(), node.getValue().getAsJsonObject(), nodeTemplates));
85 } else if ("tosca.nodes.nfv.VirtualStorage".equals(type)) {
86 body.append(buildVolume(node.getKey(), node.getValue().getAsJsonObject()));
87 } else if ("tosca.nodes.nfv.VL".equals(type)) {
88 body.append(buildVl(node.getKey()));
89 } else if ("tosca.nodes.nfv.ICP".equals(type)) {
90 body.append(buildIcp(node.getKey(), node.getValue().getAsJsonObject()));
91 } else if ("tosca.nodes.nfv.ECP".equals(type)) {
92 body.append(buildEcp(node.getKey(), node.getValue(), nodeTemplates));
94 logger.warn("The {} type is not converted", type);
97 return buildHeader(topologyTemplate) + body.toString();
99 return buildHeader(topologyTemplate);
102 private String buildHeader(JsonObject toplogyTemplate) {
103 JsonObject properties = child(child(toplogyTemplate, "substitution_mappings"), PROPERTIES);
104 String descriptorVersion = properties.get("descriptor_version").getAsString();
105 return "tosca_definitions_version: tosca_simple_yaml_1_0\n" +
109 " csarVersion: " + descriptorVersion + "\n" +
110 " csarProvider: " + properties.get("provider").getAsString() + "\n" +
112 " version: " + properties.get("software_version").getAsString() + "\n" +
113 " csarType: NFAR\n" +
114 " name: " + properties.get("product_name").getAsString() + "\n" +
115 " vnfdVersion: " + descriptorVersion + "\n\n" +
116 "topology_template:\n" +
120 " description: The ETSI configuration\n" +
121 " node_templates:\n";
124 private JsonElement get(String name, Set<Map.Entry<String, JsonElement>> nodes) {
125 for (Map.Entry<String, JsonElement> node : nodes) {
126 if (name.equals(node.getKey())) {
127 return node.getValue();
130 throw new NoSuchElementException("The VNFD does not have a node called " + name + " but required by an other node");
133 private String buildVdu(String name, JsonObject vdu, Set<Map.Entry<String, JsonElement>> nodes) {
134 String memorySize = "";
135 String cpuCount = "";
136 StringBuilder body = new StringBuilder();
137 JsonArray vduRequirements = childElement(vdu.getAsJsonObject(), REQUIREMENTS).getAsJsonArray();
138 for (int i = 0; i < vduRequirements.size(); i++) {
139 JsonObject requirement = vduRequirements.get(i).getAsJsonObject();
140 Map.Entry<String, JsonElement> next = requirement.entrySet().iterator().next();
141 String s = next.getKey();
142 if ("virtual_compute".equals(s)) {
143 JsonObject virtualCompute = get(next.getValue().getAsString(), nodes).getAsJsonObject();
144 cpuCount = childElement(child(child(virtualCompute, PROPERTIES), "virtual_cpu"), "num_virtual_cpu").getAsString();
145 memorySize = trimUnit(childElement(child(child(virtualCompute, PROPERTIES), "virtual_memory"), "virtual_mem_size").getAsString());
146 } else if ("virtual_storage".equals(s)) {
147 String item = indent(
148 "- virtual_storage:\n" +
149 " capability: tosca.capabilities.nfv.VirtualStorage\n" +
150 " node: " + next.getValue().getAsString() + "\n", 4);
156 String header = indent(name + ":\n" +
157 " type: tosca.nodes.nfv.VDU.Compute\n" +
159 " virtual_compute:\n" +
162 " virtual_memory:\n" +
163 " virtual_mem_size: " + trimUnit(memorySize) + "\n" +
165 " num_virtual_cpu: " + cpuCount + "\n", 3) +
166 " " + REQUIREMENTS + ":\n", 2);
167 return header + body.toString();
170 private String buildEcp(String name, JsonElement ecp, Set<Map.Entry<String, JsonElement>> nodes) {
171 if (ecp.getAsJsonObject().has(REQUIREMENTS)) {
172 String icpName = getRequirement(ecp.getAsJsonObject().get(REQUIREMENTS).getAsJsonArray(), "internal_connection_point");
173 if (icpName != null) {
174 return buildEcpInternal(name, icpName, nodes);
176 logger.warn("The {} ecp does not have an internal connection point", name);
179 logger.warn("The {} ecp does not have an requirements section", name);
184 private String buildEcpInternal(String ecpName, String icpName, Set<Map.Entry<String, JsonElement>> nodes) {
185 JsonObject icpNode = get(icpName, nodes).getAsJsonObject();
186 if (icpNode.has(REQUIREMENTS)) {
187 String vdu = getRequirement(icpNode.getAsJsonObject().get(REQUIREMENTS).getAsJsonArray(), "virtual_binding");
188 //internal connection point is bound to VDU
190 return buildVduCpd(ecpName, vdu, child(icpNode, PROPERTIES));
192 logger.warn("The {} internal connection point of the {} ecp does not have a VDU", icpName, ecpName);
195 logger.warn("The {} internal connection point of the {} ecp does not have a requirements section", icpName, ecpName);
200 private String buildIcp(String name, JsonObject icp) {
201 if (icp.has(REQUIREMENTS)) {
202 JsonArray requirements = icp.get(REQUIREMENTS).getAsJsonArray();
203 String vdu = getRequirement(requirements, "virtual_binding");
204 String vl = getRequirement(requirements, "virtual_link");
206 logger.warn("The {} internal connection point does not have a VDU", name);
207 } else if (vl == null) {
208 logger.warn("The {} internal connection point does not have a VL", name);
210 JsonObject properties = child(icp, PROPERTIES);
211 return indent(name + ":\n" +
212 " type: tosca.nodes.nfv.VduCpd\n" +
213 " " + PROPERTIES + ":\n" +
214 " layer_protocol: " + childElement(properties, "layer_protocol").getAsString() + "\n" +
215 " role: leaf\n" + (properties.has(DESCRIPTION) ?
216 " description: " + childElement(properties, DESCRIPTION).getAsString() + "\n" : "") +
218 " - virtual_binding: " + vdu + "\n" +
219 " - virtual_link: " + vl + "\n", 2);
222 logger.warn("The {} internal connection point does not have a requirements section", name);
227 private String buildVduCpd(String name, String vdu, JsonObject 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" +
233 (properties.has(DESCRIPTION) ?
234 " description: " + childElement(properties, DESCRIPTION).getAsString() + "\n" : "") +
236 " - virtual_binding: " + vdu + "\n", 2);
239 private String buildVolume(String nodeName, JsonObject volume) {
240 return indent(nodeName + ":\n" +
241 " type: tosca.nodes.nfv.VDU.VirtualStorage\n" +
243 " id: " + nodeName + "\n" +
244 " type_of_storage: volume\n" +
245 " size_of_storage: " + trimUnit(childElement(child(volume, PROPERTIES), "size_of_storage").getAsString()) + "\n", 2);
248 private String buildVl(String name) {
249 return indent(name + ":\n" +
250 " type: tosca.nodes.nfv.VnfVirtualLinkDesc\n" +
254 " flavourId: notUsed\n", 2);