Merge "Save new cm notification subscription"
[cps.git] / cps-ncmp-rest / src / main / java / org / onap / cps / ncmp / rest / controller / handlers / NcmpDatastoreRequestHandler.java
1 /*
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
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.ncmp.rest.controller.handlers;
22
23 import java.util.Map;
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.models.CmResourceAddress;
29 import org.onap.cps.ncmp.rest.executor.CpsNcmpTaskExecutor;
30 import org.onap.cps.ncmp.rest.util.TopicValidator;
31 import org.springframework.beans.factory.annotation.Value;
32 import org.springframework.http.ResponseEntity;
33 import org.springframework.stereotype.Service;
34
35 @Slf4j
36 @Service
37 @RequiredArgsConstructor
38 public abstract class NcmpDatastoreRequestHandler {
39
40     private static final String NO_REQUEST_ID = null;
41     private static final String NO_TOPIC = null;
42
43     @Value("${notification.async.executor.time-out-value-in-ms:60000}")
44     protected int timeOutInMilliSeconds;
45
46     @Value("${notification.enabled:true}")
47     protected boolean notificationFeatureEnabled;
48
49     protected final CpsNcmpTaskExecutor cpsNcmpTaskExecutor;
50
51     /**
52      * Executes synchronous/asynchronous get request for given cm handle.
53      *
54      * @param cmResourceAddress   the name of the datastore, cm handle and resource identifier
55      * @param optionsParamInQuery the options param in query
56      * @param topicParamInQuery   the topic param in query
57      * @param includeDescendants  whether include descendants
58      * @param authorization       contents of Authorization header, or null if not present
59      * @return the response entity
60      */
61     public ResponseEntity<Object> executeRequest(final CmResourceAddress cmResourceAddress,
62                                                  final String optionsParamInQuery,
63                                                  final String topicParamInQuery,
64                                                  final boolean includeDescendants,
65                                                  final String authorization) {
66
67         final boolean asyncResponseRequested = topicParamInQuery != null;
68         if (asyncResponseRequested && notificationFeatureEnabled) {
69             return executeAsyncTaskAndGetResponseEntity(cmResourceAddress, optionsParamInQuery, topicParamInQuery,
70                 includeDescendants, authorization);
71         }
72
73         if (asyncResponseRequested) {
74             log.warn("Asynchronous request is unavailable as notification feature is currently disabled, "
75                     + "will use synchronous operation.");
76         }
77         final Supplier<Object> taskSupplier = getTaskSupplierForGetRequest(cmResourceAddress, optionsParamInQuery,
78             NO_TOPIC, NO_REQUEST_ID, includeDescendants, authorization);
79         return executeTaskSync(taskSupplier);
80     }
81
82
83     private ResponseEntity<Object> executeTaskAsync(final String topicParamInQuery,
84                                                       final String requestId,
85                                                       final Supplier<Object> taskSupplier) {
86         TopicValidator.validateTopicName(topicParamInQuery);
87         log.debug("Received Async request with id {}", requestId);
88         cpsNcmpTaskExecutor.executeTask(taskSupplier, timeOutInMilliSeconds);
89         return ResponseEntity.ok(Map.of("requestId", requestId));
90     }
91
92     protected ResponseEntity<Object> executeTaskSync(final Supplier<Object> taskSupplier) {
93         return ResponseEntity.ok(taskSupplier.get());
94     }
95
96     private ResponseEntity<Object> executeAsyncTaskAndGetResponseEntity(final CmResourceAddress cmResourceAddress,
97                                                                         final String optionsParamInQuery,
98                                                                         final String topicParamInQuery,
99                                                                         final boolean includeDescendants,
100                                                                         final String authorization) {
101         final String requestId = UUID.randomUUID().toString();
102         final Supplier<Object> taskSupplier = getTaskSupplierForGetRequest(cmResourceAddress,
103             optionsParamInQuery, topicParamInQuery, requestId, includeDescendants, authorization);
104         return executeTaskAsync(topicParamInQuery, requestId, taskSupplier);
105     }
106
107     protected abstract Supplier<Object> getTaskSupplierForGetRequest(final CmResourceAddress cmResourceAddress,
108                                                   final String optionsParamInQuery,
109                                                   final String topicParamInQuery,
110                                                   final String requestId,
111                                                   final boolean includeDescendant,
112                                                   final String authorization);
113
114 }