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
9 package org.opendaylight.controller.config.yang.netconf.mdsal.notification;
11 import com.google.common.util.concurrent.CheckedFuture;
12 import com.google.common.util.concurrent.FutureCallback;
13 import com.google.common.util.concurrent.Futures;
14 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
15 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
18 import org.opendaylight.netconf.notifications.NetconfNotificationCollector;
19 import org.opendaylight.netconf.notifications.NotificationRegistration;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.StreamNameType;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.Netconf;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.Streams;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.streams.Stream;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.streams.StreamKey;
25 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
30 * Listens on changes in netconf notification stream availability and writes
31 * changes to the data store.
33 public final class NotificationToMdsalWriter implements AutoCloseable, NetconfNotificationCollector.NetconfNotificationStreamListener {
35 private static final Logger LOG = LoggerFactory.getLogger(NotificationToMdsalWriter.class);
37 private final NetconfNotificationCollector netconfNotificationCollector;
38 private final DataBroker dataBroker;
39 private NotificationRegistration notificationRegistration;
41 public NotificationToMdsalWriter(final NetconfNotificationCollector netconfNotificationCollector,
42 final DataBroker dataBroker) {
43 this.netconfNotificationCollector = netconfNotificationCollector;
44 this.dataBroker = dataBroker;
49 final WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
50 tx.delete(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.create(Netconf.class));
51 final CheckedFuture<Void, TransactionCommitFailedException> submit = tx.submit();
53 Futures.addCallback(submit, new FutureCallback<Void>() {
55 public void onSuccess(Void aVoid) {
56 LOG.debug("Streams cleared successfully");
60 public void onFailure(Throwable throwable) {
61 LOG.warn("Unable to clear streams", throwable);
65 notificationRegistration.close();
72 notificationRegistration = netconfNotificationCollector.registerStreamListener(this);
76 public void onStreamRegistered(Stream stream) {
77 final WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
79 final InstanceIdentifier streamIdentifier = InstanceIdentifier.create(Netconf.class).child(Streams.class).
80 builder().child(Stream.class, stream.getKey()).build();
81 tx.merge(LogicalDatastoreType.OPERATIONAL, streamIdentifier, stream, true);
84 tx.submit().checkedGet();
85 LOG.debug("Stream %s registered successfully.", stream.getName());
86 } catch (TransactionCommitFailedException e) {
87 LOG.warn("Unable to register stream.", e);
92 public void onStreamUnregistered(StreamNameType stream) {
93 final WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
95 final StreamKey streamKey = new StreamKey(stream);
96 final InstanceIdentifier streamIdentifier = InstanceIdentifier.create(Netconf.class).child(Streams.class).
97 builder().child(Stream.class, streamKey).build();
99 tx.delete(LogicalDatastoreType.OPERATIONAL, streamIdentifier);
102 tx.submit().checkedGet();
103 LOG.debug("Stream %s unregistered successfully.", stream);
104 } catch (TransactionCommitFailedException e) {
105 LOG.warn("Unable to unregister stream", e);