Condense Liquibase steps
[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.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;
33
34 @Slf4j
35 @Service
36 @RequiredArgsConstructor
37 public abstract class NcmpDatastoreRequestHandler {
38
39     private static final String NO_REQUEST_ID = null;
40     private static final String NO_TOPIC = null;
41
42     @Value("${notification.async.executor.time-out-value-in-ms:60000}")
43     protected int timeOutInMilliSeconds;
44
45     @Value("${notification.enabled:true}")
46     protected boolean notificationFeatureEnabled;
47
48     protected final CpsNcmpTaskExecutor cpsNcmpTaskExecutor;
49
50     /**
51      * Executes synchronous/asynchronous get request for given cm handle.
52      *
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      * @param authorization       contents of Authorization header, or null if not present
60      * @return the response entity
61      */
62     public ResponseEntity<Object> executeRequest(final String datastoreName,
63                                                  final String cmHandleId,
64                                                  final String resourceIdentifier,
65                                                  final String optionsParamInQuery,
66                                                  final String topicParamInQuery,
67                                                  final boolean includeDescendants,
68                                                  final String authorization) {
69
70         final boolean asyncResponseRequested = topicParamInQuery != null;
71         if (asyncResponseRequested && notificationFeatureEnabled) {
72             return executeAsyncTaskAndGetResponseEntity(datastoreName, cmHandleId, resourceIdentifier,
73                 optionsParamInQuery, topicParamInQuery, includeDescendants, authorization);
74         }
75
76         if (asyncResponseRequested) {
77             log.warn("Asynchronous request is unavailable as notification feature is currently disabled, "
78                     + "will use synchronous operation.");
79         }
80         final Supplier<Object> taskSupplier = getTaskSupplierForGetRequest(datastoreName, cmHandleId,
81                 resourceIdentifier, optionsParamInQuery, NO_TOPIC, NO_REQUEST_ID, includeDescendants, authorization);
82         return executeTaskSync(taskSupplier);
83     }
84
85
86     private ResponseEntity<Object> executeTaskAsync(final String topicParamInQuery,
87                                                       final String requestId,
88                                                       final Supplier<Object> taskSupplier) {
89         TopicValidator.validateTopicName(topicParamInQuery);
90         log.debug("Received Async request with id {}", requestId);
91         cpsNcmpTaskExecutor.executeTask(taskSupplier, timeOutInMilliSeconds);
92         return ResponseEntity.ok(Map.of("requestId", requestId));
93     }
94
95     protected ResponseEntity<Object> executeTaskSync(final Supplier<Object> taskSupplier) {
96         return ResponseEntity.ok(taskSupplier.get());
97     }
98
99     private ResponseEntity<Object> executeAsyncTaskAndGetResponseEntity(final String datastoreName,
100                                                                         final String cmHandleId,
101                                                                         final String resourceIdentifier,
102                                                                         final String optionsParamInQuery,
103                                                                         final String topicParamInQuery,
104                                                                         final boolean includeDescendants,
105                                                                         final String authorization) {
106         final String requestId = UUID.randomUUID().toString();
107         final Supplier<Object> taskSupplier = getTaskSupplierForGetRequest(datastoreName, cmHandleId,
108                 resourceIdentifier, optionsParamInQuery, topicParamInQuery, requestId, includeDescendants,
109                 authorization);
110         return executeTaskAsync(topicParamInQuery, requestId, taskSupplier);
111     }
112
113     protected abstract Supplier<Object> getTaskSupplierForGetRequest(final String datastoreName,
114                                                   final String cmHandleId,
115                                                   final String resourceIdentifier,
116                                                   final String optionsParamInQuery,
117                                                   final String topicParamInQuery,
118                                                   final String requestId,
119                                                   final boolean includeDescendant,
120                                                   final String authorization);
121
122 }