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.Gson;
 
  20 import com.google.gson.JsonArray;
 
  21 import com.google.gson.JsonElement;
 
  22 import com.google.gson.JsonObject;
 
  23 import java.util.HashMap;
 
  25 import java.util.NoSuchElementException;
 
  27 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core.SelfRegistrationManager;
 
  28 import org.slf4j.Logger;
 
  29 import org.yaml.snakeyaml.Yaml;
 
  31 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.packagetransformer.OnapVnfdBuilder.getRequirement;
 
  32 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.packagetransformer.OnapVnfdBuilder.indent;
 
  33 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.child;
 
  34 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.childElement;
 
  35 import static org.slf4j.LoggerFactory.getLogger;
 
  38  * Transforms a CBAM package into an ONAP package
 
  40 public class OnapR2VnfdBuilder {
 
  41     public static final String DESCRIPTION = "description";
 
  42     public static final String PROPERTIES = "properties";
 
  43     public static final String REQUIREMENTS = "requirements";
 
  44     private static Logger logger = getLogger(OnapR2VnfdBuilder.class);
 
  46     private static String trimUnit(String data) {
 
  47         //FIXME the unit should not be trimmed VF-C bug
 
  49         //data.trim().replaceAll("[^0-9]", "");
 
  53      * @param cbamVnfd the CBAM VNFD
 
  54      * @return the converted ONAP VNFD
 
  56     public String toOnapVnfd(String cbamVnfd) {
 
  57         JsonObject root = new Gson().toJsonTree(new Yaml().load(cbamVnfd)).getAsJsonObject();
 
  58         JsonObject topologyTemplate = child(root, "topology_template");
 
  59         JsonObject substitution_mappings = child(topologyTemplate, "substitution_mappings");
 
  60         Map<String, JsonElement> virtualLinks = new HashMap<>();
 
  61         if (topologyTemplate.has("node_templates")) {
 
  62             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(), substitution_mappings, 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                     virtualLinks.put(node.getKey(), node.getValue());
 
  73                     body.append(buildVl(node.getValue().getAsJsonObject().get(PROPERTIES).getAsJsonObject(), node.getKey()));
 
  74                 } else if ("tosca.nodes.nfv.ICP".equals(type)) {
 
  75                     body.append(buildIcp(node.getKey(), node.getValue().getAsJsonObject()));
 
  76                 } else if ("tosca.nodes.nfv.ECP".equals(type)) {
 
  77                     body.append(buildEcp(node.getKey(), node.getValue(), nodeTemplates));
 
  79                     logger.warn("The {} type is not converted", type);
 
  82             return buildHeader(topologyTemplate, virtualLinks) + body.toString();
 
  84         return buildHeader(topologyTemplate, virtualLinks);
 
  87     private String buildHeader(JsonObject toplogyTemplate, Map<String, JsonElement> virtualLinks) {
 
  88         JsonObject substitution_mappings = child(toplogyTemplate, "substitution_mappings");
 
  89         String vnfContent = buildVnf(substitution_mappings, virtualLinks);
 
  90         return "tosca_definitions_version: tosca_simple_profile_yaml_1_1\n" +
 
  92                 "topology_template:\n" +
 
  96                 "      description: The ETSI configuration\n" +
 
  97                 "  node_templates:\n" + vnfContent;
 
 100     private JsonElement get(String name, Set<Map.Entry<String, JsonElement>> nodes) {
 
 101         for (Map.Entry<String, JsonElement> node : nodes) {
 
 102             if (name.equals(node.getKey())) {
 
 103                 return node.getValue();
 
 106         throw new NoSuchElementException("The VNFD does not have a node called " + name + " but required by an other node");
 
 109     private String buildVdu(String name, JsonObject vnf, JsonObject vdu, Set<Map.Entry<String, JsonElement>> nodes) {
 
 110         String memorySize = "";
 
 111         String cpuCount = "";
 
 112         StringBuilder body = new StringBuilder();
 
 113         JsonArray vduRequirements = childElement(vdu.getAsJsonObject(), REQUIREMENTS).getAsJsonArray();
 
 114         for (int i = 0; i < vduRequirements.size(); i++) {
 
 115             JsonObject requirement = vduRequirements.get(i).getAsJsonObject();
 
 116             Map.Entry<String, JsonElement> next = requirement.entrySet().iterator().next();
 
 117             String s = next.getKey();
 
 118             if ("virtual_compute".equals(s)) {
 
 119                 JsonObject virtualCompute = get(next.getValue().getAsString(), nodes).getAsJsonObject();
 
 120                 cpuCount = childElement(child(child(virtualCompute, PROPERTIES), "virtual_cpu"), "num_virtual_cpu").getAsString();
 
 121                 memorySize = trimUnit(childElement(child(child(virtualCompute, PROPERTIES), "virtual_memory"), "virtual_mem_size").getAsString());
 
 122             } else if ("virtual_storage".equals(s)) {
 
 123                 String item = indent(
 
 124                         "- virtual_storage:\n" +
 
 125                                 "    capability: tosca.capabilities.nfv.VirtualStorage\n" +
 
 126                                 "    node: " + next.getValue().getAsString() + "\n", 4);
 
 131         JsonObject flavourProperties = child(child(child(vnf, "capabilities"), "deployment_flavour"), "properties");
 
 132         JsonObject vduSizes = child(child(flavourProperties, "vdu_profile"), name);
 
 133         String header = indent(name + ":\n" +
 
 134                 "  type: tosca.nodes.nfv.Vdu.Compute\n" +
 
 136                 "    name: " + name + "\n" +
 
 137                 "    description: " + childElement(child(vdu, PROPERTIES), "description").getAsString() + "\n" +
 
 138                 "    configurable_properties:\n" +
 
 140                 "      min_number_of_instances: " + childElement(vduSizes, "min_number_of_instances").getAsString() + "\n" +
 
 141                 "      max_number_of_instances: " + childElement(vduSizes, "max_number_of_instances").getAsString() + "\n" +
 
 143                 "    virtual_compute:\n" +
 
 146                                 "  virtual_memory:\n" +
 
 147                                 "    virtual_mem_size: " + trimUnit(memorySize) + "\n" +
 
 149                                 "    num_virtual_cpu: " + cpuCount + "\n", 3) +
 
 150                 "  " + REQUIREMENTS + ":\n", 2);
 
 151         return header + body.toString();
 
 154     private String buildEcp(String name, JsonElement ecp, Set<Map.Entry<String, JsonElement>> nodes) {
 
 155         if (ecp.getAsJsonObject().has(REQUIREMENTS)) {
 
 156             String icpName = getRequirement(ecp.getAsJsonObject().get(REQUIREMENTS).getAsJsonArray(), "internal_connection_point");
 
 157             if (icpName != null) {
 
 158                 return buildEcpInternal(name, icpName, nodes);
 
 160                 logger.warn("The {} ecp does not have an internal connection point", name);
 
 163             logger.warn("The {} ecp does not have an requirements section", name);
 
 168     private String buildVnf(JsonObject vnf, Map<String, JsonElement> virtualLinks) {
 
 169         JsonObject vnfProperties = child(vnf, PROPERTIES);
 
 170         JsonObject flavourProperties = child(child(child(vnf, "capabilities"), "deployment_flavour"), "properties");
 
 171         StringBuilder vlContent = new StringBuilder();
 
 172         for (Map.Entry<String, JsonElement> virtualLink : virtualLinks.entrySet()) {
 
 173             vlContent.append(indent("- virtual_link:\n" +
 
 174                     "    capability: tosca.capabilities.nfv.VirtualLinkable\n" +
 
 175                     "    node: " + virtualLink.getKey() + "\n", 4));
 
 177         return indent("VNF:\n" +
 
 178                 "  type: tosca.nodes.nfv.VNF\n" +
 
 179                 "  " + PROPERTIES + ":\n" +
 
 180                 "    descriptor_id: " + childElement(vnfProperties, "descriptor_id").getAsString() + "\n" +
 
 181                 "    descriptor_version: " + childElement(vnfProperties, "descriptor_version").getAsString() + "\n" +
 
 182                 "    provider: " + childElement(vnfProperties, "provider").getAsString() + "\n" +
 
 183                 "    product_name: " + childElement(vnfProperties, "product_name").getAsString() + "\n" +
 
 184                 "    software_version: " + childElement(vnfProperties, "software_version").getAsString() + "\n" +
 
 185                 "    product_info_name: " + childElement(vnfProperties, "product_info_name").getAsString() + "\n" +
 
 186                 (vnfProperties.has("product_info_description") ?
 
 187                         "    product_info_description: " + childElement(vnfProperties, "product_info_description").getAsString() + "\n" : "") +
 
 188                 "    vnfm_info: [ " + SelfRegistrationManager.SERVICE_NAME + " ]\n" +
 
 189                 "    flavour_id: " + childElement(flavourProperties, "flavour_id").getAsString() + "\n" +
 
 190                 "    flavour_description: " + childElement(flavourProperties, "description").getAsString() + "\n", 2) +
 
 191                 "      " + REQUIREMENTS + ":\n" +
 
 192                 vlContent.toString();
 
 195     private String buildEcpInternal(String ecpName, String icpName, Set<Map.Entry<String, JsonElement>> nodes) {
 
 196         JsonObject icpNode = get(icpName, nodes).getAsJsonObject();
 
 197         if (icpNode.has(REQUIREMENTS)) {
 
 198             String vdu = getRequirement(icpNode.getAsJsonObject().get(REQUIREMENTS).getAsJsonArray(), "virtual_binding");
 
 199             //internal connection point is bound to VDU
 
 201                 return buildVduCpd(ecpName, vdu, child(icpNode, PROPERTIES));
 
 203                 logger.warn("The {} internal connection point of the {} ecp does not have a VDU", icpName, ecpName);
 
 206             logger.warn("The {} internal connection point of the {} ecp does not have a requirements section", icpName, ecpName);
 
 211     private String buildIcp(String name, JsonObject icp) {
 
 212         if (icp.has(REQUIREMENTS)) {
 
 213             JsonArray requirements = icp.get(REQUIREMENTS).getAsJsonArray();
 
 214             String vdu = getRequirement(requirements, "virtual_binding");
 
 215             String vl = getRequirement(requirements, "virtual_link");
 
 217                 logger.warn("The {} internal connection point does not have a VDU", name);
 
 218             } else if (vl == null) {
 
 219                 logger.warn("The {} internal connection point does not have a VL", name);
 
 221                 JsonObject properties = child(icp, PROPERTIES);
 
 222                 return indent(name + ":\n" +
 
 223                         "  type: tosca.nodes.nfv.VduCp\n" +
 
 224                         "  " + PROPERTIES + ":\n" +
 
 225                         "    layer_protocol: [ " + childElement(properties, "layer_protocol").getAsString() + " ]\n" +
 
 226                         (properties.has(DESCRIPTION) ?
 
 227                                 "    description: " + childElement(properties, DESCRIPTION).getAsString() + "\n" : "") +
 
 228                         "    protocol_data: []\n" +
 
 229                         "    trunk_mode: false\n" +
 
 231                         "    - virtual_binding: " + vdu + "\n" +
 
 232                         "    - virtual_link: " + vl + "\n", 2);
 
 235             logger.warn("The {} internal connection point does not have a requirements section", name);
 
 240     private String buildVduCpd(String name, String vdu, JsonObject properties) {
 
 241         return indent(name + ":\n" +
 
 242                 "  type: tosca.nodes.nfv.VduCp\n" +
 
 243                 "  " + PROPERTIES + ":\n" +
 
 244                 "    layer_protocol: [ " + childElement(properties, "layer_protocol").getAsString() + " ]\n" +
 
 245                 "    protocol_data: [ ]\n" +
 
 246                 "    trunk_mode: false\n" +
 
 247                 (properties.has(DESCRIPTION) ?
 
 248                         "    description: " + childElement(properties, DESCRIPTION).getAsString() + "\n" : "") +
 
 250                 "    - virtual_binding: " + vdu + "\n", 2);
 
 253     private String buildVolume(String nodeName, JsonObject volume) {
 
 254         return indent(nodeName + ":\n" +
 
 255                 "  type: tosca.nodes.nfv.Vdu.VirtualStorage\n" +
 
 257                 "    type_of_storage: volume\n" +
 
 258                 "    size_of_storage: " + trimUnit(childElement(child(volume, PROPERTIES), "size_of_storage").getAsString()) + "\n", 2);
 
 261     private String buildVl(JsonObject vlProperties, String name) {
 
 262         return indent(name + ":\n" +
 
 263                 "  type: tosca.nodes.nfv.VnfVirtualLink\n" +
 
 265                 "    connectivity_type:\n" +
 
 266                 "      layer_protocol: [ " + childElement(child(vlProperties, "connectivity_type"), "layer_protocol").getAsString() + " ]\n" +
 
 267                 "      flow_pattern: " + childElement(child(vlProperties, "connectivity_type"), "flow_pattern").getAsString() + "\n" +
 
 269                 "      max_bit_rate_requirements:\n" +
 
 270                 "        root: " + Integer.MAX_VALUE + "\n" + //FIXME GAP IN CBAM TEMPLATE
 
 271                 "        leaf: " + Integer.MAX_VALUE + "\n" + //FIXME GAP IN CBAM TEMPLATE
 
 272                 "      min_bit_rate_requirements:\n" +
 
 273                 "        root: 0\n" + //FIXME GAP IN CBAM TEMPLATE
 
 274                 "        leaf: 0\n", 2);  //FIXME GAP IN CBAM TEMPLATE