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
8 package org.opendaylight.netconf.notifications.impl.ops;
10 import com.google.common.base.Preconditions;
11 import java.util.ArrayList;
12 import java.util.Date;
13 import java.util.List;
14 import java.util.Optional;
15 import org.opendaylight.netconf.api.DocumentedException;
16 import org.opendaylight.netconf.api.NetconfSession;
17 import org.opendaylight.netconf.api.xml.XmlElement;
18 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
19 import org.opendaylight.netconf.api.xml.XmlUtil;
20 import org.opendaylight.netconf.mapping.api.SessionAwareNetconfOperation;
21 import org.opendaylight.netconf.notifications.NetconfNotification;
22 import org.opendaylight.netconf.notifications.NetconfNotificationListener;
23 import org.opendaylight.netconf.notifications.NetconfNotificationRegistry;
24 import org.opendaylight.netconf.notifications.NotificationListenerRegistration;
25 import org.opendaylight.netconf.notifications.impl.NetconfNotificationManager;
26 import org.opendaylight.netconf.util.mapping.AbstractSingletonNetconfOperation;
27 import org.opendaylight.netconf.util.messages.SubtreeFilter;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.CreateSubscriptionInput;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.StreamNameType;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.w3c.dom.Document;
33 import org.w3c.dom.Element;
36 * Create subscription listens for create subscription requests
37 * and registers notification listeners into notification registry.
38 * Received notifications are sent to the client right away
40 public class CreateSubscription extends AbstractSingletonNetconfOperation
41 implements SessionAwareNetconfOperation, AutoCloseable {
43 private static final Logger LOG = LoggerFactory.getLogger(CreateSubscription.class);
45 static final String CREATE_SUBSCRIPTION = "create-subscription";
47 private final NetconfNotificationRegistry notifications;
48 private final List<NotificationListenerRegistration> subscriptions = new ArrayList<>();
49 private NetconfSession netconfSession;
51 public CreateSubscription(final String netconfSessionIdForReporting,
52 final NetconfNotificationRegistry notifications) {
53 super(netconfSessionIdForReporting);
54 this.notifications = notifications;
58 protected Element handleWithNoSubsequentOperations(final Document document,
59 final XmlElement operationElement) throws DocumentedException {
60 operationElement.checkName(CREATE_SUBSCRIPTION);
61 operationElement.checkNamespace(CreateSubscriptionInput.QNAME.getNamespace().toString());
62 // FIXME reimplement using CODEC_REGISTRY and parse everything into generated class instance
63 // Binding doesn't support anyxml nodes yet, so filter could not be retrieved
64 // xml -> normalized node -> CreateSubscriptionInput conversion could be slower than current approach
66 final Optional<XmlElement> filter = operationElement.getOnlyChildElementWithSameNamespaceOptionally("filter");
68 // Replay not supported
69 final Optional<XmlElement> startTime =
70 operationElement.getOnlyChildElementWithSameNamespaceOptionally("startTime");
71 Preconditions.checkArgument(!startTime.isPresent(), "StartTime element not yet supported");
73 // Stop time not supported
74 final Optional<XmlElement> stopTime =
75 operationElement.getOnlyChildElementWithSameNamespaceOptionally("stopTime");
76 Preconditions.checkArgument(!stopTime.isPresent(), "StopTime element not yet supported");
78 final StreamNameType streamNameType = parseStreamIfPresent(operationElement);
80 Preconditions.checkNotNull(netconfSession);
81 // Premature streams are allowed (meaning listener can register even if no provider is available yet)
82 if (!notifications.isStreamAvailable(streamNameType)) {
83 LOG.warn("Registering premature stream {}. No publisher available yet for session {}", streamNameType,
84 getNetconfSessionIdForReporting());
87 final NotificationListenerRegistration notificationListenerRegistration = notifications
88 .registerNotificationListener(streamNameType, new NotificationSubscription(netconfSession, filter));
89 subscriptions.add(notificationListenerRegistration);
91 return XmlUtil.createElement(document, XmlNetconfConstants.OK);
94 private static StreamNameType parseStreamIfPresent(final XmlElement operationElement) throws DocumentedException {
95 final Optional<XmlElement> stream = operationElement.getOnlyChildElementWithSameNamespaceOptionally("stream");
96 return stream.isPresent() ? new StreamNameType(stream.get().getTextContent())
97 : NetconfNotificationManager.BASE_STREAM_NAME;
101 protected String getOperationName() {
102 return CREATE_SUBSCRIPTION;
106 protected String getOperationNamespace() {
107 return CreateSubscriptionInput.QNAME.getNamespace().toString();
111 public void setSession(final NetconfSession session) {
112 this.netconfSession = session;
116 public void close() {
117 netconfSession = null;
118 // Unregister from notification streams
119 for (final NotificationListenerRegistration subscription : subscriptions) {
120 subscription.close();
124 private static class NotificationSubscription implements NetconfNotificationListener {
125 private final NetconfSession currentSession;
126 private final Optional<XmlElement> filter;
128 NotificationSubscription(final NetconfSession currentSession, final Optional<XmlElement> filter) {
129 this.currentSession = currentSession;
130 this.filter = filter;
134 public void onNotification(final StreamNameType stream, final NetconfNotification notification) {
135 if (filter.isPresent()) {
137 final Optional<Document> filtered =
138 SubtreeFilter.applySubtreeNotificationFilter(this.filter.get(), notification.getDocument());
139 if (filtered.isPresent()) {
140 final Date eventTime = notification.getEventTime();
141 currentSession.sendMessage(new NetconfNotification(filtered.get(), eventTime));
143 } catch (DocumentedException e) {
144 LOG.warn("Failed to process notification {}", notification, e);
145 currentSession.sendMessage(notification);
148 currentSession.sendMessage(notification);