2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2024 TechMahindra Ltd.
4 * Copyright (C) 2024 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.cps.events;
24 import io.cloudevents.CloudEvent;
25 import io.micrometer.core.annotation.Timed;
26 import java.time.OffsetDateTime;
27 import java.util.HashMap;
29 import lombok.RequiredArgsConstructor;
30 import lombok.extern.slf4j.Slf4j;
31 import org.onap.cps.events.model.CpsDataUpdatedEvent;
32 import org.onap.cps.events.model.Data;
33 import org.onap.cps.events.model.Data.Operation;
34 import org.onap.cps.spi.model.Anchor;
35 import org.onap.cps.utils.DateTimeUtility;
36 import org.springframework.beans.factory.annotation.Value;
37 import org.springframework.stereotype.Service;
41 @RequiredArgsConstructor
42 public class CpsDataUpdateEventsService {
44 private final EventsPublisher<CpsDataUpdatedEvent> eventsPublisher;
46 @Value("${app.cps.data-updated.topic:cps-data-updated-events}")
47 private String topicName;
49 @Value("${app.cps.data-updated.change-event-notifications-enabled:true}")
50 private boolean cpsChangeEventNotificationsEnabled;
52 @Value("${notification.enabled:false}")
53 private boolean notificationsEnabled;
56 * Publish the cps data update event with header to the public topic.
58 * @param anchor Anchor of the updated data
59 * @param xpath xpath of the updated data
60 * @param operation operation performed on the data
61 * @param observedTimestamp timestamp when data was updated.
63 @Timed(value = "cps.dataupdate.events.publish", description = "Time taken to publish Data Update event")
64 public void publishCpsDataUpdateEvent(final Anchor anchor, final String xpath,
65 final Operation operation, final OffsetDateTime observedTimestamp) {
66 if (notificationsEnabled && cpsChangeEventNotificationsEnabled) {
67 final CpsDataUpdatedEvent cpsDataUpdatedEvent = createCpsDataUpdatedEvent(anchor,
68 observedTimestamp, xpath, operation);
69 final String updateEventId = anchor.getDataspaceName() + ":" + anchor.getName();
70 final Map<String, String> extensions = createUpdateEventExtensions(updateEventId);
71 final CloudEvent cpsDataUpdatedEventAsCloudEvent =
72 CpsEvent.builder().type(CpsDataUpdatedEvent.class.getTypeName()).data(cpsDataUpdatedEvent)
73 .extensions(extensions).build().asCloudEvent();
74 eventsPublisher.publishCloudEvent(topicName, updateEventId, cpsDataUpdatedEventAsCloudEvent);
76 log.debug("State of Overall Notifications : {} and Cps Change Event Notifications : {}",
77 notificationsEnabled, cpsChangeEventNotificationsEnabled);
81 private CpsDataUpdatedEvent createCpsDataUpdatedEvent(final Anchor anchor, final OffsetDateTime observedTimestamp,
83 final Operation rootNodeOperation) {
84 final CpsDataUpdatedEvent cpsDataUpdatedEvent = new CpsDataUpdatedEvent();
85 final Data updateEventData = new Data();
86 updateEventData.setObservedTimestamp(DateTimeUtility.toString(observedTimestamp));
87 updateEventData.setDataspaceName(anchor.getDataspaceName());
88 updateEventData.setAnchorName(anchor.getName());
89 updateEventData.setSchemaSetName(anchor.getSchemaSetName());
90 updateEventData.setOperation(getRootNodeOperation(xpath, rootNodeOperation));
91 updateEventData.setXpath(xpath);
92 cpsDataUpdatedEvent.setData(updateEventData);
93 return cpsDataUpdatedEvent;
96 private Map<String, String> createUpdateEventExtensions(final String eventKey) {
97 final Map<String, String> extensions = new HashMap<>();
98 extensions.put("correlationid", eventKey);
102 private Operation getRootNodeOperation(final String xpath, final Operation operation) {
103 return isRootXpath(xpath) || isRootContainerNodeXpath(xpath) ? operation : Operation.UPDATE;
106 private static boolean isRootXpath(final String xpath) {
107 return "/".equals(xpath) || "".equals(xpath);
110 private static boolean isRootContainerNodeXpath(final String xpath) {
111 return 0 == xpath.lastIndexOf('/');