5ade389de051a2eec45b6b91f4d266b9ad1cb517
[vfc/nfvo/driver/vnfm/svnfm.git] / nokiav2 / driver / src / test / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / nokia / onap / direct / notification / TestGenericVnfManager.java
1 /*
2  * Copyright 2016-2017, Nokia Corporation
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.direct.notification;
17
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;
23 import java.util.Set;
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;
40
41 import static java.lang.Boolean.TRUE;
42 import static java.util.Optional.of;
43
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;
48
49 public class TestGenericVnfManager extends TestBase {
50     private ArgumentCaptor<GenericVnf> payload = ArgumentCaptor.forClass(GenericVnf.class);
51
52
53     @Mock
54     private AAIRestApiProvider aaiRestApiProvider;
55     @Mock
56     private NetworkApi networkApi;
57     private GenericVnfManager genericVnfManager;
58     private VnfInfo vnfInfo = new VnfInfo();
59
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());
64                 int i = 0;
65                 for (RelationshipData c : data) {
66                     assertEquals(c.getRelationshipKey(), relationship.getRelationshipData().get(i).getRelationshipKey());
67                     assertEquals(c.getRelationshipValue(), relationship.getRelationshipData().get(i).getRelationshipValue());
68                     i++;
69                 }
70                 return;
71             }
72         }
73         fail();
74     }
75
76     @Before
77     public void init() {
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>() {
83             @Override
84             public Long answer(InvocationOnMock invocation) throws Throwable {
85                 return currentTime.get();
86             }
87         });
88         Mockito.doAnswer(new Answer() {
89             @Override
90             public Object answer(InvocationOnMock invocation) throws Throwable {
91                 currentTime.addAndGet((Long) invocation.getArguments()[0] + 1);
92                 return null;
93             }
94         }).when(systemFunctions).sleep(anyLong());
95     }
96
97     /**
98      * if the VNF does not exist it is created
99      */
100     @Test
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();
107             }
108             return buildObservable(vnfs.iterator().next());
109         });
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 -> {
112             vnfs.add(vnfInAaai);
113             return VOID_OBSERVABLE.value();
114         });
115         vnfInfo.setName("vnfName");
116         //when
117         genericVnfManager.createOrUpdate(VNF_ID, true, VNFM_ID, of("nsId"));
118         //verify
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();
130     }
131
132     /**
133      * if the VNF exist it is updated
134      */
135     @Test
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");
143         //when
144         genericVnfManager.createOrUpdate(VNF_ID, true, VNFM_ID, of("nsId"));
145         //verify
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);
156     }
157
158     /**
159      * error is propagated if unable to query VNF from CBAM
160      */
161     @Test
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");
170             return null;
171         });
172         vnfInfo.setName("vnfName");
173         //when
174         try {
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());
179         }
180     }
181
182     /**
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
185      */
186     @Test
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);
195             }
196             throw new NoSuchElementException();
197         });
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;
204             }
205             return VOID_OBSERVABLE.value();
206         });
207         vnfInfo.setName("vnfName");
208         //when
209         genericVnfManager.createOrUpdate(VNF_ID, true, VNFM_ID, of("nsId"));
210         //verify
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();
225     }
226
227     /**
228      * test how entities can refer to a VNF
229      */
230     @Test
231     public void testRelations() {
232         //when
233         Relationship relationship = GenericVnfManager.linkTo(VNF_ID);
234         //verify
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());
239     }
240
241     /**
242      * test inheritence
243      */
244     @Test
245     public void testInheritence() {
246         assertEquals(logger, genericVnfManager.getLogger());
247     }
248 }