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