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 org.junit.Before;
20 import org.junit.Test;
21 import org.mockito.ArgumentCaptor;
22 import org.mockito.Mock;
23 import org.mockito.Mockito;
24 import org.mockito.invocation.InvocationOnMock;
25 import org.mockito.stubbing.Answer;
26 import org.onap.aai.domain.yang.v11.*;
27 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.direct.AAIRestApiProvider;
28 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CbamRestApiProvider;
29 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.TestBase;
31 import java.util.HashSet;
32 import java.util.NoSuchElementException;
34 import java.util.concurrent.atomic.AtomicLong;
36 import static junit.framework.TestCase.assertEquals;
37 import static junit.framework.TestCase.fail;
38 import static org.mockito.Matchers.anyLong;
39 import static org.mockito.Matchers.eq;
40 import static org.mockito.Mockito.*;
41 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.direct.AAIRestApiProvider.AAIService.NETWORK;
42 import static org.springframework.test.util.ReflectionTestUtils.setField;
44 public class TestGenericVnfManager extends TestBase {
45 private ObjectFactory OBJECT_FACTORY = new ObjectFactory();
46 private ArgumentCaptor<GenericVnf> payload = ArgumentCaptor.forClass(GenericVnf.class);
49 private AAIRestApiProvider aaiRestApiProvider;
50 private GenericVnfManager genericVnfManager;
51 private VnfInfo vnfInfo = new VnfInfo();
53 static void assertRelation(RelationshipList relationShips, String relatedTo, RelationshipData... data) {
54 for (Relationship relationship : relationShips.getRelationship()) {
55 if (relationship.getRelatedTo().equals(relatedTo)) {
56 assertEquals(data.length, relationship.getRelationshipData().size());
58 for (RelationshipData c : data) {
59 assertEquals(c.getRelationshipKey(), relationship.getRelationshipData().get(i).getRelationshipKey());
60 assertEquals(c.getRelationshipValue(), relationship.getRelationshipData().get(i).getRelationshipValue());
71 genericVnfManager = new GenericVnfManager(aaiRestApiProvider, cbamRestApiProvider, driverProperties);
72 setField(GenericVnfManager.class, "logger", logger);
73 AtomicLong currentTime = new AtomicLong(0L);
74 when(systemFunctions.currentTimeMillis()).thenAnswer(new Answer<Long>() {
76 public Long answer(InvocationOnMock invocation) throws Throwable {
77 return currentTime.get();
80 Mockito.doAnswer(new Answer() {
82 public Object answer(InvocationOnMock invocation) throws Throwable {
83 currentTime.addAndGet((Long) invocation.getArguments()[0] + 1);
86 }).when(systemFunctions).sleep(anyLong());
90 * retrieving an existing VNF
93 public void testGetExistingVnf() throws Exception {
94 GenericVnf aaiVnf = OBJECT_FACTORY.createGenericVnf();
95 when(aaiRestApiProvider.get(logger, NETWORK, "/generic-vnfs/generic-vnf/" + VNF_ID, GenericVnf.class)).thenReturn(aaiVnf);
97 GenericVnf vnf = genericVnfManager.getExistingVnf(VNF_ID);
99 assertEquals(aaiVnf, vnf);
103 * if the VNF does not exist it is created
106 public void createNonExistingVnf() throws Exception {
107 GenericVnf vnfInAaai = OBJECT_FACTORY.createGenericVnf();
108 Set<GenericVnf> vnfs = new HashSet<>();
109 when(aaiRestApiProvider.get(logger, NETWORK, "/generic-vnfs/generic-vnf/" + VNF_ID, GenericVnf.class)).thenAnswer((Answer<GenericVnf>) invocation -> {
110 if (vnfs.size() == 0) {
111 throw new NoSuchElementException();
113 return vnfs.iterator().next();
115 when(cbamRestApiProvider.getCbamLcmApi(VNFM_ID).vnfsVnfInstanceIdGet(VNF_ID, CbamRestApiProvider.NOKIA_LCM_API_VERSION)).thenReturn(buildObservable(vnfInfo));
116 when(aaiRestApiProvider.put(eq(logger), eq(NETWORK), eq("/generic-vnfs/generic-vnf/" + VNF_ID), payload.capture(), eq(Void.class))).thenAnswer(invocation -> {
120 vnfInfo.setName("vnfName");
122 genericVnfManager.createOrUpdate(VNF_ID, true);
124 GenericVnf vnfSentToAai = payload.getValue();
125 assertEquals(VNF_ID, vnfSentToAai.getVnfId());
126 assertEquals(VNF_ID, vnfSentToAai.getVnfInstanceId());
127 assertEquals("NokiaVNF", vnfSentToAai.getVnfType());
128 assertEquals(true, vnfSentToAai.isInMaint());
129 assertEquals(true, vnfSentToAai.isIsClosedLoopDisabled());
130 assertEquals("vnfName", vnfSentToAai.getVnfName());
131 verify(systemFunctions, times(10)).sleep(3000);
132 verify(aaiRestApiProvider, times(10)).get(logger, NETWORK, "/generic-vnfs/generic-vnf/" + VNF_ID, GenericVnf.class);
136 * if the VNF exist it is updated
139 public void testUpdateExistingVnf() throws Exception {
140 GenericVnf vnfInAaai = OBJECT_FACTORY.createGenericVnf();
141 vnfInAaai.setResourceVersion("v1");
142 when(aaiRestApiProvider.get(logger, NETWORK, "/generic-vnfs/generic-vnf/" + VNF_ID, GenericVnf.class)).thenReturn(vnfInAaai);
143 when(cbamRestApiProvider.getCbamLcmApi(VNFM_ID).vnfsVnfInstanceIdGet(VNF_ID, CbamRestApiProvider.NOKIA_LCM_API_VERSION)).thenReturn(buildObservable(vnfInfo));
144 when(aaiRestApiProvider.put(eq(logger), eq(NETWORK), eq("/generic-vnfs/generic-vnf/" + VNF_ID), payload.capture(), eq(Void.class))).thenReturn(null);
145 vnfInfo.setName("vnfName");
147 genericVnfManager.createOrUpdate(VNF_ID, true);
149 GenericVnf vnfSentToAai = payload.getValue();
150 assertEquals(VNF_ID, vnfSentToAai.getVnfId());
151 assertEquals(VNF_ID, vnfSentToAai.getVnfInstanceId());
152 assertEquals("NokiaVNF", vnfSentToAai.getVnfType());
153 assertEquals(true, vnfSentToAai.isInMaint());
154 assertEquals(true, vnfSentToAai.isIsClosedLoopDisabled());
155 assertEquals("vnfName", vnfSentToAai.getVnfName());
156 verify(systemFunctions, never()).sleep(anyLong());
157 verify(aaiRestApiProvider, times(1)).get(logger, NETWORK, "/generic-vnfs/generic-vnf/" + VNF_ID, GenericVnf.class);
161 * error is propagated if unable to query VNF from CBAM
164 public void testUnableToQueryVnfFromCBAM() throws Exception {
165 GenericVnf vnfInAaai = OBJECT_FACTORY.createGenericVnf();
166 vnfInAaai.setResourceVersion("v1");
167 when(aaiRestApiProvider.get(logger, NETWORK, "/generic-vnfs/generic-vnf/" + VNF_ID, GenericVnf.class)).thenReturn(vnfInAaai);
168 RuntimeException expectedException = new RuntimeException();
169 when(cbamRestApiProvider.getCbamLcmApi(VNFM_ID).vnfsVnfInstanceIdGet(VNF_ID, CbamRestApiProvider.NOKIA_LCM_API_VERSION)).thenThrow(expectedException);
170 when(aaiRestApiProvider.put(eq(logger), eq(NETWORK), eq("/generic-vnfs/generic-vnf/" + VNF_ID), payload.capture(), eq(Void.class))).thenAnswer(invocation -> {
171 vnfInAaai.setResourceVersion("v2");
174 vnfInfo.setName("vnfName");
177 genericVnfManager.createOrUpdate(VNF_ID, true);
178 } catch (Exception e) {
179 verify(logger).error("Unable to query VNF with myVnfId identifier from CBAM", expectedException);
180 assertEquals("Unable to query VNF with myVnfId identifier from CBAM", e.getMessage());
185 * if the VNF is created after the last attempt to query VNF, but before the
186 * the driver creates the VNF it is not created but updated
189 public void testConcurency1() throws Exception {
190 GenericVnf vnfInAaai = OBJECT_FACTORY.createGenericVnf();
191 vnfInAaai.setResourceVersion("v3");
192 Set<Integer> queryCount = new HashSet<>();
193 when(aaiRestApiProvider.get(logger, NETWORK, "/generic-vnfs/generic-vnf/" + VNF_ID, GenericVnf.class)).thenAnswer((Answer<GenericVnf>) invocation -> {
194 queryCount.add(queryCount.size());
195 if (queryCount.size() >= 11) {
198 throw new NoSuchElementException();
200 when(cbamRestApiProvider.getCbamLcmApi(VNFM_ID).vnfsVnfInstanceIdGet(VNF_ID, CbamRestApiProvider.NOKIA_LCM_API_VERSION)).thenReturn(buildObservable(vnfInfo));
201 RuntimeException runtimeException = new RuntimeException();
202 when(aaiRestApiProvider.put(eq(logger), eq(NETWORK), eq("/generic-vnfs/generic-vnf/" + VNF_ID), payload.capture(), eq(Void.class))).thenAnswer(invocation -> {
203 GenericVnf vnfSentToAAi = (GenericVnf) invocation.getArguments()[3];
204 if (vnfSentToAAi.getResourceVersion() == null) {
205 throw runtimeException;
209 vnfInfo.setName("vnfName");
211 genericVnfManager.createOrUpdate(VNF_ID, true);
213 GenericVnf vnfSentToAai = payload.getValue();
214 assertEquals(VNF_ID, vnfSentToAai.getVnfId());
215 assertEquals(VNF_ID, vnfSentToAai.getVnfInstanceId());
216 assertEquals("NokiaVNF", vnfSentToAai.getVnfType());
217 assertEquals(true, vnfSentToAai.isInMaint());
218 assertEquals(true, vnfSentToAai.isIsClosedLoopDisabled());
219 assertEquals("vnfName", vnfSentToAai.getVnfName());
220 assertEquals("v3", vnfSentToAai.getResourceVersion());
221 verify(systemFunctions, times(10)).sleep(3000);
222 verify(aaiRestApiProvider, times(11)).get(logger, NETWORK, "/generic-vnfs/generic-vnf/" + VNF_ID, GenericVnf.class);
223 verify(aaiRestApiProvider, times(2)).put(eq(logger), eq(NETWORK), eq("/generic-vnfs/generic-vnf/" + VNF_ID), any(), eq(Void.class));
224 verify(logger).warn(eq("The VNF with myVnfId identifier did not appear in time"), any(NoSuchElementException.class));
225 verify(logger).warn("The VNF with myVnfId identifier has been created since after the maximal wait for VNF to appear timeout", runtimeException);
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());