Fix netconf-connector-config groupId
[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 public class StreamNotificationTopicRegistration extends NotificationTopicRegistration {
36
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);
46
47     final private DOMMountPoint domMountPoint;
48     final private String nodeId;
49     final private NetconfEventSource netconfEventSource;
50     final private Stream stream;
51     private Date lastEventTime;
52
53     private ConcurrentHashMap<SchemaPath, ListenerRegistration<NetconfEventSource>> notificationRegistrationMap = new ConcurrentHashMap<>();
54     private ConcurrentHashMap<SchemaPath, ArrayList<TopicId>> notificationTopicMap = new ConcurrentHashMap<>();
55
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;
62         this.stream = stream;
63         this.lastEventTime = null;
64         setReplaySupported(this.stream.isReplaySupport());
65         setActive(false);
66         LOG.info("StreamNotificationTopicRegistration initialized for {}", getStreamName());
67     }
68
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);
77             try {
78                 csFuture.checkedGet();
79                 setActive(true);
80             } catch (DOMRpcException e) {
81                 LOG.warn("Can not subscribe stream {} on node {}", this.getSourceName(), this.nodeId);
82                 setActive(false);
83                 return;
84             }
85         } else {
86             LOG.info("Stream {} is now active on node {}", this.getStreamName(), this.nodeId);
87         }
88     }
89
90     void reActivateNotificationSource() {
91         if (isActive()) {
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()));
98             }
99             final ContainerNode input = inputBuilder.build();
100             CheckedFuture<DOMRpcResult, DOMRpcException> csFuture = domMountPoint.getService(DOMRpcService.class).get()
101                 .invokeRpc(CREATE_SUBSCRIPTION, input);
102             try {
103                 csFuture.checkedGet();
104                 setActive(true);
105             } catch (DOMRpcException e) {
106                 LOG.warn("Can not resubscribe stream {} on node {}", this.getSourceName(), this.nodeId);
107                 setActive(false);
108                 return;
109             }
110         }
111     }
112
113     @Override void deActivateNotificationSource() {
114         // no operations need
115     }
116
117     private void closeStream() {
118         if (isActive()) {
119             for (ListenerRegistration<NetconfEventSource> reg : notificationRegistrationMap.values()) {
120                 reg.close();
121             }
122             notificationRegistrationMap.clear();
123             notificationTopicMap.clear();
124             setActive(false);
125         }
126     }
127
128     private String getStreamName() {
129         return getSourceName();
130     }
131
132     @Override ArrayList<TopicId> getNotificationTopicIds(SchemaPath notificationPath) {
133         return notificationTopicMap.get(notificationPath);
134     }
135
136     @Override boolean registerNotificationTopic(SchemaPath notificationPath, TopicId topicId) {
137
138         if (checkNotificationPath(notificationPath) == false) {
139             LOG.debug("Bad SchemaPath for notification try to register");
140             return false;
141         }
142
143         final Optional<DOMNotificationService> notifyService = domMountPoint.getService(DOMNotificationService.class);
144         if (notifyService.isPresent() == false) {
145             LOG.debug("DOMNotificationService is not present");
146             return false;
147         }
148
149         activateNotificationSource();
150         if (isActive() == false) {
151             LOG.warn("Stream {} is not active, listener for notification {} is not registered.", getStreamName(),
152                 notificationPath.toString());
153             return false;
154         }
155
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);
163         } else {
164             if (topicIds.contains(topicId) == false) {
165                 topicIds.add(topicId);
166             }
167         }
168
169         notificationTopicMap.put(notificationPath, topicIds);
170         return true;
171     }
172
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);
181                 }
182             }
183         }
184         for (SchemaPath notifKey : notificationPathToRemove) {
185             notificationTopicMap.remove(notifKey);
186             ListenerRegistration<NetconfEventSource> reg = notificationRegistrationMap.remove(notifKey);
187             if (reg != null) {
188                 reg.close();
189             }
190         }
191     }
192
193     Optional<Date> getLastEventTime() {
194         return Optional.fromNullable(lastEventTime);
195     }
196
197     void setLastEventTime(Date lastEventTime) {
198         this.lastEventTime = lastEventTime;
199     }
200
201     @Override public void close() throws Exception {
202         closeStream();
203     }
204
205 }