Netconf Device Notification
[netconf.git] / restconf / restconf-nb / src / main / java / org / opendaylight / restconf / nb / rfc8040 / streams / listeners / NotificationListenerAdapter.java
1 /*
2  * Copyright (c) 2016 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 package org.opendaylight.restconf.nb.rfc8040.streams.listeners;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.base.MoreObjects.ToStringHelper;
13 import org.opendaylight.mdsal.dom.api.DOMNotificationService;
14 import org.opendaylight.yang.gen.v1.urn.sal.restconf.event.subscription.rev140708.NotificationOutputTypeGrouping.NotificationOutputType;
15 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
16 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
17
18 /**
19  * {@link NotificationListenerAdapter} is responsible to track events on notifications.
20  */
21 public final class NotificationListenerAdapter extends AbstractNotificationListenerAdaptor {
22     private final Absolute path;
23
24     /**
25      * Set path of listener and stream name.
26      *
27      * @param path       Schema path of YANG notification.
28      * @param streamName Name of the stream.
29      * @param outputType Type of output on notification (JSON or XML).
30      */
31     NotificationListenerAdapter(final Absolute path, final String streamName, final NotificationOutputType outputType) {
32         super(path.lastNodeIdentifier(), streamName, outputType);
33         this.path = requireNonNull(path);
34     }
35
36     @Override
37     EffectiveModelContext effectiveModel() {
38         return databindProvider.currentContext().modelContext();
39     }
40
41     /**
42      * Get schema path of notification.
43      *
44      * @return The configured schema path that points to observing YANG notification schema node.
45      */
46     public Absolute getSchemaPath() {
47         return path;
48     }
49
50     public synchronized void listen(final DOMNotificationService notificationService) {
51         if (!isListening()) {
52             setRegistration(notificationService.registerNotificationListener(this, getSchemaPath()));
53         }
54     }
55
56     @Override
57     ToStringHelper addToStringAttributes(final ToStringHelper helper) {
58         return super.addToStringAttributes(helper.add("path", path));
59     }
60 }