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