diff options
author | leventecsanyi <levente.csanyi@est.tech> | 2024-06-17 11:11:24 +0200 |
---|---|---|
committer | leventecsanyi <levente.csanyi@est.tech> | 2024-06-20 15:17:20 +0200 |
commit | 4a6e141c886dee974af4661db7d26792b5e75210 (patch) | |
tree | 945fa2d15b2e4a5e34873421fa4f7c9c6d553fbc /cps-ncmp-service/src/main/java | |
parent | 35e0df312cbb2fd0a3740805636338713836b5e1 (diff) |
Spliting a data-job into sub-jobs for DMI Plugin
- algorithm for create sub-job requests
- added new method to DmiServiceUrlBuilder to get the write job url
- created WriteOperationExaminer, DmiSubJobClient & testware
Issue-ID: CPS-2142
Change-Id: I258d334ef346cd388341a1deb4078d24d8bdb7cc
Signed-off-by: leventecsanyi <levente.csanyi@est.tech>
Diffstat (limited to 'cps-ncmp-service/src/main/java')
9 files changed, 369 insertions, 4 deletions
diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/datajobs/DataJobService.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/datajobs/DataJobService.java index f22124593e..6ff79a9344 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/datajobs/DataJobService.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/datajobs/DataJobService.java @@ -20,9 +20,11 @@ package org.onap.cps.ncmp.api.datajobs; +import java.util.List; import org.onap.cps.ncmp.api.datajobs.models.DataJobMetadata; import org.onap.cps.ncmp.api.datajobs.models.DataJobReadRequest; import org.onap.cps.ncmp.api.datajobs.models.DataJobWriteRequest; +import org.onap.cps.ncmp.api.datajobs.models.SubJobWriteResponse; public interface DataJobService { @@ -41,6 +43,8 @@ public interface DataJobService { * @param dataJobId Unique identifier of the job within the request * @param dataJobMetadata data job request headers * @param dataJobWriteRequest write data job request + * @return a list of sub-job write responses */ - void writeDataJob(String dataJobId, DataJobMetadata dataJobMetadata, DataJobWriteRequest dataJobWriteRequest); + List<SubJobWriteResponse> writeDataJob(String dataJobId, DataJobMetadata dataJobMetadata, + DataJobWriteRequest dataJobWriteRequest); }
\ No newline at end of file diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/datajobs/models/DmiWriteOperation.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/datajobs/models/DmiWriteOperation.java new file mode 100644 index 0000000000..7e9ca7988b --- /dev/null +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/datajobs/models/DmiWriteOperation.java @@ -0,0 +1,43 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2024 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.datajobs.models; + +import java.util.Map; + +/** + * Describes the write data job operation to be forwarded to dmi. + * + * @param path Identifier of a managed object (MO) on a network element. Defines the resource on which + * operation is executed. Typically, is Fully Distinguished Name (FDN). + * @param op Describes the operation to execute. The value can be as below: + * e.g. "add", "replace", "remove", "action" etc. + * @param moduleSetTag The module set tag of the CM Handle. + * @param value The value to be written depends on the type of operation. + * @param operationId Unique identifier of the operation within the request. + * @param privateProperties Contains the private properties of a Cm Handle. + */ +public record DmiWriteOperation( + String path, + String op, + String moduleSetTag, + Object value, + String operationId, + Map<String, String> privateProperties) {} diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/datajobs/models/ProducerKey.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/datajobs/models/ProducerKey.java new file mode 100644 index 0000000000..ac6b7f8622 --- /dev/null +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/datajobs/models/ProducerKey.java @@ -0,0 +1,36 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2024 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.datajobs.models; + +/** + * Composite key created from the DMI Service name and a data producer identifier. + * Helps to group of the sub job request for a given DMI Plugin. + * + * @param dmiServiceName Describes the name of the relevant DMI service. + * @param dataProducerIdentifier The name of a data producer identifier from a Cm Handle. + */ +public record ProducerKey(String dmiServiceName, String dataProducerIdentifier) { + + @Override + public String toString() { + return dmiServiceName + "#" + dataProducerIdentifier; + } +}
\ No newline at end of file diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/datajobs/models/SubJobWriteRequest.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/datajobs/models/SubJobWriteRequest.java new file mode 100644 index 0000000000..432b21b8c0 --- /dev/null +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/datajobs/models/SubJobWriteRequest.java @@ -0,0 +1,41 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2024 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.datajobs.models; + +import java.util.Collection; + +/** + * Response data for a write operation by the DMI Plugin. + * + * @param dataAcceptType Define the data response accept type. + * e.g. "application/vnd.3gpp.object-tree-hierarchical+json", + * "application/vnd.3gpp.object-tree-flat+json" etc. + * @param dataContentType Define the data request content type. + * e.g. "application/3gpp-json-patch+json" etc. + * @param dataProducerId Identifier of the data producer. + * + * @param data A collection of outgoing write operations. + */ +public record SubJobWriteRequest ( + String dataAcceptType, + String dataContentType, + String dataProducerId, + Collection<DmiWriteOperation> data) {}
\ No newline at end of file diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/datajobs/models/SubJobWriteResponse.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/datajobs/models/SubJobWriteResponse.java new file mode 100644 index 0000000000..9cdd8e0590 --- /dev/null +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/datajobs/models/SubJobWriteResponse.java @@ -0,0 +1,30 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2024 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.datajobs.models; + +/** + * Request data for a write operation towards DMI Plugin. + * + * @param subJobId Identifier of the sub-job from DMI. + * @param dmiServiceName The provided name of the DMI service from the request. + * @param dataProducerId Identifier of the data producer. + */ +public record SubJobWriteResponse(String subJobId, String dmiServiceName, String dataProducerId) {}
\ No newline at end of file diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/datajobs/DataJobServiceImpl.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/datajobs/DataJobServiceImpl.java index 7db6c5c272..56ed6e30da 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/datajobs/DataJobServiceImpl.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/datajobs/DataJobServiceImpl.java @@ -20,15 +20,27 @@ package org.onap.cps.ncmp.impl.datajobs; +import java.util.List; +import java.util.Map; +import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.onap.cps.ncmp.api.datajobs.DataJobService; import org.onap.cps.ncmp.api.datajobs.models.DataJobMetadata; import org.onap.cps.ncmp.api.datajobs.models.DataJobReadRequest; import org.onap.cps.ncmp.api.datajobs.models.DataJobWriteRequest; +import org.onap.cps.ncmp.api.datajobs.models.DmiWriteOperation; +import org.onap.cps.ncmp.api.datajobs.models.ProducerKey; +import org.onap.cps.ncmp.api.datajobs.models.SubJobWriteResponse; +import org.springframework.stereotype.Service; @Slf4j +@Service +@RequiredArgsConstructor public class DataJobServiceImpl implements DataJobService { + private final DmiSubJobRequestHandler dmiSubJobClient; + private final WriteRequestExaminer writeRequestExaminer; + @Override public void readDataJob(final String dataJobId, final DataJobMetadata dataJobMetadata, final DataJobReadRequest dataJobReadRequest) { @@ -36,8 +48,13 @@ public class DataJobServiceImpl implements DataJobService { } @Override - public void writeDataJob(final String dataJobId, final DataJobMetadata dataJobMetadata, - final DataJobWriteRequest dataJobWriteRequest) { + public List<SubJobWriteResponse> writeDataJob(final String dataJobId, final DataJobMetadata dataJobMetadata, + final DataJobWriteRequest dataJobWriteRequest) { log.info("data job id for write operation is: {}", dataJobId); + + final Map<ProducerKey, List<DmiWriteOperation>> dmiWriteOperationsPerProducerKey = + writeRequestExaminer.splitDmiWriteOperationsFromRequest(dataJobId, dataJobWriteRequest); + + return dmiSubJobClient.sendRequestsToDmi(dataJobId, dataJobMetadata, dmiWriteOperationsPerProducerKey); } } diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/datajobs/DmiSubJobRequestHandler.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/datajobs/DmiSubJobRequestHandler.java new file mode 100644 index 0000000000..69eadab26a --- /dev/null +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/datajobs/DmiSubJobRequestHandler.java @@ -0,0 +1,85 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2024 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.impl.datajobs; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.onap.cps.ncmp.api.datajobs.models.DataJobMetadata; +import org.onap.cps.ncmp.api.datajobs.models.DmiWriteOperation; +import org.onap.cps.ncmp.api.datajobs.models.ProducerKey; +import org.onap.cps.ncmp.api.datajobs.models.SubJobWriteRequest; +import org.onap.cps.ncmp.api.datajobs.models.SubJobWriteResponse; +import org.onap.cps.ncmp.api.impl.client.DmiRestClient; +import org.onap.cps.ncmp.api.impl.config.DmiProperties; +import org.onap.cps.ncmp.api.impl.operations.OperationType; +import org.onap.cps.ncmp.api.impl.operations.RequiredDmiService; +import org.onap.cps.ncmp.api.impl.utils.DmiServiceUrlBuilder; +import org.onap.cps.utils.JsonObjectMapper; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Service; + +@Slf4j +@Service +@RequiredArgsConstructor +public class DmiSubJobRequestHandler { + + private final DmiRestClient dmiRestClient; + private final DmiProperties dmiProperties; + private final JsonObjectMapper jsonObjectMapper; + static final String NO_AUTH_HEADER = null; + + /** + * Sends sub-job write requests to the DMI Plugin. + * + * @param dataJobId data ojb identifier + * @param dataJobMetadata the data job's metadata + * @param dmiWriteOperationsPerProducerKey a collection of write requests per producer key. + * @return a list of sub-job write responses + */ + public List<SubJobWriteResponse> sendRequestsToDmi(final String dataJobId, final DataJobMetadata dataJobMetadata, + final Map<ProducerKey, List<DmiWriteOperation>> dmiWriteOperationsPerProducerKey) { + final List<SubJobWriteResponse> subJobWriteResponses = new ArrayList<>(dmiWriteOperationsPerProducerKey.size()); + dmiWriteOperationsPerProducerKey.forEach((producerKey, dmi3ggpWriteOperations) -> { + final SubJobWriteRequest subJobWriteRequest = new SubJobWriteRequest(dataJobMetadata.dataAcceptType(), + dataJobMetadata.dataContentType(), dataJobId, dmi3ggpWriteOperations); + + final String dmiResourceUrl = getDmiResourceUrl(dataJobId, producerKey); + final ResponseEntity<Object> responseEntity = dmiRestClient.postOperationWithJsonData( + RequiredDmiService.DATA, + dmiResourceUrl, + jsonObjectMapper.asJsonString(subJobWriteRequest), + OperationType.CREATE, + NO_AUTH_HEADER); + final SubJobWriteResponse subJobWriteResponse = (SubJobWriteResponse) responseEntity.getBody(); + log.debug("Sub job write response: {}", subJobWriteResponse); + subJobWriteResponses.add(subJobWriteResponse); + }); + return subJobWriteResponses; + } + + private String getDmiResourceUrl(final String dataJobId, final ProducerKey producerKey) { + return DmiServiceUrlBuilder.newInstance().pathSegment("writeJob").variablePathSegment("requestId", dataJobId) + .build(producerKey.dmiServiceName(), dmiProperties.getDmiBasePath()); + } +}
\ No newline at end of file diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/datajobs/WriteRequestExaminer.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/datajobs/WriteRequestExaminer.java new file mode 100644 index 0000000000..1c3e476c8b --- /dev/null +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/datajobs/WriteRequestExaminer.java @@ -0,0 +1,109 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2024 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.impl.datajobs; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.onap.cps.ncmp.api.datajobs.models.DataJobWriteRequest; +import org.onap.cps.ncmp.api.datajobs.models.DmiWriteOperation; +import org.onap.cps.ncmp.api.datajobs.models.ProducerKey; +import org.onap.cps.ncmp.api.datajobs.models.WriteOperation; +import org.onap.cps.ncmp.api.impl.utils.YangDataConverter; +import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle; +import org.onap.cps.ncmp.utils.AlternateIdMatcher; +import org.onap.cps.spi.model.DataNode; +import org.springframework.stereotype.Service; + +@Slf4j +@Service +@RequiredArgsConstructor +public class WriteRequestExaminer { + + private final AlternateIdMatcher alternateIdMatcher; + private static final String PATH_SEPARATOR = "/"; + + /** + * Splitting incoming data job write request into Dmi Write Operations by ProducerKey. + * + * @param dataJobId data job identifier + * @param dataJobWriteRequest incoming data job write request + * @return {@code Map} map of Dmi Write Operations per Producer Key + */ + public Map<ProducerKey, List<DmiWriteOperation>> splitDmiWriteOperationsFromRequest( + final String dataJobId, + final DataJobWriteRequest dataJobWriteRequest) { + final Map<ProducerKey, List<DmiWriteOperation>> dmiWriteOperationsPerProducerKey = new HashMap<>(); + for (final WriteOperation writeOperation : dataJobWriteRequest.data()) { + examineWriteOperation(dataJobId, dmiWriteOperationsPerProducerKey, writeOperation); + } + return dmiWriteOperationsPerProducerKey; + } + + private void examineWriteOperation(final String dataJobId, + final Map<ProducerKey, List<DmiWriteOperation>> dmiWriteOperationsPerProducerKey, + final WriteOperation writeOperation) { + log.debug("data job id for write operation is: {}", dataJobId); + final DataNode dataNode = alternateIdMatcher + .getCmHandleDataNodeByLongestMatchingAlternateId(writeOperation.path(), PATH_SEPARATOR); + + final DmiWriteOperation dmiWriteOperation = createDmiWriteOperation(writeOperation, dataNode); + + final ProducerKey producerKey = createProducerKey(dataNode); + final List<DmiWriteOperation> dmiWriteOperations; + if (dmiWriteOperationsPerProducerKey.containsKey(producerKey)) { + dmiWriteOperations = dmiWriteOperationsPerProducerKey.get(producerKey); + } else { + dmiWriteOperations = new ArrayList<>(); + dmiWriteOperationsPerProducerKey.put(producerKey, dmiWriteOperations); + } + dmiWriteOperations.add(dmiWriteOperation); + } + + private ProducerKey createProducerKey(final DataNode dataNode) { + return new ProducerKey((String) dataNode.getLeaves().get("dmi-service-name"), + (String) dataNode.getLeaves().get("data-producer-identifier")); + } + + private DmiWriteOperation createDmiWriteOperation(final WriteOperation writeOperation, + final DataNode dataNode) { + return new DmiWriteOperation( + writeOperation.path(), + writeOperation.op(), + (String) dataNode.getLeaves().get("module-set-tag"), + writeOperation.value(), + writeOperation.operationId(), + getPrivatePropertiesFromDataNode(dataNode)); + } + + private Map<String, String> getPrivatePropertiesFromDataNode(final DataNode dataNode) { + final YangModelCmHandle yangModelCmHandle = YangDataConverter.convertCmHandleToYangModel(dataNode); + final Map<String, String> cmHandleDmiProperties = new LinkedHashMap<>(); + yangModelCmHandle.getDmiProperties() + .forEach(dmiProperty -> cmHandleDmiProperties.put(dmiProperty.getName(), dmiProperty.getValue())); + return cmHandleDmiProperties; + } + +}
\ No newline at end of file diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/utils/AlternateIdMatcher.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/utils/AlternateIdMatcher.java index 8385f19f76..3fad684437 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/utils/AlternateIdMatcher.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/utils/AlternateIdMatcher.java @@ -44,7 +44,7 @@ public class AlternateIdMatcher { * @param separator a string that separates each element from the next. * @return data node */ - public DataNode getCmHandleDataNodeByLongestMatchAlternateId(final String alternateId, final String separator) { + public DataNode getCmHandleDataNodeByLongestMatchingAlternateId(final String alternateId, final String separator) { String bestMatch = alternateId; while (StringUtils.isNotEmpty(bestMatch)) { try { |