97a4c5e8896936da30bbcdecdcae201c7b1caac1
[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.manager.impl;
22
23 import org.onap.so.adapters.nssmf.config.NssmfAdapterConfig;
24 import org.onap.so.adapters.nssmf.consts.NssmfAdapterConsts;
25 import org.onap.so.adapters.nssmf.entity.NssmfUrlInfo;
26 import org.onap.so.adapters.nssmf.enums.*;
27 import org.onap.so.adapters.nssmf.exceptions.ApplicationException;
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.util.RestUtil;
31 import org.onap.so.beans.nsmf.*;
32 import org.onap.so.db.request.beans.ResourceOperationStatus;
33 import org.onap.so.db.request.data.repository.ResourceOperationStatusRepository;
34 import org.springframework.data.domain.Example;
35 import java.util.HashMap;
36 import java.util.Map;
37 import java.util.Optional;
38 import static org.onap.so.adapters.nssmf.util.NssmfAdapterUtil.marshal;
39
40 public abstract class BaseNssmfManager implements NssmfManager {
41
42     protected RestUtil restUtil;
43
44     protected ResourceOperationStatusRepository repository;
45
46     protected NssmfAdapterConfig adapterConfig;
47
48     protected ActionType actionType;
49
50     protected EsrInfo esrInfo;
51
52     protected String nssmfUrl;
53
54     protected HttpMethod httpMethod;
55
56     protected String initStatus;
57
58     protected ServiceInfo serviceInfo;
59
60     protected RestResponse restResponse;
61
62     private ExecutorType executorType = ExecutorType.INTERNAL;
63
64     private Map<String, String> params = new HashMap<>(); // request params
65
66     @Override
67     public RestResponse allocateNssi(NssmfAdapterNBIRequest nbiRequest) throws ApplicationException {
68
69         this.params.clear();
70         this.urlHandler();
71         String requestBody = wrapAllocateReqBody(nbiRequest);
72
73         this.restResponse = sendRequest(requestBody);
74
75         this.afterRequest();
76
77         return restResponse;
78     }
79
80     protected abstract String wrapAllocateReqBody(NssmfAdapterNBIRequest nbiRequest) throws ApplicationException;
81
82     @Override
83     public RestResponse modifyNssi(NssmfAdapterNBIRequest modifyRequest) throws ApplicationException {
84         this.params.clear();
85         this.urlHandler();
86         String requestBody = wrapModifyReqBody(modifyRequest);
87
88         this.restResponse = sendRequest(requestBody);
89
90         this.afterRequest();
91
92         return restResponse;
93     }
94
95     protected abstract String wrapModifyReqBody(NssmfAdapterNBIRequest nbiRequest) throws ApplicationException;
96
97     @Override
98     public RestResponse deAllocateNssi(NssmfAdapterNBIRequest nbiRequest, String sliceId) throws ApplicationException {
99         this.params.clear();
100         this.params.put("sliceProfileId", sliceId);
101
102         this.urlHandler();
103
104         String reqBody = wrapDeAllocateReqBody(nbiRequest.getDeAllocateNssi());
105
106         this.restResponse = sendRequest(reqBody);
107
108         this.afterRequest();
109
110         return restResponse;
111     }
112
113     protected abstract String wrapDeAllocateReqBody(DeAllocateNssi deAllocateNssi) throws ApplicationException;
114
115     protected abstract String wrapReqBody(Object object) throws ApplicationException;
116
117     @Override
118     public RestResponse activateNssi(NssmfAdapterNBIRequest nbiRequest, String snssai) throws ApplicationException {
119         this.params.clear();
120         this.params.put("snssai", snssai);
121
122         this.urlHandler();
123
124         String reqBody = wrapActDeActReqBody(nbiRequest.getActDeActNssi());
125
126         this.restResponse = sendRequest(reqBody);
127
128         this.afterRequest();
129
130         return restResponse;
131     }
132
133     @Override
134     public RestResponse deActivateNssi(NssmfAdapterNBIRequest nbiRequest, String snssai) throws ApplicationException {
135         return activateNssi(nbiRequest, snssai);
136     }
137
138     protected abstract String wrapActDeActReqBody(ActDeActNssi actDeActNssi) throws ApplicationException;
139
140     @Override
141     public RestResponse queryJobStatus(NssmfAdapterNBIRequest jobReq, String jobId) throws ApplicationException {
142         this.params.clear();
143         this.params.put("jobId", jobId);
144         this.params.put("responseId", jobReq.getResponseId());
145         this.urlHandler();
146
147         /**
148          * find by jobId and nsiId jobId -> OperationId nsiId -> ServiceId serviceUuid -> resourceTemplateUUID
149          */
150         ResourceOperationStatus status =
151                 getOperationStatus(serviceInfo.getNsiId(), jobId, serviceInfo.getServiceUuid());
152
153         this.restResponse = doQueryJobStatus(status);
154
155         afterQueryJobStatus(status);
156         return restResponse;
157     }
158
159     protected abstract RestResponse doQueryJobStatus(ResourceOperationStatus status) throws ApplicationException;
160
161
162     protected abstract void afterQueryJobStatus(ResourceOperationStatus status);
163
164     private ResourceOperationStatus getOperationStatus(String nsiId, String jobId, String serviceUuid) {
165
166         ResourceOperationStatus status = new ResourceOperationStatus(nsiId, jobId, serviceUuid);
167
168         Optional<ResourceOperationStatus> optional = repository.findOne(Example.of(status));
169
170         return optional.orElse(null);
171     }
172
173     @Override
174     public RestResponse queryNSSISelectionCapability(NssmfAdapterNBIRequest nbiRequest) throws ApplicationException {
175         SelectionType res = doQueryNSSISelectionCapability();
176         HashMap<String, String> hashMap = new HashMap<>();
177         hashMap.put("selection", res.name());
178         RestResponse restResponse = new RestResponse();
179         restResponse.setStatus(200);
180         restResponse.setResponseContent(marshal(hashMap));
181         return restResponse;
182     }
183
184     protected abstract SelectionType doQueryNSSISelectionCapability();
185
186     @Override
187     public RestResponse querySubnetCapability(NssmfAdapterNBIRequest nbiRequest) throws ApplicationException {
188         this.params.clear();
189         this.urlHandler();
190
191         return doQuerySubnetCapability(nbiRequest.getSubnetCapabilityQuery());
192     }
193
194     protected abstract RestResponse doQuerySubnetCapability(String req) throws ApplicationException;
195
196     /**
197      * send request to nssmf
198      * 
199      * @param content request body
200      * @return response
201      * @throws ApplicationException
202      */
203     protected abstract RestResponse sendRequest(String content) throws ApplicationException;
204
205     /**
206      * handle the url before request to nssmf, include get the nssmf request url, replace the path variable
207      */
208     private void urlHandler() {
209         NssmfUrlInfo nssmfUrlInfo =
210                 NssmfAdapterConsts.getNssmfUrlInfo(this.executorType, this.esrInfo.getNetworkType(), actionType);
211         this.nssmfUrl = nssmfUrlInfo.getUrl();
212         this.httpMethod = nssmfUrlInfo.getHttpMethod();
213         this.nssmfUrl = nssmfUrl.replaceAll("\\{apiVersion}", getApiVersion());
214         this.params.forEach((k, v) -> this.nssmfUrl = this.nssmfUrl.replaceAll("\\{" + k + "}", v));
215     }
216
217     /**
218      * after request
219      */
220     protected abstract void afterRequest() throws ApplicationException;
221
222     protected abstract String getApiVersion();
223
224     public RestUtil getRestUtil() {
225         return restUtil;
226     }
227
228     public BaseNssmfManager setEsrInfo(EsrInfo esrInfo) {
229         this.esrInfo = esrInfo;
230         return this;
231     }
232
233     public BaseNssmfManager setExecutorType(ExecutorType executorType) {
234         this.executorType = executorType;
235         return this;
236     }
237
238     public BaseNssmfManager setRestUtil(RestUtil restUtil) {
239         this.restUtil = restUtil;
240         return this;
241     }
242
243     public BaseNssmfManager setActionType(ActionType actionType) {
244         this.actionType = actionType;
245         return this;
246     }
247
248     public BaseNssmfManager setRepository(ResourceOperationStatusRepository repository) {
249         this.repository = repository;
250         return this;
251     }
252
253     public BaseNssmfManager setServiceInfo(ServiceInfo serviceInfo) {
254         this.serviceInfo = serviceInfo;
255         return this;
256     }
257
258     public BaseNssmfManager setInitStatus(String initStatus) {
259         this.initStatus = initStatus;
260         return this;
261     }
262
263     public BaseNssmfManager setAdapterConfig(NssmfAdapterConfig adapterConfig) {
264         this.adapterConfig = adapterConfig;
265         return this;
266     }
267 }