2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.datastore;
10 import akka.actor.ActorSelection;
11 import akka.dispatch.Mapper;
12 import com.google.common.base.Preconditions;
13 import org.opendaylight.controller.cluster.datastore.identifiers.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;
21 * A {@link Mapper} extracting the {@link ActorSelection} pointing to the actor which
22 * is backing a particular transaction.
24 * This class is not for general consumption. It is public only to support the pre-lithium compatibility
27 * TODO: once we remove compatibility, make this class package-private and final.
29 public class TransactionReadyReplyMapper extends Mapper<Object, ActorSelection> {
30 protected static final Mapper<Throwable, Throwable> SAME_FAILURE_TRANSFORMER = new Mapper<Throwable, Throwable>() {
32 public Throwable apply(final Throwable failure) {
36 private static final Logger LOG = LoggerFactory.getLogger(TransactionReadyReplyMapper.class);
37 private final TransactionIdentifier identifier;
38 private final ActorContext actorContext;
40 protected TransactionReadyReplyMapper(final ActorContext actorContext, final TransactionIdentifier identifier) {
41 this.actorContext = Preconditions.checkNotNull(actorContext);
42 this.identifier = Preconditions.checkNotNull(identifier);
45 protected final ActorContext getActorContext() {
49 protected String extractCohortPathFrom(final ReadyTransactionReply readyTxReply) {
50 return readyTxReply.getCohortPath();
54 public final ActorSelection checkedApply(final Object serializedReadyReply) {
55 LOG.debug("Tx {} readyTransaction", identifier);
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));
64 // Throwing an exception here will fail the Future.
65 throw new IllegalArgumentException(String.format("%s: Invalid reply type %s",
66 identifier, serializedReadyReply.getClass()));
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());