Integrate netconf-events-mdsal
[netconf.git] / plugins / netconf-server-mdsal / src / main / java / org / opendaylight / netconf / server / mdsal / notifications / YangLibraryNotificationProducerRFC8525.java
1 /*
2  * Copyright (c) 2020 Pantheon Technologies s.r.o. 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.netconf.server.mdsal.notifications;
9
10 import java.util.Collection;
11 import org.opendaylight.mdsal.binding.api.DataBroker;
12 import org.opendaylight.mdsal.binding.api.DataTreeChangeListener;
13 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
14 import org.opendaylight.mdsal.binding.api.DataTreeModification;
15 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
16 import org.opendaylight.netconf.server.api.notifications.NetconfNotificationCollector;
17 import org.opendaylight.netconf.server.api.notifications.YangLibraryPublisherRegistration;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.YangLibrary;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.YangLibraryUpdateBuilder;
20 import org.opendaylight.yangtools.concepts.ListenerRegistration;
21 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
22 import org.osgi.service.component.annotations.Activate;
23 import org.osgi.service.component.annotations.Component;
24 import org.osgi.service.component.annotations.Deactivate;
25 import org.osgi.service.component.annotations.Reference;
26
27 /**
28  * Listens on the modules, submodules, datastores, and datastore schemas changes in data store and publishes to base
29  * NETCONF notification stream listener a server-specific identifier representing the current set of modules,
30  * submodules, datastores, and datastore schemas.
31  */
32 @Component(service = { })
33 public final class YangLibraryNotificationProducerRFC8525
34         implements DataTreeChangeListener<YangLibrary>, AutoCloseable {
35     private final ListenerRegistration<?> yangLibraryChangeListenerRegistration;
36     private final YangLibraryPublisherRegistration yangLibraryPublisherRegistration;
37
38     @Activate
39     public YangLibraryNotificationProducerRFC8525(
40             @Reference(target = "(type=netconf-notification-manager)") final NetconfNotificationCollector notifManager,
41             @Reference final DataBroker dataBroker) {
42         yangLibraryPublisherRegistration = notifManager.registerYangLibraryPublisher();
43         yangLibraryChangeListenerRegistration = dataBroker.registerDataTreeChangeListener(
44             DataTreeIdentifier.create(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.create(YangLibrary.class)),
45             this);
46     }
47
48     @Deactivate
49     @Override
50     public void close() {
51         if (yangLibraryPublisherRegistration != null) {
52             yangLibraryPublisherRegistration.close();
53         }
54         if (yangLibraryChangeListenerRegistration != null) {
55             yangLibraryChangeListenerRegistration.close();
56         }
57     }
58
59     @Override
60     public void onDataTreeChanged(final Collection<DataTreeModification<YangLibrary>> changes) {
61         for (DataTreeModification<YangLibrary> change : changes) {
62             final YangLibrary dataAfter = change.getRootNode().getDataAfter();
63             if (dataAfter != null) {
64                 yangLibraryPublisherRegistration.onYangLibraryUpdate(new YangLibraryUpdateBuilder()
65                     .setContentId(dataAfter.getContentId())
66                     .build());
67             }
68         }
69     }
70 }