26904fa8a4c2565eb09186ed9b03043244b982bd
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  # Copyright (c) 2020, CMCC Technologies Co., Ltd.
6  #
7  # Licensed under the Apache License, Version 2.0 (the "License")
8  # you may not use this file except in compliance with the License.
9  # You may obtain a copy of the License at
10  #
11  #       http://www.apache.org/licenses/LICENSE-2.0
12  #
13  # Unless required by applicable law or agreed to in writing, software
14  # distributed under the License is distributed on an "AS IS" BASIS,
15  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  # See the License for the specific language governing permissions and
17  # limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.adapters.nssmf.service.impl;
22
23
24 import org.apache.http.Header;
25 import org.apache.http.HttpEntity;
26 import org.apache.http.HttpResponse;
27 import org.apache.http.StatusLine;
28 import org.apache.http.client.HttpClient;
29 import org.apache.http.client.methods.HttpRequestBase;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.mockito.Mock;
34 import org.mockito.stubbing.Answer;
35 import org.onap.so.adapters.nssmf.consts.NssmfAdapterConsts;
36 import org.onap.so.adapters.nssmf.entity.NssmfInfo;
37 import org.onap.so.adapters.nssmf.entity.TokenResponse;
38 import org.onap.so.adapters.nssmf.enums.ActionType;
39 import org.onap.so.adapters.nssmf.enums.HttpMethod;
40 import org.onap.so.adapters.nssmf.util.RestUtil;
41 import org.onap.so.beans.nsmf.*;
42 import org.onap.so.beans.nsmf.oof.SubnetCapability;
43 import org.onap.so.db.request.beans.ResourceOperationStatus;
44 import org.onap.so.db.request.data.repository.ResourceOperationStatusRepository;
45 import org.springframework.http.ResponseEntity;
46 import org.springframework.test.context.junit4.SpringRunner;
47 import java.io.ByteArrayInputStream;
48 import java.io.InputStream;
49 import java.lang.reflect.Field;
50 import java.util.*;
51 import static java.nio.charset.StandardCharsets.UTF_8;
52 import static org.junit.Assert.assertEquals;
53 import static org.junit.Assert.assertNotNull;
54 import static org.mockito.ArgumentMatchers.any;
55 import static org.mockito.Mockito.doAnswer;
56 import static org.mockito.Mockito.when;
57 import static org.mockito.MockitoAnnotations.initMocks;
58 import static org.onap.so.adapters.nssmf.util.NssmfAdapterUtil.marshal;
59 import static org.onap.so.adapters.nssmf.util.NssmfAdapterUtil.unMarshal;
60 import static org.onap.so.beans.nsmf.NetworkType.CORE;
61 import static org.onap.so.beans.nsmf.ResourceSharingLevel.NON_SHARED;
62
63 @RunWith(SpringRunner.class)
64 public class NssmfManagerServiceImplTest {
65
66     @Mock
67     private RestUtil restUtil;
68
69
70     private NssmfManagerServiceImpl nssiManagerService;
71
72     @Mock
73     private HttpResponse tokenResponse;
74
75     @Mock
76     private HttpEntity tokenEntity;
77
78     @Mock
79     private HttpResponse commonResponse;
80
81     @Mock
82     private HttpEntity commonEntity;
83
84     @Mock
85     private StatusLine statusLine;
86
87     @Mock
88     private HttpClient httpClient;
89
90     private InputStream postStream;
91
92     private InputStream tokenStream;
93
94     @Mock
95     private ResourceOperationStatusRepository repository;
96
97     @Before
98     public void setUp() throws Exception {
99         initMocks(this);
100
101         nssiManagerService = new NssmfManagerServiceImpl();
102
103         Field restUtil = nssiManagerService.getClass().getDeclaredField("restUtil");
104         restUtil.setAccessible(true);
105         restUtil.set(nssiManagerService, this.restUtil);
106
107         Field repository = nssiManagerService.getClass().getDeclaredField("repository");
108         repository.setAccessible(true);
109         repository.set(nssiManagerService, this.repository);
110         // nssiManagerService.setRestUtil(this.restUtil);
111
112         when(this.restUtil.send(any(String.class), any(HttpMethod.class), any(), any(Header.class)))
113                 .thenCallRealMethod();
114         when(this.restUtil.createResponse(any(Integer.class), any(String.class))).thenCallRealMethod();
115     }
116
117     private void createCommonMock(int statusCode, NssmfInfo nssmf) throws Exception {
118         when(restUtil.getToken(any(NssmfInfo.class))).thenReturn("7512eb3feb5249eca5ddd742fedddd39");
119         when(restUtil.getHttpsClient()).thenReturn(httpClient);
120
121         when(statusLine.getStatusCode()).thenReturn(statusCode);
122         when(restUtil.getNssmfHost(any(EsrInfo.class))).thenReturn(nssmf);
123
124         when(tokenResponse.getEntity()).thenReturn(tokenEntity);
125         when(tokenResponse.getStatusLine()).thenReturn(statusLine);
126         when(tokenEntity.getContent()).thenReturn(tokenStream);
127
128         when(commonResponse.getEntity()).thenReturn(commonEntity);
129         when(commonResponse.getStatusLine()).thenReturn(statusLine);
130         when(commonEntity.getContent()).thenReturn(postStream);
131
132         Answer<HttpResponse> answer = invocation -> {
133             Object[] arguments = invocation.getArguments();
134             if (arguments != null && arguments.length == 1 && arguments[0] != null) {
135
136                 HttpRequestBase base = (HttpRequestBase) arguments[0];
137                 if (base.getURI().toString().endsWith("/oauth/token")) {
138                     return tokenResponse;
139                 } else {
140                     return commonResponse;
141                 }
142             }
143             return commonResponse;
144         };
145
146         doAnswer(answer).when(httpClient).execute(any(HttpRequestBase.class));
147
148     }
149
150     @Test
151     public void allocateNssi() throws Exception {
152
153         NssmfInfo nssmf = new NssmfInfo();
154         nssmf.setUserName("nssmf-user");
155         nssmf.setPassword("nssmf-pass");
156         nssmf.setPort("8080");
157         nssmf.setIpAddress("127.0.0.1");
158         nssmf.setUrl("http://127.0.0.1:8080");
159
160         NssiResponse nssiRes = new NssiResponse();
161         nssiRes.setJobId("4b45d919816ccaa2b762df5120f72067");
162         nssiRes.setNssiId("NSSI-C-001-HDBNJ-NSSMF-01-A-ZX");
163
164         TokenResponse token = new TokenResponse();
165         token.setAccessToken("7512eb3feb5249eca5ddd742fedddd39");
166         token.setExpires(1800);
167
168         postStream = new ByteArrayInputStream(marshal(nssiRes).getBytes(UTF_8));
169         tokenStream = new ByteArrayInputStream(marshal(token).getBytes(UTF_8));
170
171         createCommonMock(200, nssmf);
172
173
174         NssmfAdapterNBIRequest nbiRequest = createAllocateNssi();
175         assertNotNull(nbiRequest);
176         System.out.println(marshal(nbiRequest));
177         ResponseEntity res = nssiManagerService.allocateNssi(nbiRequest);
178         assertNotNull(res);
179         assertNotNull(res.getBody());
180         NssiResponse allRes = unMarshal(res.getBody().toString(), NssiResponse.class);
181         assertEquals(allRes.getJobId(), "4b45d919816ccaa2b762df5120f72067");
182         assertEquals(allRes.getNssiId(), "NSSI-C-001-HDBNJ-NSSMF-01-A-ZX");
183
184         System.out.println(res);
185     }
186
187
188
189     private NssmfAdapterNBIRequest createAllocateNssi() {
190         CnSliceProfile sP = new CnSliceProfile();
191         List<String> sns = new LinkedList<>();
192         sns.add("001-100001");
193         List<String> plmn = new LinkedList<>();
194         plmn.add("460-00");
195         plmn.add("460-01");
196         PerfReqEmbb embb = new PerfReqEmbb();
197         embb.setActivityFactor(50);
198         List<PerfReqEmbb> embbList = new LinkedList<>();
199         embbList.add(embb);
200         PerfReq perfReq = new PerfReq();
201         perfReq.setPerfReqEmbbList(embbList);
202         List<String> taList = new LinkedList<>();
203         taList.add("1");
204         taList.add("2");
205         taList.add("3");
206         sP.setSnssaiList(sns);
207         sP.setSliceProfileId("ab9af40f13f721b5f13539d87484098");
208         sP.setPLMNIdList(plmn);
209         sP.setPerfReq(perfReq);
210         sP.setMaxNumberOfUEs(200);
211         sP.setCoverageAreaTAList(taList);
212         sP.setLatency(6);
213         sP.setResourceSharingLevel(NON_SHARED);
214         NsiInfo nsiInfo = new NsiInfo();
215         nsiInfo.setNsiId("NSI-M-001-HDBNJ-NSMF-01-A-ZX");
216         nsiInfo.setNsiName("eMBB-001");
217         AllocateCnNssi cnNssi = new AllocateCnNssi();
218         cnNssi.setNssiId("NSST-C-001-HDBNJ-NSSMF-01-A-ZX");
219         cnNssi.setNssiName("eMBB-001");
220         cnNssi.setScriptName("CN1");
221         cnNssi.setSliceProfile(sP);
222         cnNssi.setNsiInfo(nsiInfo);
223
224         NssmfAdapterNBIRequest nbiRequest = createNbiRequest();
225         nbiRequest.setAllocateCnNssi(cnNssi);
226         return nbiRequest;
227     }
228
229     @Test
230     public void deAllocateNssi() throws Exception {
231         DeAllocateNssi deAllocateNssi = new DeAllocateNssi();
232         deAllocateNssi.setTerminateNssiOption(0);
233         List<String> snssai = new LinkedList<>();
234         snssai.add("001-100001");
235         deAllocateNssi.setNsiId("NSI-M-001-HDBNJ-NSMF-01-A-ZX");
236         deAllocateNssi.setNssiId("NSSI-C-001-HDBNJ-NSSMF-01-A-ZX");
237         deAllocateNssi.setScriptName("CN1");
238         deAllocateNssi.setSnssaiList(snssai);
239
240         NssmfAdapterNBIRequest nbiRequest = createNbiRequest();
241         nbiRequest.setDeAllocateNssi(deAllocateNssi);
242
243         NssmfInfo nssmf = new NssmfInfo();
244         nssmf.setUserName("nssmf-user");
245         nssmf.setPassword("nssmf-pass");
246         nssmf.setPort("8080");
247         nssmf.setIpAddress("127.0.0.1");
248
249         NssiResponse nssiRes = new NssiResponse();
250         nssiRes.setJobId("4b45d919816ccaa2b762df5120f72067");
251
252         TokenResponse token = new TokenResponse();
253         token.setAccessToken("7512eb3feb5249eca5ddd742fedddd39");
254         token.setExpires(1800);
255
256         postStream = new ByteArrayInputStream(marshal(nssiRes).getBytes(UTF_8));
257         tokenStream = new ByteArrayInputStream(marshal(token).getBytes(UTF_8));
258
259         createCommonMock(202, nssmf);
260         ResponseEntity res = nssiManagerService.deAllocateNssi(nbiRequest, "ab9af40f13f721b5f13539d87484098");
261         assertNotNull(res);
262         assertNotNull(res.getBody());
263         NssiResponse allRes = unMarshal(res.getBody().toString(), NssiResponse.class);
264         assertEquals(allRes.getJobId(), "4b45d919816ccaa2b762df5120f72067");
265         assertNotNull(res);
266         assertNotNull(res.getBody());
267     }
268
269     @Test
270     public void activateNssi() throws Exception {
271         NssmfInfo nssmf = new NssmfInfo();
272         nssmf.setUserName("nssmf-user");
273         nssmf.setPassword("nssmf-pass");
274         nssmf.setPort("8080");
275         nssmf.setIpAddress("127.0.0.1");
276
277         NssiResponse nssiRes = new NssiResponse();
278         nssiRes.setJobId("4b45d919816ccaa2b762df5120f72067");
279
280         TokenResponse token = new TokenResponse();
281         token.setAccessToken("7512eb3feb5249eca5ddd742fedddd39");
282         token.setExpires(1800);
283
284         postStream = new ByteArrayInputStream(marshal(nssiRes).getBytes(UTF_8));
285         tokenStream = new ByteArrayInputStream(marshal(token).getBytes(UTF_8));
286
287         ActDeActNssi act = new ActDeActNssi();
288         act.setNsiId("NSI-M-001-HDBNJ-NSMF-01-A-ZX");
289         act.setNssiId("NSSI-C-001-HDBNJ-NSSMF-01-A-ZX");
290
291         NssmfAdapterNBIRequest nbiRequest = createNbiRequest();
292         nbiRequest.setActDeActNssi(act);
293
294         createCommonMock(200, nssmf);
295         ResponseEntity res = nssiManagerService.activateNssi(nbiRequest, "001-100001");
296         assertNotNull(res);
297         assertNotNull(res.getBody());
298         NssiResponse allRes = unMarshal(res.getBody().toString(), NssiResponse.class);
299         assertEquals(allRes.getJobId(), "4b45d919816ccaa2b762df5120f72067");
300     }
301
302     @Test
303     public void deActivateNssi() throws Exception {
304         NssmfInfo nssmf = new NssmfInfo();
305         nssmf.setUserName("nssmf-user");
306         nssmf.setPassword("nssmf-pass");
307         nssmf.setPort("8080");
308         nssmf.setIpAddress("127.0.0.1");
309
310         NssiResponse nssiRes = new NssiResponse();
311         nssiRes.setJobId("4b45d919816ccaa2b762df5120f72067");
312
313         TokenResponse token = new TokenResponse();
314         token.setAccessToken("7512eb3feb5249eca5ddd742fedddd39");
315         token.setExpires(1800);
316
317         postStream = new ByteArrayInputStream(marshal(nssiRes).getBytes(UTF_8));
318         tokenStream = new ByteArrayInputStream(marshal(token).getBytes(UTF_8));
319
320         ActDeActNssi act = new ActDeActNssi();
321         act.setNsiId("NSI-M-001-HDBNJ-NSMF-01-A-ZX");
322         act.setNssiId("NSSI-C-001-HDBNJ-NSSMF-01-A-ZX");
323
324         NssmfAdapterNBIRequest nbiRequest = createNbiRequest();
325         nbiRequest.setActDeActNssi(act);
326
327         createCommonMock(200, nssmf);
328         ResponseEntity res = nssiManagerService.deActivateNssi(nbiRequest, "001-100001");
329         assertNotNull(res);
330         assertNotNull(res.getBody());
331         NssiResponse allRes = unMarshal(res.getBody().toString(), NssiResponse.class);
332         assertEquals(allRes.getJobId(), "4b45d919816ccaa2b762df5120f72067");
333     }
334
335     @Test
336     public void queryJobStatus() throws Exception {
337         NssmfInfo nssmf = new NssmfInfo();
338         nssmf.setUserName("nssmf-user");
339         nssmf.setPassword("nssmf-pass");
340         nssmf.setPort("8080");
341         nssmf.setIpAddress("127.0.0.1");
342
343         JobStatusResponse jobStatusResponse = new JobStatusResponse();
344         ResponseDescriptor descriptor = new ResponseDescriptor();
345         descriptor.setResponseId("7512eb3feb5249eca5ddd742fedddd39");
346         descriptor.setProgress(20);
347         descriptor.setStatusDescription("Initiating VNF Instance");
348         descriptor.setStatus("processing");
349         jobStatusResponse.setResponseDescriptor(descriptor);
350
351         TokenResponse token = new TokenResponse();
352         token.setAccessToken("7512eb3feb5249eca5ddd742fedddd39");
353         token.setExpires(1800);
354
355         postStream = new ByteArrayInputStream(marshal(jobStatusResponse).getBytes(UTF_8));
356         tokenStream = new ByteArrayInputStream(marshal(token).getBytes(UTF_8));
357
358         ResourceOperationStatus operationStatus = new ResourceOperationStatus();
359         operationStatus.setOperationId("4b45d919816ccaa2b762df5120f72067");
360         operationStatus.setResourceTemplateUUID("8ee5926d-720b-4bb2-86f9-d20e921c143b");
361         operationStatus.setServiceId("NSI-M-001-HDBNJ-NSMF-01-A-ZX");
362         operationStatus.setOperType(ActionType.ALLOCATE.toString());
363
364         NssmfAdapterNBIRequest nbiRequest = createNbiRequest();
365         nbiRequest.setResponseId("7512eb3feb5249eca5ddd742fedddd39");
366         List<ResourceOperationStatus> optional = new ArrayList<>();
367         optional.add(operationStatus);
368
369         doAnswer(invocation -> optional).when(repository).findByServiceIdAndOperationId(any(), any());
370
371         createCommonMock(200, nssmf);
372
373         ResponseEntity res = nssiManagerService.queryJobStatus(nbiRequest, "4b45d919816ccaa2b762df5120f72067");
374         assertNotNull(res);
375         assertNotNull(res.getBody());
376         JobStatusResponse allRes = unMarshal(res.getBody().toString(), JobStatusResponse.class);
377         assertEquals(allRes.getResponseDescriptor().getProgress(), 20);
378         assertEquals(allRes.getResponseDescriptor().getStatus(), "processing");
379         assertEquals(allRes.getResponseDescriptor().getResponseId(), "7512eb3feb5249eca5ddd742fedddd39");
380
381     }
382
383     @Test
384     public void queryNSSISelectionCapability() throws Exception {
385
386         NssmfAdapterNBIRequest nbiRequest = createNbiRequest();
387         ResponseEntity res = nssiManagerService.queryNSSISelectionCapability(nbiRequest);
388         assertNotNull(res);
389         assertNotNull(res.getBody());
390         Map allRes = unMarshal(res.getBody().toString(), Map.class);
391         assertEquals(allRes.get("selection"), "NSMF");
392
393         System.out.println(res);
394
395         nbiRequest.getEsrInfo().setVendor(NssmfAdapterConsts.ONAP_INTERNAL_TAG);
396         res = nssiManagerService.queryNSSISelectionCapability(nbiRequest);
397         assertNotNull(res);
398         assertNotNull(res.getBody());
399         allRes = unMarshal(res.getBody().toString(), Map.class);
400         assertEquals(allRes.get("selection"), "NSSMF");
401
402         System.out.println(res);
403
404         nbiRequest.getEsrInfo().setNetworkType(NetworkType.ACCESS);
405         res = nssiManagerService.queryNSSISelectionCapability(nbiRequest);
406         assertNotNull(res);
407         assertNotNull(res.getBody());
408         allRes = unMarshal(res.getBody().toString(), Map.class);
409         assertEquals(allRes.get("selection"), "NSSMF");
410
411         System.out.println(res);
412     }
413
414     private NssmfAdapterNBIRequest createNbiRequest() {
415         NssmfAdapterNBIRequest nbiRequest = new NssmfAdapterNBIRequest();
416         EsrInfo esrInfo = new EsrInfo();
417         esrInfo.setVendor("huawei");
418         esrInfo.setNetworkType(CORE);
419         ServiceInfo serviceInfo = new ServiceInfo();
420         serviceInfo.setServiceUuid("8ee5926d-720b-4bb2-86f9-d20e921c143b");
421         serviceInfo.setServiceInvariantUuid("e75698d9-925a-4cdd-a6c0-edacbe6a0b51");
422         serviceInfo.setGlobalSubscriberId("5GCustomer");
423         serviceInfo.setServiceType("5G");
424         serviceInfo.setNsiId("NSI-M-001-HDBNJ-NSMF-01-A-ZX");
425         nbiRequest.setEsrInfo(esrInfo);
426         nbiRequest.setServiceInfo(serviceInfo);
427         return nbiRequest;
428     }
429
430     @Test
431     public void querySubnetCapability() {
432         NssmfAdapterNBIRequest nbiRequest = createNbiRequest();
433
434         QuerySubnetCapability subnetCapabilityQuery = new QuerySubnetCapability();
435         List<String> subnetTypes = Arrays.asList("TN-FH", "TN-MH", "TN-BH");
436         subnetCapabilityQuery.setSubnetTypes(subnetTypes);
437         nbiRequest.setSubnetCapabilityQuery(subnetCapabilityQuery);
438         ResponseEntity res = nssiManagerService.queryNSSISelectionCapability(nbiRequest);
439         assertNotNull(res);
440         assertNotNull(res.getBody());
441     }
442 }