BUG 3030 - reconnect netconf event source
[controller.git] / opendaylight / md-sal / messagebus-impl / src / main / java / org / opendaylight / controller / messagebus / eventsources / netconf / NetconfEventSource.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
9 package org.opendaylight.controller.messagebus.eventsources.netconf;
10
11 import static com.google.common.util.concurrent.Futures.immediateFuture;
12
13 import java.io.IOException;
14 import java.util.ArrayList;
15 import java.util.Date;
16 import java.util.HashMap;
17 import java.util.List;
18 import java.util.Map;
19 import java.util.Set;
20 import java.util.concurrent.Future;
21 import java.util.regex.Pattern;
22
23 import javax.xml.stream.XMLStreamException;
24 import javax.xml.transform.dom.DOMResult;
25 import javax.xml.transform.dom.DOMSource;
26
27 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
28 import org.opendaylight.controller.md.sal.binding.api.MountPoint;
29 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
30 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
31 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
32 import org.opendaylight.controller.md.sal.dom.api.DOMEvent;
33 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
34 import org.opendaylight.controller.md.sal.dom.api.DOMNotification;
35 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationListener;
36 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationPublishService;
37 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationService;
38 import org.opendaylight.controller.messagebus.app.impl.TopicDOMNotification;
39 import org.opendaylight.controller.messagebus.app.impl.Util;
40 import org.opendaylight.controller.messagebus.spi.EventSource;
41 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
42 import org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil;
43 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventaggregator.rev141202.NotificationPattern;
44 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventaggregator.rev141202.TopicId;
45 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventaggregator.rev141202.TopicNotification;
46 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventsource.rev141202.JoinTopicInput;
47 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventsource.rev141202.JoinTopicOutput;
48 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventsource.rev141202.JoinTopicOutputBuilder;
49 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventsource.rev141202.JoinTopicStatus;
50 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.Netconf;
51 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.Streams;
52 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.streams.Stream;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
54 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
55 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
56 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
57 import org.opendaylight.yangtools.yang.common.QName;
58 import org.opendaylight.yangtools.yang.common.RpcResult;
59 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
60 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
61 import org.opendaylight.yangtools.yang.data.api.schema.AnyXmlNode;
62 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
63 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
64 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
65 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
66 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
67 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
68 import org.slf4j.Logger;
69 import org.slf4j.LoggerFactory;
70 import org.w3c.dom.Document;
71 import org.w3c.dom.Element;
72
73 import com.google.common.base.Optional;
74 import com.google.common.base.Preconditions;
75 import com.google.common.base.Throwables;
76 import com.google.common.util.concurrent.CheckedFuture;
77
78 public class NetconfEventSource implements EventSource, DOMNotificationListener {
79
80     private static final Logger LOG = LoggerFactory.getLogger(NetconfEventSource.class);
81
82     private static final NodeIdentifier TOPIC_NOTIFICATION_ARG = new NodeIdentifier(TopicNotification.QNAME);
83     private static final NodeIdentifier EVENT_SOURCE_ARG = new NodeIdentifier(QName.create(TopicNotification.QNAME, "node-id"));
84     private static final NodeIdentifier TOPIC_ID_ARG = new NodeIdentifier(QName.create(TopicNotification.QNAME, "topic-id"));
85     private static final NodeIdentifier PAYLOAD_ARG = new NodeIdentifier(QName.create(TopicNotification.QNAME, "payload"));
86     private static final String ConnectionNotificationSourceName = "ConnectionNotificationSource";
87
88     private final String nodeId;
89     private final Node node;
90
91     private final DOMMountPoint netconfMount;
92     private final MountPoint mountPoint;
93     private final DOMNotificationPublishService domPublish;
94
95     private final Map<String, String> urnPrefixToStreamMap; // key = urnPrefix, value = StreamName
96     private final List<NotificationTopicRegistration> notificationTopicRegistrationList = new ArrayList<>();
97
98     public NetconfEventSource(final Node node, final Map<String, String> streamMap, final DOMMountPoint netconfMount, final MountPoint mountPoint, final DOMNotificationPublishService publishService) {
99         this.netconfMount = Preconditions.checkNotNull(netconfMount);
100         this.mountPoint = Preconditions.checkNotNull(mountPoint);
101         this.node = Preconditions.checkNotNull(node);
102         this.urnPrefixToStreamMap = Preconditions.checkNotNull(streamMap);
103         this.domPublish = Preconditions.checkNotNull(publishService);
104         this.nodeId = node.getNodeId().getValue();
105         this.initializeNotificationTopicRegistrationList();
106
107         LOG.info("NetconfEventSource [{}] created.", this.nodeId);
108     }
109
110     private void initializeNotificationTopicRegistrationList() {
111         notificationTopicRegistrationList.add(new ConnectionNotificationTopicRegistration(ConnectionNotificationSourceName, this));
112         Optional<Map<String, Stream>> streamMap = getAvailableStreams();
113         if(streamMap.isPresent()){
114             for (String urnPrefix : this.urnPrefixToStreamMap.keySet()) {
115                 final String streamName = this.urnPrefixToStreamMap.get(urnPrefix);
116                 if(streamMap.get().containsKey(streamName)){
117                     notificationTopicRegistrationList.add(new StreamNotificationTopicRegistration(streamMap.get().get(streamName),urnPrefix, this));
118                 }
119             }
120         }
121     }
122
123     private Optional<Map<String, Stream>> getAvailableStreams(){
124
125         Map<String,Stream> streamMap = null;
126         InstanceIdentifier<Streams> pathStream = InstanceIdentifier.builder(Netconf.class).child(Streams.class).build();
127         Optional<DataBroker> dataBroker = this.mountPoint.getService(DataBroker.class);
128
129         if(dataBroker.isPresent()){
130
131             ReadOnlyTransaction tx = dataBroker.get().newReadOnlyTransaction();
132             CheckedFuture<Optional<Streams>, ReadFailedException> checkFeature = tx.read(LogicalDatastoreType.OPERATIONAL,pathStream);
133
134             try {
135                 Optional<Streams> streams = checkFeature.checkedGet();
136                 if(streams.isPresent()){
137                     streamMap = new HashMap<>();
138                     for(Stream stream : streams.get().getStream()){
139                         streamMap.put(stream.getName().getValue(), stream);
140                     }
141                 }
142             } catch (ReadFailedException e) {
143                 LOG.warn("Can not read streams for node {}",this.nodeId);
144             }
145
146         }
147
148         return Optional.fromNullable(streamMap);
149     }
150
151     @Override
152     public Future<RpcResult<JoinTopicOutput>> joinTopic(final JoinTopicInput input) {
153
154         final NotificationPattern notificationPattern = input.getNotificationPattern();
155         final List<SchemaPath> matchingNotifications = getMatchingNotifications(notificationPattern);
156         return registerTopic(input.getTopicId(),matchingNotifications);
157
158     }
159
160     private synchronized Future<RpcResult<JoinTopicOutput>> registerTopic(final TopicId topicId, final List<SchemaPath> notificationsToSubscribe){
161
162         JoinTopicStatus joinTopicStatus = JoinTopicStatus.Down;
163         if(notificationsToSubscribe != null && notificationsToSubscribe.isEmpty() == false){
164             final Optional<DOMNotificationService> notifyService = getDOMMountPoint().getService(DOMNotificationService.class);
165             if(notifyService.isPresent()){
166                 int subscribedStreams = 0;
167                 for(SchemaPath schemaNotification : notificationsToSubscribe){
168                    for(NotificationTopicRegistration reg : notificationTopicRegistrationList){
169                       LOG.info("Source of notification {} is activating, TopicId {}", reg.getSourceName(), topicId.getValue() );
170                       reg.activateNotificationSource();
171                       boolean regSuccess = reg.registerNotificationTopic(schemaNotification, topicId);
172                       if(regSuccess){
173                          subscribedStreams = subscribedStreams +1;
174                       }
175                    }
176                 }
177                 if(subscribedStreams > 0){
178                     joinTopicStatus = JoinTopicStatus.Up;
179                 }
180             }
181         }
182
183         final JoinTopicOutput output = new JoinTopicOutputBuilder().setStatus(joinTopicStatus).build();
184         return immediateFuture(RpcResultBuilder.success(output).build());
185
186     }
187
188     public void reActivateStreams(){
189         for (NotificationTopicRegistration reg : notificationTopicRegistrationList) {
190            LOG.info("Source of notification {} is reactivating on node {}", reg.getSourceName(), this.nodeId);
191             reg.reActivateNotificationSource();
192         }
193     }
194
195     public void deActivateStreams(){
196         for (NotificationTopicRegistration reg : notificationTopicRegistrationList) {
197            LOG.info("Source of notification {} is deactivating on node {}", reg.getSourceName(), this.nodeId);
198             reg.deActivateNotificationSource();
199         }
200     }
201
202     @Override
203     public void onNotification(final DOMNotification notification) {
204         LOG.info("Notification {} has been arrived...",notification.getType());
205         SchemaPath notificationPath = notification.getType();
206         Date notificationEventTime = null;
207         if(notification instanceof DOMEvent){
208             notificationEventTime = ((DOMEvent) notification).getEventTime();
209         }
210         for(NotificationTopicRegistration notifReg : notificationTopicRegistrationList){
211             ArrayList<TopicId> topicIdsForNotification = notifReg.getNotificationTopicIds(notificationPath);
212             if(topicIdsForNotification != null && topicIdsForNotification.isEmpty() == false){
213
214                 if(notifReg instanceof StreamNotificationTopicRegistration){
215                     StreamNotificationTopicRegistration streamReg = (StreamNotificationTopicRegistration)notifReg;
216                     streamReg.setLastEventTime(notificationEventTime);
217                 }
218
219                 for(TopicId topicId : topicIdsForNotification){
220                     publishNotification(notification, topicId);
221                     LOG.info("Notification {} has been published for TopicId {}",notification.getType(), topicId.getValue());
222                 }
223
224             }
225         }
226     }
227
228     private void publishNotification(final DOMNotification notification, TopicId topicId){
229          final ContainerNode topicNotification = Builders.containerBuilder()
230                  .withNodeIdentifier(TOPIC_NOTIFICATION_ARG)
231                  .withChild(ImmutableNodes.leafNode(TOPIC_ID_ARG, topicId))
232                  .withChild(ImmutableNodes.leafNode(EVENT_SOURCE_ARG, this.nodeId))
233                  .withChild(encapsulate(notification))
234                  .build();
235          try {
236              domPublish.putNotification(new TopicDOMNotification(topicNotification));
237          } catch (final InterruptedException e) {
238              throw Throwables.propagate(e);
239          }
240     }
241
242     private AnyXmlNode encapsulate(final DOMNotification body) {
243         // FIXME: Introduce something like AnyXmlWithNormalizedNodeData in Yangtools
244         final Document doc = XmlUtil.newDocument();
245         final Optional<String> namespace = Optional.of(PAYLOAD_ARG.getNodeType().getNamespace().toString());
246         final Element element = XmlUtil.createElement(doc , "payload", namespace);
247
248         final DOMResult result = new DOMResult(element);
249
250         final SchemaContext context = getDOMMountPoint().getSchemaContext();
251         final SchemaPath schemaPath = body.getType();
252         try {
253             NetconfMessageTransformUtil.writeNormalizedNode(body.getBody(), result, schemaPath, context);
254             return Builders.anyXmlBuilder().withNodeIdentifier(PAYLOAD_ARG)
255                     .withValue(new DOMSource(element))
256                     .build();
257         } catch (IOException | XMLStreamException e) {
258             LOG.error("Unable to encapsulate notification.",e);
259             throw Throwables.propagate(e);
260         }
261     }
262
263     private List<SchemaPath> getMatchingNotifications(NotificationPattern notificationPattern){
264         // FIXME: default language should already be regex
265         final String regex = Util.wildcardToRegex(notificationPattern.getValue());
266
267         final Pattern pattern = Pattern.compile(regex);
268         List<SchemaPath> availableNotifications = getAvailableNotifications();
269         if(availableNotifications == null || availableNotifications.isEmpty()){
270             return null;
271         }
272         return Util.expandQname(availableNotifications, pattern);
273     }
274
275     @Override
276     public void close() throws Exception {
277         for(NotificationTopicRegistration streamReg : notificationTopicRegistrationList){
278             streamReg.close();
279         }
280     }
281
282     @Override
283     public NodeKey getSourceNodeKey(){
284         return getNode().getKey();
285     }
286
287     @Override
288     public List<SchemaPath> getAvailableNotifications() {
289
290         final List<SchemaPath> availNotifList = new ArrayList<>();
291         // add Event Source Connection status notification
292         availNotifList.add(ConnectionNotificationTopicRegistration.EVENT_SOURCE_STATUS_PATH);
293
294         // FIXME: use SchemaContextListener to get changes asynchronously
295         final Set<NotificationDefinition> availableNotifications = getDOMMountPoint().getSchemaContext().getNotifications();
296         // add all known notifications from netconf device
297         for (final NotificationDefinition nd : availableNotifications) {
298             availNotifList.add(nd.getPath());
299         }
300         return availNotifList;
301     }
302
303     public Node getNode() {
304         return node;
305     }
306
307     DOMMountPoint getDOMMountPoint() {
308         return netconfMount;
309     }
310
311     MountPoint getMountPoint() {
312         return mountPoint;
313     }
314
315     NetconfNode getNetconfNode(){
316         return node.getAugmentation(NetconfNode.class);
317     }
318
319 }