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