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