2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.netconf.messagebus.eventsources.netconf;
10 import com.google.common.collect.Sets;
11 import java.time.Instant;
12 import java.util.Optional;
14 import java.util.concurrent.ConcurrentHashMap;
15 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventaggregator.rev141202.TopicId;
16 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
21 * Notification topic registration.
23 abstract class NotificationTopicRegistration implements AutoCloseable {
25 private static final Logger LOG = LoggerFactory.getLogger(NotificationTopicRegistration.class);
27 public enum NotificationSourceType {
29 ConnectionStatusChange
32 private boolean active;
33 private final NotificationSourceType notificationSourceType;
34 private final String sourceName;
35 private final String notificationUrnPrefix;
36 private boolean replaySupported;
37 private Instant lastEventTime;
38 protected final ConcurrentHashMap<SchemaPath, Set<TopicId>> notificationTopicMap = new ConcurrentHashMap<>();
40 protected NotificationTopicRegistration(final NotificationSourceType notificationSourceType,
41 final String sourceName, final String notificationUrnPrefix) {
42 this.notificationSourceType = notificationSourceType;
43 this.sourceName = sourceName;
44 this.notificationUrnPrefix = notificationUrnPrefix;
46 this.setReplaySupported(false);
49 public boolean isActive() {
53 protected void setActive(final boolean active) {
57 public NotificationSourceType getNotificationSourceType() {
58 return notificationSourceType;
61 public String getSourceName() {
65 public String getNotificationUrnPrefix() {
66 return notificationUrnPrefix;
70 * Returns registered topics for given notification path.
72 * @param notificationPath path
75 Set<TopicId> getTopicsForNotification(final SchemaPath notificationPath) {
76 final Set<TopicId> topicIds = notificationTopicMap.get(notificationPath);
77 return topicIds != null ? topicIds : Sets.newHashSet();
81 * Checks, if notification is from namespace belonging to this registration.
83 * @param notificationPath path
84 * @return true, if notification belongs to registration namespace
86 boolean checkNotificationPath(final SchemaPath notificationPath) {
87 if (notificationPath == null) {
90 String nameSpace = notificationPath.getLastComponent().getNamespace().toString();
91 LOG.debug("CheckNotification - name space {} - NotificationUrnPrefix {}", nameSpace,
92 getNotificationUrnPrefix());
93 return nameSpace.startsWith(getNotificationUrnPrefix());
96 Optional<Instant> getLastEventTime() {
97 return Optional.ofNullable(lastEventTime);
100 void setLastEventTime(final Instant lastEventTime) {
101 this.lastEventTime = lastEventTime;
104 abstract void activateNotificationSource();
106 abstract void deActivateNotificationSource();
108 abstract void reActivateNotificationSource();
111 * Registers associated event source notification to topic.
113 * @param notificationPath notification path
114 * @param topicId topic id
115 * @return true, if successful
117 abstract boolean registerNotificationTopic(SchemaPath notificationPath, TopicId topicId);
120 * Registers associated event source notification to topic.
122 * @param topicId topic id
124 abstract void unRegisterNotificationTopic(TopicId topicId);
126 public boolean isReplaySupported() {
127 return replaySupported;
130 protected void setReplaySupported(final boolean replaySupported) {
131 this.replaySupported = replaySupported;