2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 # Copyright (c) 2020, CMCC Technologies Co., Ltd.
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
11 # http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.so.adapters.nssmf.manager.impl;
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;
40 import java.util.Optional;
41 import static org.onap.so.adapters.nssmf.util.NssmfAdapterUtil.marshal;
43 public abstract class BaseNssmfManager implements NssmfManager {
45 private static final Logger logger = LoggerFactory.getLogger(BaseNssmfManager.class);
47 protected RestUtil restUtil;
49 protected ResourceOperationStatusRepository repository;
51 protected NssmfAdapterConfig adapterConfig;
53 protected ActionType actionType;
55 protected EsrInfo esrInfo;
57 protected String nssmfUrl;
59 protected HttpMethod httpMethod;
61 protected String initStatus;
63 protected ServiceInfo serviceInfo;
65 protected RestResponse restResponse;
67 private ExecutorType executorType = ExecutorType.INTERNAL;
69 private Map<String, String> params = new HashMap<>(); // request params
72 public RestResponse allocateNssi(NssmfAdapterNBIRequest nbiRequest) throws ApplicationException {
76 String requestBody = wrapAllocateReqBody(nbiRequest);
78 this.restResponse = sendRequest(requestBody);
85 protected abstract String wrapAllocateReqBody(NssmfAdapterNBIRequest nbiRequest) throws ApplicationException;
88 public RestResponse modifyNssi(NssmfAdapterNBIRequest modifyRequest) throws ApplicationException {
91 String requestBody = wrapModifyReqBody(modifyRequest);
93 this.restResponse = sendRequest(requestBody);
100 protected abstract String wrapModifyReqBody(NssmfAdapterNBIRequest nbiRequest) throws ApplicationException;
103 public RestResponse deAllocateNssi(NssmfAdapterNBIRequest nbiRequest, String sliceId) throws ApplicationException {
105 this.params.put("sliceProfileId", sliceId);
109 String reqBody = wrapDeAllocateReqBody(nbiRequest.getDeAllocateNssi());
111 this.restResponse = sendRequest(reqBody);
118 protected abstract String wrapDeAllocateReqBody(DeAllocateNssi deAllocateNssi) throws ApplicationException;
120 protected abstract String wrapReqBody(Object object) throws ApplicationException;
123 public RestResponse activateNssi(NssmfAdapterNBIRequest nbiRequest, String snssai) throws ApplicationException {
125 this.params.put("snssai", snssai);
129 String reqBody = wrapActDeActReqBody(nbiRequest.getActDeActNssi());
131 this.restResponse = sendRequest(reqBody);
139 public RestResponse deActivateNssi(NssmfAdapterNBIRequest nbiRequest, String snssai) throws ApplicationException {
140 return activateNssi(nbiRequest, snssai);
143 protected abstract String wrapActDeActReqBody(ActDeActNssi actDeActNssi) throws ApplicationException;
146 public RestResponse queryJobStatus(NssmfAdapterNBIRequest jobReq, String jobId) throws ApplicationException {
148 this.params.put("jobId", jobId);
149 if (jobReq.getResponseId() != null) {
150 this.params.put("responseId", jobReq.getResponseId());
155 * find by jobId and nsiId jobId -> OperationId nsiId -> ServiceId serviceUuid -> resourceTemplateUUID
157 ResourceOperationStatus status =
158 getOperationStatus(serviceInfo.getNsiId(), jobId, serviceInfo.getServiceUuid());
160 logger.info("ResourceOperationStatus = {}", status);
161 this.restResponse = doQueryJobStatus(status);
163 afterQueryJobStatus(status);
167 protected abstract RestResponse doQueryJobStatus(ResourceOperationStatus status) throws ApplicationException;
170 protected abstract void afterQueryJobStatus(ResourceOperationStatus status);
172 private ResourceOperationStatus getOperationStatus(String nsiId, String jobId, String serviceUuid) {
174 logger.info("Query operations: nsiId = [{}], jobId = [{}], serviceUuid = [{}]", nsiId, jobId, serviceUuid);
176 List<ResourceOperationStatus> resourceOperationStatuses =
177 repository.findByServiceIdAndOperationId(nsiId, jobId);
179 logger.info("resourceOperationStatuses = {}", resourceOperationStatuses);
180 if (resourceOperationStatuses.size() == 0) {
183 return resourceOperationStatuses.get(0);
187 public RestResponse queryNSSISelectionCapability(NssmfAdapterNBIRequest nbiRequest) throws ApplicationException {
188 SelectionType res = doQueryNSSISelectionCapability();
189 HashMap<String, String> hashMap = new HashMap<>();
190 hashMap.put("selection", res.name());
191 RestResponse restResponse = new RestResponse();
192 restResponse.setStatus(200);
193 restResponse.setResponseContent(marshal(hashMap));
197 protected abstract SelectionType doQueryNSSISelectionCapability();
200 public RestResponse querySubnetCapability(NssmfAdapterNBIRequest nbiRequest) throws ApplicationException {
204 return doQuerySubnetCapability(nbiRequest.getSubnetCapabilityQuery());
207 protected abstract RestResponse doQuerySubnetCapability(QuerySubnetCapability req) throws ApplicationException;
210 * send request to nssmf
212 * @param content request body
214 * @throws ApplicationException
216 protected abstract RestResponse sendRequest(String content) throws ApplicationException;
219 * handle the url before request to nssmf, include get the nssmf request url, replace the path variable
221 private void urlHandler() {
222 NssmfUrlInfo nssmfUrlInfo =
223 NssmfAdapterConsts.getNssmfUrlInfo(this.executorType, this.esrInfo.getNetworkType(), actionType);
224 if (nssmfUrlInfo != null) {
225 this.nssmfUrl = nssmfUrlInfo.getUrl();
226 this.httpMethod = nssmfUrlInfo.getHttpMethod();
227 this.nssmfUrl = nssmfUrl.replaceAll("\\{apiVersion}", getApiVersion());
228 this.params.forEach((k, v) -> this.nssmfUrl = this.nssmfUrl.replaceAll("\\{" + k + "}", v));
235 protected abstract void afterRequest() throws ApplicationException;
237 protected abstract String getApiVersion();
239 public RestUtil getRestUtil() {
243 public BaseNssmfManager setEsrInfo(EsrInfo esrInfo) {
244 this.esrInfo = esrInfo;
248 public BaseNssmfManager setExecutorType(ExecutorType executorType) {
249 this.executorType = executorType;
253 public BaseNssmfManager setRestUtil(RestUtil restUtil) {
254 this.restUtil = restUtil;
258 public BaseNssmfManager setActionType(ActionType actionType) {
259 this.actionType = actionType;
263 public BaseNssmfManager setRepository(ResourceOperationStatusRepository repository) {
264 this.repository = repository;
268 public BaseNssmfManager setServiceInfo(ServiceInfo serviceInfo) {
269 this.serviceInfo = serviceInfo;
273 public BaseNssmfManager setInitStatus(String initStatus) {
274 this.initStatus = initStatus;
278 public BaseNssmfManager setAdapterConfig(NssmfAdapterConfig adapterConfig) {
279 this.adapterConfig = adapterConfig;