From 7090a07b05517e0a748cb82ffac8840de82c7fa5 Mon Sep 17 00:00:00 2001 From: "raviteja.karumuri" Date: Fri, 23 Jun 2023 13:52:18 +0100 Subject: #2 NCMP : Replacing the word 'Batch' as 'DataOperation'. Issue-ID: CPS-1754 Signed-off-by: raviteja.karumuri Change-Id: Ib04e9d8a909e8868bf094e2625412996409b3a67 --- .../cps/ncmp/api/NetworkCmProxyDataService.java | 5 +- .../api/impl/operations/DmiBatchOperation.java | 64 ----------- .../ncmp/api/impl/operations/DmiDataOperation.java | 64 +++++++++++ .../api/impl/operations/DmiDataOperations.java | 48 ++++---- .../ncmp/api/impl/utils/DmiServiceUrlBuilder.java | 32 +++--- .../impl/utils/ResourceDataBatchRequestUtils.java | 126 -------------------- .../utils/ResourceDataOperationRequestUtils.java | 127 +++++++++++++++++++++ .../impl/operations/DmiDataOperationsSpec.groovy | 2 +- .../utils/DataOperationRequestUtilsSpec.groovy | 2 +- .../api/impl/utils/DmiServiceUrlBuilderSpec.groovy | 6 +- 10 files changed, 239 insertions(+), 237 deletions(-) delete mode 100644 cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/operations/DmiBatchOperation.java create mode 100644 cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/operations/DmiDataOperation.java delete mode 100644 cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/utils/ResourceDataBatchRequestUtils.java create mode 100644 cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/utils/ResourceDataOperationRequestUtils.java (limited to 'cps-ncmp-service') diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/NetworkCmProxyDataService.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/NetworkCmProxyDataService.java index e20780bf56..a65e3c4be1 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/NetworkCmProxyDataService.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/NetworkCmProxyDataService.java @@ -86,11 +86,10 @@ public interface NetworkCmProxyDataService { * Execute (async) data operation for group of cm handles using dmi. * * @param topicParamInQuery topic name for (triggering) async responses - * @param dataOperationRequest cm handle identifiers + * @param dataOperationRequest contains a list of operation definitions(multiple operations) */ void executeDataOperationForCmHandles(String topicParamInQuery, - DataOperationRequest - dataOperationRequest, + DataOperationRequest dataOperationRequest, String requestId); diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/operations/DmiBatchOperation.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/operations/DmiBatchOperation.java deleted file mode 100644 index 512c3bcda8..0000000000 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/operations/DmiBatchOperation.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * Copyright (C) 2023 Nordix Foundation - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.cps.ncmp.api.impl.operations; - -import com.fasterxml.jackson.annotation.JsonInclude; -import com.fasterxml.jackson.annotation.JsonProperty; -import com.fasterxml.jackson.annotation.JsonPropertyOrder; -import java.util.ArrayList; -import java.util.List; -import lombok.Builder; -import lombok.Getter; -import org.onap.cps.ncmp.api.models.DataOperationDefinition; - -@JsonInclude(JsonInclude.Include.NON_NULL) -@Getter -@Builder -@JsonPropertyOrder({"operation", "operationId", "datastore", "options", "resourceIdentifier", "cmHandles"}) -public class DmiBatchOperation { - - @JsonProperty("operation") - private OperationType operationType; - private String operationId; - private String datastore; - private String options; - private String resourceIdentifier; - - private final List cmHandles = new ArrayList<>(); - - /** - * Create and initialise a (outgoing) DMI data operation. - * - * @param dataOperationDefinition definition of incoming of dataOperation request - * @return mapped dmi operation details - */ - public static DmiBatchOperation buildDmiBatchRequestBodyWithoutCmHandles( - final DataOperationDefinition dataOperationDefinition) { - - return DmiBatchOperation.builder() - .operationType(OperationType.fromOperationName(dataOperationDefinition.getOperation())) - .operationId(dataOperationDefinition.getOperationId()) - .datastore(DatastoreType.fromDatastoreName(dataOperationDefinition.getDatastore()).getDatastoreName()) - .options(dataOperationDefinition.getOptions()) - .resourceIdentifier(dataOperationDefinition.getResourceIdentifier()) - .build(); - } -} diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/operations/DmiDataOperation.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/operations/DmiDataOperation.java new file mode 100644 index 0000000000..6346379b22 --- /dev/null +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/operations/DmiDataOperation.java @@ -0,0 +1,64 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2023 Nordix Foundation + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.cps.ncmp.api.impl.operations; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonPropertyOrder; +import java.util.ArrayList; +import java.util.List; +import lombok.Builder; +import lombok.Getter; +import org.onap.cps.ncmp.api.models.DataOperationDefinition; + +@JsonInclude(JsonInclude.Include.NON_NULL) +@Getter +@Builder +@JsonPropertyOrder({"operation", "operationId", "datastore", "options", "resourceIdentifier", "cmHandles"}) +public class DmiDataOperation { + + @JsonProperty("operation") + private OperationType operationType; + private String operationId; + private String datastore; + private String options; + private String resourceIdentifier; + + private final List cmHandles = new ArrayList<>(); + + /** + * Create and initialise a (outgoing) DMI data operation. + * + * @param dataOperationDefinition definition of incoming of dataOperation request + * @return mapped dmi operation details + */ + public static DmiDataOperation buildDmiDataOperationRequestBodyWithoutCmHandles( + final DataOperationDefinition dataOperationDefinition) { + + return DmiDataOperation.builder() + .operationType(OperationType.fromOperationName(dataOperationDefinition.getOperation())) + .operationId(dataOperationDefinition.getOperationId()) + .datastore(DatastoreType.fromDatastoreName(dataOperationDefinition.getDatastore()).getDatastoreName()) + .options(dataOperationDefinition.getOptions()) + .resourceIdentifier(dataOperationDefinition.getResourceIdentifier()) + .build(); + } +} diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/operations/DmiDataOperations.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/operations/DmiDataOperations.java index adf634da57..8596c56dca 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/operations/DmiDataOperations.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/operations/DmiDataOperations.java @@ -34,7 +34,7 @@ import org.onap.cps.ncmp.api.impl.client.DmiRestClient; import org.onap.cps.ncmp.api.impl.config.NcmpConfiguration; import org.onap.cps.ncmp.api.impl.executor.TaskExecutor; import org.onap.cps.ncmp.api.impl.utils.DmiServiceUrlBuilder; -import org.onap.cps.ncmp.api.impl.utils.ResourceDataBatchRequestUtils; +import org.onap.cps.ncmp.api.impl.utils.ResourceDataOperationRequestUtils; import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle; import org.onap.cps.ncmp.api.inventory.CmHandleState; import org.onap.cps.ncmp.api.inventory.InventoryPersistence; @@ -131,11 +131,11 @@ public class DmiDataOperations extends DmiOperations { final Collection yangModelCmHandles = getYangModelCmHandlesInReadyState(cmHandlesIds); - final Map> operationsOutPerDmiServiceName - = ResourceDataBatchRequestUtils.processPerOperationInBatchRequest(dataOperationRequest, + final Map> operationsOutPerDmiServiceName + = ResourceDataOperationRequestUtils.processPerDefinitionInDataOperationsRequest(dataOperationRequest, yangModelCmHandles); - buildBatchRequestUrlAndSendToDmiService(topicParamInQuery, requestId, operationsOutPerDmiServiceName); + buildDataOperationRequestUrlAndSendToDmiService(topicParamInQuery, requestId, operationsOutPerDmiServiceName); } /** @@ -196,13 +196,13 @@ public class DmiDataOperations extends DmiOperations { cmHandleId)); } - private String getDmiServiceBatchRequestUrl(final String dmiServiceName, - final String topicParamInQuery, - final String requestId) { - final MultiValueMap batchRequestQueryParams = dmiServiceUrlBuilder - .getBatchRequestQueryParams(topicParamInQuery, requestId); - return dmiServiceUrlBuilder.getBatchRequestUrl(batchRequestQueryParams, - dmiServiceUrlBuilder.populateBatchUriVariables(dmiServiceName)); + private String getDmiServiceDataOperationRequestUrl(final String dmiServiceName, + final String topicParamInQuery, + final String requestId) { + final MultiValueMap dataOperationRequestQueryParams = dmiServiceUrlBuilder + .getDataOperationRequestQueryParams(topicParamInQuery, requestId); + return dmiServiceUrlBuilder.getDataOperationRequestUrl(dataOperationRequestQueryParams, + dmiServiceUrlBuilder.populateDataOperationRequestUriVariables(dmiServiceName)); } private void validateIfCmHandleStateReady(final YangModelCmHandle yangModelCmHandle, @@ -230,25 +230,27 @@ public class DmiDataOperations extends DmiOperations { == CmHandleState.READY).collect(Collectors.toList()); } - private void buildBatchRequestUrlAndSendToDmiService(final String topicParamInQuery, - final String requestId, - final Map> + private void buildDataOperationRequestUrlAndSendToDmiService(final String topicParamInQuery, + final String requestId, + final Map> groupsOutPerDmiServiceName) { groupsOutPerDmiServiceName.entrySet().forEach(groupsOutPerDmiServiceNameEntry -> { final String dmiServiceName = groupsOutPerDmiServiceNameEntry.getKey(); - final List dmiBatchRequestBodies = groupsOutPerDmiServiceNameEntry.getValue(); - final String dmiBatchResourceDataUrl = getDmiServiceBatchRequestUrl(dmiServiceName, topicParamInQuery, - requestId); - sendBatchRequestToDmiService(dmiBatchResourceDataUrl, dmiBatchRequestBodies); + final List dmiDataOperationRequestBodies = groupsOutPerDmiServiceNameEntry.getValue(); + final String dmiDataOperationResourceUrl = + getDmiServiceDataOperationRequestUrl(dmiServiceName, topicParamInQuery, requestId); + sendDataOperationRequestToDmiService(dmiDataOperationResourceUrl, dmiDataOperationRequestBodies); }); } - private void sendBatchRequestToDmiService(final String batchResourceDataUrl, - final List dmiBatchRequestBodies) { - final String batchRequestBodiesAsJsonString = jsonObjectMapper.asJsonString(dmiBatchRequestBodies); - TaskExecutor.executeTask(() -> dmiRestClient.postOperationWithJsonData(batchResourceDataUrl, - batchRequestBodiesAsJsonString, READ), DEFAULT_ASYNC_TASK_EXECUTOR_TIMEOUT_IN_MILLISECONDS) + private void sendDataOperationRequestToDmiService(final String dataOperationResourceUrl, + final List dmiDataOperationRequestBodies) { + final String dataOperationRequestBodiesAsJsonString = + jsonObjectMapper.asJsonString(dmiDataOperationRequestBodies); + TaskExecutor.executeTask(() -> dmiRestClient.postOperationWithJsonData(dataOperationResourceUrl, + dataOperationRequestBodiesAsJsonString, READ), + DEFAULT_ASYNC_TASK_EXECUTOR_TIMEOUT_IN_MILLISECONDS) .whenCompleteAsync(this::handleTaskCompletion); } diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/utils/DmiServiceUrlBuilder.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/utils/DmiServiceUrlBuilder.java index 5c6fa9f0b0..d855442c53 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/utils/DmiServiceUrlBuilder.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/utils/DmiServiceUrlBuilder.java @@ -53,17 +53,17 @@ public class DmiServiceUrlBuilder { } /** - * This method builds batch request url. + * This method builds data operation request url. * - * @param batchRequestQueryParams query param map as key, value pair - * @param batchRequestUriVariables uri param map as key (placeholder), value pair - * @return {@code String} batch request url as string + * @param dataoperationRequestQueryParams query param map as key, value pair + * @param dataoperationRequestUriVariables uri param map as key (placeholder), value pair + * @return {@code String} data operation request url as string */ - public String getBatchRequestUrl(final MultiValueMap batchRequestQueryParams, - final Map batchRequestUriVariables) { - return getBatchResourceDataBasePathUriBuilder() - .queryParams(batchRequestQueryParams) - .uriVariables(batchRequestUriVariables) + public String getDataOperationRequestUrl(final MultiValueMap dataoperationRequestQueryParams, + final Map dataoperationRequestUriVariables) { + return getDataOperationResourceDataBasePathUriBuilder() + .queryParams(dataoperationRequestQueryParams) + .uriVariables(dataoperationRequestUriVariables) .buildAndExpand().toUriString(); } @@ -82,11 +82,11 @@ public class DmiServiceUrlBuilder { } /** - * This method creates the dmi service url builder object with path variables for batch of cm handles. + * This method creates the dmi service url builder object with path variables for data operation request. * * @return {@code UriComponentsBuilder} dmi service url builder object */ - public UriComponentsBuilder getBatchResourceDataBasePathUriBuilder() { + public UriComponentsBuilder getDataOperationResourceDataBasePathUriBuilder() { return UriComponentsBuilder.newInstance() .path("{dmiServiceName}") .pathSegment("{dmiBasePath}") @@ -116,12 +116,12 @@ public class DmiServiceUrlBuilder { } /** - * This method populates uri variables for batch request. + * This method populates uri variables for data operation request. * * @param dmiServiceName dmi service name * @return {@code Map} uri variables as map */ - public Map populateBatchUriVariables(final String dmiServiceName) { + public Map populateDataOperationRequestUriVariables(final String dmiServiceName) { final Map uriVariables = new HashMap<>(); final String dmiBasePath = dmiProperties.getDmiBasePath(); uriVariables.put("dmiServiceName", dmiServiceName); @@ -151,14 +151,14 @@ public class DmiServiceUrlBuilder { } /** - * This method is used to populate map from query params for batch request. + * This method is used to populate map from query params for data operation request. * * @param topicParamInQuery topic into url param * @param requestId unique id of response for valid topic * @return all valid query params as map */ - public MultiValueMap getBatchRequestQueryParams(final String topicParamInQuery, - final String requestId) { + public MultiValueMap getDataOperationRequestQueryParams(final String topicParamInQuery, + final String requestId) { final MultiValueMap queryParams = new LinkedMultiValueMap<>(); getQueryParamConsumer().accept("topic", topicParamInQuery, queryParams); getQueryParamConsumer().accept("requestId", requestId, queryParams); diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/utils/ResourceDataBatchRequestUtils.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/utils/ResourceDataBatchRequestUtils.java deleted file mode 100644 index f0d02552f6..0000000000 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/utils/ResourceDataBatchRequestUtils.java +++ /dev/null @@ -1,126 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * Copyright (C) 2023 Nordix Foundation - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.cps.ncmp.api.impl.utils; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Set; -import lombok.AccessLevel; -import lombok.NoArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.onap.cps.ncmp.api.impl.operations.CmHandle; -import org.onap.cps.ncmp.api.impl.operations.DmiBatchOperation; -import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle; -import org.onap.cps.ncmp.api.models.DataOperationDefinition; -import org.onap.cps.ncmp.api.models.DataOperationRequest; - -@Slf4j -@NoArgsConstructor(access = AccessLevel.PRIVATE) -public class ResourceDataBatchRequestUtils { - - private static final String UNKNOWN_SERVICE_NAME = null; - - /** - * Create a list of DMI batch operation per DMI service (name). - * - * @param dataOperationRequestIn incoming data operation request details - * @param yangModelCmHandles involved cm handles represented as YangModelCmHandle (incl. metadata) - * - * @return {@code Map>} Create a list of DMI batch operation per DMI service (name). - */ - public static Map> processPerOperationInBatchRequest( - final DataOperationRequest dataOperationRequestIn, - final Collection yangModelCmHandles) { - - final Map>> dmiPropertiesPerCmHandleIdPerServiceName = - DmiServiceNameOrganizer.getDmiPropertiesPerCmHandleIdPerServiceName(yangModelCmHandles); - - final Map dmiServiceNamesPerCmHandleId = - getDmiServiceNamesPerCmHandleId(dmiPropertiesPerCmHandleIdPerServiceName); - - final Map> dmiBatchOperationsOutPerDmiServiceName = new HashMap<>(); - - for (final DataOperationDefinition dataOperationDefinitionIn : - dataOperationRequestIn.getDataOperationDefinitions()) { - for (final String cmHandleId : dataOperationDefinitionIn.getCmHandleIds()) { - final String dmiServiceName = dmiServiceNamesPerCmHandleId.get(cmHandleId); - final Map cmHandleIdProperties - = dmiPropertiesPerCmHandleIdPerServiceName.get(dmiServiceName).get(cmHandleId); - if (cmHandleIdProperties == null) { - publishErrorMessageToClientTopic(cmHandleId); - } else { - final DmiBatchOperation dmiBatchOperationOut = getOrAddDmiBatchOperation(dmiServiceName, - dataOperationDefinitionIn, dmiBatchOperationsOutPerDmiServiceName); - final CmHandle cmHandle = CmHandle.buildCmHandleWithProperties(cmHandleId, cmHandleIdProperties); - dmiBatchOperationOut.getCmHandles().add(cmHandle); - } - } - } - return dmiBatchOperationsOutPerDmiServiceName; - } - - private static void publishErrorMessageToClientTopic(final String requestedCmHandleId) { - log.warn("cm handle {} not found", requestedCmHandleId); - // TODO Need to publish an error response to client given topic. - // Code should be implemented into https://jira.onap.org/browse/CPS-1583 ( - // NCMP : Handle non-existing cm handles) - } - - private static Map getDmiServiceNamesPerCmHandleId( - final Map>> dmiDmiPropertiesPerCmHandleIdPerServiceName) { - final Map dmiServiceNamesPerCmHandleId = new HashMap<>(); - for (final Map.Entry>> dmiDmiPropertiesEntry - : dmiDmiPropertiesPerCmHandleIdPerServiceName.entrySet()) { - final String dmiServiceName = dmiDmiPropertiesEntry.getKey(); - final Set cmHandleIds = dmiDmiPropertiesPerCmHandleIdPerServiceName.get(dmiServiceName).keySet(); - for (final String cmHandleId : cmHandleIds) { - dmiServiceNamesPerCmHandleId.put(cmHandleId, dmiServiceName); - } - } - dmiDmiPropertiesPerCmHandleIdPerServiceName.put(UNKNOWN_SERVICE_NAME, Collections.emptyMap()); - return dmiServiceNamesPerCmHandleId; - } - - private static DmiBatchOperation getOrAddDmiBatchOperation(final String dmiServiceName, - final DataOperationDefinition - dataOperationDefinitionIn, - final Map> - dmiBatchOperationsOutPerDmiServiceName) { - dmiBatchOperationsOutPerDmiServiceName - .computeIfAbsent(dmiServiceName, dmiServiceNameAsKey -> new ArrayList<>()); - final List dmiBatchOperationsOut - = dmiBatchOperationsOutPerDmiServiceName.get(dmiServiceName); - final boolean isNewOperation = dmiBatchOperationsOut.isEmpty() - || !dmiBatchOperationsOut.get(dmiBatchOperationsOut.size() - 1).getOperationId() - .equals(dataOperationDefinitionIn.getOperationId()); - if (isNewOperation) { - final DmiBatchOperation newDmiBatchOperationOut = - DmiBatchOperation.buildDmiBatchRequestBodyWithoutCmHandles(dataOperationDefinitionIn); - dmiBatchOperationsOut.add(newDmiBatchOperationOut); - return newDmiBatchOperationOut; - } - return dmiBatchOperationsOut.get(dmiBatchOperationsOut.size() - 1); - } -} diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/utils/ResourceDataOperationRequestUtils.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/utils/ResourceDataOperationRequestUtils.java new file mode 100644 index 0000000000..573f8b39a9 --- /dev/null +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/utils/ResourceDataOperationRequestUtils.java @@ -0,0 +1,127 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2023 Nordix Foundation + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.cps.ncmp.api.impl.utils; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.onap.cps.ncmp.api.impl.operations.CmHandle; +import org.onap.cps.ncmp.api.impl.operations.DmiDataOperation; +import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle; +import org.onap.cps.ncmp.api.models.DataOperationDefinition; +import org.onap.cps.ncmp.api.models.DataOperationRequest; + +@Slf4j +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public class ResourceDataOperationRequestUtils { + + private static final String UNKNOWN_SERVICE_NAME = null; + + /** + * Create a list of DMI data operations per DMI service (name). + * + * @param dataOperationRequestIn incoming data operation request details + * @param yangModelCmHandles involved cm handles represented as YangModelCmHandle (incl. metadata) + * + * @return {@code Map>} Create a list of DMI data operations operation + * per DMI service (name). + */ + public static Map> processPerDefinitionInDataOperationsRequest( + final DataOperationRequest dataOperationRequestIn, + final Collection yangModelCmHandles) { + + final Map>> dmiPropertiesPerCmHandleIdPerServiceName = + DmiServiceNameOrganizer.getDmiPropertiesPerCmHandleIdPerServiceName(yangModelCmHandles); + + final Map dmiServiceNamesPerCmHandleId = + getDmiServiceNamesPerCmHandleId(dmiPropertiesPerCmHandleIdPerServiceName); + + final Map> dmiDataOperationsOutPerDmiServiceName = new HashMap<>(); + + for (final DataOperationDefinition dataOperationDefinitionIn : + dataOperationRequestIn.getDataOperationDefinitions()) { + for (final String cmHandleId : dataOperationDefinitionIn.getCmHandleIds()) { + final String dmiServiceName = dmiServiceNamesPerCmHandleId.get(cmHandleId); + final Map cmHandleIdProperties + = dmiPropertiesPerCmHandleIdPerServiceName.get(dmiServiceName).get(cmHandleId); + if (cmHandleIdProperties == null) { + publishErrorMessageToClientTopic(cmHandleId); + } else { + final DmiDataOperation dmiDataOperationOut = getOrAddDmiDataOperation(dmiServiceName, + dataOperationDefinitionIn, dmiDataOperationsOutPerDmiServiceName); + final CmHandle cmHandle = CmHandle.buildCmHandleWithProperties(cmHandleId, cmHandleIdProperties); + dmiDataOperationOut.getCmHandles().add(cmHandle); + } + } + } + return dmiDataOperationsOutPerDmiServiceName; + } + + private static void publishErrorMessageToClientTopic(final String requestedCmHandleId) { + log.warn("cm handle {} not found", requestedCmHandleId); + // TODO Need to publish an error response to client given topic. + // Code should be implemented into https://jira.onap.org/browse/CPS-1583 ( + // NCMP : Handle non-existing cm handles) + } + + private static Map getDmiServiceNamesPerCmHandleId( + final Map>> dmiDmiPropertiesPerCmHandleIdPerServiceName) { + final Map dmiServiceNamesPerCmHandleId = new HashMap<>(); + for (final Map.Entry>> dmiDmiPropertiesEntry + : dmiDmiPropertiesPerCmHandleIdPerServiceName.entrySet()) { + final String dmiServiceName = dmiDmiPropertiesEntry.getKey(); + final Set cmHandleIds = dmiDmiPropertiesPerCmHandleIdPerServiceName.get(dmiServiceName).keySet(); + for (final String cmHandleId : cmHandleIds) { + dmiServiceNamesPerCmHandleId.put(cmHandleId, dmiServiceName); + } + } + dmiDmiPropertiesPerCmHandleIdPerServiceName.put(UNKNOWN_SERVICE_NAME, Collections.emptyMap()); + return dmiServiceNamesPerCmHandleId; + } + + private static DmiDataOperation getOrAddDmiDataOperation(final String dmiServiceName, + final DataOperationDefinition + dataOperationDefinitionIn, + final Map> + dmiDataOperationsOutPerDmiServiceName) { + dmiDataOperationsOutPerDmiServiceName + .computeIfAbsent(dmiServiceName, dmiServiceNameAsKey -> new ArrayList<>()); + final List dmiDataOperationsOut + = dmiDataOperationsOutPerDmiServiceName.get(dmiServiceName); + final boolean isNewOperation = dmiDataOperationsOut.isEmpty() + || !dmiDataOperationsOut.get(dmiDataOperationsOut.size() - 1).getOperationId() + .equals(dataOperationDefinitionIn.getOperationId()); + if (isNewOperation) { + final DmiDataOperation newDmiDataOperationOut = + DmiDataOperation.buildDmiDataOperationRequestBodyWithoutCmHandles(dataOperationDefinitionIn); + dmiDataOperationsOut.add(newDmiDataOperationOut); + return newDmiDataOperationOut; + } + return dmiDataOperationsOut.get(dmiDataOperationsOut.size() - 1); + } +} diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/operations/DmiDataOperationsSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/operations/DmiDataOperationsSpec.groovy index be178e1276..c7ee4e0745 100644 --- a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/operations/DmiDataOperationsSpec.groovy +++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/operations/DmiDataOperationsSpec.groovy @@ -94,7 +94,7 @@ class DmiDataOperationsSpec extends DmiOperationsBaseSpec { def expectedDmiBatchResourceDataUrl = "ncmp/v1/data/topic=my-topic-name" def expectedBatchRequestAsJson = '[{"operation":"read","operationId":"operational-14","datastore":"ncmp-datastore:passthrough-operational","options":"some option","resourceIdentifier":"some resource identifier","cmHandles":[{"id":"some-cm-handle","cmHandleProperties":{"prop1":"val1"}}]}]' mockDmiRestClient.postOperationWithJsonData(expectedDmiBatchResourceDataUrl, _, READ.operationName) >> responseFromDmi - dmiServiceUrlBuilder.getBatchRequestUrl(_, _) >> expectedDmiBatchResourceDataUrl + dmiServiceUrlBuilder.getDataOperationRequestUrl(_, _) >> expectedDmiBatchResourceDataUrl when: 'get resource data for group of cm handles are invoked' objectUnderTest.requestResourceDataFromDmi('my-topic-name', dataOperationRequest, 'requestId') then: 'wait a little to allow execution of service method by task executor (on separate thread)' diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/utils/DataOperationRequestUtilsSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/utils/DataOperationRequestUtilsSpec.groovy index af3841a40b..334b455ef7 100644 --- a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/utils/DataOperationRequestUtilsSpec.groovy +++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/utils/DataOperationRequestUtilsSpec.groovy @@ -42,7 +42,7 @@ class DataOperationRequestUtilsSpec extends Specification { and: '4 known cm handles: ch1-dmi1, ch2-dmi1, ch3-dmi2, ch4-dmi2' def yangModelCmHandles = getYangModelCmHandles() when: 'data operation request is processed' - def operationsOutPerDmiServiceName = ResourceDataBatchRequestUtils.processPerOperationInBatchRequest(dataOperationRequest, yangModelCmHandles) + def operationsOutPerDmiServiceName = ResourceDataOperationRequestUtils.processPerDefinitionInDataOperationsRequest(dataOperationRequest, yangModelCmHandles) and: 'converted to a json node' def dmiDataOperationRequestBody = jsonObjectMapper.asJsonString(operationsOutPerDmiServiceName.get(serviceName)) def dmiDataOperationRequestBodyAsJsonNode = jsonObjectMapper.convertToJsonNode(dmiDataOperationRequestBody).get(operationIndex) diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/utils/DmiServiceUrlBuilderSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/utils/DmiServiceUrlBuilderSpec.groovy index b1a8e85e2d..6c4575515f 100644 --- a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/utils/DmiServiceUrlBuilderSpec.groovy +++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/utils/DmiServiceUrlBuilderSpec.groovy @@ -81,15 +81,15 @@ class DmiServiceUrlBuilderSpec extends Specification { given: 'the required path parameters' def batchRequestUriVariables = [dmiServiceName: 'some-service', dmiBasePath: 'testBase', cmHandleId: '123'] and: 'the relevant query parameters' - def batchRequestQueryParams = objectUnderTest.getBatchRequestQueryParams('some topic', 'some id') + def batchRequestQueryParams = objectUnderTest.getDataOperationRequestQueryParams('some topic', 'some id') when: 'a URL is created' - def result = objectUnderTest.getBatchRequestUrl(batchRequestQueryParams, batchRequestUriVariables) + def result = objectUnderTest.getDataOperationRequestUrl(batchRequestQueryParams, batchRequestUriVariables) then: 'it is formed correctly' assert result.toString() == 'some-service/testBase/v1/data?topic=some topic&requestId=some id' } def 'Populate batch uri variables.'() { expect: 'Populate batch uri variables returns a map with given service name and base path from setup' - assert objectUnderTest.populateBatchUriVariables('some service') == [ dmiServiceName: 'some service',dmiBasePath: 'dmi' ] + assert objectUnderTest.populateDataOperationRequestUriVariables('some service') == [dmiServiceName: 'some service', dmiBasePath: 'dmi' ] } } -- cgit 1.2.3-korg