Merge "Updating Nokia driver"
[vfc/nfvo/driver/vnfm/svnfm.git] / nokiav2 / driver / src / main / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / nokia / packagetransformer / OnapVnfdBuilder.java
1 /*
2  * Copyright 2016-2017, Nokia Corporation
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.packagetransformer;
18
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 org.yaml.snakeyaml.Yaml;
24
25 import java.util.Map;
26 import java.util.NoSuchElementException;
27 import java.util.Set;
28
29 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.child;
30 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.childElement;
31
32 /**
33  * Transforms a CBAM package into an ONAP package
34  */
35 public class OnapVnfdBuilder {
36
37     private String buildHeader(JsonObject toplogyTemplate) {
38         JsonObject properties = child(child(toplogyTemplate, "substitution_mappings"), "properties");
39         String descriptor_version = properties.get("descriptor_version").getAsString();
40         return "tosca_definitions_version: tosca_simple_yaml_1_0\n" +
41                 "\n" +
42                 "metadata:\n" +
43                 "  vendor: Nokia\n" +
44                 "  csarVersion: " + descriptor_version + "\n" +
45                 "  csarProvider: " + properties.get("provider").getAsString() + "\n" +
46                 "  id: Simple\n" +
47                 "  version: " + properties.get("software_version").getAsString() + "\n" +
48                 "  csarType: NFAR\n" +
49                 "  name: " + properties.get("product_name").getAsString() + "\n" +
50                 "  vnfdVersion: " + descriptor_version + "\n\n" +
51                 "topology_template:\n" +
52                 "  node_templates:\n";
53     }
54
55     private JsonElement get(String name, Set<Map.Entry<String, JsonElement>> nodes) {
56         for (Map.Entry<String, JsonElement> node : nodes) {
57             if (name.equals(node.getKey())) {
58                 return node.getValue();
59             }
60         }
61         throw new NoSuchElementException("The VNFD does not have a node called " + name + " but required by an other node");
62     }
63
64     private String buildVdu(String name, JsonObject vdu, Set<Map.Entry<String, JsonElement>> nodes) {
65         String memorySize = "";
66         String cpuCount = "";
67         StringBuilder body = new StringBuilder();
68         JsonArray vduRequirements = childElement(vdu.getAsJsonObject(), "requirements").getAsJsonArray();
69         for (int i = 0; i < vduRequirements.size(); i++) {
70             JsonObject requirement = vduRequirements.get(i).getAsJsonObject();
71             Map.Entry<String, JsonElement> next = requirement.entrySet().iterator().next();
72             switch (next.getKey()) {
73                 case "virtual_compute":
74                     JsonObject virtualCompute = get(next.getValue().getAsString(), nodes).getAsJsonObject();
75                     cpuCount = childElement(child(child(virtualCompute, "properties"), "virtual_cpu"), "num_virtual_cpu").getAsString();
76                     memorySize = childElement(child(child(virtualCompute, "properties"), "virtual_memory"), "virtual_mem_size").getAsString();
77                     break;
78                 case "virtual_storage":
79                     String item =
80                             "        - virtual_storage:\n" +
81                                     "            capability: tosca.capabilities.nfv.VirtualStorage\n" +
82                                     "            node: " + next.getValue().getAsString() + "\n";
83                     body.append(item);
84                     break;
85             }
86             next.getValue();
87         }
88         String header = "    " + name + ":\n" +
89                 "      type: tosca.nodes.nfv.VDU.Compute\n" +
90                 "      capabilities:\n" +
91                 "        virtual_compute:\n" +
92                 "          properties:\n" +
93                 "            virtual_memory:\n" +
94                 "              virtual_mem_size: " + memorySize + "\n" +
95                 "            virtual_cpu:\n" +
96                 "              num_virtual_cpu: " + cpuCount + "\n" +
97                 "      requirements:\n";
98         return header + body.toString();
99     }
100
101     /**
102      * @param cbamVnfd the CBAM VNFD
103      * @return the converted ONAP VNFD
104      */
105     public String toOnapVnfd(String cbamVnfd) {
106         JsonObject root = new Gson().toJsonTree(new Yaml().load(cbamVnfd)).getAsJsonObject();
107         JsonObject topology_template = child(root, "topology_template");
108         if (topology_template.has("node_templates")) {
109             Set<Map.Entry<String, JsonElement>> node_templates = child(topology_template, "node_templates").entrySet();
110             StringBuilder body = new StringBuilder();
111             for (Map.Entry<String, JsonElement> node : node_templates) {
112                 String type = childElement(node.getValue().getAsJsonObject(), "type").getAsString();
113                 switch (type) {
114                     case "tosca.nodes.nfv.VDU":
115                         body.append(buildVdu(node.getKey(), node.getValue().getAsJsonObject(), node_templates));
116                         break;
117                     case "tosca.nodes.nfv.VirtualStorage":
118                         body.append(buildVolume(node.getKey(), node.getValue().getAsJsonObject()));
119                         break;
120                     case "tosca.nodes.nfv.VL":
121                         body.append(buildVl(node.getKey()));
122                         break;
123                     case "tosca.nodes.nfv.ICP":
124                         body.append(buildIcp(node.getKey(), node.getValue().getAsJsonObject()));
125                         break;
126                     case "tosca.nodes.nfv.ECP":
127                         body.append(buildEcp(node.getKey(), node.getValue(), node_templates));
128                         break;
129                 }
130             }
131             return buildHeader(topology_template) + body.toString();
132         }
133         return buildHeader(topology_template);
134     }
135
136     private String buildEcp(String name, JsonElement ecp, Set<Map.Entry<String, JsonElement>> nodes) {
137         if (ecp.getAsJsonObject().has("requirements")) {
138             JsonArray requirements = ecp.getAsJsonObject().get("requirements").getAsJsonArray();
139             String icpName = null;
140             for (int i = 0; i < requirements.size(); i++) {
141                 JsonElement requirement = requirements.get(i);
142                 Map.Entry<String, JsonElement> next = requirement.getAsJsonObject().entrySet().iterator().next();
143                 switch (next.getKey()) {
144                     case "internal_connection_point":
145                         icpName = next.getValue().getAsString();
146
147                 }
148             }
149             if (icpName != null) {
150                 JsonObject icpNode = get(icpName, nodes).getAsJsonObject();
151                 String vdu = null;
152                 if (icpNode.has("requirements")) {
153                     requirements = icpNode.getAsJsonObject().get("requirements").getAsJsonArray();
154                     for (int i = 0; i < requirements.size(); i++) {
155                         JsonElement requirement = requirements.get(i);
156                         Map.Entry<String, JsonElement> next = requirement.getAsJsonObject().entrySet().iterator().next();
157                         switch (next.getKey()) {
158                             case "virtual_binding":
159                                 vdu = next.getValue().getAsString();
160                         }
161                     }
162                     if (vdu != null) {
163                         JsonObject properties = child(icpNode, "properties");
164                         return "    " + name + ":\n" +
165                                 "      type: tosca.nodes.nfv.VduCpd\n" +
166                                 "      properties:\n" +
167                                 "        layer_protocol: " + childElement(properties, "layer_protocol").getAsString() + "\n" +
168                                 "        role: leaf\n" +
169                                 (properties.has("description") ?
170                                         "        description: " + childElement(properties, "description").getAsString() + "\n" : "") +
171                                 "      requirements:\n" +
172                                 "        - virtual_binding: " + vdu + "\n";
173                     }
174                 }
175             }
176         }
177         return "";
178     }
179
180     private String buildIcp(String name, JsonObject icp) {
181         if (icp.has("requirements")) {
182             JsonArray requirements = icp.get("requirements").getAsJsonArray();
183             String vdu = null;
184             String vl = null;
185             for (int i = 0; i < requirements.size(); i++) {
186                 JsonElement requirement = requirements.get(i);
187                 Map.Entry<String, JsonElement> next = requirement.getAsJsonObject().entrySet().iterator().next();
188                 switch (next.getKey()) {
189                     case "virtual_binding":
190                         vdu = next.getValue().getAsString();
191                     case "virtual_link":
192                         vl = next.getValue().getAsString();
193                         break;
194                 }
195             }
196             if (vdu != null && vl != null) {
197                 JsonObject properties = child(icp, "properties");
198                 return "    " + name + ":\n" +
199                         "      type: tosca.nodes.nfv.VduCpd\n" +
200                         "      properties:\n" +
201                         "        layer_protocol: " + childElement(properties, "layer_protocol").getAsString() + "\n" +
202                         "        role: leaf\n" + (properties.has("description") ?
203                         "        description: " + childElement(properties, "description").getAsString() + "\n" : "") +
204                         "      requirements:\n" +
205                         "        - virtual_binding: " + vdu + "\n" +
206                         "        - virtual_link: " + vl + "\n";
207             }
208         }
209         return "";
210     }
211
212     private String buildVolume(String nodeName, JsonObject volume) {
213         return "    " + nodeName + ":\n" +
214                 "      type: tosca.nodes.nfv.VDU.VirtualStorage\n" +
215                 "      properties:\n" +
216                 "        id: " + nodeName + "\n" +
217                 "        type_of_storage: volume\n" +
218                 "        size_of_storage: " + childElement(child(volume, "properties"), "size_of_storage").getAsString() + "\n";
219     }
220
221     private String buildVl(String name) {
222         return "    " + name + ":\n" +
223                 "      type: tosca.nodes.nfv.VnfVirtualLinkDesc\n" +
224                 "      properties:\n" +
225                 "        vl_flavours:\n" +
226                 "          flavours:\n" +
227                 "            flavourId: notUsed\n";
228     }
229 }