Merge "Filter data updated events based on configured pattern"
[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 java.util.Arrays;
22 import java.util.Collections;
23 import java.util.List;
24 import java.util.regex.Pattern;
25 import java.util.stream.Collectors;
26 import lombok.extern.slf4j.Slf4j;
27 import org.springframework.beans.factory.annotation.Autowired;
28 import org.springframework.stereotype.Service;
29
30 @Service
31 @Slf4j
32 public class NotificationService {
33
34     private NotificationProperties notificationProperties;
35     private NotificationPublisher notificationPublisher;
36     private CpsDataUpdatedEventFactory cpsDataUpdatedEventFactory;
37     private NotificationErrorHandler notificationErrorHandler;
38     private List<Pattern> dataspacePatterns;
39
40     /**
41      * Create an instance of Notification Subscriber.
42      *
43      * @param notificationProperties     properties for notification
44      * @param notificationPublisher      notification Publisher
45      * @param cpsDataUpdatedEventFactory to create CPSDataUpdatedEvent
46      * @param notificationErrorHandler   error handler
47      */
48     @Autowired
49     public NotificationService(
50         final NotificationProperties notificationProperties,
51         final NotificationPublisher notificationPublisher,
52         final CpsDataUpdatedEventFactory cpsDataUpdatedEventFactory,
53         final NotificationErrorHandler notificationErrorHandler) {
54         this.notificationProperties = notificationProperties;
55         this.notificationPublisher = notificationPublisher;
56         this.cpsDataUpdatedEventFactory = cpsDataUpdatedEventFactory;
57         this.notificationErrorHandler = notificationErrorHandler;
58         this.dataspacePatterns = getDataspaceFilterPatterns(notificationProperties);
59     }
60
61     private List<Pattern> getDataspaceFilterPatterns(final NotificationProperties notificationProperties) {
62         if (notificationProperties.isEnabled()) {
63             return Arrays.stream(notificationProperties.getFilters()
64                 .getOrDefault("enabled-dataspaces", "")
65                 .split(","))
66                 .map(filterPattern -> Pattern.compile(filterPattern, Pattern.CASE_INSENSITIVE))
67                 .collect(Collectors.toList());
68         } else {
69             return Collections.emptyList();
70         }
71     }
72
73     /**
74      * Process Data Updated Event and publishes the notification.
75      *
76      * @param dataspaceName dataspace name
77      * @param anchorName    anchor name
78      */
79     public void processDataUpdatedEvent(final String dataspaceName, final String anchorName) {
80         log.debug("process data updated event for dataspace '{}' & anchor '{}'", dataspaceName, anchorName);
81         try {
82             if (shouldSendNotification(dataspaceName)) {
83                 final var cpsDataUpdatedEvent =
84                     cpsDataUpdatedEventFactory.createCpsDataUpdatedEvent(dataspaceName, anchorName);
85                 log.debug("data updated event to be published {}", cpsDataUpdatedEvent);
86                 notificationPublisher.sendNotification(cpsDataUpdatedEvent);
87             }
88         } catch (final Exception exception) {
89             /* All the exceptions are handled to not to propagate it to caller.
90                CPS operation should not fail if sending event fails for any reason.
91              */
92             notificationErrorHandler.onException("Failed to process cps-data-updated-event.",
93                 exception, dataspaceName, anchorName);
94         }
95     }
96
97     /*
98         Add more complex rules based on dataspace and anchor later
99      */
100     private boolean shouldSendNotification(final String dataspaceName) {
101
102         return notificationProperties.isEnabled()
103             && dataspacePatterns.stream()
104             .anyMatch(pattern -> pattern.matcher(dataspaceName).find());
105     }
106
107 }