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