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.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core.SelfRegistrationManager;
25 import org.slf4j.Logger;
27 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.child;
28 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.childElement;
29 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.LifecycleManager.ETSI_CONFIG;
30 import static org.slf4j.LoggerFactory.getLogger;
33 * Transforms a CBAM package into an ONAP package
35 public class OnapR2VnfdBuilder extends OnapAbstractVnfdBuilder {
36 private static Logger logger = getLogger(OnapR2VnfdBuilder.class);
38 protected String buildHeader(JsonObject toplogyTemplate, Map<String, JsonElement> virtualLinks) {
39 JsonObject substitution_mappings = child(toplogyTemplate, "substitution_mappings");
40 String vnfContent = buildVnf(substitution_mappings, virtualLinks);
41 return "tosca_definitions_version: tosca_simple_profile_yaml_1_1\n" +
43 "topology_template:\n" +
45 " " + ETSI_CONFIG + ":\n" +
47 " description: The ETSI configuration\n" +
48 " node_templates:\n" + vnfContent;
51 protected String buildVdu(String name, JsonObject vnf, JsonObject vdu, Set<Map.Entry<String, JsonElement>> nodes) {
52 String memorySize = "";
54 StringBuilder body = new StringBuilder();
55 JsonArray vduRequirements = childElement(vdu.getAsJsonObject(), REQUIREMENTS).getAsJsonArray();
56 for (int i = 0; i < vduRequirements.size(); i++) {
57 JsonObject requirement = vduRequirements.get(i).getAsJsonObject();
58 Map.Entry<String, JsonElement> next = requirement.entrySet().iterator().next();
59 String s = next.getKey();
60 if ("virtual_compute".equals(s)) {
61 JsonObject virtualCompute = get(next.getValue().getAsString(), nodes).getAsJsonObject();
62 cpuCount = childElement(child(child(virtualCompute, PROPERTIES), "virtual_cpu"), "num_virtual_cpu").getAsString();
63 memorySize = childElement(child(child(virtualCompute, PROPERTIES), "virtual_memory"), "virtual_mem_size").getAsString();
64 } else if ("virtual_storage".equals(s)) {
66 "- virtual_storage:\n" +
67 " capability: tosca.capabilities.nfv.VirtualStorage\n" +
68 " node: " + next.getValue().getAsString() + "\n", 4);
73 JsonObject flavourProperties = child(child(child(vnf, "capabilities"), "deployment_flavour"), "properties");
74 JsonObject vduSizes = child(child(flavourProperties, "vdu_profile"), name);
75 String header = indent(name + ":\n" +
76 " type: tosca.nodes.nfv.Vdu.Compute\n" +
78 " name: " + name + "\n" +
79 " description: " + childElement(child(vdu, PROPERTIES), "description").getAsString() + "\n" +
80 " configurable_properties:\n" +
82 " min_number_of_instances: " + childElement(vduSizes, "min_number_of_instances").getAsString() + "\n" +
83 " max_number_of_instances: " + childElement(vduSizes, "max_number_of_instances").getAsString() + "\n" +
85 " virtual_compute:\n" +
88 " virtual_memory:\n" +
89 " virtual_mem_size: " + memorySize + "\n" +
91 " num_virtual_cpu: " + cpuCount + "\n", 3) +
92 " " + REQUIREMENTS + ":\n", 2);
93 return header + body.toString();
96 private String buildVnf(JsonObject vnf, Map<String, JsonElement> virtualLinks) {
97 JsonObject vnfProperties = child(vnf, PROPERTIES);
98 JsonObject flavourProperties = child(child(child(vnf, "capabilities"), "deployment_flavour"), "properties");
99 StringBuilder vlContent = new StringBuilder();
100 for (Map.Entry<String, JsonElement> virtualLink : virtualLinks.entrySet()) {
101 vlContent.append(indent("- virtual_link:\n" +
102 " capability: tosca.capabilities.nfv.VirtualLinkable\n" +
103 " node: " + virtualLink.getKey() + "\n", 4));
105 return indent("VNF:\n" +
106 " type: tosca.nodes.nfv.VNF\n" +
107 " " + PROPERTIES + ":\n" +
108 " descriptor_id: " + childElement(vnfProperties, "descriptor_id").getAsString() + "\n" +
109 " descriptor_version: " + childElement(vnfProperties, "descriptor_version").getAsString() + "\n" +
110 " provider: " + childElement(vnfProperties, "provider").getAsString() + "\n" +
111 " product_name: " + childElement(vnfProperties, "product_name").getAsString() + "\n" +
112 " software_version: " + childElement(vnfProperties, "software_version").getAsString() + "\n" +
113 " product_info_name: " + childElement(vnfProperties, "product_info_name").getAsString() + "\n" +
114 (vnfProperties.has("product_info_description") ?
115 " product_info_description: " + childElement(vnfProperties, "product_info_description").getAsString() + "\n" : "") +
116 " vnfm_info: [ " + SelfRegistrationManager.SERVICE_NAME + " ]\n" +
117 " flavour_id: " + childElement(flavourProperties, "flavour_id").getAsString() + "\n" +
118 " flavour_description: " + childElement(flavourProperties, "description").getAsString() + "\n", 2) +
119 " " + REQUIREMENTS + ":\n" +
120 vlContent.toString();
123 protected String buildIcp(String name, JsonObject icp) {
124 if (icp.has(REQUIREMENTS)) {
125 JsonArray requirements = icp.get(REQUIREMENTS).getAsJsonArray();
126 String vdu = getRequirement(requirements, "virtual_binding");
127 String vl = getRequirement(requirements, "virtual_link");
129 logger.warn("The {} internal connection point does not have a VDU", name);
130 } else if (vl == null) {
131 logger.warn("The {} internal connection point does not have a VL", name);
133 JsonObject properties = child(icp, PROPERTIES);
134 return indent(name + ":\n" +
135 " type: tosca.nodes.nfv.VduCp\n" +
136 " " + PROPERTIES + ":\n" +
137 " layer_protocol: [ " + childElement(properties, "layer_protocol").getAsString() + " ]\n" +
138 (properties.has(DESCRIPTION) ?
139 " description: " + childElement(properties, DESCRIPTION).getAsString() + "\n" : "") +
140 " protocol_data: []\n" +
141 " trunk_mode: false\n" +
143 " - virtual_binding: " + vdu + "\n" +
144 " - virtual_link: " + vl + "\n", 2);
147 logger.warn("The {} internal connection point does not have a requirements section", name);
152 protected String buildVduCpd(String name, String vdu, JsonObject properties) {
153 return indent(name + ":\n" +
154 " type: tosca.nodes.nfv.VduCp\n" +
155 " " + PROPERTIES + ":\n" +
156 " layer_protocol: [ " + childElement(properties, "layer_protocol").getAsString() + " ]\n" +
157 " protocol_data: [ ]\n" +
158 " trunk_mode: false\n" +
159 (properties.has(DESCRIPTION) ?
160 " description: " + childElement(properties, DESCRIPTION).getAsString() + "\n" : "") +
162 " - virtual_binding: " + vdu + "\n", 2);
165 protected String buildVolume(String nodeName, JsonObject volume) {
166 return indent(nodeName + ":\n" +
167 " type: tosca.nodes.nfv.Vdu.VirtualStorage\n" +
169 " type_of_storage: volume\n" +
170 " size_of_storage: " + childElement(child(volume, PROPERTIES), "size_of_storage").getAsString() + "\n", 2);
173 protected String buildVl(JsonObject vlProperties, String name) {
174 JsonObject connectivityType = child(vlProperties, "connectivity_type");
175 return indent(name + ":\n" +
176 " type: tosca.nodes.nfv.VnfVirtualLink\n" +
178 " connectivity_type:\n" +
179 " layer_protocol: [ " + childElement(connectivityType, "layer_protocol").getAsString() + " ]\n" +
180 (connectivityType.has("flow_pattern") ? " flow_pattern: " + childElement(connectivityType, "flow_pattern").getAsString() + "\n" : "") +
182 " max_bit_rate_requirements:\n" +
183 " root: " + Integer.MAX_VALUE + "\n" + //FIXME GAP IN CBAM TEMPLATE
184 " leaf: " + Integer.MAX_VALUE + "\n" + //FIXME GAP IN CBAM TEMPLATE
185 " min_bit_rate_requirements:\n" +
186 " root: 0\n" + //FIXME GAP IN CBAM TEMPLATE
187 " leaf: 0\n", 2); //FIXME GAP IN CBAM TEMPLATE