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