2 * Copyright (c) 2014 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
9 package org.opendaylight.controller.cluster.raft;
11 import akka.actor.ActorRef;
12 import akka.actor.ActorSelection;
13 import akka.actor.ActorSystem;
14 import akka.actor.Props;
15 import akka.actor.UntypedActorContext;
16 import akka.event.LoggingAdapter;
20 import static com.google.common.base.Preconditions.checkState;
22 public class RaftActorContextImpl implements RaftActorContext {
24 private final ActorRef actor;
26 private final UntypedActorContext context;
28 private final String id;
30 private final ElectionTerm termInformation;
32 private long commitIndex;
34 private long lastApplied;
36 private ReplicatedLog replicatedLog;
38 private final Map<String, String> peerAddresses;
40 private final LoggingAdapter LOG;
42 private final ConfigParams configParams;
44 public RaftActorContextImpl(ActorRef actor, UntypedActorContext context,
46 ElectionTerm termInformation, long commitIndex,
47 long lastApplied, ReplicatedLog replicatedLog,
48 Map<String, String> peerAddresses, ConfigParams configParams,
49 LoggingAdapter logger) {
51 this.context = context;
53 this.termInformation = termInformation;
54 this.commitIndex = commitIndex;
55 this.lastApplied = lastApplied;
56 this.replicatedLog = replicatedLog;
57 this.peerAddresses = peerAddresses;
58 this.configParams = configParams;
62 public ActorRef actorOf(Props props){
63 return context.actorOf(props);
66 public ActorSelection actorSelection(String path){
67 return context.actorSelection(path);
70 public String getId() {
74 public ActorRef getActor() {
78 public ElectionTerm getTermInformation() {
79 return termInformation;
82 public long getCommitIndex() {
86 @Override public void setCommitIndex(long commitIndex) {
87 this.commitIndex = commitIndex;
90 public long getLastApplied() {
94 @Override public void setLastApplied(long lastApplied) {
95 this.lastApplied = lastApplied;
98 @Override public void setReplicatedLog(ReplicatedLog replicatedLog) {
99 this.replicatedLog = replicatedLog;
102 @Override public ReplicatedLog getReplicatedLog() {
103 return replicatedLog;
106 @Override public ActorSystem getActorSystem() {
107 return context.system();
110 @Override public LoggingAdapter getLogger() {
114 @Override public Map<String, String> getPeerAddresses() {
115 return peerAddresses;
118 @Override public String getPeerAddress(String peerId) {
119 return peerAddresses.get(peerId);
122 @Override public ConfigParams getConfigParams() {
126 @Override public void addToPeers(String name, String address) {
127 peerAddresses.put(name, address);
130 @Override public void removePeer(String name) {
131 peerAddresses.remove(name);
134 @Override public ActorSelection getPeerActorSelection(String peerId) {
135 String peerAddress = getPeerAddress(peerId);
136 if(peerAddress != null){
137 return actorSelection(peerAddress);
142 @Override public void setPeerAddress(String peerId, String peerAddress) {
143 LOG.info("Peer address for peer {} set to {}", peerId, peerAddress);
144 checkState(peerAddresses.containsKey(peerId), peerId + " is unknown");
146 peerAddresses.put(peerId, peerAddress);