7cddb50eec96baca064da03fbecdee85535c5891
[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 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;
30
31 import java.util.HashSet;
32 import java.util.NoSuchElementException;
33 import java.util.Set;
34 import java.util.concurrent.atomic.AtomicLong;
35
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;
43
44 public class TestGenericVnfManager extends TestBase {
45     private ObjectFactory OBJECT_FACTORY = new ObjectFactory();
46     private ArgumentCaptor<GenericVnf> payload = ArgumentCaptor.forClass(GenericVnf.class);
47
48     @Mock
49     private AAIRestApiProvider aaiRestApiProvider;
50     private GenericVnfManager genericVnfManager;
51     private VnfInfo vnfInfo = new VnfInfo();
52
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());
57                 int i = 0;
58                 for (RelationshipData c : data) {
59                     assertEquals(c.getRelationshipKey(), relationship.getRelationshipData().get(i).getRelationshipKey());
60                     assertEquals(c.getRelationshipValue(), relationship.getRelationshipData().get(i).getRelationshipValue());
61                     i++;
62                 }
63                 return;
64             }
65         }
66         fail();
67     }
68
69     @Before
70     public void init() {
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>() {
75             @Override
76             public Long answer(InvocationOnMock invocation) throws Throwable {
77                 return currentTime.get();
78             }
79         });
80         Mockito.doAnswer(new Answer() {
81             @Override
82             public Object answer(InvocationOnMock invocation) throws Throwable {
83                 currentTime.addAndGet((Long) invocation.getArguments()[0] + 1);
84                 return null;
85             }
86         }).when(systemFunctions).sleep(anyLong());
87     }
88
89     /**
90      * retrieving an existing VNF
91      */
92     @Test
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);
96         //when
97         GenericVnf vnf = genericVnfManager.getExistingVnf(VNF_ID);
98         //verify
99         assertEquals(aaiVnf, vnf);
100     }
101
102     /**
103      * if the VNF does not exist it is created
104      */
105     @Test
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();
112             }
113             return vnfs.iterator().next();
114         });
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 -> {
117             vnfs.add(vnfInAaai);
118             return null;
119         });
120         vnfInfo.setName("vnfName");
121         //when
122         genericVnfManager.createOrUpdate(VNF_ID, true);
123         //verify
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);
133     }
134
135     /**
136      * if the VNF exist it is updated
137      */
138     @Test
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");
146         //when
147         genericVnfManager.createOrUpdate(VNF_ID, true);
148         //verify
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);
158     }
159
160     /**
161      * error is propagated if unable to query VNF from CBAM
162      */
163     @Test
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");
172             return null;
173         });
174         vnfInfo.setName("vnfName");
175         //when
176         try {
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());
181         }
182     }
183
184     /**
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
187      */
188     @Test
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) {
196                 return vnfInAaai;
197             }
198             throw new NoSuchElementException();
199         });
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;
206             }
207             return null;
208         });
209         vnfInfo.setName("vnfName");
210         //when
211         genericVnfManager.createOrUpdate(VNF_ID, true);
212         //verify
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);
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 }