Decouple message bus from netconf connector
[controller.git] / opendaylight / netconf / messagebus-netconf / 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 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.controller.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 public class NetconfEventSource implements EventSource, DOMNotificationListener {
77
78     private static final Logger LOG = LoggerFactory.getLogger(NetconfEventSource.class);
79
80     private static final NodeIdentifier TOPIC_NOTIFICATION_ARG = new NodeIdentifier(TopicNotification.QNAME);
81     private static final NodeIdentifier EVENT_SOURCE_ARG = new NodeIdentifier(
82         QName.create(TopicNotification.QNAME, "node-id"));
83     private static final NodeIdentifier TOPIC_ID_ARG = new NodeIdentifier(
84         QName.create(TopicNotification.QNAME, "topic-id"));
85     private static final NodeIdentifier PAYLOAD_ARG = new NodeIdentifier(
86         QName.create(TopicNotification.QNAME, "payload"));
87     private static final String ConnectionNotificationSourceName = "ConnectionNotificationSource";
88
89     private final String nodeId;
90     private final Node node;
91
92     private final DOMMountPoint netconfMount;
93     private final MountPoint mountPoint;
94     private final DOMNotificationPublishService domPublish;
95
96     private final Map<String, String> urnPrefixToStreamMap; // key = urnPrefix, value = StreamName
97     private final List<NotificationTopicRegistration> notificationTopicRegistrationList = new ArrayList<>();
98
99     public NetconfEventSource(final Node node, final Map<String, String> streamMap, final DOMMountPoint netconfMount,
100         final MountPoint mountPoint, final DOMNotificationPublishService publishService) {
101         this.netconfMount = Preconditions.checkNotNull(netconfMount);
102         this.mountPoint = Preconditions.checkNotNull(mountPoint);
103         this.node = Preconditions.checkNotNull(node);
104         this.urnPrefixToStreamMap = Preconditions.checkNotNull(streamMap);
105         this.domPublish = Preconditions.checkNotNull(publishService);
106         this.nodeId = node.getNodeId().getValue();
107         this.initializeNotificationTopicRegistrationList();
108
109         LOG.info("NetconfEventSource [{}] created.", this.nodeId);
110     }
111
112     private void initializeNotificationTopicRegistrationList() {
113         notificationTopicRegistrationList
114             .add(new ConnectionNotificationTopicRegistration(ConnectionNotificationSourceName, this));
115         Optional<Map<String, Stream>> streamMap = getAvailableStreams();
116         if (streamMap.isPresent()) {
117             LOG.debug("Stream configuration compare...");
118             for (String urnPrefix : this.urnPrefixToStreamMap.keySet()) {
119                 final String streamName = this.urnPrefixToStreamMap.get(urnPrefix);
120                 LOG.debug("urnPrefix: {} streamName: {}", urnPrefix, streamName);
121                 if (streamMap.get().containsKey(streamName)) {
122                     LOG.debug("Stream containig on device");
123                     notificationTopicRegistrationList
124                         .add(new StreamNotificationTopicRegistration(streamMap.get().get(streamName), urnPrefix, this));
125                 }
126             }
127         }
128     }
129
130     private Optional<Map<String, Stream>> getAvailableStreams() {
131
132         Map<String, Stream> streamMap = null;
133         InstanceIdentifier<Streams> pathStream = InstanceIdentifier.builder(Netconf.class).child(Streams.class).build();
134         Optional<DataBroker> dataBroker = this.mountPoint.getService(DataBroker.class);
135
136         if (dataBroker.isPresent()) {
137             LOG.debug("GET Available streams ...");
138             ReadOnlyTransaction tx = dataBroker.get().newReadOnlyTransaction();
139             CheckedFuture<Optional<Streams>, ReadFailedException> checkFeature = tx
140                 .read(LogicalDatastoreType.OPERATIONAL, pathStream);
141
142             try {
143                 Optional<Streams> streams = checkFeature.checkedGet();
144                 if (streams.isPresent()) {
145                     streamMap = new HashMap<>();
146                     for (Stream stream : streams.get().getStream()) {
147                         LOG.debug("*** find stream {}", stream.getName().getValue());
148                         streamMap.put(stream.getName().getValue(), stream);
149                     }
150                 }
151             } catch (ReadFailedException e) {
152                 LOG.warn("Can not read streams for node {}", this.nodeId);
153             }
154
155         } else {
156             LOG.warn("No databroker on node {}", this.nodeId);
157         }
158
159         return Optional.fromNullable(streamMap);
160     }
161
162     @Override public Future<RpcResult<JoinTopicOutput>> joinTopic(final JoinTopicInput input) {
163         LOG.debug("Join topic {} on {}", input.getTopicId().getValue(), this.nodeId);
164         final NotificationPattern notificationPattern = input.getNotificationPattern();
165         final List<SchemaPath> matchingNotifications = getMatchingNotifications(notificationPattern);
166         return registerTopic(input.getTopicId(), matchingNotifications);
167
168     }
169
170     @Override public Future<RpcResult<Void>> disJoinTopic(DisJoinTopicInput input) {
171         for (NotificationTopicRegistration reg : notificationTopicRegistrationList) {
172             reg.unRegisterNotificationTopic(input.getTopicId());
173         }
174         return Util.resultRpcSuccessFor((Void) null);
175     }
176
177     private synchronized Future<RpcResult<JoinTopicOutput>> registerTopic(final TopicId topicId,
178         final List<SchemaPath> notificationsToSubscribe) {
179         LOG.debug("Join topic {} - register", topicId);
180         JoinTopicStatus joinTopicStatus = JoinTopicStatus.Down;
181         if (notificationsToSubscribe != null && notificationsToSubscribe.isEmpty() == false) {
182             LOG.debug("Notifications to subscribe has found - count {}", notificationsToSubscribe.size());
183             final Optional<DOMNotificationService> notifyService = getDOMMountPoint()
184                 .getService(DOMNotificationService.class);
185             if (notifyService.isPresent()) {
186                 int registeredNotificationCount = 0;
187                 for (SchemaPath schemaNotification : notificationsToSubscribe) {
188                     for (NotificationTopicRegistration reg : notificationTopicRegistrationList) {
189                         LOG.debug("Try notification registratio {} on SchemaPathNotification {}", reg.getSourceName(),
190                             schemaNotification.getLastComponent().getLocalName());
191                         if (reg.checkNotificationPath(schemaNotification)) {
192                             LOG.info("Source of notification {} is activating, TopicId {}", reg.getSourceName(),
193                                 topicId.getValue());
194                             boolean regSuccess = reg.registerNotificationTopic(schemaNotification, topicId);
195                             if (regSuccess) {
196                                 registeredNotificationCount = registeredNotificationCount + 1;
197                             }
198                         }
199                     }
200                 }
201                 if (registeredNotificationCount > 0) {
202                     joinTopicStatus = JoinTopicStatus.Up;
203                 }
204             } else {
205                 LOG.warn("NO DOMNotification service on node {}", this.nodeId);
206             }
207         } else {
208             LOG.debug("Notifications to subscribe has NOT found");
209         }
210
211         final JoinTopicOutput output = new JoinTopicOutputBuilder().setStatus(joinTopicStatus).build();
212         return immediateFuture(RpcResultBuilder.success(output).build());
213
214     }
215
216     public void reActivateStreams() {
217         for (NotificationTopicRegistration reg : notificationTopicRegistrationList) {
218             LOG.info("Source of notification {} is reactivating on node {}", reg.getSourceName(), this.nodeId);
219             reg.reActivateNotificationSource();
220         }
221     }
222
223     public void deActivateStreams() {
224         for (NotificationTopicRegistration reg : notificationTopicRegistrationList) {
225             LOG.info("Source of notification {} is deactivating on node {}", reg.getSourceName(), this.nodeId);
226             reg.deActivateNotificationSource();
227         }
228     }
229
230     @Override public void onNotification(final DOMNotification notification) {
231         SchemaPath notificationPath = notification.getType();
232         Date notificationEventTime = null;
233         if (notification instanceof DOMEvent) {
234             notificationEventTime = ((DOMEvent) notification).getEventTime();
235         }
236         for (NotificationTopicRegistration notifReg : notificationTopicRegistrationList) {
237             ArrayList<TopicId> topicIdsForNotification = notifReg.getNotificationTopicIds(notificationPath);
238             if (topicIdsForNotification != null && topicIdsForNotification.isEmpty() == false) {
239
240                 if (notifReg instanceof StreamNotificationTopicRegistration) {
241                     StreamNotificationTopicRegistration streamReg = (StreamNotificationTopicRegistration) notifReg;
242                     streamReg.setLastEventTime(notificationEventTime);
243                 }
244
245                 for (TopicId topicId : topicIdsForNotification) {
246                     publishNotification(notification, topicId);
247                     LOG.debug("Notification {} has been published for TopicId {}", notification.getType(),
248                         topicId.getValue());
249                 }
250
251             }
252         }
253     }
254
255     private void publishNotification(final DOMNotification notification, TopicId topicId) {
256         final ContainerNode topicNotification = Builders.containerBuilder().withNodeIdentifier(TOPIC_NOTIFICATION_ARG)
257             .withChild(ImmutableNodes.leafNode(TOPIC_ID_ARG, topicId))
258             .withChild(ImmutableNodes.leafNode(EVENT_SOURCE_ARG, this.nodeId)).withChild(encapsulate(notification))
259             .build();
260         try {
261             domPublish.putNotification(new TopicDOMNotification(topicNotification));
262         } catch (final InterruptedException e) {
263             throw Throwables.propagate(e);
264         }
265     }
266
267     private AnyXmlNode encapsulate(final DOMNotification body) {
268         // FIXME: Introduce something like AnyXmlWithNormalizedNodeData in Yangtools
269         final Document doc = XmlUtil.newDocument();
270         final Optional<String> namespace = Optional.of(PAYLOAD_ARG.getNodeType().getNamespace().toString());
271         final Element element = XmlUtil.createElement(doc, "payload", namespace);
272
273         final DOMResult result = new DOMResult(element);
274
275         final SchemaContext context = getDOMMountPoint().getSchemaContext();
276         final SchemaPath schemaPath = body.getType();
277         try {
278             NetconfMessageTransformUtil.writeNormalizedNode(body.getBody(), result, schemaPath, context);
279             return Builders.anyXmlBuilder().withNodeIdentifier(PAYLOAD_ARG).withValue(new DOMSource(element)).build();
280         } catch (IOException | XMLStreamException e) {
281             LOG.error("Unable to encapsulate notification.", e);
282             throw Throwables.propagate(e);
283         }
284     }
285
286     private List<SchemaPath> getMatchingNotifications(NotificationPattern notificationPattern) {
287         // FIXME: default language should already be regex
288         final String regex = Util.wildcardToRegex(notificationPattern.getValue());
289
290         final Pattern pattern = Pattern.compile(regex);
291         List<SchemaPath> availableNotifications = getAvailableNotifications();
292         if (availableNotifications == null || availableNotifications.isEmpty()) {
293             return null;
294         }
295         return Util.expandQname(availableNotifications, pattern);
296     }
297
298     @Override public void close() throws Exception {
299         for (NotificationTopicRegistration streamReg : notificationTopicRegistrationList) {
300             streamReg.close();
301         }
302     }
303
304     @Override public NodeKey getSourceNodeKey() {
305         return getNode().getKey();
306     }
307
308     @Override public List<SchemaPath> getAvailableNotifications() {
309
310         final List<SchemaPath> availNotifList = new ArrayList<>();
311         // add Event Source Connection status notification
312         availNotifList.add(ConnectionNotificationTopicRegistration.EVENT_SOURCE_STATUS_PATH);
313
314         // FIXME: use SchemaContextListener to get changes asynchronously
315         final Set<NotificationDefinition> availableNotifications = getDOMMountPoint().getSchemaContext()
316             .getNotifications();
317         // add all known notifications from netconf device
318         for (final NotificationDefinition nd : availableNotifications) {
319             availNotifList.add(nd.getPath());
320         }
321         return availNotifList;
322     }
323
324     public Node getNode() {
325         return node;
326     }
327
328     DOMMountPoint getDOMMountPoint() {
329         return netconfMount;
330     }
331
332     MountPoint getMountPoint() {
333         return mountPoint;
334     }
335
336     NetconfNode getNetconfNode() {
337         return node.getAugmentation(NetconfNode.class);
338     }
339
340 }