a21210c376768ec7c91907581792c8937db6700b
[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.impl.data;
22
23 import static org.onap.cps.ncmp.api.data.models.DatastoreType.OPERATIONAL;
24 import static org.onap.cps.ncmp.api.data.models.OperationType.READ;
25
26 import java.util.Map;
27 import java.util.UUID;
28 import lombok.RequiredArgsConstructor;
29 import org.onap.cps.ncmp.api.data.exceptions.InvalidDatastoreException;
30 import org.onap.cps.ncmp.api.data.exceptions.OperationNotSupportedException;
31 import org.onap.cps.ncmp.api.data.models.CmResourceAddress;
32 import org.onap.cps.ncmp.api.data.models.DataOperationRequest;
33 import org.onap.cps.ncmp.api.data.models.DatastoreType;
34 import org.onap.cps.ncmp.api.data.models.OperationType;
35 import org.onap.cps.ncmp.api.exceptions.PayloadTooLargeException;
36 import org.onap.cps.ncmp.utils.events.TopicValidator;
37 import org.springframework.stereotype.Service;
38 import reactor.core.publisher.Mono;
39
40 @Service
41 @RequiredArgsConstructor
42 public class NcmpPassthroughResourceRequestHandler extends NcmpDatastoreRequestHandler {
43
44     private final DmiDataOperations dmiDataOperations;
45
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      * Executes asynchronous request for group of cm handles to resource data.
51      *
52      * @param topic                 the topic param in query
53      * @param dataOperationRequest  data operation request details for resource data
54      * @param authorization         contents of Authorization header, or null if not present
55      * @return a map with one entry of request Id for success or status and error when async feature is disabled
56      */
57     public Map<String, String> executeAsynchronousRequest(final String topic,
58                                                           final DataOperationRequest dataOperationRequest,
59                                                           final String authorization) {
60         validateDataOperationRequest(topic, dataOperationRequest);
61         if (!notificationFeatureEnabled) {
62             return Map.of("status",
63                 "Asynchronous request is unavailable as notification feature is currently disabled.");
64         }
65         final String requestId = UUID.randomUUID().toString();
66         dmiDataOperations.requestResourceDataFromDmi(topic, dataOperationRequest, requestId, authorization);
67         return Map.of("requestId", requestId);
68     }
69
70     @Override
71     protected Mono<Object> getResourceDataForCmHandle(final CmResourceAddress cmResourceAddress,
72                                                       final String options,
73                                                       final String topic,
74                                                       final String requestId,
75                                                       final boolean includeDescendants,
76                                                       final String authorization) {
77
78         return dmiDataOperations.getResourceDataFromDmi(cmResourceAddress, options, topic, requestId, authorization)
79             .flatMap(responseEntity -> Mono.justOrEmpty(responseEntity.getBody()));
80     }
81
82     private void validateDataOperationRequest(final String topicParamInQuery,
83                                               final DataOperationRequest dataOperationRequest) {
84         TopicValidator.validateTopicName(topicParamInQuery);
85         dataOperationRequest.getDataOperationDefinitions().forEach(dataOperationDefinition -> {
86             if (OperationType.fromOperationName(dataOperationDefinition.getOperation()) != READ) {
87                 throw new OperationNotSupportedException(
88                         dataOperationDefinition.getOperation() + " operation not yet supported");
89             }
90             if (DatastoreType.fromDatastoreName(dataOperationDefinition.getDatastore()) == OPERATIONAL) {
91                 throw new InvalidDatastoreException(dataOperationDefinition.getDatastore()
92                         + " datastore is not supported");
93             }
94             if (dataOperationDefinition.getCmHandleIds().size() > MAXIMUM_CM_HANDLES_PER_OPERATION) {
95                 final String errorMessage = String.format(PAYLOAD_TOO_LARGE_TEMPLATE,
96                         dataOperationDefinition.getOperationId(),
97                         dataOperationDefinition.getCmHandleIds().size());
98                 throw new PayloadTooLargeException(errorMessage);
99             }
100         });
101     }
102 }