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