ddfb8b0a02bc2c6c85608fd21145fee0c2ea368a
[netconf.git] / netconf / mdsal-netconf-notification / src / main / java / org / opendaylight / netconf / mdsal / notification / impl / 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
9 package org.opendaylight.netconf.mdsal.notification.impl;
10
11 import java.util.Collection;
12 import org.eclipse.jdt.annotation.NonNull;
13 import org.opendaylight.mdsal.binding.api.DataBroker;
14 import org.opendaylight.mdsal.binding.api.DataObjectModification;
15 import org.opendaylight.mdsal.binding.api.DataTreeModification;
16 import org.opendaylight.netconf.notifications.NetconfNotificationCollector;
17 import org.opendaylight.netconf.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.YangLibraryUpdate;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.YangLibraryUpdateBuilder;
21 import org.opendaylight.yangtools.concepts.ListenerRegistration;
22 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
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 public final class YangLibraryNotificationProducerRFC8525 extends OperationalDatastoreListener<YangLibrary>
30         implements AutoCloseable {
31
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     public YangLibraryNotificationProducerRFC8525(final NetconfNotificationCollector netconfNotificationCollector,
39                                            final DataBroker dataBroker) {
40         super(YANG_LIBRARY_INSTANCE_IDENTIFIER);
41         this.yangLibraryPublisherRegistration = netconfNotificationCollector.registerYangLibraryPublisher();
42         this.yangLibraryChangeListenerRegistration = registerOnChanges(dataBroker);
43     }
44
45     @Override
46     public void onDataTreeChanged(@NonNull Collection<DataTreeModification<YangLibrary>> changes) {
47         for (DataTreeModification<YangLibrary> change : changes) {
48             final DataObjectModification<YangLibrary> rootNode = change.getRootNode();
49             final YangLibrary dataAfter = rootNode.getDataAfter();
50             if (dataAfter != null) {
51                 final YangLibraryUpdate yangLibraryUpdate = new YangLibraryUpdateBuilder()
52                         .setContentId(dataAfter.getContentId())
53                         .build();
54                 yangLibraryPublisherRegistration.onYangLibraryUpdate(yangLibraryUpdate);
55             }
56         }
57     }
58
59     /**
60      * Invoked by blueprint.
61      */
62     @Override
63     public void close() {
64         if (yangLibraryPublisherRegistration != null) {
65             yangLibraryPublisherRegistration.close();
66         }
67         if (yangLibraryChangeListenerRegistration != null) {
68             yangLibraryChangeListenerRegistration.close();
69         }
70     }
71 }