Merge "Fixed for bug : 1171 - issue while creating subnet"
[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 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;
17
18 import java.util.Map;
19
20 import static com.google.common.base.Preconditions.checkState;
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 final ReplicatedLog replicatedLog;
37
38     private final Map<String, String> peerAddresses;
39
40     private final LoggingAdapter LOG;
41
42     public RaftActorContextImpl(ActorRef actor, UntypedActorContext context,
43         String id,
44         ElectionTerm termInformation, long commitIndex,
45         long lastApplied, ReplicatedLog replicatedLog, Map<String, String> peerAddresses, LoggingAdapter logger) {
46         this.actor = actor;
47         this.context = context;
48         this.id = id;
49         this.termInformation = termInformation;
50         this.commitIndex = commitIndex;
51         this.lastApplied = lastApplied;
52         this.replicatedLog = replicatedLog;
53         this.peerAddresses = peerAddresses;
54         this.LOG = logger;
55     }
56
57     public ActorRef actorOf(Props props){
58         return context.actorOf(props);
59     }
60
61     public ActorSelection actorSelection(String path){
62         return context.actorSelection(path);
63     }
64
65     public String getId() {
66         return id;
67     }
68
69     public ActorRef getActor() {
70         return actor;
71     }
72
73     public ElectionTerm getTermInformation() {
74         return termInformation;
75     }
76
77     public long getCommitIndex() {
78         return commitIndex;
79     }
80
81     @Override public void setCommitIndex(long commitIndex) {
82         this.commitIndex = commitIndex;
83     }
84
85     public long getLastApplied() {
86         return lastApplied;
87     }
88
89     @Override public void setLastApplied(long lastApplied) {
90         this.lastApplied = lastApplied;
91     }
92
93     @Override public ReplicatedLog getReplicatedLog() {
94         return replicatedLog;
95     }
96
97     @Override public ActorSystem getActorSystem() {
98         return context.system();
99     }
100
101     @Override public LoggingAdapter getLogger() {
102         return this.LOG;
103     }
104
105     @Override public Map<String, String> getPeerAddresses() {
106         return peerAddresses;
107     }
108
109     @Override public String getPeerAddress(String peerId) {
110         return peerAddresses.get(peerId);
111     }
112
113     @Override public void addToPeers(String name, String address) {
114         peerAddresses.put(name, address);
115     }
116
117     @Override public void removePeer(String name) {
118         peerAddresses.remove(name);
119     }
120
121     @Override public ActorSelection getPeerActorSelection(String peerId) {
122         String peerAddress = getPeerAddress(peerId);
123         if(peerAddress != null){
124             return actorSelection(peerAddress);
125         }
126         return null;
127     }
128
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");
132
133         peerAddresses.put(peerId, peerAddress);
134     }
135 }