Merge "Add kafka messaging support to integration test module"
[cps.git] / cps-ncmp-rest / src / main / java / org / onap / cps / ncmp / rest / controller / handlers / NcmpPassthroughResourceRequestHandler.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 static org.onap.cps.ncmp.api.impl.operations.DatastoreType.OPERATIONAL;
24 import static org.onap.cps.ncmp.api.impl.operations.OperationType.READ;
25
26 import java.util.Map;
27 import java.util.UUID;
28 import java.util.function.Supplier;
29 import org.onap.cps.ncmp.api.NetworkCmProxyDataService;
30 import org.onap.cps.ncmp.api.impl.exception.InvalidDatastoreException;
31 import org.onap.cps.ncmp.api.impl.operations.DatastoreType;
32 import org.onap.cps.ncmp.api.impl.operations.OperationType;
33 import org.onap.cps.ncmp.api.models.CmResourceAddress;
34 import org.onap.cps.ncmp.api.models.DataOperationRequest;
35 import org.onap.cps.ncmp.rest.exceptions.OperationNotSupportedException;
36 import org.onap.cps.ncmp.rest.executor.CpsNcmpTaskExecutor;
37 import org.onap.cps.ncmp.rest.util.TopicValidator;
38 import org.springframework.http.ResponseEntity;
39 import org.springframework.stereotype.Component;
40
41 @Component
42 public class NcmpPassthroughResourceRequestHandler extends NcmpDatastoreRequestHandler {
43
44     private final NetworkCmProxyDataService networkCmProxyDataService;
45
46     private static final Object noReturn = null;
47
48     /**
49      * Constructor.
50      *
51      * @param cpsNcmpTaskExecutor        @see org.onap.cps.ncmp.rest.executor.CpsNcmpTaskExecutor
52      * @param networkCmProxyDataService  @see org.onap.cps.ncmp.api.NetworkCmProxyDataService
53      */
54     public NcmpPassthroughResourceRequestHandler(final CpsNcmpTaskExecutor cpsNcmpTaskExecutor,
55                                                  final NetworkCmProxyDataService networkCmProxyDataService) {
56         super(cpsNcmpTaskExecutor);
57         this.networkCmProxyDataService = networkCmProxyDataService;
58     }
59
60     /**
61      * Executes asynchronous request for group of cm handles to resource data.
62      *
63      * @param topicParamInQuery        the topic param in query
64      * @param dataOperationRequest     data operation request details for resource data
65      * @param authorization            contents of Authorization header, or null if not present
66      * @return the response entity
67      */
68     public ResponseEntity<Object> executeRequest(final String topicParamInQuery,
69                                                  final DataOperationRequest dataOperationRequest,
70                                                  final String authorization) {
71         validateDataOperationRequest(topicParamInQuery, dataOperationRequest);
72         if (!notificationFeatureEnabled) {
73             return ResponseEntity.ok(Map.of("status",
74                 "Asynchronous request is unavailable as notification feature is currently disabled."));
75         }
76         return getRequestIdAndSendDataOperationRequestToDmiService(topicParamInQuery, dataOperationRequest,
77                 authorization);
78     }
79
80     @Override
81     protected Supplier<Object> getTaskSupplierForGetRequest(final CmResourceAddress cmResourceAddress,
82                                                             final String optionsParamInQuery,
83                                                             final String topicParamInQuery,
84                                                             final String requestId,
85                                                             final boolean includeDescendants,
86                                                             final String authorization) {
87
88         return () -> networkCmProxyDataService.getResourceDataForCmHandle(cmResourceAddress, optionsParamInQuery,
89             topicParamInQuery, requestId, authorization);
90     }
91
92     private ResponseEntity<Object> getRequestIdAndSendDataOperationRequestToDmiService(
93             final String topicParamInQuery,
94             final DataOperationRequest dataOperationRequest,
95             final String authorization) {
96         final String requestId = UUID.randomUUID().toString();
97         cpsNcmpTaskExecutor.executeTask(
98             getTaskSupplierForDataOperationRequest(topicParamInQuery, dataOperationRequest, requestId, authorization),
99             timeOutInMilliSeconds);
100         return ResponseEntity.ok(Map.of("requestId", requestId));
101     }
102
103     private void validateDataOperationRequest(final String topicParamInQuery,
104                                               final DataOperationRequest
105                                                   dataOperationRequest) {
106         TopicValidator.validateTopicName(topicParamInQuery);
107         dataOperationRequest.getDataOperationDefinitions().forEach(dataOperationDetail -> {
108             if (OperationType.fromOperationName(dataOperationDetail.getOperation()) != READ) {
109                 throw new OperationNotSupportedException(
110                     dataOperationDetail.getOperation() + " operation not yet supported");
111             } else if (DatastoreType.fromDatastoreName(dataOperationDetail.getDatastore()) == OPERATIONAL) {
112                 throw new InvalidDatastoreException(dataOperationDetail.getDatastore()
113                     + " datastore is not supported");
114             }
115         });
116     }
117
118     private Supplier<Object> getTaskSupplierForDataOperationRequest(final String topicParamInQuery,
119                                                                     final DataOperationRequest dataOperationRequest,
120                                                                     final String requestId,
121                                                                     final String authorization) {
122         return () -> {
123             networkCmProxyDataService.executeDataOperationForCmHandles(topicParamInQuery,
124                 dataOperationRequest,
125                 requestId,
126                 authorization);
127             return noReturn;
128         };
129     }
130
131 }