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