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.ApiException;
19 import com.nokia.cbam.lcm.v32.model.VnfInfo;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.mockito.ArgumentCaptor;
23 import org.mockito.Mock;
24 import org.mockito.Mockito;
25 import org.mockito.invocation.InvocationOnMock;
26 import org.mockito.stubbing.Answer;
27 import org.onap.aai.domain.yang.v11.*;
28 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.direct.AAIRestApiProvider;
29 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CbamRestApiProvider;
30 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.TestBase;
32 import java.util.HashSet;
33 import java.util.NoSuchElementException;
35 import java.util.concurrent.atomic.AtomicLong;
37 import static junit.framework.TestCase.assertEquals;
38 import static junit.framework.TestCase.fail;
39 import static org.mockito.Matchers.anyLong;
40 import static org.mockito.Matchers.eq;
41 import static org.mockito.Mockito.*;
42 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.direct.AAIRestApiProvider.AAIService.NETWORK;
43 import static org.springframework.test.util.ReflectionTestUtils.setField;
45 public class TestGenericVnfManager extends TestBase {
46 private ObjectFactory OBJECT_FACTORY = new ObjectFactory();
47 private ArgumentCaptor<GenericVnf> payload = ArgumentCaptor.forClass(GenericVnf.class);
50 private AAIRestApiProvider aaiRestApiProvider;
51 private GenericVnfManager genericVnfManager;
52 private VnfInfo vnfInfo = new VnfInfo();
54 static void assertRelation(RelationshipList relationShips, String relatedTo, RelationshipData... data) {
55 for (Relationship relationship : relationShips.getRelationship()) {
56 if (relationship.getRelatedTo().equals(relatedTo)) {
57 assertEquals(data.length, relationship.getRelationshipData().size());
59 for (RelationshipData c : data) {
60 assertEquals(c.getRelationshipKey(), relationship.getRelationshipData().get(i).getRelationshipKey());
61 assertEquals(c.getRelationshipValue(), relationship.getRelationshipData().get(i).getRelationshipValue());
72 genericVnfManager = new GenericVnfManager(aaiRestApiProvider, cbamRestApiProvider, driverProperties);
73 setField(GenericVnfManager.class, "logger", logger);
74 AtomicLong currentTime = new AtomicLong(0L);
75 when(systemFunctions.currentTimeMillis()).thenAnswer(new Answer<Long>() {
77 public Long answer(InvocationOnMock invocation) throws Throwable {
78 return currentTime.get();
81 Mockito.doAnswer(new Answer() {
83 public Object answer(InvocationOnMock invocation) throws Throwable {
84 currentTime.addAndGet((Long) invocation.getArguments()[0] + 1);
87 }).when(systemFunctions).sleep(anyLong());
91 * retrieving an existing VNF
94 public void testGetExistingVnf() throws Exception {
95 GenericVnf aaiVnf = OBJECT_FACTORY.createGenericVnf();
96 when(aaiRestApiProvider.get(logger, NETWORK, "/generic-vnfs/generic-vnf/" + VNF_ID, GenericVnf.class)).thenReturn(aaiVnf);
98 GenericVnf vnf = genericVnfManager.getExistingVnf(VNF_ID);
100 assertEquals(aaiVnf, vnf);
104 * if the VNF does not exist it is created
107 public void createNonExistingVnf() throws Exception {
108 GenericVnf vnfInAaai = OBJECT_FACTORY.createGenericVnf();
109 Set<GenericVnf> vnfs = new HashSet<>();
110 when(aaiRestApiProvider.get(logger, NETWORK, "/generic-vnfs/generic-vnf/" + VNF_ID, GenericVnf.class)).thenAnswer((Answer<GenericVnf>) invocation -> {
111 if (vnfs.size() == 0) {
112 throw new NoSuchElementException();
114 return vnfs.iterator().next();
116 when(cbamRestApiProvider.getCbamLcmApi(VNFM_ID).vnfsVnfInstanceIdGet(VNF_ID, CbamRestApiProvider.NOKIA_LCM_API_VERSION)).thenReturn(vnfInfo);
117 when(aaiRestApiProvider.put(eq(logger), eq(NETWORK), eq("/generic-vnfs/generic-vnf/" + VNF_ID), payload.capture(), eq(Void.class))).thenAnswer(invocation -> {
121 vnfInfo.setName("vnfName");
123 genericVnfManager.createOrUpdate(VNF_ID, true);
125 GenericVnf vnfSentToAai = payload.getValue();
126 assertEquals(VNF_ID, vnfSentToAai.getVnfId());
127 assertEquals(VNF_ID, vnfSentToAai.getVnfInstanceId());
128 assertEquals("NokiaVNF", vnfSentToAai.getVnfType());
129 assertEquals(true, vnfSentToAai.isInMaint());
130 assertEquals(true, vnfSentToAai.isIsClosedLoopDisabled());
131 assertEquals("vnfName", vnfSentToAai.getVnfName());
132 verify(systemFunctions, times(10)).sleep(3000);
133 verify(aaiRestApiProvider, times(10)).get(logger, NETWORK, "/generic-vnfs/generic-vnf/" + VNF_ID, GenericVnf.class);
137 * if the VNF exist it is updated
140 public void testUpdateExistingVnf() throws Exception {
141 GenericVnf vnfInAaai = OBJECT_FACTORY.createGenericVnf();
142 vnfInAaai.setResourceVersion("v1");
143 when(aaiRestApiProvider.get(logger, NETWORK, "/generic-vnfs/generic-vnf/" + VNF_ID, GenericVnf.class)).thenReturn(vnfInAaai);
144 when(cbamRestApiProvider.getCbamLcmApi(VNFM_ID).vnfsVnfInstanceIdGet(VNF_ID, CbamRestApiProvider.NOKIA_LCM_API_VERSION)).thenReturn(vnfInfo);
145 when(aaiRestApiProvider.put(eq(logger), eq(NETWORK), eq("/generic-vnfs/generic-vnf/" + VNF_ID), payload.capture(), eq(Void.class))).thenReturn(null);
146 vnfInfo.setName("vnfName");
148 genericVnfManager.createOrUpdate(VNF_ID, true);
150 GenericVnf vnfSentToAai = payload.getValue();
151 assertEquals(VNF_ID, vnfSentToAai.getVnfId());
152 assertEquals(VNF_ID, vnfSentToAai.getVnfInstanceId());
153 assertEquals("NokiaVNF", vnfSentToAai.getVnfType());
154 assertEquals(true, vnfSentToAai.isInMaint());
155 assertEquals(true, vnfSentToAai.isIsClosedLoopDisabled());
156 assertEquals("vnfName", vnfSentToAai.getVnfName());
157 verify(systemFunctions, never()).sleep(anyLong());
158 verify(aaiRestApiProvider, times(1)).get(logger, NETWORK, "/generic-vnfs/generic-vnf/" + VNF_ID, GenericVnf.class);
162 * error is propagated if unable to query VNF from CBAM
165 public void testUnableToQueryVnfFromCBAM() throws Exception {
166 GenericVnf vnfInAaai = OBJECT_FACTORY.createGenericVnf();
167 vnfInAaai.setResourceVersion("v1");
168 when(aaiRestApiProvider.get(logger, NETWORK, "/generic-vnfs/generic-vnf/" + VNF_ID, GenericVnf.class)).thenReturn(vnfInAaai);
169 ApiException expectedException = new ApiException();
170 when(cbamRestApiProvider.getCbamLcmApi(VNFM_ID).vnfsVnfInstanceIdGet(VNF_ID, CbamRestApiProvider.NOKIA_LCM_API_VERSION)).thenThrow(expectedException);
171 when(aaiRestApiProvider.put(eq(logger), eq(NETWORK), eq("/generic-vnfs/generic-vnf/" + VNF_ID), payload.capture(), eq(Void.class))).thenAnswer(invocation -> {
172 vnfInAaai.setResourceVersion("v2");
175 vnfInfo.setName("vnfName");
178 genericVnfManager.createOrUpdate(VNF_ID, true);
179 } catch (Exception e) {
180 verify(logger).error("Unable to query VNF with myVnfId identifier from CBAM", expectedException);
181 assertEquals("Unable to query VNF with myVnfId identifier from CBAM", e.getMessage());
186 * if the VNF is created after the last attempt to query VNF, but before the
187 * the driver creates the VNF it is not created but updated
190 public void testConcurency1() throws Exception {
191 GenericVnf vnfInAaai = OBJECT_FACTORY.createGenericVnf();
192 vnfInAaai.setResourceVersion("v3");
193 Set<Integer> queryCount = new HashSet<>();
194 when(aaiRestApiProvider.get(logger, NETWORK, "/generic-vnfs/generic-vnf/" + VNF_ID, GenericVnf.class)).thenAnswer((Answer<GenericVnf>) invocation -> {
195 queryCount.add(queryCount.size());
196 if (queryCount.size() >= 11) {
199 throw new NoSuchElementException();
201 when(cbamRestApiProvider.getCbamLcmApi(VNFM_ID).vnfsVnfInstanceIdGet(VNF_ID, CbamRestApiProvider.NOKIA_LCM_API_VERSION)).thenReturn(vnfInfo);
202 RuntimeException runtimeException = new RuntimeException();
203 when(aaiRestApiProvider.put(eq(logger), eq(NETWORK), eq("/generic-vnfs/generic-vnf/" + VNF_ID), payload.capture(), eq(Void.class))).thenAnswer(invocation -> {
204 GenericVnf vnfSentToAAi = (GenericVnf) invocation.getArguments()[3];
205 if (vnfSentToAAi.getResourceVersion() == null) {
206 throw runtimeException;
210 vnfInfo.setName("vnfName");
212 genericVnfManager.createOrUpdate(VNF_ID, true);
214 GenericVnf vnfSentToAai = payload.getValue();
215 assertEquals(VNF_ID, vnfSentToAai.getVnfId());
216 assertEquals(VNF_ID, vnfSentToAai.getVnfInstanceId());
217 assertEquals("NokiaVNF", vnfSentToAai.getVnfType());
218 assertEquals(true, vnfSentToAai.isInMaint());
219 assertEquals(true, vnfSentToAai.isIsClosedLoopDisabled());
220 assertEquals("vnfName", vnfSentToAai.getVnfName());
221 assertEquals("v3", vnfSentToAai.getResourceVersion());
222 verify(systemFunctions, times(10)).sleep(3000);
223 verify(aaiRestApiProvider, times(11)).get(logger, NETWORK, "/generic-vnfs/generic-vnf/" + VNF_ID, GenericVnf.class);
224 verify(aaiRestApiProvider, times(2)).put(eq(logger), eq(NETWORK), eq("/generic-vnfs/generic-vnf/" + VNF_ID), anyString(), eq(Void.class));
225 verify(logger).warn(eq("The VNF with myVnfId identifier did not appear in time"), any(NoSuchElementException.class));
226 verify(logger).warn("The VNF with myVnfId identifier has been created since after the maximal wait for VNF to appear timeout", runtimeException);
230 * test how entities can refer to a VNF
233 public void testRelations() {
235 Relationship relationship = GenericVnfManager.linkTo(VNF_ID);
237 assertEquals("generic-vnf", relationship.getRelatedTo());
238 assertEquals(1, relationship.getRelationshipData().size());
239 assertEquals("generic-vnf.vnf-id", relationship.getRelationshipData().get(0).getRelationshipKey());
240 assertEquals(VNF_ID, relationship.getRelationshipData().get(0).getRelationshipValue());
247 public void testInheritence() {
248 assertEquals(logger, genericVnfManager.getLogger());