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