Introduce restconf.server.{api,spi,mdsal}
[netconf.git] / restconf / restconf-nb / src / main / java / org / opendaylight / restconf / server / mdsal / streams / notif / 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.server.mdsal.streams.notif;
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.server.spi.RestconfStream.Sink;
18 import org.opendaylight.restconf.server.spi.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 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     @Override
39     protected Registration start(final Sink<DOMNotification> sink) {
40         return notificationService.registerNotificationListener(
41             new Listener(sink, () -> databindProvider.currentContext().modelContext()),
42             qnames.stream().map(Absolute::of).toList());
43     }
44
45     @Override
46     protected ToStringHelper addToStringAttributes(final ToStringHelper helper) {
47         return super.addToStringAttributes(helper.add("qnames", qnames));
48     }
49 }