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.controller.messagebus.eventsources.netconf;
10 import java.util.ArrayList;
11 import java.util.Date;
12 import java.util.List;
13 import java.util.concurrent.ConcurrentHashMap;
15 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
16 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationService;
17 import org.opendaylight.controller.md.sal.dom.api.DOMRpcException;
18 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
19 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
20 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventaggregator.rev141202.TopicId;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.CreateSubscriptionInput;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.streams.Stream;
23 import org.opendaylight.yangtools.concepts.ListenerRegistration;
24 import org.opendaylight.yangtools.yang.common.QName;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
27 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
28 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
29 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeAttrBuilder;
30 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 import com.google.common.base.Optional;
35 import com.google.common.util.concurrent.CheckedFuture;
37 public class StreamNotificationTopicRegistration extends NotificationTopicRegistration {
39 private static final Logger LOG = LoggerFactory.getLogger(StreamNotificationTopicRegistration.class);
40 private static final NodeIdentifier STREAM_QNAME = new NodeIdentifier(QName.create(CreateSubscriptionInput.QNAME,"stream"));
41 private static final SchemaPath CREATE_SUBSCRIPTION = SchemaPath.create(true, QName.create(CreateSubscriptionInput.QNAME, "create-subscription"));
42 private static final NodeIdentifier START_TIME_SUBSCRIPTION = new NodeIdentifier(QName.create(CreateSubscriptionInput.QNAME,"startTime"));
44 final private DOMMountPoint domMountPoint;
45 final private String nodeId;
46 final private NetconfEventSource netconfEventSource;
47 final private Stream stream;
48 private Date lastEventTime;
50 private ConcurrentHashMap<SchemaPath, ListenerRegistration<NetconfEventSource>> notificationRegistrationMap = new ConcurrentHashMap<>();
51 private ConcurrentHashMap<SchemaPath, ArrayList<TopicId>> notificationTopicMap = new ConcurrentHashMap<>();
53 public StreamNotificationTopicRegistration(final Stream stream, final String notificationPrefix, NetconfEventSource netconfEventSource) {
54 super(NotificationSourceType.NetconfDeviceStream, stream.getName().getValue(), notificationPrefix);
55 this.domMountPoint = netconfEventSource.getDOMMountPoint();
56 this.nodeId = netconfEventSource.getNode().getNodeId().getValue().toString();
57 this.netconfEventSource = netconfEventSource;
59 this.lastEventTime= null;
60 setReplaySupported(this.stream.isReplaySupport());
62 LOG.info("StreamNotificationTopicRegistration initialized for {}", getStreamName());
65 void activateNotificationSource() {
66 if(isActive() == false){
67 LOG.info("Stream {} is not active on node {}. Will subscribe.", this.getStreamName(), this.nodeId);
68 final ContainerNode input = Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(CreateSubscriptionInput.QNAME))
69 .withChild(ImmutableNodes.leafNode(STREAM_QNAME, this.getStreamName()))
71 CheckedFuture<DOMRpcResult, DOMRpcException> csFuture = domMountPoint.getService(DOMRpcService.class).get().invokeRpc(CREATE_SUBSCRIPTION, input);
73 csFuture.checkedGet();
75 } catch (DOMRpcException e) {
76 LOG.warn("Can not subscribe stream {} on node {}", this.getSourceName(), this.nodeId);
81 LOG.info("Stream {} is now active on node {}", this.getStreamName(), this.nodeId);
85 void reActivateNotificationSource(){
87 LOG.info("Stream {} is reactivating on node {}.", this.getStreamName(), this.nodeId);
88 DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> inputBuilder =
89 Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(CreateSubscriptionInput.QNAME))
90 .withChild(ImmutableNodes.leafNode(STREAM_QNAME, this.getStreamName()));
91 if(isReplaySupported() && this.getLastEventTime() != null){
92 inputBuilder.withChild(ImmutableNodes.leafNode(START_TIME_SUBSCRIPTION, this.getLastEventTime()));
94 final ContainerNode input = inputBuilder.build();
95 CheckedFuture<DOMRpcResult, DOMRpcException> csFuture = domMountPoint.getService(DOMRpcService.class).get().invokeRpc(CREATE_SUBSCRIPTION, input);
97 csFuture.checkedGet();
99 } catch (DOMRpcException e) {
100 LOG.warn("Can not resubscribe stream {} on node {}", this.getSourceName(), this.nodeId);
108 void deActivateNotificationSource() {
109 // no operations need
112 private void closeStream() {
114 for(ListenerRegistration<NetconfEventSource> reg : notificationRegistrationMap.values()){
117 notificationRegistrationMap.clear();
118 notificationTopicMap.clear();
123 private String getStreamName() {
124 return getSourceName();
128 ArrayList<TopicId> getNotificationTopicIds(SchemaPath notificationPath){
129 return notificationTopicMap.get(notificationPath);
133 boolean registerNotificationTopic(SchemaPath notificationPath, TopicId topicId){
135 if(checkNotificationPath(notificationPath) == false){
136 LOG.debug("Bad SchemaPath for notification try to register");
140 final Optional<DOMNotificationService> notifyService = domMountPoint.getService(DOMNotificationService.class);
141 if(notifyService.isPresent() == false){
142 LOG.debug("DOMNotificationService is not present");
146 activateNotificationSource();
147 if(isActive() == false){
148 LOG.warn("Stream {} is not active, listener for notification {} is not registered.", getStreamName(), notificationPath.toString());
152 ListenerRegistration<NetconfEventSource> registration =
153 notifyService.get().registerNotificationListener(this.netconfEventSource,notificationPath);
154 notificationRegistrationMap.put(notificationPath, registration);
155 ArrayList<TopicId> topicIds = getNotificationTopicIds(notificationPath);
156 if(topicIds == null){
157 topicIds = new ArrayList<>();
158 topicIds.add(topicId);
160 if(topicIds.contains(topicId) == false){
161 topicIds.add(topicId);
165 notificationTopicMap.put(notificationPath, topicIds);
170 synchronized void unRegisterNotificationTopic(TopicId topicId) {
171 List<SchemaPath> notificationPathToRemove = new ArrayList<>();
172 for(SchemaPath notifKey : notificationTopicMap.keySet()){
173 ArrayList<TopicId> topicList = notificationTopicMap.get(notifKey);
174 if(topicList != null){
175 topicList.remove(topicId);
176 if(topicList.isEmpty()){
177 notificationPathToRemove.add(notifKey);
181 for(SchemaPath notifKey : notificationPathToRemove){
182 notificationTopicMap.remove(notifKey);
183 ListenerRegistration<NetconfEventSource> reg = notificationRegistrationMap.remove(notifKey);
190 Optional<Date> getLastEventTime() {
191 return Optional.fromNullable(lastEventTime);
195 void setLastEventTime(Date lastEventTime) {
196 this.lastEventTime = lastEventTime;
200 public void close() throws Exception {