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.mapping.api.SessionAwareNetconfOperation;
20 import org.opendaylight.netconf.notifications.NetconfNotification;
21 import org.opendaylight.netconf.notifications.NetconfNotificationListener;
22 import org.opendaylight.netconf.notifications.NetconfNotificationRegistry;
23 import org.opendaylight.netconf.notifications.NotificationListenerRegistration;
24 import org.opendaylight.netconf.notifications.impl.NetconfNotificationManager;
25 import org.opendaylight.netconf.util.mapping.AbstractSingletonNetconfOperation;
26 import org.opendaylight.netconf.util.messages.SubtreeFilter;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.CreateSubscriptionInput;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.StreamNameType;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.w3c.dom.Document;
32 import org.w3c.dom.Element;
35 * Create subscription listens for create subscription requests
36 * and registers notification listeners into notification registry.
37 * Received notifications are sent to the client right away
39 public class CreateSubscription extends AbstractSingletonNetconfOperation
40 implements SessionAwareNetconfOperation, AutoCloseable {
42 private static final Logger LOG = LoggerFactory.getLogger(CreateSubscription.class);
44 static final String CREATE_SUBSCRIPTION = "create-subscription";
46 private final NetconfNotificationRegistry notifications;
47 private final List<NotificationListenerRegistration> subscriptions = new ArrayList<>();
48 private NetconfSession netconfSession;
50 public CreateSubscription(final String netconfSessionIdForReporting,
51 final NetconfNotificationRegistry notifications) {
52 super(netconfSessionIdForReporting);
53 this.notifications = notifications;
57 protected Element handleWithNoSubsequentOperations(final Document document,
58 final XmlElement operationElement) throws DocumentedException {
59 operationElement.checkName(CREATE_SUBSCRIPTION);
60 operationElement.checkNamespace(CreateSubscriptionInput.QNAME.getNamespace().toString());
61 // FIXME reimplement using CODEC_REGISTRY and parse everything into generated class instance
62 // Binding doesn't support anyxml nodes yet, so filter could not be retrieved
63 // xml -> normalized node -> CreateSubscriptionInput conversion could be slower than current approach
65 final Optional<XmlElement> filter = operationElement.getOnlyChildElementWithSameNamespaceOptionally("filter");
67 // Replay not supported
68 final Optional<XmlElement> startTime =
69 operationElement.getOnlyChildElementWithSameNamespaceOptionally("startTime");
70 Preconditions.checkArgument(!startTime.isPresent(), "StartTime element not yet supported");
72 // Stop time not supported
73 final Optional<XmlElement> stopTime =
74 operationElement.getOnlyChildElementWithSameNamespaceOptionally("stopTime");
75 Preconditions.checkArgument(!stopTime.isPresent(), "StopTime element not yet supported");
77 final StreamNameType streamNameType = parseStreamIfPresent(operationElement);
79 Preconditions.checkNotNull(netconfSession);
80 // Premature streams are allowed (meaning listener can register even if no provider is available yet)
81 if (!notifications.isStreamAvailable(streamNameType)) {
82 LOG.warn("Registering premature stream {}. No publisher available yet for session {}", streamNameType,
83 getNetconfSessionIdForReporting());
86 final NotificationListenerRegistration notificationListenerRegistration = notifications
87 .registerNotificationListener(streamNameType, new NotificationSubscription(netconfSession, filter));
88 subscriptions.add(notificationListenerRegistration);
90 return document.createElement(XmlNetconfConstants.OK);
93 private static StreamNameType parseStreamIfPresent(final XmlElement operationElement) throws DocumentedException {
94 final Optional<XmlElement> stream = operationElement.getOnlyChildElementWithSameNamespaceOptionally("stream");
95 return stream.isPresent() ? new StreamNameType(stream.get().getTextContent())
96 : NetconfNotificationManager.BASE_STREAM_NAME;
100 protected String getOperationName() {
101 return CREATE_SUBSCRIPTION;
105 protected String getOperationNamespace() {
106 return CreateSubscriptionInput.QNAME.getNamespace().toString();
110 public void setSession(final NetconfSession session) {
111 this.netconfSession = session;
115 public void close() {
116 netconfSession = null;
117 // Unregister from notification streams
118 for (final NotificationListenerRegistration subscription : subscriptions) {
119 subscription.close();
123 private static class NotificationSubscription implements NetconfNotificationListener {
124 private final NetconfSession currentSession;
125 private final Optional<XmlElement> filter;
127 NotificationSubscription(final NetconfSession currentSession, final Optional<XmlElement> filter) {
128 this.currentSession = currentSession;
129 this.filter = filter;
133 public void onNotification(final StreamNameType stream, final NetconfNotification notification) {
134 if (filter.isPresent()) {
136 final Optional<Document> filtered =
137 SubtreeFilter.applySubtreeNotificationFilter(this.filter.get(), notification.getDocument());
138 if (filtered.isPresent()) {
139 final Date eventTime = notification.getEventTime();
140 currentSession.sendMessage(new NetconfNotification(filtered.get(), eventTime));
142 } catch (DocumentedException e) {
143 LOG.warn("Failed to process notification {}", notification, e);
144 currentSession.sendMessage(notification);
147 currentSession.sendMessage(notification);