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 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;
32 public class NotificationService {
34 private NotificationProperties notificationProperties;
35 private NotificationPublisher notificationPublisher;
36 private CpsDataUpdatedEventFactory cpsDataUpdatedEventFactory;
37 private NotificationErrorHandler notificationErrorHandler;
38 private List<Pattern> dataspacePatterns;
41 * Create an instance of Notification Subscriber.
43 * @param notificationProperties properties for notification
44 * @param notificationPublisher notification Publisher
45 * @param cpsDataUpdatedEventFactory to create CPSDataUpdatedEvent
46 * @param notificationErrorHandler error handler
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);
61 private List<Pattern> getDataspaceFilterPatterns(final NotificationProperties notificationProperties) {
62 if (notificationProperties.isEnabled()) {
63 return Arrays.stream(notificationProperties.getFilters()
64 .getOrDefault("enabled-dataspaces", "")
66 .map(filterPattern -> Pattern.compile(filterPattern, Pattern.CASE_INSENSITIVE))
67 .collect(Collectors.toList());
69 return Collections.emptyList();
74 * Process Data Updated Event and publishes the notification.
76 * @param dataspaceName dataspace name
77 * @param anchorName anchor name
79 public void processDataUpdatedEvent(final String dataspaceName, final String anchorName) {
80 log.debug("process data updated event for dataspace '{}' & anchor '{}'", dataspaceName, anchorName);
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);
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.
92 notificationErrorHandler.onException("Failed to process cps-data-updated-event.",
93 exception, dataspaceName, anchorName);
98 Add more complex rules based on dataspace and anchor later
100 private boolean shouldSendNotification(final String dataspaceName) {
102 return notificationProperties.isEnabled()
103 && dataspacePatterns.stream()
104 .anyMatch(pattern -> pattern.matcher(dataspaceName).find());