Merge "Addition of missing license to INFO.yaml"
[cps.git] / cps-service / src / main / java / org / onap / cps / notification / NotificationService.java
1
2 /*
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
9  *
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.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.notification;
22
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;
27
28 @Service
29 @Slf4j
30 public class NotificationService {
31
32     private boolean dataUpdatedEventNotificationEnabled;
33     private NotificationPublisher notificationPublisher;
34     private CpsDataUpdatedEventFactory cpsDataUpdatedEventFactory;
35     private NotificationErrorHandler notificationErrorHandler;
36
37     /**
38      * Create an instance of Notification Subscriber.
39      *
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
45      */
46     @Autowired
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;
56     }
57
58     /**
59      * Process Data Updated Event and publishes the notification.
60      *
61      * @param dataspaceName dataspace name
62      * @param anchorName    anchor name
63      */
64     public void processDataUpdatedEvent(final String dataspaceName, final String anchorName) {
65         log.debug("process data updated event for dataspace '{}' & anchor '{}'", dataspaceName, anchorName);
66         try {
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);
72             }
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.
76              */
77             notificationErrorHandler.onException("Failed to process cps-data-updated-event.",
78                 exception, dataspaceName, anchorName);
79         }
80     }
81
82     /*
83         Add more complex rules based on dataspace and anchor later
84      */
85     private boolean shouldSendNotification() {
86         return dataUpdatedEventNotificationEnabled;
87     }
88
89 }