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.direct.AAIRestApiProvider;
37 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CbamRestApiProvider;
38 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.TestBase;
40 import static java.lang.Boolean.TRUE;
42 import static junit.framework.TestCase.assertEquals;
43 import static junit.framework.TestCase.fail;
44 import static org.mockito.Matchers.anyLong;
45 import static org.mockito.Matchers.eq;
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);
53 private AAIRestApiProvider aaiRestApiProvider;
55 private NetworkApi networkApi;
56 private GenericVnfManager genericVnfManager;
57 private VnfInfo vnfInfo = new VnfInfo();
59 static void assertRelation(List<Relationship> relationShips, String relatedTo, RelationshipData... data) {
60 for (Relationship relationship : relationShips) {
61 if (relationship.getRelatedTo().equals(relatedTo)) {
62 assertEquals(data.length, relationship.getRelationshipData().size());
64 for (RelationshipData c : data) {
65 assertEquals(c.getRelationshipKey(), relationship.getRelationshipData().get(i).getRelationshipKey());
66 assertEquals(c.getRelationshipValue(), relationship.getRelationshipData().get(i).getRelationshipValue());
77 when(aaiRestApiProvider.getNetworkApi()).thenReturn(networkApi);
78 genericVnfManager = new GenericVnfManager(aaiRestApiProvider, cbamRestApiProvider, driverProperties);
79 setField(GenericVnfManager.class, "logger", logger);
80 AtomicLong currentTime = new AtomicLong(0L);
81 when(systemFunctions.currentTimeMillis()).thenAnswer(new Answer<Long>() {
83 public Long answer(InvocationOnMock invocation) throws Throwable {
84 return currentTime.get();
87 Mockito.doAnswer(new Answer() {
89 public Object answer(InvocationOnMock invocation) throws Throwable {
90 currentTime.addAndGet((Long) invocation.getArguments()[0] + 1);
93 }).when(systemFunctions).sleep(anyLong());
97 * if the VNF does not exist it is created
100 public void createNonExistingVnf() throws Exception {
101 GenericVnf vnfInAaai = new GenericVnf();
102 Set<GenericVnf> vnfs = new HashSet<>();
103 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 -> {
104 if (vnfs.size() == 0) {
105 throw new NoSuchElementException();
107 return buildObservable(vnfs.iterator().next());
109 when(cbamRestApiProvider.getCbamLcmApi(VNFM_ID).vnfsVnfInstanceIdGet(VNF_ID, CbamRestApiProvider.NOKIA_LCM_API_VERSION)).thenReturn(buildObservable(vnfInfo));
110 when(networkApi.createOrUpdateNetworkGenericVnfsGenericVnf(eq(VNF_ID), payload.capture())).thenAnswer(invocation -> {
114 vnfInfo.setName("vnfName");
116 genericVnfManager.createOrUpdate(VNF_ID, true);
118 GenericVnf vnfSentToAai = payload.getValue();
119 assertEquals(VNF_ID, vnfSentToAai.getVnfId());
120 assertEquals(VNF_ID, vnfSentToAai.getVnfId());
121 assertEquals("NokiaVNF", vnfSentToAai.getVnfType());
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);
130 * if the VNF exist it is updated
133 public void testUpdateExistingVnf() throws Exception {
134 GenericVnf vnfInAaai = new GenericVnf();
135 vnfInAaai.setResourceVersion("v1");
136 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));
137 when(cbamRestApiProvider.getCbamLcmApi(VNFM_ID).vnfsVnfInstanceIdGet(VNF_ID, CbamRestApiProvider.NOKIA_LCM_API_VERSION)).thenReturn(buildObservable(vnfInfo));
138 when(networkApi.createOrUpdateNetworkGenericVnfsGenericVnf(eq(VNF_ID), payload.capture())).thenReturn(null);
139 vnfInfo.setName("vnfName");
141 genericVnfManager.createOrUpdate(VNF_ID, true);
143 GenericVnf vnfSentToAai = payload.getValue();
144 assertEquals(VNF_ID, vnfSentToAai.getVnfId());
145 assertEquals(VNF_ID, vnfSentToAai.getVnfId());
146 assertEquals("NokiaVNF", vnfSentToAai.getVnfType());
147 assertEquals(TRUE, vnfSentToAai.isInMaint());
148 assertEquals(TRUE, vnfSentToAai.isIsClosedLoopDisabled());
149 assertEquals("vnfName", vnfSentToAai.getVnfName());
150 verify(systemFunctions, never()).sleep(anyLong());
151 verify(networkApi, times(1)).getNetworkGenericVnfsGenericVnf(VNF_ID, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null);
155 * error is propagated if unable to query VNF from CBAM
158 public void testUnableToQueryVnfFromCBAM() throws Exception {
159 GenericVnf vnfInAaai = new GenericVnf();
160 vnfInAaai.setResourceVersion("v1");
161 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));
162 RuntimeException expectedException = new RuntimeException();
163 when(cbamRestApiProvider.getCbamLcmApi(VNFM_ID).vnfsVnfInstanceIdGet(VNF_ID, CbamRestApiProvider.NOKIA_LCM_API_VERSION)).thenThrow(expectedException);
164 when(networkApi.createOrUpdateNetworkGenericVnfsGenericVnf(eq(VNF_ID), payload.capture())).thenAnswer(invocation -> {
165 vnfInAaai.setResourceVersion("v2");
168 vnfInfo.setName("vnfName");
171 genericVnfManager.createOrUpdate(VNF_ID, true);
172 } catch (Exception e) {
173 verify(logger).error("Unable to query VNF with myVnfId identifier from CBAM", expectedException);
174 assertEquals("Unable to query VNF with myVnfId identifier from CBAM", e.getMessage());
179 * if the VNF is created after the last attempt to query VNF, but before the
180 * the driver creates the VNF it is not created but updated
183 public void testConcurency1() throws Exception {
184 GenericVnf vnfInAaai = new GenericVnf();
185 vnfInAaai.setResourceVersion("v3");
186 Set<Integer> queryCount = new HashSet<>();
187 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 -> {
188 queryCount.add(queryCount.size());
189 if (queryCount.size() >= 11) {
190 return buildObservable(vnfInAaai);
192 throw new NoSuchElementException();
194 when(cbamRestApiProvider.getCbamLcmApi(VNFM_ID).vnfsVnfInstanceIdGet(VNF_ID, CbamRestApiProvider.NOKIA_LCM_API_VERSION)).thenReturn(buildObservable(vnfInfo));
195 RuntimeException runtimeException = new RuntimeException();
196 when(networkApi.createOrUpdateNetworkGenericVnfsGenericVnf(eq(VNF_ID), payload.capture())).thenAnswer(invocation -> {
197 GenericVnf vnfSentToAAi = (GenericVnf) invocation.getArguments()[1];
198 if (vnfSentToAAi.getResourceVersion() == null) {
199 throw runtimeException;
203 vnfInfo.setName("vnfName");
205 genericVnfManager.createOrUpdate(VNF_ID, true);
207 GenericVnf vnfSentToAai = payload.getValue();
208 assertEquals(VNF_ID, vnfSentToAai.getVnfId());
209 assertEquals(VNF_ID, vnfSentToAai.getVnfId());
210 assertEquals("NokiaVNF", vnfSentToAai.getVnfType());
211 assertEquals(TRUE, vnfSentToAai.isInMaint());
212 assertEquals(TRUE, vnfSentToAai.isIsClosedLoopDisabled());
213 assertEquals("vnfName", vnfSentToAai.getVnfName());
214 assertEquals("v3", vnfSentToAai.getResourceVersion());
215 verify(systemFunctions, times(10)).sleep(3000);
216 verify(networkApi, times(11)).getNetworkGenericVnfsGenericVnf(VNF_ID, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null);
217 verify(networkApi, times(2)).createOrUpdateNetworkGenericVnfsGenericVnf(eq(VNF_ID), any());
218 verify(logger).warn(eq("The VNF with myVnfId identifier did not appear in time"), any(NoSuchElementException.class));
219 verify(logger).warn("The VNF with myVnfId identifier has been created since after the maximal wait for VNF to appear timeout", runtimeException);
223 * test how entities can refer to a VNF
226 public void testRelations() {
228 Relationship relationship = GenericVnfManager.linkTo(VNF_ID);
230 assertEquals("generic-vnf", relationship.getRelatedTo());
231 assertEquals(1, relationship.getRelationshipData().size());
232 assertEquals("generic-vnf.vnf-id", relationship.getRelationshipData().get(0).getRelationshipKey());
233 assertEquals(VNF_ID, relationship.getRelationshipData().get(0).getRelationshipValue());
240 public void testInheritence() {
241 assertEquals(logger, genericVnfManager.getLogger());