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 / OnapR1VnfdBuilder.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.gson.JsonArray;
20 import com.google.gson.JsonElement;
21 import com.google.gson.JsonObject;
22 import java.util.Map;
23 import java.util.Set;
24 import org.slf4j.Logger;
25
26 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.child;
27 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.childElement;
28 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.LifecycleManager.ETSI_CONFIG;
29 import static org.slf4j.LoggerFactory.getLogger;
30
31 /**
32  * Transforms a CBAM package into an ONAP package
33  */
34 public class OnapR1VnfdBuilder extends OnapAbstractVnfdBuilder {
35     private static Logger logger = getLogger(OnapR1VnfdBuilder.class);
36
37     private static String trimUnit(String data) {
38         //The R1 templates in Amsterdam release can not handle the scalar-unit types in Tosca
39         //templates, so that the MB, GB, ... units need to be removed even though the created
40         //Tosca template will be invalid
41         return data.trim().replaceAll("[^0-9]", "");
42     }
43
44     @Override
45     protected String buildHeader(JsonObject toplogyTemplate, Map<String, JsonElement> virtualLinks) {
46         JsonObject properties = child(child(toplogyTemplate, "substitution_mappings"), PROPERTIES);
47         String descriptorVersion = properties.get("descriptor_version").getAsString();
48         return "tosca_definitions_version: tosca_simple_yaml_1_0\n" +
49                 "\n" +
50                 "metadata:\n" +
51                 "  vendor: Nokia\n" +
52                 "  csarVersion: " + descriptorVersion + "\n" +
53                 "  csarProvider: " + properties.get("provider").getAsString() + "\n" +
54                 "  id: Simple\n" +
55                 "  version: " + properties.get("software_version").getAsString() + "\n" +
56                 "  csarType: NFAR\n" +
57                 "  name: " + properties.get("product_name").getAsString() + "\n" +
58                 "  vnfdVersion: " + descriptorVersion + "\n\n" +
59                 "topology_template:\n" +
60                 "  inputs:\n" +
61                 "    " + ETSI_CONFIG + ":\n" +
62                 "      type: string\n" +
63                 "      description: The ETSI configuration\n" +
64                 "  node_templates:\n";
65     }
66
67     @Override
68     protected String buildVdu(String name, JsonObject vnf, JsonObject vdu, Set<Map.Entry<String, JsonElement>> nodes) {
69         String memorySize = "";
70         String cpuCount = "";
71         StringBuilder body = new StringBuilder();
72         JsonArray vduRequirements = childElement(vdu.getAsJsonObject(), REQUIREMENTS).getAsJsonArray();
73         for (int i = 0; i < vduRequirements.size(); i++) {
74             JsonObject requirement = vduRequirements.get(i).getAsJsonObject();
75             Map.Entry<String, JsonElement> next = requirement.entrySet().iterator().next();
76             String s = next.getKey();
77             if ("virtual_compute".equals(s)) {
78                 JsonObject virtualCompute = get(next.getValue().getAsString(), nodes).getAsJsonObject();
79                 cpuCount = childElement(child(child(virtualCompute, PROPERTIES), "virtual_cpu"), "num_virtual_cpu").getAsString();
80                 memorySize = trimUnit(childElement(child(child(virtualCompute, PROPERTIES), "virtual_memory"), "virtual_mem_size").getAsString());
81             } else if ("virtual_storage".equals(s)) {
82                 String item = indent(
83                         "- virtual_storage:\n" +
84                                 "    capability: tosca.capabilities.nfv.VirtualStorage\n" +
85                                 "    node: " + next.getValue().getAsString() + "\n", 4);
86                 body.append(item);
87
88             }
89             next.getValue();
90         }
91         String header = indent(name + ":\n" +
92                 "  type: tosca.nodes.nfv.VDU.Compute\n" +
93                 "  capabilities:\n" +
94                 "    virtual_compute:\n" +
95                 indent(
96                         "properties:\n" +
97                                 "  virtual_memory:\n" +
98                                 "    virtual_mem_size: " + trimUnit(memorySize) + "\n" +
99                                 "  virtual_cpu:\n" +
100                                 "    num_virtual_cpu: " + cpuCount + "\n", 3) +
101                 "  " + REQUIREMENTS + ":\n", 2);
102         return header + body.toString();
103     }
104
105     protected String buildIcp(String name, JsonObject icp) {
106         if (icp.has(REQUIREMENTS)) {
107             JsonArray requirements = icp.get(REQUIREMENTS).getAsJsonArray();
108             String vdu = getRequirement(requirements, "virtual_binding");
109             String vl = getRequirement(requirements, "virtual_link");
110             if (vdu == null) {
111                 logger.warn("The {} internal connection point does not have a VDU", name);
112             } else if (vl == null) {
113                 logger.warn("The {} internal connection point does not have a VL", name);
114             } else {
115                 JsonObject properties = child(icp, PROPERTIES);
116                 return indent(name + ":\n" +
117                         "  type: tosca.nodes.nfv.VduCpd\n" +
118                         "  " + PROPERTIES + ":\n" +
119                         "    layer_protocol: " + childElement(properties, "layer_protocol").getAsString() + "\n" +
120                         "    role: leaf\n" + (properties.has(DESCRIPTION) ?
121                         "    description: " + childElement(properties, DESCRIPTION).getAsString() + "\n" : "") +
122                         "  requirements:\n" +
123                         "    - virtual_binding: " + vdu + "\n" +
124                         "    - virtual_link: " + vl + "\n", 2);
125             }
126         } else {
127             logger.warn("The {} internal connection point does not have a requirements section", name);
128         }
129         return "";
130     }
131
132     protected String buildVduCpd(String name, String vdu, JsonObject properties) {
133         return indent(name + ":\n" +
134                 "  type: tosca.nodes.nfv.VduCpd\n" +
135                 "  " + PROPERTIES + ":\n" +
136                 "    layer_protocol: " + childElement(properties, "layer_protocol").getAsString() + "\n" +
137                 "    role: leaf\n" +
138                 (properties.has(DESCRIPTION) ?
139                         "    description: " + childElement(properties, DESCRIPTION).getAsString() + "\n" : "") +
140                 "  requirements:\n" +
141                 "    - virtual_binding: " + vdu + "\n", 2);
142     }
143
144     protected String buildVolume(String nodeName, JsonObject volume) {
145         return indent(nodeName + ":\n" +
146                 "  type: tosca.nodes.nfv.VDU.VirtualStorage\n" +
147                 "  properties:\n" +
148                 "    id: " + nodeName + "\n" +
149                 "    type_of_storage: volume\n" +
150                 "    size_of_storage: " + trimUnit(childElement(child(volume, PROPERTIES), "size_of_storage").getAsString()) + "\n", 2);
151     }
152
153     @Override
154     protected String buildVl(JsonObject vlProperties, String name) {
155         return indent(name + ":\n" +
156                 "  type: tosca.nodes.nfv.VnfVirtualLinkDesc\n" +
157                 "  properties:\n" +
158                 "    vl_flavours:\n" +
159                 "      flavours:\n" +
160                 "        flavourId: notUsed\n", 2);
161     }
162 }