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