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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.cps.ncmp.api.impl.operations;
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;
29 import io.micrometer.core.annotation.Timed;
30 import java.util.Collection;
31 import java.util.List;
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;
55 * Operations class for DMI data.
59 public class DmiDataOperations extends DmiOperations {
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);
70 * This method fetches the resource data from operational data store for given cm handle
71 * identifier on given resource using dmi client.
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
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,
97 final String dmiResourceDataUrl = getDmiRequestUrl(dataStoreName, cmHandleId, resourceId, optionsParamInQuery,
98 topicParamInQuery, yangModelCmHandle.resolveDmiServiceName(RequiredDmiService.DATA));
99 return dmiRestClient.postOperationWithJsonData(dmiResourceDataUrl, jsonRequestBody, READ, authorization);
103 * This method fetches all the resource data from operational data store for given cm handle
104 * identifier using dmi client.
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
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,
117 final String dmiResourceDataUrl = getDmiRequestUrl(dataStoreName, cmHandleId, "/",
119 yangModelCmHandle.resolveDmiServiceName(RequiredDmiService.DATA));
120 final CmHandleState cmHandleState = yangModelCmHandle.getCompositeState().getCmHandleState();
121 validateIfCmHandleStateReady(yangModelCmHandle, cmHandleState);
122 return dmiRestClient.postOperationWithJsonData(dmiResourceDataUrl, jsonRequestBody, READ, null);
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.
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
134 public void requestResourceDataFromDmi(final String topicParamInQuery,
135 final DataOperationRequest dataOperationRequest,
136 final String requestId,
137 final String authorization) {
139 final Set<String> cmHandlesIds
140 = getDistinctCmHandleIdsFromDataOperationRequest(dataOperationRequest);
142 final Collection<YangModelCmHandle> yangModelCmHandles
143 = inventoryPersistence.getYangModelCmHandles(cmHandlesIds);
145 final Map<String, List<DmiDataOperation>> operationsOutPerDmiServiceName
146 = ResourceDataOperationRequestUtils.processPerDefinitionInDataOperationsRequest(topicParamInQuery,
147 requestId, dataOperationRequest, yangModelCmHandles);
149 buildDataOperationRequestUrlAndSendToDmiService(topicParamInQuery, requestId, operationsOutPerDmiServiceName,
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.
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
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,
174 final String dmiUrl = getDmiRequestUrl(PASSTHROUGH_RUNNING.getDatastoreName(), cmHandleId, resourceId,
176 yangModelCmHandle.resolveDmiServiceName(RequiredDmiService.DATA));
177 final CmHandleState cmHandleState = yangModelCmHandle.getCompositeState().getCmHandleState();
178 validateIfCmHandleStateReady(yangModelCmHandle, cmHandleState);
179 return dmiRestClient.postOperationWithJsonData(dmiUrl, jsonRequestBody, operationType, authorization);
182 private YangModelCmHandle getYangModelCmHandle(final String cmHandleId) {
183 return inventoryPersistence.getYangModelCmHandle(cmHandleId);
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)
197 dmiRequestBody.asDmiProperties(yangModelCmHandle.getDmiProperties());
198 return jsonObjectMapper.asJsonString(dmiRequestBody);
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,
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));
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());
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());
238 private void buildDataOperationRequestUrlAndSendToDmiService(final String topicParamInQuery,
239 final String requestId,
240 final Map<String, List<DmiDataOperation>>
241 groupsOutPerDmiServiceName,
242 final String authorization) {
244 groupsOutPerDmiServiceName.forEach((dmiServiceName, dmiDataOperationRequestBodies) -> {
245 final String dmiDataOperationResourceUrl =
246 getDmiServiceDataOperationRequestUrl(dmiServiceName, topicParamInQuery, requestId);
247 sendDataOperationRequestToDmiService(dmiDataOperationResourceUrl, dmiDataOperationRequestBodies,
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);
260 dmiRestClient.postOperationWithJsonData(dataOperationResourceUrl, dmiDataOperationRequestAsJsonString, READ,
262 } catch (final Exception exception) {
263 handleTaskCompletionException(exception, dataOperationResourceUrl, dmiDataOperationRequestBodies);
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);
276 final MultiValueMap<DmiDataOperation, Map<NcmpResponseStatus, List<String>>>
277 cmHandleIdsPerResponseCodesPerOperation = new LinkedMultiValueMap<>();
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));
286 cmHandleIdsPerResponseCodesPerOperation.add(dmiDataOperationRequestBody,
287 Map.of(DMI_SERVICE_NOT_RESPONDING, cmHandleIds));
290 ResourceDataOperationRequestUtils.publishErrorMessageToClientTopic(topicName, requestId,
291 cmHandleIdsPerResponseCodesPerOperation);