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 static java.util.Objects.requireNonNull;
12 import akka.actor.ActorSelection;
13 import akka.dispatch.Mapper;
14 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
15 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransactionReply;
16 import org.opendaylight.controller.cluster.datastore.utils.ActorUtils;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19 import scala.concurrent.Future;
22 * A {@link Mapper} extracting the {@link ActorSelection} pointing to the actor which
23 * is backing a particular transaction.
26 * This class is not for general consumption. It is public only to support the pre-lithium compatibility
28 * TODO: once we remove compatibility, make this class package-private and final.
30 public class TransactionReadyReplyMapper extends Mapper<Object, ActorSelection> {
31 protected static final Mapper<Throwable, Throwable> SAME_FAILURE_TRANSFORMER = new Mapper<Throwable, Throwable>() {
33 public Throwable apply(final Throwable failure) {
37 private static final Logger LOG = LoggerFactory.getLogger(TransactionReadyReplyMapper.class);
38 private final TransactionIdentifier identifier;
39 private final ActorUtils actorUtils;
41 protected TransactionReadyReplyMapper(final ActorUtils actorUtils, final TransactionIdentifier identifier) {
42 this.actorUtils = requireNonNull(actorUtils);
43 this.identifier = requireNonNull(identifier);
46 protected final ActorUtils getActorUtils() {
50 protected String extractCohortPathFrom(final ReadyTransactionReply readyTxReply) {
51 return readyTxReply.getCohortPath();
55 public final ActorSelection checkedApply(final Object serializedReadyReply) {
56 LOG.debug("Tx {} readyTransaction", identifier);
58 // At this point the ready operation succeeded and we need to extract the cohort
59 // actor path from the reply.
60 if (ReadyTransactionReply.isSerializedType(serializedReadyReply)) {
61 ReadyTransactionReply readyTxReply = ReadyTransactionReply.fromSerializable(serializedReadyReply);
62 return actorUtils.actorSelection(extractCohortPathFrom(readyTxReply));
65 // Throwing an exception here will fail the Future.
66 throw new IllegalArgumentException(String.format("%s: Invalid reply type %s",
67 identifier, serializedReadyReply.getClass()));
70 static Future<ActorSelection> transform(final Future<Object> readyReplyFuture, final ActorUtils actorUtils,
71 final TransactionIdentifier identifier) {
72 return readyReplyFuture.transform(new TransactionReadyReplyMapper(actorUtils, identifier),
73 SAME_FAILURE_TRANSFORMER, actorUtils.getClientDispatcher());