Implement AsyncWriteTransaction.commit()
[netconf.git] / netconf / netconf-topology-singleton / src / main / java / org / opendaylight / netconf / topology / singleton / impl / tx / ProxyReadWriteTransaction.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.tx;
10
11 import akka.actor.ActorRef;
12 import akka.actor.ActorSystem;
13 import akka.util.Timeout;
14 import com.google.common.base.Optional;
15 import com.google.common.util.concurrent.CheckedFuture;
16 import com.google.common.util.concurrent.FluentFuture;
17 import com.google.common.util.concurrent.MoreExecutors;
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
20 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
21 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
22 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
23 import org.opendaylight.mdsal.common.api.CommitInfo;
24 import org.opendaylight.mdsal.common.api.MappingCheckedFuture;
25 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
26 import org.opendaylight.yangtools.util.concurrent.ExceptionMapper;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
28 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
29
30 /**
31  * ProxyReadWriteTransaction uses provided {@link ActorRef} to delegate method calls to master
32  * {@link org.opendaylight.netconf.topology.singleton.impl.actors.ReadWriteTransactionActor}.
33  */
34 public class ProxyReadWriteTransaction implements DOMDataReadWriteTransaction {
35
36     private final ProxyReadAdapter delegateRead;
37     private final ProxyWriteAdapter delegateWrite;
38
39     /**
40      * Constructor for {@code ProxyReadWriteTransaction}.
41      *
42      * @param masterTxActor
43      * {@link org.opendaylight.netconf.topology.singleton.impl.actors.ReadWriteTransactionActor} ref
44      * @param id            device id
45      * @param actorSystem   system
46      * @param askTimeout    timeout
47      */
48     public ProxyReadWriteTransaction(final ActorRef masterTxActor, final RemoteDeviceId id,
49                                      final ActorSystem actorSystem, final Timeout askTimeout) {
50         delegateRead = new ProxyReadAdapter(masterTxActor, id, actorSystem, askTimeout);
51         delegateWrite = new ProxyWriteAdapter(masterTxActor, id, actorSystem, askTimeout);
52     }
53
54     @Override
55     public boolean cancel() {
56         return delegateWrite.cancel();
57     }
58
59     @Override
60     public CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> read(final LogicalDatastoreType store,
61                                                                                    final YangInstanceIdentifier path) {
62         return delegateRead.read(store, path);
63     }
64
65     @Override
66     public CheckedFuture<Boolean, ReadFailedException> exists(final LogicalDatastoreType store,
67                                                               final YangInstanceIdentifier path) {
68         return delegateRead.exists(store, path);
69     }
70
71     @Override
72     public void delete(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
73         delegateWrite.delete(store, path);
74     }
75
76     @Override
77     public CheckedFuture<Void, TransactionCommitFailedException> submit() {
78         return MappingCheckedFuture.create(commit().transform(ignored -> null, MoreExecutors.directExecutor()),
79             new ExceptionMapper<TransactionCommitFailedException>("commit", TransactionCommitFailedException.class) {
80                 @Override
81                 protected TransactionCommitFailedException newWithCause(String message, Throwable cause) {
82                     return new TransactionCommitFailedException(message, cause);
83                 }
84             });
85     }
86
87     @Override
88     public @NonNull FluentFuture<? extends @NonNull CommitInfo> commit() {
89         return delegateWrite.commit(getIdentifier());
90     }
91
92     @Override
93     public void put(final LogicalDatastoreType store, final YangInstanceIdentifier path,
94                     final NormalizedNode<?, ?> data) {
95         delegateWrite.put(store, path, data, getIdentifier());
96     }
97
98     @Override
99     public void merge(final LogicalDatastoreType store, final YangInstanceIdentifier path,
100                       final NormalizedNode<?, ?> data) {
101         delegateWrite.merge(store, path, data, getIdentifier());
102     }
103
104     @Override
105     public Object getIdentifier() {
106         return this;
107     }
108 }