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;
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;
44 public abstract class BaseNssmfManager implements NssmfManager {
46 private static final Logger logger = LoggerFactory.getLogger(BaseNssmfManager.class);
48 protected RestUtil restUtil;
50 protected ResourceOperationStatusRepository repository;
52 protected NssmfAdapterConfig adapterConfig;
54 protected ActionType actionType;
56 protected EsrInfo esrInfo;
58 protected String nssmfUrl;
60 protected HttpMethod httpMethod;
62 protected String initStatus;
64 protected ServiceInfo serviceInfo;
66 protected RestResponse restResponse;
68 private ExecutorType executorType = ExecutorType.INTERNAL;
70 private Map<String, String> params = new HashMap<>(); // request params
73 public RestResponse allocateNssi(NssmfAdapterNBIRequest nbiRequest) throws ApplicationException {
77 String requestBody = wrapAllocateReqBody(nbiRequest);
79 this.restResponse = sendRequest(requestBody);
86 protected abstract String wrapAllocateReqBody(NssmfAdapterNBIRequest nbiRequest) throws ApplicationException;
89 public RestResponse modifyNssi(NssmfAdapterNBIRequest modifyRequest) throws ApplicationException {
92 String requestBody = wrapModifyReqBody(modifyRequest);
94 this.restResponse = sendRequest(requestBody);
101 protected abstract String wrapModifyReqBody(NssmfAdapterNBIRequest nbiRequest) throws ApplicationException;
104 public RestResponse deAllocateNssi(NssmfAdapterNBIRequest nbiRequest, String sliceId) throws ApplicationException {
106 this.params.put("sliceProfileId", sliceId);
110 String reqBody = wrapDeAllocateReqBody(nbiRequest.getDeAllocateNssi());
112 this.restResponse = sendRequest(reqBody);
119 protected abstract String wrapDeAllocateReqBody(DeAllocateNssi deAllocateNssi) throws ApplicationException;
121 protected abstract String wrapReqBody(Object object) throws ApplicationException;
124 public RestResponse activateNssi(NssmfAdapterNBIRequest nbiRequest, String snssai) throws ApplicationException {
126 this.params.put("snssai", snssai);
130 String reqBody = wrapActDeActReqBody(nbiRequest.getActDeActNssi());
132 this.restResponse = sendRequest(reqBody);
140 public RestResponse deActivateNssi(NssmfAdapterNBIRequest nbiRequest, String snssai) throws ApplicationException {
141 return activateNssi(nbiRequest, snssai);
144 protected abstract String wrapActDeActReqBody(ActDeActNssi actDeActNssi) throws ApplicationException;
147 public RestResponse queryJobStatus(NssmfAdapterNBIRequest jobReq, String jobId) throws ApplicationException {
149 this.params.put("jobId", jobId);
150 this.params.put("responseId", jobReq.getResponseId());
154 * find by jobId and nsiId jobId -> OperationId nsiId -> ServiceId serviceUuid -> resourceTemplateUUID
156 ResourceOperationStatus status =
157 getOperationStatus(serviceInfo.getNsiId(), jobId, serviceInfo.getServiceUuid());
159 this.restResponse = doQueryJobStatus(status);
161 afterQueryJobStatus(status);
165 protected abstract RestResponse doQueryJobStatus(ResourceOperationStatus status) throws ApplicationException;
168 protected abstract void afterQueryJobStatus(ResourceOperationStatus status);
170 private ResourceOperationStatus getOperationStatus(String nsiId, String jobId, String serviceUuid) {
172 ResourceOperationStatus status = new ResourceOperationStatus(nsiId, jobId, serviceUuid);
174 Optional<ResourceOperationStatus> optional = repository.findOne(Example.of(status));
176 return optional.orElse(null);
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));
190 protected abstract SelectionType doQueryNSSISelectionCapability();
193 public RestResponse querySubnetCapability(NssmfAdapterNBIRequest nbiRequest) throws ApplicationException {
197 return doQuerySubnetCapability(nbiRequest.getSubnetCapabilityQuery());
200 protected abstract RestResponse doQuerySubnetCapability(String req) throws ApplicationException;
203 * send request to nssmf
205 * @param content request body
207 * @throws ApplicationException
209 protected abstract RestResponse sendRequest(String content) throws ApplicationException;
212 * handle the url before request to nssmf, include get the nssmf request url, replace the path variable
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));
226 protected abstract void afterRequest() throws ApplicationException;
228 protected abstract String getApiVersion();
230 public RestUtil getRestUtil() {
234 public BaseNssmfManager setEsrInfo(EsrInfo esrInfo) {
235 this.esrInfo = esrInfo;
239 public BaseNssmfManager setExecutorType(ExecutorType executorType) {
240 this.executorType = executorType;
244 public BaseNssmfManager setRestUtil(RestUtil restUtil) {
245 this.restUtil = restUtil;
249 public BaseNssmfManager setActionType(ActionType actionType) {
250 this.actionType = actionType;
254 public BaseNssmfManager setRepository(ResourceOperationStatusRepository repository) {
255 this.repository = repository;
259 public BaseNssmfManager setServiceInfo(ServiceInfo serviceInfo) {
260 this.serviceInfo = serviceInfo;
264 public BaseNssmfManager setInitStatus(String initStatus) {
265 this.initStatus = initStatus;
269 public BaseNssmfManager setAdapterConfig(NssmfAdapterConfig adapterConfig) {
270 this.adapterConfig = adapterConfig;