Merge "Re-added config.version to config-module-archetype."
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / TransactionProxy.java
1 /*
2  * Copyright (c) 2014 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.controller.cluster.datastore;
10
11 import akka.actor.ActorPath;
12 import akka.actor.ActorSelection;
13 import com.google.common.base.Optional;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import com.google.common.util.concurrent.ListenableFutureTask;
16 import org.opendaylight.controller.cluster.datastore.messages.CloseTransaction;
17 import org.opendaylight.controller.cluster.datastore.messages.CreateTransaction;
18 import org.opendaylight.controller.cluster.datastore.messages.CreateTransactionReply;
19 import org.opendaylight.controller.cluster.datastore.messages.DeleteData;
20 import org.opendaylight.controller.cluster.datastore.messages.MergeData;
21 import org.opendaylight.controller.cluster.datastore.messages.ReadData;
22 import org.opendaylight.controller.cluster.datastore.messages.ReadDataReply;
23 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransaction;
24 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransactionReply;
25 import org.opendaylight.controller.cluster.datastore.messages.WriteData;
26 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
27 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
28 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
29 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
30 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
31
32 import java.util.ArrayList;
33 import java.util.HashMap;
34 import java.util.List;
35 import java.util.Map;
36 import java.util.concurrent.Callable;
37 import java.util.concurrent.ExecutorService;
38 import java.util.concurrent.atomic.AtomicLong;
39
40 /**
41  * TransactionProxy acts as a proxy for one or more transactions that were created on a remote shard
42  * <p>
43  * Creating a transaction on the consumer side will create one instance of a transaction proxy. If during
44  * the transaction reads and writes are done on data that belongs to different shards then a separate transaction will
45  * be created on each of those shards by the TransactionProxy
46  *</p>
47  * <p>
48  * The TransactionProxy does not make any guarantees about atomicity or order in which the transactions on the various
49  * shards will be executed.
50  * </p>
51  */
52 public class TransactionProxy implements DOMStoreReadWriteTransaction {
53     public enum TransactionType {
54         READ_ONLY,
55         WRITE_ONLY,
56         READ_WRITE
57     }
58
59     private static final AtomicLong counter = new AtomicLong();
60
61     private final TransactionType transactionType;
62     private final ActorContext actorContext;
63     private final Map<String, ActorSelection> remoteTransactionPaths = new HashMap<>();
64     private final String identifier;
65     private final ExecutorService executor;
66
67     public TransactionProxy(
68         ActorContext actorContext,
69         TransactionType transactionType,
70         ExecutorService executor
71         ) {
72
73         this.identifier = "txn-" + counter.getAndIncrement();
74         this.transactionType = transactionType;
75         this.actorContext = actorContext;
76         this.executor = executor;
77
78         Object response = actorContext.executeShardOperation(Shard.DEFAULT_NAME, new CreateTransaction(identifier), ActorContext.ASK_DURATION);
79         if(response instanceof CreateTransactionReply){
80             CreateTransactionReply reply = (CreateTransactionReply) response;
81             remoteTransactionPaths.put(Shard.DEFAULT_NAME, actorContext.actorSelection(reply.getTransactionPath()));
82         }
83     }
84
85     @Override
86     public ListenableFuture<Optional<NormalizedNode<?, ?>>> read(final InstanceIdentifier path) {
87         final ActorSelection remoteTransaction = remoteTransactionFromIdentifier(path);
88
89         Callable<Optional<NormalizedNode<?,?>>> call = new Callable() {
90
91             @Override public Optional<NormalizedNode<?,?>> call() throws Exception {
92                 Object response = actorContext
93                     .executeRemoteOperation(remoteTransaction, new ReadData(path),
94                         ActorContext.ASK_DURATION);
95                 if(response instanceof ReadDataReply){
96                     ReadDataReply reply = (ReadDataReply) response;
97                     if(reply.getNormalizedNode() == null){
98                         return Optional.absent();
99                     }
100                     //FIXME : A cast should not be required here ???
101                     return (Optional<NormalizedNode<?, ?>>) Optional.of(reply.getNormalizedNode());
102                 }
103
104                 return Optional.absent();
105             }
106         };
107
108         ListenableFutureTask<Optional<NormalizedNode<?, ?>>>
109             future = ListenableFutureTask.create(call);
110
111         executor.submit(future);
112
113         return future;
114     }
115
116     @Override
117     public void write(InstanceIdentifier path, NormalizedNode<?, ?> data) {
118         final ActorSelection remoteTransaction = remoteTransactionFromIdentifier(path);
119         remoteTransaction.tell(new WriteData(path, data), null);
120     }
121
122     @Override
123     public void merge(InstanceIdentifier path, NormalizedNode<?, ?> data) {
124         final ActorSelection remoteTransaction = remoteTransactionFromIdentifier(path);
125         remoteTransaction.tell(new MergeData(path, data), null);
126     }
127
128     @Override
129     public void delete(InstanceIdentifier path) {
130         final ActorSelection remoteTransaction = remoteTransactionFromIdentifier(path);
131         remoteTransaction.tell(new DeleteData(path), null);
132     }
133
134     @Override
135     public DOMStoreThreePhaseCommitCohort ready() {
136         List<ActorPath> cohortPaths = new ArrayList<>();
137
138         for(ActorSelection remoteTransaction : remoteTransactionPaths.values()) {
139             Object result = actorContext.executeRemoteOperation(remoteTransaction,
140                 new ReadyTransaction(),
141                 ActorContext.ASK_DURATION
142             );
143
144             if(result instanceof ReadyTransactionReply){
145                 ReadyTransactionReply reply = (ReadyTransactionReply) result;
146                 cohortPaths.add(reply.getCohortPath());
147             }
148         }
149
150         return new ThreePhaseCommitCohortProxy(actorContext, cohortPaths, identifier, executor);
151     }
152
153     @Override
154     public Object getIdentifier() {
155         return this.identifier;
156     }
157
158     @Override
159     public void close() {
160         for(ActorSelection remoteTransaction : remoteTransactionPaths.values()) {
161             remoteTransaction.tell(new CloseTransaction(), null);
162         }
163     }
164
165     private ActorSelection remoteTransactionFromIdentifier(InstanceIdentifier path){
166         String shardName = shardNameFromIdentifier(path);
167         return remoteTransactionPaths.get(shardName);
168     }
169
170     private String shardNameFromIdentifier(InstanceIdentifier path){
171         return Shard.DEFAULT_NAME;
172     }
173 }