Merge "Bug 2669: Use slf4j Logger instead of akka LoggingAdapter"
[controller.git] / opendaylight / netconf / netconf-notifications-impl / src / main / java / org / opendaylight / controller / netconf / notifications / impl / ops / CreateSubscription.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.netconf.notifications.impl.ops;
10
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.common.collect.Lists;
14 import java.util.List;
15 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
16 import org.opendaylight.controller.netconf.api.NetconfSession;
17 import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
18 import org.opendaylight.controller.netconf.mapping.api.SessionAwareNetconfOperation;
19 import org.opendaylight.controller.netconf.notifications.NetconfNotification;
20 import org.opendaylight.controller.netconf.notifications.NetconfNotificationListener;
21 import org.opendaylight.controller.netconf.notifications.NetconfNotificationRegistry;
22 import org.opendaylight.controller.netconf.notifications.NotificationListenerRegistration;
23 import org.opendaylight.controller.netconf.notifications.impl.NetconfNotificationManager;
24 import org.opendaylight.controller.netconf.util.mapping.AbstractLastNetconfOperation;
25 import org.opendaylight.controller.netconf.util.xml.XmlElement;
26 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
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;
33
34 /**
35  * Create subscription listens for create subscription requests and registers notification listeners into notification registry.
36  * Received notifications are sent to the client right away
37  */
38 public class CreateSubscription extends AbstractLastNetconfOperation implements SessionAwareNetconfOperation, AutoCloseable {
39
40     private static final Logger LOG = LoggerFactory.getLogger(CreateSubscription.class);
41
42     static final String CREATE_SUBSCRIPTION = "create-subscription";
43
44     private final NetconfNotificationRegistry notifications;
45     private final List<NotificationListenerRegistration> subscriptions = Lists.newArrayList();
46     private NetconfSession netconfSession;
47
48     public CreateSubscription(final String netconfSessionIdForReporting, final NetconfNotificationRegistry notifications) {
49         super(netconfSessionIdForReporting);
50         this.notifications = notifications;
51     }
52
53     @Override
54     protected Element handleWithNoSubsequentOperations(final Document document, final XmlElement operationElement) throws NetconfDocumentedException {
55         operationElement.checkName(CREATE_SUBSCRIPTION);
56         operationElement.checkNamespace(CreateSubscriptionInput.QNAME.getNamespace().toString());
57         // FIXME reimplement using CODEC_REGISTRY and parse everything into generated class instance
58         // Waiting ofr https://git.opendaylight.org/gerrit/#/c/13763/
59
60         // FIXME filter could be supported same way as netconf server filters get and get-config results
61         final Optional<XmlElement> filter = operationElement.getOnlyChildElementWithSameNamespaceOptionally("filter");
62         Preconditions.checkArgument(filter.isPresent() == false, "Filter element not yet supported");
63
64         // Replay not supported
65         final Optional<XmlElement> startTime = operationElement.getOnlyChildElementWithSameNamespaceOptionally("startTime");
66         Preconditions.checkArgument(startTime.isPresent() == false, "StartTime element not yet supported");
67
68         // Stop time not supported
69         final Optional<XmlElement> stopTime = operationElement.getOnlyChildElementWithSameNamespaceOptionally("stopTime");
70         Preconditions.checkArgument(stopTime.isPresent() == false, "StopTime element not yet supported");
71
72         final StreamNameType streamNameType = parseStreamIfPresent(operationElement);
73
74         Preconditions.checkNotNull(netconfSession);
75         // Premature streams are allowed (meaning listener can register even if no provider is available yet)
76         if(notifications.isStreamAvailable(streamNameType) == false) {
77             LOG.warn("Registering premature stream {}. No publisher available yet for session {}", streamNameType, getNetconfSessionIdForReporting());
78         }
79
80         final NotificationListenerRegistration notificationListenerRegistration =
81                 notifications.registerNotificationListener(streamNameType, new NotificationSubscription(netconfSession));
82         subscriptions.add(notificationListenerRegistration);
83
84         return XmlUtil.createElement(document, XmlNetconfConstants.OK, Optional.<String>absent());
85     }
86
87     private StreamNameType parseStreamIfPresent(final XmlElement operationElement) throws NetconfDocumentedException {
88         final Optional<XmlElement> stream = operationElement.getOnlyChildElementWithSameNamespaceOptionally("stream");
89         return stream.isPresent() ? new StreamNameType(stream.get().getTextContent()) : NetconfNotificationManager.BASE_STREAM_NAME;
90     }
91
92     @Override
93     protected String getOperationName() {
94         return CREATE_SUBSCRIPTION;
95     }
96
97     @Override
98     protected String getOperationNamespace() {
99         return CreateSubscriptionInput.QNAME.getNamespace().toString();
100     }
101
102     @Override
103     public void setSession(final NetconfSession session) {
104         this.netconfSession = session;
105     }
106
107     @Override
108     public void close() {
109         netconfSession = null;
110         // Unregister from notification streams
111         for (final NotificationListenerRegistration subscription : subscriptions) {
112             subscription.close();
113         }
114     }
115
116     private static class NotificationSubscription implements NetconfNotificationListener {
117         private final NetconfSession currentSession;
118
119         public NotificationSubscription(final NetconfSession currentSession) {
120             this.currentSession = currentSession;
121         }
122
123         @Override
124         public void onNotification(final StreamNameType stream, final NetconfNotification notification) {
125             currentSession.sendMessage(notification);
126         }
127     }
128 }