Bump versions to 2.0.0-SNAPSHOT
[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 java.time.Instant;
11 import java.util.HashSet;
12 import java.util.Optional;
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 @Deprecated(forRemoval = true)
24 abstract class NotificationTopicRegistration implements AutoCloseable {
25
26     private static final Logger LOG = LoggerFactory.getLogger(NotificationTopicRegistration.class);
27
28     public enum NotificationSourceType {
29         NetconfDeviceStream,
30         ConnectionStatusChange
31     }
32
33     private boolean active;
34     private final NotificationSourceType notificationSourceType;
35     private final String sourceName;
36     private final String notificationUrnPrefix;
37     private boolean replaySupported;
38     private Instant lastEventTime;
39     protected final ConcurrentHashMap<SchemaPath, Set<TopicId>> notificationTopicMap = new ConcurrentHashMap<>();
40
41     protected NotificationTopicRegistration(final NotificationSourceType notificationSourceType,
42             final String sourceName, final String notificationUrnPrefix) {
43         this.notificationSourceType = notificationSourceType;
44         this.sourceName = sourceName;
45         this.notificationUrnPrefix = notificationUrnPrefix;
46         this.active = false;
47         this.setReplaySupported(false);
48     }
49
50     public boolean isActive() {
51         return active;
52     }
53
54     protected void setActive(final boolean active) {
55         this.active = active;
56     }
57
58     public NotificationSourceType getNotificationSourceType() {
59         return notificationSourceType;
60     }
61
62     public String getSourceName() {
63         return sourceName;
64     }
65
66     public String getNotificationUrnPrefix() {
67         return notificationUrnPrefix;
68     }
69
70     /**
71      * Returns registered topics for given notification path.
72      *
73      * @param notificationPath path
74      * @return topicIds
75      */
76     Set<TopicId> getTopicsForNotification(final SchemaPath notificationPath) {
77         final Set<TopicId> topicIds = notificationTopicMap.get(notificationPath);
78         return topicIds != null ? topicIds : new HashSet<>();
79     }
80
81     /**
82      * Checks, if notification is from namespace belonging to this registration.
83      *
84      * @param notificationPath path
85      * @return true, if notification belongs to registration namespace
86      */
87     boolean checkNotificationPath(final SchemaPath notificationPath) {
88         if (notificationPath == null) {
89             return false;
90         }
91         String nameSpace = notificationPath.getLastComponent().getNamespace().toString();
92         LOG.debug("CheckNotification - name space {} - NotificationUrnPrefix {}", nameSpace,
93                 getNotificationUrnPrefix());
94         return nameSpace.startsWith(getNotificationUrnPrefix());
95     }
96
97     Optional<Instant> getLastEventTime() {
98         return Optional.ofNullable(lastEventTime);
99     }
100
101     void setLastEventTime(final Instant lastEventTime) {
102         this.lastEventTime = lastEventTime;
103     }
104
105     abstract void activateNotificationSource();
106
107     abstract void deActivateNotificationSource();
108
109     abstract void reActivateNotificationSource();
110
111     /**
112      * Registers associated event source notification to topic.
113      *
114      * @param notificationPath notification path
115      * @param topicId          topic id
116      * @return true, if successful
117      */
118     abstract boolean registerNotificationTopic(SchemaPath notificationPath, TopicId topicId);
119
120     /**
121      * Registers associated event source notification to topic.
122      *
123      * @param topicId topic id
124      */
125     abstract void unRegisterNotificationTopic(TopicId topicId);
126
127     public boolean isReplaySupported() {
128         return replaySupported;
129     }
130
131     protected void setReplaySupported(final boolean replaySupported) {
132         this.replaySupported = replaySupported;
133     }
134
135 }