da95e9f02afada156b16b34754d39757d31fe500
[netconf.git] / netconf / netconf-topology-singleton / src / main / java / org / opendaylight / netconf / topology / singleton / impl / RemoteOperationTxProcessorImpl.java
1 /*
2  * Copyright (c) 2016 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.netconf.topology.singleton.impl;
10
11 import akka.actor.ActorRef;
12 import com.google.common.base.Optional;
13 import com.google.common.util.concurrent.CheckedFuture;
14 import com.google.common.util.concurrent.FutureCallback;
15 import com.google.common.util.concurrent.Futures;
16 import javax.annotation.Nonnull;
17 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
19 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
20 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
21 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadOnlyTransaction;
22 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
23 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
24 import org.opendaylight.netconf.topology.singleton.api.RemoteOperationTxProcessor;
25 import org.opendaylight.netconf.topology.singleton.messages.NormalizedNodeMessage;
26 import org.opendaylight.netconf.topology.singleton.messages.SubmitFailedReply;
27 import org.opendaylight.netconf.topology.singleton.messages.transactions.EmptyReadResponse;
28 import org.opendaylight.netconf.topology.singleton.messages.transactions.SubmitReply;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
30 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 public class RemoteOperationTxProcessorImpl implements RemoteOperationTxProcessor, AutoCloseable {
35
36     private static final Logger LOG = LoggerFactory.getLogger(RemoteOperationTxProcessorImpl.class);
37
38     private final DOMDataBroker dataBroker;
39     private final RemoteDeviceId id;
40     private DOMDataWriteTransaction writeTx;
41     private DOMDataReadOnlyTransaction readTx;
42
43     public RemoteOperationTxProcessorImpl(final DOMDataBroker dataBroker, final RemoteDeviceId id) {
44         this.dataBroker = dataBroker;
45         this.id = id;
46         this.readTx = dataBroker.newReadOnlyTransaction();
47     }
48
49     @Override
50     public void doDelete(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
51         if (writeTx == null) {
52             writeTx = dataBroker.newWriteOnlyTransaction();
53         }
54         writeTx.delete(store, path);
55     }
56
57     @Override
58     public void doSubmit(final ActorRef recipient, final ActorRef sender) {
59         if (writeTx != null) {
60             CheckedFuture<Void, TransactionCommitFailedException> submitFuture = writeTx.submit();
61             Futures.addCallback(submitFuture, new FutureCallback<Void>() {
62                 @Override
63                 public void onSuccess(Void result) {
64                     recipient.tell(new SubmitReply(), sender);
65                 }
66
67                 @Override
68                 public void onFailure(@Nonnull Throwable throwable) {
69                     recipient.tell(throwable, sender);
70                 }
71             });
72         } else {
73             recipient.tell(new SubmitFailedReply(), sender);
74             LOG.warn("{}: Couldn't submit transaction because it was already closed.", id);
75         }
76     }
77
78     @Override
79     public void doCancel(final ActorRef recipient, final ActorRef sender) {
80         boolean cancel = false;
81         if (writeTx != null) {
82             cancel = writeTx.cancel();
83         }
84         recipient.tell(cancel, sender);
85     }
86
87     @Override
88     public void doPut(final LogicalDatastoreType store, final NormalizedNodeMessage data) {
89         if (writeTx == null) {
90             writeTx = dataBroker.newWriteOnlyTransaction();
91         }
92         writeTx.put(store, data.getIdentifier(), data.getNode());
93     }
94
95     @Override
96     public void doMerge(final LogicalDatastoreType store, final NormalizedNodeMessage data) {
97         if (writeTx == null) {
98             writeTx = dataBroker.newWriteOnlyTransaction();
99         }
100         writeTx.merge(store, data.getIdentifier(), data.getNode());
101     }
102
103     @Override
104     public void doRead(final LogicalDatastoreType store, final YangInstanceIdentifier path, final ActorRef recipient,
105                        final ActorRef sender) {
106         final CheckedFuture<Optional<NormalizedNode<?,?>>, ReadFailedException> readFuture =
107                 readTx.read(store, path);
108
109         Futures.addCallback(readFuture, new FutureCallback<Optional<NormalizedNode<?, ?>>>() {
110
111             @Override
112             public void onSuccess(final Optional<NormalizedNode<?, ?>> result) {
113                 if (!result.isPresent()) {
114                     recipient.tell(new EmptyReadResponse(), sender);
115                     return;
116                 }
117                 recipient.tell(new NormalizedNodeMessage(path, result.get()), sender);
118             }
119
120             @Override
121             public void onFailure(@Nonnull final Throwable throwable) {
122                 recipient.tell(throwable, sender);
123             }
124         });
125     }
126
127     @Override
128     public void doExists(final LogicalDatastoreType store, final YangInstanceIdentifier path, final ActorRef recipient,
129                          final ActorRef sender) {
130         final CheckedFuture<Boolean, ReadFailedException> readFuture =
131                 readTx.exists(store, path);
132         Futures.addCallback(readFuture, new FutureCallback<Boolean>() {
133             @Override
134             public void onSuccess(final Boolean result) {
135                 if (result == null) {
136                     recipient.tell(false, sender);
137                 } else {
138                     recipient.tell(result, sender);
139                 }
140             }
141
142             @Override
143             public void onFailure(@Nonnull final Throwable throwable) {
144                 recipient.tell(throwable, sender);
145             }
146         });
147     }
148
149     @Override
150     public void close() throws Exception {
151         if (readTx != null) {
152             readTx.close();
153         }
154     }
155 }