789f4f239d9e679a3ca08532c07661bef6e7bef0
[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         if (jobReq.getResponseId() != null) {
145             this.params.put("responseId", jobReq.getResponseId());
146         }
147         this.urlHandler();
148
149         /**
150          * find by jobId and nsiId jobId -> OperationId nsiId -> ServiceId serviceUuid -> resourceTemplateUUID
151          */
152         ResourceOperationStatus status =
153                 getOperationStatus(serviceInfo.getNsiId(), jobId, serviceInfo.getServiceUuid());
154
155         this.restResponse = doQueryJobStatus(status);
156
157         afterQueryJobStatus(status);
158         return restResponse;
159     }
160
161     protected abstract RestResponse doQueryJobStatus(ResourceOperationStatus status) throws ApplicationException;
162
163
164     protected abstract void afterQueryJobStatus(ResourceOperationStatus status);
165
166     private ResourceOperationStatus getOperationStatus(String nsiId, String jobId, String serviceUuid) {
167
168         ResourceOperationStatus status = new ResourceOperationStatus(nsiId, jobId, serviceUuid);
169
170         Optional<ResourceOperationStatus> optional = repository.findOne(Example.of(status));
171
172         return optional.orElse(null);
173     }
174
175     @Override
176     public RestResponse queryNSSISelectionCapability(NssmfAdapterNBIRequest nbiRequest) throws ApplicationException {
177         SelectionType res = doQueryNSSISelectionCapability();
178         HashMap<String, String> hashMap = new HashMap<>();
179         hashMap.put("selection", res.name());
180         RestResponse restResponse = new RestResponse();
181         restResponse.setStatus(200);
182         restResponse.setResponseContent(marshal(hashMap));
183         return restResponse;
184     }
185
186     protected abstract SelectionType doQueryNSSISelectionCapability();
187
188     @Override
189     public RestResponse querySubnetCapability(NssmfAdapterNBIRequest nbiRequest) throws ApplicationException {
190         this.params.clear();
191         this.urlHandler();
192
193         return doQuerySubnetCapability(nbiRequest.getSubnetCapabilityQuery());
194     }
195
196     protected abstract <T> RestResponse doQuerySubnetCapability(T req) throws ApplicationException;
197
198     /**
199      * send request to nssmf
200      * 
201      * @param content request body
202      * @return response
203      * @throws ApplicationException
204      */
205     protected abstract RestResponse sendRequest(String content) throws ApplicationException;
206
207     /**
208      * handle the url before request to nssmf, include get the nssmf request url, replace the path variable
209      */
210     private void urlHandler() {
211         NssmfUrlInfo nssmfUrlInfo =
212                 NssmfAdapterConsts.getNssmfUrlInfo(this.executorType, this.esrInfo.getNetworkType(), actionType);
213         if (nssmfUrlInfo != null) {
214             this.nssmfUrl = nssmfUrlInfo.getUrl();
215             this.httpMethod = nssmfUrlInfo.getHttpMethod();
216             this.nssmfUrl = nssmfUrl.replaceAll("\\{apiVersion}", getApiVersion());
217             this.params.forEach((k, v) -> this.nssmfUrl = this.nssmfUrl.replaceAll("\\{" + k + "}", v));
218         }
219     }
220
221     /**
222      * after request
223      */
224     protected abstract void afterRequest() throws ApplicationException;
225
226     protected abstract String getApiVersion();
227
228     public RestUtil getRestUtil() {
229         return restUtil;
230     }
231
232     public BaseNssmfManager setEsrInfo(EsrInfo esrInfo) {
233         this.esrInfo = esrInfo;
234         return this;
235     }
236
237     public BaseNssmfManager setExecutorType(ExecutorType executorType) {
238         this.executorType = executorType;
239         return this;
240     }
241
242     public BaseNssmfManager setRestUtil(RestUtil restUtil) {
243         this.restUtil = restUtil;
244         return this;
245     }
246
247     public BaseNssmfManager setActionType(ActionType actionType) {
248         this.actionType = actionType;
249         return this;
250     }
251
252     public BaseNssmfManager setRepository(ResourceOperationStatusRepository repository) {
253         this.repository = repository;
254         return this;
255     }
256
257     public BaseNssmfManager setServiceInfo(ServiceInfo serviceInfo) {
258         this.serviceInfo = serviceInfo;
259         return this;
260     }
261
262     public BaseNssmfManager setInitStatus(String initStatus) {
263         this.initStatus = initStatus;
264         return this;
265     }
266
267     public BaseNssmfManager setAdapterConfig(NssmfAdapterConfig adapterConfig) {
268         this.adapterConfig = adapterConfig;
269         return this;
270     }
271 }