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 final ReplicatedLog replicatedLog;
38 private final Map<String, String> peerAddresses;
40 private final LoggingAdapter LOG;
42 public RaftActorContextImpl(ActorRef actor, UntypedActorContext context,
44 ElectionTerm termInformation, long commitIndex,
45 long lastApplied, ReplicatedLog replicatedLog, Map<String, String> peerAddresses, LoggingAdapter logger) {
47 this.context = context;
49 this.termInformation = termInformation;
50 this.commitIndex = commitIndex;
51 this.lastApplied = lastApplied;
52 this.replicatedLog = replicatedLog;
53 this.peerAddresses = peerAddresses;
57 public ActorRef actorOf(Props props){
58 return context.actorOf(props);
61 public ActorSelection actorSelection(String path){
62 return context.actorSelection(path);
65 public String getId() {
69 public ActorRef getActor() {
73 public ElectionTerm getTermInformation() {
74 return termInformation;
77 public long getCommitIndex() {
81 @Override public void setCommitIndex(long commitIndex) {
82 this.commitIndex = commitIndex;
85 public long getLastApplied() {
89 @Override public void setLastApplied(long lastApplied) {
90 this.lastApplied = lastApplied;
93 @Override public ReplicatedLog getReplicatedLog() {
97 @Override public ActorSystem getActorSystem() {
98 return context.system();
101 @Override public LoggingAdapter getLogger() {
105 @Override public Map<String, String> getPeerAddresses() {
106 return peerAddresses;
109 @Override public String getPeerAddress(String peerId) {
110 return peerAddresses.get(peerId);
113 @Override public void addToPeers(String name, String address) {
114 peerAddresses.put(name, address);
117 @Override public void removePeer(String name) {
118 peerAddresses.remove(name);
121 @Override public ActorSelection getPeerActorSelection(String peerId) {
122 String peerAddress = getPeerAddress(peerId);
123 if(peerAddress != null){
124 return actorSelection(peerAddress);
129 @Override public void setPeerAddress(String peerId, String peerAddress) {
130 LOG.info("Peer address for peer {} set to {}", peerId, peerAddress);
131 checkState(peerAddresses.containsKey(peerId), peerId + " is unknown");
133 peerAddresses.put(peerId, peerAddress);