Refactor ReplicatedLogImpl to separate class
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / raft / RaftActorContextImpl.java
1 /*
2  * Copyright (c) 2014 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
9 package org.opendaylight.controller.cluster.raft;
10
11 import static com.google.common.base.Preconditions.checkState;
12 import akka.actor.ActorRef;
13 import akka.actor.ActorSelection;
14 import akka.actor.ActorSystem;
15 import akka.actor.Props;
16 import akka.actor.UntypedActorContext;
17 import com.google.common.annotations.VisibleForTesting;
18 import com.google.common.base.Supplier;
19 import java.util.Map;
20 import org.slf4j.Logger;
21
22 public class RaftActorContextImpl implements RaftActorContext {
23
24     private final ActorRef actor;
25
26     private final UntypedActorContext context;
27
28     private final String id;
29
30     private final ElectionTerm termInformation;
31
32     private long commitIndex;
33
34     private long lastApplied;
35
36     private ReplicatedLog replicatedLog;
37
38     private final Map<String, String> peerAddresses;
39
40     private final Logger LOG;
41
42     private ConfigParams configParams;
43
44     @VisibleForTesting
45     private Supplier<Long> totalMemoryRetriever;
46
47     // Snapshot manager will need to be created on demand as it needs raft actor context which cannot
48     // be passed to it in the constructor
49     private SnapshotManager snapshotManager;
50
51     public RaftActorContextImpl(ActorRef actor, UntypedActorContext context, String id,
52             ElectionTerm termInformation, long commitIndex, long lastApplied, Map<String, String> peerAddresses,
53             ConfigParams configParams, Logger logger) {
54         this.actor = actor;
55         this.context = context;
56         this.id = id;
57         this.termInformation = termInformation;
58         this.commitIndex = commitIndex;
59         this.lastApplied = lastApplied;
60         this.peerAddresses = peerAddresses;
61         this.configParams = configParams;
62         this.LOG = logger;
63     }
64
65     void setConfigParams(ConfigParams configParams) {
66         this.configParams = configParams;
67     }
68
69     @Override
70     public ActorRef actorOf(Props props){
71         return context.actorOf(props);
72     }
73
74     @Override
75     public ActorSelection actorSelection(String path){
76         return context.actorSelection(path);
77     }
78
79     @Override
80     public String getId() {
81         return id;
82     }
83
84     @Override
85     public ActorRef getActor() {
86         return actor;
87     }
88
89     @Override
90     public ElectionTerm getTermInformation() {
91         return termInformation;
92     }
93
94     @Override
95     public long getCommitIndex() {
96         return commitIndex;
97     }
98
99     @Override public void setCommitIndex(long commitIndex) {
100         this.commitIndex = commitIndex;
101     }
102
103     @Override
104     public long getLastApplied() {
105         return lastApplied;
106     }
107
108     @Override public void setLastApplied(long lastApplied) {
109         this.lastApplied = lastApplied;
110     }
111
112     @Override public void setReplicatedLog(ReplicatedLog replicatedLog) {
113         this.replicatedLog = replicatedLog;
114     }
115
116     @Override public ReplicatedLog getReplicatedLog() {
117         return replicatedLog;
118     }
119
120     @Override public ActorSystem getActorSystem() {
121         return context.system();
122     }
123
124     @Override public Logger getLogger() {
125         return this.LOG;
126     }
127
128     @Override public Map<String, String> getPeerAddresses() {
129         return peerAddresses;
130     }
131
132     @Override public String getPeerAddress(String peerId) {
133         return peerAddresses.get(peerId);
134     }
135
136     @Override public ConfigParams getConfigParams() {
137         return configParams;
138     }
139
140     @Override public void addToPeers(String name, String address) {
141         peerAddresses.put(name, address);
142     }
143
144     @Override public void removePeer(String name) {
145         peerAddresses.remove(name);
146     }
147
148     @Override public ActorSelection getPeerActorSelection(String peerId) {
149         String peerAddress = getPeerAddress(peerId);
150         if(peerAddress != null){
151             return actorSelection(peerAddress);
152         }
153         return null;
154     }
155
156     @Override public void setPeerAddress(String peerId, String peerAddress) {
157         LOG.info("Peer address for peer {} set to {}", peerId, peerAddress);
158         checkState(peerAddresses.containsKey(peerId), peerId + " is unknown");
159
160         peerAddresses.put(peerId, peerAddress);
161     }
162
163     @Override
164     public SnapshotManager getSnapshotManager() {
165         if(snapshotManager == null){
166             snapshotManager = new SnapshotManager(this, LOG);
167         }
168         return snapshotManager;
169     }
170
171     @Override
172     public long getTotalMemory() {
173         return totalMemoryRetriever != null ? totalMemoryRetriever.get() : Runtime.getRuntime().totalMemory();
174     }
175
176     @Override
177     public void setTotalMemoryRetriever(Supplier<Long> retriever) {
178         totalMemoryRetriever = retriever;
179     }
180
181     @Override
182     public boolean hasFollowers() {
183         return getPeerAddresses().keySet().size() > 0;
184     }
185 }