X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=cps-service%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fcps%2Fnotification%2FCpsDataUpdatedEventFactory.java;h=1013c13b76b4ba04ae052e6dd4473819410b9466;hb=ebfa4077b2e462237301e93566fed6ef2f56674c;hp=ae5e9ba290c9ab828310b1347738045b8aea695a;hpb=3142122f6365a75defec12c945061971e22efd93;p=cps.git diff --git a/cps-service/src/main/java/org/onap/cps/notification/CpsDataUpdatedEventFactory.java b/cps-service/src/main/java/org/onap/cps/notification/CpsDataUpdatedEventFactory.java index ae5e9ba29..1013c13b7 100644 --- a/cps-service/src/main/java/org/onap/cps/notification/CpsDataUpdatedEventFactory.java +++ b/cps-service/src/main/java/org/onap/cps/notification/CpsDataUpdatedEventFactory.java @@ -1,20 +1,21 @@ /* * ============LICENSE_START======================================================= - * Copyright (C) 2021 Bell Canada. All rights reserved. - * ================================================================================ - * 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 + * Copyright (c) 2021-2022 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. + * http://www.apache.org/licenses/LICENSE-2.0 * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= + * 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.notification; @@ -24,28 +25,31 @@ import java.net.URISyntaxException; import java.time.OffsetDateTime; import java.time.format.DateTimeFormatter; import java.util.UUID; -import org.onap.cps.api.CpsAdminService; +import lombok.AllArgsConstructor; import org.onap.cps.api.CpsDataService; import org.onap.cps.event.model.Content; import org.onap.cps.event.model.CpsDataUpdatedEvent; -import org.onap.cps.event.model.CpsDataUpdatedEvent.Schema; import org.onap.cps.event.model.Data; import org.onap.cps.spi.FetchDescendantsOption; import org.onap.cps.spi.model.Anchor; import org.onap.cps.spi.model.DataNode; import org.onap.cps.utils.DataMapUtils; +import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Component; @Component -class CpsDataUpdatedEventFactory { +@AllArgsConstructor(onConstructor = @__(@Lazy)) +public class CpsDataUpdatedEventFactory { + private static final URI EVENT_SCHEMA; private static final URI EVENT_SOURCE; private static final String EVENT_TYPE = "org.onap.cps.data-updated-event"; - private static final DateTimeFormatter dateTimeFormatter = + private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ"); - static { + static { try { + EVENT_SCHEMA = new URI("urn:cps:org.onap.cps:data-updated-event-schema:v1"); EVENT_SOURCE = new URI("urn:cps:org.onap.cps"); } catch (final URISyntaxException e) { // As it is fixed string, I don't expect to see this error @@ -53,26 +57,31 @@ class CpsDataUpdatedEventFactory { } } - private CpsDataService cpsDataService; - private CpsAdminService cpsAdminService; - - public CpsDataUpdatedEventFactory(final CpsDataService cpsDataService, final CpsAdminService cpsAdminService) { - this.cpsDataService = cpsDataService; - this.cpsAdminService = cpsAdminService; - } + @Lazy + private final CpsDataService cpsDataService; - CpsDataUpdatedEvent createCpsDataUpdatedEvent(final String dataspaceName, final String anchorName) { - final var dataNode = cpsDataService - .getDataNode(dataspaceName, anchorName, "/", FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS); - final var anchor = cpsAdminService.getAnchor(dataspaceName, anchorName); - return toCpsDataUpdatedEvent(anchor, dataNode); + /** + * Generates CPS Data Updated event. If observedTimestamp is not provided, then current timestamp is used. + * + * @param anchor anchor + * @param observedTimestamp observedTimestamp + * @param operation operation + * @return CpsDataUpdatedEvent + */ + public CpsDataUpdatedEvent createCpsDataUpdatedEvent(final Anchor anchor, + final OffsetDateTime observedTimestamp, final Operation operation) { + final var dataNode = (operation == Operation.DELETE) ? null : + cpsDataService.getDataNode(anchor.getDataspaceName(), anchor.getName(), + "/", FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS); + return toCpsDataUpdatedEvent(anchor, dataNode, observedTimestamp, operation); } - private CpsDataUpdatedEvent toCpsDataUpdatedEvent(final Anchor anchor, final DataNode dataNode) { + private CpsDataUpdatedEvent toCpsDataUpdatedEvent(final Anchor anchor, final DataNode dataNode, + final OffsetDateTime observedTimestamp, final Operation operation) { final var cpsDataUpdatedEvent = new CpsDataUpdatedEvent(); - cpsDataUpdatedEvent.withContent(createContent(anchor, dataNode)); + cpsDataUpdatedEvent.withContent(createContent(anchor, dataNode, observedTimestamp, operation)); cpsDataUpdatedEvent.withId(UUID.randomUUID().toString()); - cpsDataUpdatedEvent.withSchema(Schema.URN_CPS_ORG_ONAP_CPS_DATA_UPDATED_EVENT_SCHEMA_1_1_0_SNAPSHOT); + cpsDataUpdatedEvent.withSchema(EVENT_SCHEMA); cpsDataUpdatedEvent.withSource(EVENT_SOURCE); cpsDataUpdatedEvent.withType(EVENT_TYPE); return cpsDataUpdatedEvent; @@ -80,17 +89,22 @@ class CpsDataUpdatedEventFactory { private Data createData(final DataNode dataNode) { final var data = new Data(); - DataMapUtils.toDataMap(dataNode).forEach((key, value) -> data.setAdditionalProperty(key, value)); + DataMapUtils.toDataMapWithIdentifier(dataNode).forEach(data::setAdditionalProperty); return data; } - private Content createContent(final Anchor anchor, final DataNode dataNode) { + private Content createContent(final Anchor anchor, final DataNode dataNode, + final OffsetDateTime observedTimestamp, final Operation operation) { final var content = new Content(); content.withAnchorName(anchor.getName()); content.withDataspaceName(anchor.getDataspaceName()); content.withSchemaSetName(anchor.getSchemaSetName()); - content.withData(createData(dataNode)); - content.withObservedTimestamp(dateTimeFormatter.format(OffsetDateTime.now())); + content.withOperation(Content.Operation.fromValue(operation.name())); + content.withObservedTimestamp( + DATE_TIME_FORMATTER.format(observedTimestamp == null ? OffsetDateTime.now() : observedTimestamp)); + if (dataNode != null) { + content.withData(createData(dataNode)); + } return content; } }