2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022-2024 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 lombok.RequiredArgsConstructor;
26 import lombok.extern.slf4j.Slf4j;
27 import org.onap.cps.ncmp.api.models.CmResourceAddress;
28 import org.onap.cps.ncmp.rest.util.TopicValidator;
29 import org.springframework.beans.factory.annotation.Value;
30 import org.springframework.http.ResponseEntity;
31 import org.springframework.stereotype.Service;
32 import reactor.core.publisher.Mono;
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:60000}")
43 protected int timeOutInMilliSeconds;
44 @Value("${notification.enabled:true}")
45 protected boolean notificationFeatureEnabled;
48 * Executes synchronous/asynchronous get request for given cm handle.
50 * @param cmResourceAddress the name of the datastore, cm handle and resource identifier
51 * @param optionsParamInQuery the options param in query
52 * @param topicParamInQuery the topic param in query
53 * @param includeDescendants whether include descendants
54 * @param authorization contents of Authorization header, or null if not present
55 * @return the response entity
57 public ResponseEntity<Object> executeRequest(final CmResourceAddress cmResourceAddress,
58 final String optionsParamInQuery,
59 final String topicParamInQuery,
60 final boolean includeDescendants,
61 final String authorization) {
63 final boolean asyncResponseRequested = topicParamInQuery != null;
64 if (asyncResponseRequested && notificationFeatureEnabled) {
65 return fetchResourceDataAsynchronously(cmResourceAddress, optionsParamInQuery, topicParamInQuery,
66 includeDescendants, authorization);
69 if (asyncResponseRequested) {
70 log.warn("Asynchronous request is unavailable as notification feature is currently disabled, "
71 + "will use synchronous operation.");
73 final Mono<Object> resourceDataMono = getResourceDataForCmHandle(cmResourceAddress, optionsParamInQuery,
74 NO_TOPIC, NO_REQUEST_ID, includeDescendants, authorization);
75 return fetchResourceDataSynchronously(resourceDataMono);
78 private ResponseEntity<Object> fetchResourceDataSynchronously(final Mono<Object> resourceDataMono) {
79 return ResponseEntity.ok(resourceDataMono.block());
82 private ResponseEntity<Object> fetchResourceDataAsynchronously(final CmResourceAddress cmResourceAddress,
83 final String optionsParamInQuery,
84 final String topicParamInQuery,
85 final boolean includeDescendants,
86 final String authorization) {
87 TopicValidator.validateTopicName(topicParamInQuery);
88 final String requestId = UUID.randomUUID().toString();
89 getResourceDataForCmHandle(cmResourceAddress, optionsParamInQuery, topicParamInQuery, requestId,
90 includeDescendants, authorization)
91 .doOnSuccess(result -> log.debug("Async operation succeeded for request id {}: {}", requestId, result))
93 log.error("Async operation failed for request id {}: {}", requestId, error.getMessage()))
95 log.debug("Received Async request with id {}", requestId);
96 return ResponseEntity.ok(Map.of("requestId", requestId));
99 protected abstract Mono<Object> getResourceDataForCmHandle(final CmResourceAddress cmResourceAddress,
100 final String optionsParamInQuery,
101 final String topicParamInQuery,
102 final String requestId,
103 final boolean includeDescendant,
104 final String authorization);