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.springframework.data.domain.Example;
 
  35 import java.util.HashMap;
 
  37 import java.util.Optional;
 
  38 import static org.onap.so.adapters.nssmf.util.NssmfAdapterUtil.marshal;
 
  40 public abstract class BaseNssmfManager implements NssmfManager {
 
  42     protected RestUtil restUtil;
 
  44     protected ResourceOperationStatusRepository repository;
 
  46     protected NssmfAdapterConfig adapterConfig;
 
  48     protected ActionType actionType;
 
  50     protected EsrInfo esrInfo;
 
  52     protected String nssmfUrl;
 
  54     protected HttpMethod httpMethod;
 
  56     protected String initStatus;
 
  58     protected ServiceInfo serviceInfo;
 
  60     protected RestResponse restResponse;
 
  62     private ExecutorType executorType = ExecutorType.INTERNAL;
 
  64     private Map<String, String> params = new HashMap<>(); // request params
 
  67     public RestResponse allocateNssi(NssmfAdapterNBIRequest nbiRequest) throws ApplicationException {
 
  71         String requestBody = wrapAllocateReqBody(nbiRequest);
 
  73         this.restResponse = sendRequest(requestBody);
 
  80     protected abstract String wrapAllocateReqBody(NssmfAdapterNBIRequest nbiRequest) throws ApplicationException;
 
  83     public RestResponse modifyNssi(NssmfAdapterNBIRequest modifyRequest) throws ApplicationException {
 
  86         String requestBody = wrapModifyReqBody(modifyRequest);
 
  88         this.restResponse = sendRequest(requestBody);
 
  95     protected abstract String wrapModifyReqBody(NssmfAdapterNBIRequest nbiRequest) throws ApplicationException;
 
  98     public RestResponse deAllocateNssi(NssmfAdapterNBIRequest nbiRequest, String sliceId) throws ApplicationException {
 
 100         this.params.put("sliceProfileId", sliceId);
 
 104         String reqBody = wrapDeAllocateReqBody(nbiRequest.getDeAllocateNssi());
 
 106         this.restResponse = sendRequest(reqBody);
 
 113     protected abstract String wrapDeAllocateReqBody(DeAllocateNssi deAllocateNssi) throws ApplicationException;
 
 115     protected abstract String wrapReqBody(Object object) throws ApplicationException;
 
 118     public RestResponse activateNssi(NssmfAdapterNBIRequest nbiRequest, String snssai) throws ApplicationException {
 
 120         this.params.put("snssai", snssai);
 
 124         String reqBody = wrapActDeActReqBody(nbiRequest.getActDeActNssi());
 
 126         this.restResponse = sendRequest(reqBody);
 
 134     public RestResponse deActivateNssi(NssmfAdapterNBIRequest nbiRequest, String snssai) throws ApplicationException {
 
 135         return activateNssi(nbiRequest, snssai);
 
 138     protected abstract String wrapActDeActReqBody(ActDeActNssi actDeActNssi) throws ApplicationException;
 
 141     public RestResponse queryJobStatus(NssmfAdapterNBIRequest jobReq, String jobId) throws ApplicationException {
 
 143         this.params.put("jobId", jobId);
 
 144         if (jobReq.getResponseId() != null) {
 
 145             this.params.put("responseId", jobReq.getResponseId());
 
 150          * find by jobId and nsiId jobId -> OperationId nsiId -> ServiceId serviceUuid -> resourceTemplateUUID
 
 152         ResourceOperationStatus status =
 
 153                 getOperationStatus(serviceInfo.getNsiId(), jobId, serviceInfo.getServiceUuid());
 
 155         this.restResponse = doQueryJobStatus(status);
 
 157         afterQueryJobStatus(status);
 
 161     protected abstract RestResponse doQueryJobStatus(ResourceOperationStatus status) throws ApplicationException;
 
 164     protected abstract void afterQueryJobStatus(ResourceOperationStatus status);
 
 166     private ResourceOperationStatus getOperationStatus(String nsiId, String jobId, String serviceUuid) {
 
 168         ResourceOperationStatus status = new ResourceOperationStatus(nsiId, jobId, serviceUuid);
 
 170         Optional<ResourceOperationStatus> optional = repository.findOne(Example.of(status));
 
 172         return optional.orElse(null);
 
 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));
 
 186     protected abstract SelectionType doQueryNSSISelectionCapability();
 
 189     public RestResponse querySubnetCapability(NssmfAdapterNBIRequest nbiRequest) throws ApplicationException {
 
 193         return doQuerySubnetCapability(nbiRequest.getSubnetCapabilityQuery());
 
 196     protected abstract RestResponse doQuerySubnetCapability(QuerySubnetCapability req) throws ApplicationException;
 
 199      * send request to nssmf
 
 201      * @param content request body
 
 203      * @throws ApplicationException
 
 205     protected abstract RestResponse sendRequest(String content) throws ApplicationException;
 
 208      * handle the url before request to nssmf, include get the nssmf request url, replace the path variable
 
 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));
 
 224     protected abstract void afterRequest() throws ApplicationException;
 
 226     protected abstract String getApiVersion();
 
 228     public RestUtil getRestUtil() {
 
 232     public BaseNssmfManager setEsrInfo(EsrInfo esrInfo) {
 
 233         this.esrInfo = esrInfo;
 
 237     public BaseNssmfManager setExecutorType(ExecutorType executorType) {
 
 238         this.executorType = executorType;
 
 242     public BaseNssmfManager setRestUtil(RestUtil restUtil) {
 
 243         this.restUtil = restUtil;
 
 247     public BaseNssmfManager setActionType(ActionType actionType) {
 
 248         this.actionType = actionType;
 
 252     public BaseNssmfManager setRepository(ResourceOperationStatusRepository repository) {
 
 253         this.repository = repository;
 
 257     public BaseNssmfManager setServiceInfo(ServiceInfo serviceInfo) {
 
 258         this.serviceInfo = serviceInfo;
 
 262     public BaseNssmfManager setInitStatus(String initStatus) {
 
 263         this.initStatus = initStatus;
 
 267     public BaseNssmfManager setAdapterConfig(NssmfAdapterConfig adapterConfig) {
 
 268         this.adapterConfig = adapterConfig;