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;
21 import java.util.concurrent.atomic.AtomicLong;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.mockito.ArgumentCaptor;
25 import org.mockito.Mock;
26 import org.mockito.Mockito;
27 import org.mockito.invocation.InvocationOnMock;
28 import org.mockito.stubbing.Answer;
29 import org.onap.aai.api.NetworkApi;
30 import org.onap.aai.model.GenericVnf;
31 import org.onap.aai.model.Relationship;
32 import org.onap.aai.model.RelationshipData;
33 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core.SelfRegistrationManager;
34 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.direct.AAIRestApiProvider;
35 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CbamRestApiProvider;
36 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.TestBase;
38 import static java.lang.Boolean.TRUE;
39 import static java.util.Optional.empty;
40 import static java.util.Optional.of;
42 import static junit.framework.TestCase.assertEquals;
43 import static junit.framework.TestCase.fail;
44 import static org.mockito.Mockito.*;
45 import static org.springframework.test.util.ReflectionTestUtils.setField;
47 public class TestGenericVnfManager extends TestBase {
48 private ArgumentCaptor<GenericVnf> payload = ArgumentCaptor.forClass(GenericVnf.class);
52 private AAIRestApiProvider aaiRestApiProvider;
54 private NetworkApi networkApi;
55 private GenericVnfManager genericVnfManager;
56 private VnfInfo vnfInfo = new VnfInfo();
58 static void assertRelation(List<Relationship> relationShips, String relatedTo, RelationshipData... data) {
59 for (Relationship relationship : relationShips) {
60 if (relationship.getRelatedTo().equals(relatedTo)) {
61 assertEquals(data.length, relationship.getRelationshipData().size());
63 for (RelationshipData c : data) {
64 assertEquals(c.getRelationshipKey(), relationship.getRelationshipData().get(i).getRelationshipKey());
65 assertEquals(c.getRelationshipValue(), relationship.getRelationshipData().get(i).getRelationshipValue());
76 when(aaiRestApiProvider.getNetworkApi()).thenReturn(networkApi);
77 genericVnfManager = new GenericVnfManager(aaiRestApiProvider, cbamRestApiProviderForSo);
78 setField(GenericVnfManager.class, "logger", logger);
79 AtomicLong currentTime = new AtomicLong(0L);
80 when(systemFunctions.currentTimeMillis()).thenAnswer(new Answer<Long>() {
82 public Long answer(InvocationOnMock invocation) throws Throwable {
83 return currentTime.get();
86 Mockito.doAnswer(new Answer() {
88 public Object answer(InvocationOnMock invocation) throws Throwable {
89 currentTime.addAndGet((Long) invocation.getArguments()[0] + 1);
92 }).when(systemFunctions).sleep(anyLong());
96 * if the VNF does not exist it is created
99 public void createNonExistingVnf() throws Exception {
100 GenericVnf vnfInAaai = new GenericVnf();
101 Set<GenericVnf> vnfs = new HashSet<>();
102 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 -> {
103 if (vnfs.size() == 0) {
104 throw new NoSuchElementException();
106 return buildObservable(vnfs.iterator().next());
108 when(cbamRestApiProviderForSo.getCbamLcmApi(VNFM_ID).vnfsVnfInstanceIdGet(VNF_ID, CbamRestApiProvider.NOKIA_LCM_API_VERSION)).thenReturn(buildObservable(vnfInfo));
109 when(networkApi.createOrUpdateNetworkGenericVnfsGenericVnf(eq(VNF_ID), payload.capture())).thenAnswer(invocation -> {
111 return VOID_OBSERVABLE.value();
113 vnfInfo.setName("vnfName");
115 genericVnfManager.createOrUpdate(VNF_ID, true, VNFM_ID, of("nsId"));
117 GenericVnf vnfSentToAai = payload.getValue();
118 assertEquals(VNF_ID, vnfSentToAai.getVnfId());
119 assertEquals(VNF_ID, vnfSentToAai.getVnfId());
120 assertEquals("NokiaVNF", vnfSentToAai.getVnfType());
121 assertEquals(SelfRegistrationManager.SERVICE_NAME, vnfSentToAai.getNfType());
122 assertEquals(TRUE, vnfSentToAai.isInMaint());
123 assertEquals(TRUE, vnfSentToAai.isIsClosedLoopDisabled());
124 assertEquals("vnfName", vnfSentToAai.getVnfName());
125 verify(systemFunctions, times(10)).sleep(3000);
126 verify(networkApi, times(10)).getNetworkGenericVnfsGenericVnf(VNF_ID, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null);
127 VOID_OBSERVABLE.assertCalled();
131 * if the VNF exist it is updated
134 public void testUpdateExistingVnf() throws Exception {
135 GenericVnf vnfInAaai = new GenericVnf();
136 vnfInAaai.setResourceVersion("v1");
137 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));
138 when(cbamRestApiProviderForSo.getCbamLcmApi(VNFM_ID).vnfsVnfInstanceIdGet(VNF_ID, CbamRestApiProvider.NOKIA_LCM_API_VERSION)).thenReturn(buildObservable(vnfInfo));
139 when(networkApi.createOrUpdateNetworkGenericVnfsGenericVnf(eq(VNF_ID), payload.capture())).thenReturn(VOID_OBSERVABLE.value());
140 vnfInfo.setName("vnfName");
141 vnfInAaai.setRelationshipList(new ArrayList<>());
142 vnfInAaai.getRelationshipList().add(new Relationship());
144 genericVnfManager.createOrUpdate(VNF_ID, true, VNFM_ID, empty());
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 assertEquals(1, vnfInAaai.getRelationshipList().size());
156 verify(networkApi, times(1)).getNetworkGenericVnfsGenericVnf(VNF_ID, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null);
160 * error is propagated if unable to query VNF from CBAM
163 public void testUnableToQueryVnfFromCBAM() throws Exception {
164 GenericVnf vnfInAaai = new GenericVnf();
165 vnfInAaai.setResourceVersion("v1");
166 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));
167 RuntimeException expectedException = new RuntimeException();
168 when(cbamRestApiProviderForSo.getCbamLcmApi(VNFM_ID).vnfsVnfInstanceIdGet(VNF_ID, CbamRestApiProvider.NOKIA_LCM_API_VERSION)).thenThrow(expectedException);
169 when(networkApi.createOrUpdateNetworkGenericVnfsGenericVnf(eq(VNF_ID), payload.capture())).thenAnswer(invocation -> {
170 vnfInAaai.setResourceVersion("v2");
173 vnfInfo.setName("vnfName");
176 genericVnfManager.createOrUpdate(VNF_ID, true, VNFM_ID, of("nsId"));
177 } catch (Exception e) {
178 verify(logger).error("Unable to query VNF with myVnfId identifier from CBAM", expectedException);
179 assertEquals("Unable to query VNF with myVnfId identifier from CBAM", e.getMessage());
184 * if the VNF is created after the last attempt to query VNF, but before the
185 * the driver creates the VNF it is not created but updated
188 public void testConcurency1() throws Exception {
189 GenericVnf vnfInAaai = new GenericVnf();
190 vnfInAaai.setResourceVersion("v3");
191 Set<Integer> queryCount = new HashSet<>();
192 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 -> {
193 queryCount.add(queryCount.size());
194 if (queryCount.size() >= 11) {
195 return buildObservable(vnfInAaai);
197 throw new NoSuchElementException();
199 when(cbamRestApiProviderForSo.getCbamLcmApi(VNFM_ID).vnfsVnfInstanceIdGet(VNF_ID, CbamRestApiProvider.NOKIA_LCM_API_VERSION)).thenReturn(buildObservable(vnfInfo));
200 RuntimeException runtimeException = new RuntimeException();
201 when(networkApi.createOrUpdateNetworkGenericVnfsGenericVnf(eq(VNF_ID), payload.capture())).thenAnswer(invocation -> {
202 GenericVnf vnfSentToAAi = (GenericVnf) invocation.getArguments()[1];
203 if (vnfSentToAAi.getResourceVersion() == null) {
204 throw runtimeException;
206 return VOID_OBSERVABLE.value();
208 vnfInfo.setName("vnfName");
210 genericVnfManager.createOrUpdate(VNF_ID, true, VNFM_ID, of("nsId"));
212 GenericVnf vnfSentToAai = payload.getValue();
213 assertEquals(VNF_ID, vnfSentToAai.getVnfId());
214 assertEquals(VNF_ID, vnfSentToAai.getVnfId());
215 assertEquals("NokiaVNF", vnfSentToAai.getVnfType());
216 assertEquals(TRUE, vnfSentToAai.isInMaint());
217 assertEquals(TRUE, vnfSentToAai.isIsClosedLoopDisabled());
218 assertEquals("vnfName", vnfSentToAai.getVnfName());
219 assertEquals("v3", vnfSentToAai.getResourceVersion());
220 verify(systemFunctions, times(10)).sleep(3000);
221 verify(networkApi, times(11)).getNetworkGenericVnfsGenericVnf(VNF_ID, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null);
222 verify(networkApi, times(2)).createOrUpdateNetworkGenericVnfsGenericVnf(eq(VNF_ID), any());
223 verify(logger).warn(eq("The VNF with myVnfId identifier did not appear in time"), any(NoSuchElementException.class));
224 verify(logger).warn("The VNF with myVnfId identifier has been created since after the maximal wait for VNF to appear timeout", runtimeException);
225 VOID_OBSERVABLE.assertCalled();
229 * test how entities can refer to a VNF
232 public void testRelations() {
234 Relationship relationship = GenericVnfManager.linkTo(VNF_ID);
236 assertEquals("generic-vnf", relationship.getRelatedTo());
237 assertEquals(1, relationship.getRelationshipData().size());
238 assertEquals("generic-vnf.vnf-id", relationship.getRelationshipData().get(0).getRelationshipKey());
239 assertEquals(VNF_ID, relationship.getRelationshipData().get(0).getRelationshipValue());
246 public void testInheritence() {
247 assertEquals(logger, genericVnfManager.getLogger());