1408123be77fc1183c4621b8814110c8e6392f00
[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 import org.apache.commons.lang3.StringUtils;
24 import org.onap.so.adapters.nssmf.config.NssmfAdapterConfig;
25 import org.onap.so.adapters.nssmf.enums.ActionType;
26 import org.onap.so.adapters.nssmf.exceptions.ApplicationException;
27 import org.onap.so.adapters.nssmf.manager.NssmfManagerBuilder;
28 import org.onap.so.adapters.nssmf.entity.RestResponse;
29 import org.onap.so.adapters.nssmf.manager.NssmfManager;
30 import org.onap.so.adapters.nssmf.service.NssmfManagerService;
31 import org.onap.so.adapters.nssmf.util.RestUtil;
32 import org.onap.so.beans.nsmf.*;
33 import org.onap.so.db.request.data.repository.ResourceOperationStatusRepository;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.http.ResponseEntity;
36 import org.springframework.stereotype.Service;
37
38
39 @Service
40 public class NssmfManagerServiceImpl implements NssmfManagerService {
41
42     @Autowired
43     private RestUtil restUtil;
44
45     @Autowired
46     private ResourceOperationStatusRepository repository;
47
48     @Autowired
49     private NssmfAdapterConfig nssmfAdapterConfig;
50
51     @Override
52     public ResponseEntity allocateNssi(NssmfAdapterNBIRequest request) {
53         try {
54
55             if (StringUtils.isNotBlank(request.getServiceInfo().getNssiId())) {
56                 return buildResponse(buildNssmfManager(request, ActionType.MODIFY).modifyNssi(request));
57             }
58
59             return buildResponse(buildNssmfManager(request, ActionType.ALLOCATE).allocateNssi(request));
60
61         } catch (ApplicationException e) {
62             return e.buildErrorResponse();
63         }
64     }
65
66     @Override
67     public ResponseEntity deAllocateNssi(NssmfAdapterNBIRequest request, String sliceProfileId) {
68         try {
69             return buildResponse(
70                     buildNssmfManager(request, ActionType.DEALLOCATE).deAllocateNssi(request, sliceProfileId));
71         } catch (ApplicationException e) {
72             return e.buildErrorResponse();
73         }
74     }
75
76     @Override
77     public ResponseEntity activateNssi(NssmfAdapterNBIRequest request, String snssai) {
78         try {
79             return buildResponse(buildNssmfManager(request, ActionType.ACTIVATE).activateNssi(request, snssai));
80         } catch (ApplicationException e) {
81             return e.buildErrorResponse();
82         }
83     }
84
85     @Override
86     public ResponseEntity deActivateNssi(NssmfAdapterNBIRequest request, String snssai) {
87         try {
88             return buildResponse(buildNssmfManager(request, ActionType.DEACTIVATE).deActivateNssi(request, snssai));
89         } catch (ApplicationException e) {
90             return e.buildErrorResponse();
91         }
92     }
93
94     @Override
95     public ResponseEntity queryJobStatus(NssmfAdapterNBIRequest jobReq, String jobId) {
96         try {
97             return buildResponse(buildNssmfManager(jobReq, ActionType.QUERY_JOB_STATUS).queryJobStatus(jobReq, jobId));
98         } catch (ApplicationException e) {
99             return e.buildErrorResponse();
100         }
101     }
102
103     @Override
104     public ResponseEntity queryNSSISelectionCapability(NssmfAdapterNBIRequest nbiRequest) {
105         EsrInfo esrInfo = nbiRequest.getEsrInfo();
106         try {
107             return buildResponse(buildNssmfManager(esrInfo, ActionType.QUERY_JOB_STATUS, null)
108                     .queryNSSISelectionCapability(nbiRequest));
109         } catch (ApplicationException e) {
110             return e.buildErrorResponse();
111         }
112     }
113
114     @Override
115     public ResponseEntity querySubnetCapability(NssmfAdapterNBIRequest nbiRequest) {
116         EsrInfo esrInfo = nbiRequest.getEsrInfo();
117         try {
118             return buildResponse(
119                     buildNssmfManager(esrInfo, ActionType.QUERY_JOB_STATUS, null).querySubnetCapability(nbiRequest));
120         } catch (ApplicationException e) {
121             return e.buildErrorResponse();
122         }
123     }
124
125     private ResponseEntity buildResponse(RestResponse rsp) {
126         return ResponseEntity.status(rsp.getStatus()).body(rsp.getResponseContent());
127     }
128
129
130     private NssmfManager buildNssmfManager(NssmfAdapterNBIRequest request, ActionType actionType)
131             throws ApplicationException {
132         return buildNssmfManager(request.getEsrInfo(), actionType, request.getServiceInfo());
133     }
134
135     private NssmfManager buildNssmfManager(EsrInfo esrInfo, ActionType actionType, ServiceInfo serviceInfo)
136             throws ApplicationException {
137
138         return new NssmfManagerBuilder(esrInfo).setActionType(actionType).setRepository(repository)
139                 .setRestUtil(restUtil).setAdapterConfig(nssmfAdapterConfig).setServiceInfo(serviceInfo).build();
140     }
141 }