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;
23 import java.util.List;
25 import java.util.UUID;
26 import java.util.function.Supplier;
27 import lombok.NoArgsConstructor;
28 import lombok.extern.slf4j.Slf4j;
29 import org.onap.cps.ncmp.api.NetworkCmProxyDataService;
30 import org.onap.cps.ncmp.rest.executor.CpsNcmpTaskExecutor;
31 import org.onap.cps.ncmp.rest.util.TopicValidator;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.beans.factory.annotation.Value;
34 import org.springframework.http.HttpStatus;
35 import org.springframework.http.ResponseEntity;
36 import org.springframework.stereotype.Service;
41 public class NcmpDatastoreRequestHandler implements TaskManagementDefaultHandler {
43 @Value("${notification.async.executor.time-out-value-in-ms:2000}")
44 protected int timeOutInMilliSeconds;
45 @Value("${notification.enabled:true}")
46 protected boolean notificationFeatureEnabled;
48 protected NetworkCmProxyDataService networkCmProxyDataService;
50 protected CpsNcmpTaskExecutor cpsNcmpTaskExecutor;
53 * Executes synchronous/asynchronous request for given cm handle.
55 * @param cmHandleId the cm handle
56 * @param resourceIdentifier the resource identifier
57 * @param optionsParamInQuery the options param in query
58 * @param topicParamInQuery the topic param in query
59 * @param includeDescendants whether include descendants
60 * @return the response entity
62 public ResponseEntity<Object> executeRequest(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(cmHandleId, resourceIdentifier, optionsParamInQuery,
71 topicParamInQuery, includeDescendants, false);
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(cmHandleId,
79 resourceIdentifier, optionsParamInQuery, NO_TOPIC, NO_REQUEST_ID, includeDescendants);
80 return executeTaskSync(taskSupplier);
84 * Executes a synchronous request for given cm handle.
85 * Note. Currently only ncmp-datastore:operational supports query operations.
87 * @param cmHandleId the cm handle
88 * @param resourceIdentifier the resource identifier
89 * @param includeDescendants whether include descendants
90 * @return the response entity
92 public ResponseEntity<Object> executeRequest(final String cmHandleId,
93 final String resourceIdentifier,
94 final boolean includeDescendants) {
96 final Supplier<Object> taskSupplier = getTaskSupplierForQueryRequest(cmHandleId, resourceIdentifier,
98 return executeTaskSync(taskSupplier);
102 * Executes synchronous/asynchronous request for batch of cm handles.
104 * @param cmHandleIds list of cm handles
105 * @param resourceIdentifier the resource identifier
106 * @param optionsParamInQuery the options param in query
107 * @param topicParamInQuery the topic param in query
108 * @param includeDescendants whether include descendants
109 * @return the response entity
111 public ResponseEntity<Object> executeRequest(final List<String> cmHandleIds,
112 final String resourceIdentifier,
113 final String optionsParamInQuery,
114 final String topicParamInQuery,
115 final boolean includeDescendants) {
117 return executeAsyncTaskAndGetResponseEntity(cmHandleIds, resourceIdentifier, optionsParamInQuery,
118 topicParamInQuery, includeDescendants, true);
122 protected ResponseEntity<Object> executeTaskAsync(final String topicParamInQuery,
123 final String requestId,
124 final Supplier<Object> taskSupplier) {
126 TopicValidator.validateTopicName(topicParamInQuery);
127 log.debug("Received Async request with id {}", requestId);
128 cpsNcmpTaskExecutor.executeTask(taskSupplier, timeOutInMilliSeconds);
130 return ResponseEntity.ok(Map.of("requestId", requestId));
133 protected ResponseEntity<Object> executeTaskSync(final Supplier<Object> taskSupplier) {
134 return ResponseEntity.ok(taskSupplier.get());
137 private ResponseEntity<Object> executeAsyncTaskAndGetResponseEntity(final Object targetObject,
138 final String resourceIdentifier,
139 final String optionsParamInQuery,
140 final String topicParamInQuery,
141 final boolean includeDescendants,
142 final boolean isBulkRequest) {
143 final String requestId = UUID.randomUUID().toString();
144 final Supplier<Object> taskSupplier;
146 taskSupplier = getTaskSupplierForBulkRequest((List<String>) targetObject,
147 resourceIdentifier, optionsParamInQuery, topicParamInQuery, requestId, includeDescendants);
149 taskSupplier = getTaskSupplierForGetRequest(targetObject.toString(), resourceIdentifier,
150 optionsParamInQuery, topicParamInQuery, requestId, includeDescendants);
152 if (taskSupplier == NO_OBJECT_SUPPLIER) {
153 return new ResponseEntity<>(Map.of("status", "Unable to execute request as "
154 + "datastore is not implemented."), HttpStatus.NOT_IMPLEMENTED);
156 return executeTaskAsync(topicParamInQuery, requestId, taskSupplier);