d5ba353fe64ab4ed812c330c859d0104ddfe826a
[netconf.git] / netconf / mdsal-netconf-notification / src / main / java / org / opendaylight / controller / config / yang / netconf / mdsal / notification / NotificationToMdsalWriter.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. 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.controller.config.yang.netconf.mdsal.notification;
10
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 com.google.common.util.concurrent.MoreExecutors;
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
17 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
19 import org.opendaylight.netconf.notifications.NetconfNotificationCollector;
20 import org.opendaylight.netconf.notifications.NotificationRegistration;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.StreamNameType;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.Netconf;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.Streams;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.streams.Stream;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.streams.StreamKey;
26 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * Listens on changes in netconf notification stream availability and writes
32  * changes to the data store.
33  */
34 public final class NotificationToMdsalWriter implements AutoCloseable, NetconfNotificationCollector
35         .NetconfNotificationStreamListener {
36
37     private static final Logger LOG = LoggerFactory.getLogger(NotificationToMdsalWriter.class);
38
39     private final NetconfNotificationCollector netconfNotificationCollector;
40     private final DataBroker dataBroker;
41     private NotificationRegistration notificationRegistration;
42
43     public NotificationToMdsalWriter(final NetconfNotificationCollector netconfNotificationCollector,
44                                      final DataBroker dataBroker) {
45         this.netconfNotificationCollector = netconfNotificationCollector;
46         this.dataBroker = dataBroker;
47     }
48
49     @Override
50     public void close() {
51         final WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
52         tx.delete(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.create(Netconf.class));
53         final CheckedFuture<Void, TransactionCommitFailedException> submit = tx.submit();
54
55         Futures.addCallback(submit, new FutureCallback<Void>() {
56             @Override
57             public void onSuccess(Void avoid) {
58                 LOG.debug("Streams cleared successfully");
59             }
60
61             @Override
62             public void onFailure(Throwable throwable) {
63                 LOG.warn("Unable to clear streams", throwable);
64             }
65         }, MoreExecutors.directExecutor());
66
67         notificationRegistration.close();
68     }
69
70     /**
71      * Invoked by blueprint.
72      */
73     public void start() {
74         notificationRegistration = netconfNotificationCollector.registerStreamListener(this);
75     }
76
77     @Override
78     public void onStreamRegistered(Stream stream) {
79         final WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
80
81         final InstanceIdentifier streamIdentifier = InstanceIdentifier.create(Netconf.class).child(Streams.class)
82                 .builder().child(Stream.class, stream.getKey()).build();
83         tx.merge(LogicalDatastoreType.OPERATIONAL, streamIdentifier, stream, true);
84
85         try {
86             tx.submit().checkedGet();
87             LOG.debug("Stream %s registered successfully.", stream.getName());
88         } catch (TransactionCommitFailedException e) {
89             LOG.warn("Unable to register stream.", e);
90         }
91     }
92
93     @Override
94     public void onStreamUnregistered(StreamNameType stream) {
95         final WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
96
97         final StreamKey streamKey = new StreamKey(stream);
98         final InstanceIdentifier streamIdentifier = InstanceIdentifier.create(Netconf.class).child(Streams.class)
99                 .builder().child(Stream.class, streamKey).build();
100
101         tx.delete(LogicalDatastoreType.OPERATIONAL, streamIdentifier);
102
103         try {
104             tx.submit().checkedGet();
105             LOG.debug("Stream %s unregistered successfully.", stream);
106         } catch (TransactionCommitFailedException e) {
107             LOG.warn("Unable to unregister stream", e);
108         }
109     }
110 }