43c3be11c278de94cddfdccb66bcc26414bf1f5f
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / TransactionReadyReplyMapper.java
1 /*
2  * Copyright (c) 2015 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 package org.opendaylight.controller.cluster.datastore;
9
10 import akka.actor.ActorSelection;
11 import akka.dispatch.Mapper;
12 import com.google.common.base.Preconditions;
13 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
14 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransactionReply;
15 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18 import scala.concurrent.Future;
19
20 /**
21  * A {@link Mapper} extracting the {@link ActorSelection} pointing to the actor which
22  * is backing a particular transaction.
23  *
24  * This class is not for general consumption. It is public only to support the pre-lithium compatibility
25  * package.
26  *
27  * TODO: once we remove compatibility, make this class package-private and final.
28  */
29 public class TransactionReadyReplyMapper extends Mapper<Object, ActorSelection> {
30     protected static final Mapper<Throwable, Throwable> SAME_FAILURE_TRANSFORMER = new Mapper<Throwable, Throwable>() {
31         @Override
32         public Throwable apply(final Throwable failure) {
33             return failure;
34         }
35     };
36     private static final Logger LOG = LoggerFactory.getLogger(TransactionReadyReplyMapper.class);
37     private final TransactionIdentifier identifier;
38     private final ActorContext actorContext;
39
40     protected TransactionReadyReplyMapper(final ActorContext actorContext, final TransactionIdentifier identifier) {
41         this.actorContext = Preconditions.checkNotNull(actorContext);
42         this.identifier = Preconditions.checkNotNull(identifier);
43     }
44
45     protected final ActorContext getActorContext() {
46         return actorContext;
47     }
48
49     protected String extractCohortPathFrom(final ReadyTransactionReply readyTxReply) {
50         return readyTxReply.getCohortPath();
51     }
52
53     @Override
54     public final ActorSelection checkedApply(final Object serializedReadyReply) {
55         LOG.debug("Tx {} readyTransaction", identifier);
56
57         // At this point the ready operation succeeded and we need to extract the cohort
58         // actor path from the reply.
59         if (ReadyTransactionReply.isSerializedType(serializedReadyReply)) {
60             ReadyTransactionReply readyTxReply = ReadyTransactionReply.fromSerializable(serializedReadyReply);
61             return actorContext.actorSelection(extractCohortPathFrom(readyTxReply));
62         }
63
64         // Throwing an exception here will fail the Future.
65         throw new IllegalArgumentException(String.format("%s: Invalid reply type %s",
66                 identifier, serializedReadyReply.getClass()));
67     }
68
69     static Future<ActorSelection> transform(final Future<Object> readyReplyFuture, final ActorContext actorContext,
70             final TransactionIdentifier identifier) {
71         return readyReplyFuture.transform(new TransactionReadyReplyMapper(actorContext, identifier),
72             SAME_FAILURE_TRANSFORMER, actorContext.getClientDispatcher());
73     }
74 }