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