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