CPS-1553 :REST endpoint to accept collection of cm handles for GET operation
[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-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
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.List;
24 import java.util.Map;
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;
37
38 @NoArgsConstructor
39 @Slf4j
40 @Service
41 public class NcmpDatastoreRequestHandler implements TaskManagementDefaultHandler {
42
43     @Value("${notification.async.executor.time-out-value-in-ms:2000}")
44     protected int timeOutInMilliSeconds;
45     @Value("${notification.enabled:true}")
46     protected boolean notificationFeatureEnabled;
47     @Autowired
48     protected NetworkCmProxyDataService networkCmProxyDataService;
49     @Autowired
50     protected CpsNcmpTaskExecutor cpsNcmpTaskExecutor;
51
52     /**
53      * Executes synchronous/asynchronous request for given cm handle.
54      *
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
61      */
62     public ResponseEntity<Object> executeRequest(final String cmHandleId,
63                                                  final String resourceIdentifier,
64                                                  final String optionsParamInQuery,
65                                                  final String topicParamInQuery,
66                                                  final boolean includeDescendants) {
67
68         final boolean asyncResponseRequested = topicParamInQuery != null;
69         if (asyncResponseRequested && notificationFeatureEnabled) {
70             return executeAsyncTaskAndGetResponseEntity(cmHandleId, resourceIdentifier, optionsParamInQuery,
71                     topicParamInQuery, includeDescendants, false);
72         }
73
74         if (asyncResponseRequested) {
75             log.warn("Asynchronous request is unavailable as notification feature is currently disabled, "
76                     + "will use synchronous operation.");
77         }
78         final Supplier<Object> taskSupplier = getTaskSupplierForGetRequest(cmHandleId,
79                 resourceIdentifier, optionsParamInQuery, NO_TOPIC, NO_REQUEST_ID, includeDescendants);
80         return executeTaskSync(taskSupplier);
81     }
82
83     /**
84      * Executes a synchronous request for given cm handle.
85      * Note. Currently only ncmp-datastore:operational supports query operations.
86      *
87      * @param cmHandleId         the cm handle
88      * @param resourceIdentifier the resource identifier
89      * @param includeDescendants whether include descendants
90      * @return the response entity
91      */
92     public ResponseEntity<Object> executeRequest(final String cmHandleId,
93                                                  final String resourceIdentifier,
94                                                  final boolean includeDescendants) {
95
96         final Supplier<Object> taskSupplier = getTaskSupplierForQueryRequest(cmHandleId, resourceIdentifier,
97                 includeDescendants);
98         return executeTaskSync(taskSupplier);
99     }
100
101     /**
102      * Executes synchronous/asynchronous request for batch of cm handles.
103      *
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
110      */
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) {
116
117         return executeAsyncTaskAndGetResponseEntity(cmHandleIds, resourceIdentifier, optionsParamInQuery,
118                 topicParamInQuery, includeDescendants, true);
119
120     }
121
122     protected ResponseEntity<Object> executeTaskAsync(final String topicParamInQuery,
123                                                       final String requestId,
124                                                       final Supplier<Object> taskSupplier) {
125
126         TopicValidator.validateTopicName(topicParamInQuery);
127         log.debug("Received Async request with id {}", requestId);
128         cpsNcmpTaskExecutor.executeTask(taskSupplier, timeOutInMilliSeconds);
129
130         return ResponseEntity.ok(Map.of("requestId", requestId));
131     }
132
133     protected ResponseEntity<Object> executeTaskSync(final Supplier<Object> taskSupplier) {
134         return ResponseEntity.ok(taskSupplier.get());
135     }
136
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;
145         if (isBulkRequest) {
146             taskSupplier = getTaskSupplierForBulkRequest((List<String>) targetObject,
147                     resourceIdentifier, optionsParamInQuery, topicParamInQuery, requestId, includeDescendants);
148         } else {
149             taskSupplier = getTaskSupplierForGetRequest(targetObject.toString(), resourceIdentifier,
150                     optionsParamInQuery, topicParamInQuery, requestId, includeDescendants);
151         }
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);
155         }
156         return executeTaskAsync(topicParamInQuery, requestId, taskSupplier);
157     }
158 }