Fixup checkstyle
[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 static java.util.Objects.requireNonNull;
11
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;
20
21 /**
22  * A {@link Mapper} extracting the {@link ActorSelection} pointing to the actor which
23  * is backing a particular transaction.
24  *
25  * <p>
26  * This class is not for general consumption. It is public only to support the pre-lithium compatibility
27  * package.
28  * TODO: once we remove compatibility, make this class package-private and final.
29  */
30 public class TransactionReadyReplyMapper extends Mapper<Object, ActorSelection> {
31     protected static final Mapper<Throwable, Throwable> SAME_FAILURE_TRANSFORMER = new Mapper<Throwable, Throwable>() {
32         @Override
33         public Throwable apply(final Throwable failure) {
34             return failure;
35         }
36     };
37     private static final Logger LOG = LoggerFactory.getLogger(TransactionReadyReplyMapper.class);
38     private final TransactionIdentifier identifier;
39     private final ActorUtils actorUtils;
40
41     protected TransactionReadyReplyMapper(final ActorUtils actorUtils, final TransactionIdentifier identifier) {
42         this.actorUtils = requireNonNull(actorUtils);
43         this.identifier = requireNonNull(identifier);
44     }
45
46     protected final ActorUtils getActorUtils() {
47         return actorUtils;
48     }
49
50     protected String extractCohortPathFrom(final ReadyTransactionReply readyTxReply) {
51         return readyTxReply.getCohortPath();
52     }
53
54     @Override
55     public final ActorSelection checkedApply(final Object serializedReadyReply) {
56         LOG.debug("Tx {} readyTransaction", identifier);
57
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));
63         }
64
65         // Throwing an exception here will fail the Future.
66         throw new IllegalArgumentException(String.format("%s: Invalid reply type %s",
67                 identifier, serializedReadyReply.getClass()));
68     }
69
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());
74     }
75 }