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         ResourceOperationStatus status = new ResourceOperationStatus(nsiId, jobId, serviceUuid);
 
 178         List<ResourceOperationStatus> resourceOperationStatuses =
 
 179                 repository.findByServiceIdAndOperationId(nsiId, jobId);
 
 181         logger.info("resourceOperationStatuses = {}", resourceOperationStatuses);
 
 182         if (resourceOperationStatuses.size() == 0) {
 
 185         return resourceOperationStatuses.get(0);
 
 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));
 
 199     protected abstract SelectionType doQueryNSSISelectionCapability();
 
 202     public RestResponse querySubnetCapability(NssmfAdapterNBIRequest nbiRequest) throws ApplicationException {
 
 206         return doQuerySubnetCapability(nbiRequest.getSubnetCapabilityQuery());
 
 209     protected abstract RestResponse doQuerySubnetCapability(QuerySubnetCapability req) throws ApplicationException;
 
 212      * send request to nssmf
 
 214      * @param content request body
 
 216      * @throws ApplicationException
 
 218     protected abstract RestResponse sendRequest(String content) throws ApplicationException;
 
 221      * handle the url before request to nssmf, include get the nssmf request url, replace the path variable
 
 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));
 
 237     protected abstract void afterRequest() throws ApplicationException;
 
 239     protected abstract String getApiVersion();
 
 241     public RestUtil getRestUtil() {
 
 245     public BaseNssmfManager setEsrInfo(EsrInfo esrInfo) {
 
 246         this.esrInfo = esrInfo;
 
 250     public BaseNssmfManager setExecutorType(ExecutorType executorType) {
 
 251         this.executorType = executorType;
 
 255     public BaseNssmfManager setRestUtil(RestUtil restUtil) {
 
 256         this.restUtil = restUtil;
 
 260     public BaseNssmfManager setActionType(ActionType actionType) {
 
 261         this.actionType = actionType;
 
 265     public BaseNssmfManager setRepository(ResourceOperationStatusRepository repository) {
 
 266         this.repository = repository;
 
 270     public BaseNssmfManager setServiceInfo(ServiceInfo serviceInfo) {
 
 271         this.serviceInfo = serviceInfo;
 
 275     public BaseNssmfManager setInitStatus(String initStatus) {
 
 276         this.initStatus = initStatus;
 
 280     public BaseNssmfManager setAdapterConfig(NssmfAdapterConfig adapterConfig) {
 
 281         this.adapterConfig = adapterConfig;