Merge "Added get APIs for dataspace."
[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 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.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;
33
34 @RequiredArgsConstructor
35 @Slf4j
36 public abstract class NcmpDatastoreRequestHandler {
37
38     private static final String NO_REQUEST_ID = null;
39     private static final String NO_TOPIC = null;
40
41     protected final NetworkCmProxyDataService networkCmProxyDataService;
42     protected final CpsNcmpTaskExecutor cpsNcmpTaskExecutor;
43     protected final int timeOutInMilliSeconds;
44     protected final boolean notificationFeatureEnabled;
45
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);
52
53     /**
54      * Execute a request on a datastore.
55      *
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
62      */
63     public ResponseEntity<Object> executeRequest(final String cmHandleId,
64                                                  final String resourceIdentifier,
65                                                  final String optionsParamInQuery,
66                                                  final String topicParamInQuery,
67                                                  final Boolean includeDescendants) {
68
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);
75         }
76
77         if (asyncResponseRequested) {
78             log.warn("Asynchronous request is unavailable as notification feature is currently disabled, "
79                 + "will use synchronous operation.");
80         }
81         final Supplier<Object> taskSupplier = getTaskSupplier(cmHandleId, resourceIdentifier, optionsParamInQuery,
82             NO_TOPIC, NO_REQUEST_ID, includeDescendants);
83         return executeTaskSync(taskSupplier);
84     }
85
86     protected ResponseEntity<Object> executeTaskAsync(final String topicParamInQuery,
87                                                       final String requestId,
88                                                       final Supplier<Object> taskSupplier) {
89
90         TopicValidator.validateTopicName(topicParamInQuery);
91         log.debug("Received Async request with id {}", requestId);
92         cpsNcmpTaskExecutor.executeTask(taskSupplier, timeOutInMilliSeconds);
93
94         return ResponseEntity.ok(Map.of("requestId", requestId));
95     }
96
97     protected ResponseEntity<Object> executeTaskSync(final Supplier<Object> taskSupplier) {
98         return ResponseEntity.ok(taskSupplier.get());
99     }
100
101     protected static FetchDescendantsOption getFetchDescendantsOption(final Boolean includeDescendant) {
102         return Boolean.TRUE.equals(includeDescendant) ? FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS
103             : FetchDescendantsOption.OMIT_DESCENDANTS;
104     }
105
106 }