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