2 * Copyright 2016-2017, Nokia Corporation
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.direct.notification;
18 import com.nokia.cbam.lcm.v32.model.VnfInfo;
19 import io.reactivex.Observable;
20 import java.util.HashSet;
21 import java.util.List;
22 import java.util.NoSuchElementException;
24 import java.util.concurrent.atomic.AtomicLong;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.mockito.ArgumentCaptor;
28 import org.mockito.Mock;
29 import org.mockito.Mockito;
30 import org.mockito.invocation.InvocationOnMock;
31 import org.mockito.stubbing.Answer;
32 import org.onap.aai.api.NetworkApi;
33 import org.onap.aai.model.GenericVnf;
34 import org.onap.aai.model.Relationship;
35 import org.onap.aai.model.RelationshipData;
36 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core.SelfRegistrationManager;
37 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.direct.AAIRestApiProvider;
38 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CbamRestApiProvider;
39 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.TestBase;
41 import static java.lang.Boolean.TRUE;
42 import static java.util.Optional.of;
44 import static junit.framework.TestCase.assertEquals;
45 import static junit.framework.TestCase.fail;
46 import static org.mockito.Mockito.*;
47 import static org.springframework.test.util.ReflectionTestUtils.setField;
49 public class TestGenericVnfManager extends TestBase {
50 private ArgumentCaptor<GenericVnf> payload = ArgumentCaptor.forClass(GenericVnf.class);
54 private AAIRestApiProvider aaiRestApiProvider;
56 private NetworkApi networkApi;
57 private GenericVnfManager genericVnfManager;
58 private VnfInfo vnfInfo = new VnfInfo();
60 static void assertRelation(List<Relationship> relationShips, String relatedTo, RelationshipData... data) {
61 for (Relationship relationship : relationShips) {
62 if (relationship.getRelatedTo().equals(relatedTo)) {
63 assertEquals(data.length, relationship.getRelationshipData().size());
65 for (RelationshipData c : data) {
66 assertEquals(c.getRelationshipKey(), relationship.getRelationshipData().get(i).getRelationshipKey());
67 assertEquals(c.getRelationshipValue(), relationship.getRelationshipData().get(i).getRelationshipValue());
78 when(aaiRestApiProvider.getNetworkApi()).thenReturn(networkApi);
79 genericVnfManager = new GenericVnfManager(aaiRestApiProvider, cbamRestApiProviderForSo);
80 setField(GenericVnfManager.class, "logger", logger);
81 AtomicLong currentTime = new AtomicLong(0L);
82 when(systemFunctions.currentTimeMillis()).thenAnswer(new Answer<Long>() {
84 public Long answer(InvocationOnMock invocation) throws Throwable {
85 return currentTime.get();
88 Mockito.doAnswer(new Answer() {
90 public Object answer(InvocationOnMock invocation) throws Throwable {
91 currentTime.addAndGet((Long) invocation.getArguments()[0] + 1);
94 }).when(systemFunctions).sleep(anyLong());
98 * if the VNF does not exist it is created
101 public void createNonExistingVnf() throws Exception {
102 GenericVnf vnfInAaai = new GenericVnf();
103 Set<GenericVnf> vnfs = new HashSet<>();
104 when(networkApi.getNetworkGenericVnfsGenericVnf(VNF_ID, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null)).thenAnswer((Answer<Observable<GenericVnf>>) invocation -> {
105 if (vnfs.size() == 0) {
106 throw new NoSuchElementException();
108 return buildObservable(vnfs.iterator().next());
110 when(cbamRestApiProviderForSo.getCbamLcmApi(VNFM_ID).vnfsVnfInstanceIdGet(VNF_ID, CbamRestApiProvider.NOKIA_LCM_API_VERSION)).thenReturn(buildObservable(vnfInfo));
111 when(networkApi.createOrUpdateNetworkGenericVnfsGenericVnf(eq(VNF_ID), payload.capture())).thenAnswer(invocation -> {
113 return VOID_OBSERVABLE.value();
115 vnfInfo.setName("vnfName");
117 genericVnfManager.createOrUpdate(VNF_ID, true, VNFM_ID, of("nsId"));
119 GenericVnf vnfSentToAai = payload.getValue();
120 assertEquals(VNF_ID, vnfSentToAai.getVnfId());
121 assertEquals(VNF_ID, vnfSentToAai.getVnfId());
122 assertEquals("NokiaVNF", vnfSentToAai.getVnfType());
123 assertEquals(SelfRegistrationManager.SERVICE_NAME, vnfSentToAai.getNfType());
124 assertEquals(TRUE, vnfSentToAai.isInMaint());
125 assertEquals(TRUE, vnfSentToAai.isIsClosedLoopDisabled());
126 assertEquals("vnfName", vnfSentToAai.getVnfName());
127 verify(systemFunctions, times(10)).sleep(3000);
128 verify(networkApi, times(10)).getNetworkGenericVnfsGenericVnf(VNF_ID, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null);
129 VOID_OBSERVABLE.assertCalled();
133 * if the VNF exist it is updated
136 public void testUpdateExistingVnf() throws Exception {
137 GenericVnf vnfInAaai = new GenericVnf();
138 vnfInAaai.setResourceVersion("v1");
139 when(networkApi.getNetworkGenericVnfsGenericVnf(VNF_ID, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null)).thenReturn(buildObservable(vnfInAaai));
140 when(cbamRestApiProviderForSo.getCbamLcmApi(VNFM_ID).vnfsVnfInstanceIdGet(VNF_ID, CbamRestApiProvider.NOKIA_LCM_API_VERSION)).thenReturn(buildObservable(vnfInfo));
141 when(networkApi.createOrUpdateNetworkGenericVnfsGenericVnf(eq(VNF_ID), payload.capture())).thenReturn(VOID_OBSERVABLE.value());
142 vnfInfo.setName("vnfName");
144 genericVnfManager.createOrUpdate(VNF_ID, true, VNFM_ID, of("nsId"));
146 GenericVnf vnfSentToAai = payload.getValue();
147 assertEquals(VNF_ID, vnfSentToAai.getVnfId());
148 assertEquals(VNF_ID, vnfSentToAai.getVnfId());
149 assertEquals("NokiaVNF", vnfSentToAai.getVnfType());
150 assertEquals(TRUE, vnfSentToAai.isInMaint());
151 assertEquals(TRUE, vnfSentToAai.isIsClosedLoopDisabled());
152 assertEquals("vnfName", vnfSentToAai.getVnfName());
153 verify(systemFunctions, never()).sleep(anyLong());
154 VOID_OBSERVABLE.assertCalled();
155 verify(networkApi, times(1)).getNetworkGenericVnfsGenericVnf(VNF_ID, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null);
159 * error is propagated if unable to query VNF from CBAM
162 public void testUnableToQueryVnfFromCBAM() throws Exception {
163 GenericVnf vnfInAaai = new GenericVnf();
164 vnfInAaai.setResourceVersion("v1");
165 when(networkApi.getNetworkGenericVnfsGenericVnf(VNF_ID, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null)).thenReturn(buildObservable(vnfInAaai));
166 RuntimeException expectedException = new RuntimeException();
167 when(cbamRestApiProviderForSo.getCbamLcmApi(VNFM_ID).vnfsVnfInstanceIdGet(VNF_ID, CbamRestApiProvider.NOKIA_LCM_API_VERSION)).thenThrow(expectedException);
168 when(networkApi.createOrUpdateNetworkGenericVnfsGenericVnf(eq(VNF_ID), payload.capture())).thenAnswer(invocation -> {
169 vnfInAaai.setResourceVersion("v2");
172 vnfInfo.setName("vnfName");
175 genericVnfManager.createOrUpdate(VNF_ID, true, VNFM_ID, of("nsId"));
176 } catch (Exception e) {
177 verify(logger).error("Unable to query VNF with myVnfId identifier from CBAM", expectedException);
178 assertEquals("Unable to query VNF with myVnfId identifier from CBAM", e.getMessage());
183 * if the VNF is created after the last attempt to query VNF, but before the
184 * the driver creates the VNF it is not created but updated
187 public void testConcurency1() throws Exception {
188 GenericVnf vnfInAaai = new GenericVnf();
189 vnfInAaai.setResourceVersion("v3");
190 Set<Integer> queryCount = new HashSet<>();
191 when(networkApi.getNetworkGenericVnfsGenericVnf(VNF_ID, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null)).thenAnswer((Answer<Observable>) invocationOnMock -> {
192 queryCount.add(queryCount.size());
193 if (queryCount.size() >= 11) {
194 return buildObservable(vnfInAaai);
196 throw new NoSuchElementException();
198 when(cbamRestApiProviderForSo.getCbamLcmApi(VNFM_ID).vnfsVnfInstanceIdGet(VNF_ID, CbamRestApiProvider.NOKIA_LCM_API_VERSION)).thenReturn(buildObservable(vnfInfo));
199 RuntimeException runtimeException = new RuntimeException();
200 when(networkApi.createOrUpdateNetworkGenericVnfsGenericVnf(eq(VNF_ID), payload.capture())).thenAnswer(invocation -> {
201 GenericVnf vnfSentToAAi = (GenericVnf) invocation.getArguments()[1];
202 if (vnfSentToAAi.getResourceVersion() == null) {
203 throw runtimeException;
205 return VOID_OBSERVABLE.value();
207 vnfInfo.setName("vnfName");
209 genericVnfManager.createOrUpdate(VNF_ID, true, VNFM_ID, of("nsId"));
211 GenericVnf vnfSentToAai = payload.getValue();
212 assertEquals(VNF_ID, vnfSentToAai.getVnfId());
213 assertEquals(VNF_ID, vnfSentToAai.getVnfId());
214 assertEquals("NokiaVNF", vnfSentToAai.getVnfType());
215 assertEquals(TRUE, vnfSentToAai.isInMaint());
216 assertEquals(TRUE, vnfSentToAai.isIsClosedLoopDisabled());
217 assertEquals("vnfName", vnfSentToAai.getVnfName());
218 assertEquals("v3", vnfSentToAai.getResourceVersion());
219 verify(systemFunctions, times(10)).sleep(3000);
220 verify(networkApi, times(11)).getNetworkGenericVnfsGenericVnf(VNF_ID, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null);
221 verify(networkApi, times(2)).createOrUpdateNetworkGenericVnfsGenericVnf(eq(VNF_ID), any());
222 verify(logger).warn(eq("The VNF with myVnfId identifier did not appear in time"), any(NoSuchElementException.class));
223 verify(logger).warn("The VNF with myVnfId identifier has been created since after the maximal wait for VNF to appear timeout", runtimeException);
224 VOID_OBSERVABLE.assertCalled();
228 * test how entities can refer to a VNF
231 public void testRelations() {
233 Relationship relationship = GenericVnfManager.linkTo(VNF_ID);
235 assertEquals("generic-vnf", relationship.getRelatedTo());
236 assertEquals(1, relationship.getRelationshipData().size());
237 assertEquals("generic-vnf.vnf-id", relationship.getRelationshipData().get(0).getRelationshipKey());
238 assertEquals(VNF_ID, relationship.getRelationshipData().get(0).getRelationshipValue());
245 public void testInheritence() {
246 assertEquals(logger, genericVnfManager.getLogger());