From 60f41161ac9589dfaf0000587f216cae0e734142 Mon Sep 17 00:00:00 2001 From: Renu Kumari Date: Wed, 8 Dec 2021 11:16:58 -0500 Subject: Add support for delete operation - Added new column operation in DB - Updated existing rows to have 'UPDATE' value for operation field - Added ability to process both V1 and V2 event schema - Changed code and testcase to support operation field Issue-ID: CPS-790 Signed-off-by: Renu Kumari Change-Id: Ife24daa4b442e1499094b162727cc8704c25011e --- .../listener/kafka/DataUpdatedEventListener.java | 4 +- .../event/model/CpsDataUpdatedEventMapper.java | 7 +++ .../org/onap/cps/temporal/domain/NetworkData.java | 10 ++++- .../org/onap/cps/temporal/domain/Operation.java | 27 ++++++++++++ .../temporal/service/NetworkDataServiceImpl.java | 18 ++++++-- .../resources/db/changelog/changelog-master.xml | 2 +- .../04-added-operation-field-in-network-data.xml | 51 ++++++++++++++++++++++ 7 files changed, 112 insertions(+), 7 deletions(-) create mode 100644 src/main/java/org/onap/cps/temporal/domain/Operation.java create mode 100644 src/main/resources/db/changelog/schema/04-added-operation-field-in-network-data.xml (limited to 'src/main') diff --git a/src/main/java/org/onap/cps/temporal/controller/event/listener/kafka/DataUpdatedEventListener.java b/src/main/java/org/onap/cps/temporal/controller/event/listener/kafka/DataUpdatedEventListener.java index 5fce94e..2ae675e 100644 --- a/src/main/java/org/onap/cps/temporal/controller/event/listener/kafka/DataUpdatedEventListener.java +++ b/src/main/java/org/onap/cps/temporal/controller/event/listener/kafka/DataUpdatedEventListener.java @@ -6,13 +6,15 @@ * 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 + * 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========================================================= */ diff --git a/src/main/java/org/onap/cps/temporal/controller/event/model/CpsDataUpdatedEventMapper.java b/src/main/java/org/onap/cps/temporal/controller/event/model/CpsDataUpdatedEventMapper.java index d180509..cb06d5f 100644 --- a/src/main/java/org/onap/cps/temporal/controller/event/model/CpsDataUpdatedEventMapper.java +++ b/src/main/java/org/onap/cps/temporal/controller/event/model/CpsDataUpdatedEventMapper.java @@ -25,10 +25,12 @@ import com.fasterxml.jackson.databind.ObjectMapper; import java.time.OffsetDateTime; import org.mapstruct.Mapper; import org.mapstruct.Mapping; +import org.onap.cps.event.model.Content; import org.onap.cps.event.model.CpsDataUpdatedEvent; import org.onap.cps.event.model.Data; import org.onap.cps.temporal.controller.utils.DateTimeUtility; import org.onap.cps.temporal.domain.NetworkData; +import org.onap.cps.temporal.domain.Operation; /** * Mapper for data updated event schema. @@ -43,6 +45,7 @@ public abstract class CpsDataUpdatedEventMapper { @Mapping(source = "content.schemaSetName", target = "schemaSet") @Mapping(source = "content.anchorName", target = "anchor") @Mapping(source = "content.data", target = "payload") + @Mapping(source = "content.operation", target = "operation") @Mapping(expression = "java(null)", target = "createdTimestamp") public abstract NetworkData eventToEntity(CpsDataUpdatedEvent cpsDataUpdatedEvent); @@ -50,6 +53,10 @@ public abstract class CpsDataUpdatedEventMapper { return data != null ? objectMapper.writeValueAsString(data) : null; } + Operation map(final Content.Operation inputOperation) { + return inputOperation == null ? Operation.UPDATE : Operation.valueOf(inputOperation.toString()); + } + OffsetDateTime map(final String timestamp) { return DateTimeUtility.toOffsetDateTime(timestamp); } diff --git a/src/main/java/org/onap/cps/temporal/domain/NetworkData.java b/src/main/java/org/onap/cps/temporal/domain/NetworkData.java index 1537e4a..e147871 100644 --- a/src/main/java/org/onap/cps/temporal/domain/NetworkData.java +++ b/src/main/java/org/onap/cps/temporal/domain/NetworkData.java @@ -6,13 +6,15 @@ * 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 + * 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========================================================= */ @@ -23,6 +25,8 @@ import java.io.Serializable; import java.time.OffsetDateTime; import javax.persistence.Column; import javax.persistence.Entity; +import javax.persistence.EnumType; +import javax.persistence.Enumerated; import javax.persistence.Id; import javax.persistence.IdClass; import javax.persistence.Table; @@ -70,6 +74,10 @@ public class NetworkData implements Serializable { private String schemaSet; @NotNull + @Column(updatable = false) + @Enumerated(EnumType.STRING) + private Operation operation; + @Type(type = "jsonb") @Column(columnDefinition = "jsonb", updatable = false) private String payload; diff --git a/src/main/java/org/onap/cps/temporal/domain/Operation.java b/src/main/java/org/onap/cps/temporal/domain/Operation.java new file mode 100644 index 0000000..06b5099 --- /dev/null +++ b/src/main/java/org/onap/cps/temporal/domain/Operation.java @@ -0,0 +1,27 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (c) 2021 Bell Canada. + * ================================================================================ + * 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.temporal.domain; + +public enum Operation { + CREATE, + UPDATE, + DELETE +} diff --git a/src/main/java/org/onap/cps/temporal/service/NetworkDataServiceImpl.java b/src/main/java/org/onap/cps/temporal/service/NetworkDataServiceImpl.java index 3eba6fb..8d282b4 100644 --- a/src/main/java/org/onap/cps/temporal/service/NetworkDataServiceImpl.java +++ b/src/main/java/org/onap/cps/temporal/service/NetworkDataServiceImpl.java @@ -25,6 +25,7 @@ import javax.validation.ValidationException; import lombok.extern.slf4j.Slf4j; import org.onap.cps.temporal.domain.NetworkData; import org.onap.cps.temporal.domain.NetworkDataId; +import org.onap.cps.temporal.domain.Operation; import org.onap.cps.temporal.domain.SearchCriteria; import org.onap.cps.temporal.repository.NetworkDataRepository; import org.springframework.beans.factory.annotation.Value; @@ -42,26 +43,35 @@ public class NetworkDataServiceImpl implements NetworkDataService { private final int maxPageSize; public NetworkDataServiceImpl(final NetworkDataRepository networkDataRepository, - final @Value("${app.query.response.max-page-size}") int maxPageSize) { + final @Value("${app.query.response.max-page-size}") int maxPageSize) { this.networkDataRepository = networkDataRepository; this.maxPageSize = maxPageSize; } @Override public NetworkData addNetworkData(final NetworkData networkData) { + validateNetworkData(networkData); final var savedNetworkData = networkDataRepository.save(networkData); if (savedNetworkData.getCreatedTimestamp() == null) { // Data already exists and can not be inserted final var id = - new NetworkDataId( - networkData.getObservedTimestamp(), networkData.getDataspace(), networkData.getAnchor()); + new NetworkDataId( + networkData.getObservedTimestamp(), networkData.getDataspace(), networkData.getAnchor()); final Optional existingNetworkData = networkDataRepository.findById(id); throw new ServiceException( - "Failed to create network data. It already exists: " + (existingNetworkData.orElse(null))); + "Failed to create network data. It already exists: " + (existingNetworkData.orElse(null))); } return savedNetworkData; } + private void validateNetworkData(final NetworkData networkData) { + if (networkData.getOperation() != Operation.DELETE + && networkData.getPayload() == null) { + throw new ValidationException( + String.format("The operation %s must not have null payload", networkData.getOperation())); + } + } + @Override public Slice searchNetworkData(final SearchCriteria searchCriteria) { if (searchCriteria.getPageable().getPageSize() > maxPageSize) { diff --git a/src/main/resources/db/changelog/changelog-master.xml b/src/main/resources/db/changelog/changelog-master.xml index 6ec36fb..7986d5e 100644 --- a/src/main/resources/db/changelog/changelog-master.xml +++ b/src/main/resources/db/changelog/changelog-master.xml @@ -26,5 +26,5 @@ - + diff --git a/src/main/resources/db/changelog/schema/04-added-operation-field-in-network-data.xml b/src/main/resources/db/changelog/schema/04-added-operation-field-in-network-data.xml new file mode 100644 index 0000000..62b93b9 --- /dev/null +++ b/src/main/resources/db/changelog/schema/04-added-operation-field-in-network-data.xml @@ -0,0 +1,51 @@ + + + + + + + Add operation field in network data timescale table + and set default 'UPDATE' value for existing data + + + + + + + + operation is NULL + + + + + + + + + Remove not null constraint from payload to support delete operation + + + -- cgit 1.2.3-korg