Refactor NetconfEventSource
[netconf.git] / netconf / messagebus-netconf / src / main / java / org / opendaylight / netconf / messagebus / eventsources / netconf / NotificationTopicRegistration.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.netconf.messagebus.eventsources.netconf;
9
10 import com.google.common.base.Optional;
11 import com.google.common.collect.Sets;
12 import java.util.Date;
13 import java.util.Set;
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;
19
20 /**
21  * Notification topic registration.
22  */
23 abstract class NotificationTopicRegistration implements AutoCloseable {
24
25     private static final Logger LOG = LoggerFactory.getLogger(NotificationTopicRegistration.class);
26
27     public enum NotificationSourceType {
28         NetconfDeviceStream,
29         ConnectionStatusChange
30     }
31
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 Date lastEventTime;
38     protected final ConcurrentHashMap<SchemaPath, Set<TopicId>> notificationTopicMap = new ConcurrentHashMap<>();
39
40     protected NotificationTopicRegistration(NotificationSourceType notificationSourceType, String sourceName,
41         String notificationUrnPrefix) {
42         this.notificationSourceType = notificationSourceType;
43         this.sourceName = sourceName;
44         this.notificationUrnPrefix = notificationUrnPrefix;
45         this.active = false;
46         this.setReplaySupported(false);
47     }
48
49     public boolean isActive() {
50         return active;
51     }
52
53     protected void setActive(boolean active) {
54         this.active = active;
55     }
56
57     public NotificationSourceType getNotificationSourceType() {
58         return notificationSourceType;
59     }
60
61     public String getSourceName() {
62         return sourceName;
63     }
64
65     public String getNotificationUrnPrefix() {
66         return notificationUrnPrefix;
67     }
68
69     /**
70      * Returns registered topics for given notification path.
71      * @param notificationPath path
72      * @return topicIds
73      */
74     Set<TopicId> getTopicsForNotification(SchemaPath notificationPath) {
75         final Set<TopicId> topicIds = notificationTopicMap.get(notificationPath);
76         return topicIds != null ? topicIds : Sets.newHashSet();
77     }
78
79     /**
80      * Checks, if notification is from namespace belonging to this registration.
81      * @param notificationPath path
82      * @return true, if notification belongs to registration namespace
83      */
84     boolean checkNotificationPath(SchemaPath notificationPath) {
85         if (notificationPath == null) {
86             return false;
87         }
88         String nameSpace = notificationPath.getLastComponent().getNamespace().toString();
89         LOG.debug("CheckNotification - name space {} - NotificationUrnPrefix {}", nameSpace,
90             getNotificationUrnPrefix());
91         return nameSpace.startsWith(getNotificationUrnPrefix());
92     }
93
94     Optional<Date> getLastEventTime() {
95         return Optional.fromNullable(lastEventTime);
96     }
97
98     void setLastEventTime(Date lastEventTime) {
99         this.lastEventTime = lastEventTime;
100     }
101
102     abstract void activateNotificationSource();
103
104     abstract void deActivateNotificationSource();
105
106     abstract void reActivateNotificationSource();
107
108     /**
109      * Registers associated event source notification to topic.
110      * @param notificationPath notification path
111      * @param topicId topic id
112      * @return true, if successful
113      */
114     abstract boolean registerNotificationTopic(SchemaPath notificationPath, TopicId topicId);
115
116     /**
117      * Registers associated event source notification to topic.
118      * @param topicId topic id
119      * @return true, if successful
120      */
121     abstract void unRegisterNotificationTopic(TopicId topicId);
122
123     public boolean isReplaySupported() {
124         return replaySupported;
125     }
126
127     protected void setReplaySupported(boolean replaySupported) {
128         this.replaySupported = replaySupported;
129     }
130
131 }