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