Expose streams with all supported encodings
[netconf.git] / restconf / restconf-nb / src / main / java / org / opendaylight / restconf / nb / rfc8040 / streams / NotificationSource.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;
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.DOMNotification;
15 import org.opendaylight.mdsal.dom.api.DOMNotificationService;
16 import org.opendaylight.restconf.nb.rfc8040.databind.DatabindProvider;
17 import org.opendaylight.restconf.nb.rfc8040.streams.RestconfStream.Sink;
18 import org.opendaylight.restconf.nb.rfc8040.streams.RestconfStream.Source;
19 import org.opendaylight.yangtools.concepts.Registration;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
22
23 /**
24  * A {@link Source} reporting YANG notifications.
25  */
26 public final class NotificationSource extends AbstractNotificationSource {
27     private final DatabindProvider databindProvider;
28     private final DOMNotificationService notificationService;
29     private final ImmutableSet<QName> qnames;
30
31     NotificationSource(final DatabindProvider databindProvider, final DOMNotificationService notificationService,
32             final ImmutableSet<QName> qnames) {
33         this.databindProvider = requireNonNull(databindProvider);
34         this.notificationService = requireNonNull(notificationService);
35         this.qnames = requireNonNull(qnames);
36     }
37
38     /**
39      * Return notification QNames.
40      *
41      * @return The YANG notification {@link QName}s this listener is bound to
42      */
43     public ImmutableSet<QName> qnames() {
44         return qnames;
45     }
46
47     @Override
48     protected Registration start(final Sink<DOMNotification> sink) {
49         return notificationService.registerNotificationListener(
50             new Listener(sink, () -> databindProvider.currentContext().modelContext()),
51             qnames.stream().map(Absolute::of).toList());
52     }
53
54     @Override
55     protected ToStringHelper addToStringAttributes(final ToStringHelper helper) {
56         return super.addToStringAttributes(helper.add("qnames", qnames));
57     }
58 }