Use YangInstanceIdentifier.EMPTY
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / ShardTransaction.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.ActorRef;
12 import akka.actor.PoisonPill;
13 import akka.actor.Props;
14 import akka.actor.ReceiveTimeout;
15 import akka.japi.Creator;
16 import com.google.common.base.Optional;
17 import com.google.common.base.Preconditions;
18 import org.opendaylight.controller.cluster.common.actor.AbstractUntypedActorWithMetering;
19 import org.opendaylight.controller.cluster.datastore.exceptions.UnknownMessageException;
20 import org.opendaylight.controller.cluster.datastore.jmx.mbeans.shard.ShardStats;
21 import org.opendaylight.controller.cluster.datastore.messages.CloseTransaction;
22 import org.opendaylight.controller.cluster.datastore.messages.CloseTransactionReply;
23 import org.opendaylight.controller.cluster.datastore.messages.DataExists;
24 import org.opendaylight.controller.cluster.datastore.messages.DataExistsReply;
25 import org.opendaylight.controller.cluster.datastore.messages.ReadData;
26 import org.opendaylight.controller.cluster.datastore.messages.ReadDataReply;
27 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
29 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
30
31 /**
32  * The ShardTransaction Actor represents a remote transaction
33  * <p>
34  * The ShardTransaction Actor delegates all actions to DOMDataReadWriteTransaction
35  * </p>
36  * <p>
37  * Handles Messages <br/>
38  * ---------------- <br/>
39  * <li> {@link org.opendaylight.controller.cluster.datastore.messages.ReadData}
40  * <li> {@link org.opendaylight.controller.cluster.datastore.messages.CloseTransaction}
41  * </p>
42  */
43 public abstract class ShardTransaction extends AbstractUntypedActorWithMetering {
44     private final ActorRef shardActor;
45     private final ShardStats shardStats;
46     private final String transactionID;
47
48     protected ShardTransaction(ActorRef shardActor, ShardStats shardStats, String transactionID) {
49         super("shard-tx"); //actor name override used for metering. This does not change the "real" actor name
50         this.shardActor = shardActor;
51         this.shardStats = shardStats;
52         this.transactionID = Preconditions.checkNotNull(transactionID);
53     }
54
55     public static Props props(TransactionType type, AbstractShardDataTreeTransaction<?> transaction, ActorRef shardActor,
56             DatastoreContext datastoreContext, ShardStats shardStats, String transactionID) {
57         return Props.create(new ShardTransactionCreator(type, transaction, shardActor,
58            datastoreContext, shardStats, transactionID));
59     }
60
61     protected abstract AbstractShardDataTreeTransaction<?> getDOMStoreTransaction();
62
63     protected ActorRef getShardActor() {
64         return shardActor;
65     }
66
67     protected String getTransactionID() {
68         return transactionID;
69     }
70
71     @Override
72     public void handleReceive(Object message) throws Exception {
73         if (CloseTransaction.isSerializedType(message)) {
74             closeTransaction(true);
75         } else if (message instanceof ReceiveTimeout) {
76             if(LOG.isDebugEnabled()) {
77                 LOG.debug("Got ReceiveTimeout for inactivity - closing Tx");
78             }
79             closeTransaction(false);
80         } else {
81             throw new UnknownMessageException(message);
82         }
83     }
84
85     protected boolean returnCloseTransactionReply() {
86         return true;
87     }
88
89     private void closeTransaction(boolean sendReply) {
90         getDOMStoreTransaction().abort();
91
92         if(sendReply && returnCloseTransactionReply()) {
93             getSender().tell(new CloseTransactionReply(), getSelf());
94         }
95
96         getSelf().tell(PoisonPill.getInstance(), getSelf());
97     }
98
99     private boolean checkClosed(AbstractShardDataTreeTransaction<?> transaction) {
100         final boolean ret = transaction.isClosed();
101         if (ret) {
102             shardStats.incrementFailedReadTransactionsCount();
103             getSender().tell(new akka.actor.Status.Failure(new ReadFailedException("Transaction is closed")), getSelf());
104         }
105         return ret;
106     }
107
108     protected void readData(AbstractShardDataTreeTransaction<?> transaction, ReadData message) {
109         if (checkClosed(transaction)) {
110             return;
111         }
112
113         final YangInstanceIdentifier path = message.getPath();
114         Optional<NormalizedNode<?, ?>> optional = transaction.getSnapshot().readNode(path);
115         ReadDataReply readDataReply = new ReadDataReply(optional.orNull(), message.getVersion());
116         sender().tell(readDataReply.toSerializable(), self());
117     }
118
119     protected void dataExists(AbstractShardDataTreeTransaction<?> transaction, DataExists message) {
120         if (checkClosed(transaction)) {
121             return;
122         }
123
124         final YangInstanceIdentifier path = message.getPath();
125         boolean exists = transaction.getSnapshot().readNode(path).isPresent();
126         getSender().tell(new DataExistsReply(exists, message.getVersion()).toSerializable(), getSelf());
127     }
128
129     private static class ShardTransactionCreator implements Creator<ShardTransaction> {
130
131         private static final long serialVersionUID = 1L;
132
133         final AbstractShardDataTreeTransaction<?> transaction;
134         final ActorRef shardActor;
135         final DatastoreContext datastoreContext;
136         final ShardStats shardStats;
137         final String transactionID;
138         final TransactionType type;
139
140         ShardTransactionCreator(TransactionType type, AbstractShardDataTreeTransaction<?> transaction, ActorRef shardActor,
141                 DatastoreContext datastoreContext, ShardStats shardStats, String transactionID) {
142             this.transaction = Preconditions.checkNotNull(transaction);
143             this.shardActor = shardActor;
144             this.shardStats = shardStats;
145             this.datastoreContext = datastoreContext;
146             this.transactionID = Preconditions.checkNotNull(transactionID);
147             this.type = type;
148         }
149
150         @Override
151         public ShardTransaction create() throws Exception {
152             final ShardTransaction tx;
153             switch (type) {
154             case READ_ONLY:
155                 tx = new ShardReadTransaction(transaction, shardActor,
156                     shardStats, transactionID);
157                 break;
158             case READ_WRITE:
159                 tx = new ShardReadWriteTransaction((ReadWriteShardDataTreeTransaction)transaction,
160                     shardActor, shardStats, transactionID);
161                 break;
162             case WRITE_ONLY:
163                 tx = new ShardWriteTransaction((ReadWriteShardDataTreeTransaction)transaction,
164                     shardActor, shardStats, transactionID);
165                 break;
166             default:
167                 throw new IllegalArgumentException("Unhandled transaction type " + type);
168             }
169
170             tx.getContext().setReceiveTimeout(datastoreContext.getShardTransactionIdleTimeout());
171             return tx;
172         }
173     }
174 }