3 * ============LICENSE_START=======================================================
4 * Copyright (C) 2021 Bell Canada. All rights reserved.
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
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.notification;
23 import lombok.extern.slf4j.Slf4j;
24 import org.springframework.beans.factory.annotation.Autowired;
25 import org.springframework.beans.factory.annotation.Value;
26 import org.springframework.stereotype.Service;
30 public class NotificationService {
32 private boolean dataUpdatedEventNotificationEnabled;
33 private NotificationPublisher notificationPublisher;
34 private CpsDataUpdatedEventFactory cpsDataUpdatedEventFactory;
35 private NotificationErrorHandler notificationErrorHandler;
38 * Create an instance of Notification Subscriber.
40 * @param dataUpdatedEventNotificationEnabled notification can be enabled by setting
41 * 'notification.data-updated.enabled=true' in application properties
42 * @param notificationPublisher notification Publisher
43 * @param cpsDataUpdatedEventFactory to create CPSDataUpdatedEvent
44 * @param notificationErrorHandler error handler
47 public NotificationService(
48 @Value("${notification.data-updated.enabled}") final boolean dataUpdatedEventNotificationEnabled,
49 final NotificationPublisher notificationPublisher,
50 final CpsDataUpdatedEventFactory cpsDataUpdatedEventFactory,
51 final NotificationErrorHandler notificationErrorHandler) {
52 this.dataUpdatedEventNotificationEnabled = dataUpdatedEventNotificationEnabled;
53 this.notificationPublisher = notificationPublisher;
54 this.cpsDataUpdatedEventFactory = cpsDataUpdatedEventFactory;
55 this.notificationErrorHandler = notificationErrorHandler;
59 * Process Data Updated Event and publishes the notification.
61 * @param dataspaceName dataspace name
62 * @param anchorName anchor name
64 public void processDataUpdatedEvent(final String dataspaceName, final String anchorName) {
65 log.debug("process data updated event for dataspace '{}' & anchor '{}'", dataspaceName, anchorName);
67 if (shouldSendNotification()) {
68 final var cpsDataUpdatedEvent =
69 cpsDataUpdatedEventFactory.createCpsDataUpdatedEvent(dataspaceName, anchorName);
70 log.debug("data updated event to be published {}", cpsDataUpdatedEvent);
71 notificationPublisher.sendNotification(cpsDataUpdatedEvent);
73 } catch (final Exception exception) {
74 /* All the exceptions are handled to not to propagate it to caller.
75 CPS operation should not fail if sending event fails for any reason.
77 notificationErrorHandler.onException("Failed to process cps-data-updated-event.",
78 exception, dataspaceName, anchorName);
83 Add more complex rules based on dataspace and anchor later
85 private boolean shouldSendNotification() {
86 return dataUpdatedEventNotificationEnabled;