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