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.onap.cps.spi.FetchDescendantsOption;
32 import org.springframework.http.ResponseEntity;
34 @RequiredArgsConstructor
36 public abstract class NcmpDatastoreRequestHandler {
38 private static final String NO_REQUEST_ID = null;
39 private static final String NO_TOPIC = null;
41 protected final NetworkCmProxyDataService networkCmProxyDataService;
42 protected final CpsNcmpTaskExecutor cpsNcmpTaskExecutor;
43 protected final int timeOutInMilliSeconds;
44 protected final boolean notificationFeatureEnabled;
46 protected abstract Supplier<Object> getTaskSupplier(final String cmHandle,
47 final String resourceIdentifier,
48 final String optionsParamInQuery,
49 final String topicParamInQuery,
50 final String requestId,
51 final Boolean includeDescendant);
54 * Execute a request on a 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> executeRequest(final String cmHandleId,
64 final String resourceIdentifier,
65 final String optionsParamInQuery,
66 final String topicParamInQuery,
67 final Boolean includeDescendants) {
69 final boolean asyncResponseRequested = topicParamInQuery != null;
70 if (asyncResponseRequested && notificationFeatureEnabled) {
71 final String requestId = UUID.randomUUID().toString();
72 final Supplier<Object> taskSupplier = getTaskSupplier(cmHandleId, resourceIdentifier, optionsParamInQuery,
73 topicParamInQuery, requestId, includeDescendants);
74 return executeTaskAsync(topicParamInQuery, requestId, taskSupplier);
77 if (asyncResponseRequested) {
78 log.warn("Asynchronous request is unavailable as notification feature is currently disabled, "
79 + "will use synchronous operation.");
81 final Supplier<Object> taskSupplier = getTaskSupplier(cmHandleId, resourceIdentifier, optionsParamInQuery,
82 NO_TOPIC, NO_REQUEST_ID, includeDescendants);
83 return executeTaskSync(taskSupplier);
86 protected ResponseEntity<Object> executeTaskAsync(final String topicParamInQuery,
87 final String requestId,
88 final Supplier<Object> taskSupplier) {
90 TopicValidator.validateTopicName(topicParamInQuery);
91 log.debug("Received Async request with id {}", requestId);
92 cpsNcmpTaskExecutor.executeTask(taskSupplier, timeOutInMilliSeconds);
94 return ResponseEntity.ok(Map.of("requestId", requestId));
97 protected ResponseEntity<Object> executeTaskSync(final Supplier<Object> taskSupplier) {
98 return ResponseEntity.ok(taskSupplier.get());
101 protected static FetchDescendantsOption getFetchDescendantsOption(final Boolean includeDescendant) {
102 return Boolean.TRUE.equals(includeDescendant) ? FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS
103 : FetchDescendantsOption.OMIT_DESCENDANTS;