Implement create-notification-stream
[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 com.google.common.collect.ImmutableSet;
14 import org.opendaylight.mdsal.dom.api.DOMNotificationService;
15 import org.opendaylight.yang.gen.v1.urn.sal.restconf.event.subscription.rev140708.NotificationOutputTypeGrouping.NotificationOutputType;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
18 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
19
20 /**
21  * {@link NotificationListenerAdapter} is responsible to track events on notifications.
22  */
23 public final class NotificationListenerAdapter extends AbstractNotificationListenerAdaptor {
24     private final ImmutableSet<QName> paths;
25
26     /**
27      * Set path of listener and stream name.
28      *
29      * @param paths      Top-level  Schema path of YANG notification.
30      * @param streamName Name of the stream.
31      * @param outputType Type of output on notification (JSON or XML).
32      * @param listenersBroker Associated {@link ListenersBroker}
33      */
34     NotificationListenerAdapter(final ImmutableSet<QName> paths, final String streamName,
35             final NotificationOutputType outputType, final ListenersBroker listenersBroker) {
36         super(streamName, outputType, listenersBroker);
37         this.paths = requireNonNull(paths);
38     }
39
40     @Override
41     EffectiveModelContext effectiveModel() {
42         return databindProvider.currentContext().modelContext();
43     }
44
45     /**
46      * Return notification QNames.
47      *
48      * @return The YANG notification {@link QName}s this listener is bound to
49      */
50     public ImmutableSet<QName> qnames() {
51         return paths;
52     }
53
54     public synchronized void listen(final DOMNotificationService notificationService) {
55         if (!isListening()) {
56             setRegistration(notificationService.registerNotificationListener(this,
57                 paths.stream().map(Absolute::of).toList()));
58         }
59     }
60
61     @Override
62     ToStringHelper addToStringAttributes(final ToStringHelper helper) {
63         return super.addToStringAttributes(helper.add("paths", paths));
64     }
65 }