Save new cm notification subscription
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / api / impl / operations / DmiDataOperations.java
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2021-2024 Nordix Foundation
4  *  Modifications Copyright (C) 2022 Bell Canada
5  *  ================================================================================
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 package org.onap.cps.ncmp.api.impl.operations;
23
24 import static org.onap.cps.ncmp.api.NcmpResponseStatus.DMI_SERVICE_NOT_RESPONDING;
25 import static org.onap.cps.ncmp.api.NcmpResponseStatus.UNABLE_TO_READ_RESOURCE_DATA;
26 import static org.onap.cps.ncmp.api.impl.operations.DatastoreType.PASSTHROUGH_RUNNING;
27 import static org.onap.cps.ncmp.api.impl.operations.OperationType.READ;
28
29 import io.micrometer.core.annotation.Timed;
30 import java.util.Collection;
31 import java.util.List;
32 import java.util.Map;
33 import java.util.Set;
34 import java.util.stream.Collectors;
35 import lombok.extern.slf4j.Slf4j;
36 import org.onap.cps.ncmp.api.NcmpResponseStatus;
37 import org.onap.cps.ncmp.api.impl.client.DmiRestClient;
38 import org.onap.cps.ncmp.api.impl.config.NcmpConfiguration;
39 import org.onap.cps.ncmp.api.impl.exception.HttpClientRequestException;
40 import org.onap.cps.ncmp.api.impl.inventory.CmHandleState;
41 import org.onap.cps.ncmp.api.impl.inventory.InventoryPersistence;
42 import org.onap.cps.ncmp.api.impl.utils.DmiServiceUrlBuilder;
43 import org.onap.cps.ncmp.api.impl.utils.data.operation.ResourceDataOperationRequestUtils;
44 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle;
45 import org.onap.cps.ncmp.api.models.DataOperationRequest;
46 import org.onap.cps.spi.exceptions.CpsException;
47 import org.onap.cps.utils.JsonObjectMapper;
48 import org.springframework.http.ResponseEntity;
49 import org.springframework.stereotype.Component;
50 import org.springframework.util.LinkedMultiValueMap;
51 import org.springframework.util.MultiValueMap;
52 import org.springframework.web.util.UriComponentsBuilder;
53
54 /**
55  * Operations class for DMI data.
56  */
57 @Component
58 @Slf4j
59 public class DmiDataOperations extends DmiOperations {
60
61     public DmiDataOperations(final InventoryPersistence inventoryPersistence,
62                              final JsonObjectMapper jsonObjectMapper,
63                              final NcmpConfiguration.DmiProperties dmiProperties,
64                              final DmiRestClient dmiRestClient,
65                              final DmiServiceUrlBuilder dmiServiceUrlBuilder) {
66         super(inventoryPersistence, jsonObjectMapper, dmiProperties, dmiRestClient, dmiServiceUrlBuilder);
67     }
68
69     /**
70      * This method fetches the resource data from operational data store for given cm handle
71      * identifier on given resource using dmi client.
72      *
73      * @param dataStoreName       name of data store
74      * @param cmHandleId          network resource identifier
75      * @param resourceId          resource identifier
76      * @param optionsParamInQuery options query
77      * @param topicParamInQuery   topic name for (triggering) async responses
78      * @param requestId           requestId for async responses
79      * @param authorization       contents of Authorization header, or null if not present
80      * @return {@code ResponseEntity} response entity
81      */
82     @Timed(value = "cps.ncmp.dmi.get",
83             description = "Time taken to fetch the resource data from operational data store for given cm handle "
84                     + "identifier on given resource using dmi client")
85     public ResponseEntity<Object> getResourceDataFromDmi(final String dataStoreName,
86                                                          final String cmHandleId,
87                                                          final String resourceId,
88                                                          final String optionsParamInQuery,
89                                                          final String topicParamInQuery,
90                                                          final String requestId,
91                                                          final String authorization) {
92         final YangModelCmHandle yangModelCmHandle = getYangModelCmHandle(cmHandleId);
93         final CmHandleState cmHandleState = yangModelCmHandle.getCompositeState().getCmHandleState();
94         validateIfCmHandleStateReady(yangModelCmHandle, cmHandleState);
95         final String jsonRequestBody = getDmiRequestBody(READ, requestId, null, null,
96                 yangModelCmHandle);
97         final String dmiResourceDataUrl = getDmiRequestUrl(dataStoreName, cmHandleId, resourceId, optionsParamInQuery,
98                 topicParamInQuery, yangModelCmHandle.resolveDmiServiceName(RequiredDmiService.DATA));
99         return dmiRestClient.postOperationWithJsonData(dmiResourceDataUrl, jsonRequestBody, READ, authorization);
100     }
101
102     /**
103      * This method fetches all the resource data from operational data store for given cm handle
104      * identifier using dmi client.
105      *
106      * @param dataStoreName data store name
107      * @param cmHandleId    network resource identifier
108      * @param requestId     requestId for async responses
109      * @return {@code ResponseEntity} response entity
110      */
111     public ResponseEntity<Object> getResourceDataFromDmi(final String dataStoreName,
112                                                          final String cmHandleId,
113                                                          final String requestId) {
114         final YangModelCmHandle yangModelCmHandle = getYangModelCmHandle(cmHandleId);
115         final String jsonRequestBody = getDmiRequestBody(READ, requestId, null, null,
116                 yangModelCmHandle);
117         final String dmiResourceDataUrl = getDmiRequestUrl(dataStoreName, cmHandleId, "/",
118                 null, null,
119                 yangModelCmHandle.resolveDmiServiceName(RequiredDmiService.DATA));
120         final CmHandleState cmHandleState = yangModelCmHandle.getCompositeState().getCmHandleState();
121         validateIfCmHandleStateReady(yangModelCmHandle, cmHandleState);
122         return dmiRestClient.postOperationWithJsonData(dmiResourceDataUrl, jsonRequestBody, READ, null);
123     }
124
125     /**
126      * This method requests the resource data by data store for given list of cm handles using dmi client.
127      * The data wil be returned as message on the topic specified.
128      *
129      * @param topicParamInQuery        topic name for (triggering) async responses
130      * @param dataOperationRequest     data operation request to execute operations
131      * @param requestId                requestId for as a response
132      * @param authorization            contents of Authorization header, or null if not present
133      */
134     public void requestResourceDataFromDmi(final String topicParamInQuery,
135                                            final DataOperationRequest dataOperationRequest,
136                                            final String requestId,
137                                            final String authorization)  {
138
139         final Set<String> cmHandlesIds
140                 = getDistinctCmHandleIdsFromDataOperationRequest(dataOperationRequest);
141
142         final Collection<YangModelCmHandle> yangModelCmHandles
143                 = inventoryPersistence.getYangModelCmHandles(cmHandlesIds);
144
145         final Map<String, List<DmiDataOperation>> operationsOutPerDmiServiceName
146                 = ResourceDataOperationRequestUtils.processPerDefinitionInDataOperationsRequest(topicParamInQuery,
147                 requestId, dataOperationRequest, yangModelCmHandles);
148
149         buildDataOperationRequestUrlAndSendToDmiService(topicParamInQuery, requestId, operationsOutPerDmiServiceName,
150                 authorization);
151     }
152
153     /**
154      * This method creates the resource data from pass-through running data store for given cm handle
155      * identifier on given resource using dmi client.
156      *
157      * @param cmHandleId    network resource identifier
158      * @param resourceId    resource identifier
159      * @param operationType operation enum
160      * @param requestData   the request data
161      * @param dataType      data type
162      * @param authorization contents of Authorization header, or null if not present
163      * @return {@code ResponseEntity} response entity
164      */
165     public ResponseEntity<Object> writeResourceDataPassThroughRunningFromDmi(final String cmHandleId,
166                                                                              final String resourceId,
167                                                                              final OperationType operationType,
168                                                                              final String requestData,
169                                                                              final String dataType,
170                                                                              final String authorization) {
171         final YangModelCmHandle yangModelCmHandle = getYangModelCmHandle(cmHandleId);
172         final String jsonRequestBody = getDmiRequestBody(operationType, null, requestData, dataType,
173                 yangModelCmHandle);
174         final String dmiUrl = getDmiRequestUrl(PASSTHROUGH_RUNNING.getDatastoreName(), cmHandleId, resourceId,
175                 null, null,
176                 yangModelCmHandle.resolveDmiServiceName(RequiredDmiService.DATA));
177         final CmHandleState cmHandleState = yangModelCmHandle.getCompositeState().getCmHandleState();
178         validateIfCmHandleStateReady(yangModelCmHandle, cmHandleState);
179         return dmiRestClient.postOperationWithJsonData(dmiUrl, jsonRequestBody, operationType, authorization);
180     }
181
182     private YangModelCmHandle getYangModelCmHandle(final String cmHandleId) {
183         return inventoryPersistence.getYangModelCmHandle(cmHandleId);
184     }
185
186     private String getDmiRequestBody(final OperationType operationType,
187                                      final String requestId,
188                                      final String requestData,
189                                      final String dataType,
190                                      final YangModelCmHandle yangModelCmHandle) {
191         final DmiRequestBody dmiRequestBody = DmiRequestBody.builder()
192                 .operationType(operationType)
193                 .requestId(requestId)
194                 .data(requestData)
195                 .dataType(dataType)
196                 .build();
197         dmiRequestBody.asDmiProperties(yangModelCmHandle.getDmiProperties());
198         return jsonObjectMapper.asJsonString(dmiRequestBody);
199     }
200
201     private String getDmiRequestUrl(final String dataStoreName,
202                                     final String cmHandleId,
203                                     final String resourceId,
204                                     final String optionsParamInQuery,
205                                     final String topicParamInQuery,
206                                     final String dmiServiceName) {
207         return dmiServiceUrlBuilder.getDmiDatastoreUrl(
208                 dmiServiceUrlBuilder.populateQueryParams(resourceId, optionsParamInQuery,
209                         topicParamInQuery), dmiServiceUrlBuilder.populateUriVariables(dataStoreName, dmiServiceName,
210                         cmHandleId));
211     }
212
213     private String getDmiServiceDataOperationRequestUrl(final String dmiServiceName,
214                                                         final String topicParamInQuery,
215                                                         final String requestId) {
216         final MultiValueMap<String, String> dataOperationRequestQueryParams = dmiServiceUrlBuilder
217                 .getDataOperationRequestQueryParams(topicParamInQuery, requestId);
218         return dmiServiceUrlBuilder.getDataOperationRequestUrl(dataOperationRequestQueryParams,
219                 dmiServiceUrlBuilder.populateDataOperationRequestUriVariables(dmiServiceName));
220     }
221
222     private void validateIfCmHandleStateReady(final YangModelCmHandle yangModelCmHandle,
223                                               final CmHandleState cmHandleState) {
224         if (cmHandleState != CmHandleState.READY) {
225             throw new CpsException("State mismatch exception.", "Cm-Handle not in READY state. "
226                     + "cm handle state is "
227                     + yangModelCmHandle.getCompositeState().getCmHandleState());
228         }
229     }
230
231     private static Set<String> getDistinctCmHandleIdsFromDataOperationRequest(final DataOperationRequest
232                                                                               dataOperationRequest) {
233         return dataOperationRequest.getDataOperationDefinitions().stream()
234                 .flatMap(dataOperationDefinition ->
235                         dataOperationDefinition.getCmHandleIds().stream()).collect(Collectors.toSet());
236     }
237
238     private void buildDataOperationRequestUrlAndSendToDmiService(final String topicParamInQuery,
239                                                                  final String requestId,
240                                                                  final Map<String, List<DmiDataOperation>>
241                                                                 groupsOutPerDmiServiceName,
242                                                                  final String authorization) {
243
244         groupsOutPerDmiServiceName.forEach((dmiServiceName, dmiDataOperationRequestBodies) -> {
245             final String dmiDataOperationResourceUrl =
246                     getDmiServiceDataOperationRequestUrl(dmiServiceName, topicParamInQuery, requestId);
247             sendDataOperationRequestToDmiService(dmiDataOperationResourceUrl, dmiDataOperationRequestBodies,
248                     authorization);
249         });
250     }
251
252     private void sendDataOperationRequestToDmiService(final String dataOperationResourceUrl,
253                                                       final List<DmiDataOperation> dmiDataOperationRequestBodies,
254                                                       final String authorization) {
255         final DmiDataOperationRequest dmiDataOperationRequest = DmiDataOperationRequest.builder()
256                 .operations(dmiDataOperationRequestBodies).build();
257         final String dmiDataOperationRequestAsJsonString =
258                 jsonObjectMapper.asJsonString(dmiDataOperationRequest);
259         try {
260             dmiRestClient.postOperationWithJsonData(dataOperationResourceUrl, dmiDataOperationRequestAsJsonString, READ,
261                     authorization);
262         } catch (final Exception exception) {
263             handleTaskCompletionException(exception, dataOperationResourceUrl, dmiDataOperationRequestBodies);
264         }
265     }
266
267     private void handleTaskCompletionException(final Throwable throwable,
268                                                final String dataOperationResourceUrl,
269                                                final List<DmiDataOperation> dmiDataOperationRequestBodies) {
270         if (throwable != null) {
271             final MultiValueMap<String, String> dataOperationResourceUrlParameters =
272                     UriComponentsBuilder.fromUriString(dataOperationResourceUrl).build().getQueryParams();
273             final String topicName = dataOperationResourceUrlParameters.get("topic").get(0);
274             final String requestId = dataOperationResourceUrlParameters.get("requestId").get(0);
275
276             final MultiValueMap<DmiDataOperation, Map<NcmpResponseStatus, List<String>>>
277                     cmHandleIdsPerResponseCodesPerOperation = new LinkedMultiValueMap<>();
278
279             dmiDataOperationRequestBodies.forEach(dmiDataOperationRequestBody -> {
280                 final List<String> cmHandleIds = dmiDataOperationRequestBody.getCmHandles().stream()
281                         .map(CmHandle::getId).toList();
282                 if (throwable.getCause() instanceof HttpClientRequestException) {
283                     cmHandleIdsPerResponseCodesPerOperation.add(dmiDataOperationRequestBody,
284                             Map.of(UNABLE_TO_READ_RESOURCE_DATA, cmHandleIds));
285                 } else {
286                     cmHandleIdsPerResponseCodesPerOperation.add(dmiDataOperationRequestBody,
287                             Map.of(DMI_SERVICE_NOT_RESPONDING, cmHandleIds));
288                 }
289             });
290             ResourceDataOperationRequestUtils.publishErrorMessageToClientTopic(topicName, requestId,
291                     cmHandleIdsPerResponseCodesPerOperation);
292         }
293     }
294 }