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