2  *  ============LICENSE_START=======================================================
 
   3  *  Copyright (C) 2022 Nordix Foundation
 
   4  *  ================================================================================
 
   5  *  Licensed under the Apache License, Version 2.0 (the "License");
 
   6  *  you may not use this file except in compliance with the License.
 
   7  *  You may obtain a copy of the License at
 
   9  *        http://www.apache.org/licenses/LICENSE-2.0
 
  11  *  Unless required by applicable law or agreed to in writing, software
 
  12  *  distributed under the License is distributed on an "AS IS" BASIS,
 
  13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  14  *  See the License for the specific language governing permissions and
 
  15  *  limitations under the License.
 
  17  *  SPDX-License-Identifier: Apache-2.0
 
  18  *  ============LICENSE_END=========================================================
 
  21 package org.onap.cps.ncmp.rest.controller.handlers;
 
  24 import java.util.UUID;
 
  25 import java.util.function.Supplier;
 
  26 import lombok.RequiredArgsConstructor;
 
  27 import lombok.extern.slf4j.Slf4j;
 
  28 import org.onap.cps.ncmp.api.NetworkCmProxyDataService;
 
  29 import org.onap.cps.ncmp.rest.executor.CpsNcmpTaskExecutor;
 
  30 import org.onap.cps.ncmp.rest.util.TopicValidator;
 
  31 import org.springframework.http.ResponseEntity;
 
  33 @RequiredArgsConstructor
 
  35 public abstract class NcmpDatastoreResourceRequestHandler {
 
  37     private static final String NO_REQUEST_ID = null;
 
  38     private static final String NO_TOPIC = null;
 
  40     protected final NetworkCmProxyDataService networkCmProxyDataService;
 
  41     protected final CpsNcmpTaskExecutor cpsNcmpTaskExecutor;
 
  42     protected final int timeOutInMilliSeconds;
 
  43     protected final boolean notificationFeatureEnabled;
 
  45     protected abstract Supplier<Object> getTask(final String cmHandle,
 
  46                                                 final String resourceIdentifier,
 
  47                                                 final String optionsParamInQuery,
 
  48                                                 final String topicParamInQuery,
 
  49                                                 final String requestId,
 
  50                                                 final Boolean includeDescendant);
 
  54      * Get resource data from datastore.
 
  56      * @param cmHandleId          the cm handle
 
  57      * @param resourceIdentifier  the resource identifier
 
  58      * @param optionsParamInQuery the options param in query
 
  59      * @param topicParamInQuery   the topic param in query
 
  60      * @param includeDescendants  whether include descendants
 
  61      * @return the response entity
 
  63     public ResponseEntity<Object> getResourceData(final String cmHandleId,
 
  64                                                   final String resourceIdentifier,
 
  65                                                   final String optionsParamInQuery,
 
  66                                                   final String topicParamInQuery,
 
  67                                                   final Boolean includeDescendants) {
 
  69         final String requestId = UUID.randomUUID().toString();
 
  70         final boolean asyncResponseRequested = topicParamInQuery != null;
 
  72         if (asyncResponseRequested && notificationFeatureEnabled) {
 
  73             TopicValidator.validateTopicName(topicParamInQuery);
 
  74             log.debug("Received Async request with id {}", requestId);
 
  75             cpsNcmpTaskExecutor.executeTask(
 
  76                     getTask(cmHandleId, resourceIdentifier, optionsParamInQuery, topicParamInQuery, requestId,
 
  77                             includeDescendants), timeOutInMilliSeconds);
 
  79             return ResponseEntity.ok(Map.of("requestId", requestId));
 
  82         if (asyncResponseRequested) {
 
  83             log.warn("Asynchronous messaging is currently disabled, will use synchronous operation.");
 
  86         final Object responseObject =
 
  87                 getTask(cmHandleId, resourceIdentifier, optionsParamInQuery, NO_TOPIC, NO_REQUEST_ID,
 
  88                         includeDescendants).get();
 
  90         return ResponseEntity.ok(responseObject);