2 * ============LICENSE_START=======================================================
3 * Copyright (c) 2021 Bell Canada.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 * ============LICENSE_END=========================================================
19 package org.onap.cps.notification;
21 import lombok.extern.slf4j.Slf4j;
22 import org.springframework.beans.factory.annotation.Autowired;
23 import org.springframework.beans.factory.annotation.Value;
24 import org.springframework.stereotype.Service;
28 public class NotificationService {
30 private boolean dataUpdatedEventNotificationEnabled;
31 private NotificationPublisher notificationPublisher;
32 private CpsDataUpdatedEventFactory cpsDataUpdatedEventFactory;
33 private NotificationErrorHandler notificationErrorHandler;
36 * Create an instance of Notification Subscriber.
38 * @param dataUpdatedEventNotificationEnabled notification can be enabled by setting
39 * 'notification.data-updated.enabled=true' in application properties
40 * @param notificationPublisher notification Publisher
41 * @param cpsDataUpdatedEventFactory to create CPSDataUpdatedEvent
42 * @param notificationErrorHandler error handler
45 public NotificationService(
46 @Value("${notification.data-updated.enabled}") final boolean dataUpdatedEventNotificationEnabled,
47 final NotificationPublisher notificationPublisher,
48 final CpsDataUpdatedEventFactory cpsDataUpdatedEventFactory,
49 final NotificationErrorHandler notificationErrorHandler) {
50 this.dataUpdatedEventNotificationEnabled = dataUpdatedEventNotificationEnabled;
51 this.notificationPublisher = notificationPublisher;
52 this.cpsDataUpdatedEventFactory = cpsDataUpdatedEventFactory;
53 this.notificationErrorHandler = notificationErrorHandler;
57 * Process Data Updated Event and publishes the notification.
59 * @param dataspaceName dataspace name
60 * @param anchorName anchor name
62 public void processDataUpdatedEvent(final String dataspaceName, final String anchorName) {
63 log.debug("process data updated event for dataspace '{}' & anchor '{}'", dataspaceName, anchorName);
65 if (shouldSendNotification()) {
66 final var cpsDataUpdatedEvent =
67 cpsDataUpdatedEventFactory.createCpsDataUpdatedEvent(dataspaceName, anchorName);
68 log.debug("data updated event to be published {}", cpsDataUpdatedEvent);
69 notificationPublisher.sendNotification(cpsDataUpdatedEvent);
71 } catch (final Exception exception) {
72 /* All the exceptions are handled to not to propagate it to caller.
73 CPS operation should not fail if sending event fails for any reason.
75 notificationErrorHandler.onException("Failed to process cps-data-updated-event.",
76 exception, dataspaceName, anchorName);
81 Add more complex rules based on dataspace and anchor later
83 private boolean shouldSendNotification() {
84 return dataUpdatedEventNotificationEnabled;