a2bf03055c9a8696c78b05c0ec69fd679fc09e00
[vfc/nfvo/driver/vnfm/svnfm.git] / nokiav2 / driver / src / test / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / nokia / onap / vfc / TestVfcGrantManager.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.vfc;
17
18 import com.google.common.collect.Lists;
19 import com.google.gson.Gson;
20 import com.google.gson.JsonObject;
21 import com.nokia.cbam.lcm.v32.model.*;
22 import com.nokia.cbam.lcm.v32.model.VnfInfo;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.mockito.ArgumentCaptor;
26 import org.mockito.InjectMocks;
27 import org.mockito.Mock;
28 import org.mockito.Mockito;
29 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CatalogManager;
30 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.LifecycleManager;
31 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.TestBase;
32 import org.onap.vnfmdriver.ApiException;
33 import org.onap.vnfmdriver.model.*;
34 import org.onap.vnfmdriver.model.ScaleDirection;
35
36 import java.nio.file.Paths;
37 import java.util.ArrayList;
38 import java.util.Iterator;
39 import java.util.List;
40
41 import static java.nio.file.Files.readAllBytes;
42 import static junit.framework.TestCase.*;
43 import static org.mockito.Mockito.*;
44 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CbamRestApiProvider.NOKIA_LCM_API_VERSION;
45 import static org.springframework.test.util.ReflectionTestUtils.setField;
46
47 public class TestVfcGrantManager extends TestBase {
48
49     private ArgumentCaptor<GrantVNFRequest> grantRequest = ArgumentCaptor.forClass(GrantVNFRequest.class);
50     private GrantVNFResponseVim vim = new GrantVNFResponseVim();
51     private GrantVNFResponse grantResponse = new GrantVNFResponse();
52     @Mock
53     private CatalogManager cbamCatalogManager;
54     @InjectMocks
55     private VfcGrantManager vfcGrantManager;
56
57     @Before
58     public void initMocks() throws Exception {
59         setField(VfcGrantManager.class, "logger", logger);
60         when(nsLcmApi.grantvnf(grantRequest.capture())).thenReturn(grantResponse);
61         grantResponse.setVim(vim);
62     }
63
64     /**
65      * test grant request for instantiation
66      */
67     @Test
68     public void testGrantDuringInstantiation() throws Exception {
69         String cbamVnfdContent = new String(readAllBytes(Paths.get(TestVfcGrantManager.class.getResource("/unittests/vnfd.instantiation.yaml").toURI())));
70         //when
71         vfcGrantManager.requestGrantForInstantiate(VNFM_ID, VNF_ID, VIM_ID, ONAP_CSAR_ID, "level1", cbamVnfdContent, JOB_ID);
72         //verify
73         assertEquals(1, grantRequest.getAllValues().size());
74         GrantVNFRequest request = grantRequest.getValue();
75         assertVduInGrant(request.getAddResource(), "vdu1", 1);
76         assertVduInGrant(request.getAddResource(), "vdu2", 2);
77         assertEquals(0, request.getRemoveResource().size());
78         assertBasicGrantAttributes(request, org.onap.vnfmdriver.model.OperationType.INSTANTIATE);
79     }
80
81     /**
82      * test failure logging & propagation during grant request for instantiation
83      */
84     @Test
85     public void testFailureDuringGrantPreparation() throws Exception {
86         String cbamVnfdContent = new String(readAllBytes(Paths.get(TestVfcGrantManager.class.getResource("/unittests/vnfd.instantiation.yaml").toURI())));
87         //when
88         try {
89             vfcGrantManager.requestGrantForInstantiate(VNFM_ID, VNF_ID, VIM_ID, ONAP_CSAR_ID, "missingLevel", cbamVnfdContent, JOB_ID);
90             //verify
91             fail();
92         } catch (RuntimeException e) {
93             verify(logger).error(Mockito.eq("Unable to prepare grant request for instantiation"), Mockito.any(RuntimeException.class));
94             verifyNoMoreInteractions(nsLcmApi);
95         }
96     }
97
98     /**
99      * test grant request for instantiation
100      */
101     @Test
102     public void testFailureDuringGrantRequest() throws Exception {
103         String cbamVnfdContent = new String(readAllBytes(Paths.get(TestVfcGrantManager.class.getResource("/unittests/vnfd.instantiation.yaml").toURI())));
104         ApiException expectedException = new ApiException("a");
105         when(nsLcmApi.grantvnf(Mockito.any())).thenThrow(expectedException);
106         //when
107         try {
108             vfcGrantManager.requestGrantForInstantiate(VNFM_ID, VNF_ID, VIM_ID, ONAP_CSAR_ID, "level1", cbamVnfdContent, JOB_ID);
109             //verify
110             fail();
111         } catch (RuntimeException e) {
112             verify(logger).error("Unable to request grant", expectedException);
113         }
114     }
115
116     /**
117      * No grant is requested for termination if the the VNF is not instantiated
118      */
119     @Test
120     public void testNoGrantIsRequestedIfNotInstantiated() {
121         VnfInfo vnf = new VnfInfo();
122         vnf.setId(VNF_ID);
123         vnf.setInstantiationState(InstantiationState.NOT_INSTANTIATED);
124         //when
125         vfcGrantManager.requestGrantForTerminate(VNFM_ID, VNF_ID, VIM_ID, ONAP_CSAR_ID, vnf, JOB_ID);
126         //verify
127         verifyNoMoreInteractions(nsLcmApi);
128     }
129
130     /**
131      * grant is requested for termination if the the VNF is instantiated
132      */
133     @Test
134     public void testGrantIsRequestedIfInstantiated() {
135         VnfInfo vnf = new VnfInfo();
136         vnf.setId(VNF_ID);
137         vnf.setInstantiationState(InstantiationState.INSTANTIATED);
138         InstantiatedVnfInfo instantiatedVnfInfo = new InstantiatedVnfInfo();
139         VnfcResourceInfo vnfc = new VnfcResourceInfo();
140         vnfc.setId("vnfcId1");
141         vnfc.setVduId("vdu1");
142         instantiatedVnfInfo.setVnfcResourceInfo(new ArrayList<>());
143         instantiatedVnfInfo.getVnfcResourceInfo().add(vnfc);
144         vnf.setInstantiatedVnfInfo(instantiatedVnfInfo);
145         VnfProperty prop = new VnfProperty();
146         prop.setName(LifecycleManager.ONAP_CSAR_ID);
147         prop.setValue(ONAP_CSAR_ID);
148         vnf.setVnfConfigurableProperties(new ArrayList<>());
149         vnf.getVnfConfigurableProperties().add(prop);
150         //when
151         vfcGrantManager.requestGrantForTerminate(VNFM_ID, VNF_ID, VIM_ID, ONAP_CSAR_ID, vnf, JOB_ID);
152         //verify
153         assertEquals(1, grantRequest.getAllValues().size());
154         GrantVNFRequest request = grantRequest.getValue();
155         assertVduInGrant(request.getRemoveResource(), "vdu1", 1);
156         assertVduInGrant(request.getRemoveResource(), "vdu2", 0);
157         assertEquals(0, request.getAddResource().size());
158         assertBasicGrantAttributes(request, org.onap.vnfmdriver.model.OperationType.TERMINAL);
159     }
160
161     /**
162      * test failure logging & propagation during grant request for instantiation
163      */
164     @Test
165     public void testFailureDuringTerminationGrantPreparation() throws Exception {
166         VnfInfo vnf = new VnfInfo();
167         vnf.setId(VNF_ID);
168         vnf.setInstantiatedVnfInfo(null);
169         vnf.setInstantiationState(InstantiationState.INSTANTIATED);
170         //when
171         try {
172             vfcGrantManager.requestGrantForTerminate(VNFM_ID, VNF_ID, VIM_ID, ONAP_CSAR_ID, vnf, JOB_ID);
173             //verify
174             fail();
175         } catch (RuntimeException e) {
176             verify(logger).error(Mockito.eq("Unable to prepare grant request for termination"), Mockito.any(RuntimeException.class));
177             verifyNoMoreInteractions(nsLcmApi);
178         }
179     }
180
181     /**
182      * failuire is to request grant is logged
183      */
184     @Test
185     public void testFailureToRequestGrantIsLogged() throws Exception {
186         VnfInfo vnf = new VnfInfo();
187         vnf.setId(VNF_ID);
188         vnf.setInstantiationState(InstantiationState.INSTANTIATED);
189         InstantiatedVnfInfo instantiatedVnfInfo = new InstantiatedVnfInfo();
190         VnfcResourceInfo vnfc = new VnfcResourceInfo();
191         vnfc.setId("vnfcId1");
192         vnfc.setVduId("vdu1");
193         instantiatedVnfInfo.setVnfcResourceInfo(new ArrayList<>());
194         instantiatedVnfInfo.getVnfcResourceInfo().add(vnfc);
195         vnf.setInstantiatedVnfInfo(instantiatedVnfInfo);
196         VnfProperty prop = new VnfProperty();
197         prop.setName(LifecycleManager.ONAP_CSAR_ID);
198         prop.setValue(ONAP_CSAR_ID);
199         vnf.setVnfConfigurableProperties(new ArrayList<>());
200         vnf.getVnfConfigurableProperties().add(prop);
201         ApiException expectedException = new ApiException();
202         when(nsLcmApi.grantvnf(Mockito.any())).thenThrow(expectedException);
203         //when
204         try {
205             vfcGrantManager.requestGrantForTerminate(VNFM_ID, VNF_ID, VIM_ID, ONAP_CSAR_ID, vnf, JOB_ID);
206             //verify
207             fail();
208         } catch (RuntimeException e) {
209             verify(logger).error(Mockito.eq("Unable to request grant"), Mockito.eq(expectedException));
210         }
211     }
212
213     /**
214      * failuire is to request grant is logged
215      */
216     @Test
217     public void testFailureToRequestGrantForScaleIsLogged() throws Exception {
218         String cbamVnfdContent = new String(readAllBytes(Paths.get(TestVfcGrantManager.class.getResource("/unittests/vnfd.scale.yaml").toURI())));
219         VnfScaleRequest scaleRequest = new VnfScaleRequest();
220         scaleRequest.setType(ScaleDirection.OUT);
221         scaleRequest.setAspectId("aspect1");
222         scaleRequest.setNumberOfSteps("2");
223         com.nokia.cbam.lcm.v32.ApiException expectedException = new com.nokia.cbam.lcm.v32.ApiException();
224         when(vnfApi.vnfsVnfInstanceIdGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenThrow(expectedException);
225         //when
226         try {
227             vfcGrantManager.requestGrantForScale(VNFM_ID, VNF_ID, VIM_ID, ONAP_CSAR_ID, scaleRequest, JOB_ID);
228             //verify
229             fail();
230         } catch (RuntimeException e) {
231             verify(logger).error(Mockito.eq("Unable to query VNF myVnfId"), Mockito.eq(expectedException));
232             assertEquals(e.getCause(), expectedException);
233         }
234     }
235
236     /**
237      * test grant request for scale out
238      */
239     @Test
240     public void testGrantDuringScaleOut() throws Exception {
241         String cbamVnfdContent = new String(readAllBytes(Paths.get(TestVfcGrantManager.class.getResource("/unittests/vnfd.scale.yaml").toURI())));
242         VnfScaleRequest scaleRequest = new VnfScaleRequest();
243         scaleRequest.setType(ScaleDirection.OUT);
244         scaleRequest.setAspectId("aspect1");
245         scaleRequest.setNumberOfSteps("2");
246         VnfInfo vnf = new VnfInfo();
247         when(vnfApi.vnfsVnfInstanceIdGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenReturn(vnf);
248         vnf.setVnfdId(CBAM_VNFD_ID);
249         when(cbamCatalogManager.getCbamVnfdContent(VNFM_ID, CBAM_VNFD_ID)).thenReturn(cbamVnfdContent);
250         //when
251         vfcGrantManager.requestGrantForScale(VNFM_ID, VNF_ID, VIM_ID, ONAP_CSAR_ID, scaleRequest, JOB_ID);
252         //verify
253         assertEquals(1, grantRequest.getAllValues().size());
254         GrantVNFRequest request = grantRequest.getValue();
255         assertVduInGrant(request.getAddResource(), "vdu1", 4);
256         assertVduInGrant(request.getAddResource(), "vdu2", 2);
257         assertEquals(0, request.getRemoveResource().size());
258         assertBasicGrantAttributes(request, org.onap.vnfmdriver.model.OperationType.SCALEOUT);
259     }
260
261     /**
262      * test grant request for scale in
263      */
264     @Test
265     public void testGrantDuringScaleIn() throws Exception {
266         String cbamVnfdContent = new String(readAllBytes(Paths.get(TestVfcGrantManager.class.getResource("/unittests/vnfd.scale.yaml").toURI())));
267         VnfScaleRequest scaleRequest = new VnfScaleRequest();
268         scaleRequest.setType(ScaleDirection.IN);
269         scaleRequest.setAspectId("aspect1");
270         scaleRequest.setNumberOfSteps("2");
271         VnfInfo vnf = new VnfInfo();
272         when(vnfApi.vnfsVnfInstanceIdGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenReturn(vnf);
273         vnf.setVnfdId(CBAM_VNFD_ID);
274         when(cbamCatalogManager.getCbamVnfdContent(VNFM_ID, CBAM_VNFD_ID)).thenReturn(cbamVnfdContent);
275         //when
276         vfcGrantManager.requestGrantForScale(VNFM_ID, VNF_ID, VIM_ID, ONAP_CSAR_ID, scaleRequest, JOB_ID);
277         //verify
278         assertEquals(1, grantRequest.getAllValues().size());
279         GrantVNFRequest request = grantRequest.getValue();
280         assertVduInGrant(request.getRemoveResource(), "vdu1", 4);
281         assertVduInGrant(request.getRemoveResource(), "vdu2", 2);
282         assertEquals(0, request.getAddResource().size());
283         assertBasicGrantAttributes(request, org.onap.vnfmdriver.model.OperationType.SCALEIN);
284     }
285
286     /**
287      * test grant request for healing
288      */
289     @Test
290     public void testGrantDuringHealing() throws Exception {
291         //when
292         VnfHealRequest healRequest = new VnfHealRequest();
293         VnfHealRequestAffectedvm affectedVm = new VnfHealRequestAffectedvm();
294         affectedVm.setVduid("vdu1");
295         healRequest.setAffectedvm(affectedVm);
296         vfcGrantManager.requestGrantForHeal(VNFM_ID, VNF_ID, VIM_ID, ONAP_CSAR_ID, healRequest, JOB_ID);
297         //verify
298         assertEquals(1, grantRequest.getAllValues().size());
299         GrantVNFRequest request = grantRequest.getValue();
300         assertVduInGrant(request.getAddResource(), "vdu1", 1);
301         assertVduInGrant(request.getRemoveResource(), "vdu1", 1);
302         assertBasicGrantAttributes(request, org.onap.vnfmdriver.model.OperationType.HEAL);
303     }
304
305     @Test
306     public void testPOJO() {
307         VfcGrantManager.AdditionalGrantParams additionalGrantParams = new VfcGrantManager.AdditionalGrantParams(VNFM_ID, VIM_ID);
308         assertEquals(VNFM_ID, additionalGrantParams.getVnfmId());
309         assertEquals(VIM_ID, additionalGrantParams.getVimId());
310     }
311
312     private void assertBasicGrantAttributes(GrantVNFRequest request, org.onap.vnfmdriver.model.OperationType type) {
313         assertEquals(JOB_ID, request.getJobId());
314         assertEquals(type, request.getLifecycleOperation());
315         assertEquals(ONAP_CSAR_ID, request.getVnfDescriptorId());
316         assertEquals(VNF_ID, request.getVnfInstanceId());
317         JsonObject additionalParams = new Gson().toJsonTree(request.getAdditionalParam()).getAsJsonObject();
318         assertEquals(VIM_ID, additionalParams.get("vimId").getAsString());
319         assertEquals(VNFM_ID, additionalParams.get("vnfmId").getAsString());
320     }
321
322     private void assertVduInGrant(List<ResourceChange> changes, String vduName, int count) {
323         ArrayList<ResourceChange> clonedChanges = Lists.newArrayList(changes);
324         for (int i = 0; i < count + 1; i++) {
325             Iterator<ResourceChange> iter = clonedChanges.iterator();
326             boolean found = false;
327             while (iter.hasNext()) {
328                 ResourceChange change = iter.next();
329                 if (change.getVdu().equals(vduName)) {
330                     iter.remove();
331                     found = true;
332                     break;
333                 }
334             }
335             if (i >= count) {
336                 assertFalse(found);
337             } else {
338                 assertTrue(found);
339             }
340         }
341     }
342
343 }