Bug 8153: Enforce check-style rules for netconf - mdsal-netconf-notification
[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 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;
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
38     private final NetconfNotificationCollector netconfNotificationCollector;
39     private final DataBroker dataBroker;
40     private NotificationRegistration notificationRegistration;
41
42     public NotificationToMdsalWriter(final NetconfNotificationCollector netconfNotificationCollector,
43                                      final DataBroker dataBroker) {
44         this.netconfNotificationCollector = netconfNotificationCollector;
45         this.dataBroker = dataBroker;
46     }
47
48     @Override
49     public void close() {
50         final WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
51         tx.delete(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.create(Netconf.class));
52         final CheckedFuture<Void, TransactionCommitFailedException> submit = tx.submit();
53
54         Futures.addCallback(submit, new FutureCallback<Void>() {
55             @Override
56             public void onSuccess(Void avoid) {
57                 LOG.debug("Streams cleared successfully");
58             }
59
60             @Override
61             public void onFailure(Throwable throwable) {
62                 LOG.warn("Unable to clear streams", throwable);
63             }
64         });
65
66         notificationRegistration.close();
67     }
68
69     /**
70      * Invoked by blueprint.
71      */
72     public void start() {
73         notificationRegistration = netconfNotificationCollector.registerStreamListener(this);
74     }
75
76     @Override
77     public void onStreamRegistered(Stream stream) {
78         final WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
79
80         final InstanceIdentifier streamIdentifier = InstanceIdentifier.create(Netconf.class).child(Streams.class)
81                 .builder().child(Stream.class, stream.getKey()).build();
82         tx.merge(LogicalDatastoreType.OPERATIONAL, streamIdentifier, stream, true);
83
84         try {
85             tx.submit().checkedGet();
86             LOG.debug("Stream %s registered successfully.", stream.getName());
87         } catch (TransactionCommitFailedException e) {
88             LOG.warn("Unable to register stream.", e);
89         }
90     }
91
92     @Override
93     public void onStreamUnregistered(StreamNameType stream) {
94         final WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
95
96         final StreamKey streamKey = new StreamKey(stream);
97         final InstanceIdentifier streamIdentifier = InstanceIdentifier.create(Netconf.class).child(Streams.class)
98                 .builder().child(Stream.class, streamKey).build();
99
100         tx.delete(LogicalDatastoreType.OPERATIONAL, streamIdentifier);
101
102         try {
103             tx.submit().checkedGet();
104             LOG.debug("Stream %s unregistered successfully.", stream);
105         } catch (TransactionCommitFailedException e) {
106             LOG.warn("Unable to unregister stream", e);
107         }
108     }
109 }