Migrate netconf users of submit() to commit()
[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.FutureCallback;
12 import com.google.common.util.concurrent.MoreExecutors;
13 import java.util.concurrent.ExecutionException;
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.mdsal.common.api.CommitInfo;
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;
28
29 /**
30  * Listens on changes in netconf notification stream availability and writes
31  * changes to the data store.
32  */
33 public final class NotificationToMdsalWriter implements AutoCloseable, NetconfNotificationCollector
34         .NetconfNotificationStreamListener {
35
36     private static final Logger LOG = LoggerFactory.getLogger(NotificationToMdsalWriter.class);
37     private static final InstanceIdentifier<Streams> STREAMS = InstanceIdentifier.builder(Netconf.class)
38             .child(Streams.class).build();
39
40     private final NetconfNotificationCollector netconfNotificationCollector;
41     private final DataBroker dataBroker;
42     private NotificationRegistration notificationRegistration;
43
44     public NotificationToMdsalWriter(final NetconfNotificationCollector netconfNotificationCollector,
45                                      final DataBroker dataBroker) {
46         this.netconfNotificationCollector = netconfNotificationCollector;
47         this.dataBroker = dataBroker;
48     }
49
50     @Override
51     public void close() {
52         final WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
53         tx.delete(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.create(Netconf.class));
54
55         tx.commit().addCallback(new FutureCallback<CommitInfo>() {
56             @Override
57             public void onSuccess(final CommitInfo info) {
58                 LOG.debug("Streams cleared successfully");
59             }
60
61             @Override
62             public void onFailure(final 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(final Stream stream) {
79         final WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
80
81         final InstanceIdentifier<Stream> streamIdentifier = STREAMS.child(Stream.class, stream.key());
82         tx.merge(LogicalDatastoreType.OPERATIONAL, streamIdentifier, stream, true);
83
84         try {
85             tx.commit().get();
86             LOG.debug("Stream %s registered successfully.", stream.getName());
87         } catch (InterruptedException | ExecutionException e) {
88             LOG.warn("Unable to register stream.", e);
89         }
90     }
91
92     @Override
93     public void onStreamUnregistered(final StreamNameType stream) {
94         final WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
95
96         final InstanceIdentifier<Stream> streamIdentifier = STREAMS.child(Stream.class, new StreamKey(stream));
97
98         tx.delete(LogicalDatastoreType.OPERATIONAL, streamIdentifier);
99
100         try {
101             tx.commit().get();
102             LOG.debug("Stream %s unregistered successfully.", stream);
103         } catch (InterruptedException | ExecutionException e) {
104             LOG.warn("Unable to unregister stream", e);
105         }
106     }
107 }