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
9 package org.opendaylight.netconf.messagebus.eventsources.netconf;
11 import static com.google.common.util.concurrent.Futures.immediateFuture;
13 import com.google.common.base.Function;
14 import com.google.common.base.Optional;
15 import com.google.common.base.Preconditions;
16 import com.google.common.base.Throwables;
17 import com.google.common.collect.ArrayListMultimap;
18 import com.google.common.collect.Maps;
19 import com.google.common.collect.Multimap;
20 import com.google.common.collect.Multimaps;
21 import java.io.IOException;
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.Date;
25 import java.util.HashMap;
26 import java.util.List;
29 import java.util.concurrent.Future;
30 import java.util.regex.Pattern;
31 import javax.annotation.Nullable;
32 import javax.xml.stream.XMLStreamException;
33 import javax.xml.transform.dom.DOMResult;
34 import javax.xml.transform.dom.DOMSource;
35 import org.opendaylight.controller.config.util.xml.XmlUtil;
36 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
37 import org.opendaylight.controller.md.sal.dom.api.DOMEvent;
38 import org.opendaylight.controller.md.sal.dom.api.DOMNotification;
39 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationListener;
40 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationPublishService;
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.util.NetconfUtil;
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.streams.Stream;
54 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
55 import org.opendaylight.yangtools.yang.common.QName;
56 import org.opendaylight.yangtools.yang.common.RpcResult;
57 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
58 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
59 import org.opendaylight.yangtools.yang.data.api.schema.AnyXmlNode;
60 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
61 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
62 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
63 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
64 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
65 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
66 import org.slf4j.Logger;
67 import org.slf4j.LoggerFactory;
68 import org.w3c.dom.Document;
69 import org.w3c.dom.Element;
72 * NetconfEventSource serves as proxy between nodes and messagebus. Subscribers can join topic stream from this source.
73 * Then they will receive notifications from device that matches pattern specified by topic.
75 public class NetconfEventSource implements EventSource, DOMNotificationListener {
77 private static final Logger LOG = LoggerFactory.getLogger(NetconfEventSource.class);
79 private static final NodeIdentifier TOPIC_NOTIFICATION_ARG = NodeIdentifier.create(TopicNotification.QNAME);
80 private static final NodeIdentifier EVENT_SOURCE_ARG = NodeIdentifier.create(
81 QName.create(TopicNotification.QNAME, "node-id"));
82 private static final NodeIdentifier TOPIC_ID_ARG = NodeIdentifier.create(
83 QName.create(TopicNotification.QNAME, "topic-id"));
84 private static final NodeIdentifier PAYLOAD_ARG = NodeIdentifier.create(
85 QName.create(TopicNotification.QNAME, "payload"));
86 private static final String CONNECTION_NOTIFICATION_SOURCE_NAME = "ConnectionNotificationSource";
88 private final DOMNotificationPublishService domPublish;
90 private final Map<String, String> urnPrefixToStreamMap; // key = urnPrefix, value = StreamName
93 * Map notification uri -> registrations.
95 private final Multimap<String, NotificationTopicRegistration>
96 notificationTopicRegistrations = Multimaps.synchronizedListMultimap(ArrayListMultimap.create());
97 private final NetconfEventSourceMount mount;
100 * Creates new NetconfEventSource for node. Topic notifications will be published via provided
101 * {@link DOMNotificationPublishService}
103 * @param streamMap netconf streams from device
104 * @param publishService publish service
106 public NetconfEventSource(final Map<String, String> streamMap,
107 final NetconfEventSourceMount mount,
108 final DOMNotificationPublishService publishService) {
110 this.urnPrefixToStreamMap = Preconditions.checkNotNull(streamMap);
111 this.domPublish = Preconditions.checkNotNull(publishService);
112 this.initializeNotificationTopicRegistrationList();
114 LOG.info("NetconfEventSource [{}] created.", mount.getNodeId());
118 * Creates {@link ConnectionNotificationTopicRegistration} for connection. Also creates
119 * {@link StreamNotificationTopicRegistration} for every prefix and available stream as defined in config file.
121 private void initializeNotificationTopicRegistrationList() {
122 final ConnectionNotificationTopicRegistration cntr =
123 new ConnectionNotificationTopicRegistration(CONNECTION_NOTIFICATION_SOURCE_NAME, this);
124 notificationTopicRegistrations
125 .put(cntr.getNotificationUrnPrefix(), cntr);
126 Map<String, Stream> availableStreams = getAvailableStreams();
127 LOG.debug("Stream configuration compare...");
128 for (String urnPrefix : this.urnPrefixToStreamMap.keySet()) {
129 final String streamName = this.urnPrefixToStreamMap.get(urnPrefix);
130 LOG.debug("urnPrefix: {} streamName: {}", urnPrefix, streamName);
131 if (availableStreams.containsKey(streamName)) {
132 LOG.debug("Stream containig on device");
133 notificationTopicRegistrations
134 .put(urnPrefix, new StreamNotificationTopicRegistration(availableStreams.get(streamName),
140 private Map<String, Stream> getAvailableStreams() {
141 Map<String, Stream> streamMap = new HashMap<>();
142 final List<Stream> availableStreams;
144 availableStreams = mount.getAvailableStreams();
145 streamMap = Maps.uniqueIndex(availableStreams, new Function<Stream, String>() {
148 public String apply(@Nullable Stream input) {
149 return input.getName().getValue();
152 } catch (ReadFailedException e) {
153 LOG.warn("Can not read streams for node {}", mount.getNodeId());
159 public Future<RpcResult<JoinTopicOutput>> joinTopic(final JoinTopicInput input) {
160 LOG.debug("Join topic {} on {}", input.getTopicId().getValue(), mount.getNodeId());
161 final NotificationPattern notificationPattern = input.getNotificationPattern();
162 final List<SchemaPath> matchingNotifications = getMatchingNotifications(notificationPattern);
163 return registerTopic(input.getTopicId(), matchingNotifications);
168 public Future<RpcResult<Void>> disJoinTopic(DisJoinTopicInput input) {
169 for (NotificationTopicRegistration reg : notificationTopicRegistrations.values()) {
170 reg.unRegisterNotificationTopic(input.getTopicId());
172 return Util.resultRpcSuccessFor((Void) null);
175 private synchronized Future<RpcResult<JoinTopicOutput>> registerTopic(
176 final TopicId topicId,
177 final List<SchemaPath> notificationsToSubscribe) {
178 Preconditions.checkNotNull(notificationsToSubscribe);
179 LOG.debug("Join topic {} - register", topicId);
180 JoinTopicStatus joinTopicStatus = JoinTopicStatus.Down;
182 LOG.debug("Notifications to subscribe has found - count {}", notificationsToSubscribe.size());
183 int registeredNotificationCount = 0;
184 for (SchemaPath schemaPath : notificationsToSubscribe) {
185 final Collection<NotificationTopicRegistration> topicRegistrations =
186 notificationTopicRegistrations.get(schemaPath.getLastComponent().getNamespace().toString());
187 for (NotificationTopicRegistration reg : topicRegistrations) {
188 LOG.info("Source of notification {} is activating, TopicId {}", reg.getSourceName(),
190 boolean regSuccess = reg.registerNotificationTopic(schemaPath, topicId);
192 registeredNotificationCount = registeredNotificationCount + 1;
196 if (registeredNotificationCount > 0) {
197 joinTopicStatus = JoinTopicStatus.Up;
199 final JoinTopicOutput output = new JoinTopicOutputBuilder().setStatus(joinTopicStatus).build();
200 return immediateFuture(RpcResultBuilder.success(output).build());
204 public void reActivateStreams() {
205 for (NotificationTopicRegistration reg : notificationTopicRegistrations.values()) {
206 LOG.info("Source of notification {} is reactivating on node {}", reg.getSourceName(), mount.getNodeId());
207 reg.reActivateNotificationSource();
211 public void deActivateStreams() {
212 for (NotificationTopicRegistration reg : notificationTopicRegistrations.values()) {
213 LOG.info("Source of notification {} is deactivating on node {}", reg.getSourceName(), mount.getNodeId());
214 reg.deActivateNotificationSource();
219 public void onNotification(final DOMNotification notification) {
220 SchemaPath notificationPath = notification.getType();
221 Date notificationEventTime = null;
222 if (notification instanceof DOMEvent) {
223 notificationEventTime = ((DOMEvent) notification).getEventTime();
225 final String namespace = notification.getType().getLastComponent().getNamespace().toString();
226 for (NotificationTopicRegistration notifReg : notificationTopicRegistrations.get(namespace)) {
227 notifReg.setLastEventTime(notificationEventTime);
228 Set<TopicId> topicIdsForNotification = notifReg.getTopicsForNotification(notificationPath);
229 for (TopicId topicId : topicIdsForNotification) {
230 publishNotification(notification, topicId);
231 LOG.debug("Notification {} has been published for TopicId {}", notification.getType(),
237 private void publishNotification(final DOMNotification notification, TopicId topicId) {
238 final ContainerNode topicNotification = Builders.containerBuilder().withNodeIdentifier(TOPIC_NOTIFICATION_ARG)
239 .withChild(ImmutableNodes.leafNode(TOPIC_ID_ARG, topicId))
240 .withChild(ImmutableNodes.leafNode(EVENT_SOURCE_ARG, mount.getNodeId()))
241 .withChild(encapsulate(notification))
244 domPublish.putNotification(new TopicDOMNotification(topicNotification));
245 } catch (final InterruptedException e) {
246 throw Throwables.propagate(e);
250 private AnyXmlNode encapsulate(final DOMNotification body) {
251 // FIXME: Introduce something like YangModeledAnyXmlNode in Yangtools
252 final Document doc = XmlUtil.newDocument();
253 final Optional<String> namespace = Optional.of(PAYLOAD_ARG.getNodeType().getNamespace().toString());
254 final Element element = XmlUtil.createElement(doc, "payload", namespace);
256 final DOMResult result = new DOMResult(element);
258 final SchemaContext context = mount.getSchemaContext();
259 final SchemaPath schemaPath = body.getType();
261 NetconfUtil.writeNormalizedNode(body.getBody(), result, schemaPath, context);
262 return Builders.anyXmlBuilder().withNodeIdentifier(PAYLOAD_ARG).withValue(new DOMSource(element)).build();
263 } catch (IOException | XMLStreamException e) {
264 LOG.error("Unable to encapsulate notification.", e);
265 throw Throwables.propagate(e);
270 * Returns all available notification paths that matches given pattern.
272 * @param notificationPattern pattern
273 * @return notification paths
275 private List<SchemaPath> getMatchingNotifications(NotificationPattern notificationPattern) {
276 final String regex = notificationPattern.getValue();
278 final Pattern pattern = Pattern.compile(regex);
279 List<SchemaPath> availableNotifications = getAvailableNotifications();
280 return Util.expandQname(availableNotifications, pattern);
284 public void close() throws Exception {
285 for (NotificationTopicRegistration streamReg : notificationTopicRegistrations.values()) {
291 public NodeKey getSourceNodeKey() {
292 return mount.getNode().getKey();
296 public List<SchemaPath> getAvailableNotifications() {
298 final List<SchemaPath> availNotifList = new ArrayList<>();
299 // add Event Source Connection status notification
300 availNotifList.add(ConnectionNotificationTopicRegistration.EVENT_SOURCE_STATUS_PATH);
302 final Set<NotificationDefinition> availableNotifications = mount.getSchemaContext()
304 // add all known notifications from netconf device
305 for (final NotificationDefinition nd : availableNotifications) {
306 availNotifList.add(nd.getPath());
308 return availNotifList;
311 NetconfEventSourceMount getMount() {