diff options
author | Denes Nemeth <denes.nemeth@nokia.com> | 2018-02-12 20:55:54 +0100 |
---|---|---|
committer | Denes Nemeth <denes.nemeth@nokia.com> | 2018-02-23 11:44:45 +0100 |
commit | b17042b955489d8a023d09abad5436ff9b900dc3 (patch) | |
tree | 1e4392ac04a2fb1ed8d17075d504cf6594acaf16 /nokiav2/driver/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/packagetransformer | |
parent | d4982f7b1777e9cdae9a4cc7d0d104263889ac69 (diff) |
Updating Nokia driver
Change-Id: I950afe6acbdb359cd67a448024f006a45e8fc293
Signed-off-by: Denes Nemeth <denes.nemeth@nokia.com>
Issue-ID: VFC-728
Diffstat (limited to 'nokiav2/driver/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/packagetransformer')
5 files changed, 506 insertions, 0 deletions
diff --git a/nokiav2/driver/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/packagetransformer/CbamVnfPackageBuilder.java b/nokiav2/driver/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/packagetransformer/CbamVnfPackageBuilder.java new file mode 100644 index 00000000..5f9f6341 --- /dev/null +++ b/nokiav2/driver/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/packagetransformer/CbamVnfPackageBuilder.java @@ -0,0 +1,72 @@ +/* + * Copyright 2016-2017, Nokia Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.packagetransformer; + +import com.google.common.io.ByteStreams; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; +import java.util.zip.ZipOutputStream; + +import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.SystemFunctions.systemFunctions; + +/** + * Builds a CBAM VNF package capable to be deployed on ONAP from a CBAM package + */ +public class CbamVnfPackageBuilder { + + /** + * @param originalCbamVnfPackage the original CBAM VNF package + * @param vnfdLocation the location of the VNFD within the CBAM VNF package + * @param modifiedCbamVnfdContent the modified CBAM VNFD content + * @return the mod + */ + public byte[] toModifiedCbamVnfPackage(byte[] originalCbamVnfPackage, String vnfdLocation, String modifiedCbamVnfdContent) throws IOException { + ZipInputStream zipInputStream = new ZipInputStream(new ByteArrayInputStream(originalCbamVnfPackage)); + ByteArrayOutputStream result = new ByteArrayOutputStream(); + ZipOutputStream out = new ZipOutputStream(result); + ZipEntry zipEntry; + while ((zipEntry = zipInputStream.getNextEntry()) != null) { + if (zipEntry.getName().matches(vnfdLocation)) { + out.putNextEntry(new ZipEntry(vnfdLocation)); + out.write(modifiedCbamVnfdContent.getBytes()); + out.closeEntry(); + } else { + out.putNextEntry(new ZipEntry(zipEntry.getName())); + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + ByteStreams.copy(zipInputStream, byteArrayOutputStream); + out.write(byteArrayOutputStream.toByteArray()); + out.closeEntry(); + } + } + out.putNextEntry(new ZipEntry("javascript/cbam.pre.collectConnectionPoints.js")); + out.write(systemFunctions().loadFile("cbam.pre.collectConnectionPoints.js")); + out.closeEntry(); + out.putNextEntry(new ZipEntry("javascript/cbam.collectConnectionPoints.js")); + out.write(systemFunctions().loadFile("cbam.collectConnectionPoints.js")); + out.closeEntry(); + out.putNextEntry(new ZipEntry("javascript/cbam.post.collectConnectionPoints.js")); + out.write(systemFunctions().loadFile("cbam.post.collectConnectionPoints.js")); + out.closeEntry(); + out.close(); + zipInputStream.close(); + return result.toByteArray(); + } +} diff --git a/nokiav2/driver/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/packagetransformer/CbamVnfdBuilder.java b/nokiav2/driver/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/packagetransformer/CbamVnfdBuilder.java new file mode 100644 index 00000000..ba17bbfd --- /dev/null +++ b/nokiav2/driver/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/packagetransformer/CbamVnfdBuilder.java @@ -0,0 +1,100 @@ +/* + * Copyright 2016-2017, Nokia Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.packagetransformer; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.dataformat.yaml.YAMLMapper; +import com.google.gson.*; +import org.yaml.snakeyaml.Yaml; + +import java.io.IOException; + +import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.child; + +/** + * Modifies a CBAM VNFD to fit ONAP + */ +public class CbamVnfdBuilder { + + /** + * @param cbamVnfdContent the original CBAM VNFD + * @return the modified content CBAM VNFD + */ + public String build(String cbamVnfdContent) throws IOException { + JsonObject root = new Gson().toJsonTree(new Yaml().load(cbamVnfdContent)).getAsJsonObject(); + JsonObject substitution_mappings = child(child(root, "topology_template"), "substitution_mappings"); + JsonObject extensions = addChild(addChild(addChild(addChild(addChild(substitution_mappings, "capabilities"), "vnf"), "properties"), "modifiable_attributes"), "extensions"); + JsonObject onapCsarId = addChild(extensions, "onapCsarId"); + onapCsarId.add("default", new JsonPrimitive("kuku")); + JsonObject vimId = addChild(extensions, "vimId"); + vimId.add("default", new JsonPrimitive("kuku")); + JsonObject interfaces = child(substitution_mappings, "interfaces"); + JsonObject basic = addChild(interfaces, "Basic"); + addOperationParams(addChild(basic, "instantiate")); + addOperationParams(addChild(basic, "terminate")); + if (interfaces.has("Scalable")) { + addOperationParams(addChild(child(interfaces, "Scalable"), "scale")); + } + if (interfaces.has("Healable")) { + addOperationParams(addChild(child(interfaces, "Healable"), "heal")); + } + JsonNode jsonNodeTree = new ObjectMapper().readTree(new GsonBuilder().setPrettyPrinting().create().toJson(root)); + return new YAMLMapper().writeValueAsString(jsonNodeTree); + } + + private void addOperationParams(JsonObject operation) { + JsonObject inputs = addChild(operation, "inputs"); + JsonObject extensions = addChild(inputs, "extensions"); + JsonArray pre_actions = addChildArray(extensions, "pre_actions"); + pre_actions.add(addAction("javascript/cbam.pre.collectConnectionPoints.js")); + JsonArray post_actions = addChildArray(extensions, "post_actions"); + post_actions.add(addAction("javascript/cbam.post.collectConnectionPoints.js")); + JsonObject additional_parameters = addChild(inputs, "additional_parameters"); + additional_parameters.addProperty("jobId", "kuku"); + } + + private JsonElement addAction(String jsAction) { + JsonObject action = new JsonObject(); + action.addProperty("javascript", jsAction); + JsonArray myInclude = new JsonArray(); + myInclude.add("javascript/cbam.collectConnectionPoints.js"); + action.add("include", myInclude); + action.addProperty("output", "operation_result"); + return action; + } + + private JsonArray addChildArray(JsonObject root, String name) { + if (root.has(name)) { + return root.get(name).getAsJsonArray(); + } else { + JsonArray child = new JsonArray(); + root.add(name, child); + return child; + } + } + + private JsonObject addChild(JsonObject root, String name) { + if (root.has(name)) { + return root.get(name).getAsJsonObject(); + } else { + JsonObject child = new JsonObject(); + root.add(name, child); + return child; + } + } +} diff --git a/nokiav2/driver/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/packagetransformer/OnapVnfPackageBuilder.java b/nokiav2/driver/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/packagetransformer/OnapVnfPackageBuilder.java new file mode 100644 index 00000000..f769becb --- /dev/null +++ b/nokiav2/driver/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/packagetransformer/OnapVnfPackageBuilder.java @@ -0,0 +1,85 @@ +/* + * Copyright 2016-2017, Nokia Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.packagetransformer; + +import com.google.common.io.ByteStreams; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.util.zip.ZipEntry; +import java.util.zip.ZipOutputStream; + +import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.SystemFunctions.systemFunctions; +import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CatalogManager.getFileInZip; +import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CatalogManager.getVnfdLocation; + +/** + * Transforms a CBAM package into an ONAP package + */ + +public class OnapVnfPackageBuilder { + + /** + * Entry point for the command line package transformer + * + * @param args not used (required due to signature) + */ + public static void main(String[] args) throws Exception { + byte[] covert = new OnapVnfPackageBuilder().covert(systemFunctions().in()); + systemFunctions().out().write(covert); + } + + /** + * @param zip the original CBAM package + * @return the converted ONAP package + */ + public byte[] covert(InputStream zip) throws Exception { + byte[] cbamVnfPackage = ByteStreams.toByteArray(zip); + String vnfdLocation = getVnfdLocation(new ByteArrayInputStream(cbamVnfPackage)); + ByteArrayOutputStream vnfdContent = getFileInZip(new ByteArrayInputStream(cbamVnfPackage), vnfdLocation); + byte[] cbamVnfdContent = vnfdContent.toByteArray(); + String onapVnfd = new OnapVnfdBuilder().toOnapVnfd(new String(cbamVnfdContent, StandardCharsets.UTF_8)); + byte[] modifiedCbamPackage = new CbamVnfPackageBuilder().toModifiedCbamVnfPackage(cbamVnfPackage, vnfdLocation, new CbamVnfdBuilder().build(new String(cbamVnfdContent))); + return buildNewOnapPackage(modifiedCbamPackage, onapVnfd); + } + + private byte[] buildNewOnapPackage(byte[] modifiedCbamPackage, String onapVnfd) throws Exception { + ByteArrayOutputStream result = new ByteArrayOutputStream(); + ZipOutputStream out = new ZipOutputStream(result); + out.putNextEntry(new ZipEntry("Artifacts/Deployment/OTHER/cbam.package.zip")); + out.write(modifiedCbamPackage); + out.closeEntry(); + out.putNextEntry(new ZipEntry("TOSCA-Metadata/TOSCA.meta")); + out.write(systemFunctions().loadFile("TOSCA.meta")); + out.closeEntry(); + out.putNextEntry(new ZipEntry("MainServiceTemplate.yaml")); + out.write(onapVnfd.getBytes()); + out.closeEntry(); + out.putNextEntry(new ZipEntry("Definitions/MainServiceTemplate.yaml")); + out.write(onapVnfd.getBytes()); + out.closeEntry(); + out.putNextEntry(new ZipEntry("MainServiceTemplate.meta")); + out.write(systemFunctions().loadFile("MainServiceTemplate.meta")); + out.closeEntry(); + out.close(); + return result.toByteArray(); + } + + +} 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 new file mode 100644 index 00000000..d4ff6e41 --- /dev/null +++ b/nokiav2/driver/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/packagetransformer/OnapVnfdBuilder.java @@ -0,0 +1,229 @@ +/* + * Copyright 2016-2017, Nokia Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.packagetransformer; + +import com.google.gson.Gson; +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import org.yaml.snakeyaml.Yaml; + +import java.util.Map; +import java.util.NoSuchElementException; +import java.util.Set; + +import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.child; +import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.childElement; + +/** + * Transforms a CBAM package into an ONAP package + */ +public class OnapVnfdBuilder { + + private String buildHeader(JsonObject toplogyTemplate) { + JsonObject properties = child(child(toplogyTemplate, "substitution_mappings"), "properties"); + String descriptor_version = 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" + + " 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" + + "topology_template:\n" + + " node_templates:\n"; + } + + private JsonElement get(String name, Set<Map.Entry<String, JsonElement>> nodes) { + for (Map.Entry<String, JsonElement> node : nodes) { + if (name.equals(node.getKey())) { + return node.getValue(); + } + } + throw new NoSuchElementException("The VNFD does not have a node called " + name + " but required by an other node"); + } + + private String buildVdu(String name, JsonObject vdu, Set<Map.Entry<String, JsonElement>> nodes) { + String memorySize = ""; + String cpuCount = ""; + StringBuilder body = new StringBuilder(); + JsonArray vduRequirements = childElement(vdu.getAsJsonObject(), "requirements").getAsJsonArray(); + for (int i = 0; i < vduRequirements.size(); i++) { + JsonObject requirement = vduRequirements.get(i).getAsJsonObject(); + Map.Entry<String, JsonElement> 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; + } + next.getValue(); + } + String header = " " + name + ":\n" + + " type: tosca.nodes.nfv.VDU.Compute\n" + + " capabilities:\n" + + " virtual_compute:\n" + + " properties:\n" + + " virtual_memory:\n" + + " virtual_mem_size: " + memorySize + "\n" + + " virtual_cpu:\n" + + " num_virtual_cpu: " + cpuCount + "\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<Map.Entry<String, JsonElement>> node_templates = child(topology_template, "node_templates").entrySet(); + StringBuilder body = new StringBuilder(); + for (Map.Entry<String, JsonElement> 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<Map.Entry<String, JsonElement>> 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<String, JsonElement> next = requirement.getAsJsonObject().entrySet().iterator().next(); + switch (next.getKey()) { + case "internal_connection_point": + icpName = next.getValue().getAsString(); + + } + } + 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<String, JsonElement> next = requirement.getAsJsonObject().entrySet().iterator().next(); + switch (next.getKey()) { + case "virtual_binding": + vdu = next.getValue().getAsString(); + } + } + 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"; + } + } + } + } + return ""; + } + + private String buildIcp(String name, JsonObject icp) { + 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<String, JsonElement> next = requirement.getAsJsonObject().entrySet().iterator().next(); + switch (next.getKey()) { + case "virtual_binding": + vdu = next.getValue().getAsString(); + case "virtual_link": + vl = next.getValue().getAsString(); + break; + } + } + if (vdu != null && vl != null) { + JsonObject properties = child(icp, "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" + + " - virtual_link: " + vl + "\n"; + } + } + return ""; + } + + private String buildVolume(String nodeName, JsonObject volume) { + return " " + nodeName + ":\n" + + " type: tosca.nodes.nfv.VDU.VirtualStorage\n" + + " properties:\n" + + " id: " + nodeName + "\n" + + " type_of_storage: volume\n" + + " size_of_storage: " + childElement(child(volume, "properties"), "size_of_storage").getAsString() + "\n"; + } + + private String buildVl(String name) { + return " " + name + ":\n" + + " type: tosca.nodes.nfv.VnfVirtualLinkDesc\n" + + " properties:\n" + + " vl_flavours:\n" + + " flavours:\n" + + " flavourId: notUsed\n"; + } +} diff --git a/nokiav2/driver/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/packagetransformer/package-info.java b/nokiav2/driver/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/packagetransformer/package-info.java new file mode 100644 index 00000000..7a94ee79 --- /dev/null +++ b/nokiav2/driver/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/packagetransformer/package-info.java @@ -0,0 +1,20 @@ +/* + * Copyright 2016-2017, Nokia Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Converts a CBAM package into an ONAP package + */ +package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.packagetransformer; |