2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022-2023 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.rest.executor.CpsNcmpTaskExecutor;
29 import org.onap.cps.ncmp.rest.util.TopicValidator;
30 import org.springframework.beans.factory.annotation.Value;
31 import org.springframework.http.ResponseEntity;
32 import org.springframework.stereotype.Service;
36 @RequiredArgsConstructor
37 public abstract class NcmpDatastoreRequestHandler {
39 private static final String NO_REQUEST_ID = null;
40 private static final String NO_TOPIC = null;
42 @Value("${notification.async.executor.time-out-value-in-ms:2000}")
43 protected int timeOutInMilliSeconds;
45 @Value("${notification.enabled:true}")
46 protected boolean notificationFeatureEnabled;
48 protected final CpsNcmpTaskExecutor cpsNcmpTaskExecutor;
51 * Executes synchronous/asynchronous get request for given cm handle.
53 * @param datastoreName the name of the datastore
54 * @param cmHandleId the cm handle
55 * @param resourceIdentifier the resource identifier
56 * @param optionsParamInQuery the options param in query
57 * @param topicParamInQuery the topic param in query
58 * @param includeDescendants whether include descendants
59 * @return the response entity
61 public ResponseEntity<Object> executeRequest(final String datastoreName,
62 final String cmHandleId,
63 final String resourceIdentifier,
64 final String optionsParamInQuery,
65 final String topicParamInQuery,
66 final boolean includeDescendants) {
68 final boolean asyncResponseRequested = topicParamInQuery != null;
69 if (asyncResponseRequested && notificationFeatureEnabled) {
70 return executeAsyncTaskAndGetResponseEntity(datastoreName, cmHandleId, resourceIdentifier,
71 optionsParamInQuery, topicParamInQuery, includeDescendants);
74 if (asyncResponseRequested) {
75 log.warn("Asynchronous request is unavailable as notification feature is currently disabled, "
76 + "will use synchronous operation.");
78 final Supplier<Object> taskSupplier = getTaskSupplierForGetRequest(datastoreName, cmHandleId,
79 resourceIdentifier, optionsParamInQuery, NO_TOPIC, NO_REQUEST_ID, includeDescendants);
80 return executeTaskSync(taskSupplier);
84 private ResponseEntity<Object> executeTaskAsync(final String topicParamInQuery,
85 final String requestId,
86 final Supplier<Object> taskSupplier) {
87 TopicValidator.validateTopicName(topicParamInQuery);
88 log.debug("Received Async request with id {}", requestId);
89 cpsNcmpTaskExecutor.executeTask(taskSupplier, timeOutInMilliSeconds);
90 return ResponseEntity.ok(Map.of("requestId", requestId));
93 protected ResponseEntity<Object> executeTaskSync(final Supplier<Object> taskSupplier) {
94 return ResponseEntity.ok(taskSupplier.get());
97 private ResponseEntity<Object> executeAsyncTaskAndGetResponseEntity(final String datastoreName,
98 final String cmHandleId,
99 final String resourceIdentifier,
100 final String optionsParamInQuery,
101 final String topicParamInQuery,
102 final boolean includeDescendants) {
103 final String requestId = UUID.randomUUID().toString();
104 final Supplier<Object> taskSupplier = getTaskSupplierForGetRequest(datastoreName, cmHandleId,
105 resourceIdentifier, optionsParamInQuery, topicParamInQuery, requestId, includeDescendants);
106 return executeTaskAsync(topicParamInQuery, requestId, taskSupplier);
109 protected abstract Supplier<Object> getTaskSupplierForGetRequest(final String datastoreName,
110 final String cmHandleId,
111 final String resourceIdentifier,
112 final String optionsParamInQuery,
113 final String topicParamInQuery,
114 final String requestId,
115 final boolean includeDescendant);