Added get APIs for dataspace.
[cps.git] / cps-ncmp-rest / src / main / java / org / onap / cps / ncmp / rest / controller / handlers / NcmpDatastoreResourceRequestHandler.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.springframework.http.ResponseEntity;
32
33 @RequiredArgsConstructor
34 @Slf4j
35 public abstract class NcmpDatastoreResourceRequestHandler {
36
37     private static final String NO_REQUEST_ID = null;
38     private static final String NO_TOPIC = null;
39
40     protected final NetworkCmProxyDataService networkCmProxyDataService;
41     protected final CpsNcmpTaskExecutor cpsNcmpTaskExecutor;
42     protected final int timeOutInMilliSeconds;
43     protected final boolean notificationFeatureEnabled;
44
45     protected abstract Supplier<Object> getTask(final String cmHandle,
46                                                 final String resourceIdentifier,
47                                                 final String optionsParamInQuery,
48                                                 final String topicParamInQuery,
49                                                 final String requestId,
50                                                 final Boolean includeDescendant);
51
52
53     /**
54      * Get resource data from 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> getResourceData(final String cmHandleId,
64                                                   final String resourceIdentifier,
65                                                   final String optionsParamInQuery,
66                                                   final String topicParamInQuery,
67                                                   final Boolean includeDescendants) {
68
69         final String requestId = UUID.randomUUID().toString();
70         final boolean asyncResponseRequested = topicParamInQuery != null;
71
72         if (asyncResponseRequested && notificationFeatureEnabled) {
73             TopicValidator.validateTopicName(topicParamInQuery);
74             log.debug("Received Async request with id {}", requestId);
75             cpsNcmpTaskExecutor.executeTask(
76                     getTask(cmHandleId, resourceIdentifier, optionsParamInQuery, topicParamInQuery, requestId,
77                             includeDescendants), timeOutInMilliSeconds);
78
79             return ResponseEntity.ok(Map.of("requestId", requestId));
80         }
81
82         if (asyncResponseRequested) {
83             log.warn("Asynchronous messaging is currently disabled, will use synchronous operation.");
84         }
85         
86         final Object responseObject =
87                 getTask(cmHandleId, resourceIdentifier, optionsParamInQuery, NO_TOPIC, NO_REQUEST_ID,
88                         includeDescendants).get();
89
90         return ResponseEntity.ok(responseObject);
91     }
92 }