bf5dce34220078a243c39fecd76dd253dd2e78ac
[netconf.git] / netconf / mdsal-netconf-notification / src / main / java / org / opendaylight / netconf / mdsal / notification / impl / YangLibraryNotificationProducer.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.opendaylight.mdsal.binding.api.DataBroker;
13 import org.opendaylight.mdsal.binding.api.DataObjectModification;
14 import org.opendaylight.mdsal.binding.api.DataTreeModification;
15 import org.opendaylight.netconf.notifications.NetconfNotificationCollector;
16 import org.opendaylight.netconf.notifications.YangLibraryPublisherRegistration;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.ModulesState;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.YangLibraryChange;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.YangLibraryChangeBuilder;
20 import org.opendaylight.yangtools.concepts.ListenerRegistration;
21 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
22
23 /**
24  * Listens on the set of modules and submodules changes in data store and publishes
25  * to base netconf notification stream listener a server-specific identifier representing
26  * the current set of modules and submodules.
27  *
28  * @deprecated ietf-yang-library:yang-library-change was deprecated in the new RFC8525.
29  *             Use {@link YangLibraryNotificationProducerRFC8525}.
30  */
31 @Deprecated(forRemoval = true)
32 public final class YangLibraryNotificationProducer extends OperationalDatastoreListener<ModulesState>
33     implements AutoCloseable {
34
35     private static final InstanceIdentifier<ModulesState> MODULES_STATE_INSTANCE_IDENTIFIER =
36             InstanceIdentifier.create(ModulesState.class);
37
38     private final ListenerRegistration<?> yangLibraryChangeListenerRegistration;
39     private final YangLibraryPublisherRegistration yangLibraryPublisherRegistration;
40
41     public YangLibraryNotificationProducer(final NetconfNotificationCollector netconfNotificationCollector,
42                                            final DataBroker dataBroker) {
43         super(MODULES_STATE_INSTANCE_IDENTIFIER);
44         this.yangLibraryPublisherRegistration = netconfNotificationCollector.registerYangLibraryPublisher();
45         this.yangLibraryChangeListenerRegistration = registerOnChanges(dataBroker);
46     }
47
48     @Override
49     public void onDataTreeChanged(final Collection<DataTreeModification<ModulesState>> changes) {
50         for (DataTreeModification<ModulesState> change : changes) {
51             final DataObjectModification<ModulesState> rootNode = change.getRootNode();
52             final ModulesState dataAfter = rootNode.getDataAfter();
53             if (dataAfter != null) {
54                 final YangLibraryChange yangLibraryChange = new YangLibraryChangeBuilder()
55                         .setModuleSetId(dataAfter.getModuleSetId())
56                         .build();
57                 yangLibraryPublisherRegistration.onYangLibraryChange(yangLibraryChange);
58             }
59         }
60     }
61
62     /**
63      * Invoked by blueprint.
64      */
65     @Override
66     public void close() {
67         if (yangLibraryPublisherRegistration != null) {
68             yangLibraryPublisherRegistration.close();
69         }
70         if (yangLibraryChangeListenerRegistration != null) {
71             yangLibraryChangeListenerRegistration.close();
72         }
73     }
74 }