d19fa0b77746980800f62d1c829cc10ed33e352e
[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 package org.opendaylight.controller.config.yang.netconf.mdsal.notification;
9
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;
27
28 /**
29  * Listens on changes in netconf notification stream availability and writes
30  * changes to the data store.
31  */
32 public final class NotificationToMdsalWriter implements AutoCloseable, NetconfNotificationCollector
33         .NetconfNotificationStreamListener {
34
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();
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
54         tx.commit().addCallback(new FutureCallback<CommitInfo>() {
55             @Override
56             public void onSuccess(final CommitInfo info) {
57                 LOG.debug("Streams cleared successfully");
58             }
59
60             @Override
61             public void onFailure(final Throwable throwable) {
62                 LOG.warn("Unable to clear streams", throwable);
63             }
64         }, MoreExecutors.directExecutor());
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(final Stream stream) {
78         final WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
79
80         final InstanceIdentifier<Stream> streamIdentifier = STREAMS.child(Stream.class, stream.key());
81         tx.merge(LogicalDatastoreType.OPERATIONAL, streamIdentifier, stream, true);
82
83         try {
84             tx.commit().get();
85             LOG.debug("Stream {} registered successfully.", stream.getName());
86         } catch (InterruptedException | ExecutionException e) {
87             LOG.warn("Unable to register stream {}.", stream, e);
88         }
89     }
90
91     @Override
92     public void onStreamUnregistered(final StreamNameType stream) {
93         final WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
94
95         final InstanceIdentifier<Stream> streamIdentifier = STREAMS.child(Stream.class, new StreamKey(stream));
96
97         tx.delete(LogicalDatastoreType.OPERATIONAL, streamIdentifier);
98
99         try {
100             tx.commit().get();
101             LOG.debug("Stream {} unregistered successfully.", stream);
102         } catch (InterruptedException | ExecutionException e) {
103             LOG.warn("Unable to unregister stream {}", stream, e);
104         }
105     }
106 }