Bug 8153: Enforce check-style on messagebus-netconf
[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      *
72      * @param notificationPath path
73      * @return topicIds
74      */
75     Set<TopicId> getTopicsForNotification(SchemaPath notificationPath) {
76         final Set<TopicId> topicIds = notificationTopicMap.get(notificationPath);
77         return topicIds != null ? topicIds : Sets.newHashSet();
78     }
79
80     /**
81      * Checks, if notification is from namespace belonging to this registration.
82      *
83      * @param notificationPath path
84      * @return true, if notification belongs to registration namespace
85      */
86     boolean checkNotificationPath(SchemaPath notificationPath) {
87         if (notificationPath == null) {
88             return false;
89         }
90         String nameSpace = notificationPath.getLastComponent().getNamespace().toString();
91         LOG.debug("CheckNotification - name space {} - NotificationUrnPrefix {}", nameSpace,
92                 getNotificationUrnPrefix());
93         return nameSpace.startsWith(getNotificationUrnPrefix());
94     }
95
96     Optional<Date> getLastEventTime() {
97         return Optional.fromNullable(lastEventTime);
98     }
99
100     void setLastEventTime(Date lastEventTime) {
101         this.lastEventTime = lastEventTime;
102     }
103
104     abstract void activateNotificationSource();
105
106     abstract void deActivateNotificationSource();
107
108     abstract void reActivateNotificationSource();
109
110     /**
111      * Registers associated event source notification to topic.
112      *
113      * @param notificationPath notification path
114      * @param topicId          topic id
115      * @return true, if successful
116      */
117     abstract boolean registerNotificationTopic(SchemaPath notificationPath, TopicId topicId);
118
119     /**
120      * Registers associated event source notification to topic.
121      *
122      * @param topicId topic id
123      * @return true, if successful
124      */
125     abstract void unRegisterNotificationTopic(TopicId topicId);
126
127     public boolean isReplaySupported() {
128         return replaySupported;
129     }
130
131     protected void setReplaySupported(boolean replaySupported) {
132         this.replaySupported = replaySupported;
133     }
134
135 }