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