2 * ============LICENSE_START=======================================================
3 * Copyright (c) 2021-2022 Bell Canada.
4 * Modifications Copyright (C) 2022 Nordix Foundation
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
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.cps.notification;
24 import java.time.OffsetDateTime;
25 import java.util.Arrays;
26 import java.util.Collections;
27 import java.util.List;
28 import java.util.concurrent.CompletableFuture;
29 import java.util.concurrent.Future;
30 import java.util.regex.Pattern;
31 import java.util.stream.Collectors;
32 import javax.annotation.PostConstruct;
33 import lombok.RequiredArgsConstructor;
34 import lombok.extern.slf4j.Slf4j;
35 import org.onap.cps.spi.model.Anchor;
36 import org.springframework.scheduling.annotation.Async;
37 import org.springframework.stereotype.Service;
41 @RequiredArgsConstructor
42 public class NotificationService {
44 private final NotificationProperties notificationProperties;
45 private final NotificationPublisher notificationPublisher;
46 private final CpsDataUpdatedEventFactory cpsDataUpdatedEventFactory;
47 private final NotificationErrorHandler notificationErrorHandler;
48 private List<Pattern> dataspacePatterns;
52 log.info("Notification Properties {}", notificationProperties);
53 this.dataspacePatterns = getDataspaceFilterPatterns(notificationProperties);
56 private List<Pattern> getDataspaceFilterPatterns(final NotificationProperties notificationProperties) {
57 if (notificationProperties.isEnabled()) {
58 return Arrays.stream(notificationProperties.getFilters()
59 .getOrDefault("enabled-dataspaces", "")
61 .map(filterPattern -> Pattern.compile(filterPattern, Pattern.CASE_INSENSITIVE))
62 .collect(Collectors.toList());
64 return Collections.emptyList();
69 * Process Data Updated Event and publishes the notification.
71 * @param anchor anchor
72 * @param observedTimestamp observedTimestamp
73 * @param xpath xpath of changed data node
74 * @param operation operation
77 @Async("notificationExecutor")
78 public Future<Void> processDataUpdatedEvent(final Anchor anchor, final OffsetDateTime observedTimestamp,
79 final String xpath, final Operation operation) {
80 log.debug("process data updated event for anchor '{}'", anchor);
82 if (shouldSendNotification(anchor.getDataspaceName())) {
83 final var cpsDataUpdatedEvent =
84 cpsDataUpdatedEventFactory.createCpsDataUpdatedEvent(anchor,
85 observedTimestamp, getRootNodeOperation(xpath, operation));
86 log.debug("data updated event to be published {}", cpsDataUpdatedEvent);
87 notificationPublisher.sendNotification(cpsDataUpdatedEvent);
89 } catch (final Exception exception) {
90 /* All the exceptions are handled to not to propagate it to caller.
91 CPS operation should not fail if sending event fails for any reason.
93 notificationErrorHandler.onException("Failed to process cps-data-updated-event.",
94 exception, anchor, xpath, operation);
96 return CompletableFuture.completedFuture(null);
100 Add more complex rules based on dataspace and anchor later
102 private boolean shouldSendNotification(final String dataspaceName) {
104 return notificationProperties.isEnabled()
105 && dataspacePatterns.stream()
106 .anyMatch(pattern -> pattern.matcher(dataspaceName).find());
109 private Operation getRootNodeOperation(final String xpath, final Operation operation) {
110 return isRootXpath(xpath) || isRootContainerNodeXpath(xpath) ? operation : Operation.UPDATE;
113 private static boolean isRootXpath(final String xpath) {
114 return "/".equals(xpath) || "".equals(xpath);
117 private static boolean isRootContainerNodeXpath(final String xpath) {
118 return 0 == xpath.lastIndexOf('/');