Bump yangtools to 1.0.0-SNAPSHOT
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / compat / PreLithiumTransactionContextImpl.java
1 /*
2  * Copyright (c) 2015 Brocade Communications 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 package org.opendaylight.controller.cluster.datastore.compat;
9
10 import akka.actor.ActorSelection;
11 import org.opendaylight.controller.cluster.datastore.DataStoreVersions;
12 import org.opendaylight.controller.cluster.datastore.OperationLimiter;
13 import org.opendaylight.controller.cluster.datastore.RemoteTransactionContext;
14 import org.opendaylight.controller.cluster.datastore.identifiers.TransactionIdentifier;
15 import org.opendaylight.controller.cluster.datastore.messages.DeleteData;
16 import org.opendaylight.controller.cluster.datastore.messages.MergeData;
17 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransaction;
18 import org.opendaylight.controller.cluster.datastore.messages.VersionedExternalizableMessage;
19 import org.opendaylight.controller.cluster.datastore.messages.WriteData;
20 import org.opendaylight.controller.cluster.datastore.modification.AbstractModification;
21 import org.opendaylight.controller.cluster.datastore.modification.DeleteModification;
22 import org.opendaylight.controller.cluster.datastore.modification.MergeModification;
23 import org.opendaylight.controller.cluster.datastore.modification.WriteModification;
24 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import scala.concurrent.Future;
30
31 /**
32  * Implementation of TransactionContextImpl used when talking to a pre-Lithium controller that doesn't
33  * support the BatchedModifications message.
34  *
35  * @author Thomas Pantelis
36  */
37 @Deprecated
38 public class PreLithiumTransactionContextImpl extends RemoteTransactionContext {
39     private static final Logger LOG = LoggerFactory.getLogger(PreLithiumTransactionContextImpl.class);
40
41     private final String transactionPath;
42
43     public PreLithiumTransactionContextImpl(TransactionIdentifier identifier, String transactionPath, ActorSelection actor,
44             ActorContext actorContext, boolean isTxActorLocal,
45             short remoteTransactionVersion, OperationLimiter limiter) {
46         super(identifier, actor, actorContext, isTxActorLocal, remoteTransactionVersion, limiter);
47         this.transactionPath = transactionPath;
48     }
49
50     @Override
51     public void executeModification(AbstractModification modification) {
52         final short remoteTransactionVersion = getRemoteTransactionVersion();
53         final YangInstanceIdentifier path = modification.getPath();
54         VersionedExternalizableMessage msg = null;
55
56         if(modification instanceof DeleteModification) {
57             msg = new DeleteData(path, remoteTransactionVersion);
58         } else if(modification instanceof WriteModification) {
59             final NormalizedNode<?, ?> data = ((WriteModification) modification).getData();
60
61             // be sure to check for Merge before Write, since Merge is a subclass of Write
62             if(modification instanceof MergeModification) {
63                 msg = new MergeData(path, data, remoteTransactionVersion);
64             } else {
65                 msg = new WriteData(path, data, remoteTransactionVersion);
66             }
67         } else {
68             LOG.error("Invalid modification type " + modification.getClass().getName());
69         }
70
71         if(msg != null) {
72             executeOperationAsync(msg);
73         }
74     }
75
76     @Override
77     public Future<ActorSelection> readyTransaction() {
78         LOG.debug("Tx {} readyTransaction called", getIdentifier());
79
80         // Send the ReadyTransaction message to the Tx actor.
81
82         Future<Object> lastReplyFuture = executeOperationAsync(ReadyTransaction.INSTANCE);
83
84         return transformReadyReply(lastReplyFuture);
85     }
86
87     @Override
88     protected Future<ActorSelection> transformReadyReply(final Future<Object> readyReplyFuture) {
89         // In base Helium we used to return the local path of the actor which represented
90         // a remote ThreePhaseCommitCohort. The local path would then be converted to
91         // a remote path using this resolvePath method. To maintain compatibility with
92         // a Helium node we need to continue to do this conversion.
93         // At some point in the future when upgrades from Helium are not supported
94         // we could remove this code to resolvePath and just use the cohortPath as the
95         // resolved cohortPath
96         if (getRemoteTransactionVersion() < DataStoreVersions.HELIUM_1_VERSION) {
97             return PreLithiumTransactionReadyReplyMapper.transform(readyReplyFuture, getActorContext(), getIdentifier(), transactionPath);
98         } else {
99             return super.transformReadyReply(readyReplyFuture);
100         }
101     }
102
103     @Override
104     public boolean supportsDirectCommit() {
105         return false;
106     }
107
108     @Override
109     public Future<Object> directCommit() {
110         throw new UnsupportedOperationException("directCommit is not supported for " + getClass());
111     }
112 }