2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.netconf.server.mdsal.notifications;
10 import com.google.common.collect.ImmutableSet;
11 import com.google.common.collect.Sets;
12 import java.util.List;
14 import org.opendaylight.mdsal.binding.api.DataBroker;
15 import org.opendaylight.mdsal.binding.api.DataTreeChangeListener;
16 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
17 import org.opendaylight.mdsal.binding.api.DataTreeModification;
18 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
19 import org.opendaylight.netconf.server.api.notifications.BaseNotificationPublisherRegistration;
20 import org.opendaylight.netconf.server.api.notifications.NetconfNotificationCollector;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.NetconfState;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.Capabilities;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.notifications.rev120206.NetconfCapabilityChangeBuilder;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.notifications.rev120206.changed.by.parms.ChangedByBuilder;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.notifications.rev120206.changed.by.parms.changed.by.server.or.user.ServerBuilder;
27 import org.opendaylight.yangtools.concepts.Registration;
28 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
29 import org.opendaylight.yangtools.yang.common.Empty;
30 import org.osgi.service.component.annotations.Activate;
31 import org.osgi.service.component.annotations.Component;
32 import org.osgi.service.component.annotations.Deactivate;
33 import org.osgi.service.component.annotations.Reference;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 * Listens on capabilities changes in data store and publishes them to base NETCONF notification stream listener.
40 @Component(service = { })
41 public final class CapabilityChangeNotificationProducer implements DataTreeChangeListener<Capabilities>, AutoCloseable {
42 private static final Logger LOG = LoggerFactory.getLogger(CapabilityChangeNotificationProducer.class);
44 private final BaseNotificationPublisherRegistration baseNotificationPublisherRegistration;
45 private final Registration capabilityChangeListenerRegistration;
48 public CapabilityChangeNotificationProducer(
49 @Reference(target = "(type=netconf-notification-manager)") final NetconfNotificationCollector notifManager,
50 @Reference final DataBroker dataBroker) {
51 baseNotificationPublisherRegistration = notifManager.registerBaseNotificationPublisher();
52 capabilityChangeListenerRegistration = dataBroker.registerTreeChangeListener(
53 DataTreeIdentifier.of(LogicalDatastoreType.OPERATIONAL,
54 InstanceIdentifier.builder(NetconfState.class).child(Capabilities.class).build()), this);
60 if (baseNotificationPublisherRegistration != null) {
61 baseNotificationPublisherRegistration.close();
63 if (capabilityChangeListenerRegistration != null) {
64 capabilityChangeListenerRegistration.close();
69 public void onDataTreeChanged(final List<DataTreeModification<Capabilities>> changes) {
70 for (var change : changes) {
71 final var rootNode = change.getRootNode();
72 final var modificationType = rootNode.modificationType();
73 switch (modificationType) {
75 final Capabilities dataAfter = rootNode.dataAfter();
76 final Capabilities dataBefore = rootNode.dataBefore();
77 final Set<Uri> before = dataBefore != null ? ImmutableSet.copyOf(dataBefore.getCapability())
79 final Set<Uri> after = dataAfter != null ? ImmutableSet.copyOf(dataAfter.getCapability())
81 final Set<Uri> added = Sets.difference(after, before);
82 final Set<Uri> removed = Sets.difference(before, after);
83 publishNotification(added, removed);
87 final Capabilities dataBeforeDelete = rootNode.dataBefore();
88 if (dataBeforeDelete != null) {
89 final Set<Uri> removed = ImmutableSet.copyOf(dataBeforeDelete.getCapability());
90 publishNotification(Set.of(), removed);
95 LOG.debug("Received intentionally unhandled type: {}.", modificationType);
100 private void publishNotification(final Set<Uri> added, final Set<Uri> removed) {
101 baseNotificationPublisherRegistration.onCapabilityChanged(new NetconfCapabilityChangeBuilder()
102 .setChangedBy(new ChangedByBuilder()
103 .setServerOrUser(new ServerBuilder().setServer(Empty.value()).build())
105 .setAddedCapability(Set.copyOf(added))
106 .setDeletedCapability(Set.copyOf(removed))
107 // TODO modified should be computed ... but why ?
108 .setModifiedCapability(Set.of())