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.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;
35 public class StreamNotificationTopicRegistration extends NotificationTopicRegistration {
37 private static final Logger LOG = LoggerFactory.getLogger(StreamNotificationTopicRegistration.class);
38 private static final NodeIdentifier STREAM_QNAME = NodeIdentifier.create(
39 QName.create(CreateSubscriptionInput.QNAME, "stream"));
40 private static final SchemaPath CREATE_SUBSCRIPTION = SchemaPath
41 .create(true, QName.create(CreateSubscriptionInput.QNAME, "create-subscription"));
42 private static final NodeIdentifier START_TIME_SUBSCRIPTION = NodeIdentifier.create(
43 QName.create(CreateSubscriptionInput.QNAME, "startTime"));
44 private static final NodeIdentifier CREATE_SUBSCRIPTION_INPUT = NodeIdentifier.create(
45 CreateSubscriptionInput.QNAME);
47 final private DOMMountPoint domMountPoint;
48 final private String nodeId;
49 final private NetconfEventSource netconfEventSource;
50 final private Stream stream;
51 private Date lastEventTime;
53 private ConcurrentHashMap<SchemaPath, ListenerRegistration<NetconfEventSource>> notificationRegistrationMap = new ConcurrentHashMap<>();
54 private ConcurrentHashMap<SchemaPath, ArrayList<TopicId>> notificationTopicMap = new ConcurrentHashMap<>();
56 public StreamNotificationTopicRegistration(final Stream stream, final String notificationPrefix,
57 NetconfEventSource netconfEventSource) {
58 super(NotificationSourceType.NetconfDeviceStream, stream.getName().getValue(), notificationPrefix);
59 this.domMountPoint = netconfEventSource.getDOMMountPoint();
60 this.nodeId = netconfEventSource.getNode().getNodeId().getValue().toString();
61 this.netconfEventSource = netconfEventSource;
63 this.lastEventTime = null;
64 setReplaySupported(this.stream.isReplaySupport());
66 LOG.info("StreamNotificationTopicRegistration initialized for {}", getStreamName());
69 void activateNotificationSource() {
70 if (isActive() == false) {
71 LOG.info("Stream {} is not active on node {}. Will subscribe.", this.getStreamName(), this.nodeId);
72 final ContainerNode input = Builders.containerBuilder()
73 .withNodeIdentifier(CREATE_SUBSCRIPTION_INPUT)
74 .withChild(ImmutableNodes.leafNode(STREAM_QNAME, this.getStreamName())).build();
75 CheckedFuture<DOMRpcResult, DOMRpcException> csFuture = domMountPoint.getService(DOMRpcService.class).get()
76 .invokeRpc(CREATE_SUBSCRIPTION, input);
78 csFuture.checkedGet();
80 } catch (DOMRpcException e) {
81 LOG.warn("Can not subscribe stream {} on node {}", this.getSourceName(), this.nodeId);
86 LOG.info("Stream {} is now active on node {}", this.getStreamName(), this.nodeId);
90 void reActivateNotificationSource() {
92 LOG.info("Stream {} is reactivating on node {}.", this.getStreamName(), this.nodeId);
93 DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> inputBuilder = Builders.containerBuilder()
94 .withNodeIdentifier(CREATE_SUBSCRIPTION_INPUT)
95 .withChild(ImmutableNodes.leafNode(STREAM_QNAME, this.getStreamName()));
96 if (isReplaySupported() && this.getLastEventTime() != null) {
97 inputBuilder.withChild(ImmutableNodes.leafNode(START_TIME_SUBSCRIPTION, this.getLastEventTime()));
99 final ContainerNode input = inputBuilder.build();
100 CheckedFuture<DOMRpcResult, DOMRpcException> csFuture = domMountPoint.getService(DOMRpcService.class).get()
101 .invokeRpc(CREATE_SUBSCRIPTION, input);
103 csFuture.checkedGet();
105 } catch (DOMRpcException e) {
106 LOG.warn("Can not resubscribe stream {} on node {}", this.getSourceName(), this.nodeId);
113 @Override void deActivateNotificationSource() {
114 // no operations need
117 private void closeStream() {
119 for (ListenerRegistration<NetconfEventSource> reg : notificationRegistrationMap.values()) {
122 notificationRegistrationMap.clear();
123 notificationTopicMap.clear();
128 private String getStreamName() {
129 return getSourceName();
132 @Override ArrayList<TopicId> getNotificationTopicIds(SchemaPath notificationPath) {
133 return notificationTopicMap.get(notificationPath);
136 @Override boolean registerNotificationTopic(SchemaPath notificationPath, TopicId topicId) {
138 if (checkNotificationPath(notificationPath) == false) {
139 LOG.debug("Bad SchemaPath for notification try to register");
143 final Optional<DOMNotificationService> notifyService = domMountPoint.getService(DOMNotificationService.class);
144 if (notifyService.isPresent() == false) {
145 LOG.debug("DOMNotificationService is not present");
149 activateNotificationSource();
150 if (isActive() == false) {
151 LOG.warn("Stream {} is not active, listener for notification {} is not registered.", getStreamName(),
152 notificationPath.toString());
156 ListenerRegistration<NetconfEventSource> registration = notifyService.get()
157 .registerNotificationListener(this.netconfEventSource, notificationPath);
158 notificationRegistrationMap.put(notificationPath, registration);
159 ArrayList<TopicId> topicIds = getNotificationTopicIds(notificationPath);
160 if (topicIds == null) {
161 topicIds = new ArrayList<>();
162 topicIds.add(topicId);
164 if (topicIds.contains(topicId) == false) {
165 topicIds.add(topicId);
169 notificationTopicMap.put(notificationPath, topicIds);
173 @Override synchronized void unRegisterNotificationTopic(TopicId topicId) {
174 List<SchemaPath> notificationPathToRemove = new ArrayList<>();
175 for (SchemaPath notifKey : notificationTopicMap.keySet()) {
176 ArrayList<TopicId> topicList = notificationTopicMap.get(notifKey);
177 if (topicList != null) {
178 topicList.remove(topicId);
179 if (topicList.isEmpty()) {
180 notificationPathToRemove.add(notifKey);
184 for (SchemaPath notifKey : notificationPathToRemove) {
185 notificationTopicMap.remove(notifKey);
186 ListenerRegistration<NetconfEventSource> reg = notificationRegistrationMap.remove(notifKey);
193 Optional<Date> getLastEventTime() {
194 return Optional.fromNullable(lastEventTime);
197 void setLastEventTime(Date lastEventTime) {
198 this.lastEventTime = lastEventTime;
201 @Override public void close() throws Exception {