Fix template generation for R2
[vfc/nfvo/driver/vnfm/svnfm.git] / nokiav2 / driver / src / main / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / nokia / packagetransformer / OnapR2VnfdBuilder.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.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core.SelfRegistrationManager;
25 import org.slf4j.Logger;
26
27 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.child;
28 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.childElement;
29 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.LifecycleManager.ETSI_CONFIG;
30 import static org.slf4j.LoggerFactory.getLogger;
31
32 /**
33  * Transforms a CBAM package into an ONAP package
34  */
35 public class OnapR2VnfdBuilder extends OnapAbstractVnfdBuilder {
36     private static Logger logger = getLogger(OnapR2VnfdBuilder.class);
37
38     protected String buildHeader(JsonObject toplogyTemplate, Map<String, JsonElement> virtualLinks) {
39         JsonObject substitution_mappings = child(toplogyTemplate, "substitution_mappings");
40         String vnfContent = buildVnf(substitution_mappings, virtualLinks);
41         return "tosca_definitions_version: tosca_simple_profile_yaml_1_1\n" +
42                 "\n" +
43                 "topology_template:\n" +
44                 "  inputs:\n" +
45                 "    " + ETSI_CONFIG + ":\n" +
46                 "      type: string\n" +
47                 "      description: The ETSI configuration\n" +
48                 "  node_templates:\n" + vnfContent;
49     }
50
51     protected String buildVdu(String name, JsonObject vnf, JsonObject vdu, Set<Map.Entry<String, JsonElement>> nodes) {
52         String memorySize = "";
53         String cpuCount = "";
54         StringBuilder body = new StringBuilder();
55         JsonArray vduRequirements = childElement(vdu.getAsJsonObject(), REQUIREMENTS).getAsJsonArray();
56         for (int i = 0; i < vduRequirements.size(); i++) {
57             JsonObject requirement = vduRequirements.get(i).getAsJsonObject();
58             Map.Entry<String, JsonElement> next = requirement.entrySet().iterator().next();
59             String s = next.getKey();
60             if ("virtual_compute".equals(s)) {
61                 JsonObject virtualCompute = get(next.getValue().getAsString(), nodes).getAsJsonObject();
62                 cpuCount = childElement(child(child(virtualCompute, PROPERTIES), "virtual_cpu"), "num_virtual_cpu").getAsString();
63                 memorySize = childElement(child(child(virtualCompute, PROPERTIES), "virtual_memory"), "virtual_mem_size").getAsString();
64             } else if ("virtual_storage".equals(s)) {
65                 String item = indent(
66                         "- virtual_storage:\n" +
67                                 "    capability: tosca.capabilities.nfv.VirtualStorage\n" +
68                                 "    node: " + next.getValue().getAsString() + "\n", 4);
69                 body.append(item);
70             }
71             next.getValue();
72         }
73         JsonObject flavourProperties = child(child(child(vnf, "capabilities"), "deployment_flavour"), "properties");
74         JsonObject vduSizes = child(child(flavourProperties, "vdu_profile"), name);
75         String header = indent(name + ":\n" +
76                 "  type: tosca.nodes.nfv.Vdu.Compute\n" +
77                 "  properties:\n" +
78                 "    name: " + name + "\n" +
79                 "    description: " + childElement(child(vdu, PROPERTIES), "description").getAsString() + "\n" +
80                 "    configurable_properties:\n" +
81                 "    vdu_profile:\n" +
82                 "      min_number_of_instances: " + childElement(vduSizes, "min_number_of_instances").getAsString() + "\n" +
83                 "      max_number_of_instances: " + childElement(vduSizes, "max_number_of_instances").getAsString() + "\n" +
84                 "  capabilities:\n" +
85                 "    virtual_compute:\n" +
86                 indent(
87                         "properties:\n" +
88                                 "  virtual_memory:\n" +
89                                 "    virtual_mem_size: " + memorySize + "\n" +
90                                 "  virtual_cpu:\n" +
91                                 "    num_virtual_cpu: " + cpuCount + "\n", 3) +
92                 "  " + REQUIREMENTS + ":\n", 2);
93         return header + body.toString();
94     }
95
96     private String buildVnf(JsonObject vnf, Map<String, JsonElement> virtualLinks) {
97         JsonObject vnfProperties = child(vnf, PROPERTIES);
98         JsonObject flavourProperties = child(child(child(vnf, "capabilities"), "deployment_flavour"), "properties");
99         StringBuilder vlContent = new StringBuilder();
100         for (Map.Entry<String, JsonElement> virtualLink : virtualLinks.entrySet()) {
101             vlContent.append(indent("- virtual_link:\n" +
102                     "    capability: tosca.capabilities.nfv.VirtualLinkable\n" +
103                     "    node: " + virtualLink.getKey() + "\n", 4));
104         }
105         return indent("VNF:\n" +
106                 "  type: tosca.nodes.nfv.VNF\n" +
107                 "  " + PROPERTIES + ":\n" +
108                 "    descriptor_id: " + childElement(vnfProperties, "descriptor_id").getAsString() + "\n" +
109                 "    descriptor_version: " + childElement(vnfProperties, "descriptor_version").getAsString() + "\n" +
110                 "    provider: " + childElement(vnfProperties, "provider").getAsString() + "\n" +
111                 "    product_name: " + childElement(vnfProperties, "product_name").getAsString() + "\n" +
112                 "    software_version: " + childElement(vnfProperties, "software_version").getAsString() + "\n" +
113                 "    product_info_name: " + childElement(vnfProperties, "product_info_name").getAsString() + "\n" +
114                 (vnfProperties.has("product_info_description") ?
115                         "    product_info_description: " + childElement(vnfProperties, "product_info_description").getAsString() + "\n" : "") +
116                 "    vnfm_info: [ " + SelfRegistrationManager.SERVICE_NAME + " ]\n" +
117                 "    flavour_id: " + childElement(flavourProperties, "flavour_id").getAsString() + "\n" +
118                 "    flavour_description: " + childElement(flavourProperties, "description").getAsString() + "\n", 2) +
119                 "      " + REQUIREMENTS + ":\n" +
120                 vlContent.toString();
121     }
122
123     protected String buildIcp(String name, JsonObject icp) {
124         if (icp.has(REQUIREMENTS)) {
125             JsonArray requirements = icp.get(REQUIREMENTS).getAsJsonArray();
126             String vdu = getRequirement(requirements, "virtual_binding");
127             String vl = getRequirement(requirements, "virtual_link");
128             if (vdu == null) {
129                 logger.warn("The {} internal connection point does not have a VDU", name);
130             } else if (vl == null) {
131                 logger.warn("The {} internal connection point does not have a VL", name);
132             } else {
133                 JsonObject properties = child(icp, PROPERTIES);
134                 return indent(name + ":\n" +
135                         "  type: tosca.nodes.nfv.VduCp\n" +
136                         "  " + PROPERTIES + ":\n" +
137                         "    layer_protocol: [ " + childElement(properties, "layer_protocol").getAsString() + " ]\n" +
138                         (properties.has(DESCRIPTION) ?
139                                 "    description: " + childElement(properties, DESCRIPTION).getAsString() + "\n" : "") +
140                         "    protocol_data: []\n" +
141                         "    trunk_mode: false\n" +
142                         "  requirements:\n" +
143                         "    - virtual_binding: " + vdu + "\n" +
144                         "    - virtual_link: " + vl + "\n", 2);
145             }
146         } else {
147             logger.warn("The {} internal connection point does not have a requirements section", name);
148         }
149         return "";
150     }
151
152     protected String buildVduCpd(String name, String vdu, JsonObject properties) {
153         return indent(name + ":\n" +
154                 "  type: tosca.nodes.nfv.VduCp\n" +
155                 "  " + PROPERTIES + ":\n" +
156                 "    layer_protocol: [ " + childElement(properties, "layer_protocol").getAsString() + " ]\n" +
157                 "    protocol_data: [ ]\n" +
158                 "    trunk_mode: false\n" +
159                 (properties.has(DESCRIPTION) ?
160                         "    description: " + childElement(properties, DESCRIPTION).getAsString() + "\n" : "") +
161                 "  requirements:\n" +
162                 "    - virtual_binding: " + vdu + "\n", 2);
163     }
164
165     protected String buildVolume(String nodeName, JsonObject volume) {
166         return indent(nodeName + ":\n" +
167                 "  type: tosca.nodes.nfv.Vdu.VirtualStorage\n" +
168                 "  properties:\n" +
169                 "    type_of_storage: volume\n" +
170                 "    size_of_storage: " + childElement(child(volume, PROPERTIES), "size_of_storage").getAsString() + "\n", 2);
171     }
172
173     protected String buildVl(JsonObject vlProperties, String name) {
174         JsonObject connectivityType = child(vlProperties, "connectivity_type");
175         return indent(name + ":\n" +
176                 "  type: tosca.nodes.nfv.VnfVirtualLink\n" +
177                 "  properties:\n" +
178                 "    connectivity_type:\n" +
179                 "      layer_protocol: [ " + childElement(connectivityType, "layer_protocol").getAsString() + " ]\n" +
180                 (connectivityType.has("flow_pattern") ? "      flow_pattern: " + childElement(connectivityType, "flow_pattern").getAsString() + "\n" : "") +
181                 "    vl_profile:\n" +
182                 "      max_bit_rate_requirements:\n" +
183                 "        root: " + Integer.MAX_VALUE + "\n" + //FIXME GAP IN CBAM TEMPLATE
184                 "        leaf: " + Integer.MAX_VALUE + "\n" + //FIXME GAP IN CBAM TEMPLATE
185                 "      min_bit_rate_requirements:\n" +
186                 "        root: 0\n" + //FIXME GAP IN CBAM TEMPLATE
187                 "        leaf: 0\n", 2);  //FIXME GAP IN CBAM TEMPLATE
188     }
189 }