From d4ee0c786ca2cc27b35fdfaa7d9d53718fa0bd20 Mon Sep 17 00:00:00 2001 From: Denes Nemeth Date: Thu, 31 May 2018 13:59:34 +0200 Subject: Fix template generation for R2 Change-Id: Ibf53ee4f203f248c930611812f3eb248e3023be8 Signed-off-by: Denes Nemeth Issue-ID: VFC-728 --- nokiav2/docs/onboarding.rst | 2 + .../simple/cbam.instantiation.request.json | 60 ----- nokiav2/docs/sampleVnfs/simple/simple.v1.csar | Bin 11484 -> 7668 bytes nokiav2/docs/sampleVnfs/simple/simple.v2.csar | Bin 0 -> 7787 bytes .../simpleDual/cbam.instantiation.request.json | 60 ----- .../docs/sampleVnfs/simpleDual/cbam.package.zip | Bin 0 -> 7488 bytes .../docs/sampleVnfs/simpleDual/etsi_config.json | 39 ++++ .../docs/sampleVnfs/simpleDual/simple.dual.v1.csar | Bin 0 -> 8334 bytes .../docs/sampleVnfs/simpleDual/simple.dual.v2.csar | Bin 0 -> 8516 bytes nokiav2/docs/sampleVnfs/simpleDual/simpleDual.csar | Bin 12325 -> 0 bytes nokiav2/docs/sampleVnfs/simple_cmcc/simple.v1.csar | Bin 11484 -> 7668 bytes nokiav2/docs/sampleVnfs/simple_cmcc/simple.v2.csar | Bin 7773 -> 7787 bytes nokiav2/docs/samplevnf.rst | 34 ++- .../OnapAbstractVnfdBuilder.java | 83 ++++++- .../OnapR1VnfPackageBuilder.java | 84 ------- .../packagetransformer/OnapR1VnfdBuilder.java | 162 +++++++++++++ .../packagetransformer/OnapR2VnfdBuilder.java | 117 ++-------- .../packagetransformer/OnapVnfPackageBuilder.java | 84 +++++++ .../nokia/packagetransformer/OnapVnfdBuilder.java | 256 --------------------- .../vnfm/svnfm/nokia/restapi/ConverterApi.java | 4 +- .../vnfm/svnfm/nokia/vnfm/LifecycleManager.java | 1 + .../driver/vnfm/svnfm/nokia/FullUnitTestSuite.java | 8 +- .../TestOnapAbstractVnfdBuilder.java | 33 +++ .../packagetransformer/TestOnapR1VnfdBuilder.java | 77 +++++++ .../packagetransformer/TestOnapR2VnfdBuilder.java | 8 +- .../TestOnapVnfPackageBuilder.java | 53 ++++- .../packagetransformer/TestOnapVnfdBuilder.java | 85 ------- .../vnfm/svnfm/nokia/restapi/TestConverterApi.java | 25 +- .../test/resources/application-direct.properties | 4 - .../src/test/resources/application.properties | 4 - .../unittests/packageconverter/cbam.package.zip | Bin 1288 -> 1333 bytes .../packageconverter/cbam.package.zip.vnfd | 5 + .../packageconverter/nodes.vnfd.cbam.yaml | 1 - .../packageconverter/nodes.vnfd.onap.v2.yaml | 1 - 34 files changed, 605 insertions(+), 685 deletions(-) delete mode 100644 nokiav2/docs/sampleVnfs/simple/cbam.instantiation.request.json create mode 100644 nokiav2/docs/sampleVnfs/simple/simple.v2.csar delete mode 100644 nokiav2/docs/sampleVnfs/simpleDual/cbam.instantiation.request.json create mode 100644 nokiav2/docs/sampleVnfs/simpleDual/cbam.package.zip create mode 100644 nokiav2/docs/sampleVnfs/simpleDual/etsi_config.json create mode 100644 nokiav2/docs/sampleVnfs/simpleDual/simple.dual.v1.csar create mode 100644 nokiav2/docs/sampleVnfs/simpleDual/simple.dual.v2.csar delete mode 100644 nokiav2/docs/sampleVnfs/simpleDual/simpleDual.csar delete mode 100644 nokiav2/driver/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/packagetransformer/OnapR1VnfPackageBuilder.java create mode 100644 nokiav2/driver/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/packagetransformer/OnapR1VnfdBuilder.java create mode 100644 nokiav2/driver/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/packagetransformer/OnapVnfPackageBuilder.java delete mode 100644 nokiav2/driver/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/packagetransformer/OnapVnfdBuilder.java create mode 100644 nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/packagetransformer/TestOnapAbstractVnfdBuilder.java create mode 100644 nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/packagetransformer/TestOnapR1VnfdBuilder.java delete mode 100644 nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/packagetransformer/TestOnapVnfdBuilder.java diff --git a/nokiav2/docs/onboarding.rst b/nokiav2/docs/onboarding.rst index 80aff0b2..4613777d 100644 --- a/nokiav2/docs/onboarding.rst +++ b/nokiav2/docs/onboarding.rst @@ -181,6 +181,8 @@ The following section requires the CSAR and the ETSI configuration of the VNF to - Select the ETSI configuration file using Browse + - the name of the file on the local file system must be etsi_config.json + - Click on Done - Click on Check in diff --git a/nokiav2/docs/sampleVnfs/simple/cbam.instantiation.request.json b/nokiav2/docs/sampleVnfs/simple/cbam.instantiation.request.json deleted file mode 100644 index a85ca0ae..00000000 --- a/nokiav2/docs/sampleVnfs/simple/cbam.instantiation.request.json +++ /dev/null @@ -1,60 +0,0 @@ -{ - "apiVersion":"3.1", - "computeResourceFlavours": [ - { - "vnfdVirtualComputeDescId": "server_compute", - "resourceId": "m1.small" - } - ], - "extVirtualLinks": [ - { - "extCps": [ - { - "cpdId": "commisioning_ecp" - } - ], - "resourceId": "pub_net" - }, - { - "extCps": [ - { - "cpdId": "vnftovnf_ecp" - } - ], - "resourceId": "preservedNetwork" - } - ], - "zones": [ - { - "resourceId": "compute-a", - "id": "zoneInfoId" - } - ], - "flavourId": "scalable", - "grantlessMode": true, - "softwareImages": [ - { - "resourceId": "ubuntu.16.04", - "vnfdSoftwareImageId": "server_image" - } - ], - "vims": [ - { - "accessInfo": { - "username": "", - "tenant": "", - "password": "", - "region": "RegionOne" - }, - "interfaceInfo": { - "endpoint": "https://:5000/v2.0/", - "trustedCertificates": [], - "skipCertificateVerification": true, - "skipCertificateHostnameCheck": true - }, - "vimInfoType": "OPENSTACK_V2_INFO", - "id": "vim_id" - } - ], - "instantiationLevelId": "default" -} diff --git a/nokiav2/docs/sampleVnfs/simple/simple.v1.csar b/nokiav2/docs/sampleVnfs/simple/simple.v1.csar index bdea8ad8..3f66c11f 100644 Binary files a/nokiav2/docs/sampleVnfs/simple/simple.v1.csar and b/nokiav2/docs/sampleVnfs/simple/simple.v1.csar differ diff --git a/nokiav2/docs/sampleVnfs/simple/simple.v2.csar b/nokiav2/docs/sampleVnfs/simple/simple.v2.csar new file mode 100644 index 00000000..a7466259 Binary files /dev/null and b/nokiav2/docs/sampleVnfs/simple/simple.v2.csar differ diff --git a/nokiav2/docs/sampleVnfs/simpleDual/cbam.instantiation.request.json b/nokiav2/docs/sampleVnfs/simpleDual/cbam.instantiation.request.json deleted file mode 100644 index 16fc4d14..00000000 --- a/nokiav2/docs/sampleVnfs/simpleDual/cbam.instantiation.request.json +++ /dev/null @@ -1,60 +0,0 @@ -{ - "apiVersion":"3.1", - "computeResourceFlavours": [ - { - "vnfdVirtualComputeDescId": "server_compute", - "resourceId": "m1.small" - } - ], - "extVirtualLinks": [ - { - "extCps": [ - { - "cpdId": "commisioning_ecp" - } - ], - "resourceId": "pub_net" - }, - { - "extCps": [ - { - "cpdId": "vnftovnf_ecp" - } - ], - "resourceId": "preservedNetwork" - } - ], - "zones": [ - { - "resourceId": "compute-a", - "id": "zoneInfoId" - } - ], - "flavourId": "scalable", - "grantlessMode": true, - "softwareImages": [ - { - "resourceId": "ubuntu.16.04", - "vnfdSoftwareImageId": "server_image" - } - ], - "vims": [ - { - "accessInfo": { - "username": "", - "tenant": "", - "password": "", - "region": "RegionOne" - }, - "interfaceInfo": { - "endpoint": "https://:5000/v2.0/", - "trustedCertificates": [], - "skipCertificateVerification": true, - "skipCertificateHostnameCheck": true - }, - "vimInfoType": "OPENSTACK_V2_INFO", - "id": "vim_id" - } - ], - "instantiationLevelId": "default" -} diff --git a/nokiav2/docs/sampleVnfs/simpleDual/cbam.package.zip b/nokiav2/docs/sampleVnfs/simpleDual/cbam.package.zip new file mode 100644 index 00000000..48841624 Binary files /dev/null and b/nokiav2/docs/sampleVnfs/simpleDual/cbam.package.zip differ diff --git a/nokiav2/docs/sampleVnfs/simpleDual/etsi_config.json b/nokiav2/docs/sampleVnfs/simpleDual/etsi_config.json new file mode 100644 index 00000000..a0cff014 --- /dev/null +++ b/nokiav2/docs/sampleVnfs/simpleDual/etsi_config.json @@ -0,0 +1,39 @@ +{ + "extensions": [ + { + "name": "myNameKey", + "value": "myName" + } + ], + "computeResourceFlavours": [ + { + "vnfdVirtualComputeDescId": "server_compute", + "resourceId": "m1.small" + } + ], + "extVirtualLinks": [ + { + "extCps": [ + { + "cpdId": "universal_ecp" + } + ], + "resourceId": "pub_net" + } + ], + "zones": [ + { + "resourceId": "compute-a", + "id": "zoneInfoId" + } + ], + "flavourId": "scalable", + "vimType": "OPENSTACK_V3_INFO", + "instantiationLevel": "default", + "softwareImages": [ + { + "resourceId": "ubuntu.16.04", + "vnfdSoftwareImageId": "server_image" + } + ] +} \ No newline at end of file diff --git a/nokiav2/docs/sampleVnfs/simpleDual/simple.dual.v1.csar b/nokiav2/docs/sampleVnfs/simpleDual/simple.dual.v1.csar new file mode 100644 index 00000000..6f2aeec6 Binary files /dev/null and b/nokiav2/docs/sampleVnfs/simpleDual/simple.dual.v1.csar differ diff --git a/nokiav2/docs/sampleVnfs/simpleDual/simple.dual.v2.csar b/nokiav2/docs/sampleVnfs/simpleDual/simple.dual.v2.csar new file mode 100644 index 00000000..89c448b7 Binary files /dev/null and b/nokiav2/docs/sampleVnfs/simpleDual/simple.dual.v2.csar differ diff --git a/nokiav2/docs/sampleVnfs/simpleDual/simpleDual.csar b/nokiav2/docs/sampleVnfs/simpleDual/simpleDual.csar deleted file mode 100644 index 3a43b788..00000000 Binary files a/nokiav2/docs/sampleVnfs/simpleDual/simpleDual.csar and /dev/null differ diff --git a/nokiav2/docs/sampleVnfs/simple_cmcc/simple.v1.csar b/nokiav2/docs/sampleVnfs/simple_cmcc/simple.v1.csar index bdea8ad8..eb52ccb3 100644 Binary files a/nokiav2/docs/sampleVnfs/simple_cmcc/simple.v1.csar and b/nokiav2/docs/sampleVnfs/simple_cmcc/simple.v1.csar differ diff --git a/nokiav2/docs/sampleVnfs/simple_cmcc/simple.v2.csar b/nokiav2/docs/sampleVnfs/simple_cmcc/simple.v2.csar index dbb6cdad..e142609d 100644 Binary files a/nokiav2/docs/sampleVnfs/simple_cmcc/simple.v2.csar and b/nokiav2/docs/sampleVnfs/simple_cmcc/simple.v2.csar differ diff --git a/nokiav2/docs/samplevnf.rst b/nokiav2/docs/samplevnf.rst index dd6efdbe..3228786d 100644 --- a/nokiav2/docs/samplevnf.rst +++ b/nokiav2/docs/samplevnf.rst @@ -4,7 +4,13 @@ Sample VNFs Simple ------ -- CSAR: :download:`csar ` +- CSAR R1: :download:`csar ` + +- CSAR R2: :download:`csar ` + +- Original CBAM package: :download:`csar ` + +- ETSI configuration: :download:`csar ` - NS creation request on VF-C API :download:`create.json ` (several fields must be changed to fit actual environment) @@ -23,7 +29,11 @@ Simple CMCC The Simple VNF altered to suite the CMCC lab environment (no support for floating IPs) -- CSAR: :download:`csar ` +- CSAR R1: :download:`csar ` + +- CSAR R2: :download:`csar ` + +- Original CBAM package: :download:`csar ` - ETSI configuration: :download:`csar ` @@ -43,18 +53,22 @@ The Simple VNF altered to suite the CMCC lab environment (no support for floatin Simple Dual ----------- -- CSAR: :download:`csar ` +- CSAR R1: :download:`csar ` -- NS creation request on VF-C API :download:`create.json ` (several fields must be changed to fit actual environment) +- CSAR R2: :download:`csar ` -- NS instantiation request on VF-C API :download:`instantiate.json ` (several fields must be changed to fit actual environment) +- Original CBAM package: :download:`csar ` -- NS heal request on VF-C API: :download:`heal.json ` (several fields must be changed to fit actual environment) +- ETSI configuration: :download:`csar ` + +- NS creation request on VF-C API :download:`create.json ` (several fields must be changed to fit actual environment) + +- NS instantiation request on VF-C API :download:`instantiate.json ` (several fields must be changed to fit actual environment) -- VNF scale request on VF-C API: :download:`scale.json ` (several fields must be changed to fit actual environment) +- NS instantiation request on VF-C API when ETSI config is not supplied: download:`instantiate.json ` (several fields must be changed to fit actual environment) +- E2E scaling data: :download:`instantiate.json ` (several fields must be changed to fit actual environment) -VOLTE IMS ---------- +- NS heal request on VF-C API: :download:`heal.json ` (several fields must be changed to fit actual environment) -- TODO \ No newline at end of file +- VNF scale request on VF-C API: :download:`scale.json ` (several fields must be changed to fit actual environment) \ No newline at end of file diff --git a/nokiav2/driver/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/packagetransformer/OnapAbstractVnfdBuilder.java b/nokiav2/driver/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/packagetransformer/OnapAbstractVnfdBuilder.java index f7f247a0..5e9be7f6 100644 --- a/nokiav2/driver/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/packagetransformer/OnapAbstractVnfdBuilder.java +++ b/nokiav2/driver/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/packagetransformer/OnapAbstractVnfdBuilder.java @@ -21,6 +21,7 @@ import com.google.gson.Gson; import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; +import java.util.HashMap; import java.util.Map; import java.util.NoSuchElementException; import java.util.Set; @@ -33,9 +34,9 @@ import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.childElem import static org.slf4j.LoggerFactory.getLogger; /** - * Transforms a CBAM package into an ONAP package + * Generic non ONAP version dependent package conversion */ -public class OnapAbstractVnfdBuilder { +abstract class OnapAbstractVnfdBuilder { public static final String DESCRIPTION = "description"; public static final String PROPERTIES = "properties"; public static final String REQUIREMENTS = "requirements"; @@ -63,7 +64,7 @@ public class OnapAbstractVnfdBuilder { return null; } - private JsonElement get(String name, Set> nodes) { + protected JsonElement get(String name, Set> nodes) { for (Map.Entry node : nodes) { if (name.equals(node.getKey())) { return node.getValue(); @@ -71,4 +72,80 @@ public class OnapAbstractVnfdBuilder { } throw new NoSuchElementException("The VNFD does not have a node called " + name + " but required by an other node"); } + + protected String buildEcp(String name, JsonElement ecp, Set> nodes) { + if (ecp.getAsJsonObject().has(REQUIREMENTS)) { + String icpName = getRequirement(ecp.getAsJsonObject().get(REQUIREMENTS).getAsJsonArray(), "internal_connection_point"); + if (icpName != null) { + return buildEcpInternal(name, icpName, nodes); + } else { + logger.warn("The {} ecp does not have an internal connection point", name); + } + } else { + logger.warn("The {} ecp does not have an requirements section", name); + } + return ""; + } + + private String buildEcpInternal(String ecpName, String icpName, Set> nodes) { + JsonObject icpNode = get(icpName, nodes).getAsJsonObject(); + if (icpNode.has(REQUIREMENTS)) { + String vdu = getRequirement(icpNode.getAsJsonObject().get(REQUIREMENTS).getAsJsonArray(), "virtual_binding"); + //internal connection point is bound to VDU + if (vdu != null) { + return buildVduCpd(ecpName, vdu, child(icpNode, PROPERTIES)); + } else { + logger.warn("The {} internal connection point of the {} ecp does not have a VDU", icpName, ecpName); + } + } else { + logger.warn("The {} internal connection point of the {} ecp does not have a requirements section", icpName, ecpName); + } + return ""; + } + + /** + * @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"); + JsonObject substitution_mappings = child(topologyTemplate, "substitution_mappings"); + Map virtualLinks = new HashMap<>(); + 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(); + if ("tosca.nodes.nfv.VDU".equals(type)) { + body.append(buildVdu(node.getKey(), substitution_mappings, node.getValue().getAsJsonObject(), nodeTemplates)); + } else if ("tosca.nodes.nfv.VirtualStorage".equals(type)) { + body.append(buildVolume(node.getKey(), node.getValue().getAsJsonObject())); + } else if ("tosca.nodes.nfv.VL".equals(type)) { + virtualLinks.put(node.getKey(), node.getValue()); + body.append(buildVl(node.getValue().getAsJsonObject().get(PROPERTIES).getAsJsonObject(), node.getKey())); + } else if ("tosca.nodes.nfv.ICP".equals(type)) { + body.append(buildIcp(node.getKey(), node.getValue().getAsJsonObject())); + } else if ("tosca.nodes.nfv.ECP".equals(type)) { + body.append(buildEcp(node.getKey(), node.getValue(), nodeTemplates)); + } else { + logger.warn("The {} type is not converted", type); + } + } + return buildHeader(topologyTemplate, virtualLinks) + body.toString(); + } + return buildHeader(topologyTemplate, virtualLinks); + } + + abstract protected String buildHeader(JsonObject toplogyTemplate, Map virtualLinks); + + abstract protected String buildVduCpd(String name, String vdu, JsonObject properties); + + abstract protected String buildVdu(String name, JsonObject vnf, JsonObject vdu, Set> nodes); + + abstract protected String buildIcp(String name, JsonObject icp); + + abstract protected String buildVolume(String nodeName, JsonObject volume); + + abstract protected String buildVl(JsonObject vlProperties, String name); } diff --git a/nokiav2/driver/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/packagetransformer/OnapR1VnfPackageBuilder.java b/nokiav2/driver/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/packagetransformer/OnapR1VnfPackageBuilder.java deleted file mode 100644 index 6542a032..00000000 --- a/nokiav2/driver/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/packagetransformer/OnapR1VnfPackageBuilder.java +++ /dev/null @@ -1,84 +0,0 @@ -/* - * 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.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 OnapR1VnfPackageBuilder { - - /** - * 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 OnapR1VnfPackageBuilder().covert(systemFunctions().in(), SupportedOnapPackageVersions.V1); - systemFunctions().out().write(covert); - } - - /** - * @param zip the original CBAM package - * @param version - * @return the converted ONAP package - */ - public byte[] covert(InputStream zip, SupportedOnapPackageVersions version) throws IOException { - byte[] cbamVnfPackage = ByteStreams.toByteArray(zip); - String vnfdLocation = getVnfdLocation(new ByteArrayInputStream(cbamVnfPackage)); - ByteArrayOutputStream vnfdContent = getFileInZip(new ByteArrayInputStream(cbamVnfPackage), vnfdLocation); - byte[] cbamVnfdContent = vnfdContent.toByteArray(); - String onapVnfd = SupportedOnapPackageVersions.V2 == version ? - new OnapR2VnfdBuilder().toOnapVnfd(new String(cbamVnfdContent, StandardCharsets.UTF_8)) : - 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 IOException { - 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.closeEntry(); - out.putNextEntry(new ZipEntry("MainServiceTemplate.mf")); - out.write(systemFunctions().loadFile("MainServiceTemplate.mf")); - out.closeEntry(); - out.close(); - return result.toByteArray(); - } -} diff --git a/nokiav2/driver/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/packagetransformer/OnapR1VnfdBuilder.java b/nokiav2/driver/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/packagetransformer/OnapR1VnfdBuilder.java new file mode 100644 index 00000000..4e0fbb0b --- /dev/null +++ b/nokiav2/driver/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/packagetransformer/OnapR1VnfdBuilder.java @@ -0,0 +1,162 @@ +/* + * 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.JsonArray; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import java.util.Map; +import java.util.Set; +import org.slf4j.Logger; + +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; +import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.LifecycleManager.ETSI_CONFIG; +import static org.slf4j.LoggerFactory.getLogger; + +/** + * Transforms a CBAM package into an ONAP package + */ +public class OnapR1VnfdBuilder extends OnapAbstractVnfdBuilder { + private static Logger logger = getLogger(OnapR1VnfdBuilder.class); + + private static String trimUnit(String data) { + //The R1 templates in Amsterdam release can not handle the scalar-unit types in Tosca + //templates, so that the MB, GB, ... units need to be removed even though the created + //Tosca template will be invalid + return data.trim().replaceAll("[^0-9]", ""); + } + + @Override + protected String buildHeader(JsonObject toplogyTemplate, Map virtualLinks) { + 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: " + 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: " + descriptorVersion + "\n\n" + + "topology_template:\n" + + " inputs:\n" + + " " + ETSI_CONFIG + ":\n" + + " type: string\n" + + " description: The ETSI configuration\n" + + " node_templates:\n"; + } + + @Override + protected String buildVdu(String name, JsonObject vnf, JsonObject vdu, Set> 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 next = requirement.entrySet().iterator().next(); + 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 = trimUnit(childElement(child(child(virtualCompute, PROPERTIES), "virtual_memory"), "virtual_mem_size").getAsString()); + } else if ("virtual_storage".equals(s)) { + String item = indent( + "- virtual_storage:\n" + + " capability: tosca.capabilities.nfv.VirtualStorage\n" + + " node: " + next.getValue().getAsString() + "\n", 4); + body.append(item); + + } + next.getValue(); + } + String header = indent(name + ":\n" + + " type: tosca.nodes.nfv.VDU.Compute\n" + + " capabilities:\n" + + " virtual_compute:\n" + + indent( + "properties:\n" + + " virtual_memory:\n" + + " virtual_mem_size: " + trimUnit(memorySize) + "\n" + + " virtual_cpu:\n" + + " num_virtual_cpu: " + cpuCount + "\n", 3) + + " " + REQUIREMENTS + ":\n", 2); + return header + body.toString(); + } + + protected String buildIcp(String name, JsonObject icp) { + if (icp.has(REQUIREMENTS)) { + JsonArray requirements = icp.get(REQUIREMENTS).getAsJsonArray(); + String vdu = getRequirement(requirements, "virtual_binding"); + String vl = getRequirement(requirements, "virtual_link"); + if (vdu == null) { + logger.warn("The {} internal connection point does not have a VDU", name); + } else if (vl == null) { + logger.warn("The {} internal connection point does not have a VL", name); + } else { + JsonObject properties = child(icp, PROPERTIES); + return indent(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", 2); + } + } else { + logger.warn("The {} internal connection point does not have a requirements section", name); + } + return ""; + } + + protected String buildVduCpd(String name, String vdu, JsonObject properties) { + return indent(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", 2); + } + + protected String buildVolume(String nodeName, JsonObject volume) { + return indent(nodeName + ":\n" + + " type: tosca.nodes.nfv.VDU.VirtualStorage\n" + + " properties:\n" + + " id: " + nodeName + "\n" + + " type_of_storage: volume\n" + + " size_of_storage: " + trimUnit(childElement(child(volume, PROPERTIES), "size_of_storage").getAsString()) + "\n", 2); + } + + @Override + protected String buildVl(JsonObject vlProperties, String name) { + return indent(name + ":\n" + + " type: tosca.nodes.nfv.VnfVirtualLinkDesc\n" + + " properties:\n" + + " vl_flavours:\n" + + " flavours:\n" + + " flavourId: notUsed\n", 2); + } +} diff --git a/nokiav2/driver/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/packagetransformer/OnapR2VnfdBuilder.java b/nokiav2/driver/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/packagetransformer/OnapR2VnfdBuilder.java index 099f641c..e242284c 100644 --- a/nokiav2/driver/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/packagetransformer/OnapR2VnfdBuilder.java +++ b/nokiav2/driver/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/packagetransformer/OnapR2VnfdBuilder.java @@ -16,97 +16,39 @@ 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 java.util.HashMap; import java.util.Map; -import java.util.NoSuchElementException; import java.util.Set; import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core.SelfRegistrationManager; import org.slf4j.Logger; -import org.yaml.snakeyaml.Yaml; -import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.packagetransformer.OnapVnfdBuilder.getRequirement; -import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.packagetransformer.OnapVnfdBuilder.indent; 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; +import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.LifecycleManager.ETSI_CONFIG; import static org.slf4j.LoggerFactory.getLogger; /** * Transforms a CBAM package into an ONAP package */ -public class OnapR2VnfdBuilder { - public static final String DESCRIPTION = "description"; - public static final String PROPERTIES = "properties"; - public static final String REQUIREMENTS = "requirements"; +public class OnapR2VnfdBuilder extends OnapAbstractVnfdBuilder { private static Logger logger = getLogger(OnapR2VnfdBuilder.class); - private static String trimUnit(String data) { - //FIXME the unit should not be trimmed VF-C bug - return data; - //data.trim().replaceAll("[^0-9]", ""); - } - - /** - * @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"); - JsonObject substitution_mappings = child(topologyTemplate, "substitution_mappings"); - Map virtualLinks = new HashMap<>(); - 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(); - if ("tosca.nodes.nfv.VDU".equals(type)) { - body.append(buildVdu(node.getKey(), substitution_mappings, node.getValue().getAsJsonObject(), nodeTemplates)); - } else if ("tosca.nodes.nfv.VirtualStorage".equals(type)) { - body.append(buildVolume(node.getKey(), node.getValue().getAsJsonObject())); - } else if ("tosca.nodes.nfv.VL".equals(type)) { - virtualLinks.put(node.getKey(), node.getValue()); - body.append(buildVl(node.getValue().getAsJsonObject().get(PROPERTIES).getAsJsonObject(), node.getKey())); - } else if ("tosca.nodes.nfv.ICP".equals(type)) { - body.append(buildIcp(node.getKey(), node.getValue().getAsJsonObject())); - } else if ("tosca.nodes.nfv.ECP".equals(type)) { - body.append(buildEcp(node.getKey(), node.getValue(), nodeTemplates)); - } else { - logger.warn("The {} type is not converted", type); - } - } - return buildHeader(topologyTemplate, virtualLinks) + body.toString(); - } - return buildHeader(topologyTemplate, virtualLinks); - } - - private String buildHeader(JsonObject toplogyTemplate, Map virtualLinks) { + protected String buildHeader(JsonObject toplogyTemplate, Map virtualLinks) { JsonObject substitution_mappings = child(toplogyTemplate, "substitution_mappings"); String vnfContent = buildVnf(substitution_mappings, virtualLinks); return "tosca_definitions_version: tosca_simple_profile_yaml_1_1\n" + "\n" + "topology_template:\n" + " inputs:\n" + - " etsi_config:\n" + + " " + ETSI_CONFIG + ":\n" + " type: string\n" + " description: The ETSI configuration\n" + " node_templates:\n" + vnfContent; } - private JsonElement get(String name, Set> nodes) { - for (Map.Entry 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 vnf, JsonObject vdu, Set> nodes) { + protected String buildVdu(String name, JsonObject vnf, JsonObject vdu, Set> nodes) { String memorySize = ""; String cpuCount = ""; StringBuilder body = new StringBuilder(); @@ -118,7 +60,7 @@ public class OnapR2VnfdBuilder { 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 = trimUnit(childElement(child(child(virtualCompute, PROPERTIES), "virtual_memory"), "virtual_mem_size").getAsString()); + memorySize = childElement(child(child(virtualCompute, PROPERTIES), "virtual_memory"), "virtual_mem_size").getAsString(); } else if ("virtual_storage".equals(s)) { String item = indent( "- virtual_storage:\n" + @@ -144,27 +86,13 @@ public class OnapR2VnfdBuilder { indent( "properties:\n" + " virtual_memory:\n" + - " virtual_mem_size: " + trimUnit(memorySize) + "\n" + + " virtual_mem_size: " + memorySize + "\n" + " virtual_cpu:\n" + " num_virtual_cpu: " + cpuCount + "\n", 3) + " " + REQUIREMENTS + ":\n", 2); return header + body.toString(); } - private String buildEcp(String name, JsonElement ecp, Set> nodes) { - if (ecp.getAsJsonObject().has(REQUIREMENTS)) { - String icpName = getRequirement(ecp.getAsJsonObject().get(REQUIREMENTS).getAsJsonArray(), "internal_connection_point"); - if (icpName != null) { - return buildEcpInternal(name, icpName, nodes); - } else { - logger.warn("The {} ecp does not have an internal connection point", name); - } - } else { - logger.warn("The {} ecp does not have an requirements section", name); - } - return ""; - } - private String buildVnf(JsonObject vnf, Map virtualLinks) { JsonObject vnfProperties = child(vnf, PROPERTIES); JsonObject flavourProperties = child(child(child(vnf, "capabilities"), "deployment_flavour"), "properties"); @@ -192,23 +120,7 @@ public class OnapR2VnfdBuilder { vlContent.toString(); } - private String buildEcpInternal(String ecpName, String icpName, Set> nodes) { - JsonObject icpNode = get(icpName, nodes).getAsJsonObject(); - if (icpNode.has(REQUIREMENTS)) { - String vdu = getRequirement(icpNode.getAsJsonObject().get(REQUIREMENTS).getAsJsonArray(), "virtual_binding"); - //internal connection point is bound to VDU - if (vdu != null) { - return buildVduCpd(ecpName, vdu, child(icpNode, PROPERTIES)); - } else { - logger.warn("The {} internal connection point of the {} ecp does not have a VDU", icpName, ecpName); - } - } else { - logger.warn("The {} internal connection point of the {} ecp does not have a requirements section", icpName, ecpName); - } - return ""; - } - - private String buildIcp(String name, JsonObject icp) { + protected String buildIcp(String name, JsonObject icp) { if (icp.has(REQUIREMENTS)) { JsonArray requirements = icp.get(REQUIREMENTS).getAsJsonArray(); String vdu = getRequirement(requirements, "virtual_binding"); @@ -237,7 +149,7 @@ public class OnapR2VnfdBuilder { return ""; } - private String buildVduCpd(String name, String vdu, JsonObject properties) { + protected String buildVduCpd(String name, String vdu, JsonObject properties) { return indent(name + ":\n" + " type: tosca.nodes.nfv.VduCp\n" + " " + PROPERTIES + ":\n" + @@ -250,21 +162,22 @@ public class OnapR2VnfdBuilder { " - virtual_binding: " + vdu + "\n", 2); } - private String buildVolume(String nodeName, JsonObject volume) { + protected String buildVolume(String nodeName, JsonObject volume) { return indent(nodeName + ":\n" + " type: tosca.nodes.nfv.Vdu.VirtualStorage\n" + " properties:\n" + " type_of_storage: volume\n" + - " size_of_storage: " + trimUnit(childElement(child(volume, PROPERTIES), "size_of_storage").getAsString()) + "\n", 2); + " size_of_storage: " + childElement(child(volume, PROPERTIES), "size_of_storage").getAsString() + "\n", 2); } - private String buildVl(JsonObject vlProperties, String name) { + protected String buildVl(JsonObject vlProperties, String name) { + JsonObject connectivityType = child(vlProperties, "connectivity_type"); return indent(name + ":\n" + " type: tosca.nodes.nfv.VnfVirtualLink\n" + " properties:\n" + " connectivity_type:\n" + - " layer_protocol: [ " + childElement(child(vlProperties, "connectivity_type"), "layer_protocol").getAsString() + " ]\n" + - " flow_pattern: " + childElement(child(vlProperties, "connectivity_type"), "flow_pattern").getAsString() + "\n" + + " layer_protocol: [ " + childElement(connectivityType, "layer_protocol").getAsString() + " ]\n" + + (connectivityType.has("flow_pattern") ? " flow_pattern: " + childElement(connectivityType, "flow_pattern").getAsString() + "\n" : "") + " vl_profile:\n" + " max_bit_rate_requirements:\n" + " root: " + Integer.MAX_VALUE + "\n" + //FIXME GAP IN CBAM TEMPLATE 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..8de82347 --- /dev/null +++ b/nokiav2/driver/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/packagetransformer/OnapVnfPackageBuilder.java @@ -0,0 +1,84 @@ +/* + * 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.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(), SupportedOnapPackageVersions.V1); + systemFunctions().out().write(covert); + } + + /** + * @param zip the original CBAM package + * @param version + * @return the converted ONAP package + */ + public byte[] covert(InputStream zip, SupportedOnapPackageVersions version) throws IOException { + byte[] cbamVnfPackage = ByteStreams.toByteArray(zip); + String vnfdLocation = getVnfdLocation(new ByteArrayInputStream(cbamVnfPackage)); + ByteArrayOutputStream vnfdContent = getFileInZip(new ByteArrayInputStream(cbamVnfPackage), vnfdLocation); + byte[] cbamVnfdContent = vnfdContent.toByteArray(); + String onapVnfd = SupportedOnapPackageVersions.V2 == version ? + new OnapR2VnfdBuilder().toOnapVnfd(new String(cbamVnfdContent, StandardCharsets.UTF_8)) : + new OnapR1VnfdBuilder().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 IOException { + 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.closeEntry(); + out.putNextEntry(new ZipEntry("MainServiceTemplate.mf")); + out.write(systemFunctions().loadFile("MainServiceTemplate.mf")); + 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 deleted file mode 100644 index b5dd9616..00000000 --- a/nokiav2/driver/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/packagetransformer/OnapVnfdBuilder.java +++ /dev/null @@ -1,256 +0,0 @@ -/* - * 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.annotations.VisibleForTesting; -import com.google.gson.Gson; -import com.google.gson.JsonArray; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import java.util.Map; -import java.util.NoSuchElementException; -import java.util.Set; -import java.util.regex.Pattern; -import org.slf4j.Logger; -import org.yaml.snakeyaml.Yaml; - -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; -import static org.slf4j.LoggerFactory.getLogger; - -/** - * Transforms a CBAM package into an ONAP package - */ -public class OnapVnfdBuilder { - public static final String DESCRIPTION = "description"; - public static final String PROPERTIES = "properties"; - public static final String REQUIREMENTS = "requirements"; - private static Logger logger = getLogger(OnapVnfdBuilder.class); - - @VisibleForTesting - static String indent(String content, int prefixSize) { - StringBuilder sb = new StringBuilder(); - for (int i = 0; i < prefixSize; i++) { - sb.append(" "); - } - Pattern pattern = Pattern.compile("^(.*)$", Pattern.MULTILINE); - return pattern.matcher(content).replaceAll(sb.toString() + "$1"); - } - - private static String trimUnit(String data) { - //FIXME the unit should not be trimmed VF-C bug - return data.trim().replaceAll("[^0-9]", ""); - } - - public static String getRequirement(JsonArray requirements, String key) { - 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 (key.equals(s)) { - return next.getValue().getAsString(); - } - } - return null; - } - - /** - * @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(); - if ("tosca.nodes.nfv.VDU".equals(type)) { - body.append(buildVdu(node.getKey(), node.getValue().getAsJsonObject(), nodeTemplates)); - } else if ("tosca.nodes.nfv.VirtualStorage".equals(type)) { - body.append(buildVolume(node.getKey(), node.getValue().getAsJsonObject())); - } else if ("tosca.nodes.nfv.VL".equals(type)) { - body.append(buildVl(node.getKey())); - } else if ("tosca.nodes.nfv.ICP".equals(type)) { - body.append(buildIcp(node.getKey(), node.getValue().getAsJsonObject())); - } else if ("tosca.nodes.nfv.ECP".equals(type)) { - body.append(buildEcp(node.getKey(), node.getValue(), nodeTemplates)); - } else { - logger.warn("The {} type is not converted", type); - } - } - return buildHeader(topologyTemplate) + body.toString(); - } - return buildHeader(topologyTemplate); - } - - private String buildHeader(JsonObject toplogyTemplate) { - 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: " + 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: " + descriptorVersion + "\n\n" + - "topology_template:\n" + - " inputs:\n" + - " etsi_config:\n" + - " type: string\n" + - " description: The ETSI configuration\n" + - " node_templates:\n"; - } - - private JsonElement get(String name, Set> nodes) { - for (Map.Entry 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> 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 next = requirement.entrySet().iterator().next(); - 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 = trimUnit(childElement(child(child(virtualCompute, PROPERTIES), "virtual_memory"), "virtual_mem_size").getAsString()); - } else if ("virtual_storage".equals(s)) { - String item = indent( - "- virtual_storage:\n" + - " capability: tosca.capabilities.nfv.VirtualStorage\n" + - " node: " + next.getValue().getAsString() + "\n", 4); - body.append(item); - - } - next.getValue(); - } - String header = indent(name + ":\n" + - " type: tosca.nodes.nfv.VDU.Compute\n" + - " capabilities:\n" + - " virtual_compute:\n" + - indent( - "properties:\n" + - " virtual_memory:\n" + - " virtual_mem_size: " + trimUnit(memorySize) + "\n" + - " virtual_cpu:\n" + - " num_virtual_cpu: " + cpuCount + "\n", 3) + - " " + REQUIREMENTS + ":\n", 2); - return header + body.toString(); - } - - private String buildEcp(String name, JsonElement ecp, Set> nodes) { - if (ecp.getAsJsonObject().has(REQUIREMENTS)) { - String icpName = getRequirement(ecp.getAsJsonObject().get(REQUIREMENTS).getAsJsonArray(), "internal_connection_point"); - if (icpName != null) { - return buildEcpInternal(name, icpName, nodes); - } else { - logger.warn("The {} ecp does not have an internal connection point", name); - } - } else { - logger.warn("The {} ecp does not have an requirements section", name); - } - return ""; - } - - private String buildEcpInternal(String ecpName, String icpName, Set> nodes) { - JsonObject icpNode = get(icpName, nodes).getAsJsonObject(); - if (icpNode.has(REQUIREMENTS)) { - String vdu = getRequirement(icpNode.getAsJsonObject().get(REQUIREMENTS).getAsJsonArray(), "virtual_binding"); - //internal connection point is bound to VDU - if (vdu != null) { - return buildVduCpd(ecpName, vdu, child(icpNode, PROPERTIES)); - } else { - logger.warn("The {} internal connection point of the {} ecp does not have a VDU", icpName, ecpName); - } - } else { - logger.warn("The {} internal connection point of the {} ecp does not have a requirements section", icpName, ecpName); - } - return ""; - } - - private String buildIcp(String name, JsonObject icp) { - if (icp.has(REQUIREMENTS)) { - JsonArray requirements = icp.get(REQUIREMENTS).getAsJsonArray(); - String vdu = getRequirement(requirements, "virtual_binding"); - String vl = getRequirement(requirements, "virtual_link"); - if (vdu == null) { - logger.warn("The {} internal connection point does not have a VDU", name); - } else if (vl == null) { - logger.warn("The {} internal connection point does not have a VL", name); - } else { - JsonObject properties = child(icp, PROPERTIES); - return indent(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", 2); - } - } else { - logger.warn("The {} internal connection point does not have a requirements section", name); - } - return ""; - } - - private String buildVduCpd(String name, String vdu, JsonObject properties) { - return indent(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", 2); - } - - private String buildVolume(String nodeName, JsonObject volume) { - return indent(nodeName + ":\n" + - " type: tosca.nodes.nfv.VDU.VirtualStorage\n" + - " properties:\n" + - " id: " + nodeName + "\n" + - " type_of_storage: volume\n" + - " size_of_storage: " + trimUnit(childElement(child(volume, PROPERTIES), "size_of_storage").getAsString()) + "\n", 2); - } - - private String buildVl(String name) { - return indent(name + ":\n" + - " type: tosca.nodes.nfv.VnfVirtualLinkDesc\n" + - " properties:\n" + - " vl_flavours:\n" + - " flavours:\n" + - " flavourId: notUsed\n", 2); - } -} diff --git a/nokiav2/driver/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/restapi/ConverterApi.java b/nokiav2/driver/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/restapi/ConverterApi.java index 95438caa..758c8b3f 100644 --- a/nokiav2/driver/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/restapi/ConverterApi.java +++ b/nokiav2/driver/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/restapi/ConverterApi.java @@ -22,7 +22,7 @@ import java.io.IOException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.Part; -import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.packagetransformer.OnapR1VnfPackageBuilder; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.packagetransformer.OnapVnfPackageBuilder; import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.packagetransformer.SupportedOnapPackageVersions; import org.slf4j.Logger; import org.springframework.stereotype.Controller; @@ -47,7 +47,7 @@ import static org.springframework.web.bind.annotation.RequestMethod.POST; @RequestMapping(value = BASE_URL) public class ConverterApi { private static Logger logger = getLogger(ConverterApi.class); - private OnapR1VnfPackageBuilder vnfPackageConverter = new OnapR1VnfPackageBuilder(); + private OnapVnfPackageBuilder vnfPackageConverter = new OnapVnfPackageBuilder(); /** * Return the converted ONAP package diff --git a/nokiav2/driver/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/vnfm/LifecycleManager.java b/nokiav2/driver/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/vnfm/LifecycleManager.java index f64ce1e0..7c8e2318 100644 --- a/nokiav2/driver/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/vnfm/LifecycleManager.java +++ b/nokiav2/driver/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/vnfm/LifecycleManager.java @@ -334,6 +334,7 @@ public class LifecycleManager { return child(child(operation.getValue().getAsJsonObject(), "inputs"), "additional_parameters").entrySet(); } } + throw buildFatalFailure(logger, "Unable to find operation named " + operationName); } diff --git a/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/FullUnitTestSuite.java b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/FullUnitTestSuite.java index 69e3482c..b37d95ff 100644 --- a/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/FullUnitTestSuite.java +++ b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/FullUnitTestSuite.java @@ -28,10 +28,7 @@ import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.direct.TestSdcPackageProvi import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.direct.notification.*; import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.so.TestSoLifecycleManager; import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.vfc.*; -import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.packagetransformer.TestCbamVnfPackageBuilder; -import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.packagetransformer.TestCbamVnfdBuilder; -import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.packagetransformer.TestOnapVnfPackageBuilder; -import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.packagetransformer.TestOnapVnfdBuilder; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.packagetransformer.*; import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.restapi.*; import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.spring.TestRealConfig; import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.spring.TestServletInitializer; @@ -70,7 +67,8 @@ import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.notification.TestReportedA TestVfcNotificationSender.class, TestCbamVnfdBuilder.class, - TestOnapVnfdBuilder.class, + TestOnapR1VnfdBuilder.class, + TestOnapR2VnfdBuilder.class, TestCbamVnfPackageBuilder.class, TestOnapVnfPackageBuilder.class, diff --git a/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/packagetransformer/TestOnapAbstractVnfdBuilder.java b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/packagetransformer/TestOnapAbstractVnfdBuilder.java new file mode 100644 index 00000000..0d2c348c --- /dev/null +++ b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/packagetransformer/TestOnapAbstractVnfdBuilder.java @@ -0,0 +1,33 @@ +/* + * 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 org.junit.Test; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.TestBase; + +import static junit.framework.TestCase.assertEquals; + +public class TestOnapAbstractVnfdBuilder extends TestBase { + + @Test + public void indent() { + assertEquals(" x", OnapAbstractVnfdBuilder.indent("x", 2)); + assertEquals(" x\n", OnapAbstractVnfdBuilder.indent("x\n", 2)); + assertEquals(" x\n y", OnapAbstractVnfdBuilder.indent("x\ny", 2)); + assertEquals(" x\n y\n", OnapAbstractVnfdBuilder.indent("x\ny\n", 2)); + assertEquals(" \n", OnapAbstractVnfdBuilder.indent("\n", 2)); + } +} diff --git a/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/packagetransformer/TestOnapR1VnfdBuilder.java b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/packagetransformer/TestOnapR1VnfdBuilder.java new file mode 100644 index 00000000..8c0e8a46 --- /dev/null +++ b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/packagetransformer/TestOnapR1VnfdBuilder.java @@ -0,0 +1,77 @@ +/* + * 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 java.util.NoSuchElementException; +import org.junit.Before; +import org.junit.Test; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.TestUtil; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.TestBase; + +import static junit.framework.TestCase.assertEquals; +import static junit.framework.TestCase.fail; +import static org.mockito.Mockito.verify; +import static org.springframework.test.util.ReflectionTestUtils.setField; + + +public class TestOnapR1VnfdBuilder extends TestBase { + private OnapR1VnfdBuilder packageTransformer = new OnapR1VnfdBuilder(); + + + @Before + public void init() { + setField(OnapAbstractVnfdBuilder.class, "logger", logger); + setField(OnapR1VnfdBuilder.class, "logger", logger); + } + + /** + * Test empty VNFD conversion + */ + @Test + public void testEmpty() { + assertEquals(new String(TestUtil.loadFile("unittests/packageconverter/empty.vnfd.onap.yaml")), packageTransformer.toOnapVnfd(new String(TestUtil.loadFile("unittests/packageconverter/empty.vnfd.cbam.yaml")))); + } + + /** + * Test all Tosca nodes conversions for successful scenario + */ + @Test + public void testNodes() { + assertEquals(new String(TestUtil.loadFile("unittests/packageconverter/nodes.vnfd.onap.yaml")), packageTransformer.toOnapVnfd(new String(TestUtil.loadFile("unittests/packageconverter/nodes.vnfd.cbam.yaml")))); + verify(logger).warn("The {} ecp does not have an internal connection point", "myEcpWithoutIcp"); + verify(logger).warn("The {} ecp does not have an requirements section", "ecpWithIcpWithOutRequirements"); + verify(logger).warn("The {} internal connection point of the {} ecp does not have a VDU", "icpWithoutVdu", "myEcpWithoutIcpWithoutVdu"); + verify(logger).warn("The {} internal connection point of the {} ecp does not have a requirements section", "icpWithOutRequiements", "myEcpWithoutIcpWithoutIcpReq"); + verify(logger).warn("The {} internal connection point does not have a VDU", "icpWithOutVdu"); + verify(logger).warn("The {} internal connection point does not have a requirements section", "icpWithOutRequiements"); + verify(logger).warn("The {} internal connection point does not have a VL", "icpWithOutVl"); + verify(logger).warn("The {} type is not converted", "tosca.nodes.nfv.Unknown"); + } + + /** + * if a node refers to a non existing node it results in a failure + */ + @Test + public void testInconsistentVnfd() { + try { + packageTransformer.toOnapVnfd(new String(TestUtil.loadFile("unittests/packageconverter/nodes.vnfd.inconsistent.cbam.yaml"))); + fail(); + } catch (NoSuchElementException e) { + assertEquals("The VNFD does not have a node called myComputeMissing but required by an other node", e.getMessage()); + } + } + +} diff --git a/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/packagetransformer/TestOnapR2VnfdBuilder.java b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/packagetransformer/TestOnapR2VnfdBuilder.java index 6916d382..a8f6a645 100644 --- a/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/packagetransformer/TestOnapR2VnfdBuilder.java +++ b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/packagetransformer/TestOnapR2VnfdBuilder.java @@ -23,6 +23,7 @@ import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.TestBase; import static junit.framework.TestCase.assertEquals; import static junit.framework.TestCase.fail; +import static org.mockito.Mockito.verify; import static org.springframework.test.util.ReflectionTestUtils.setField; @@ -32,7 +33,8 @@ public class TestOnapR2VnfdBuilder extends TestBase { @Before public void init() { - setField(OnapVnfdBuilder.class, "logger", logger); + setField(OnapAbstractVnfdBuilder.class, "logger", logger); + setField(OnapR2VnfdBuilder.class, "logger", logger); } @@ -50,14 +52,14 @@ public class TestOnapR2VnfdBuilder extends TestBase { @Test public void testNodes() { assertEquals(new String(TestUtil.loadFile("unittests/packageconverter/nodes.vnfd.onap.v2.yaml")), packageTransformer.toOnapVnfd(new String(TestUtil.loadFile("unittests/packageconverter/nodes.vnfd.cbam.yaml")))); - /* verify(logger).warn("The {} ecp does not have an internal connection point", "myEcpWithoutIcp"); + verify(logger).warn("The {} ecp does not have an internal connection point", "myEcpWithoutIcp"); verify(logger).warn("The {} ecp does not have an requirements section", "ecpWithIcpWithOutRequirements"); verify(logger).warn("The {} internal connection point of the {} ecp does not have a VDU", "icpWithoutVdu", "myEcpWithoutIcpWithoutVdu"); verify(logger).warn("The {} internal connection point of the {} ecp does not have a requirements section", "icpWithOutRequiements", "myEcpWithoutIcpWithoutIcpReq"); verify(logger).warn("The {} internal connection point does not have a VDU", "icpWithOutVdu"); verify(logger).warn("The {} internal connection point does not have a requirements section", "icpWithOutRequiements"); verify(logger).warn("The {} internal connection point does not have a VL", "icpWithOutVl"); - verify(logger).warn("The {} type is not converted", "tosca.nodes.nfv.Unknown")*/ + verify(logger).warn("The {} type is not converted", "tosca.nodes.nfv.Unknown"); } /** diff --git a/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/packagetransformer/TestOnapVnfPackageBuilder.java b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/packagetransformer/TestOnapVnfPackageBuilder.java index 55949ab0..d2a92556 100644 --- a/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/packagetransformer/TestOnapVnfPackageBuilder.java +++ b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/packagetransformer/TestOnapVnfPackageBuilder.java @@ -46,10 +46,10 @@ public class TestOnapVnfPackageBuilder extends TestBase { when(systemFunctions.loadFile("MainServiceTemplate.mf")).thenCallRealMethod(); String cbamVnfd = new String(TestUtil.loadFile("unittests/packageconverter/cbam.package.zip.vnfd")); - String expectedOnapVnfd = new OnapVnfdBuilder().toOnapVnfd(cbamVnfd); + String expectedOnapVnfd = new OnapR1VnfdBuilder().toOnapVnfd(cbamVnfd); //when - OnapR1VnfPackageBuilder.main(null); + OnapVnfPackageBuilder.main(null); //verify assertFileInZip(bos.toByteArray(), "TOSCA-Metadata/TOSCA.meta", TestUtil.loadFile("TOSCA.meta")); assertFileInZip(bos.toByteArray(), "MainServiceTemplate.yaml", expectedOnapVnfd.getBytes()); @@ -59,13 +59,60 @@ public class TestOnapVnfPackageBuilder extends TestBase { assertItenticalZips(expectedModifiedCbamPackage, actualModifiedCbamVnfPackage.toByteArray()); } + /** + * Test conversion for V1 package + */ + @Test + public void testConversionViaV1() throws Exception { + when(systemFunctions.loadFile("cbam.pre.collectConnectionPoints.js")).thenCallRealMethod(); + when(systemFunctions.loadFile("cbam.collectConnectionPoints.js")).thenCallRealMethod(); + when(systemFunctions.loadFile("cbam.post.collectConnectionPoints.js")).thenCallRealMethod(); + when(systemFunctions.loadFile("TOSCA.meta")).thenCallRealMethod(); + when(systemFunctions.loadFile("MainServiceTemplate.mf")).thenCallRealMethod(); + + String cbamVnfd = new String(TestUtil.loadFile("unittests/packageconverter/cbam.package.zip.vnfd")); + String expectedOnapVnfd = new OnapR1VnfdBuilder().toOnapVnfd(cbamVnfd); + //when + byte[] convertedPackage = new OnapVnfPackageBuilder().covert(new ByteArrayInputStream(TestUtil.loadFile("unittests/packageconverter/cbam.package.zip")), SupportedOnapPackageVersions.V1); + //verify + assertFileInZip(convertedPackage, "TOSCA-Metadata/TOSCA.meta", TestUtil.loadFile("TOSCA.meta")); + assertFileInZip(convertedPackage, "MainServiceTemplate.yaml", expectedOnapVnfd.getBytes()); + assertFileInZip(convertedPackage, "MainServiceTemplate.mf", TestUtil.loadFile("MainServiceTemplate.mf")); + ByteArrayOutputStream actualModifiedCbamVnfPackage = getFileInZip(new ByteArrayInputStream(convertedPackage), "Artifacts/Deployment/OTHER/cbam.package.zip"); + byte[] expectedModifiedCbamPackage = new CbamVnfPackageBuilder().toModifiedCbamVnfPackage(TestUtil.loadFile("unittests/packageconverter/cbam.package.zip"), "vnfdloc/a.yaml", new CbamVnfdBuilder().build(cbamVnfd)); + assertItenticalZips(expectedModifiedCbamPackage, actualModifiedCbamVnfPackage.toByteArray()); + } + + /** + * Test conversion for V2 package + */ + @Test + public void testConversionViaV2() throws Exception { + when(systemFunctions.loadFile("cbam.pre.collectConnectionPoints.js")).thenCallRealMethod(); + when(systemFunctions.loadFile("cbam.collectConnectionPoints.js")).thenCallRealMethod(); + when(systemFunctions.loadFile("cbam.post.collectConnectionPoints.js")).thenCallRealMethod(); + when(systemFunctions.loadFile("TOSCA.meta")).thenCallRealMethod(); + when(systemFunctions.loadFile("MainServiceTemplate.mf")).thenCallRealMethod(); + + String cbamVnfd = new String(TestUtil.loadFile("unittests/packageconverter/cbam.package.zip.vnfd")); + String expectedOnapVnfd = new OnapR2VnfdBuilder().toOnapVnfd(cbamVnfd); + //when + byte[] convertedPackage = new OnapVnfPackageBuilder().covert(new ByteArrayInputStream(TestUtil.loadFile("unittests/packageconverter/cbam.package.zip")), SupportedOnapPackageVersions.V2); + //verify + assertFileInZip(convertedPackage, "TOSCA-Metadata/TOSCA.meta", TestUtil.loadFile("TOSCA.meta")); + assertFileInZip(convertedPackage, "MainServiceTemplate.yaml", expectedOnapVnfd.getBytes()); + assertFileInZip(convertedPackage, "MainServiceTemplate.mf", TestUtil.loadFile("MainServiceTemplate.mf")); + ByteArrayOutputStream actualModifiedCbamVnfPackage = getFileInZip(new ByteArrayInputStream(convertedPackage), "Artifacts/Deployment/OTHER/cbam.package.zip"); + byte[] expectedModifiedCbamPackage = new CbamVnfPackageBuilder().toModifiedCbamVnfPackage(TestUtil.loadFile("unittests/packageconverter/cbam.package.zip"), "vnfdloc/a.yaml", new CbamVnfdBuilder().build(cbamVnfd)); + assertItenticalZips(expectedModifiedCbamPackage, actualModifiedCbamVnfPackage.toByteArray()); + } /** * Prevents moving the class (customer documentation) must be updated */ @Test public void testPreventMove() { - assertEquals("b3JnLm9uYXAudmZjLm5mdm8uZHJpdmVyLnZuZm0uc3ZuZm0ubm9raWEucGFja2FnZXRyYW5zZm9ybWVyLk9uYXBSMVZuZlBhY2thZ2VCdWlsZGVy", Base64.getEncoder().encodeToString(OnapR1VnfPackageBuilder.class.getCanonicalName().getBytes())); + assertEquals("b3JnLm9uYXAudmZjLm5mdm8uZHJpdmVyLnZuZm0uc3ZuZm0ubm9raWEucGFja2FnZXRyYW5zZm9ybWVyLk9uYXBWbmZQYWNrYWdlQnVpbGRlcg==", Base64.getEncoder().encodeToString(OnapVnfPackageBuilder.class.getCanonicalName().getBytes())); } diff --git a/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/packagetransformer/TestOnapVnfdBuilder.java b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/packagetransformer/TestOnapVnfdBuilder.java deleted file mode 100644 index a97ee4f1..00000000 --- a/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/packagetransformer/TestOnapVnfdBuilder.java +++ /dev/null @@ -1,85 +0,0 @@ -/* - * 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 java.util.NoSuchElementException; -import org.junit.Before; -import org.junit.Test; -import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.TestUtil; -import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.TestBase; - -import static junit.framework.TestCase.assertEquals; -import static junit.framework.TestCase.fail; -import static org.mockito.Mockito.verify; -import static org.springframework.test.util.ReflectionTestUtils.setField; - - -public class TestOnapVnfdBuilder extends TestBase { - private OnapVnfdBuilder packageTransformer = new OnapVnfdBuilder(); - - - @Before - public void init() { - setField(OnapVnfdBuilder.class, "logger", logger); - } - - @Test - public void indent() { - assertEquals(" x", packageTransformer.indent("x", 2)); - assertEquals(" x\n", packageTransformer.indent("x\n", 2)); - assertEquals(" x\n y", packageTransformer.indent("x\ny", 2)); - assertEquals(" x\n y\n", packageTransformer.indent("x\ny\n", 2)); - assertEquals(" \n", packageTransformer.indent("\n", 2)); - } - - /** - * Test empty VNFD conversion - */ - @Test - public void testEmpty() { - assertEquals(new String(TestUtil.loadFile("unittests/packageconverter/empty.vnfd.onap.yaml")), packageTransformer.toOnapVnfd(new String(TestUtil.loadFile("unittests/packageconverter/empty.vnfd.cbam.yaml")))); - } - - /** - * Test all Tosca nodes conversions for successful scenario - */ - @Test - public void testNodes() { - assertEquals(new String(TestUtil.loadFile("unittests/packageconverter/nodes.vnfd.onap.yaml")), packageTransformer.toOnapVnfd(new String(TestUtil.loadFile("unittests/packageconverter/nodes.vnfd.cbam.yaml")))); - verify(logger).warn("The {} ecp does not have an internal connection point", "myEcpWithoutIcp"); - verify(logger).warn("The {} ecp does not have an requirements section", "ecpWithIcpWithOutRequirements"); - verify(logger).warn("The {} internal connection point of the {} ecp does not have a VDU", "icpWithoutVdu", "myEcpWithoutIcpWithoutVdu"); - verify(logger).warn("The {} internal connection point of the {} ecp does not have a requirements section", "icpWithOutRequiements", "myEcpWithoutIcpWithoutIcpReq"); - verify(logger).warn("The {} internal connection point does not have a VDU", "icpWithOutVdu"); - verify(logger).warn("The {} internal connection point does not have a requirements section", "icpWithOutRequiements"); - verify(logger).warn("The {} internal connection point does not have a VL", "icpWithOutVl"); - verify(logger).warn("The {} type is not converted", "tosca.nodes.nfv.Unknown"); - } - - /** - * if a node refers to a non existing node it results in a failure - */ - @Test - public void testInconsitentVnfd() { - try { - packageTransformer.toOnapVnfd(new String(TestUtil.loadFile("unittests/packageconverter/nodes.vnfd.inconsistent.cbam.yaml"))); - fail(); - } catch (NoSuchElementException e) { - assertEquals("The VNFD does not have a node called myComputeMissing but required by an other node", e.getMessage()); - } - } - -} diff --git a/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/restapi/TestConverterApi.java b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/restapi/TestConverterApi.java index 48d2cab0..99fd203a 100644 --- a/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/restapi/TestConverterApi.java +++ b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/restapi/TestConverterApi.java @@ -31,7 +31,7 @@ import org.mockito.Mock; import org.mockito.Mockito; import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.packagetransformer.CbamVnfPackageBuilder; import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.packagetransformer.CbamVnfdBuilder; -import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.packagetransformer.OnapVnfdBuilder; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.packagetransformer.OnapR1VnfdBuilder; import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.TestUtil; import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.TestBase; import org.springframework.http.HttpHeaders; @@ -88,7 +88,7 @@ public class TestConverterApi extends TestBase { private void verifyVnfPackageWritterToOutputStream(ByteArrayOutputStream bos) throws Exception { String cbamVnfd = new String(TestUtil.loadFile("unittests/packageconverter/cbam.package.zip.vnfd")); - String expectedOnapVnfd = new OnapVnfdBuilder().toOnapVnfd(cbamVnfd); + String expectedOnapVnfd = new OnapR1VnfdBuilder().toOnapVnfd(cbamVnfd); assertFileInZip(bos.toByteArray(), "TOSCA-Metadata/TOSCA.meta", TestUtil.loadFile("TOSCA.meta")); assertFileInZip(bos.toByteArray(), "MainServiceTemplate.yaml", expectedOnapVnfd.getBytes()); assertFileInZip(bos.toByteArray(), "MainServiceTemplate.mf", TestUtil.loadFile("MainServiceTemplate.mf")); @@ -133,6 +133,27 @@ public class TestConverterApi extends TestBase { } } + /** + * error is propagated if unable to extract version from HTTP request + */ + @Test + public void testUnableToExtractVersion() throws Exception { + IOException expectedException = new IOException(); + Part part = Mockito.mock(Part.class); + when(part.getInputStream()).thenReturn(new ByteArrayInputStream(TestUtil.loadFile("unittests/packageconverter/cbam.package.zip"))); + when(httpRequest.getPart("fileToUpload")).thenReturn(part); + when(httpRequest.getPart("version")).thenThrow(expectedException); + when(part.getInputStream()).thenReturn(new ByteArrayInputStream("V1".getBytes())); + try { + converterApi.convert(httpResponse, httpRequest); + fail(); + } catch (Exception e) { + verify(logger).error("Unable to determine the desired ONAP package version", expectedException); + assertEquals("Unable to determine the desired ONAP package version", e.getMessage()); + assertEquals(expectedException, e.getCause()); + } + } + /** * error is propagated if unable to extract package from HTTP request */ diff --git a/nokiav2/driver/src/test/resources/application-direct.properties b/nokiav2/driver/src/test/resources/application-direct.properties index 5702c913..b9c1c68c 100644 --- a/nokiav2/driver/src/test/resources/application-direct.properties +++ b/nokiav2/driver/src/test/resources/application-direct.properties @@ -13,9 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. ############################################################################### - server.error.whitelabel.enabled=false - ############################################################################### # Beginning of mandatory properties # ############################################################################### @@ -42,7 +40,6 @@ skipCertificateVerification=true trustedCertificates= # the identifier of the VNFM in A&AI core system registry vnfmId=7c267318-2a6a-4d47-b039-a7cce5fea38b - ############################################################################### # Configuration parameters for direct integration ############################################################################### @@ -61,7 +58,6 @@ aaiPassword=AAI sdcUsername=SDC #The password to access SDC sdcPassword=SDC - ############################################################################### # End of mandatory properties for driver # ############################################################################### diff --git a/nokiav2/driver/src/test/resources/application.properties b/nokiav2/driver/src/test/resources/application.properties index af64fd95..d2732910 100644 --- a/nokiav2/driver/src/test/resources/application.properties +++ b/nokiav2/driver/src/test/resources/application.properties @@ -13,9 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. ############################################################################### - server.error.whitelabel.enabled=false - ############################################################################### # Beginning of mandatory properties # ############################################################################### @@ -42,7 +40,6 @@ skipCertificateVerification=true trustedCertificates= # the identifier of the VNFM in A&AI core system registry vnfmId=53fbba58-464e-4cc4-8d33-aaaf072f0a27 - ############################################################################### # Configuration parameters for direct integration ############################################################################### @@ -61,7 +58,6 @@ aaiPassword=AAI sdcUsername=SDC #The password to access SDC sdcPassword=SDC - ############################################################################### # End of mandatory properties for driver # ############################################################################### diff --git a/nokiav2/driver/src/test/resources/unittests/packageconverter/cbam.package.zip b/nokiav2/driver/src/test/resources/unittests/packageconverter/cbam.package.zip index ac555dff..fe996956 100644 Binary files a/nokiav2/driver/src/test/resources/unittests/packageconverter/cbam.package.zip and b/nokiav2/driver/src/test/resources/unittests/packageconverter/cbam.package.zip differ diff --git a/nokiav2/driver/src/test/resources/unittests/packageconverter/cbam.package.zip.vnfd b/nokiav2/driver/src/test/resources/unittests/packageconverter/cbam.package.zip.vnfd index 3c121386..af4a530a 100644 --- a/nokiav2/driver/src/test/resources/unittests/packageconverter/cbam.package.zip.vnfd +++ b/nokiav2/driver/src/test/resources/unittests/packageconverter/cbam.package.zip.vnfd @@ -15,3 +15,8 @@ topology_template: Basic: instantiate: terminate: + capabilities: + deployment_flavour: + properties: + flavour_id: myFlavorId + description: myFlavorDescription diff --git a/nokiav2/driver/src/test/resources/unittests/packageconverter/nodes.vnfd.cbam.yaml b/nokiav2/driver/src/test/resources/unittests/packageconverter/nodes.vnfd.cbam.yaml index 3870605f..c893b930 100644 --- a/nokiav2/driver/src/test/resources/unittests/packageconverter/nodes.vnfd.cbam.yaml +++ b/nokiav2/driver/src/test/resources/unittests/packageconverter/nodes.vnfd.cbam.yaml @@ -67,7 +67,6 @@ topology_template: properties: connectivity_type: layer_protocol: ipv4 - flow_pattern: mesh description: myDescription icpWithOutEcp: diff --git a/nokiav2/driver/src/test/resources/unittests/packageconverter/nodes.vnfd.onap.v2.yaml b/nokiav2/driver/src/test/resources/unittests/packageconverter/nodes.vnfd.onap.v2.yaml index fe0ccf4c..ba1c8fa8 100644 --- a/nokiav2/driver/src/test/resources/unittests/packageconverter/nodes.vnfd.onap.v2.yaml +++ b/nokiav2/driver/src/test/resources/unittests/packageconverter/nodes.vnfd.onap.v2.yaml @@ -77,7 +77,6 @@ topology_template: properties: connectivity_type: layer_protocol: [ ipv4 ] - flow_pattern: mesh vl_profile: max_bit_rate_requirements: root: 2147483647 -- cgit 1.2.3-korg