Revert "Add PasswordCredentialAuth interface"
[netconf.git] / netconf / messagebus-netconf / src / main / java / org / opendaylight / netconf / messagebus / eventsources / netconf / StreamNotificationTopicRegistration.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.util.concurrent.CheckedFuture;
12 import java.util.ArrayList;
13 import java.util.Date;
14 import java.util.List;
15 import java.util.concurrent.ConcurrentHashMap;
16 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
17 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationService;
18 import org.opendaylight.controller.md.sal.dom.api.DOMRpcException;
19 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
20 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
21 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventaggregator.rev141202.TopicId;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.CreateSubscriptionInput;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.streams.Stream;
24 import org.opendaylight.yangtools.concepts.ListenerRegistration;
25 import org.opendaylight.yangtools.yang.common.QName;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
27 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
28 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
29 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
30 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeAttrBuilder;
31 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 /**
36  * Topic registration for notification stream.
37  */
38 public class StreamNotificationTopicRegistration extends NotificationTopicRegistration {
39
40     private static final Logger LOG = LoggerFactory.getLogger(StreamNotificationTopicRegistration.class);
41     private static final NodeIdentifier STREAM_QNAME = NodeIdentifier.create(
42         QName.create(CreateSubscriptionInput.QNAME, "stream"));
43     private static final SchemaPath CREATE_SUBSCRIPTION = SchemaPath
44         .create(true, QName.create(CreateSubscriptionInput.QNAME, "create-subscription"));
45     private static final NodeIdentifier START_TIME_SUBSCRIPTION = NodeIdentifier.create(
46         QName.create(CreateSubscriptionInput.QNAME, "startTime"));
47     private static final NodeIdentifier CREATE_SUBSCRIPTION_INPUT = NodeIdentifier.create(
48         CreateSubscriptionInput.QNAME);
49
50     final private DOMMountPoint domMountPoint;
51     final private String nodeId;
52     final private NetconfEventSource netconfEventSource;
53     final private Stream stream;
54     private Date lastEventTime;
55
56     private ConcurrentHashMap<SchemaPath, ListenerRegistration<NetconfEventSource>> notificationRegistrationMap = new ConcurrentHashMap<>();
57     private ConcurrentHashMap<SchemaPath, ArrayList<TopicId>> notificationTopicMap = new ConcurrentHashMap<>();
58
59     /**
60      * Creates registration to notification stream.
61      * @param stream stream
62      * @param notificationPrefix notifications namespace
63      * @param netconfEventSource event source
64      */
65     public StreamNotificationTopicRegistration(final Stream stream, final String notificationPrefix,
66         NetconfEventSource netconfEventSource) {
67         super(NotificationSourceType.NetconfDeviceStream, stream.getName().getValue(), notificationPrefix);
68         this.domMountPoint = netconfEventSource.getDOMMountPoint();
69         this.nodeId = netconfEventSource.getNode().getNodeId().getValue().toString();
70         this.netconfEventSource = netconfEventSource;
71         this.stream = stream;
72         this.lastEventTime = null;
73         setReplaySupported(this.stream.isReplaySupport());
74         setActive(false);
75         LOG.info("StreamNotificationTopicRegistration initialized for {}", getStreamName());
76     }
77
78     /**
79      * Subscribes to notification stream associated with this registration.
80      */
81     void activateNotificationSource() {
82         if (isActive() == false) {
83             LOG.info("Stream {} is not active on node {}. Will subscribe.", this.getStreamName(), this.nodeId);
84             final ContainerNode input = Builders.containerBuilder()
85                 .withNodeIdentifier(CREATE_SUBSCRIPTION_INPUT)
86                 .withChild(ImmutableNodes.leafNode(STREAM_QNAME, this.getStreamName())).build();
87             CheckedFuture<DOMRpcResult, DOMRpcException> csFuture = domMountPoint.getService(DOMRpcService.class).get()
88                 .invokeRpc(CREATE_SUBSCRIPTION, input);
89             try {
90                 csFuture.checkedGet();
91                 setActive(true);
92             } catch (DOMRpcException e) {
93                 LOG.warn("Can not subscribe stream {} on node {}", this.getSourceName(), this.nodeId);
94                 setActive(false);
95                 return;
96             }
97         } else {
98             LOG.info("Stream {} is now active on node {}", this.getStreamName(), this.nodeId);
99         }
100     }
101
102     /**
103      * Subscribes to notification stream associated with this registration. If replay is supported, notifications from last
104      * received event time will be requested.
105      */
106     void reActivateNotificationSource() {
107         if (isActive()) {
108             LOG.info("Stream {} is reactivating on node {}.", this.getStreamName(), this.nodeId);
109             DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> inputBuilder = Builders.containerBuilder()
110                 .withNodeIdentifier(CREATE_SUBSCRIPTION_INPUT)
111                 .withChild(ImmutableNodes.leafNode(STREAM_QNAME, this.getStreamName()));
112             if (isReplaySupported() && this.getLastEventTime() != null) {
113                 inputBuilder.withChild(ImmutableNodes.leafNode(START_TIME_SUBSCRIPTION, this.getLastEventTime()));
114             }
115             final ContainerNode input = inputBuilder.build();
116             CheckedFuture<DOMRpcResult, DOMRpcException> csFuture = domMountPoint.getService(DOMRpcService.class).get()
117                 .invokeRpc(CREATE_SUBSCRIPTION, input);
118             try {
119                 csFuture.checkedGet();
120                 setActive(true);
121             } catch (DOMRpcException e) {
122                 LOG.warn("Can not resubscribe stream {} on node {}", this.getSourceName(), this.nodeId);
123                 setActive(false);
124                 return;
125             }
126         }
127     }
128
129     @Override void deActivateNotificationSource() {
130         // no operations need
131     }
132
133     private void closeStream() {
134         if (isActive()) {
135             for (ListenerRegistration<NetconfEventSource> reg : notificationRegistrationMap.values()) {
136                 reg.close();
137             }
138             notificationRegistrationMap.clear();
139             notificationTopicMap.clear();
140             setActive(false);
141         }
142     }
143
144     private String getStreamName() {
145         return getSourceName();
146     }
147
148     @Override ArrayList<TopicId> getNotificationTopicIds(SchemaPath notificationPath) {
149         return notificationTopicMap.get(notificationPath);
150     }
151
152     @Override boolean registerNotificationTopic(SchemaPath notificationPath, TopicId topicId) {
153
154         if (checkNotificationPath(notificationPath) == false) {
155             LOG.debug("Bad SchemaPath for notification try to register");
156             return false;
157         }
158
159         final Optional<DOMNotificationService> notifyService = domMountPoint.getService(DOMNotificationService.class);
160         if (notifyService.isPresent() == false) {
161             LOG.debug("DOMNotificationService is not present");
162             return false;
163         }
164
165         activateNotificationSource();
166         if (isActive() == false) {
167             LOG.warn("Stream {} is not active, listener for notification {} is not registered.", getStreamName(),
168                 notificationPath.toString());
169             return false;
170         }
171
172         ListenerRegistration<NetconfEventSource> registration = notifyService.get()
173             .registerNotificationListener(this.netconfEventSource, notificationPath);
174         notificationRegistrationMap.put(notificationPath, registration);
175         ArrayList<TopicId> topicIds = getNotificationTopicIds(notificationPath);
176         if (topicIds == null) {
177             topicIds = new ArrayList<>();
178             topicIds.add(topicId);
179         } else {
180             if (topicIds.contains(topicId) == false) {
181                 topicIds.add(topicId);
182             }
183         }
184
185         notificationTopicMap.put(notificationPath, topicIds);
186         return true;
187     }
188
189     @Override synchronized void unRegisterNotificationTopic(TopicId topicId) {
190         List<SchemaPath> notificationPathToRemove = new ArrayList<>();
191         for (SchemaPath notifKey : notificationTopicMap.keySet()) {
192             ArrayList<TopicId> topicList = notificationTopicMap.get(notifKey);
193             if (topicList != null) {
194                 topicList.remove(topicId);
195                 if (topicList.isEmpty()) {
196                     notificationPathToRemove.add(notifKey);
197                 }
198             }
199         }
200         for (SchemaPath notifKey : notificationPathToRemove) {
201             notificationTopicMap.remove(notifKey);
202             ListenerRegistration<NetconfEventSource> reg = notificationRegistrationMap.remove(notifKey);
203             if (reg != null) {
204                 reg.close();
205             }
206         }
207     }
208
209     Optional<Date> getLastEventTime() {
210         return Optional.fromNullable(lastEventTime);
211     }
212
213     void setLastEventTime(Date lastEventTime) {
214         this.lastEventTime = lastEventTime;
215     }
216
217     @Override public void close() throws Exception {
218         closeStream();
219     }
220
221 }