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("${notification.enabled:false}")
50 private boolean notificationsEnabled;
53 * Publish the cps data update event with header to the public topic.
55 * @param anchor Anchor of the updated data
56 * @param xpath xpath of the updated data
57 * @param operation operation performed on the data
58 * @param observedTimestamp timestamp when data was updated.
60 @Timed(value = "cps.dataupdate.events.publish", description = "Time taken to publish Data Update event")
61 public void publishCpsDataUpdateEvent(final Anchor anchor, final String xpath,
62 final Operation operation, final OffsetDateTime observedTimestamp) {
63 if (notificationsEnabled) {
64 final CpsDataUpdatedEvent cpsDataUpdatedEvent = createCpsDataUpdatedEvent(anchor,
65 observedTimestamp, xpath, operation);
66 final String updateEventId = anchor.getDataspaceName() + ":" + anchor.getName();
67 final Map<String, String> extensions = createUpdateEventExtensions(updateEventId);
68 final CloudEvent cpsDataUpdatedEventAsCloudEvent =
69 CpsEvent.builder().type(CpsDataUpdatedEvent.class.getTypeName()).data(cpsDataUpdatedEvent)
70 .extensions(extensions).build().asCloudEvent();
71 eventsPublisher.publishCloudEvent(topicName, updateEventId, cpsDataUpdatedEventAsCloudEvent);
73 log.debug("Notifications disabled.");
77 private CpsDataUpdatedEvent createCpsDataUpdatedEvent(final Anchor anchor, final OffsetDateTime observedTimestamp,
79 final Operation rootNodeOperation) {
80 final CpsDataUpdatedEvent cpsDataUpdatedEvent = new CpsDataUpdatedEvent();
81 final Data updateEventData = new Data();
82 updateEventData.setObservedTimestamp(DateTimeUtility.toString(observedTimestamp));
83 updateEventData.setDataspaceName(anchor.getDataspaceName());
84 updateEventData.setAnchorName(anchor.getName());
85 updateEventData.setSchemaSetName(anchor.getSchemaSetName());
86 updateEventData.setOperation(getRootNodeOperation(xpath, rootNodeOperation));
87 updateEventData.setXpath(xpath);
88 cpsDataUpdatedEvent.setData(updateEventData);
89 return cpsDataUpdatedEvent;
92 private Map<String, String> createUpdateEventExtensions(final String eventKey) {
93 final Map<String, String> extensions = new HashMap<>();
94 extensions.put("correlationid", eventKey);
98 private Operation getRootNodeOperation(final String xpath, final Operation operation) {
99 return isRootXpath(xpath) || isRootContainerNodeXpath(xpath) ? operation : Operation.UPDATE;
102 private static boolean isRootXpath(final String xpath) {
103 return "/".equals(xpath) || "".equals(xpath);
106 private static boolean isRootContainerNodeXpath(final String xpath) {
107 return 0 == xpath.lastIndexOf('/');