Convert users from submit() to commit()
[netconf.git] / netconf / netconf-topology-singleton / src / main / java / org / opendaylight / netconf / topology / singleton / impl / actors / WriteAdapter.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 package org.opendaylight.netconf.topology.singleton.impl.actors;
9
10 import akka.actor.ActorContext;
11 import akka.actor.ActorRef;
12 import akka.actor.Status.Failure;
13 import akka.actor.Status.Success;
14 import com.google.common.util.concurrent.FluentFuture;
15 import com.google.common.util.concurrent.FutureCallback;
16 import com.google.common.util.concurrent.MoreExecutors;
17 import javax.annotation.Nonnull;
18 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
19 import org.opendaylight.mdsal.common.api.CommitInfo;
20 import org.opendaylight.netconf.topology.singleton.messages.NormalizedNodeMessage;
21 import org.opendaylight.netconf.topology.singleton.messages.transactions.CancelRequest;
22 import org.opendaylight.netconf.topology.singleton.messages.transactions.DeleteRequest;
23 import org.opendaylight.netconf.topology.singleton.messages.transactions.MergeRequest;
24 import org.opendaylight.netconf.topology.singleton.messages.transactions.PutRequest;
25 import org.opendaylight.netconf.topology.singleton.messages.transactions.SubmitRequest;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 class WriteAdapter {
30
31     private static final Logger LOG = LoggerFactory.getLogger(WriteAdapter.class);
32
33     private final DOMDataWriteTransaction tx;
34
35     WriteAdapter(final DOMDataWriteTransaction tx) {
36         this.tx = tx;
37     }
38
39     private void cancel(final ActorContext context, final ActorRef sender, final ActorRef self) {
40         final boolean cancelled = tx.cancel();
41         sender.tell(cancelled, self);
42         context.stop(self);
43     }
44
45     private void submit(final ActorRef requester, final ActorRef self, final ActorContext context) {
46         final FluentFuture<? extends CommitInfo> submitFuture = tx.commit();
47         context.stop(self);
48         submitFuture.addCallback(new FutureCallback<CommitInfo>() {
49             @Override
50             public void onSuccess(final CommitInfo result) {
51                 requester.tell(new Success(null), self);
52             }
53
54             @Override
55             public void onFailure(@Nonnull final Throwable throwable) {
56                 requester.tell(new Failure(throwable), self);
57             }
58         }, MoreExecutors.directExecutor());
59     }
60
61     @SuppressWarnings("checkstyle:IllegalCatch")
62     public void handle(final Object message, final ActorRef sender, final ActorContext context, final ActorRef self) {
63         // we need to catch everything, since an unchecked exception can be thrown from the underlying parse.
64         // TODO Maybe we should store it and fail the submit immediately?.
65         try {
66             if (message instanceof MergeRequest) {
67                 final MergeRequest mergeRequest = (MergeRequest) message;
68                 final NormalizedNodeMessage data = mergeRequest.getNormalizedNodeMessage();
69                 tx.merge(mergeRequest.getStore(), data.getIdentifier(), data.getNode());
70             } else if (message instanceof PutRequest) {
71                 final PutRequest putRequest = (PutRequest) message;
72                 final NormalizedNodeMessage data = putRequest.getNormalizedNodeMessage();
73                 tx.put(putRequest.getStore(), data.getIdentifier(), data.getNode());
74             } else if (message instanceof DeleteRequest) {
75                 final DeleteRequest deleteRequest = (DeleteRequest) message;
76                 tx.delete(deleteRequest.getStore(), deleteRequest.getPath());
77             } else if (message instanceof CancelRequest) {
78                 cancel(context, sender, self);
79             } else if (message instanceof SubmitRequest) {
80                 submit(sender, self, context);
81             }
82
83         } catch (final RuntimeException exception) {
84             LOG.error("Write command has failed.", exception);
85         }
86     }
87 }