Convert mdsal-netconf-notification to OSGi DS
[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 package org.opendaylight.netconf.mdsal.notification.impl;
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.ModulesState;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.YangLibraryChangeBuilder;
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 set of modules and submodules changes in data store and publishes
26  * to base netconf notification stream listener a server-specific identifier representing
27  * the current set of modules and submodules.
28  *
29  * @deprecated ietf-yang-library:yang-library-change was deprecated in the new RFC8525.
30  *             Use {@link YangLibraryNotificationProducerRFC8525}.
31  */
32 @Component(service = { })
33 @Deprecated(forRemoval = true)
34 public final class YangLibraryNotificationProducer extends OperationalDatastoreListener<ModulesState>
35         implements AutoCloseable {
36     private static final InstanceIdentifier<ModulesState> MODULES_STATE_INSTANCE_IDENTIFIER =
37             InstanceIdentifier.create(ModulesState.class);
38
39     private final ListenerRegistration<?> yangLibraryChangeListenerRegistration;
40     private final YangLibraryPublisherRegistration yangLibraryPublisherRegistration;
41
42     @Activate
43     public YangLibraryNotificationProducer(
44             @Reference(target = "(type=netconf-notification-manager)") final NetconfNotificationCollector notifManager,
45             @Reference final DataBroker dataBroker) {
46         super(MODULES_STATE_INSTANCE_IDENTIFIER);
47         yangLibraryPublisherRegistration = notifManager.registerYangLibraryPublisher();
48         yangLibraryChangeListenerRegistration = registerOnChanges(dataBroker);
49     }
50
51     @Deactivate
52     @Override
53     public void close() {
54         if (yangLibraryPublisherRegistration != null) {
55             yangLibraryPublisherRegistration.close();
56         }
57         if (yangLibraryChangeListenerRegistration != null) {
58             yangLibraryChangeListenerRegistration.close();
59         }
60     }
61
62     @Override
63     public void onDataTreeChanged(final Collection<DataTreeModification<ModulesState>> changes) {
64         for (DataTreeModification<ModulesState> change : changes) {
65             final ModulesState dataAfter = change.getRootNode().getDataAfter();
66             if (dataAfter != null) {
67                 yangLibraryPublisherRegistration.onYangLibraryChange(new YangLibraryChangeBuilder()
68                     .setModuleSetId(dataAfter.getModuleSetId())
69                     .build());
70             }
71         }
72     }
73 }