diff options
Diffstat (limited to 'src/main')
7 files changed, 112 insertions, 7 deletions
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<NetworkData> 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<NetworkData> 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 @@ <include file="db/changelog/schema/01-init-schema.xml"/> <include file="db/changelog/data/02-init-data.xml"/> <include file="db/changelog/schema/03-rename-network-data-timestamp-fields.xml"/> - + <include file="db/changelog/schema/04-added-operation-field-in-network-data.xml"/> </databaseChangeLog> 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 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + ============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========================================================= +--> + +<databaseChangeLog + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xmlns="http://www.liquibase.org/xml/ns/dbchangelog" + xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog + http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.3.xsd"> + + <changeSet id="4.1" author="cps"> + <comment>Add operation field in network data timescale table + and set default 'UPDATE' value for existing data + </comment> + <addColumn tableName="network_data"> + <column name="operation" type="VARCHAR(20)" + remarks="Field to store the operation type"> + </column> + </addColumn> + <update tableName="network_data"> + <column name="operation" value="UPDATE"/> + <where>operation is NULL</where> + </update> + <rollback> + <dropColumn tableName="network_data"> + <column name="operation"/> + </dropColumn> + </rollback> + </changeSet> + <changeSet id="4.2" author="cps"> + <comment>Remove not null constraint from payload to support delete operation</comment> + <dropNotNullConstraint tableName="network_data" columnName="payload"/> + </changeSet> +</databaseChangeLog> |