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.controller.config.yang.netconf.mdsal.notification;
10 import com.google.common.util.concurrent.FutureCallback;
11 import com.google.common.util.concurrent.MoreExecutors;
12 import java.util.concurrent.ExecutionException;
13 import org.opendaylight.mdsal.binding.api.DataBroker;
14 import org.opendaylight.mdsal.binding.api.WriteTransaction;
15 import org.opendaylight.mdsal.common.api.CommitInfo;
16 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
17 import org.opendaylight.netconf.notifications.NetconfNotificationCollector;
18 import org.opendaylight.netconf.notifications.NotificationRegistration;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.StreamNameType;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.Netconf;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.Streams;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.streams.Stream;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.streams.StreamKey;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
29 * Listens on changes in netconf notification stream availability and writes
30 * changes to the data store.
32 public final class NotificationToMdsalWriter implements AutoCloseable, NetconfNotificationCollector
33 .NetconfNotificationStreamListener {
35 private static final Logger LOG = LoggerFactory.getLogger(NotificationToMdsalWriter.class);
36 private static final InstanceIdentifier<Streams> STREAMS = InstanceIdentifier.builder(Netconf.class)
37 .child(Streams.class).build();
39 private final NetconfNotificationCollector netconfNotificationCollector;
40 private final DataBroker dataBroker;
41 private NotificationRegistration notificationRegistration;
43 public NotificationToMdsalWriter(final NetconfNotificationCollector netconfNotificationCollector,
44 final DataBroker dataBroker) {
45 this.netconfNotificationCollector = netconfNotificationCollector;
46 this.dataBroker = dataBroker;
51 final WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
52 tx.delete(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.create(Netconf.class));
54 tx.commit().addCallback(new FutureCallback<CommitInfo>() {
56 public void onSuccess(final CommitInfo info) {
57 LOG.debug("Streams cleared successfully");
61 public void onFailure(final Throwable throwable) {
62 LOG.warn("Unable to clear streams", throwable);
64 }, MoreExecutors.directExecutor());
66 notificationRegistration.close();
70 * Invoked by blueprint.
73 notificationRegistration = netconfNotificationCollector.registerStreamListener(this);
77 public void onStreamRegistered(final Stream stream) {
78 final WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
80 final InstanceIdentifier<Stream> streamIdentifier = STREAMS.child(Stream.class, stream.key());
81 tx.merge(LogicalDatastoreType.OPERATIONAL, streamIdentifier, stream, true);
85 LOG.debug("Stream {} registered successfully.", stream.getName());
86 } catch (InterruptedException | ExecutionException e) {
87 LOG.warn("Unable to register stream {}.", stream, e);
92 public void onStreamUnregistered(final StreamNameType stream) {
93 final WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
95 final InstanceIdentifier<Stream> streamIdentifier = STREAMS.child(Stream.class, new StreamKey(stream));
97 tx.delete(LogicalDatastoreType.OPERATIONAL, streamIdentifier);
101 LOG.debug("Stream {} unregistered successfully.", stream);
102 } catch (InterruptedException | ExecutionException e) {
103 LOG.warn("Unable to unregister stream {}", stream, e);