add direct information source
[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.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;
31
32 import java.util.HashSet;
33 import java.util.NoSuchElementException;
34 import java.util.Set;
35 import java.util.concurrent.atomic.AtomicLong;
36
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;
44
45 public class TestGenericVnfManager extends TestBase {
46     private ObjectFactory OBJECT_FACTORY = new ObjectFactory();
47     private ArgumentCaptor<GenericVnf> payload = ArgumentCaptor.forClass(GenericVnf.class);
48
49     @Mock
50     private AAIRestApiProvider aaiRestApiProvider;
51     private GenericVnfManager genericVnfManager;
52     private VnfInfo vnfInfo = new VnfInfo();
53
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());
58                 int i = 0;
59                 for (RelationshipData c : data) {
60                     assertEquals(c.getRelationshipKey(), relationship.getRelationshipData().get(i).getRelationshipKey());
61                     assertEquals(c.getRelationshipValue(), relationship.getRelationshipData().get(i).getRelationshipValue());
62                     i++;
63                 }
64                 return;
65             }
66         }
67         fail();
68     }
69
70     @Before
71     public void init() {
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>() {
76             @Override
77             public Long answer(InvocationOnMock invocation) throws Throwable {
78                 return currentTime.get();
79             }
80         });
81         Mockito.doAnswer(new Answer() {
82             @Override
83             public Object answer(InvocationOnMock invocation) throws Throwable {
84                 currentTime.addAndGet((Long) invocation.getArguments()[0] + 1);
85                 return null;
86             }
87         }).when(systemFunctions).sleep(anyLong());
88     }
89
90     /**
91      * retrieving an existing VNF
92      */
93     @Test
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);
97         //when
98         GenericVnf vnf = genericVnfManager.getExistingVnf(VNF_ID);
99         //verify
100         assertEquals(aaiVnf, vnf);
101     }
102
103     /**
104      * if the VNF does not exist it is created
105      */
106     @Test
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();
113             }
114             return vnfs.iterator().next();
115         });
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 -> {
118             vnfs.add(vnfInAaai);
119             return null;
120         });
121         vnfInfo.setName("vnfName");
122         //when
123         genericVnfManager.createOrUpdate(VNF_ID, true);
124         //verify
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);
134     }
135
136     /**
137      * if the VNF exist it is updated
138      */
139     @Test
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");
147         //when
148         genericVnfManager.createOrUpdate(VNF_ID, true);
149         //verify
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);
159     }
160
161     /**
162      * error is propagated if unable to query VNF from CBAM
163      */
164     @Test
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");
173             return null;
174         });
175         vnfInfo.setName("vnfName");
176         //when
177         try {
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());
182         }
183     }
184
185     /**
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
188      */
189     @Test
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) {
197                 return vnfInAaai;
198             }
199             throw new NoSuchElementException();
200         });
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;
207             }
208             return null;
209         });
210         vnfInfo.setName("vnfName");
211         //when
212         genericVnfManager.createOrUpdate(VNF_ID, true);
213         //verify
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);
227     }
228
229     /**
230      * test how entities can refer to a VNF
231      */
232     @Test
233     public void testRelations() {
234         //when
235         Relationship relationship = GenericVnfManager.linkTo(VNF_ID);
236         //verify
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());
241     }
242
243     /**
244      * test inheritence
245      */
246     @Test
247     public void testInheritence() {
248         assertEquals(logger, genericVnfManager.getLogger());
249     }
250 }