2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 # Copyright (c) 2020, CMCC Technologies Co., Ltd.
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
11 # http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.so.adapters.nssmf.service.impl;
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;
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;
63 @RunWith(SpringRunner.class)
64 public class NssmfManagerServiceImplTest {
67 private RestUtil restUtil;
70 private NssmfManagerServiceImpl nssiManagerService;
73 private HttpResponse tokenResponse;
76 private HttpEntity tokenEntity;
79 private HttpResponse commonResponse;
82 private HttpEntity commonEntity;
85 private StatusLine statusLine;
88 private HttpClient httpClient;
90 private InputStream postStream;
92 private InputStream tokenStream;
95 private ResourceOperationStatusRepository repository;
98 public void setUp() throws Exception {
101 nssiManagerService = new NssmfManagerServiceImpl();
103 Field restUtil = nssiManagerService.getClass().getDeclaredField("restUtil");
104 restUtil.setAccessible(true);
105 restUtil.set(nssiManagerService, this.restUtil);
107 Field repository = nssiManagerService.getClass().getDeclaredField("repository");
108 repository.setAccessible(true);
109 repository.set(nssiManagerService, this.repository);
110 // nssiManagerService.setRestUtil(this.restUtil);
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();
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);
121 when(statusLine.getStatusCode()).thenReturn(statusCode);
122 when(restUtil.getNssmfHost(any(EsrInfo.class))).thenReturn(nssmf);
124 when(tokenResponse.getEntity()).thenReturn(tokenEntity);
125 when(tokenResponse.getStatusLine()).thenReturn(statusLine);
126 when(tokenEntity.getContent()).thenReturn(tokenStream);
128 when(commonResponse.getEntity()).thenReturn(commonEntity);
129 when(commonResponse.getStatusLine()).thenReturn(statusLine);
130 when(commonEntity.getContent()).thenReturn(postStream);
132 Answer<HttpResponse> answer = invocation -> {
133 Object[] arguments = invocation.getArguments();
134 if (arguments != null && arguments.length == 1 && arguments[0] != null) {
136 HttpRequestBase base = (HttpRequestBase) arguments[0];
137 if (base.getURI().toString().endsWith("/oauth/token")) {
138 return tokenResponse;
140 return commonResponse;
143 return commonResponse;
146 doAnswer(answer).when(httpClient).execute(any(HttpRequestBase.class));
151 public void allocateNssi() throws Exception {
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");
160 NssiResponse nssiRes = new NssiResponse();
161 nssiRes.setJobId("4b45d919816ccaa2b762df5120f72067");
162 nssiRes.setNssiId("NSSI-C-001-HDBNJ-NSSMF-01-A-ZX");
164 TokenResponse token = new TokenResponse();
165 token.setAccessToken("7512eb3feb5249eca5ddd742fedddd39");
166 token.setExpires(1800);
168 postStream = new ByteArrayInputStream(marshal(nssiRes).getBytes(UTF_8));
169 tokenStream = new ByteArrayInputStream(marshal(token).getBytes(UTF_8));
171 createCommonMock(200, nssmf);
174 NssmfAdapterNBIRequest nbiRequest = createAllocateNssi();
175 assertNotNull(nbiRequest);
176 System.out.println(marshal(nbiRequest));
177 ResponseEntity res = nssiManagerService.allocateNssi(nbiRequest);
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");
184 System.out.println(res);
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<>();
196 PerfReqEmbb embb = new PerfReqEmbb();
197 embb.setActivityFactor(50);
198 List<PerfReqEmbb> embbList = new LinkedList<>();
200 PerfReq perfReq = new PerfReq();
201 perfReq.setPerfReqEmbbList(embbList);
202 List<String> taList = new LinkedList<>();
206 sP.setSnssaiList(sns);
207 sP.setSliceProfileId("ab9af40f13f721b5f13539d87484098");
208 sP.setPLMNIdList(plmn);
209 sP.setPerfReq(perfReq);
210 sP.setMaxNumberOfUEs(200);
211 sP.setCoverageAreaTAList(taList);
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);
224 NssmfAdapterNBIRequest nbiRequest = createNbiRequest();
225 nbiRequest.setAllocateCnNssi(cnNssi);
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);
240 NssmfAdapterNBIRequest nbiRequest = createNbiRequest();
241 nbiRequest.setDeAllocateNssi(deAllocateNssi);
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");
249 NssiResponse nssiRes = new NssiResponse();
250 nssiRes.setJobId("4b45d919816ccaa2b762df5120f72067");
252 TokenResponse token = new TokenResponse();
253 token.setAccessToken("7512eb3feb5249eca5ddd742fedddd39");
254 token.setExpires(1800);
256 postStream = new ByteArrayInputStream(marshal(nssiRes).getBytes(UTF_8));
257 tokenStream = new ByteArrayInputStream(marshal(token).getBytes(UTF_8));
259 createCommonMock(202, nssmf);
260 ResponseEntity res = nssiManagerService.deAllocateNssi(nbiRequest, "ab9af40f13f721b5f13539d87484098");
262 assertNotNull(res.getBody());
263 NssiResponse allRes = unMarshal(res.getBody().toString(), NssiResponse.class);
264 assertEquals(allRes.getJobId(), "4b45d919816ccaa2b762df5120f72067");
266 assertNotNull(res.getBody());
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");
277 NssiResponse nssiRes = new NssiResponse();
278 nssiRes.setJobId("4b45d919816ccaa2b762df5120f72067");
280 TokenResponse token = new TokenResponse();
281 token.setAccessToken("7512eb3feb5249eca5ddd742fedddd39");
282 token.setExpires(1800);
284 postStream = new ByteArrayInputStream(marshal(nssiRes).getBytes(UTF_8));
285 tokenStream = new ByteArrayInputStream(marshal(token).getBytes(UTF_8));
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");
291 NssmfAdapterNBIRequest nbiRequest = createNbiRequest();
292 nbiRequest.setActDeActNssi(act);
294 createCommonMock(200, nssmf);
295 ResponseEntity res = nssiManagerService.activateNssi(nbiRequest, "001-100001");
297 assertNotNull(res.getBody());
298 NssiResponse allRes = unMarshal(res.getBody().toString(), NssiResponse.class);
299 assertEquals(allRes.getJobId(), "4b45d919816ccaa2b762df5120f72067");
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");
310 NssiResponse nssiRes = new NssiResponse();
311 nssiRes.setJobId("4b45d919816ccaa2b762df5120f72067");
313 TokenResponse token = new TokenResponse();
314 token.setAccessToken("7512eb3feb5249eca5ddd742fedddd39");
315 token.setExpires(1800);
317 postStream = new ByteArrayInputStream(marshal(nssiRes).getBytes(UTF_8));
318 tokenStream = new ByteArrayInputStream(marshal(token).getBytes(UTF_8));
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");
324 NssmfAdapterNBIRequest nbiRequest = createNbiRequest();
325 nbiRequest.setActDeActNssi(act);
327 createCommonMock(200, nssmf);
328 ResponseEntity res = nssiManagerService.deActivateNssi(nbiRequest, "001-100001");
330 assertNotNull(res.getBody());
331 NssiResponse allRes = unMarshal(res.getBody().toString(), NssiResponse.class);
332 assertEquals(allRes.getJobId(), "4b45d919816ccaa2b762df5120f72067");
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");
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);
351 TokenResponse token = new TokenResponse();
352 token.setAccessToken("7512eb3feb5249eca5ddd742fedddd39");
353 token.setExpires(1800);
355 postStream = new ByteArrayInputStream(marshal(jobStatusResponse).getBytes(UTF_8));
356 tokenStream = new ByteArrayInputStream(marshal(token).getBytes(UTF_8));
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());
364 NssmfAdapterNBIRequest nbiRequest = createNbiRequest();
365 nbiRequest.setResponseId("7512eb3feb5249eca5ddd742fedddd39");
366 List<ResourceOperationStatus> optional = new ArrayList<>();
367 optional.add(operationStatus);
369 doAnswer(invocation -> optional).when(repository).findByServiceIdAndOperationId(any(), any());
371 createCommonMock(200, nssmf);
373 ResponseEntity res = nssiManagerService.queryJobStatus(nbiRequest, "4b45d919816ccaa2b762df5120f72067");
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");
384 public void queryNSSISelectionCapability() throws Exception {
386 NssmfAdapterNBIRequest nbiRequest = createNbiRequest();
387 ResponseEntity res = nssiManagerService.queryNSSISelectionCapability(nbiRequest);
389 assertNotNull(res.getBody());
390 Map allRes = unMarshal(res.getBody().toString(), Map.class);
391 assertEquals(allRes.get("selection"), "NSMF");
393 System.out.println(res);
395 nbiRequest.getEsrInfo().setVendor(NssmfAdapterConsts.ONAP_INTERNAL_TAG);
396 res = nssiManagerService.queryNSSISelectionCapability(nbiRequest);
398 assertNotNull(res.getBody());
399 allRes = unMarshal(res.getBody().toString(), Map.class);
400 assertEquals(allRes.get("selection"), "NSSMF");
402 System.out.println(res);
404 nbiRequest.getEsrInfo().setNetworkType(NetworkType.ACCESS);
405 res = nssiManagerService.queryNSSISelectionCapability(nbiRequest);
407 assertNotNull(res.getBody());
408 allRes = unMarshal(res.getBody().toString(), Map.class);
409 assertEquals(allRes.get("selection"), "NSSMF");
411 System.out.println(res);
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);
431 public void querySubnetCapability() {
432 NssmfAdapterNBIRequest nbiRequest = createNbiRequest();
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);
440 assertNotNull(res.getBody());