5350b6478ed317e6be5e18e6b6f58a7a76435912
[netconf.git] / netconf / netconf-topology-singleton / src / main / java / org / opendaylight / netconf / topology / singleton / impl / actors / WriteTransactionActor.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. 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.actors;
10
11 import akka.actor.ActorRef;
12 import akka.actor.Props;
13 import akka.actor.UntypedActor;
14 import com.google.common.util.concurrent.CheckedFuture;
15 import com.google.common.util.concurrent.FutureCallback;
16 import com.google.common.util.concurrent.Futures;
17 import javax.annotation.Nonnull;
18 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
19 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadOnlyTransaction;
20 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
21 import org.opendaylight.netconf.topology.singleton.messages.NormalizedNodeMessage;
22 import org.opendaylight.netconf.topology.singleton.messages.transactions.CancelRequest;
23 import org.opendaylight.netconf.topology.singleton.messages.transactions.DeleteRequest;
24 import org.opendaylight.netconf.topology.singleton.messages.transactions.MergeRequest;
25 import org.opendaylight.netconf.topology.singleton.messages.transactions.PutRequest;
26 import org.opendaylight.netconf.topology.singleton.messages.transactions.SubmitReply;
27 import org.opendaylight.netconf.topology.singleton.messages.transactions.SubmitRequest;
28
29 /**
30  * WriteTransactionActor is an interface to device's {@link DOMDataReadOnlyTransaction} for cluster nodes.
31  */
32 public class WriteTransactionActor extends UntypedActor {
33
34     private final DOMDataWriteTransaction tx;
35
36     /**
37      * Creates new actor Props.
38      *
39      * @param tx delegate device write transaction
40      * @return props
41      */
42     static Props props(final DOMDataWriteTransaction tx) {
43         return Props.create(WriteTransactionActor.class, () -> new WriteTransactionActor(tx));
44     }
45
46     private WriteTransactionActor(final DOMDataWriteTransaction tx) {
47         this.tx = tx;
48     }
49
50     @Override
51     public void onReceive(final Object message) throws Throwable {
52         if (message instanceof MergeRequest) {
53             final MergeRequest mergeRequest = (MergeRequest) message;
54             final NormalizedNodeMessage data = mergeRequest.getNormalizedNodeMessage();
55             tx.merge(mergeRequest.getStore(), data.getIdentifier(), data.getNode());
56         } else if (message instanceof PutRequest) {
57             final PutRequest putRequest = (PutRequest) message;
58             final NormalizedNodeMessage data = putRequest.getNormalizedNodeMessage();
59             tx.put(putRequest.getStore(), data.getIdentifier(), data.getNode());
60         } else if (message instanceof DeleteRequest) {
61             final DeleteRequest deleteRequest = (DeleteRequest) message;
62             tx.delete(deleteRequest.getStore(), deleteRequest.getPath());
63         } else if (message instanceof CancelRequest) {
64             cancel();
65         } else if (message instanceof SubmitRequest) {
66             submit(sender(), self());
67         } else {
68             unhandled(message);
69         }
70     }
71
72     private void cancel() {
73         final boolean cancelled = tx.cancel();
74         sender().tell(cancelled, self());
75         context().stop(self());
76     }
77
78     private void submit(final ActorRef requester, final ActorRef self) {
79         final CheckedFuture<Void, TransactionCommitFailedException> submitFuture = tx.submit();
80         context().stop(self);
81         Futures.addCallback(submitFuture, new FutureCallback<Void>() {
82             @Override
83             public void onSuccess(final Void result) {
84                 requester.tell(new SubmitReply(), self);
85             }
86
87             @Override
88             public void onFailure(@Nonnull final Throwable throwable) {
89                 requester.tell(throwable, self);
90             }
91         });
92     }
93 }