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