Update docker image to fix CSIT failure
[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.*;
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;
37
38 import static java.lang.Boolean.TRUE;
39 import static java.util.Optional.empty;
40 import static java.util.Optional.of;
41
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;
46
47 public class TestGenericVnfManager extends TestBase {
48     private ArgumentCaptor<GenericVnf> payload = ArgumentCaptor.forClass(GenericVnf.class);
49
50
51     @Mock
52     private AAIRestApiProvider aaiRestApiProvider;
53     @Mock
54     private NetworkApi networkApi;
55     private GenericVnfManager genericVnfManager;
56     private VnfInfo vnfInfo = new VnfInfo();
57
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());
62                 int i = 0;
63                 for (RelationshipData c : data) {
64                     assertEquals(c.getRelationshipKey(), relationship.getRelationshipData().get(i).getRelationshipKey());
65                     assertEquals(c.getRelationshipValue(), relationship.getRelationshipData().get(i).getRelationshipValue());
66                     i++;
67                 }
68                 return;
69             }
70         }
71         fail();
72     }
73
74     @Before
75     public void init() {
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>() {
81             @Override
82             public Long answer(InvocationOnMock invocation) throws Throwable {
83                 return currentTime.get();
84             }
85         });
86         Mockito.doAnswer(new Answer() {
87             @Override
88             public Object answer(InvocationOnMock invocation) throws Throwable {
89                 currentTime.addAndGet((Long) invocation.getArguments()[0] + 1);
90                 return null;
91             }
92         }).when(systemFunctions).sleep(anyLong());
93     }
94
95     /**
96      * if the VNF does not exist it is created
97      */
98     @Test
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();
105             }
106             return buildObservable(vnfs.iterator().next());
107         });
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 -> {
110             vnfs.add(vnfInAaai);
111             return VOID_OBSERVABLE.value();
112         });
113         vnfInfo.setName("vnfName");
114         //when
115         genericVnfManager.createOrUpdate(VNF_ID, true, VNFM_ID, of("nsId"));
116         //verify
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();
128     }
129
130     /**
131      * if the VNF exist it is updated
132      */
133     @Test
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());
143         //when
144         genericVnfManager.createOrUpdate(VNF_ID, true, VNFM_ID, empty());
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         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);
157     }
158
159     /**
160      * error is propagated if unable to query VNF from CBAM
161      */
162     @Test
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");
171             return null;
172         });
173         vnfInfo.setName("vnfName");
174         //when
175         try {
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());
180         }
181     }
182
183     /**
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
186      */
187     @Test
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);
196             }
197             throw new NoSuchElementException();
198         });
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;
205             }
206             return VOID_OBSERVABLE.value();
207         });
208         vnfInfo.setName("vnfName");
209         //when
210         genericVnfManager.createOrUpdate(VNF_ID, true, VNFM_ID, of("nsId"));
211         //verify
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();
226     }
227
228     /**
229      * test how entities can refer to a VNF
230      */
231     @Test
232     public void testRelations() {
233         //when
234         Relationship relationship = GenericVnfManager.linkTo(VNF_ID);
235         //verify
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());
240     }
241
242     /**
243      * test inheritence
244      */
245     @Test
246     public void testInheritence() {
247         assertEquals(logger, genericVnfManager.getLogger());
248     }
249 }