[BUG] Make failed async task report failure on Kafka topic
[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.BiConsumer;
29 import java.util.function.Supplier;
30 import org.onap.cps.ncmp.api.NetworkCmProxyDataService;
31 import org.onap.cps.ncmp.api.impl.exception.InvalidDatastoreException;
32 import org.onap.cps.ncmp.api.impl.operations.DatastoreType;
33 import org.onap.cps.ncmp.api.impl.operations.OperationType;
34 import org.onap.cps.ncmp.api.impl.utils.data.operation.ResourceDataOperationRequestUtils;
35 import org.onap.cps.ncmp.api.models.CmResourceAddress;
36 import org.onap.cps.ncmp.api.models.DataOperationRequest;
37 import org.onap.cps.ncmp.rest.exceptions.OperationNotSupportedException;
38 import org.onap.cps.ncmp.rest.exceptions.PayloadTooLargeException;
39 import org.onap.cps.ncmp.rest.executor.CpsNcmpTaskExecutor;
40 import org.onap.cps.ncmp.rest.util.TopicValidator;
41 import org.springframework.http.ResponseEntity;
42 import org.springframework.stereotype.Component;
43
44 @Component
45 public class NcmpPassthroughResourceRequestHandler extends NcmpDatastoreRequestHandler {
46
47     private final NetworkCmProxyDataService networkCmProxyDataService;
48
49     private static final Object noReturn = null;
50
51     private static final int MAXIMUM_CM_HANDLES_PER_OPERATION = 50000;
52
53     private static final String PAYLOAD_TOO_LARGE_TEMPLATE = "Operation '%s' affects too many (%d) cm handles";
54
55     /**
56      * Constructor.
57      *
58      * @param cpsNcmpTaskExecutor        @see org.onap.cps.ncmp.rest.executor.CpsNcmpTaskExecutor
59      * @param networkCmProxyDataService  @see org.onap.cps.ncmp.api.NetworkCmProxyDataService
60      */
61     public NcmpPassthroughResourceRequestHandler(final CpsNcmpTaskExecutor cpsNcmpTaskExecutor,
62                                                  final NetworkCmProxyDataService networkCmProxyDataService) {
63         super(cpsNcmpTaskExecutor);
64         this.networkCmProxyDataService = networkCmProxyDataService;
65     }
66
67     /**
68      * Executes asynchronous request for group of cm handles to resource data.
69      *
70      * @param topicParamInQuery        the topic param in query
71      * @param dataOperationRequest     data operation request details for resource data
72      * @param authorization            contents of Authorization header, or null if not present
73      * @return the response entity
74      */
75     public ResponseEntity<Object> executeRequest(final String topicParamInQuery,
76                                                  final DataOperationRequest dataOperationRequest,
77                                                  final String authorization) {
78         validateDataOperationRequest(topicParamInQuery, dataOperationRequest);
79         if (!notificationFeatureEnabled) {
80             return ResponseEntity.ok(Map.of("status",
81                 "Asynchronous request is unavailable as notification feature is currently disabled."));
82         }
83         return getRequestIdAndSendDataOperationRequestToDmiService(topicParamInQuery, dataOperationRequest,
84                 authorization);
85     }
86
87     @Override
88     protected Supplier<Object> getTaskSupplierForGetRequest(final CmResourceAddress cmResourceAddress,
89                                                             final String optionsParamInQuery,
90                                                             final String topicParamInQuery,
91                                                             final String requestId,
92                                                             final boolean includeDescendants,
93                                                             final String authorization) {
94
95         return () -> networkCmProxyDataService.getResourceDataForCmHandle(cmResourceAddress, optionsParamInQuery,
96             topicParamInQuery, requestId, authorization);
97     }
98
99     private ResponseEntity<Object> getRequestIdAndSendDataOperationRequestToDmiService(
100             final String topicParamInQuery,
101             final DataOperationRequest dataOperationRequest,
102             final String authorization) {
103         final String requestId = UUID.randomUUID().toString();
104         cpsNcmpTaskExecutor.executeTaskWithErrorHandling(
105             getTaskSupplierForDataOperationRequest(topicParamInQuery, dataOperationRequest, requestId, authorization),
106             getTaskCompletionHandlerForDataOperationRequest(topicParamInQuery, dataOperationRequest, requestId),
107             timeOutInMilliSeconds);
108         return ResponseEntity.ok(Map.of("requestId", requestId));
109     }
110
111     private void validateDataOperationRequest(final String topicParamInQuery,
112                                               final DataOperationRequest dataOperationRequest) {
113         TopicValidator.validateTopicName(topicParamInQuery);
114         dataOperationRequest.getDataOperationDefinitions().forEach(dataOperationDetail -> {
115             if (OperationType.fromOperationName(dataOperationDetail.getOperation()) != READ) {
116                 throw new OperationNotSupportedException(
117                     dataOperationDetail.getOperation() + " operation not yet supported");
118             }
119             if (DatastoreType.fromDatastoreName(dataOperationDetail.getDatastore()) == OPERATIONAL) {
120                 throw new InvalidDatastoreException(dataOperationDetail.getDatastore()
121                     + " datastore is not supported");
122             }
123             if (dataOperationDetail.getCmHandleIds().size() > MAXIMUM_CM_HANDLES_PER_OPERATION) {
124                 final String errorMessage = String.format(PAYLOAD_TOO_LARGE_TEMPLATE,
125                     dataOperationDetail.getOperationId(),
126                     dataOperationDetail.getCmHandleIds().size());
127                 throw new PayloadTooLargeException(errorMessage);
128             }
129         });
130     }
131
132     private Supplier<Object> getTaskSupplierForDataOperationRequest(final String topicParamInQuery,
133                                                                     final DataOperationRequest dataOperationRequest,
134                                                                     final String requestId,
135                                                                     final String authorization) {
136         return () -> {
137             networkCmProxyDataService.executeDataOperationForCmHandles(topicParamInQuery,
138                 dataOperationRequest,
139                 requestId,
140                 authorization);
141             return noReturn;
142         };
143     }
144
145     private static BiConsumer<Object, Throwable> getTaskCompletionHandlerForDataOperationRequest(
146             final String topicParamInQuery,
147             final DataOperationRequest dataOperationRequest,
148             final String requestId) {
149         return (result, throwable) ->
150                 ResourceDataOperationRequestUtils.handleAsyncTaskCompletionForDataOperationsRequest(topicParamInQuery,
151                         requestId, dataOperationRequest, throwable);
152     }
153
154 }