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