From 1a29d7be6656f3fac3cacd73ddd5a2e050fb04e7 Mon Sep 17 00:00:00 2001 From: Denes Nemeth Date: Tue, 6 Mar 2018 11:24:26 +0100 Subject: Fix most sonar issues Change-Id: I2dfae3f808aeaf94513a6e3c917eea52d6ab6322 Signed-off-by: Denes Nemeth Issue-ID: VFC-728 --- .../nokia/packagetransformer/OnapVnfdBuilder.java | 222 +++++++++++---------- 1 file changed, 122 insertions(+), 100 deletions(-) (limited to 'nokiav2/driver/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/packagetransformer/OnapVnfdBuilder.java') diff --git a/nokiav2/driver/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/packagetransformer/OnapVnfdBuilder.java b/nokiav2/driver/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/packagetransformer/OnapVnfdBuilder.java index d4ff6e41..01256aa0 100644 --- a/nokiav2/driver/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/packagetransformer/OnapVnfdBuilder.java +++ b/nokiav2/driver/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/packagetransformer/OnapVnfdBuilder.java @@ -20,6 +20,7 @@ import com.google.gson.Gson; import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; +import org.jetbrains.annotations.Nullable; import org.yaml.snakeyaml.Yaml; import java.util.Map; @@ -34,20 +35,61 @@ import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.childElem */ public class OnapVnfdBuilder { + public static final String DESCRIPTION = "description"; + public static final String PROPERTIES = "properties"; + public static final String REQUIREMENTS = "requirements"; + + /** + * @param cbamVnfd the CBAM VNFD + * @return the converted ONAP VNFD + */ + public String toOnapVnfd(String cbamVnfd) { + JsonObject root = new Gson().toJsonTree(new Yaml().load(cbamVnfd)).getAsJsonObject(); + JsonObject topologyTemplate = child(root, "topology_template"); + if (topologyTemplate.has("node_templates")) { + Set> nodeTemplates = child(topologyTemplate, "node_templates").entrySet(); + StringBuilder body = new StringBuilder(); + for (Map.Entry node : nodeTemplates) { + String type = childElement(node.getValue().getAsJsonObject(), "type").getAsString(); + switch (type) { + case "tosca.nodes.nfv.VDU": + body.append(buildVdu(node.getKey(), node.getValue().getAsJsonObject(), nodeTemplates)); + break; + case "tosca.nodes.nfv.VirtualStorage": + body.append(buildVolume(node.getKey(), node.getValue().getAsJsonObject())); + break; + case "tosca.nodes.nfv.VL": + body.append(buildVl(node.getKey())); + break; + case "tosca.nodes.nfv.ICP": + body.append(buildIcp(node.getKey(), node.getValue().getAsJsonObject())); + break; + case "tosca.nodes.nfv.ECP": + body.append(buildEcp(node.getKey(), node.getValue(), nodeTemplates)); + break; + default: + // + } + } + return buildHeader(topologyTemplate) + body.toString(); + } + return buildHeader(topologyTemplate); + } + private String buildHeader(JsonObject toplogyTemplate) { - JsonObject properties = child(child(toplogyTemplate, "substitution_mappings"), "properties"); - String descriptor_version = properties.get("descriptor_version").getAsString(); + JsonObject properties = child(child(toplogyTemplate, "substitution_mappings"), PROPERTIES); + String descriptorVersion = properties.get("descriptor_version").getAsString(); return "tosca_definitions_version: tosca_simple_yaml_1_0\n" + "\n" + "metadata:\n" + " vendor: Nokia\n" + - " csarVersion: " + descriptor_version + "\n" + + " csarVersion: " + descriptorVersion + "\n" + " csarProvider: " + properties.get("provider").getAsString() + "\n" + " id: Simple\n" + " version: " + properties.get("software_version").getAsString() + "\n" + " csarType: NFAR\n" + " name: " + properties.get("product_name").getAsString() + "\n" + - " vnfdVersion: " + descriptor_version + "\n\n" + + " vnfdVersion: " + descriptorVersion + "\n\n" + "topology_template:\n" + " node_templates:\n"; } @@ -65,23 +107,23 @@ public class OnapVnfdBuilder { String memorySize = ""; String cpuCount = ""; StringBuilder body = new StringBuilder(); - JsonArray vduRequirements = childElement(vdu.getAsJsonObject(), "requirements").getAsJsonArray(); + JsonArray vduRequirements = childElement(vdu.getAsJsonObject(), REQUIREMENTS).getAsJsonArray(); for (int i = 0; i < vduRequirements.size(); i++) { JsonObject requirement = vduRequirements.get(i).getAsJsonObject(); Map.Entry next = requirement.entrySet().iterator().next(); - switch (next.getKey()) { - case "virtual_compute": - JsonObject virtualCompute = get(next.getValue().getAsString(), nodes).getAsJsonObject(); - cpuCount = childElement(child(child(virtualCompute, "properties"), "virtual_cpu"), "num_virtual_cpu").getAsString(); - memorySize = childElement(child(child(virtualCompute, "properties"), "virtual_memory"), "virtual_mem_size").getAsString(); - break; - case "virtual_storage": - String item = - " - virtual_storage:\n" + - " capability: tosca.capabilities.nfv.VirtualStorage\n" + - " node: " + next.getValue().getAsString() + "\n"; - body.append(item); - break; + String s = next.getKey(); + if ("virtual_compute".equals(s)) { + JsonObject virtualCompute = get(next.getValue().getAsString(), nodes).getAsJsonObject(); + cpuCount = childElement(child(child(virtualCompute, PROPERTIES), "virtual_cpu"), "num_virtual_cpu").getAsString(); + memorySize = childElement(child(child(virtualCompute, PROPERTIES), "virtual_memory"), "virtual_mem_size").getAsString(); + + } else if ("virtual_storage".equals(s)) { + String item = + " - virtual_storage:\n" + + " capability: tosca.capabilities.nfv.VirtualStorage\n" + + " node: " + next.getValue().getAsString() + "\n"; + body.append(item); + } next.getValue(); } @@ -94,82 +136,22 @@ public class OnapVnfdBuilder { " virtual_mem_size: " + memorySize + "\n" + " virtual_cpu:\n" + " num_virtual_cpu: " + cpuCount + "\n" + - " requirements:\n"; + " " + REQUIREMENTS + ":\n"; return header + body.toString(); } - /** - * @param cbamVnfd the CBAM VNFD - * @return the converted ONAP VNFD - */ - public String toOnapVnfd(String cbamVnfd) { - JsonObject root = new Gson().toJsonTree(new Yaml().load(cbamVnfd)).getAsJsonObject(); - JsonObject topology_template = child(root, "topology_template"); - if (topology_template.has("node_templates")) { - Set> node_templates = child(topology_template, "node_templates").entrySet(); - StringBuilder body = new StringBuilder(); - for (Map.Entry node : node_templates) { - String type = childElement(node.getValue().getAsJsonObject(), "type").getAsString(); - switch (type) { - case "tosca.nodes.nfv.VDU": - body.append(buildVdu(node.getKey(), node.getValue().getAsJsonObject(), node_templates)); - break; - case "tosca.nodes.nfv.VirtualStorage": - body.append(buildVolume(node.getKey(), node.getValue().getAsJsonObject())); - break; - case "tosca.nodes.nfv.VL": - body.append(buildVl(node.getKey())); - break; - case "tosca.nodes.nfv.ICP": - body.append(buildIcp(node.getKey(), node.getValue().getAsJsonObject())); - break; - case "tosca.nodes.nfv.ECP": - body.append(buildEcp(node.getKey(), node.getValue(), node_templates)); - break; - } - } - return buildHeader(topology_template) + body.toString(); - } - return buildHeader(topology_template); - } - private String buildEcp(String name, JsonElement ecp, Set> nodes) { - if (ecp.getAsJsonObject().has("requirements")) { - JsonArray requirements = ecp.getAsJsonObject().get("requirements").getAsJsonArray(); - String icpName = null; - for (int i = 0; i < requirements.size(); i++) { - JsonElement requirement = requirements.get(i); - Map.Entry next = requirement.getAsJsonObject().entrySet().iterator().next(); - switch (next.getKey()) { - case "internal_connection_point": - icpName = next.getValue().getAsString(); - - } - } + if (ecp.getAsJsonObject().has(REQUIREMENTS)) { + JsonArray requirements = ecp.getAsJsonObject().get(REQUIREMENTS).getAsJsonArray(); + String icpName = getIcpName(requirements); if (icpName != null) { JsonObject icpNode = get(icpName, nodes).getAsJsonObject(); - String vdu = null; - if (icpNode.has("requirements")) { - requirements = icpNode.getAsJsonObject().get("requirements").getAsJsonArray(); - for (int i = 0; i < requirements.size(); i++) { - JsonElement requirement = requirements.get(i); - Map.Entry next = requirement.getAsJsonObject().entrySet().iterator().next(); - switch (next.getKey()) { - case "virtual_binding": - vdu = next.getValue().getAsString(); - } - } + if (icpNode.has(REQUIREMENTS)) { + requirements = icpNode.getAsJsonObject().get(REQUIREMENTS).getAsJsonArray(); + String vdu = getVdu(requirements); if (vdu != null) { - JsonObject properties = child(icpNode, "properties"); - return " " + name + ":\n" + - " type: tosca.nodes.nfv.VduCpd\n" + - " properties:\n" + - " layer_protocol: " + childElement(properties, "layer_protocol").getAsString() + "\n" + - " role: leaf\n" + - (properties.has("description") ? - " description: " + childElement(properties, "description").getAsString() + "\n" : "") + - " requirements:\n" + - " - virtual_binding: " + vdu + "\n"; + JsonObject properties = child(icpNode, PROPERTIES); + return buildVduCpd(name, vdu, properties); } } } @@ -177,30 +159,70 @@ public class OnapVnfdBuilder { return ""; } + @Nullable + private String getVdu(JsonArray requirements) { + String vdu = null; + for (int i = 0; i < requirements.size(); i++) { + JsonElement requirement = requirements.get(i); + Map.Entry next = requirement.getAsJsonObject().entrySet().iterator().next(); + String s = next.getKey(); + if ("virtual_binding".equals(s)) { + vdu = next.getValue().getAsString(); + } + } + return vdu; + } + + @Nullable + private String getIcpName(JsonArray requirements) { + String icpName = null; + for (int i = 0; i < requirements.size(); i++) { + JsonElement requirement = requirements.get(i); + Map.Entry next = requirement.getAsJsonObject().entrySet().iterator().next(); + String s = next.getKey(); + if ("internal_connection_point".equals(s)) { + icpName = next.getValue().getAsString(); + } + } + return icpName; + } + + private String buildVduCpd(String name, String vdu, JsonObject properties) { + return " " + name + ":\n" + + " type: tosca.nodes.nfv.VduCpd\n" + + " " + PROPERTIES + ":\n" + + " layer_protocol: " + childElement(properties, "layer_protocol").getAsString() + "\n" + + " role: leaf\n" + + (properties.has(DESCRIPTION) ? + " description: " + childElement(properties, DESCRIPTION).getAsString() + "\n" : "") + + " requirements:\n" + + " - virtual_binding: " + vdu + "\n"; + } + private String buildIcp(String name, JsonObject icp) { - if (icp.has("requirements")) { - JsonArray requirements = icp.get("requirements").getAsJsonArray(); + if (icp.has(REQUIREMENTS)) { + JsonArray requirements = icp.get(REQUIREMENTS).getAsJsonArray(); String vdu = null; String vl = null; for (int i = 0; i < requirements.size(); i++) { JsonElement requirement = requirements.get(i); Map.Entry next = requirement.getAsJsonObject().entrySet().iterator().next(); - switch (next.getKey()) { - case "virtual_binding": - vdu = next.getValue().getAsString(); - case "virtual_link": - vl = next.getValue().getAsString(); - break; + String s = next.getKey(); + if ("virtual_binding".equals(s)) { + vdu = next.getValue().getAsString(); + + } else if ("virtual_link".equals(s)) { + vl = next.getValue().getAsString(); } } if (vdu != null && vl != null) { - JsonObject properties = child(icp, "properties"); + JsonObject properties = child(icp, PROPERTIES); return " " + name + ":\n" + " type: tosca.nodes.nfv.VduCpd\n" + - " properties:\n" + + " " + PROPERTIES + ":\n" + " layer_protocol: " + childElement(properties, "layer_protocol").getAsString() + "\n" + - " role: leaf\n" + (properties.has("description") ? - " description: " + childElement(properties, "description").getAsString() + "\n" : "") + + " role: leaf\n" + (properties.has(DESCRIPTION) ? + " description: " + childElement(properties, DESCRIPTION).getAsString() + "\n" : "") + " requirements:\n" + " - virtual_binding: " + vdu + "\n" + " - virtual_link: " + vl + "\n"; @@ -215,7 +237,7 @@ public class OnapVnfdBuilder { " properties:\n" + " id: " + nodeName + "\n" + " type_of_storage: volume\n" + - " size_of_storage: " + childElement(child(volume, "properties"), "size_of_storage").getAsString() + "\n"; + " size_of_storage: " + childElement(child(volume, PROPERTIES), "size_of_storage").getAsString() + "\n"; } private String buildVl(String name) { -- cgit 1.2.3-korg