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/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/vnfm | |
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/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/vnfm')
12 files changed, 3739 insertions, 0 deletions
diff --git a/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/vnfm/TestAdditionalParams.java b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/vnfm/TestAdditionalParams.java new file mode 100644 index 00000000..e0e90000 --- /dev/null +++ b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/vnfm/TestAdditionalParams.java @@ -0,0 +1,61 @@ +/* + * 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.vnfm; + +import com.google.common.collect.Maps; +import com.nokia.cbam.lcm.v32.model.NetworkAddress; +import org.junit.Test; +import pl.pojo.tester.internal.field.AbstractFieldValueChanger; +import pl.pojo.tester.internal.field.DefaultFieldValueChanger; +import pl.pojo.tester.internal.field.collections.map.AbstractMapFieldValueChanger; + +import java.util.List; +import java.util.Map; +import java.util.UUID; + +import static pl.pojo.tester.api.assertion.Assertions.assertPojoMethodsFor; + + +public class TestAdditionalParams { + + /** + * Test basic POJO behaviour + */ + @Test + public void test() { + + class MapValueChanger extends AbstractMapFieldValueChanger<Map<String, List<NetworkAddress>>> { + @Override + protected Map<String, List<NetworkAddress>> increaseValue(Map<String, List<NetworkAddress>> stringListMap, Class<?> aClass) { + if (stringListMap == null) { + return Maps.newHashMap(); + } else { + stringListMap.put(UUID.randomUUID().toString(), null); + return stringListMap; + } + } + + @Override + protected boolean canChange(Class<?> type) { + return type.getCanonicalName().contains("Map"); + } + } + + final AbstractFieldValueChanger valueChanger = new MapValueChanger().attachNext(DefaultFieldValueChanger.INSTANCE); + assertPojoMethodsFor(AdditionalParams.class).using(valueChanger).areWellImplemented(); + } +} diff --git a/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/vnfm/TestBase.java b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/vnfm/TestBase.java new file mode 100644 index 00000000..be3ea0eb --- /dev/null +++ b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/vnfm/TestBase.java @@ -0,0 +1,174 @@ +/* + * 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.vnfm; + +import com.google.common.io.ByteStreams; +import com.nokia.cbam.catalog.v1.api.DefaultApi; +import com.nokia.cbam.lcm.v32.api.OperationExecutionsApi; +import com.nokia.cbam.lcm.v32.api.VnfsApi; +import com.nokia.cbam.lcn.v32.api.SubscriptionsApi; +import org.apache.commons.lang3.ArrayUtils; +import org.apache.http.HttpEntity; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpUriRequest; +import org.apache.http.impl.client.CloseableHttpClient; +import org.assertj.core.util.Lists; +import org.junit.After; +import org.junit.Before; +import org.mockito.ArgumentCaptor; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.onap.msb.sdk.httpclient.msb.MSBServiceClient; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.api.INotificationSender; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.api.VnfmInfoProvider; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core.MsbApiProvider; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core.SelfRegistrationManager; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.vfc.VfcRestApiProvider; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.SystemFunctions; +import org.onap.vfccatalog.api.VnfpackageApi; +import org.onap.vnfmdriver.api.NslcmApi; +import org.slf4j.Logger; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.core.env.Environment; +import org.springframework.test.util.ReflectionTestUtils; + +import javax.servlet.http.HttpServletResponse; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.lang.reflect.Field; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; + +import static junit.framework.TestCase.assertEquals; +import static junit.framework.TestCase.assertTrue; +import static org.mockito.Mockito.when; +import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CatalogManager.getFileInZip; + +public class TestBase { + public static final String VNF_ID = "myVnfId"; + public static final String VNFM_ID = "myVnfmId"; + public static final String ONAP_CSAR_ID = "myOnapCsarId"; + public static final String VIM_ID = "myVimId"; + public static final String JOB_ID = "myJobId"; + public static final String CBAM_VNFD_ID = "cbamVnfdId"; + @Mock + protected CbamRestApiProvider cbamRestApiProvider; + @Mock + protected VfcRestApiProvider vfcRestApiProvider; + @Mock + protected MsbApiProvider msbApiProvider; + @Mock + protected VnfmInfoProvider vnfmInfoProvider; + @Mock + protected VnfsApi vnfApi; + @Mock + protected OperationExecutionsApi operationExecutionApi; + @Mock + protected SelfRegistrationManager selfRegistrationManager; + @Mock + protected Logger logger; + @Mock + protected SubscriptionsApi lcnApi; + @Mock + protected MSBServiceClient msbClient; + @Mock + protected DriverProperties driverProperties; + @Mock + protected NslcmApi nsLcmApi; + @Mock + protected INotificationSender notificationSender; + @Mock + protected SystemFunctions systemFunctions; + @Mock + protected VnfpackageApi vfcCatalogApi; + @Mock + protected DefaultApi cbamCatalogApi; + @Mock + protected CloseableHttpClient httpClient; + @Mock + protected CloseableHttpResponse response; + protected ArgumentCaptor<HttpUriRequest> request = ArgumentCaptor.forClass(HttpUriRequest.class); + @Mock + protected HttpEntity entity; + @Mock + protected HttpServletResponse httpResponse; + @Mock + protected Environment environment; + + @Before + public void genericSetup() throws Exception { + MockitoAnnotations.initMocks(this); + ReflectionTestUtils.setField(SystemFunctions.class, "INSTANCE", systemFunctions); + when(cbamRestApiProvider.getCbamLcmApi(VNFM_ID)).thenReturn(vnfApi); + when(cbamRestApiProvider.getCbamOperationExecutionApi(VNFM_ID)).thenReturn(operationExecutionApi); + when(cbamRestApiProvider.getCbamLcnApi(VNFM_ID)).thenReturn(lcnApi); + when(cbamRestApiProvider.getCbamCatalogApi(VNFM_ID)).thenReturn(cbamCatalogApi); + when(msbApiProvider.getMsbClient()).thenReturn(msbClient); + when(vfcRestApiProvider.getNsLcmApi()).thenReturn(nsLcmApi); + when(vfcRestApiProvider.getOnapCatalogApi()).thenReturn(vfcCatalogApi); + when(systemFunctions.getHttpClient()).thenReturn(httpClient); + when(httpClient.execute(request.capture())).thenReturn(response); + when(response.getEntity()).thenReturn(entity); + when(driverProperties.getVnfmId()).thenReturn(VNFM_ID); + when(systemFunctions.getHttpClient()).thenReturn(httpClient); + } + + @After + public void tearGeneric() { + ReflectionTestUtils.setField(SystemFunctions.class, "INSTANCE", null); + } + + protected void assertFileInZip(byte[] zip, String path, byte[] expectedContent) throws Exception { + assertTrue(Arrays.equals(expectedContent, getFileInZip(new ByteArrayInputStream(zip), path).toByteArray())); + } + + protected void assertItenticalZips(byte[] expected, byte[] actual) throws Exception { + assertEquals(build(expected), build(actual)); + } + + private Map<String, List<Byte>> build(byte[] zip) throws Exception { + Map<String, List<Byte>> files = new HashMap<>(); + ZipInputStream zipInputStream = new ZipInputStream(new ByteArrayInputStream(zip)); + ZipEntry zipEntry; + while ((zipEntry = zipInputStream.getNextEntry()) != null) { + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + ByteStreams.copy(zipInputStream, byteArrayOutputStream); + files.put(zipEntry.getName(), Lists.newArrayList(ArrayUtils.toObject(byteArrayOutputStream.toByteArray()))); + } + zipInputStream.close(); + return files; + } + + protected void setFieldWithPropertyAnnotation(Object obj, String key, String value) { + for (Field field : obj.getClass().getDeclaredFields()) { + for (Value fieldValue : field.getAnnotationsByType(Value.class)) { + if (fieldValue.value().equals(key)) { + try { + field.setAccessible(true); + field.set(obj, value); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } + } + } + } + } +} diff --git a/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/vnfm/TestCbamCatalogManager.java b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/vnfm/TestCbamCatalogManager.java new file mode 100644 index 00000000..f38758f0 --- /dev/null +++ b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/vnfm/TestCbamCatalogManager.java @@ -0,0 +1,261 @@ +/* + * 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.vnfm; + +import com.nokia.cbam.catalog.v1.model.CatalogAdapterVnfpackage; +import org.junit.Before; +import org.junit.Test; +import org.mockito.ArgumentCaptor; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.api.IPackageProvider; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.TestUtil; + +import java.io.ByteArrayInputStream; +import java.io.File; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.UUID; + +import static junit.framework.TestCase.*; +import static org.junit.Assert.assertArrayEquals; +import static org.mockito.Mockito.*; +import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CatalogManager.getFileInZip; +import static org.springframework.test.util.ReflectionTestUtils.setField; + +public class TestCbamCatalogManager extends TestBase { + + private static final String CSAR_ID = "csarId"; + private static final String CBAM_VNFD_ID = "CBAM_VNFD_ID"; + private CatalogManager cbamCatalogManager; + @Mock + private IPackageProvider packageProvider; + + private List<CatalogAdapterVnfpackage> existingVnfPackages = new ArrayList<>(); + private ArgumentCaptor<File> uploadedFile = ArgumentCaptor.forClass(File.class); + + @Before + public void initMocks() throws Exception { + setField(CatalogManager.class, "logger", logger); + when(cbamCatalogApi.list()).thenReturn(existingVnfPackages); + cbamCatalogManager = new CatalogManager(cbamRestApiProvider, packageProvider); + } + + /** + * the package is transferred from source to CBAM catalog + */ + @Test + public void testPackageTransfer() throws Exception { + CatalogAdapterVnfpackage existingPackage = new CatalogAdapterVnfpackage(); + existingPackage.setVnfdId("unknownId"); + existingVnfPackages.add(existingPackage); + CatalogAdapterVnfpackage createdPackage = new CatalogAdapterVnfpackage(); + createdPackage.setVnfdId(CBAM_VNFD_ID); + when(cbamCatalogApi.create(uploadedFile.capture())).thenAnswer(new Answer<CatalogAdapterVnfpackage>() { + @Override + public CatalogAdapterVnfpackage answer(InvocationOnMock invocationOnMock) throws Throwable { + return createdPackage; + } + }); + byte[] onapPackageContent = TestUtil.loadFile("unittests/TestCbamCatalogManager.sample.csar"); + when(packageProvider.getPackage(CSAR_ID)).thenReturn(onapPackageContent); + when(packageProvider.getCbamVnfdId(CSAR_ID)).thenReturn(CBAM_VNFD_ID); + //when + CatalogAdapterVnfpackage cbamPackage = cbamCatalogManager.preparePackageInCbam(VNFM_ID, CSAR_ID); + //verify + byte[] a2 = Files.readAllBytes(uploadedFile.getValue().toPath()); + assertArrayEquals(getFileInZip(new ByteArrayInputStream(onapPackageContent), "Artifacts/Deployment/OTHER/cbam.package.zip").toByteArray(), a2); + assertEquals(createdPackage, cbamPackage); + } + + /** + * the package is transfer fails, but the package has been uploaded (possibly by other thread / work flow) + * the transfer succeeds + */ + @Test + public void testPackageTransferConcurrency() throws Exception { + CatalogAdapterVnfpackage existingPackage = new CatalogAdapterVnfpackage(); + existingPackage.setVnfdId("unknownId"); + existingVnfPackages.add(existingPackage); + CatalogAdapterVnfpackage createdPackage = new CatalogAdapterVnfpackage(); + createdPackage.setVnfdId(CBAM_VNFD_ID); + byte[] onapPackageContent = TestUtil.loadFile("unittests/TestCbamCatalogManager.sample.csar"); + when(packageProvider.getPackage(CSAR_ID)).thenReturn(onapPackageContent); + when(packageProvider.getCbamVnfdId(CSAR_ID)).thenReturn(CBAM_VNFD_ID); + RuntimeException can_not_upload_package = new RuntimeException("Can not upload package"); + when(cbamCatalogApi.create(uploadedFile.capture())).thenAnswer(new Answer<CatalogAdapterVnfpackage>() { + @Override + public CatalogAdapterVnfpackage answer(InvocationOnMock invocationOnMock) throws Throwable { + //this is done by an other thread + existingVnfPackages.add(createdPackage); + when(cbamCatalogApi.getById(CBAM_VNFD_ID)).thenReturn(createdPackage); + throw can_not_upload_package; + } + }); + //when + CatalogAdapterVnfpackage cbamPackage = cbamCatalogManager.preparePackageInCbam(VNFM_ID, CSAR_ID); + //verify + //the correct portion of the package is extracted and uploaded to CBAM + byte[] expectedContentToUpload = getFileInZip(new ByteArrayInputStream(onapPackageContent), "Artifacts/Deployment/OTHER/cbam.package.zip").toByteArray(); + assertTrue(Arrays.equals(expectedContentToUpload, Files.readAllBytes(uploadedFile.getValue().toPath()))); + assertEquals(createdPackage, cbamPackage); + verify(logger).debug("Probably concurrent package uploads", can_not_upload_package); + } + + /** + * If the package already exists in CBAM catalog it is not re-uploaded + */ + @Test + public void testIdempotentPackageUpload() throws Exception { + CatalogAdapterVnfpackage createdPackage = new CatalogAdapterVnfpackage(); + createdPackage.setVnfdId(CBAM_VNFD_ID); + when(cbamCatalogApi.create(uploadedFile.capture())).thenAnswer(new Answer<CatalogAdapterVnfpackage>() { + @Override + public CatalogAdapterVnfpackage answer(InvocationOnMock invocationOnMock) throws Throwable { + return createdPackage; + } + }); + when(packageProvider.getCbamVnfdId(CSAR_ID)).thenReturn(CBAM_VNFD_ID); + CatalogAdapterVnfpackage existingPackage = new CatalogAdapterVnfpackage(); + existingPackage.setVnfdId(CBAM_VNFD_ID); + existingVnfPackages.add(existingPackage); + when(cbamCatalogApi.getById(CBAM_VNFD_ID)).thenReturn(existingPackage); + //when + CatalogAdapterVnfpackage cbamPackage = cbamCatalogManager.preparePackageInCbam(VNFM_ID, CSAR_ID); + //verify + verify(cbamCatalogApi, never()).create(Mockito.any()); + assertEquals(existingPackage, cbamPackage); + verify(packageProvider, never()).getPackage(CSAR_ID); + } + + /** + * failure to list package in CBAM results in error + */ + @Test + public void testFailureToListVnfPackagesInCbam() throws Exception { + when(packageProvider.getCbamVnfdId(CSAR_ID)).thenReturn(CBAM_VNFD_ID); + com.nokia.cbam.catalog.v1.ApiException expectedException = new com.nokia.cbam.catalog.v1.ApiException(); + when(cbamCatalogApi.list()).thenThrow(expectedException); + //when + try { + cbamCatalogManager.preparePackageInCbam(VNFM_ID, CSAR_ID); + fail(); + } catch (Exception e) { + verify(logger).error("Unable to determine if the VNF package has been replicated in CBAM", expectedException); + assertEquals(expectedException, e.getCause()); + } + } + + /** + * failure to query package from CBAM results in error + */ + @Test + public void testFailureToQueryVnfPackagesFromCbam() throws Exception { + when(packageProvider.getCbamVnfdId(CSAR_ID)).thenReturn(CBAM_VNFD_ID); + CatalogAdapterVnfpackage existingPackage = new CatalogAdapterVnfpackage(); + existingPackage.setVnfdId(CBAM_VNFD_ID); + existingVnfPackages.add(existingPackage); + com.nokia.cbam.catalog.v1.ApiException expectedException = new com.nokia.cbam.catalog.v1.ApiException(); + when(cbamCatalogApi.getById(CBAM_VNFD_ID)).thenThrow(expectedException); + //when + try { + cbamCatalogManager.preparePackageInCbam(VNFM_ID, CSAR_ID); + fail(); + } catch (Exception e) { + verify(logger).error("Unable to query VNF package with CBAM_VNFD_ID from CBAM", expectedException); + assertEquals(expectedException, e.getCause()); + } + } + + /** + * failure to create package in CBAM results in error + */ + @Test + public void testFailureToCreatePackageInCbam() throws Exception { + CatalogAdapterVnfpackage existingPackage = new CatalogAdapterVnfpackage(); + existingPackage.setVnfdId("unknownId"); + existingVnfPackages.add(existingPackage); + when(packageProvider.getCbamVnfdId(CSAR_ID)).thenReturn(CBAM_VNFD_ID); + byte[] onapPackageContent = TestUtil.loadFile("unittests/TestCbamCatalogManager.sample.csar"); + when(packageProvider.getPackage(CSAR_ID)).thenReturn(onapPackageContent); + com.nokia.cbam.catalog.v1.ApiException expectedException = new com.nokia.cbam.catalog.v1.ApiException(); + when(cbamCatalogApi.create(Mockito.any())).thenThrow(expectedException); + try { + cbamCatalogManager.preparePackageInCbam(VNFM_ID, CSAR_ID); + fail(); + } catch (Exception e) { + verify(logger).error("Unable to create VNF with csarId CSAR identifier in package in CBAM", expectedException); + assertEquals(expectedException, e.getCause()); + } + } + + /** + * the VNFD is extracted from zip + */ + @Test + public void testExtractVnfdFromPackage() throws Exception { + Path csar = Files.createTempFile(UUID.randomUUID().toString(), "csar"); + Files.write(csar, TestUtil.loadFile("unittests/cbam.package.zip")); + when(cbamCatalogApi.content(CBAM_VNFD_ID)).thenReturn(csar.toFile()); + //when + String content = cbamCatalogManager.getCbamVnfdContent(VNFM_ID, CBAM_VNFD_ID); + //verify + assertEquals("dummy vnfd\n", content); + } + + /** + * if VNFD the Tosca meta can not be extracted sensible error is returned + */ + @Test + public void testEmptyCbamPackage() throws Exception { + Path csar = Files.createTempFile(UUID.randomUUID().toString(), "csar"); + Files.write(csar, TestUtil.loadFile("unittests/empty.zip")); + when(cbamCatalogApi.content(CBAM_VNFD_ID)).thenReturn(csar.toFile()); + //when + try { + cbamCatalogManager.getCbamVnfdContent(VNFM_ID, CBAM_VNFD_ID); + fail(); + } catch (RuntimeException e) { + verify(logger).error("Unable to get package with (CBAM_VNFD_ID)", e.getCause()); + assertEquals("Unable to find the TOSCA-Metadata/TOSCA.meta in archive found: []", e.getCause().getMessage()); + } + } + + /** + * if VNFD can not be extracted sensible error is returned + */ + @Test + public void testMissingVnfdCbamPackage() throws Exception { + Path csar = Files.createTempFile(UUID.randomUUID().toString(), "csar"); + Files.write(csar, TestUtil.loadFile("unittests/missing.vnfd.zip")); + when(cbamCatalogApi.content(CBAM_VNFD_ID)).thenReturn(csar.toFile()); + //when + try { + cbamCatalogManager.getCbamVnfdContent(VNFM_ID, CBAM_VNFD_ID); + fail(); + } catch (RuntimeException e) { + verify(logger).error("Unable to get package with (" + CBAM_VNFD_ID + ")", e.getCause()); + assertTrue("Unable to find the vnfdloc/a.yaml in archive found: [TOSCA-Metadata/, TOSCA-Metadata/TOSCA.meta]".equals(e.getCause().getMessage()) + || "Unable to find the vnfdloc/a.yaml in archive found: [TOSCA-Metadata/TOSCA.meta, TOSCA-Metadata/]".equals(e.getCause().getMessage()) + ); + } + } +} diff --git a/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/vnfm/TestCbamTokenProvider.java b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/vnfm/TestCbamTokenProvider.java new file mode 100644 index 00000000..670aa77f --- /dev/null +++ b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/vnfm/TestCbamTokenProvider.java @@ -0,0 +1,438 @@ +/* + * 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.vnfm; + +import com.google.common.io.ByteStreams; +import org.eclipse.jetty.server.NetworkTrafficServerConnector; +import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.server.handler.AbstractHandler; +import org.eclipse.jetty.util.ssl.SslContextFactory; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.mockito.ArgumentCaptor; +import org.mockito.InjectMocks; +import org.mockito.Mockito; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.api.VnfmInfoProvider; +import org.onap.vnfmdriver.model.VnfmInfo; +import org.springframework.http.HttpStatus; + +import javax.net.ssl.*; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.net.URI; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.security.GeneralSecurityException; +import java.security.KeyStoreException; +import java.security.NoSuchAlgorithmException; +import java.security.NoSuchProviderException; +import java.security.cert.CertificateException; +import java.security.spec.InvalidKeySpecException; +import java.util.ArrayList; +import java.util.Base64; +import java.util.List; + +import static junit.framework.TestCase.*; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import static org.springframework.test.util.ReflectionTestUtils.setField; + +class HttpTestServer { + Server _server; + List<String> requests = new ArrayList<>(); + List<Integer> codes = new ArrayList<>(); + List<String> respones = new ArrayList<>(); + + public void start() throws Exception { + configureServer(); + startServer(); + } + + private void startServer() throws Exception { + requests.clear(); + codes.clear(); + _server.start(); + } + + protected void configureServer() throws Exception { + Path jksPath = Paths.get(TestCbamTokenProvider.class.getResource("/unittests/localhost.jks").toURI()); + String path = jksPath.normalize().toAbsolutePath().toUri().toString(); + _server = new Server(); + SslContextFactory factory = new SslContextFactory(path); + factory.setKeyStorePassword("changeit"); + NetworkTrafficServerConnector connector = new NetworkTrafficServerConnector(_server, factory); + connector.setHost("127.0.0.1"); + _server.addConnector(connector); + _server.setHandler(new AbstractHandler() { + @Override + public void handle(String target, org.eclipse.jetty.server.Request request, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException, ServletException { + requests.add(new String(ByteStreams.toByteArray(request.getInputStream()))); + httpServletResponse.getWriter().write(respones.remove(0)); + httpServletResponse.setStatus(codes.remove(0)); + request.setHandled(true); + } + }); + } + + public void stop() throws Exception { + _server.stop(); + } +} + +public class TestCbamTokenProvider extends TestBase { + + private static String GOOD_RESPONSE = "{ \"access_token\" : \"myToken\", \"expires_in\" : 1000 }"; + @InjectMocks + private CbamTokenProvider cbamTokenProvider; + private VnfmInfo vnfmInfo = new VnfmInfo(); + private ArgumentCaptor<SSLSocketFactory> sslSocketFactory = ArgumentCaptor.forClass(SSLSocketFactory.class); + private ArgumentCaptor<HostnameVerifier> hostnameVerifier = ArgumentCaptor.forClass(HostnameVerifier.class); + private HttpTestServer testServer; + + @Before + public void initMocks() throws Exception { + setField(CbamTokenProvider.class, "logger", logger); + setField(cbamTokenProvider, "username", "myUserName"); + setField(cbamTokenProvider, "password", "myPassword"); + setField(cbamTokenProvider, "skipCertificateVerification", true); + setField(cbamTokenProvider, "skipHostnameVerification", true); + when(vnfmInfoProvider.getVnfmInfo(VNFM_ID)).thenReturn(vnfmInfo); + vnfmInfo.setPassword("vnfmPassword"); + vnfmInfo.setUserName("vnfmUserName"); + vnfmInfo.setUrl("http://127.0.0.3:12345"); + testServer = new HttpTestServer(); + testServer.start(); + URI uri = testServer._server.getURI(); + setField(cbamTokenProvider, "cbamKeyCloakBaseUrl", uri.toString()); + } + + private void addGoodTokenResponse() { + testServer.respones.add(GOOD_RESPONSE); + testServer.codes.add(HttpStatus.OK.value()); + } + + @After + public void testServer() throws Exception { + testServer.stop(); + } + + /** + * a new token is requested no token has been requested before + */ + @Test + public void testBasicTokenRequest() throws Exception { + //given + addGoodTokenResponse(); + //when + String token = cbamTokenProvider.getToken(VNFM_ID); + //verify + assertEquals(1, testServer.requests.size()); + assertTokenRequest(testServer.requests.get(0)); + assertEquals("myToken", token); + + } + + /** + * a new token is requested if the previous token has expired + */ + @Test + public void testTokenIsRequestedIfPreviousExpired() throws Exception { + //given + addGoodTokenResponse(); + String firstToken = cbamTokenProvider.getToken(VNFM_ID); + testServer.respones.add("{ \"access_token\" : \"myToken2\", \"expires_in\" : 2000 }"); + testServer.codes.add(HttpStatus.OK.value()); + when(systemFunctions.currentTimeMillis()).thenReturn(500L * 1000 + 1L); + //when + String token = cbamTokenProvider.getToken(VNFM_ID); + //verify + assertEquals(2, testServer.requests.size()); + assertTokenRequest(testServer.requests.get(0)); + assertTokenRequest(testServer.requests.get(1)); + assertEquals("myToken2", token); + } + + /** + * a new token is not requested if the previous token has not expired + */ + @Test + public void testTokenIsNotRequestedIfPreviousHasNotExpired() throws Exception { + //given + addGoodTokenResponse(); + String firstToken = cbamTokenProvider.getToken(VNFM_ID); + testServer.respones.add("{ \"access_token\" : \"myToken2\", \"expires_in\" : 2000 }"); + testServer.codes.add(HttpStatus.OK.value()); + when(systemFunctions.currentTimeMillis()).thenReturn(500L * 1000); + //when + String token = cbamTokenProvider.getToken(VNFM_ID); + //verify + assertEquals(1, testServer.requests.size()); + assertTokenRequest(testServer.requests.get(0)); + assertEquals("myToken", token); + } + + /** + * failed token requests are retried for a fixed number amount of times + */ + @Test + public void testRetry() throws Exception { + //given + addFailedResponse(); + addFailedResponse(); + addFailedResponse(); + addFailedResponse(); + addGoodTokenResponse(); + //cbamTokenProvider.failOnRequestNumber = 5; + //when + String token = cbamTokenProvider.getToken(VNFM_ID); + //verify + assertEquals(5, testServer.requests.size()); + assertTokenRequest(testServer.requests.get(0)); + assertTokenRequest(testServer.requests.get(1)); + assertTokenRequest(testServer.requests.get(2)); + assertTokenRequest(testServer.requests.get(3)); + assertTokenRequest(testServer.requests.get(4)); + verify(logger).warn(eq("Unable to get token to access CBAM API (1/5)"), Mockito.<RuntimeException>any()); + verify(logger).warn(eq("Unable to get token to access CBAM API (2/5)"), Mockito.<RuntimeException>any()); + verify(logger).warn(eq("Unable to get token to access CBAM API (3/5)"), Mockito.<RuntimeException>any()); + verify(logger).warn(eq("Unable to get token to access CBAM API (4/5)"), Mockito.<RuntimeException>any()); + assertEquals("myToken", token); + } + + /** + * failed token requests are retried for a fixed number amount of times (reacing maximal number or retries) + */ + @Test + public void testNoMoreRetry() throws Exception { + //given + addFailedResponse(); + addFailedResponse(); + addFailedResponse(); + addFailedResponse(); + addFailedResponse(); + //when + try { + cbamTokenProvider.getToken(VNFM_ID); + fail(); + } catch (RuntimeException e) { + assertNotNull(e.getCause()); + } + //verify + assertEquals(5, testServer.requests.size()); + assertTokenRequest(testServer.requests.get(0)); + assertTokenRequest(testServer.requests.get(1)); + assertTokenRequest(testServer.requests.get(2)); + assertTokenRequest(testServer.requests.get(3)); + assertTokenRequest(testServer.requests.get(4)); + verify(logger).warn(eq("Unable to get token to access CBAM API (1/5)"), Mockito.<RuntimeException>any()); + verify(logger).warn(eq("Unable to get token to access CBAM API (2/5)"), Mockito.<RuntimeException>any()); + verify(logger).warn(eq("Unable to get token to access CBAM API (3/5)"), Mockito.<RuntimeException>any()); + verify(logger).warn(eq("Unable to get token to access CBAM API (4/5)"), Mockito.<RuntimeException>any()); + verify(logger).error(eq("Unable to get token to access CBAM API (giving up retries)"), Mockito.<RuntimeException>any()); + } + + private void addFailedResponse() { + testServer.codes.add(HttpStatus.UNAUTHORIZED.value()); + testServer.respones.add(new String()); + } + + /** + * the SSL connection is established without certificate & hostname verification + */ + @Test + public void noSslVerification() throws Exception { + //given + //the default settings is no SSL & hostname check + addGoodTokenResponse(); + //when + cbamTokenProvider.getToken(VNFM_ID); + //verify + //no exception is thrown + } + + /** + * if SSL is verified the certificates must be defined + */ + @Test + public void testInvalidCombinationOfSettings() throws Exception { + //given + setField(cbamTokenProvider, "skipCertificateVerification", false); + //when + try { + cbamTokenProvider.getToken(VNFM_ID); + //verify + fail(); + } catch (RuntimeException e) { + assertEquals("If the skipCertificateVerification is set to false (default) the trustedCertificates can not be empty", e.getMessage()); + } + } + + /** + * if SSL is verified the certificates must be defined + */ + @Test + public void testInvalidCombinationOfSettings2() throws Exception { + //given + setField(cbamTokenProvider, "skipCertificateVerification", false); + setField(cbamTokenProvider, "trustedCertificates", "xx\nxx"); + //when + try { + cbamTokenProvider.getToken(VNFM_ID); + //verify + fail(); + } catch (RuntimeException e) { + assertEquals("The trustedCertificates must be a base64 encoded collection of PEM certificates", e.getMessage()); + assertNotNull(e.getCause()); + } + } + + /** + * the SSL connection is established without certificate & hostname verification + */ + @Test + public void testNotTrustedSslConnection() throws Exception { + //given + setField(cbamTokenProvider, "skipCertificateVerification", false); + Path caPem = Paths.get(TestCbamTokenProvider.class.getResource("/unittests/sample.cert.pem").toURI()); + setField(cbamTokenProvider, "trustedCertificates", Base64.getEncoder().encodeToString(Files.readAllBytes(caPem))); + addGoodTokenResponse(); + //when + try { + cbamTokenProvider.getToken(VNFM_ID); + //verify + fail(); + } catch (RuntimeException e) { + assertTrue(e.getMessage().contains("unable to find valid certification path")); + assertTrue(e.getCause() instanceof SSLHandshakeException); + } + } + + /** + * the SSL connection is established with certificate & hostname verification + */ + @Test + public void testHostnameVerificationSucceeds() throws Exception { + //given + setField(cbamTokenProvider, "skipCertificateVerification", false); + Path caPem = Paths.get(TestCbamTokenProvider.class.getResource("/unittests/localhost.cert.pem").toURI()); + setField(cbamTokenProvider, "trustedCertificates", Base64.getEncoder().encodeToString(Files.readAllBytes(caPem))); + setField(cbamTokenProvider, "cbamKeyCloakBaseUrl", testServer._server.getURI().toString().replace("127.0.0.1", "localhost")); + setField(cbamTokenProvider, "skipHostnameVerification", false); + addGoodTokenResponse(); + //when + cbamTokenProvider.getToken(VNFM_ID); + //verify + //no seception is thrown + } + + /** + * the SSL connection is dropped with certificate & hostname verification due to invalid hostname + */ + @Test + public void testHostnameverifcationfail() throws Exception { + //given + setField(cbamTokenProvider, "skipCertificateVerification", false); + Path caPem = Paths.get(TestCbamTokenProvider.class.getResource("/unittests/localhost.cert.pem").toURI()); + setField(cbamTokenProvider, "trustedCertificates", Base64.getEncoder().encodeToString(Files.readAllBytes(caPem))); + setField(cbamTokenProvider, "skipHostnameVerification", false); + addGoodTokenResponse(); + //when + try { + cbamTokenProvider.getToken(VNFM_ID); + //verify + fail(); + } catch (RuntimeException e) { + assertTrue(e.getMessage().contains("Hostname 127.0.0.1 not verified")); + assertTrue(e.getCause() instanceof SSLPeerUnverifiedException); + } + } + + /** + * invalid certificate content + */ + @Test + public void testInvalidCerificateContent() throws Exception { + //given + setField(cbamTokenProvider, "skipCertificateVerification", false); + setField(cbamTokenProvider, "trustedCertificates", Base64.getEncoder().encodeToString("-----BEGIN CERTIFICATE-----\nkuku\n-----END CERTIFICATE-----\n".getBytes())); + setField(cbamTokenProvider, "skipHostnameVerification", false); + addGoodTokenResponse(); + //when + try { + cbamTokenProvider.getToken(VNFM_ID); + //verify + fail(); + } catch (RuntimeException e) { + assertTrue(e.getMessage().contains("Unable to load certificates")); + assertTrue(e.getCause() instanceof GeneralSecurityException); + } + } + + /** + * Verify client certificates are not verified + * \ + */ + @Test + public void testClientCertificates() throws Exception { + //when + new CbamTokenProvider.AllTrustedTrustManager().checkClientTrusted(null, null); + //verify + //no security exception is thrown + } + + /** + * Exception during keystore creation is logged (semi-useless) + */ + @Test + public void testKeystoreCreationFailure() { + KeyStoreException expectedException = new KeyStoreException(); + class X extends CbamTokenProvider { + X(VnfmInfoProvider vnfmInfoProvider) { + super(vnfmInfoProvider); + } + + @Override + TrustManager[] buildTrustManager() throws KeyStoreException, NoSuchProviderException, NoSuchAlgorithmException, InvalidKeySpecException, CertificateException { + throw expectedException; + } + } + try { + new X(null).buildSSLSocketFactory(); + fail(); + } catch (RuntimeException e) { + assertEquals(expectedException, e.getCause()); + verify(logger).error("Unable to create SSL socket factory", expectedException); + } + } + + private void assertTokenRequest(String body) { + assertContains(body, "grant_type", "password"); + assertContains(body, "client_id", "vnfmUserName"); + assertContains(body, "client_secret", "vnfmPassword"); + assertContains(body, "username", "myUserName"); + assertContains(body, "password", "myPassword"); + } + + private void assertContains(String content, String key, String value) { + assertTrue(content.contains(key + "=" + value)); + } +} diff --git a/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/vnfm/TestDriverProperties.java b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/vnfm/TestDriverProperties.java new file mode 100644 index 00000000..22db05b2 --- /dev/null +++ b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/vnfm/TestDriverProperties.java @@ -0,0 +1,35 @@ +/* + * 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.vnfm; + +import org.junit.Test; + +import static pl.pojo.tester.api.assertion.Assertions.assertPojoMethodsFor; + + +public class TestDriverProperties { + + /** + * Test basic POJO behaviour + */ + @Test + public void test() { + + + assertPojoMethodsFor(DriverProperties.class).areWellImplemented(); + } +} diff --git a/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/vnfm/TestJobManager.java b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/vnfm/TestJobManager.java new file mode 100644 index 00000000..1ba58abe --- /dev/null +++ b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/vnfm/TestJobManager.java @@ -0,0 +1,544 @@ +/* + * 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.vnfm; + +import com.google.common.collect.Lists; +import com.google.gson.JsonElement; +import com.google.gson.JsonParser; +import com.nokia.cbam.lcm.v32.ApiException; +import com.nokia.cbam.lcm.v32.model.*; +import org.junit.Before; +import org.junit.Test; +import org.mockito.ArgumentCaptor; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; +import org.onap.vnfmdriver.model.JobDetailInfo; +import org.onap.vnfmdriver.model.JobResponseInfo; +import org.onap.vnfmdriver.model.JobStatus; +import org.springframework.test.util.ReflectionTestUtils; +import org.threeten.bp.OffsetDateTime; + +import javax.servlet.http.HttpServletResponse; +import java.util.ArrayList; +import java.util.List; +import java.util.NoSuchElementException; +import java.util.UUID; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; + +import static junit.framework.TestCase.*; +import static org.mockito.Mockito.*; +import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CbamRestApiProvider.NOKIA_LCM_API_VERSION; +import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.ILifecycleChangeNotificationManager.SEPARATOR; +import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.JobManager.extractOnapJobId; +import static org.onap.vnfmdriver.model.JobStatus.*; + +public class TestJobManager extends TestBase { + + @Mock + private HttpServletResponse httpResponse; + + @InjectMocks + private JobManager jobManager; + private List<VnfInfo> vnfs = new ArrayList<>(); + + @Before + public void initMocks() throws Exception { + ReflectionTestUtils.setField(JobManager.class, "logger", logger); + when(vnfApi.vnfsGet(NOKIA_LCM_API_VERSION)).thenReturn(vnfs); + when(selfRegistrationManager.isReady()).thenReturn(true); + } + + /** + * Only the _ can be used as separator + * . / % & handled specially in URLs + * - used in CBAM for separation + */ + @Test + public void testSeparator() { + assertEquals("_", SEPARATOR); + } + + /** + * The operation result must contain the ONAP job identifier under the jobId field + */ + @Test + public void testJobIdExtractionFromOperationResult() { + assertEquals("1234", extractOnapJobId(new JsonParser().parse("{ \"additionalParams\" : { \"jobId\" : \"1234\"}}"))); + try { + extractOnapJobId(new JsonParser().parse("{ }")); + fail(); + } catch (NoSuchElementException e) { + assertEquals("The operation result {} does not contain the mandatory additionalParams structure", e.getMessage()); + } + try { + extractOnapJobId(new JsonParser().parse("{ \"additionalParams\" : { } }")); + fail(); + } catch (NoSuchElementException e) { + assertEquals("The operation result {\"additionalParams\":{}} does not contain the mandatory jobId in the additionalParams structure", e.getMessage()); + } + } + + /** + * If the VNF does not exists but the job manager still runs the VNF manipulation process the job is reported to be running + */ + @Test + public void testJobForNonExistingVnfReportedRunningIfJobIsOngoing() throws Exception { + String jobId = jobManager.spawnJob(VNF_ID, httpResponse); + //when + JobDetailInfo job = jobManager.getJob(VNFM_ID, jobId); + //verify + assertResult(jobId, job, STARTED, "50", "Operation started"); + } + + /** + * If the VNF does not exists and the internal job is not running the job is reported to be finished + */ + @Test + public void testJobForExistingVnfReportedRunningIfJobIsFinished() throws Exception { + String jobId = jobManager.spawnJob(VNF_ID, httpResponse); + jobManager.jobFinished(jobId); + //when + JobDetailInfo job = jobManager.getJob(VNFM_ID, jobId); + //verify + assertResult(jobId, job, JobStatus.FINISHED, "100", "Operation finished"); + } + + /** + * Spawning jobs after preparing for shutdown results in error + */ + @Test + public void testNoMoreJobsAreAllowedAfterPrepareForShutdown() throws Exception { + jobManager.prepareForShutdown(); + //when + try { + jobManager.spawnJob(JOB_ID, httpResponse); + fail(); + } catch (Exception e) { + verify(logger).error("The service is preparing to shut down"); + } + } + + /** + * Verify if the jobId has valid format + */ + @Test + public void testJobIdValidation() throws Exception { + try { + //when + jobManager.getJob(VNFM_ID, "bad"); + //verify + fail(); + } catch (IllegalArgumentException e) { + assertEquals("The jobId should be in the <vnfId>_<UUID> format, but was bad", e.getMessage()); + } + try { + //when + jobManager.getJob(VNFM_ID, "vnfId_"); + //verify + fail(); + } catch (IllegalArgumentException e) { + assertEquals("The UUID in the jobId (vnfId_) can not be empty", e.getMessage()); + } + try { + //when + jobManager.getJob(VNFM_ID, "_UUID"); + //verify + fail(); + } catch (IllegalArgumentException e) { + assertEquals("The vnfId in the jobId (_UUID) can not be empty", e.getMessage()); + } + } + + /** + * If the VNF exists but no operation execution is present with given internalJobId, than the state of the + * job is ongoing if the internal job is ongoing + */ + @Test + public void testExistingVnfWithNotYetStartedOperation() throws Exception { + String jobId = jobManager.spawnJob(VNF_ID, httpResponse); + VnfInfo vnf = new VnfInfo(); + vnf.setId(VNF_ID); + vnfs.add(vnf); + VnfInfo detailedVnf = new VnfInfo(); + detailedVnf.setId(VNF_ID); + when(vnfApi.vnfsVnfInstanceIdGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenReturn(detailedVnf); + JobDetailInfo job = jobManager.getJob(VNFM_ID, jobId); + //verify + assertResult(jobId, job, STARTED, "50", "Operation started"); + assertTrue(jobManager.hasOngoingJobs()); + } + + /** + * If the VNF exists but no operation execution is present with given internalJobId, than the state of the + * job is failed if the internal job is finished (the operation on CBAM was not able to start) + */ + @Test + public void testExistingVnfWithNotUnableToStartOperation() throws Exception { + String jobId = jobManager.spawnJob(VNF_ID, httpResponse); + VnfInfo vnf = new VnfInfo(); + vnf.setId(VNF_ID); + vnfs.add(vnf); + VnfInfo detailedVnf = new VnfInfo(); + detailedVnf.setId(VNF_ID); + when(vnfApi.vnfsVnfInstanceIdGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenReturn(detailedVnf); + jobManager.jobFinished(jobId); + JobDetailInfo job = jobManager.getJob(VNFM_ID, jobId); + //verify + assertResult(jobId, job, ERROR, "100", "Operation failed due to The requested operation was not able to start on CBAM"); + assertFalse(jobManager.hasOngoingJobs()); + } + + /** + * If the VNF exists but and the operation execution is present with given internalJobId, than the state of the + * job is ongoing if the operation is ongoing + */ + @Test + public void testExistingVnfWithStartedOperation() throws Exception { + String jobId = jobManager.spawnJob(VNF_ID, httpResponse); + VnfInfo vnf = new VnfInfo(); + vnf.setId(VNF_ID); + vnfs.add(vnf); + VnfInfo detailedVnf = new VnfInfo(); + detailedVnf.setId(VNF_ID); + when(vnfApi.vnfsVnfInstanceIdGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenReturn(detailedVnf); + OperationExecution operation = new OperationExecution(); + operation.setId(UUID.randomUUID().toString()); + operation.setStartTime(OffsetDateTime.now()); + operation.setStatus(OperationStatus.STARTED); + detailedVnf.setOperationExecutions(new ArrayList<>()); + detailedVnf.getOperationExecutions().add(operation); + JsonElement operationParams = new JsonParser().parse("{ \"additionalParams\" : { \"jobId\" : \"" + jobId + "\"}}"); + when(operationExecutionApi.operationExecutionsOperationExecutionIdOperationParamsGet(operation.getId(), NOKIA_LCM_API_VERSION)).thenReturn(operationParams); + JobDetailInfo job = jobManager.getJob(VNFM_ID, jobId); + //verify + assertResult(jobId, job, STARTED, "50", "Operation started"); + assertTrue(jobManager.hasOngoingJobs()); + } + + /** + * If the VNF does not exists till the time the job queries the status of the operation + */ + @Test + public void testTerminatedVnf() throws Exception { + //ddd + String jobId = jobManager.spawnJob(VNF_ID, httpResponse); + VnfInfo vnf = new VnfInfo(); + vnf.setId(VNF_ID); + vnfs.add(vnf); + VnfInfo detailedVnf = new VnfInfo(); + detailedVnf.setId(VNF_ID); + List<Integer> vnfQueryCallCounter = new ArrayList<>(); + when(vnfApi.vnfsVnfInstanceIdGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenAnswer(new Answer<VnfInfo>() { + @Override + public VnfInfo answer(InvocationOnMock invocation) throws Throwable { + vnfs.clear(); + return detailedVnf; + } + }); + + jobManager.jobFinished(jobId); + + OperationExecution operation = new OperationExecution(); + operation.setId(UUID.randomUUID().toString()); + operation.setStartTime(OffsetDateTime.now()); + operation.setStatus(OperationStatus.FINISHED); + operation.setOperationType(OperationType.TERMINATE); + detailedVnf.setOperationExecutions(new ArrayList<>()); + detailedVnf.getOperationExecutions().add(operation); + + JsonElement operationParams = new JsonParser().parse("{ \"additionalParams\" : { \"jobId\" : \"" + jobId + "\"}}"); + when(operationExecutionApi.operationExecutionsOperationExecutionIdOperationParamsGet(operation.getId(), NOKIA_LCM_API_VERSION)).thenReturn(operationParams); + //when + JobDetailInfo job = jobManager.getJob(VNFM_ID, jobId); + //verify + assertResult(jobId, job, FINISHED, "100", "Operation finished"); + } + + /** + * If the VNF exists but and the operation execution is present with given internalJobId, than the state of the + * job is error if the operation is failed + */ + @Test + public void testExistingVnfWithFailedOperation() throws Exception { + String jobId = jobManager.spawnJob(VNF_ID, httpResponse); + VnfInfo vnf = new VnfInfo(); + vnf.setId(VNF_ID); + vnfs.add(vnf); + VnfInfo detailedVnf = new VnfInfo(); + detailedVnf.setId(VNF_ID); + List<Integer> vnfCounter = new ArrayList<>(); + when(vnfApi.vnfsVnfInstanceIdGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenReturn(detailedVnf); + OperationExecution operation = new OperationExecution(); + operation.setId(UUID.randomUUID().toString()); + operation.setStartTime(OffsetDateTime.now()); + operation.setStatus(OperationStatus.FAILED); + ProblemDetails errorDetails = new ProblemDetails(); + errorDetails.setTitle("Title"); + errorDetails.setDetail("detail"); + operation.setError(errorDetails); + detailedVnf.setOperationExecutions(new ArrayList<>()); + detailedVnf.getOperationExecutions().add(operation); + JsonElement operationParams = new JsonParser().parse("{ \"additionalParams\" : { \"jobId\" : \"" + jobId + "\"}}"); + when(operationExecutionApi.operationExecutionsOperationExecutionIdOperationParamsGet(operation.getId(), NOKIA_LCM_API_VERSION)).thenReturn(operationParams); + //when + JobDetailInfo job = jobManager.getJob(VNFM_ID, jobId); + //verify + assertResult(jobId, job, ERROR, "100", "Operation failed due to Title: detail"); + assertTrue(jobManager.hasOngoingJobs()); + } + + private void assertResult(String jobId, JobDetailInfo job, JobStatus status, String progress, String descriptor) { + assertEquals(jobId, job.getJobId()); + assertEquals(status, job.getResponseDescriptor().getStatus()); + assertEquals(progress, job.getResponseDescriptor().getProgress()); + assertNull(job.getResponseDescriptor().getErrorCode()); + boolean finalState = JobStatus.ERROR.equals(status) || JobStatus.FINISHED.equals(status); + if (finalState) { + assertEquals(2, job.getResponseDescriptor().getResponseHistoryList().size()); + JobResponseInfo startEvent = job.getResponseDescriptor().getResponseHistoryList().get(0); + JobResponseInfo endEvent = job.getResponseDescriptor().getResponseHistoryList().get(1); + assertNull(startEvent.getErrorCode()); + assertEquals("50", startEvent.getProgress()); + assertEquals(JobStatus.STARTED.name(), startEvent.getStatus()); + assertEquals("1", startEvent.getResponseId()); + assertEquals("Operation started", startEvent.getStatusDescription()); + + assertNull(endEvent.getErrorCode()); + assertEquals("100", endEvent.getProgress()); + assertEquals(job.getResponseDescriptor().getStatus().name(), endEvent.getStatus()); + assertEquals("2", endEvent.getResponseId()); + assertEquals(descriptor, endEvent.getStatusDescription()); + } else { + assertEquals(1, job.getResponseDescriptor().getResponseHistoryList().size()); + assertNull(job.getResponseDescriptor().getResponseHistoryList().get(0).getErrorCode()); + assertEquals(progress, job.getResponseDescriptor().getResponseHistoryList().get(0).getProgress()); + assertEquals(job.getResponseDescriptor().getStatus().name(), job.getResponseDescriptor().getResponseHistoryList().get(0).getStatus()); + assertEquals("1", job.getResponseDescriptor().getResponseHistoryList().get(0).getResponseId()); + assertEquals(descriptor, job.getResponseDescriptor().getResponseHistoryList().get(0).getStatusDescription()); + } + assertEquals(Integer.toString(job.getResponseDescriptor().getResponseHistoryList().size()), job.getResponseDescriptor().getResponseId()); + assertEquals(descriptor, job.getResponseDescriptor().getStatusDescription()); + } + + /** + * If the VNF exists but and the operation execution is present with given internalJobId, than the state of the + * job is finished if the operation is finished, but is not a termination + */ + @Test + public void testExistingVnfWithFinishedOperation() throws Exception { + String jobId = jobManager.spawnJob(VNF_ID, httpResponse); + VnfInfo vnf = new VnfInfo(); + vnf.setId(VNF_ID); + vnfs.add(vnf); + VnfInfo detailedVnf = new VnfInfo(); + detailedVnf.setId(VNF_ID); + when(vnfApi.vnfsVnfInstanceIdGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenReturn(detailedVnf); + OperationExecution operation = new OperationExecution(); + operation.setId(UUID.randomUUID().toString()); + operation.setStartTime(OffsetDateTime.now()); + operation.setStatus(OperationStatus.FINISHED); + operation.setOperationType(OperationType.SCALE); + detailedVnf.setOperationExecutions(new ArrayList<>()); + detailedVnf.getOperationExecutions().add(operation); + JsonElement operationParams = new JsonParser().parse("{ \"additionalParams\" : { \"jobId\" : \"" + jobId + "\"}}"); + when(operationExecutionApi.operationExecutionsOperationExecutionIdOperationParamsGet(operation.getId(), NOKIA_LCM_API_VERSION)).thenReturn(operationParams); + JobDetailInfo job = jobManager.getJob(VNFM_ID, jobId); + //verify + assertResult(jobId, job, JobStatus.FINISHED, "100", "Operation finished"); + assertTrue(jobManager.hasOngoingJobs()); + } + + /** + * If the VNF exists but and the operation execution is present with given internalJobId, than the state of the + * job is ongoing if the termination operation is finished. In ONAP terminology the termination includes + * delete, so the ONAP operation is ongoing since the VNF is not yet deleted + */ + @Test + public void testExistingVnfWithFinishedTerminationOperation() throws Exception { + String jobId = jobManager.spawnJob(VNF_ID, httpResponse); + VnfInfo vnf = new VnfInfo(); + vnf.setId(VNF_ID); + vnfs.add(vnf); + VnfInfo detailedVnf = new VnfInfo(); + detailedVnf.setId(VNF_ID); + when(vnfApi.vnfsVnfInstanceIdGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenReturn(detailedVnf); + OperationExecution operation = new OperationExecution(); + operation.setId(UUID.randomUUID().toString()); + operation.setStartTime(OffsetDateTime.now()); + operation.setStatus(OperationStatus.FINISHED); + operation.setOperationType(OperationType.TERMINATE); + detailedVnf.setOperationExecutions(new ArrayList<>()); + detailedVnf.getOperationExecutions().add(operation); + JsonElement operationParams = new JsonParser().parse("{ \"additionalParams\" : { \"jobId\" : \"" + jobId + "\"}}"); + when(operationExecutionApi.operationExecutionsOperationExecutionIdOperationParamsGet(operation.getId(), NOKIA_LCM_API_VERSION)).thenReturn(operationParams); + JobDetailInfo job = jobManager.getJob(VNFM_ID, jobId); + //verify + assertResult(jobId, job, STARTED, "50", "Operation started"); + //when + jobManager.jobFinished(jobId); + job = jobManager.getJob(VNFM_ID, jobId); + //verify + assertResult(jobId, job, ERROR, "100", "Operation failed due to unable to delete VNF"); + assertFalse(jobManager.hasOngoingJobs()); + + } + + /** + * Failuire to retrieve operation parameters (CBAM REST API fail) is logged and propagated + */ + @Test + public void failuresDuringOperationExecutionRetrievalIsLoggedAndPropagated() throws Exception { + String jobId = jobManager.spawnJob(VNF_ID, httpResponse); + VnfInfo vnf = new VnfInfo(); + vnf.setId(VNF_ID); + vnfs.add(vnf); + VnfInfo detailedVnf = new VnfInfo(); + detailedVnf.setId(VNF_ID); + when(vnfApi.vnfsVnfInstanceIdGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenReturn(detailedVnf); + OperationExecution operation = new OperationExecution(); + operation.setId(UUID.randomUUID().toString()); + detailedVnf.setOperationExecutions(new ArrayList<>()); + detailedVnf.getOperationExecutions().add(operation); + ApiException expectedException = new ApiException(); + when(operationExecutionApi.operationExecutionsOperationExecutionIdOperationParamsGet(operation.getId(), NOKIA_LCM_API_VERSION)).thenThrow(expectedException); + //verify + try { + JobDetailInfo job = jobManager.getJob(VNFM_ID, jobId); + fail(); + } catch (RuntimeException e) { + assertEquals(expectedException, e.getCause()); + verify(logger).error("Unable to retrieve operation parameters", expectedException); + } + assertTrue(jobManager.hasOngoingJobs()); + } + + /** + * Failure to retrieve VNF (CBAM REST API fail) is logged and propagated + */ + @Test + public void failuresDuringVnfRetrievalIsLoggedAndPropagated() throws Exception { + String jobId = jobManager.spawnJob(VNF_ID, httpResponse); + VnfInfo vnf = new VnfInfo(); + vnf.setId(VNF_ID); + vnfs.add(vnf); + ApiException expectedException = new ApiException(); + when(vnfApi.vnfsVnfInstanceIdGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenThrow(expectedException); + //verify + try { + JobDetailInfo job = jobManager.getJob(VNFM_ID, jobId); + fail(); + } catch (RuntimeException e) { + assertEquals(expectedException, e.getCause()); + verify(logger).error("Unable to retrieve VNF", expectedException); + } + assertTrue(jobManager.hasOngoingJobs()); + } + + /** + * When searching for the ONAP job by iterating the operation executions. The newest jobs + * are inspected first (performance optimalization) + */ + @Test + public void testNewestOperationAreInspectedFirst() throws Exception { + String jobId = jobManager.spawnJob(VNF_ID, httpResponse); + VnfInfo vnf = new VnfInfo(); + vnf.setId(VNF_ID); + vnfs.add(vnf); + VnfInfo detailedVnf = new VnfInfo(); + detailedVnf.setId(VNF_ID); + when(vnfApi.vnfsVnfInstanceIdGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenReturn(detailedVnf); + OperationExecution olderOperation = new OperationExecution(); + olderOperation.setId(UUID.randomUUID().toString()); + olderOperation.setStartTime(OffsetDateTime.now()); + olderOperation.setStatus(OperationStatus.FINISHED); + olderOperation.setOperationType(OperationType.TERMINATE); + OperationExecution newerOperation = new OperationExecution(); + newerOperation.setId(UUID.randomUUID().toString()); + newerOperation.setStartTime(OffsetDateTime.now().plusDays(1)); + newerOperation.setStatus(OperationStatus.FINISHED); + newerOperation.setOperationType(OperationType.TERMINATE); + detailedVnf.setOperationExecutions(new ArrayList<>()); + detailedVnf.getOperationExecutions().add(olderOperation); + detailedVnf.getOperationExecutions().add(newerOperation); + JsonElement operationParams = new JsonParser().parse("{ \"additionalParams\" : { \"jobId\" : \"" + jobId + "\"}}"); + List<String> queriedOperaionsInOrder = new ArrayList<>(); + when(operationExecutionApi.operationExecutionsOperationExecutionIdOperationParamsGet(Mockito.anyString(), Mockito.eq(NOKIA_LCM_API_VERSION))) + .then(new Answer<Object>() { + @Override + public Object answer(InvocationOnMock invocationOnMock) throws Throwable { + queriedOperaionsInOrder.add(invocationOnMock.getArguments()[0].toString()); + if (invocationOnMock.getArguments()[0].equals(olderOperation.getId())) { + return new JsonParser().parse("{ \"additionalParams\" : { \"jobId\" : \"" + jobId + "\"}}"); + } else { + return new JsonParser().parse("{ \"additionalParams\" : { \"jobId\" : \"" + "nonMatching" + "\"}}"); + } + } + }); + JobDetailInfo job = jobManager.getJob(VNFM_ID, jobId); + //verify + assertEquals(Lists.newArrayList(newerOperation.getId(), olderOperation.getId()), queriedOperaionsInOrder); + assertTrue(jobManager.hasOngoingJobs()); + } + + /** + * if the registration process has not finished it is prevented to spawn jobs + */ + @Test + public void noJobCanBeStartedIfRegistrationNotFinished() throws Exception { + //given + when(selfRegistrationManager.isReady()).thenReturn(false); + //when + try { + jobManager.spawnJob(VNF_ID, httpResponse); + fail(); + } catch (RuntimeException e) { + assertEquals("The service is not yet ready", e.getMessage()); + } + } + + /** + * Ongoing job are out waited during the the preparation for shutdown + */ + @Test + public void onGoingJobsAreOutwaitedDuringShutdown() throws Exception { + String firstJobId = jobManager.spawnJob(VNF_ID, httpResponse); + ExecutorService executorService = Executors.newCachedThreadPool(); + ArgumentCaptor<Integer> sleeps = ArgumentCaptor.forClass(Integer.class); + doNothing().when(systemFunctions).sleep(sleeps.capture()); + Future<?> shutDown = executorService.submit(new Runnable() { + @Override + public void run() { + jobManager.prepareForShutdown(); + } + }); + while (sleeps.getAllValues().size() == 0) { + try { + Thread.sleep(1); + } catch (InterruptedException e) { + } + } + assertFalse(shutDown.isDone()); + jobManager.jobFinished(firstJobId); + //verify + shutDown.get(); + verify(systemFunctions, times(sleeps.getAllValues().size())).sleep(500L); + } +} diff --git a/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/vnfm/TestLifecycleManager.java b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/vnfm/TestLifecycleManager.java new file mode 100644 index 00000000..64adaab1 --- /dev/null +++ b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/vnfm/TestLifecycleManager.java @@ -0,0 +1,1336 @@ +/* + * 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.vnfm; + +import com.google.gson.Gson; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import com.nokia.cbam.catalog.v1.model.CatalogAdapterVnfpackage; +import com.nokia.cbam.lcm.v32.ApiException; +import com.nokia.cbam.lcm.v32.model.*; +import com.nokia.cbam.lcm.v32.model.OperationType; +import com.nokia.cbam.lcm.v32.model.VimInfo; +import com.nokia.cbam.lcm.v32.model.VnfInfo; +import org.junit.Before; +import org.junit.Test; +import org.mockito.ArgumentCaptor; +import org.mockito.InOrder; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.api.VimInfoProvider; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.vfc.TestVfcGrantManager; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.vfc.VfcGrantManager; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.StoreLoader; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.notification.LifecycleChangeNotificationManager; +import org.onap.vnfmdriver.model.ExtVirtualLinkInfo; +import org.onap.vnfmdriver.model.*; +import org.onap.vnfmdriver.model.ScaleDirection; +import org.threeten.bp.OffsetDateTime; + +import javax.servlet.http.HttpServletResponse; +import java.nio.file.Paths; +import java.util.*; + +import static java.lang.Boolean.parseBoolean; +import static java.nio.file.Files.readAllBytes; +import static junit.framework.TestCase.*; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.*; +import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.SystemFunctions.systemFunctions; +import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CbamRestApiProvider.NOKIA_LCM_API_VERSION; +import static org.springframework.test.util.ReflectionTestUtils.setField; + +public class TestLifecycleManager extends TestBase { + public static final String JOB_ID = "myJobId"; + public static final String CBAM_VNFD_ID = "cbamVnfdId"; + public static final String TENANT = "myTenant"; + public static final String OPERATION_EXECUTION_ID = "operationExecutionId"; + private static final String ONAP_CSAR_ID = "myOnapCsarId"; + private static final String VIM_ID = "ownerId_regionId"; + private GrantVNFResponseVim vim = new GrantVNFResponseVim(); + @Mock + private CatalogManager catalogManager; + @Mock + private VfcGrantManager vfcGrantManager; + @Mock + private CbamTokenProvider tokenProvider; + @Mock + private JobManager jobManager; + @Mock + private LifecycleChangeNotificationManager notificationManager; + @Mock + private HttpServletResponse restResponse; + @Mock + private VimInfoProvider vimInfoProvider; + + private ArgumentCaptor<CreateVnfRequest> createRequest = ArgumentCaptor.forClass(CreateVnfRequest.class); + private AdditionalParams additionalParam = new AdditionalParams(); + private String INSTANTIATION_LEVEL = "level1"; + private GrantVNFResponseVim grantResponse = new GrantVNFResponseVim(); + private String cbamVnfdContent; + private OperationExecution instantiationOperationExecution = new OperationExecution(); + private OperationExecution modifyPropertyoperationExecution = new OperationExecution(); + private OperationExecution scaleOperationExecution = new OperationExecution(); + private OperationExecution healOperationExecution = new OperationExecution(); + + private VnfInfo vnfInfo = new VnfInfo(); + private List<OperationExecution> operationExecutions = new ArrayList<>(); + private org.onap.vnfmdriver.model.VimInfo vimInfo = new org.onap.vnfmdriver.model.VimInfo(); + private ExtVirtualLinkInfo externalVirtualLink = new ExtVirtualLinkInfo(); + private ExtManagedVirtualLinkData extManVl = new ExtManagedVirtualLinkData(); + private ArgumentCaptor<ModifyVnfInfoRequest> actualVnfModifyRequest = ArgumentCaptor.forClass(ModifyVnfInfoRequest.class); + private Set<Boolean> finished = new HashSet<>(); + private ArgumentCaptor<ScaleVnfRequest> actualScaleRequest = ArgumentCaptor.forClass(ScaleVnfRequest.class); + private ArgumentCaptor<HealVnfRequest> actualHealRequest = ArgumentCaptor.forClass(HealVnfRequest.class); + + private LifecycleManager lifecycleManager; + + @Before + public void initMocks() throws Exception { + vnfInfo.setExtensions(new ArrayList<>()); + vnfInfo.setOperationExecutions(new ArrayList<>()); + lifecycleManager = new LifecycleManager(catalogManager, vfcGrantManager, cbamRestApiProvider, vimInfoProvider, jobManager, notificationManager); + cbamVnfdContent = new String(readAllBytes(Paths.get(TestVfcGrantManager.class.getResource("/unittests/vnfd.full.yaml").toURI()))); + setField(LifecycleManager.class, "logger", logger); + CatalogAdapterVnfpackage cbamPackage = new CatalogAdapterVnfpackage(); + when(catalogManager.preparePackageInCbam(VNFM_ID, ONAP_CSAR_ID)).thenReturn(cbamPackage); + cbamPackage.setVnfdId(CBAM_VNFD_ID); + vnfInfo.setVnfdId(CBAM_VNFD_ID); + vnfInfo.setId(VNF_ID); + when(jobManager.spawnJob(VNF_ID, restResponse)).thenReturn(JOB_ID); + when(catalogManager.getCbamVnfdContent(VNFM_ID, CBAM_VNFD_ID)).thenReturn(cbamVnfdContent); + cbamPackage.setId(CBAM_VNFD_ID); + vimInfo.setUrl("cloudUrl"); + vimInfo.setPassword("vimPassword"); + vimInfo.setUserName("vimUsername"); + vimInfo.setSslInsecure("true"); + vimInfo.setVimId(VIM_ID); + vimInfo.setName("vimName"); + when(vimInfoProvider.getVimInfo((VIM_ID))).thenReturn(vimInfo); + instantiationOperationExecution.setId(OPERATION_EXECUTION_ID); + instantiationOperationExecution.setOperationType(OperationType.INSTANTIATE); + instantiationOperationExecution.setStartTime(OffsetDateTime.now()); + when(vnfApi.vnfsVnfInstanceIdOperationExecutionsGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenReturn(operationExecutions); + operationExecutions.add(modifyPropertyoperationExecution); + modifyPropertyoperationExecution.setStartTime(OffsetDateTime.now()); + modifyPropertyoperationExecution.setOperationType(OperationType.MODIFY_INFO); + operationExecutions.add(instantiationOperationExecution); + instantiationOperationExecution.setStatus(OperationStatus.FINISHED); + modifyPropertyoperationExecution.setStatus(OperationStatus.FINISHED); + modifyPropertyoperationExecution.setId(UUID.randomUUID().toString()); + scaleOperationExecution.setId(UUID.randomUUID().toString()); + healOperationExecution.setId(UUID.randomUUID().toString()); + when(vnfApi.vnfsVnfInstanceIdPatch(eq(VNF_ID), actualVnfModifyRequest.capture(), eq(NOKIA_LCM_API_VERSION))).thenReturn(modifyPropertyoperationExecution); + doAnswer(new Answer() { + @Override + public Object answer(InvocationOnMock invocation) throws Throwable { + finished.add(Boolean.TRUE); + return null; + } + }).when(jobManager).jobFinished(JOB_ID); + when(vnfApi.vnfsVnfInstanceIdScalePost(eq(VNF_ID), actualScaleRequest.capture(), eq(NOKIA_LCM_API_VERSION))).thenAnswer(new Answer<OperationExecution>() { + @Override + public OperationExecution answer(InvocationOnMock invocation) throws Throwable { + operationExecutions.add(scaleOperationExecution); + return scaleOperationExecution; + } + }); + when(vnfApi.vnfsVnfInstanceIdHealPost(eq(VNF_ID), actualHealRequest.capture(), eq(NOKIA_LCM_API_VERSION))).thenAnswer(new Answer<OperationExecution>() { + @Override + public OperationExecution answer(InvocationOnMock invocation) throws Throwable { + operationExecutions.add(healOperationExecution); + return healOperationExecution; + } + }); + } + + /** + * test instantiation + */ + @Test + public void testInstantiation() throws Exception { + //given + VnfInstantiateRequest instantiationRequest = prepareInstantiationRequest(VimInfo.VimInfoTypeEnum.OPENSTACK_V2_INFO); + + when(vnfApi.vnfsPost(createRequest.capture(), eq(NOKIA_LCM_API_VERSION))).thenReturn(vnfInfo); + additionalParam.setInstantiationLevel(INSTANTIATION_LEVEL); + when(vfcGrantManager.requestGrantForInstantiate(VNFM_ID, VNF_ID, VIM_ID, ONAP_CSAR_ID, INSTANTIATION_LEVEL, cbamVnfdContent, JOB_ID)).thenReturn(grantResponse); + grantResponse.setVimId(VIM_ID); + GrantVNFResponseVimAccessInfo accessInfo = new GrantVNFResponseVimAccessInfo(); + accessInfo.setTenant(TENANT); + grantResponse.setAccessInfo(accessInfo); + ArgumentCaptor<InstantiateVnfRequest> actualInstantiationRequest = ArgumentCaptor.forClass(InstantiateVnfRequest.class); + when(vnfApi.vnfsVnfInstanceIdInstantiatePost(eq(VNF_ID), actualInstantiationRequest.capture(), eq(NOKIA_LCM_API_VERSION))).thenReturn(instantiationOperationExecution); + //when + VnfInstantiateResponse response = lifecycleManager.instantiate(VNFM_ID, instantiationRequest, restResponse); + waitForJobToFinishInJobManager(finished); + //verify + assertEquals(VNF_ID, response.getVnfInstanceId()); + assertEquals(JOB_ID, response.getJobId()); + assertEquals(createRequest.getAllValues().size(), 1); + assertEquals("myDescription", createRequest.getValue().getDescription()); + assertEquals("vnfName", createRequest.getValue().getName()); + assertEquals(CBAM_VNFD_ID, createRequest.getValue().getVnfdId()); + assertEquals(1, actualInstantiationRequest.getAllValues().size()); + assertEquals(1, actualInstantiationRequest.getValue().getVims().size()); + OPENSTACKV2INFO actualVim = (OPENSTACKV2INFO) actualInstantiationRequest.getValue().getVims().get(0); + assertEquals(VIM_ID, actualVim.getId()); + assertEquals(VimInfo.VimInfoTypeEnum.OPENSTACK_V2_INFO, actualVim.getVimInfoType()); + assertEquals(Boolean.valueOf(parseBoolean(vimInfo.getSslInsecure())), actualVim.getInterfaceInfo().isSkipCertificateVerification()); + assertEquals("cloudUrl", actualVim.getInterfaceInfo().getEndpoint()); + //FIXME assertEquals();actualVim.getInterfaceInfo().getTrustedCertificates()); + assertEquals("vimPassword", actualVim.getAccessInfo().getPassword()); + assertEquals("regionId", actualVim.getAccessInfo().getRegion()); + assertEquals("myTenant", actualVim.getAccessInfo().getTenant()); + assertEquals("vimUsername", actualVim.getAccessInfo().getUsername()); + assertEquals(1, actualInstantiationRequest.getValue().getComputeResourceFlavours().size()); + assertEquals("flavourProviderId", actualInstantiationRequest.getValue().getComputeResourceFlavours().get(0).getResourceId()); + assertEquals(VIM_ID, actualInstantiationRequest.getValue().getComputeResourceFlavours().get(0).getVimId()); + assertEquals("virtualComputeDescId", actualInstantiationRequest.getValue().getComputeResourceFlavours().get(0).getVnfdVirtualComputeDescId()); + assertEquals(1, actualInstantiationRequest.getValue().getExtManagedVirtualLinks().size()); + assertEquals(extManVl, actualInstantiationRequest.getValue().getExtManagedVirtualLinks().get(0)); + assertEquals(2, actualInstantiationRequest.getValue().getExtVirtualLinks().size()); + + assertEquals("myNetworkProviderId", actualInstantiationRequest.getValue().getExtVirtualLinks().get(0).getResourceId()); + assertEquals("myEVlId", actualInstantiationRequest.getValue().getExtVirtualLinks().get(0).getExtVirtualLinkId()); + assertEquals(1, actualInstantiationRequest.getValue().getExtVirtualLinks().get(0).getExtCps().size()); + assertEquals("myCpdId", actualInstantiationRequest.getValue().getExtVirtualLinks().get(0).getExtCps().get(0).getCpdId()); + + assertEquals(VIM_ID, actualInstantiationRequest.getValue().getExtVirtualLinks().get(1).getVimId()); + assertEquals("evlId1", actualInstantiationRequest.getValue().getExtVirtualLinks().get(1).getExtVirtualLinkId()); + assertEquals("networkProviderId1", actualInstantiationRequest.getValue().getExtVirtualLinks().get(1).getResourceId()); + assertEquals(1, actualInstantiationRequest.getValue().getExtVirtualLinks().get(1).getExtCps().size()); + + + assertEquals(Integer.valueOf(2), actualInstantiationRequest.getValue().getExtVirtualLinks().get(1).getExtCps().get(0).getNumDynamicAddresses()); + assertEquals("cpdId3", actualInstantiationRequest.getValue().getExtVirtualLinks().get(1).getExtCps().get(0).getCpdId()); + assertEquals(1, actualInstantiationRequest.getValue().getExtVirtualLinks().get(1).getExtCps().get(0).getAddresses().size()); + assertEquals("1.2.3.4", actualInstantiationRequest.getValue().getExtVirtualLinks().get(1).getExtCps().get(0).getAddresses().get(0).getIp()); + assertEquals("mac", actualInstantiationRequest.getValue().getExtVirtualLinks().get(1).getExtCps().get(0).getAddresses().get(0).getMac()); + assertEquals("subnetId", actualInstantiationRequest.getValue().getExtVirtualLinks().get(1).getExtCps().get(0).getAddresses().get(0).getSubnetId()); + + assertEquals("myFlavorId", actualInstantiationRequest.getValue().getFlavourId()); + assertEquals(Boolean.TRUE, actualInstantiationRequest.getValue().isGrantlessMode()); + assertEquals("level1", actualInstantiationRequest.getValue().getInstantiationLevelId()); + assertEquals(1, actualInstantiationRequest.getValue().getZones().size()); + assertEquals(VIM_ID, actualInstantiationRequest.getValue().getZones().get(0).getVimId()); + assertEquals("zoneProviderId", actualInstantiationRequest.getValue().getZones().get(0).getResourceId()); + assertEquals("zoneId", actualInstantiationRequest.getValue().getZones().get(0).getId()); + assertEquals(1, actualInstantiationRequest.getValue().getSoftwareImages().size()); + assertEquals(VIM_ID, actualInstantiationRequest.getValue().getSoftwareImages().get(0).getVimId()); + assertEquals("imageProviderId", actualInstantiationRequest.getValue().getSoftwareImages().get(0).getResourceId()); + assertEquals("imageId", actualInstantiationRequest.getValue().getSoftwareImages().get(0).getVnfdSoftwareImageId()); + String actualEmbeddedAdditionParams = new Gson().toJson(actualInstantiationRequest.getValue().getAdditionalParams()); + assertTrue("{\"jobId\":\"myJobId\",\"a\":\"b\"}".equals(actualEmbeddedAdditionParams) || "{\"a\":\"b\",\"jobId\":\"myJobId\"}".equals(actualEmbeddedAdditionParams)); + assertTrue(actualVim.getInterfaceInfo().isSkipCertificateVerification()); + assertTrue(actualVim.getInterfaceInfo().isSkipCertificateHostnameCheck()); + + assertEquals(1, actualVnfModifyRequest.getAllValues().size()); + assertEquals(2, actualVnfModifyRequest.getValue().getExtensions().size()); + assertEquals(LifecycleManager.ONAP_CSAR_ID, actualVnfModifyRequest.getValue().getExtensions().get(0).getName()); + assertEquals(ONAP_CSAR_ID, actualVnfModifyRequest.getValue().getExtensions().get(0).getValue()); + assertEquals(ILifecycleChangeNotificationManager.EXTERNAL_VNFM_ID, actualVnfModifyRequest.getValue().getExtensions().get(1).getName()); + assertEquals(VNFM_ID, actualVnfModifyRequest.getValue().getExtensions().get(1).getValue()); + + //the 3.2 API does not accept empty array + assertNull(actualVnfModifyRequest.getValue().getVnfConfigurableProperties()); + verify(jobManager).spawnJob(VNF_ID, restResponse); + } + + /** + * invalid VIM type results in failure + */ + @Test + public void testInstantiationWithInvalidVimType() throws Exception { + //given + VnfInstantiateRequest instantiationRequest = prepareInstantiationRequest(VimInfo.VimInfoTypeEnum.OTHER_VIM_INFO); + when(vnfApi.vnfsPost(createRequest.capture(), eq(NOKIA_LCM_API_VERSION))).thenReturn(vnfInfo); + //when + try { + lifecycleManager.instantiate(VNFM_ID, instantiationRequest, restResponse); + //verify + fail(); + } catch (Exception e) { + assertEquals("Only OPENSTACK_V2_INFO, OPENSTACK_V3_INFO and VMWARE_VCLOUD_INFO is the supported VIM types", e.getMessage()); + } + verify(vnfApi, never()).vnfsPost(Mockito.any(), Mockito.any()); + verify(logger).error("Only OPENSTACK_V2_INFO, OPENSTACK_V3_INFO and VMWARE_VCLOUD_INFO is the supported VIM types"); + } + + /** + * test instantiation with KeyStone V2 based with SSL + */ + @Test + public void testInstantiationV2WithSsl() throws Exception { + VnfInstantiateRequest instantiationRequest = prepareInstantiationRequest(VimInfo.VimInfoTypeEnum.OPENSTACK_V2_INFO); + + when(vnfApi.vnfsPost(createRequest.capture(), eq(NOKIA_LCM_API_VERSION))).thenReturn(vnfInfo); + additionalParam.setInstantiationLevel(INSTANTIATION_LEVEL); + when(vfcGrantManager.requestGrantForInstantiate(VNFM_ID, VNF_ID, VIM_ID, ONAP_CSAR_ID, INSTANTIATION_LEVEL, cbamVnfdContent, JOB_ID)).thenReturn(grantResponse); + grantResponse.setVimId(VIM_ID); + GrantVNFResponseVimAccessInfo accessInfo = new GrantVNFResponseVimAccessInfo(); + accessInfo.setTenant(TENANT); + String caCert = new String(readAllBytes(Paths.get(TestVfcGrantManager.class.getResource("/unittests/localhost.cert.pem").toURI()))); + vimInfo.setSslInsecure("false"); + vimInfo.setSslCacert(caCert); + grantResponse.setAccessInfo(accessInfo); + ArgumentCaptor<InstantiateVnfRequest> actualInstantiationRequest = ArgumentCaptor.forClass(InstantiateVnfRequest.class); + when(vnfApi.vnfsVnfInstanceIdInstantiatePost(eq(VNF_ID), actualInstantiationRequest.capture(), eq(NOKIA_LCM_API_VERSION))).thenReturn(instantiationOperationExecution); + //when + VnfInstantiateResponse response = lifecycleManager.instantiate(VNFM_ID, instantiationRequest, restResponse); + waitForJobToFinishInJobManager(finished); + assertEquals(1, actualInstantiationRequest.getValue().getVims().size()); + //verify + OPENSTACKV2INFO actualVim = (OPENSTACKV2INFO) actualInstantiationRequest.getValue().getVims().get(0); + assertEquals(StoreLoader.getCertifacates(caCert).iterator().next(), new String(actualVim.getInterfaceInfo().getTrustedCertificates().get(0))); + assertTrue(!actualVim.getInterfaceInfo().isSkipCertificateVerification()); + assertTrue(!actualVim.getInterfaceInfo().isSkipCertificateHostnameCheck()); + } + + /** + * test instantiation with KeyStone V3 based + */ + @Test + public void testInstantiationV3() throws Exception { + VnfInstantiateRequest instantiationRequest = prepareInstantiationRequest(VimInfo.VimInfoTypeEnum.OPENSTACK_V3_INFO); + + when(vnfApi.vnfsPost(createRequest.capture(), eq(NOKIA_LCM_API_VERSION))).thenReturn(vnfInfo); + additionalParam.setInstantiationLevel(INSTANTIATION_LEVEL); + when(vfcGrantManager.requestGrantForInstantiate(VNFM_ID, VNF_ID, VIM_ID, ONAP_CSAR_ID, INSTANTIATION_LEVEL, cbamVnfdContent, JOB_ID)).thenReturn(grantResponse); + grantResponse.setVimId(VIM_ID); + GrantVNFResponseVimAccessInfo accessInfo = new GrantVNFResponseVimAccessInfo(); + accessInfo.setTenant(TENANT); + grantResponse.setAccessInfo(accessInfo); + ArgumentCaptor<InstantiateVnfRequest> actualInstantiationRequest = ArgumentCaptor.forClass(InstantiateVnfRequest.class); + when(vnfApi.vnfsVnfInstanceIdInstantiatePost(eq(VNF_ID), actualInstantiationRequest.capture(), eq(NOKIA_LCM_API_VERSION))).thenReturn(instantiationOperationExecution); + //when + VnfInstantiateResponse response = lifecycleManager.instantiate(VNFM_ID, instantiationRequest, restResponse); + waitForJobToFinishInJobManager(finished); + assertEquals(1, actualInstantiationRequest.getValue().getVims().size()); + //verify + OPENSTACKV3INFO actualVim = (OPENSTACKV3INFO) actualInstantiationRequest.getValue().getVims().get(0); + assertEquals(VIM_ID, actualVim.getId()); + assertEquals(VimInfo.VimInfoTypeEnum.OPENSTACK_V3_INFO, actualVim.getVimInfoType()); + assertEquals(Boolean.valueOf(parseBoolean(vimInfo.getSslInsecure())), actualVim.getInterfaceInfo().isSkipCertificateVerification()); + assertEquals("cloudUrl", actualVim.getInterfaceInfo().getEndpoint()); + //FIXME assertEquals();actualVim.getInterfaceInfo().getTrustedCertificates()); + assertEquals("vimPassword", actualVim.getAccessInfo().getPassword()); + assertEquals("regionId", actualVim.getAccessInfo().getRegion()); + assertEquals("myTenant", actualVim.getAccessInfo().getProject()); + assertEquals("myDomain", actualVim.getAccessInfo().getDomain()); + assertEquals("vimUsername", actualVim.getAccessInfo().getUsername()); + assertTrue(actualVim.getInterfaceInfo().isSkipCertificateVerification()); + assertTrue(actualVim.getInterfaceInfo().isSkipCertificateHostnameCheck()); + } + + /** + * test instantiation with KeyStone V3 based with SSL + */ + @Test + public void testInstantiationV3WithSsl() throws Exception { + VnfInstantiateRequest instantiationRequest = prepareInstantiationRequest(VimInfo.VimInfoTypeEnum.OPENSTACK_V3_INFO); + + when(vnfApi.vnfsPost(createRequest.capture(), eq(NOKIA_LCM_API_VERSION))).thenReturn(vnfInfo); + additionalParam.setInstantiationLevel(INSTANTIATION_LEVEL); + when(vfcGrantManager.requestGrantForInstantiate(VNFM_ID, VNF_ID, VIM_ID, ONAP_CSAR_ID, INSTANTIATION_LEVEL, cbamVnfdContent, JOB_ID)).thenReturn(grantResponse); + grantResponse.setVimId(VIM_ID); + GrantVNFResponseVimAccessInfo accessInfo = new GrantVNFResponseVimAccessInfo(); + accessInfo.setTenant(TENANT); + String caCert = new String(readAllBytes(Paths.get(TestVfcGrantManager.class.getResource("/unittests/localhost.cert.pem").toURI()))); + vimInfo.setSslInsecure("false"); + vimInfo.setSslCacert(caCert); + grantResponse.setAccessInfo(accessInfo); + ArgumentCaptor<InstantiateVnfRequest> actualInstantiationRequest = ArgumentCaptor.forClass(InstantiateVnfRequest.class); + when(vnfApi.vnfsVnfInstanceIdInstantiatePost(eq(VNF_ID), actualInstantiationRequest.capture(), eq(NOKIA_LCM_API_VERSION))).thenReturn(instantiationOperationExecution); + //when + VnfInstantiateResponse response = lifecycleManager.instantiate(VNFM_ID, instantiationRequest, restResponse); + waitForJobToFinishInJobManager(finished); + assertEquals(1, actualInstantiationRequest.getValue().getVims().size()); + //verify + OPENSTACKV3INFO actualVim = (OPENSTACKV3INFO) actualInstantiationRequest.getValue().getVims().get(0); + assertEquals(VIM_ID, actualVim.getId()); + assertEquals(VimInfo.VimInfoTypeEnum.OPENSTACK_V3_INFO, actualVim.getVimInfoType()); + assertEquals(Boolean.valueOf(parseBoolean(vimInfo.getSslInsecure())), actualVim.getInterfaceInfo().isSkipCertificateVerification()); + assertEquals("cloudUrl", actualVim.getInterfaceInfo().getEndpoint()); + //FIXME assertEquals();actualVim.getInterfaceInfo().getTrustedCertificates()); + assertEquals("vimPassword", actualVim.getAccessInfo().getPassword()); + assertEquals("regionId", actualVim.getAccessInfo().getRegion()); + assertEquals("myTenant", actualVim.getAccessInfo().getProject()); + assertEquals("myDomain", actualVim.getAccessInfo().getDomain()); + assertEquals("vimUsername", actualVim.getAccessInfo().getUsername()); + assertEquals(StoreLoader.getCertifacates(caCert).iterator().next(), new String(actualVim.getInterfaceInfo().getTrustedCertificates().get(0))); + assertTrue(!actualVim.getInterfaceInfo().isSkipCertificateVerification()); + assertTrue(!actualVim.getInterfaceInfo().isSkipCertificateHostnameCheck()); + } + + /** + * test instantiation with vcloud + */ + @Test + public void testInstantiationVcloud() throws Exception { + VnfInstantiateRequest instantiationRequest = prepareInstantiationRequest(VimInfo.VimInfoTypeEnum.VMWARE_VCLOUD_INFO); + + when(vnfApi.vnfsPost(createRequest.capture(), eq(NOKIA_LCM_API_VERSION))).thenReturn(vnfInfo); + additionalParam.setInstantiationLevel(INSTANTIATION_LEVEL); + when(vfcGrantManager.requestGrantForInstantiate(VNFM_ID, VNF_ID, VIM_ID, ONAP_CSAR_ID, INSTANTIATION_LEVEL, cbamVnfdContent, JOB_ID)).thenReturn(grantResponse); + grantResponse.setVimId(VIM_ID); + GrantVNFResponseVimAccessInfo accessInfo = new GrantVNFResponseVimAccessInfo(); + accessInfo.setTenant(TENANT); + grantResponse.setAccessInfo(accessInfo); + ArgumentCaptor<InstantiateVnfRequest> actualInstantiationRequest = ArgumentCaptor.forClass(InstantiateVnfRequest.class); + when(vnfApi.vnfsVnfInstanceIdInstantiatePost(eq(VNF_ID), actualInstantiationRequest.capture(), eq(NOKIA_LCM_API_VERSION))).thenReturn(instantiationOperationExecution); + //when + VnfInstantiateResponse response = lifecycleManager.instantiate(VNFM_ID, instantiationRequest, restResponse); + waitForJobToFinishInJobManager(finished); + assertEquals(1, actualInstantiationRequest.getValue().getVims().size()); + //verify + VMWAREVCLOUDINFO actualVim = (VMWAREVCLOUDINFO) actualInstantiationRequest.getValue().getVims().get(0); + assertEquals(VIM_ID, actualVim.getId()); + assertEquals(VimInfo.VimInfoTypeEnum.VMWARE_VCLOUD_INFO, actualVim.getVimInfoType()); + assertEquals(Boolean.valueOf(parseBoolean(vimInfo.getSslInsecure())), actualVim.getInterfaceInfo().isSkipCertificateVerification()); + assertEquals("cloudUrl", actualVim.getInterfaceInfo().getEndpoint()); + //FIXME assertEquals();actualVim.getInterfaceInfo().getTrustedCertificates()); + assertEquals("vimPassword", actualVim.getAccessInfo().getPassword()); + assertEquals("regionId", actualVim.getAccessInfo().getOrganization()); + assertEquals("vimUsername", actualVim.getAccessInfo().getUsername()); + assertTrue(actualVim.getInterfaceInfo().isSkipCertificateVerification()); + assertTrue(actualVim.getInterfaceInfo().isSkipCertificateHostnameCheck()); + } + + /** + * test instantiation with vCloud with SSL + */ + @Test + public void testInstantiationVcloudWithSsl() throws Exception { + VnfInstantiateRequest instantiationRequest = prepareInstantiationRequest(VimInfo.VimInfoTypeEnum.VMWARE_VCLOUD_INFO); + + when(vnfApi.vnfsPost(createRequest.capture(), eq(NOKIA_LCM_API_VERSION))).thenReturn(vnfInfo); + additionalParam.setInstantiationLevel(INSTANTIATION_LEVEL); + when(vfcGrantManager.requestGrantForInstantiate(VNFM_ID, VNF_ID, VIM_ID, ONAP_CSAR_ID, INSTANTIATION_LEVEL, cbamVnfdContent, JOB_ID)).thenReturn(grantResponse); + grantResponse.setVimId(VIM_ID); + GrantVNFResponseVimAccessInfo accessInfo = new GrantVNFResponseVimAccessInfo(); + accessInfo.setTenant(TENANT); + String caCert = new String(readAllBytes(Paths.get(TestVfcGrantManager.class.getResource("/unittests/localhost.cert.pem").toURI()))); + vimInfo.setSslInsecure("false"); + vimInfo.setSslCacert(caCert); + grantResponse.setAccessInfo(accessInfo); + ArgumentCaptor<InstantiateVnfRequest> actualInstantiationRequest = ArgumentCaptor.forClass(InstantiateVnfRequest.class); + when(vnfApi.vnfsVnfInstanceIdInstantiatePost(eq(VNF_ID), actualInstantiationRequest.capture(), eq(NOKIA_LCM_API_VERSION))).thenReturn(instantiationOperationExecution); + //when + VnfInstantiateResponse response = lifecycleManager.instantiate(VNFM_ID, instantiationRequest, restResponse); + waitForJobToFinishInJobManager(finished); + assertEquals(1, actualInstantiationRequest.getValue().getVims().size()); + //verify + VMWAREVCLOUDINFO actualVim = (VMWAREVCLOUDINFO) actualInstantiationRequest.getValue().getVims().get(0); + assertEquals(VIM_ID, actualVim.getId()); + assertEquals(VimInfo.VimInfoTypeEnum.VMWARE_VCLOUD_INFO, actualVim.getVimInfoType()); + assertEquals(Boolean.valueOf(parseBoolean(vimInfo.getSslInsecure())), actualVim.getInterfaceInfo().isSkipCertificateVerification()); + assertEquals("cloudUrl", actualVim.getInterfaceInfo().getEndpoint()); + //FIXME assertEquals();actualVim.getInterfaceInfo().getTrustedCertificates()); + assertEquals("vimPassword", actualVim.getAccessInfo().getPassword()); + assertEquals("regionId", actualVim.getAccessInfo().getOrganization()); + assertEquals("vimUsername", actualVim.getAccessInfo().getUsername()); + assertEquals(StoreLoader.getCertifacates(caCert).iterator().next(), new String(actualVim.getInterfaceInfo().getTrustedCertificates().get(0))); + assertTrue(!actualVim.getInterfaceInfo().isSkipCertificateVerification()); + assertTrue(!actualVim.getInterfaceInfo().isSkipCertificateHostnameCheck()); + } + + /** + * test failure in the instantiation request marks the job to be finished in job manager + */ + @Test + public void testFailureInTheInstantiationRequest() throws Exception { + VnfInstantiateRequest instantiationRequest = prepareInstantiationRequest(VimInfo.VimInfoTypeEnum.OPENSTACK_V2_INFO); + when(vnfApi.vnfsPost(createRequest.capture(), eq(NOKIA_LCM_API_VERSION))).thenReturn(vnfInfo); + additionalParam.setInstantiationLevel(INSTANTIATION_LEVEL); + when(vfcGrantManager.requestGrantForInstantiate(VNFM_ID, VNF_ID, VIM_ID, ONAP_CSAR_ID, INSTANTIATION_LEVEL, cbamVnfdContent, JOB_ID)).thenReturn(grantResponse); + grantResponse.setVimId(VIM_ID); + GrantVNFResponseVimAccessInfo accessInfo = new GrantVNFResponseVimAccessInfo(); + accessInfo.setTenant(TENANT); + grantResponse.setAccessInfo(accessInfo); + ArgumentCaptor<InstantiateVnfRequest> actualInstantiationRequest = ArgumentCaptor.forClass(InstantiateVnfRequest.class); + ApiException expectedException = new ApiException(); + when(vnfApi.vnfsVnfInstanceIdInstantiatePost(eq(VNF_ID), actualInstantiationRequest.capture(), eq(NOKIA_LCM_API_VERSION))).thenThrow(expectedException); + + //when + VnfInstantiateResponse response = lifecycleManager.instantiate(VNFM_ID, instantiationRequest, restResponse); + //verfiy + waitForJobToFinishInJobManager(finished); + assertEquals(VNF_ID, response.getVnfInstanceId()); + assertEquals(JOB_ID, response.getJobId()); + verify(logger).error("Unable to instantiate VNF with myVnfId identifier", expectedException); + } + + /** + * instantiation fails if VF-C does not send vim identifier in grant response + */ + @Test + public void testVfcFailsToSendVimId() throws Exception { + VnfInstantiateRequest instantiationRequest = prepareInstantiationRequest(VimInfo.VimInfoTypeEnum.OPENSTACK_V2_INFO); + + when(vnfApi.vnfsPost(createRequest.capture(), eq(NOKIA_LCM_API_VERSION))).thenReturn(vnfInfo); + additionalParam.setInstantiationLevel(INSTANTIATION_LEVEL); + when(vfcGrantManager.requestGrantForInstantiate(VNFM_ID, VNF_ID, VIM_ID, ONAP_CSAR_ID, INSTANTIATION_LEVEL, cbamVnfdContent, JOB_ID)).thenReturn(grantResponse); + //grantResponse.setVimId(VIM_ID); + GrantVNFResponseVimAccessInfo accessInfo = new GrantVNFResponseVimAccessInfo(); + accessInfo.setTenant(TENANT); + String caCert = new String(readAllBytes(Paths.get(TestVfcGrantManager.class.getResource("/unittests/localhost.cert.pem").toURI()))); + vimInfo.setSslInsecure("false"); + vimInfo.setSslCacert(caCert); + grantResponse.setAccessInfo(accessInfo); + ArgumentCaptor<InstantiateVnfRequest> actualInstantiationRequest = ArgumentCaptor.forClass(InstantiateVnfRequest.class); + when(vnfApi.vnfsVnfInstanceIdInstantiatePost(eq(VNF_ID), actualInstantiationRequest.capture(), eq(NOKIA_LCM_API_VERSION))).thenReturn(instantiationOperationExecution); + //when + VnfInstantiateResponse response = lifecycleManager.instantiate(VNFM_ID, instantiationRequest, restResponse); + waitForJobToFinishInJobManager(finished); + assertEquals(0, actualInstantiationRequest.getAllValues().size()); + //verify + verify(logger).error("VF-C did not send VIM identifier in grant response"); + + } + + /** + * test operation execution polling is retried in case of failures + */ + @Test + public void testFailureInTheOperationExecutionPollingDuringInstantiationRequest() throws Exception { + VnfInstantiateRequest instantiationRequest = prepareInstantiationRequest(VimInfo.VimInfoTypeEnum.OPENSTACK_V2_INFO); + when(vnfApi.vnfsPost(createRequest.capture(), eq(NOKIA_LCM_API_VERSION))).thenReturn(vnfInfo); + additionalParam.setInstantiationLevel(INSTANTIATION_LEVEL); + when(vfcGrantManager.requestGrantForInstantiate(VNFM_ID, VNF_ID, VIM_ID, ONAP_CSAR_ID, INSTANTIATION_LEVEL, cbamVnfdContent, JOB_ID)).thenReturn(grantResponse); + grantResponse.setVimId(VIM_ID); + GrantVNFResponseVimAccessInfo accessInfo = new GrantVNFResponseVimAccessInfo(); + accessInfo.setTenant(TENANT); + grantResponse.setAccessInfo(accessInfo); + ArgumentCaptor<InstantiateVnfRequest> actualInstantiationRequest = ArgumentCaptor.forClass(InstantiateVnfRequest.class); + ApiException expectedException = new ApiException(); + List<ApiException> polling = new ArrayList<>(); + when(vnfApi.vnfsVnfInstanceIdInstantiatePost(eq(VNF_ID), actualInstantiationRequest.capture(), eq(NOKIA_LCM_API_VERSION))).thenReturn(instantiationOperationExecution); + when(vnfApi.vnfsVnfInstanceIdOperationExecutionsGet(VNF_ID, NOKIA_LCM_API_VERSION)).then(new Answer<List<OperationExecution>>() { + @Override + public List<OperationExecution> answer(InvocationOnMock invocation) throws Throwable { + if (polling.size() > 2) { + return operationExecutions; + } + ApiException apiException = new ApiException(); + polling.add(apiException); + throw apiException; + } + }); + //when + VnfInstantiateResponse response = lifecycleManager.instantiate(VNFM_ID, instantiationRequest, restResponse); + //verfiy + waitForJobToFinishInJobManager(finished); + assertEquals(VNF_ID, response.getVnfInstanceId()); + assertEquals(JOB_ID, response.getJobId()); + assertEquals(3, polling.size()); + for (ApiException e : polling) { + verify(logger).warn("Unable to retrieve operations details", e); + } + verify(systemFunctions, Mockito.times(3)).sleep(5000); + } + + /** + * failure in VNF creation is logged an proagated + */ + @Test + public void failureInVnfCreationIsPropagated() throws Exception { + //given + VnfInstantiateRequest instantiationRequest = prepareInstantiationRequest(VimInfo.VimInfoTypeEnum.OPENSTACK_V2_INFO); + + ApiException expectedException = new ApiException(); + when(vnfApi.vnfsPost(createRequest.capture(), eq(NOKIA_LCM_API_VERSION))).thenThrow(expectedException); + //when + try { + lifecycleManager.instantiate(VNFM_ID, instantiationRequest, restResponse); + //verify + fail(); + } catch (RuntimeException e) { + assertEquals(expectedException, e.getCause()); + verify(logger).error("Unable to create the VNF", expectedException); + } + } + + /** + * failure in updating the modifyable attributes of the VNF is logged an proagated + */ + @Test + public void failureInVnfModificationIsPropagated() throws Exception { + //given + VnfInstantiateRequest instantiationRequest = prepareInstantiationRequest(VimInfo.VimInfoTypeEnum.OPENSTACK_V2_INFO); + + ApiException expectedException = new ApiException(); + when(vnfApi.vnfsPost(createRequest.capture(), eq(NOKIA_LCM_API_VERSION))).thenReturn(vnfInfo); + when(vnfApi.vnfsVnfInstanceIdPatch(eq(VNF_ID), actualVnfModifyRequest.capture(), eq(NOKIA_LCM_API_VERSION))).thenThrow(expectedException); + + //when + try { + lifecycleManager.instantiate(VNFM_ID, instantiationRequest, restResponse); + //verify + fail(); + } catch (RuntimeException e) { + assertEquals(expectedException, e.getCause().getCause()); + verify(logger).error("Unable to set the onapCsarId property on the VNF", expectedException); + } + } + + /** + * if the VIM info can not be queried the VNF is not instantiated and + * error propagated through job + */ + @Test + public void testFailureInQueryVimInfo() throws Exception { + VnfInstantiateRequest instantiationRequest = prepareInstantiationRequest(VimInfo.VimInfoTypeEnum.OPENSTACK_V2_INFO); + when(vnfApi.vnfsPost(createRequest.capture(), eq(NOKIA_LCM_API_VERSION))).thenReturn(vnfInfo); + when(vfcGrantManager.requestGrantForInstantiate(VNFM_ID, VNF_ID, VIM_ID, ONAP_CSAR_ID, INSTANTIATION_LEVEL, cbamVnfdContent, JOB_ID)).thenReturn(grantResponse); + grantResponse.setVimId(VIM_ID); + GrantVNFResponseVimAccessInfo accessInfo = new GrantVNFResponseVimAccessInfo(); + accessInfo.setTenant(TENANT); + grantResponse.setAccessInfo(accessInfo); + + when(vimInfoProvider.getVimInfo(VIM_ID)).thenThrow(new RuntimeException()); + //when + lifecycleManager.instantiate(VNFM_ID, instantiationRequest, restResponse); + //verify + waitForJobToFinishInJobManager(finished); + verify(vnfApi, never()).vnfsVnfInstanceIdInstantiatePost(Mockito.any(), Mockito.any(), Mockito.any()); + } + + /** + * test termination basic success scenario + * - the VNF is not deleted before the notifications are processed + */ + @Test + public void testTermination() throws Exception { + //given + VnfTerminateRequest terminationRequest = new VnfTerminateRequest(); + when(vnfApi.vnfsVnfInstanceIdGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenReturn(vnfInfo); + vnfInfo.setInstantiationState(InstantiationState.INSTANTIATED); + vnfInfo.setOperationExecutions(operationExecutions); + VnfProperty vnfdId = new VnfProperty(); + vnfdId.setName(LifecycleManager.ONAP_CSAR_ID); + vnfdId.setValue(ONAP_CSAR_ID); + vnfInfo.getExtensions().add(vnfdId); + ArgumentCaptor<TerminateVnfRequest> actualTerminationRequest = ArgumentCaptor.forClass(TerminateVnfRequest.class); + when(vnfApi.vnfsVnfInstanceIdTerminatePost(eq(VNF_ID), actualTerminationRequest.capture(), eq(NOKIA_LCM_API_VERSION))).thenAnswer(new Answer<OperationExecution>() { + @Override + public OperationExecution answer(InvocationOnMock invocation) throws Throwable { + OperationExecution terminationOperation = new OperationExecution(); + terminationOperation.setId("terminationId"); + operationExecutions.add(terminationOperation); + terminationOperation.setStatus(OperationStatus.FINISHED); + return terminationOperation; + } + }); + JsonElement instantiationParameters = new JsonParser().parse("{ \"vims\" : [ { \"id\" : \"" + VIM_ID + "\" } ] } "); + when(operationExecutionApi.operationExecutionsOperationExecutionIdOperationParamsGet("operationExecutionId", NOKIA_LCM_API_VERSION)).thenReturn(instantiationParameters); + //when + JobInfo jobInfo = lifecycleManager.terminateVnf(VNFM_ID, VNF_ID, terminationRequest, restResponse); + //verify + waitForJobToFinishInJobManager(finished); + assertEquals(1, actualTerminationRequest.getAllValues().size()); + assertEquals(TerminationType.FORCEFUL, actualTerminationRequest.getValue().getTerminationType()); + InOrder notificationIsProcessedBeforeDeletingTheVnf = Mockito.inOrder(vfcGrantManager, notificationManager, vnfApi); + notificationIsProcessedBeforeDeletingTheVnf.verify(vfcGrantManager).requestGrantForTerminate(VNFM_ID, VNF_ID, VIM_ID, ONAP_CSAR_ID, vnfInfo, JOB_ID); + notificationIsProcessedBeforeDeletingTheVnf.verify(notificationManager).waitForTerminationToBeProcessed("terminationId"); + notificationIsProcessedBeforeDeletingTheVnf.verify(vnfApi).vnfsVnfInstanceIdDelete(VNF_ID, NOKIA_LCM_API_VERSION); + verify(jobManager).spawnJob(VNF_ID, restResponse); + } + + /** + * test termination of a non instantiated VNF + * - the VNF is not terminated (only deleted) + */ + @Test + public void testTerminationOfNonInstantiated() throws Exception { + //given + VnfTerminateRequest terminationRequest = new VnfTerminateRequest(); + when(vnfApi.vnfsVnfInstanceIdGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenReturn(vnfInfo); + vnfInfo.setInstantiationState(InstantiationState.NOT_INSTANTIATED); + vnfInfo.setOperationExecutions(operationExecutions); + VnfProperty vnfdId = new VnfProperty(); + vnfdId.setName(LifecycleManager.ONAP_CSAR_ID); + vnfdId.setValue(ONAP_CSAR_ID); + vnfInfo.getExtensions().add(vnfdId); + JsonElement instantiationParameters = new JsonParser().parse("{ \"vims\" : [ { \"id\" : \"" + VIM_ID + "\" } ] } "); + when(operationExecutionApi.operationExecutionsOperationExecutionIdOperationParamsGet("operationExecutionId", NOKIA_LCM_API_VERSION)).thenReturn(instantiationParameters); + //when + JobInfo jobInfo = lifecycleManager.terminateVnf(VNFM_ID, VNF_ID, terminationRequest, restResponse); + //verify + boolean deleted = false; + while (!deleted) { + try { + Mockito. + verify(vnfApi).vnfsVnfInstanceIdDelete(VNF_ID, NOKIA_LCM_API_VERSION); + deleted = true; + } catch (Error e) { + } + } + verify(vfcGrantManager, never()).requestGrantForTerminate(VNFM_ID, VNF_ID, VIM_ID, ONAP_CSAR_ID, vnfInfo, JOB_ID); + verify(notificationManager, never()).waitForTerminationToBeProcessed("terminationId"); + } + + /** + * test that the VNF deletion is not started before the termination finishes + */ + @Test + public void testTerminationOperationIsOutwaited() throws Exception { + //given + VnfTerminateRequest terminationRequest = new VnfTerminateRequest(); + when(vnfApi.vnfsVnfInstanceIdGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenReturn(vnfInfo); + vnfInfo.setInstantiationState(InstantiationState.INSTANTIATED); + vnfInfo.setOperationExecutions(operationExecutions); + VnfProperty vnfdId = new VnfProperty(); + vnfdId.setName(LifecycleManager.ONAP_CSAR_ID); + vnfdId.setValue(ONAP_CSAR_ID); + vnfInfo.getExtensions().add(vnfdId); + ArgumentCaptor<TerminateVnfRequest> actualTerminationRequest = ArgumentCaptor.forClass(TerminateVnfRequest.class); + OperationExecution terminationOperation = new OperationExecution(); + when(vnfApi.vnfsVnfInstanceIdTerminatePost(eq(VNF_ID), actualTerminationRequest.capture(), eq(NOKIA_LCM_API_VERSION))).thenAnswer(new Answer<OperationExecution>() { + @Override + public OperationExecution answer(InvocationOnMock invocation) throws Throwable { + terminationOperation.setId("terminationId"); + operationExecutions.add(terminationOperation); + terminationOperation.setStatus(OperationStatus.STARTED); + return terminationOperation; + } + }); + JsonElement instantiationParameters = new JsonParser().parse("{ \"vims\" : [ { \"id\" : \"" + VIM_ID + "\" } ] } "); + when(operationExecutionApi.operationExecutionsOperationExecutionIdOperationParamsGet("operationExecutionId", NOKIA_LCM_API_VERSION)).thenReturn(instantiationParameters); + Set<Integer> calls = new HashSet<>(); + when(vnfApi.vnfsVnfInstanceIdOperationExecutionsGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenAnswer(new Answer<List<OperationExecution>>() { + @Override + public List<OperationExecution> answer(InvocationOnMock invocation) throws Throwable { + if (calls.size() == 1000) { + terminationOperation.setStatus(OperationStatus.FINISHED); + } + calls.add(calls.size()); + return operationExecutions; + } + }); + //when + JobInfo jobInfo = lifecycleManager.terminateVnf(VNFM_ID, VNF_ID, terminationRequest, restResponse); + //verify + waitForJobToFinishInJobManager(finished); + verify(vnfApi, times(1001)).vnfsVnfInstanceIdOperationExecutionsGet(VNF_ID, NOKIA_LCM_API_VERSION); + verify(systemFunctions, times(1000)).sleep(5000); + } + + + /** + * test that failured during waiting for the operation to finish is tolerated (idefineiatelly) + */ + @Test + public void testTerminationOperationIsOutwaitedWithErrors() throws Exception { + //given + VnfTerminateRequest terminationRequest = new VnfTerminateRequest(); + when(vnfApi.vnfsVnfInstanceIdGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenReturn(vnfInfo); + vnfInfo.setInstantiationState(InstantiationState.INSTANTIATED); + vnfInfo.setOperationExecutions(operationExecutions); + VnfProperty vnfdId = new VnfProperty(); + vnfdId.setName(LifecycleManager.ONAP_CSAR_ID); + vnfdId.setValue(ONAP_CSAR_ID); + vnfInfo.getExtensions().add(vnfdId); + ArgumentCaptor<TerminateVnfRequest> actualTerminationRequest = ArgumentCaptor.forClass(TerminateVnfRequest.class); + OperationExecution terminationOperation = new OperationExecution(); + when(vnfApi.vnfsVnfInstanceIdTerminatePost(eq(VNF_ID), actualTerminationRequest.capture(), eq(NOKIA_LCM_API_VERSION))).thenAnswer(new Answer<OperationExecution>() { + @Override + public OperationExecution answer(InvocationOnMock invocation) throws Throwable { + terminationOperation.setId("terminationId"); + operationExecutions.add(terminationOperation); + terminationOperation.setStatus(OperationStatus.STARTED); + return terminationOperation; + } + }); + JsonElement instantiationParameters = new JsonParser().parse("{ \"vims\" : [ { \"id\" : \"" + VIM_ID + "\" } ] } "); + when(operationExecutionApi.operationExecutionsOperationExecutionIdOperationParamsGet("operationExecutionId", NOKIA_LCM_API_VERSION)).thenReturn(instantiationParameters); + Set<Integer> calls = new HashSet<>(); + List<ApiException> expectedExceptions = new ArrayList<>(); + when(vnfApi.vnfsVnfInstanceIdOperationExecutionsGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenAnswer(new Answer<List<OperationExecution>>() { + @Override + public List<OperationExecution> answer(InvocationOnMock invocation) throws Throwable { + if (calls.size() >= 100) { + terminationOperation.setStatus(OperationStatus.FINISHED); + return operationExecutions; + } + calls.add(calls.size()); + ApiException apiException = new ApiException(); + expectedExceptions.add(apiException); + throw apiException; + } + }); + //when + JobInfo jobInfo = lifecycleManager.terminateVnf(VNFM_ID, VNF_ID, terminationRequest, restResponse); + //verify + waitForJobToFinishInJobManager(finished); + verify(vnfApi, times(101)).vnfsVnfInstanceIdOperationExecutionsGet(VNF_ID, NOKIA_LCM_API_VERSION); + verify(systemFunctions, times(100)).sleep(5000); + for (ApiException expectedException : expectedExceptions) { + verify(logger).warn("Unable to retrieve operations details", expectedException); + } + } + + /** + * test gracefull termination + */ + @Test + public void testGracefullTermination() throws Exception { + //given + VnfTerminateRequest terminationRequest = new VnfTerminateRequest(); + when(vnfApi.vnfsVnfInstanceIdGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenReturn(vnfInfo); + terminationRequest.setTerminationType(VnfTerminationType.GRACEFUL); + terminationRequest.setGracefulTerminationTimeout("1234"); + vnfInfo.setInstantiationState(InstantiationState.INSTANTIATED); + vnfInfo.setOperationExecutions(operationExecutions); + VnfProperty vnfdId = new VnfProperty(); + vnfdId.setName(LifecycleManager.ONAP_CSAR_ID); + vnfdId.setValue(ONAP_CSAR_ID); + vnfInfo.getExtensions().add(vnfdId); + ArgumentCaptor<TerminateVnfRequest> actualTerminationRequest = ArgumentCaptor.forClass(TerminateVnfRequest.class); + when(vnfApi.vnfsVnfInstanceIdTerminatePost(eq(VNF_ID), actualTerminationRequest.capture(), eq(NOKIA_LCM_API_VERSION))).thenAnswer(new Answer<OperationExecution>() { + @Override + public OperationExecution answer(InvocationOnMock invocation) throws Throwable { + OperationExecution terminationOperation = new OperationExecution(); + terminationOperation.setId("terminationId"); + operationExecutions.add(terminationOperation); + terminationOperation.setStatus(OperationStatus.FINISHED); + return terminationOperation; + } + }); + doAnswer(invocation -> { + verify(jobManager, Mockito.never()).jobFinished(JOB_ID); + return null; + }).when(vnfApi).vnfsVnfInstanceIdDelete(VNF_ID, NOKIA_LCM_API_VERSION); + JsonElement instantiationParameters = new JsonParser().parse("{ \"vims\" : [ { \"id\" : \"" + VIM_ID + "\" } ] } "); + when(operationExecutionApi.operationExecutionsOperationExecutionIdOperationParamsGet("operationExecutionId", NOKIA_LCM_API_VERSION)).thenReturn(instantiationParameters); + //when + JobInfo jobInfo = lifecycleManager.terminateVnf(VNFM_ID, VNF_ID, terminationRequest, restResponse); + //verify + waitForJobToFinishInJobManager(finished); + assertEquals(1, actualTerminationRequest.getAllValues().size()); + assertEquals(TerminationType.GRACEFUL, actualTerminationRequest.getValue().getTerminationType()); + assertEquals(Integer.valueOf(1234), actualTerminationRequest.getValue().getGracefulTerminationTimeout()); + InOrder notificationIsProcessedBeforeDeletingTheVnf = Mockito.inOrder(vfcGrantManager, notificationManager, vnfApi); + notificationIsProcessedBeforeDeletingTheVnf.verify(vfcGrantManager).requestGrantForTerminate(VNFM_ID, VNF_ID, VIM_ID, ONAP_CSAR_ID, vnfInfo, JOB_ID); + notificationIsProcessedBeforeDeletingTheVnf.verify(notificationManager).waitForTerminationToBeProcessed("terminationId"); + notificationIsProcessedBeforeDeletingTheVnf.verify(vnfApi).vnfsVnfInstanceIdDelete(VNF_ID, NOKIA_LCM_API_VERSION); + } + + /** + * instantiation with missing ONAP csarId to instantiation extra param result in failure + */ + @Test + public void testMissingVnfParameters() throws Exception { + //given + VnfInstantiateRequest instantiationRequest = prepareInstantiationRequest(VimInfo.VimInfoTypeEnum.OPENSTACK_V2_INFO); + String src = "{ \"inputs\" : { \"vnfs\" : { \"" + ONAP_CSAR_ID + "invalid" + "\" : {}}}, \"vimId\" : \"" + VIM_ID + "\"}"; + instantiationRequest.setAdditionalParam(new JsonParser().parse(src)); + //when + try { + VnfInstantiateResponse response = lifecycleManager.instantiate(VNFM_ID, instantiationRequest, restResponse); + fail(); + } catch (Exception e) { + assertEquals("The additional parameter section does not contain setting for VNF with myOnapCsarId CSAR id", e.getMessage()); + verify(logger).error("The additional parameter section does not contain setting for VNF with myOnapCsarId CSAR id"); + } + } + + /** + * test explicit forceful termination + */ + @Test + public void testExplicitForcefulTermination() throws Exception { + //given + VnfTerminateRequest terminationRequest = new VnfTerminateRequest(); + when(vnfApi.vnfsVnfInstanceIdGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenReturn(vnfInfo); + terminationRequest.setTerminationType(VnfTerminationType.FORCEFUL); + terminationRequest.setGracefulTerminationTimeout("1234"); + vnfInfo.setInstantiationState(InstantiationState.INSTANTIATED); + vnfInfo.setOperationExecutions(operationExecutions); + VnfProperty vnfdId = new VnfProperty(); + vnfdId.setName(LifecycleManager.ONAP_CSAR_ID); + vnfdId.setValue(ONAP_CSAR_ID); + vnfInfo.getExtensions().add(vnfdId); + ArgumentCaptor<TerminateVnfRequest> actualTerminationRequest = ArgumentCaptor.forClass(TerminateVnfRequest.class); + when(vnfApi.vnfsVnfInstanceIdTerminatePost(eq(VNF_ID), actualTerminationRequest.capture(), eq(NOKIA_LCM_API_VERSION))).thenAnswer(new Answer<OperationExecution>() { + @Override + public OperationExecution answer(InvocationOnMock invocation) throws Throwable { + OperationExecution terminationOperation = new OperationExecution(); + terminationOperation.setId("terminationId"); + operationExecutions.add(terminationOperation); + terminationOperation.setStatus(OperationStatus.FINISHED); + return terminationOperation; + } + }); + doAnswer(invocation -> { + verify(jobManager, Mockito.never()).jobFinished(JOB_ID); + return null; + }).when(vnfApi).vnfsVnfInstanceIdDelete(VNF_ID, NOKIA_LCM_API_VERSION); + JsonElement instantiationParameters = new JsonParser().parse("{ \"vims\" : [ { \"id\" : \"" + VIM_ID + "\" } ] } "); + when(operationExecutionApi.operationExecutionsOperationExecutionIdOperationParamsGet("operationExecutionId", NOKIA_LCM_API_VERSION)).thenReturn(instantiationParameters); + //when + JobInfo jobInfo = lifecycleManager.terminateVnf(VNFM_ID, VNF_ID, terminationRequest, restResponse); + //verify + waitForJobToFinishInJobManager(finished); + assertEquals(1, actualTerminationRequest.getAllValues().size()); + assertEquals(TerminationType.FORCEFUL, actualTerminationRequest.getValue().getTerminationType()); + assertNull(actualTerminationRequest.getValue().getGracefulTerminationTimeout()); + InOrder notificationIsProcessedBeforeDeletingTheVnf = Mockito.inOrder(vfcGrantManager, notificationManager, vnfApi); + notificationIsProcessedBeforeDeletingTheVnf.verify(vfcGrantManager).requestGrantForTerminate(VNFM_ID, VNF_ID, VIM_ID, ONAP_CSAR_ID, vnfInfo, JOB_ID); + notificationIsProcessedBeforeDeletingTheVnf.verify(notificationManager).waitForTerminationToBeProcessed("terminationId"); + notificationIsProcessedBeforeDeletingTheVnf.verify(vnfApi).vnfsVnfInstanceIdDelete(VNF_ID, NOKIA_LCM_API_VERSION); + } + + /** + * test failure in the termination workflow finishes the job + */ + @Test + public void testFailureInTheTerminationFinishesTheManagedJob() throws Exception { + //given + VnfTerminateRequest terminationRequest = new VnfTerminateRequest(); + when(vnfApi.vnfsVnfInstanceIdGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenReturn(vnfInfo); + terminationRequest.setTerminationType(VnfTerminationType.FORCEFUL); + terminationRequest.setGracefulTerminationTimeout("1234"); + vnfInfo.setInstantiationState(InstantiationState.INSTANTIATED); + vnfInfo.setOperationExecutions(operationExecutions); + VnfProperty vnfdId = new VnfProperty(); + vnfdId.setName(LifecycleManager.ONAP_CSAR_ID); + vnfdId.setValue(ONAP_CSAR_ID); + vnfInfo.getExtensions().add(vnfdId); + ArgumentCaptor<TerminateVnfRequest> actualTerminationRequest = ArgumentCaptor.forClass(TerminateVnfRequest.class); + when(vnfApi.vnfsVnfInstanceIdTerminatePost(eq(VNF_ID), actualTerminationRequest.capture(), eq(NOKIA_LCM_API_VERSION))).thenAnswer(new Answer<OperationExecution>() { + @Override + public OperationExecution answer(InvocationOnMock invocation) throws Throwable { + OperationExecution terminationOperation = new OperationExecution(); + terminationOperation.setId("terminationId"); + operationExecutions.add(terminationOperation); + terminationOperation.setStatus(OperationStatus.FINISHED); + return terminationOperation; + } + }); + ApiException expectedException = new ApiException(); + when(vnfApi.vnfsVnfInstanceIdGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenThrow(expectedException); + JsonElement instantiationParameters = new JsonParser().parse("{ \"vims\" : [ { \"id\" : \"" + VIM_ID + "\" } ] } "); + when(operationExecutionApi.operationExecutionsOperationExecutionIdOperationParamsGet("operationExecutionId", NOKIA_LCM_API_VERSION)).thenReturn(instantiationParameters); + //when + JobInfo jobInfo = lifecycleManager.terminateVnf(VNFM_ID, VNF_ID, terminationRequest, restResponse); + //verify + waitForJobToFinishInJobManager(finished); + assertEquals(0, actualTerminationRequest.getAllValues().size()); + Mockito.verifyZeroInteractions(vfcGrantManager); + } + + /** + * if termination fails the VNF is not deleted + */ + @Test + public void testFailedTerminationAbortsTerminationWorkflow() throws Exception { + //given + VnfTerminateRequest terminationRequest = new VnfTerminateRequest(); + when(vnfApi.vnfsVnfInstanceIdGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenReturn(vnfInfo); + vnfInfo.setInstantiationState(InstantiationState.INSTANTIATED); + vnfInfo.setOperationExecutions(operationExecutions); + VnfProperty vnfdId = new VnfProperty(); + vnfdId.setName(LifecycleManager.ONAP_CSAR_ID); + vnfdId.setValue(ONAP_CSAR_ID); + vnfInfo.getExtensions().add(vnfdId); + ArgumentCaptor<TerminateVnfRequest> actualTerminationRequest = ArgumentCaptor.forClass(TerminateVnfRequest.class); + when(vnfApi.vnfsVnfInstanceIdTerminatePost(eq(VNF_ID), actualTerminationRequest.capture(), eq(NOKIA_LCM_API_VERSION))).thenAnswer(new Answer<OperationExecution>() { + @Override + public OperationExecution answer(InvocationOnMock invocation) throws Throwable { + OperationExecution terminationOperation = new OperationExecution(); + terminationOperation.setId("terminationId"); + operationExecutions.add(terminationOperation); + terminationOperation.setStatus(OperationStatus.FAILED); + return terminationOperation; + } + }); + JsonElement instantiationParameters = new JsonParser().parse("{ \"vims\" : [ { \"id\" : \"" + VIM_ID + "\" } ] } "); + when(operationExecutionApi.operationExecutionsOperationExecutionIdOperationParamsGet("operationExecutionId", NOKIA_LCM_API_VERSION)).thenReturn(instantiationParameters); + //when + JobInfo jobInfo = lifecycleManager.terminateVnf(VNFM_ID, VNF_ID, terminationRequest, restResponse); + //verify + waitForJobToFinishInJobManager(finished); + assertEquals(1, actualTerminationRequest.getAllValues().size()); + assertEquals(TerminationType.FORCEFUL, actualTerminationRequest.getValue().getTerminationType()); + verify(vfcGrantManager).requestGrantForTerminate(VNFM_ID, VNF_ID, VIM_ID, ONAP_CSAR_ID, vnfInfo, JOB_ID); + verify(vnfApi, never()).vnfsVnfInstanceIdDelete(VNF_ID, NOKIA_LCM_API_VERSION); + verify(logger).error("Unable to terminate VNF the operation did not finish with success"); + } + + /** + * test VNF query basic success scenario + */ + @Test + public void testQuery() throws Exception { + vnfInfo.setDescription("myDescription"); + vnfInfo.setName("myName"); + vnfInfo.setVnfSoftwareVersion("vnfSoftwareVersion"); + vnfInfo.setVnfProvider("myProvider"); + when(vnfApi.vnfsVnfInstanceIdGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenReturn(vnfInfo); + VnfProperty prop = new VnfProperty(); + prop.setName(LifecycleManager.ONAP_CSAR_ID); + prop.setValue(ONAP_CSAR_ID); + vnfInfo.getExtensions().add(prop); + //when + org.onap.vnfmdriver.model.VnfInfo vnf = lifecycleManager.queryVnf(VNFM_ID, VNF_ID); + //verify + assertEquals(VNF_ID, vnf.getVnfInstanceId()); + //FIXME ? (do not know what exactly the vnf version mean in core terminology) + assertEquals("vnfSoftwareVersion", vnf.getVersion()); + assertEquals(ONAP_CSAR_ID, vnf.getVnfdId()); + assertEquals("myDescription", vnf.getVnfInstanceDescription()); + assertEquals("myName", vnf.getVnfInstanceName()); + assertEquals(ONAP_CSAR_ID, vnf.getVnfPackageId()); + assertEquals("myProvider", vnf.getVnfProvider()); + //FIXME (in swagger schema ) + assertEquals("ACTIVE", vnf.getVnfStatus()); + assertEquals("Kuku", vnf.getVnfType()); + } + + /** + * error is propagated and logged if the queried VNF does not exist + */ + @Test + public void testQueryForNonExistingVnf() throws Exception { + + ApiException expectedException = new ApiException(); + when(vnfApi.vnfsVnfInstanceIdGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenThrow(expectedException); + //when + try { + lifecycleManager.queryVnf(VNFM_ID, VNF_ID); + //verify + fail(); + } catch (Exception e) { + verify(logger).error("Unable to query VNF (myVnfId)", expectedException); + assertEquals(expectedException, e.getCause()); + } + } + + /** + * test scale basic scenario + */ + @Test + public void testScale() throws Exception { + VnfScaleRequest scaleRequest = new VnfScaleRequest(); + scaleRequest.setNumberOfSteps("2"); + scaleRequest.setAspectId("myAspect"); + scaleRequest.setType(ScaleDirection.IN); + scaleRequest.setAdditionalParam(new JsonParser().parse("{ \"a\" : \"b\", \"c\" : \"d\" }")); + scaleOperationExecution.setStatus(OperationStatus.FINISHED); + when(vnfApi.vnfsVnfInstanceIdGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenReturn(vnfInfo); + VnfProperty prop = new VnfProperty(); + prop.setValue(ONAP_CSAR_ID); + prop.setName(LifecycleManager.ONAP_CSAR_ID); + vnfInfo.getExtensions().add(prop); + vnfInfo.getOperationExecutions().add(instantiationOperationExecution); + String instantiationParams = "{ \"vims\" : [ { \"id\" : \"" + VIM_ID + "\" } ] }"; + when(operationExecutionApi.operationExecutionsOperationExecutionIdOperationParamsGet(instantiationOperationExecution.getId(), NOKIA_LCM_API_VERSION)).thenReturn(new JsonParser().parse(instantiationParams)); + //when + JobInfo job = lifecycleManager.scaleVnf(VNFM_ID, VNF_ID, scaleRequest, restResponse); + //verify + waitForJobToFinishInJobManager(finished); + assertEquals(1, actualScaleRequest.getAllValues().size()); + ScaleVnfRequest sRequest = actualScaleRequest.getValue(); + InOrder workflowOrder = Mockito.inOrder(vfcGrantManager, vnfApi); + workflowOrder.verify(vfcGrantManager).requestGrantForScale(eq(VNFM_ID), eq(VNF_ID), eq(VIM_ID), eq(ONAP_CSAR_ID), eq(scaleRequest), eq(JOB_ID)); + workflowOrder.verify(vnfApi).vnfsVnfInstanceIdScalePost(VNF_ID, sRequest, NOKIA_LCM_API_VERSION); + assertEquals("myAspect", sRequest.getAspectId()); + assertEquals(com.nokia.cbam.lcm.v32.model.ScaleDirection.IN, sRequest.getType()); + assertEquals(Integer.valueOf(2), sRequest.getNumberOfSteps()); + assertTrue("{\"jobId\":\"myJobId\",\"a\":\"b\"}".equals(new Gson().toJson(sRequest.getAdditionalParams())) || "{\"a\":\"b\",\"jobId\":\"myJobId\"}".equals(new Gson().toJson(sRequest.getAdditionalParams()))); + verify(jobManager).spawnJob(VNF_ID, restResponse); + } + + /** + * test scale out basic scenario + */ + @Test + public void testScaleOut() throws Exception { + VnfScaleRequest scaleRequest = new VnfScaleRequest(); + scaleRequest.setNumberOfSteps("2"); + scaleRequest.setAspectId("myAspect"); + scaleRequest.setType(ScaleDirection.OUT); + scaleRequest.setAdditionalParam(new JsonParser().parse("{ \"a\" : \"b\" }")); + scaleOperationExecution.setStatus(OperationStatus.FINISHED); + when(vnfApi.vnfsVnfInstanceIdGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenReturn(vnfInfo); + VnfProperty prop = new VnfProperty(); + prop.setValue(ONAP_CSAR_ID); + prop.setName(LifecycleManager.ONAP_CSAR_ID); + vnfInfo.getExtensions().add(prop); + vnfInfo.getOperationExecutions().add(instantiationOperationExecution); + String instantiationParams = "{ \"vims\" : [ { \"id\" : \"" + VIM_ID + "\" } ] }"; + when(operationExecutionApi.operationExecutionsOperationExecutionIdOperationParamsGet(instantiationOperationExecution.getId(), NOKIA_LCM_API_VERSION)).thenReturn(new JsonParser().parse(instantiationParams)); + //when + JobInfo job = lifecycleManager.scaleVnf(VNFM_ID, VNF_ID, scaleRequest, restResponse); + //verify + waitForJobToFinishInJobManager(finished); + assertEquals(1, actualScaleRequest.getAllValues().size()); + ScaleVnfRequest sRequest = actualScaleRequest.getValue(); + InOrder workflowOrder = Mockito.inOrder(vfcGrantManager, vnfApi); + workflowOrder.verify(vfcGrantManager).requestGrantForScale(eq(VNFM_ID), eq(VNF_ID), eq(VIM_ID), eq(ONAP_CSAR_ID), eq(scaleRequest), eq(JOB_ID)); + workflowOrder.verify(vnfApi).vnfsVnfInstanceIdScalePost(VNF_ID, sRequest, NOKIA_LCM_API_VERSION); + assertEquals("myAspect", sRequest.getAspectId()); + assertEquals(com.nokia.cbam.lcm.v32.model.ScaleDirection.OUT, sRequest.getType()); + assertEquals(Integer.valueOf(2), sRequest.getNumberOfSteps()); + assertTrue("{\"jobId\":\"myJobId\",\"a\":\"b\"}".equals(new Gson().toJson(sRequest.getAdditionalParams())) || "{\"a\":\"b\",\"jobId\":\"myJobId\"}".equals(new Gson().toJson(sRequest.getAdditionalParams()))); + } + + /** + * test scale operation is out waited + */ + @Test + public void testScaleOutwait() throws Exception { + VnfScaleRequest scaleRequest = new VnfScaleRequest(); + scaleRequest.setNumberOfSteps("2"); + scaleRequest.setAspectId("myAspect"); + scaleRequest.setType(ScaleDirection.IN); + scaleRequest.setAdditionalParam(new JsonParser().parse("{ \"a\" : \"b\" }")); + scaleOperationExecution.setStatus(OperationStatus.STARTED); + when(vnfApi.vnfsVnfInstanceIdGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenReturn(vnfInfo); + VnfProperty prop = new VnfProperty(); + prop.setValue(ONAP_CSAR_ID); + prop.setName(LifecycleManager.ONAP_CSAR_ID); + vnfInfo.getExtensions().add(prop); + vnfInfo.getOperationExecutions().add(instantiationOperationExecution); + String instantiationParams = "{ \"vims\" : [ { \"id\" : \"" + VIM_ID + "\" } ] }"; + when(operationExecutionApi.operationExecutionsOperationExecutionIdOperationParamsGet(instantiationOperationExecution.getId(), NOKIA_LCM_API_VERSION)).thenReturn(new JsonParser().parse(instantiationParams)); + List<ApiException> expectedExceptions = new ArrayList<>(); + when(vnfApi.vnfsVnfInstanceIdOperationExecutionsGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenAnswer((Answer<List<OperationExecution>>) invocation -> { + if (expectedExceptions.size() >= 100) { + scaleOperationExecution.setStatus(OperationStatus.FINISHED); + return operationExecutions; + } + ApiException apiException = new ApiException(); + expectedExceptions.add(apiException); + // when(operationExecutionApi.operationExecutionsOperationExecutionIdGet(scaleOperationExecution.getId(), NOKIA_LCM_API_VERSION)).thenReturn(scaleOperationExecution); + throw apiException; + }); + + //when + JobInfo job = lifecycleManager.scaleVnf(VNFM_ID, VNF_ID, scaleRequest, restResponse); + //verify + waitForJobToFinishInJobManager(finished); + assertEquals(100, expectedExceptions.size()); + for (ApiException expectedException : expectedExceptions) { + verify(logger).warn("Unable to retrieve operations details", expectedException); + } + verify(systemFunctions, times(100)).sleep(5000); + } + + /** + * test scale failure propagation + */ + @Test + public void testScaleFailurePropagation() throws Exception { + ApiException expectedException = new ApiException(); + VnfScaleRequest scaleRequest = new VnfScaleRequest(); + scaleRequest.setNumberOfSteps("2"); + scaleRequest.setAspectId("myAspect"); + scaleRequest.setType(ScaleDirection.IN); + when(vnfApi.vnfsVnfInstanceIdGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenThrow(expectedException); + //when + lifecycleManager.scaleVnf(VNFM_ID, VNF_ID, scaleRequest, restResponse); + //verify + waitForJobToFinishInJobManager(finished); + verify(logger).error("Unable to scale VNF with myVnfId identifier", expectedException); + } + + /** + * test heal basic scenario + */ + @Test + public void testHeal() throws Exception { + VnfHealRequest healRequest = new VnfHealRequest(); + healRequest.setAction("myAction"); + VnfHealRequestAffectedvm affectedVm = new VnfHealRequestAffectedvm(); + affectedVm.setVmname("vmName"); + healRequest.setAffectedvm(affectedVm); + healOperationExecution.setStatus(OperationStatus.FINISHED); + when(vnfApi.vnfsVnfInstanceIdGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenReturn(vnfInfo); + VnfProperty prop = new VnfProperty(); + prop.setValue(ONAP_CSAR_ID); + prop.setName(LifecycleManager.ONAP_CSAR_ID); + vnfInfo.getExtensions().add(prop); + vnfInfo.getOperationExecutions().add(instantiationOperationExecution); + String instantiationParams = "{ \"vims\" : [ { \"id\" : \"" + VIM_ID + "\" } ] }"; + when(operationExecutionApi.operationExecutionsOperationExecutionIdOperationParamsGet(instantiationOperationExecution.getId(), NOKIA_LCM_API_VERSION)).thenReturn(new JsonParser().parse(instantiationParams)); + //when + JobInfo job = lifecycleManager.healVnf(VNFM_ID, VNF_ID, healRequest, restResponse); + //verify + waitForJobToFinishInJobManager(finished); + assertEquals(1, actualHealRequest.getAllValues().size()); + HealVnfRequest sRequest = actualHealRequest.getValue(); + InOrder workflowOrder = Mockito.inOrder(vfcGrantManager, vnfApi); + workflowOrder.verify(vfcGrantManager).requestGrantForHeal(eq(VNFM_ID), eq(VNF_ID), eq(VIM_ID), eq(ONAP_CSAR_ID), eq(healRequest), eq(JOB_ID)); + workflowOrder.verify(vnfApi).vnfsVnfInstanceIdHealPost(VNF_ID, sRequest, NOKIA_LCM_API_VERSION); + JsonObject root = new Gson().toJsonTree(sRequest.getAdditionalParams()).getAsJsonObject(); + assertEquals("myAction", root.get("action").getAsString()); + assertEquals("vmName", root.get("vmName").getAsString()); + assertEquals(JOB_ID, root.get("jobId").getAsString()); + verify(jobManager).spawnJob(VNF_ID, restResponse); + } + + /** + * test heal operation is out waited + */ + @Test + public void testHealOutwait() throws Exception { + VnfHealRequest healRequest = new VnfHealRequest(); + healRequest.setAction("myAction"); + VnfHealRequestAffectedvm affectedVm = new VnfHealRequestAffectedvm(); + affectedVm.setVmname("vmName"); + healRequest.setAffectedvm(affectedVm); + healOperationExecution.setStatus(OperationStatus.FINISHED); + when(vnfApi.vnfsVnfInstanceIdGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenReturn(vnfInfo); + VnfProperty prop = new VnfProperty(); + prop.setValue(ONAP_CSAR_ID); + prop.setName(LifecycleManager.ONAP_CSAR_ID); + vnfInfo.getExtensions().add(prop); + vnfInfo.getOperationExecutions().add(instantiationOperationExecution); + String instantiationParams = "{ \"vims\" : [ { \"id\" : \"" + VIM_ID + "\" } ] }"; + when(operationExecutionApi.operationExecutionsOperationExecutionIdOperationParamsGet(instantiationOperationExecution.getId(), NOKIA_LCM_API_VERSION)).thenReturn(new JsonParser().parse(instantiationParams)); + List<ApiException> expectedExceptions = new ArrayList<>(); + when(vnfApi.vnfsVnfInstanceIdOperationExecutionsGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenAnswer(new Answer<List<OperationExecution>>() { + @Override + public List<OperationExecution> answer(InvocationOnMock invocation) throws Throwable { + if (expectedExceptions.size() >= 100) { + scaleOperationExecution.setStatus(OperationStatus.FINISHED); + return operationExecutions; + } + ApiException apiException = new ApiException(); + expectedExceptions.add(apiException); + throw apiException; + } + }); + //when + JobInfo job = lifecycleManager.healVnf(VNFM_ID, VNF_ID, healRequest, restResponse); + //verify + waitForJobToFinishInJobManager(finished); + assertEquals(100, expectedExceptions.size()); + for (ApiException expectedException : expectedExceptions) { + verify(logger).warn("Unable to retrieve operations details", expectedException); + } + verify(systemFunctions, times(100)).sleep(5000); + } + + /** + * failure in heal propagates in error + */ + @Test + public void testHealFailurePropagation() throws Exception { + ApiException expectedException = new ApiException(); + VnfHealRequest healRequest = new VnfHealRequest(); + healRequest.setAction("myAction"); + VnfHealRequestAffectedvm affectedVm = new VnfHealRequestAffectedvm(); + affectedVm.setVmname("vmName"); + healRequest.setAffectedvm(affectedVm); + when(vnfApi.vnfsVnfInstanceIdGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenThrow(expectedException); + //when + JobInfo job = lifecycleManager.healVnf(VNFM_ID, VNF_ID, healRequest, restResponse); + //verify + waitForJobToFinishInJobManager(finished); + verify(logger).error("Unable to heal VNF with myVnfId identifier", expectedException); + } + + private void waitForJobToFinishInJobManager(Set<Boolean> finished) throws InterruptedException { + while (finished.size() == 0) { + systemFunctions().sleep(100); + } + } + + private VnfInstantiateRequest prepareInstantiationRequest(VimInfo.VimInfoTypeEnum cloudType) { + VnfInstantiateRequest instantiationRequest = new VnfInstantiateRequest(); + instantiationRequest.setVnfPackageId(ONAP_CSAR_ID); + instantiationRequest.setVnfDescriptorId(ONAP_CSAR_ID); + instantiationRequest.setVnfInstanceDescription("myDescription"); + instantiationRequest.setVnfInstanceName("vnfName"); + additionalParam.setInstantiationLevel("level1"); + switch (cloudType) { + case OPENSTACK_V2_INFO: + additionalParam.setVimType(VimInfo.VimInfoTypeEnum.OPENSTACK_V2_INFO); + break; + case OPENSTACK_V3_INFO: + additionalParam.setVimType(VimInfo.VimInfoTypeEnum.OPENSTACK_V3_INFO); + additionalParam.setDomain("myDomain"); + break; + case VMWARE_VCLOUD_INFO: + additionalParam.setVimType(VimInfo.VimInfoTypeEnum.VMWARE_VCLOUD_INFO); + break; + default: + additionalParam.setVimType(VimInfo.VimInfoTypeEnum.OTHER_VIM_INFO); + } + + Map<String, List<NetworkAddress>> exteranalConnectionPointAddresses = new HashMap<>(); + exteranalConnectionPointAddresses.put("ecp1", new ArrayList<>()); + NetworkAddress networkAddress = new NetworkAddress(); + networkAddress.setIp("1.2.3.4"); + networkAddress.setMac("mac"); + networkAddress.setSubnetId("subnetId"); + exteranalConnectionPointAddresses.get("ecp1").add(networkAddress); + additionalParam.setExternalConnectionPointAddresses(exteranalConnectionPointAddresses); + VimComputeResourceFlavour flavor = new VimComputeResourceFlavour(); + flavor.setResourceId("flavourProviderId"); + flavor.setVimId(VIM_ID); + flavor.setVnfdVirtualComputeDescId("virtualComputeDescId"); + additionalParam.getComputeResourceFlavours().add(flavor); + ExtVirtualLinkData evl = new ExtVirtualLinkData(); + evl.setResourceId("networkProviderId1"); + evl.setVimId(VIM_ID); + evl.setExtVirtualLinkId("evlId1"); + VnfExtCpData ecp2 = new VnfExtCpData(); + ecp2.setCpdId("cpdId3"); + ecp2.setAddresses(new ArrayList<>()); + ecp2.getAddresses().add(networkAddress); + ecp2.setNumDynamicAddresses(2); + evl.getExtCps().add(ecp2); + additionalParam.getExtVirtualLinks().add(evl); + externalVirtualLink.setCpdId("myCpdId"); + externalVirtualLink.setResourceId("myNetworkProviderId"); + externalVirtualLink.setVlInstanceId("myEVlId"); + externalVirtualLink.setResourceSubnetId("notUsedSubnetId"); + instantiationRequest.setExtVirtualLink(new ArrayList<>()); + instantiationRequest.getExtVirtualLink().add(externalVirtualLink); + additionalParam.getExtManagedVirtualLinks().add(extManVl); + ZoneInfo zone = new ZoneInfo(); + zone.setId("zoneId"); + zone.setResourceId("zoneProviderId"); + zone.setVimId(VIM_ID); + additionalParam.getZones().add(zone); + VimSoftwareImage image = new VimSoftwareImage(); + image.setResourceId("imageProviderId"); + image.setVimId(VIM_ID); + image.setVnfdSoftwareImageId("imageId"); + additionalParam.getSoftwareImages().add(image); + additionalParam.setAdditionalParams(new JsonParser().parse("{ \"a\" : \"b\" }")); + String params = new Gson().toJson(additionalParam); + String src = "{ \"inputs\" : { \"vnfs\" : { \"" + ONAP_CSAR_ID + "\" : " + params + "}}, \"vimId\" : \"" + VIM_ID + "\"}"; + instantiationRequest.setAdditionalParam(new JsonParser().parse(src)); + return instantiationRequest; + } + + /** + * Test vimId decomposition + */ + @Test + public void testVimIdSplitting() { + assertEquals("regionId", LifecycleManager.getRegionName("cloudOwner_regionId")); + assertEquals("cloudOwner", LifecycleManager.getCloudOwner("cloudOwner_regionId")); + } +} diff --git a/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/vnfm/TestSelfRegistrationManager.java b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/vnfm/TestSelfRegistrationManager.java new file mode 100644 index 00000000..79ec1a83 --- /dev/null +++ b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/vnfm/TestSelfRegistrationManager.java @@ -0,0 +1,345 @@ +/* + * 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.vnfm; + +import com.google.common.collect.Lists; +import com.nokia.cbam.lcn.v32.ApiException; +import com.nokia.cbam.lcn.v32.model.CreateSubscriptionRequest; +import com.nokia.cbam.lcn.v32.model.Subscription; +import com.nokia.cbam.lcn.v32.model.SubscriptionAuthentication; +import com.nokia.cbam.lcn.v32.model.VnfNotificationType; +import org.junit.Before; +import org.junit.Test; +import org.mockito.*; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; +import org.onap.msb.sdk.discovery.common.RouteException; +import org.onap.msb.sdk.discovery.entity.MicroServiceFullInfo; +import org.onap.msb.sdk.discovery.entity.MicroServiceInfo; +import org.onap.msb.sdk.discovery.entity.Node; +import org.onap.msb.sdk.discovery.entity.RouteResult; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core.SelfRegistrationManager; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.TestUtil; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.UUID; + +import static com.nokia.cbam.lcn.v32.model.OperationType.*; +import static junit.framework.TestCase.*; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.*; +import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CbamRestApiProvider.NOKIA_LCN_API_VERSION; +import static org.springframework.test.util.ReflectionTestUtils.setField; + +public class TestSelfRegistrationManager extends TestBase { + @Mock + private JobManager jobManager; + private List<Subscription> subscriptions = new ArrayList<>(); + private ArgumentCaptor<MicroServiceInfo> registeredMicroservice = ArgumentCaptor.forClass(MicroServiceInfo.class); + private ArgumentCaptor<CreateSubscriptionRequest> subscriptionToCreate = ArgumentCaptor.forClass(CreateSubscriptionRequest.class); + @InjectMocks + private SelfRegistrationManager selfRegistrationManager; + + @Before + public void initMocks() throws Exception { + setField(SelfRegistrationManager.class, "logger", logger); + when(lcnApi.subscriptionsGet(NOKIA_LCN_API_VERSION)).thenReturn(subscriptions); + when(driverProperties.getVnfmId()).thenReturn(VNFM_ID); + setField(selfRegistrationManager, "driverMsbExternalIp", "1.2.3.4"); + setField(selfRegistrationManager, "driverVnfmExternalIp", "5.6.7.8"); + setField(selfRegistrationManager, "driverPort", "12345"); + Subscription unknownSubscription = new Subscription(); + unknownSubscription.setId(UUID.randomUUID().toString()); + unknownSubscription.setCallbackUrl("unknown"); + subscriptions.add(unknownSubscription); + } + + /** + * test the basic registration process + * - first subscribe to CBAM LCNs + * - second publish it's existence on MSB + */ + @Test + public void testRegistration() throws Exception { + //given + Subscription subscription = new Subscription(); + when(lcnApi.subscriptionsPost(subscriptionToCreate.capture(), Mockito.eq(NOKIA_LCN_API_VERSION))).thenReturn(subscription); + MicroServiceFullInfo returnedMicroService = new MicroServiceFullInfo(); + when(msbClient.registerMicroServiceInfo(registeredMicroservice.capture())).thenReturn(returnedMicroService); + //when + selfRegistrationManager.register(); + //verify + InOrder registrationOrder = Mockito.inOrder(lcnApi, msbClient); + registrationOrder.verify(lcnApi).subscriptionsPost(any(), any()); + registrationOrder.verify(msbClient).registerMicroServiceInfo(any()); + + assertMicroserviceRegistered(); + assertNewLcnSubscription(); + assertServiceUp(); + } + + private void assertNewLcnSubscription() { + CreateSubscriptionRequest subscriptionCreation = subscriptionToCreate.getValue(); + assertEquals("http://5.6.7.8:12345/api/NokiaSVNFM/v1/lcn", subscriptionCreation.getCallbackUrl()); + assertEquals(SubscriptionAuthentication.TypeEnum.NONE, subscriptionCreation.getAuthentication().getType()); + assertNull(subscriptionCreation.getAuthentication().getUserName()); + assertNull(subscriptionCreation.getAuthentication().getClientName()); + assertNull(subscriptionCreation.getAuthentication().getClientPassword()); + assertNull(subscriptionCreation.getAuthentication().getPassword()); + assertNull(subscriptionCreation.getAuthentication().getTokenUrl()); + assertNull(subscriptionCreation.getFilter().getVnfdId()); + assertNull(subscriptionCreation.getFilter().getVnfInstanceId()); + assertNull(subscriptionCreation.getFilter().getVnfProductName()); + assertNull(subscriptionCreation.getFilter().getVnfSoftwareVersion()); + assertEquals(Lists.newArrayList(VnfNotificationType.VNFLIFECYCLECHANGENOTIFICATION), subscriptionCreation.getFilter().getNotificationTypes()); + assertTrue(subscriptionCreation.getFilter().getOperationTypes().contains(HEAL)); + assertTrue(subscriptionCreation.getFilter().getOperationTypes().contains(SCALE)); + assertTrue(subscriptionCreation.getFilter().getOperationTypes().contains(TERMINATE)); + assertTrue(subscriptionCreation.getFilter().getOperationTypes().contains(INSTANTIATE)); + assertEquals(4, subscriptionCreation.getFilter().getOperationTypes().size()); + } + + private void assertMicroserviceRegistered() { + MicroServiceInfo microserviceRequest = registeredMicroservice.getValue(); + assertEquals(1, microserviceRequest.getNodes().size()); + Node node = microserviceRequest.getNodes().iterator().next(); + assertEquals("0", node.getTtl()); + assertEquals("1.2.3.4", node.getIp()); + assertEquals("12345", node.getPort()); + assertEquals("REST", microserviceRequest.getProtocol()); + assertNull(microserviceRequest.getMetadata()); + //very strange, but it should be null for ONAP to work + assertEquals("", microserviceRequest.getPath()); + assertEquals(SelfRegistrationManager.SERVICE_NAME, microserviceRequest.getServiceName()); + assertEquals("/api/NokiaSVNFM/v1", microserviceRequest.getUrl()); + assertEquals("v1", microserviceRequest.getVersion()); + //1 means internal service to ONAP + assertEquals("1", microserviceRequest.getVisualRange()); + } + + /** + * If the subscription already exists the subscription is not recreated + */ + @Test + public void testResubscription() throws Exception { + //given + MicroServiceFullInfo returnedMicroService = new MicroServiceFullInfo(); + when(msbClient.registerMicroServiceInfo(registeredMicroservice.capture())).thenReturn(returnedMicroService); + Subscription existingSubscription = new Subscription(); + existingSubscription.setId(UUID.randomUUID().toString()); + existingSubscription.setCallbackUrl("http://5.6.7.8:12345/api/NokiaSVNFM/v1/lcn"); + subscriptions.add(existingSubscription); + //when + selfRegistrationManager.register(); + //verify + assertMicroserviceRegistered(); + verify(lcnApi, never()).subscriptionsPost(any(), any()); + assertServiceUp(); + } + + /** + * If the LCN subscription fails the microservice is not registered + */ + @Test + public void testFailedLcnSubscription() throws Exception { + //given + ApiException expectedException = new ApiException(); + when(lcnApi.subscriptionsPost(any(), any())).thenThrow(expectedException); + //when + try { + selfRegistrationManager.register(); + fail(); + } catch (RuntimeException e) { + assertEquals(expectedException, e.getCause()); + } + //verify + verify(msbClient, never()).registerMicroServiceInfo(any()); + verify(logger).error("Unable to subscribe to CBAM LCN", expectedException); + assertServiceDown(); + } + + /** + * If the registration to MSB fails the subscription is deleted + */ + @Test + public void testFailedMsbPublish() throws Exception { + //given + Subscription subscription = new Subscription(); + when(lcnApi.subscriptionsPost(subscriptionToCreate.capture(), Mockito.eq(NOKIA_LCN_API_VERSION))).thenAnswer(new Answer<Subscription>() { + @Override + public Subscription answer(InvocationOnMock invocationOnMock) throws Throwable { + subscription.setCallbackUrl("http://5.6.7.8:12345/api/NokiaSVNFM/v1/lcn"); + subscription.setId(UUID.randomUUID().toString()); + subscriptions.add(subscription); + return subscription; + } + }); + MicroServiceFullInfo returnedMicroService = new MicroServiceFullInfo(); + RouteException expectedException = new RouteException(); + when(msbClient.registerMicroServiceInfo(registeredMicroservice.capture())).thenThrow(expectedException); + //when + try { + selfRegistrationManager.register(); + //verify + fail(); + } catch (RuntimeException e) { + assertEquals(expectedException, e.getCause()); + } + assertNewLcnSubscription(); + verify(lcnApi).subscriptionsSubscriptionIdDelete(subscription.getId(), NOKIA_LCN_API_VERSION); + assertServiceDown(); + } + + /** + * basic service unregistration + * - ongoing jobs are outwaited + * - first the service is removed from MSB + * - second unregistration + */ + @Test + public void testUnregistration() throws Exception { + //given + Subscription subscription = new Subscription(); + subscription.setCallbackUrl("http://5.6.7.8:12345/api/NokiaSVNFM/v1/lcn"); + subscription.setId(UUID.randomUUID().toString()); + subscriptions.add(subscription); + when(jobManager.hasOngoingJobs()).thenReturn(false); + MicroServiceFullInfo returnedMicroService = new MicroServiceFullInfo(); + //when + selfRegistrationManager.deRegister(); + //verify + InOrder inOrder = Mockito.inOrder(jobManager, msbClient, lcnApi); + inOrder.verify(msbClient).cancelMicroServiceInfo(SelfRegistrationManager.SERVICE_NAME, SelfRegistrationManager.DRIVER_VERSION); + inOrder.verify(lcnApi).subscriptionsSubscriptionIdDelete(subscription.getId(), NOKIA_LCN_API_VERSION); + assertServiceDown(); + } + + /** + * if the MSB reports that it could not cancel the service, but the service has + * disappeared from MSB the cancellation is considered to be successful + */ + @Test + public void testPartiallyFailedMsbCancel() throws Exception { + //given + Subscription subscription = new Subscription(); + subscription.setCallbackUrl("http://5.6.7.8:12345/api/NokiaSVNFM/v1/lcn"); + subscription.setId(UUID.randomUUID().toString()); + subscriptions.add(subscription); + when(jobManager.hasOngoingJobs()).thenReturn(false); + when(msbClient.cancelMicroServiceInfo(SelfRegistrationManager.SERVICE_NAME, SelfRegistrationManager.DRIVER_VERSION)).then(new Answer<RouteResult>() { + @Override + public RouteResult answer(InvocationOnMock invocationOnMock) throws Throwable { + when(msbClient.queryMicroServiceInfo(SelfRegistrationManager.SERVICE_NAME, SelfRegistrationManager.DRIVER_VERSION)).thenThrow(new RouteException()); + throw new RouteException(); + } + }); + MicroServiceFullInfo returnedMicroService = new MicroServiceFullInfo(); + //when + selfRegistrationManager.deRegister(); + //verify + InOrder inOrder = Mockito.inOrder(jobManager, msbClient, lcnApi); + inOrder.verify(msbClient).cancelMicroServiceInfo(SelfRegistrationManager.SERVICE_NAME, SelfRegistrationManager.DRIVER_VERSION); + inOrder.verify(msbClient).queryMicroServiceInfo(SelfRegistrationManager.SERVICE_NAME, SelfRegistrationManager.DRIVER_VERSION); + inOrder.verify(lcnApi).subscriptionsSubscriptionIdDelete(subscription.getId(), NOKIA_LCN_API_VERSION); + assertServiceDown(); + } + + /** + * failure of unregistration from MSB should be propagated + */ + @Test + public void testUnregistrationFailure() throws Exception { + //given + Subscription subscription = new Subscription(); + subscription.setCallbackUrl("http://5.6.7.8:12345/api/NokiaSVNFM/v1/lcn"); + subscription.setId(UUID.randomUUID().toString()); + subscriptions.add(subscription); + when(msbClient.cancelMicroServiceInfo(SelfRegistrationManager.SERVICE_NAME, SelfRegistrationManager.DRIVER_VERSION)).then(new Answer<RouteResult>() { + @Override + public RouteResult answer(InvocationOnMock invocationOnMock) throws Throwable { + throw new RouteException(); + } + }); + MicroServiceFullInfo returnedMicroService = new MicroServiceFullInfo(); + //when + try { + selfRegistrationManager.deRegister(); + fail(); + } catch (RuntimeException e) { + + } + //verify + InOrder inOrder = Mockito.inOrder(jobManager, msbClient, lcnApi); + inOrder.verify(msbClient).cancelMicroServiceInfo(SelfRegistrationManager.SERVICE_NAME, SelfRegistrationManager.DRIVER_VERSION); + inOrder.verify(msbClient).queryMicroServiceInfo(SelfRegistrationManager.SERVICE_NAME, SelfRegistrationManager.DRIVER_VERSION); + verify(lcnApi, Mockito.never()).subscriptionsSubscriptionIdDelete(subscription.getId(), NOKIA_LCN_API_VERSION); + assertServiceDown(); + } + + /** + * failure of subscription deletion from MSB should be propagated + */ + @Test + public void testSubscriptionFailure() throws Exception { + //given + Subscription subscription = new Subscription(); + subscription.setCallbackUrl("http://5.6.7.8:12345/api/NokiaSVNFM/v1/lcn"); + subscription.setId(UUID.randomUUID().toString()); + subscriptions.add(subscription); + when(jobManager.hasOngoingJobs()).thenReturn(false); + ApiException expectedException = new ApiException(); + doThrow(expectedException).when(lcnApi).subscriptionsSubscriptionIdDelete(subscription.getId(), NOKIA_LCN_API_VERSION); + //when + try { + selfRegistrationManager.deRegister(); + fail(); + } catch (RuntimeException e) { + + } + //verify + InOrder inOrder = Mockito.inOrder(jobManager, msbClient, lcnApi); + inOrder.verify(msbClient).cancelMicroServiceInfo(SelfRegistrationManager.SERVICE_NAME, SelfRegistrationManager.DRIVER_VERSION); + inOrder.verify(lcnApi).subscriptionsSubscriptionIdDelete(subscription.getId(), NOKIA_LCN_API_VERSION); + assertServiceDown(); + } + + /** + * the swagger API definitions embedded in the code + */ + @Test + public void testSwaggerApi() throws Exception { + //no idea how to test this except repeat implementation + byte[] a = TestUtil.loadFile(SelfRegistrationManager.SWAGGER_API_DEFINITION); + tearGeneric(); + //when + assertTrue(Arrays.equals(a, selfRegistrationManager.getSwaggerApiDefinition())); + } + + public void assertServiceUp() throws Exception { + assertTrue(selfRegistrationManager.isReady()); + } + + /** + * if there are ongoing jobs then the guard thros exception + */ + public void assertServiceDown() { + assertFalse(selfRegistrationManager.isReady()); + + } + +} diff --git a/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/vnfm/notification/TestLifecycleChangeNotificationManager.java b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/vnfm/notification/TestLifecycleChangeNotificationManager.java new file mode 100644 index 00000000..ed009b97 --- /dev/null +++ b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/vnfm/notification/TestLifecycleChangeNotificationManager.java @@ -0,0 +1,457 @@ +/* + * 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.vnfm.notification; + +import com.google.gson.*; +import com.nokia.cbam.lcm.v32.ApiException; +import com.nokia.cbam.lcm.v32.model.*; +import org.joda.time.DateTime; +import org.junit.Before; +import org.junit.Test; +import org.mockito.ArgumentCaptor; +import org.mockito.InjectMocks; +import org.mockito.Mockito; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.ILifecycleChangeNotificationManager; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.TestBase; +import org.threeten.bp.OffsetDateTime; + +import java.util.ArrayList; +import java.util.List; +import java.util.NoSuchElementException; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; + +import static com.nokia.cbam.lcm.v32.model.OperationType.*; +import static junit.framework.TestCase.*; +import static org.mockito.Mockito.*; +import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CbamRestApiProvider.NOKIA_LCM_API_VERSION; +import static org.springframework.test.util.ReflectionTestUtils.setField; + +public class TestLifecycleChangeNotificationManager extends TestBase { + + public static final String OPERATION_EXECUTION_ID = "myOperationExecutionId"; + + @InjectMocks + private LifecycleChangeNotificationManager lifecycleChangeNotificationManager; + private VnfLifecycleChangeNotification recievedLcn = new VnfLifecycleChangeNotification(); + private List<OperationExecution> operationExecutions = new ArrayList<>(); + private OperationExecution instantiationOperation = new OperationExecution(); + private OperationExecution scaleOperation = new OperationExecution(); + private OperationExecution healOperation = new OperationExecution(); + private OperationExecution terminationOperation = new OperationExecution(); + + private ArgumentCaptor<OperationExecution> currentOperationExecution = ArgumentCaptor.forClass(OperationExecution.class); + private ArgumentCaptor<ReportedAffectedConnectionPoints> affectedConnectionPoints = ArgumentCaptor.forClass(ReportedAffectedConnectionPoints.class); + + private List<VnfInfo> vnfs = new ArrayList<>(); + private VnfInfo vnf = new VnfInfo(); + + @Before + public void initMocks() throws Exception { + setField(LifecycleChangeNotificationManager.class, "logger", logger); + instantiationOperation.setId("instantiationOperationExecutionId"); + instantiationOperation.setStartTime(OffsetDateTime.now()); + instantiationOperation.setOperationType(OperationType.INSTANTIATE); + scaleOperation.setId("scaleOperationExecutionId"); + scaleOperation.setStartTime(OffsetDateTime.now().plusDays(1)); + scaleOperation.setOperationType(OperationType.SCALE); + terminationOperation.setId("terminationExecutionId"); + terminationOperation.setStartTime(OffsetDateTime.now().plusDays(1)); + terminationOperation.setOperationType(OperationType.TERMINATE); + healOperation.setId("healOperaitonExecutionId"); + healOperation.setOperationType(OperationType.HEAL); + recievedLcn.setLifecycleOperationOccurrenceId("instantiationOperationExecutionId"); + healOperation.setStartTime(OffsetDateTime.now().plusDays(1)); + recievedLcn.setVnfInstanceId(VNF_ID); + when(vnfApi.vnfsVnfInstanceIdOperationExecutionsGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenReturn(operationExecutions); + prepOperation(instantiationOperation); + prepOperation(scaleOperation); + prepOperation(healOperation); + prepOperation(terminationOperation); + doNothing().when(notificationSender).processNotification(eq(recievedLcn), currentOperationExecution.capture(), affectedConnectionPoints.capture(), eq(VIM_ID)); + InstantiateVnfRequest instantiateVnfRequest = new InstantiateVnfRequest(); + VimInfo vimInfo = new VimInfo(); + vimInfo.setId(VIM_ID); + instantiateVnfRequest.getVims().add(vimInfo); + when(operationExecutionApi.operationExecutionsOperationExecutionIdOperationParamsGet(instantiationOperation.getId(), NOKIA_LCM_API_VERSION)).thenReturn(new Gson().toJsonTree(instantiateVnfRequest)); + when(vnfApi.vnfsGet(NOKIA_LCM_API_VERSION)).thenReturn(vnfs); + vnfs.add(vnf); + vnf.setId(VNF_ID); + VnfProperty prop = new VnfProperty(); + prop.setName(ILifecycleChangeNotificationManager.EXTERNAL_VNFM_ID); + prop.setValue(VNFM_ID); + vnf.setExtensions(new ArrayList<>()); + vnf.getExtensions().add(prop); + when(vnfApi.vnfsVnfInstanceIdGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenReturn(vnf); + } + + private void prepOperation(OperationExecution operationExecution) throws ApiException { + addEmptyModifiedConnectionPoints(operationExecution); + JsonElement root = new JsonParser().parse("{ \"additionalParams\" : { \"jobId\" : \"" + JOB_ID + "\"}}"); + operationExecution.setOperationParams(root); + switch (operationExecution.getOperationType()) { + case TERMINATE: + root.getAsJsonObject().addProperty("terminationType", "GRACEFULL"); + } + when(operationExecutionApi.operationExecutionsOperationExecutionIdGet(operationExecution.getId(), NOKIA_LCM_API_VERSION)).thenReturn(operationExecution); + operationExecutions.add(operationExecution); + } + + private void addEmptyModifiedConnectionPoints(OperationExecution operationExecution) { + OperationResult operationResult = new OperationResult(); + operationResult.operationResult = new ReportedAffectedConnectionPoints(); + JsonElement additionalData = new Gson().toJsonTree(operationResult); + operationExecution.setAdditionalData(additionalData); + } + + /** + * The first instantiation before the current operation is selected + */ + @Test + public void testLastInstantiationSelection() { + List<OperationExecution> operations = new ArrayList<>(); + + OperationExecution operation = buildOperation(OffsetDateTime.now(), TERMINATE); + OperationExecution operationScale = buildOperation(OffsetDateTime.now().minusDays(1), SCALE); + OperationExecution operationClosestInstantiate = buildOperation(OffsetDateTime.now().minusDays(2), INSTANTIATE); + OperationExecution operationFurthers = buildOperation(OffsetDateTime.now().minusDays(3), INSTANTIATE); + + operations.add(operation); + operations.add(operationScale); + operations.add(operationClosestInstantiate); + operations.add(operationFurthers); + assertEquals(operationClosestInstantiate, LifecycleChangeNotificationManager.findLastInstantiationBefore(operations, operation)); + } + + /** + * The instantiation operation itself is valid as the last instantiation operation + */ + @Test + public void testInstantiationSufficesTheLastInstantiation() { + DateTime baseTime = DateTime.now(); + List<OperationExecution> operations = new ArrayList<>(); + + OperationExecution operation = buildOperation(OffsetDateTime.now(), INSTANTIATE); + OperationExecution operationScale = buildOperation(OffsetDateTime.now().minusDays(1), SCALE); + OperationExecution operationFurthers = buildOperation(OffsetDateTime.now().minusDays(2), INSTANTIATE); + + operations.add(operation); + operations.add(operationScale); + operations.add(operationFurthers); + assertEquals(operation, LifecycleChangeNotificationManager.findLastInstantiationBefore(operations, operation)); + } + + /** + * If no instantiation operation is found for before the selected operation + */ + @Test + public void testNoInstantiation() { + DateTime baseTime = DateTime.now(); + List<OperationExecution> operations = new ArrayList<>(); + + OperationExecution operation = buildOperation(OffsetDateTime.now(), TERMINATE); + OperationExecution operationScale = buildOperation(OffsetDateTime.now().minusDays(1), SCALE); + + operations.add(operation); + operations.add(operationScale); + try { + LifecycleChangeNotificationManager.findLastInstantiationBefore(operations, operation); + fail(); + } catch (NoSuchElementException e) { + + } + } + + /** + * the operations are ordered from newest (first) to oldest (last) + */ + @Test + public void testOperationOrdering() { + List<OperationExecution> operationExecutions = new ArrayList<>(); + OperationExecution before = buildOperation(OffsetDateTime.now(), OperationType.INSTANTIATE); + operationExecutions.add(before); + OperationExecution after = buildOperation(OffsetDateTime.now().plusDays(1), OperationType.SCALE); + operationExecutions.add(after); + List<OperationExecution> sorted1 = LifecycleChangeNotificationManager.NEWEST_OPERATIONS_FIRST.sortedCopy(operationExecutions); + assertEquals(after, sorted1.get(0)); + assertEquals(before, sorted1.get(1)); + } + + /** + * if VNF listing fails the processing of the notifications is aborted + */ + @Test + public void testUnableToListVnfs() throws Exception { + ApiException expectedException = new ApiException(); + when(vnfApi.vnfsGet(NOKIA_LCM_API_VERSION)).thenThrow(expectedException); + //when + try { + lifecycleChangeNotificationManager.handleLcn(recievedLcn); + fail(); + } catch (Exception e) { + assertEquals(expectedException, e.getCause()); + verify(logger).error("Unable to list VNFs / query VNF", expectedException); + } + } + + /** + * if VNF query fails the processing of the notifications is aborted + */ + @Test + public void testUnableToQueryVnf() throws Exception { + ApiException expectedException = new ApiException(); + when(vnfApi.vnfsVnfInstanceIdGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenThrow(expectedException); + //when + try { + lifecycleChangeNotificationManager.handleLcn(recievedLcn); + fail(); + } catch (Exception e) { + assertEquals(expectedException, e.getCause()); + verify(logger).error("Unable to list VNFs / query VNF", expectedException); + } + } + + /** + * if the VNF is not managed by this VNFM the LCN is dropped + */ + @Test + public void testNonManagedVnf() throws Exception { + vnf.getExtensions().clear(); + //when + lifecycleChangeNotificationManager.handleLcn(recievedLcn); + //verify + Mockito.verifyZeroInteractions(operationExecutionApi); + verify(logger).warn("The VNF with " + VNF_ID + " identifier is not a managed VNF"); + } + + /** + * if the VNF is not managed by this VNFM the LCN is dropped + */ + @Test + public void testManagedByOtherVnf() throws Exception { + vnf.getExtensions().get(0).setValue("unknownVnfmId"); + //when + lifecycleChangeNotificationManager.handleLcn(recievedLcn); + //verify + Mockito.verifyZeroInteractions(operationExecutionApi); + verify(logger).warn("The VNF with " + VNF_ID + " identifier is not a managed by the VNFM with id unknownVnfmId"); + } + + /** + * if the VNF disappeared before processing the LCN + */ + @Test + public void testDisappearedVnf() throws Exception { + vnfs.clear(); + //when + lifecycleChangeNotificationManager.handleLcn(recievedLcn); + //verify + Mockito.verifyZeroInteractions(operationExecutionApi); + verify(logger).warn("The VNF with " + VNF_ID + " disappeared before being able to process the LCN"); + } + + /** + * if the operation parameters of the last instantiation is non querieable error is propagated + */ + @Test + public void testUnableToQueryOperationParams() throws Exception { + recievedLcn.setOperation(OperationType.TERMINATE); + recievedLcn.setStatus(OperationStatus.FINISHED); + RuntimeException expectedException = new RuntimeException(); + when(operationExecutionApi.operationExecutionsOperationExecutionIdOperationParamsGet(instantiationOperation.getId(), NOKIA_LCM_API_VERSION)).thenThrow(expectedException); + //when + try { + lifecycleChangeNotificationManager.handleLcn(recievedLcn); + fail(); + } catch (Exception e) { + //verify + Mockito.verifyZeroInteractions(nsLcmApi); + assertEquals(expectedException, e.getCause()); + verify(logger).error("Unable to detect last instantiation operation", e.getCause()); + } + } + + /** + * if unable to send LCN to VF-C the error is propagated + */ + @Test + public void testUnableToQueryCurrentOperation() throws Exception { + recievedLcn.setOperation(OperationType.TERMINATE); + recievedLcn.setStatus(OperationStatus.FINISHED); + ApiException expectedException = new ApiException(); + when(vnfApi.vnfsVnfInstanceIdOperationExecutionsGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenThrow(expectedException); + //when + try { + lifecycleChangeNotificationManager.handleLcn(recievedLcn); + fail(); + } catch (Exception e) { + //verify + assertEquals(expectedException, e.getCause()); + verify(logger).error("Unable to retrieve the current VNF myVnfId", e.getCause()); + } + } + + /** + * test that waitForTerminationToBeProcessed outwaits the successfull processing of the termination notification + */ + @Test + public void testWaitForTermination() throws Exception { + //given + //add an non processed notification + VnfLifecycleChangeNotification nonProcessedEvent = new VnfLifecycleChangeNotification(); + nonProcessedEvent.setStatus(OperationStatus.FINISHED); + nonProcessedEvent.setOperation(OperationType.TERMINATE); + OperationExecution secondTerminationOperationExecution = new OperationExecution(); + secondTerminationOperationExecution.setOperationType(OperationType.TERMINATE); + secondTerminationOperationExecution.setId("secondId"); + secondTerminationOperationExecution.setOperationParams(buildTerminationParams()); + nonProcessedEvent.setLifecycleOperationOccurrenceId(secondTerminationOperationExecution.getId()); + lifecycleChangeNotificationManager.handleLcn(nonProcessedEvent); + //add second termination + recievedLcn.setOperation(OperationType.TERMINATE); + recievedLcn.setStatus(OperationStatus.FINISHED); + recievedLcn.setLifecycleOperationOccurrenceId(terminationOperation.getId()); + ExecutorService executorService = Executors.newCachedThreadPool(); + Future<Boolean> waitExitedWithSuccess = executorService.submit(new Callable<Boolean>() { + @Override + public Boolean call() throws Exception { + try { + lifecycleChangeNotificationManager.waitForTerminationToBeProcessed(terminationOperation.getId()); + return true; + } catch (Exception e) { + return false; + } + } + }); + //when + lifecycleChangeNotificationManager.handleLcn(recievedLcn); + //verify + assertTrue(waitExitedWithSuccess.get()); + } + + /** + * Forceful termination results in an empty affected connection points + */ + @Test + public void testMissingPreResultForForcefullTermination() { + //given + recievedLcn.setOperation(OperationType.INSTANTIATE); + recievedLcn.setStatus(OperationStatus.FINISHED); + recievedLcn.setLifecycleOperationOccurrenceId(terminationOperation.getId()); + JsonObject additionalData = new JsonObject(); + additionalData.add("operationResult", new JsonObject()); + ((JsonObject) terminationOperation.getOperationParams()).addProperty("terminationType", "FORCEFUL"); + terminationOperation.setAdditionalData(additionalData); + terminationOperation.setStatus(OperationStatus.FINISHED); + terminationOperation.setOperationType(OperationType.TERMINATE); + //when + lifecycleChangeNotificationManager.handleLcn(recievedLcn); + assertNull(affectedConnectionPoints.getValue()); + verify(logger).warn("Unable to send information related to affected connection points during forceful termination"); + } + + /** + * test end notification scenario for failed scale-out + * - LCN is sent to VF-C, but the + */ + @Test + public void testMissingPreResultForFailedOperation() { + //given + recievedLcn.setOperation(OperationType.SCALE); + recievedLcn.setStatus(OperationStatus.FAILED); + recievedLcn.setLifecycleOperationOccurrenceId(scaleOperation.getId()); + ScaleVnfRequest request = new ScaleVnfRequest(); + request.setAdditionalParams(new JsonParser().parse("{ \"type\" : \"IN\", \"jobId\" : \"" + JOB_ID + "\" }")); + request.setType(ScaleDirection.OUT); + scaleOperation.setOperationParams(request); + scaleOperation.setAdditionalData(null); + scaleOperation.setStatus(OperationStatus.FAILED); + scaleOperation.setOperationType(OperationType.SCALE); + //when + lifecycleChangeNotificationManager.handleLcn(recievedLcn); + assertEquals(0, affectedConnectionPoints.getValue().getPost().size()); + assertEquals(0, affectedConnectionPoints.getValue().getPre().size()); + verify(logger).warn("The operation failed and the affected connection points were not reported"); + } + + /** + * test end notification success scenario for scale-out + * - LCN is sent to VF-C + */ + @Test + public void testMissingPreResult() { + //given + recievedLcn.setOperation(OperationType.SCALE); + recievedLcn.setStatus(OperationStatus.FINISHED); + recievedLcn.setLifecycleOperationOccurrenceId(scaleOperation.getId()); + ScaleVnfRequest request = new ScaleVnfRequest(); + request.setAdditionalParams(new JsonParser().parse("{ \"type\" : \"IN\", \"jobId\" : \"" + JOB_ID + "\" }")); + request.setType(ScaleDirection.OUT); + scaleOperation.setOperationParams(request); + JsonObject additionalData = new JsonObject(); + additionalData.add("operationResult", new JsonObject()); + scaleOperation.setAdditionalData(additionalData); + scaleOperation.setStatus(OperationStatus.FINISHED); + scaleOperation.setOperationType(OperationType.SCALE); + JsonElement root = new JsonParser().parse("{ \"additionalParams\" : { \"jobId\" : \"" + JOB_ID + "\"}}"); + JsonObject operationParams = new JsonObject(); + //when + try { + lifecycleChangeNotificationManager.handleLcn(recievedLcn); + fail(); + } catch (Exception e) { + assertEquals("All operations must return the { \"operationResult\" : { \"cbam_pre\" : [<fillMeOut>], \"cbam_post\" : [<fillMeOut>] } } structure", e.getMessage()); + } + } + + /** + * missing connection points are tolerated in case of failed operations + */ + @Test + public void testMissingConnectionPoints() { + //given + recievedLcn.setOperation(OperationType.INSTANTIATE); + recievedLcn.setStatus(OperationStatus.FAILED); + recievedLcn.setLifecycleOperationOccurrenceId(instantiationOperation.getId()); + instantiationOperation.setAdditionalData(null); + instantiationOperation.setStatus(OperationStatus.FAILED); + //when + lifecycleChangeNotificationManager.handleLcn(recievedLcn); + assertEquals(0, affectedConnectionPoints.getValue().getPost().size()); + assertEquals(0, affectedConnectionPoints.getValue().getPre().size()); + verify(logger).warn("The operation failed and the affected connection points were not reported"); + } + + private JsonObject buildTerminationParams() { + JsonObject root = new JsonObject(); + root.add("terminationType", new JsonPrimitive("GRACEFULL")); + return root; + } + + private OperationExecution buildOperation(OffsetDateTime baseTime, OperationType operationType) { + OperationExecution operation = new OperationExecution(); + operation.setStartTime(baseTime); + operation.setOperationType(operationType); + return operation; + } + + class OperationResult { + ReportedAffectedConnectionPoints operationResult; + } + +} diff --git a/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/vnfm/notification/TestProcessedNotification.java b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/vnfm/notification/TestProcessedNotification.java new file mode 100644 index 00000000..bbe660a0 --- /dev/null +++ b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/vnfm/notification/TestProcessedNotification.java @@ -0,0 +1,28 @@ +/* + * 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.vnfm.notification; + +import org.junit.Test; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.TestBase; + +import static pl.pojo.tester.api.assertion.Assertions.assertPojoMethodsFor; + +public class TestProcessedNotification extends TestBase { + @Test + public void testPojo() { + assertPojoMethodsFor(ProcessedNotification.class).areWellImplemented(); + } +} diff --git a/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/vnfm/notification/TestReportedAffectedConnectionPoints.java b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/vnfm/notification/TestReportedAffectedConnectionPoints.java new file mode 100644 index 00000000..83699723 --- /dev/null +++ b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/vnfm/notification/TestReportedAffectedConnectionPoints.java @@ -0,0 +1,30 @@ +/* + * 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.vnfm.notification; + +import org.junit.Test; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.TestBase; + +import static pl.pojo.tester.api.assertion.Assertions.assertPojoMethodsFor; + +public class TestReportedAffectedConnectionPoints extends TestBase { + @Test + public void testPojo() { + assertPojoMethodsFor(ReportedAffectedConnectionPoints.class).areWellImplemented(); + + } + +} diff --git a/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/vnfm/notification/TestReportedAffectedCp.java b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/vnfm/notification/TestReportedAffectedCp.java new file mode 100644 index 00000000..cb4d7e1c --- /dev/null +++ b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/vnfm/notification/TestReportedAffectedCp.java @@ -0,0 +1,30 @@ +/* + * 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.vnfm.notification; + +import org.junit.Test; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.TestBase; + +import static pl.pojo.tester.api.assertion.Assertions.assertPojoMethodsFor; + +public class TestReportedAffectedCp extends TestBase { + @Test + public void testPojo() { + assertPojoMethodsFor(ReportedAffectedCp.class).areWellImplemented(); + + } + +} |