Merge "Support concurrent requests to create schema sets"
[cps.git] / cps-service / src / main / java / org / onap / cps / notification / NotificationService.java
1 /*
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
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
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=========================================================
17  */
18
19 package org.onap.cps.notification;
20
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;
25
26 @Service
27 @Slf4j
28 public class NotificationService {
29
30     private boolean dataUpdatedEventNotificationEnabled;
31     private NotificationPublisher notificationPublisher;
32     private CpsDataUpdatedEventFactory cpsDataUpdatedEventFactory;
33     private NotificationErrorHandler notificationErrorHandler;
34
35     /**
36      * Create an instance of Notification Subscriber.
37      *
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
43      */
44     @Autowired
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;
54     }
55
56     /**
57      * Process Data Updated Event and publishes the notification.
58      *
59      * @param dataspaceName dataspace name
60      * @param anchorName    anchor name
61      */
62     public void processDataUpdatedEvent(final String dataspaceName, final String anchorName) {
63         log.debug("process data updated event for dataspace '{}' & anchor '{}'", dataspaceName, anchorName);
64         try {
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);
70             }
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.
74              */
75             notificationErrorHandler.onException("Failed to process cps-data-updated-event.",
76                 exception, dataspaceName, anchorName);
77         }
78     }
79
80     /*
81         Add more complex rules based on dataspace and anchor later
82      */
83     private boolean shouldSendNotification() {
84         return dataUpdatedEventNotificationEnabled;
85     }
86
87 }