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.JsonArray;
20 import com.google.gson.JsonElement;
21 import com.google.gson.JsonObject;
24 import org.slf4j.Logger;
26 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.child;
27 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.childElement;
28 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.LifecycleManager.ETSI_CONFIG;
29 import static org.slf4j.LoggerFactory.getLogger;
32 * Transforms a CBAM package into an ONAP package
34 public class OnapR1VnfdBuilder extends OnapAbstractVnfdBuilder {
35 private static Logger logger = getLogger(OnapR1VnfdBuilder.class);
37 private static String trimUnit(String data) {
38 //The R1 templates in Amsterdam release can not handle the scalar-unit types in Tosca
39 //templates, so that the MB, GB, ... units need to be removed even though the created
40 //Tosca template will be invalid
41 return data.trim().replaceAll("[^0-9]", "");
45 protected String buildHeader(JsonObject toplogyTemplate, Map<String, JsonElement> virtualLinks) {
46 JsonObject properties = child(child(toplogyTemplate, "substitution_mappings"), PROPERTIES);
47 String descriptorVersion = properties.get("descriptor_version").getAsString();
48 return "tosca_definitions_version: tosca_simple_yaml_1_0\n" +
52 " csarVersion: " + descriptorVersion + "\n" +
53 " csarProvider: " + properties.get("provider").getAsString() + "\n" +
55 " version: " + properties.get("software_version").getAsString() + "\n" +
57 " name: " + properties.get("product_name").getAsString() + "\n" +
58 " vnfdVersion: " + descriptorVersion + "\n\n" +
59 "topology_template:\n" +
61 " " + ETSI_CONFIG + ":\n" +
63 " description: The ETSI configuration\n" +
68 protected String buildVdu(String name, JsonObject vnf, JsonObject vdu, Set<Map.Entry<String, JsonElement>> nodes) {
69 String memorySize = "";
71 StringBuilder body = new StringBuilder();
72 JsonArray vduRequirements = childElement(vdu.getAsJsonObject(), REQUIREMENTS).getAsJsonArray();
73 for (int i = 0; i < vduRequirements.size(); i++) {
74 JsonObject requirement = vduRequirements.get(i).getAsJsonObject();
75 Map.Entry<String, JsonElement> next = requirement.entrySet().iterator().next();
76 String s = next.getKey();
77 if ("virtual_compute".equals(s)) {
78 JsonObject virtualCompute = get(next.getValue().getAsString(), nodes).getAsJsonObject();
79 cpuCount = childElement(child(child(virtualCompute, PROPERTIES), "virtual_cpu"), "num_virtual_cpu").getAsString();
80 memorySize = trimUnit(childElement(child(child(virtualCompute, PROPERTIES), "virtual_memory"), "virtual_mem_size").getAsString());
81 } else if ("virtual_storage".equals(s)) {
83 "- virtual_storage:\n" +
84 " capability: tosca.capabilities.nfv.VirtualStorage\n" +
85 " node: " + next.getValue().getAsString() + "\n", 4);
91 String header = indent(name + ":\n" +
92 " type: tosca.nodes.nfv.VDU.Compute\n" +
94 " virtual_compute:\n" +
97 " virtual_memory:\n" +
98 " virtual_mem_size: " + trimUnit(memorySize) + "\n" +
100 " num_virtual_cpu: " + cpuCount + "\n", 3) +
101 " " + REQUIREMENTS + ":\n", 2);
102 return header + body.toString();
105 protected String buildIcp(String name, JsonObject icp) {
106 if (icp.has(REQUIREMENTS)) {
107 JsonArray requirements = icp.get(REQUIREMENTS).getAsJsonArray();
108 String vdu = getRequirement(requirements, "virtual_binding");
109 String vl = getRequirement(requirements, "virtual_link");
111 logger.warn("The {} internal connection point does not have a VDU", name);
112 } else if (vl == null) {
113 logger.warn("The {} internal connection point does not have a VL", name);
115 JsonObject properties = child(icp, PROPERTIES);
116 return indent(name + ":\n" +
117 " type: tosca.nodes.nfv.VduCpd\n" +
118 " " + PROPERTIES + ":\n" +
119 " layer_protocol: " + childElement(properties, "layer_protocol").getAsString() + "\n" +
120 " role: leaf\n" + (properties.has(DESCRIPTION) ?
121 " description: " + childElement(properties, DESCRIPTION).getAsString() + "\n" : "") +
123 " - virtual_binding: " + vdu + "\n" +
124 " - virtual_link: " + vl + "\n", 2);
127 logger.warn("The {} internal connection point does not have a requirements section", name);
132 protected String buildVduCpd(String name, String vdu, JsonObject properties) {
133 return indent(name + ":\n" +
134 " type: tosca.nodes.nfv.VduCpd\n" +
135 " " + PROPERTIES + ":\n" +
136 " layer_protocol: " + childElement(properties, "layer_protocol").getAsString() + "\n" +
138 (properties.has(DESCRIPTION) ?
139 " description: " + childElement(properties, DESCRIPTION).getAsString() + "\n" : "") +
141 " - virtual_binding: " + vdu + "\n", 2);
144 protected String buildVolume(String nodeName, JsonObject volume) {
145 return indent(nodeName + ":\n" +
146 " type: tosca.nodes.nfv.VDU.VirtualStorage\n" +
148 " id: " + nodeName + "\n" +
149 " type_of_storage: volume\n" +
150 " size_of_storage: " + trimUnit(childElement(child(volume, PROPERTIES), "size_of_storage").getAsString()) + "\n", 2);
154 protected String buildVl(JsonObject vlProperties, String name) {
155 return indent(name + ":\n" +
156 " type: tosca.nodes.nfv.VnfVirtualLinkDesc\n" +
160 " flavourId: notUsed\n", 2);