1 package org.opendaylight.mwtn.config.impl;
4 import java.util.ArrayList;
6 import java.util.regex.Matcher;
7 import java.util.regex.Pattern;
9 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory;
12 import com.typesafe.config.Config;
13 import com.typesafe.config.ConfigFactory;
15 public class AkkaConfig {
17 private static final Logger LOG = LoggerFactory.getLogger(AkkaConfig.class);
19 public static class ClusterNodeInfo {
21 public String toString() {
22 return "ClusterNodeInfo [protocol=" + protocol + ", clusterName=" + clusterName + ", remoteAdr=" + remoteAdr
23 + ", port=" + port + "]";
26 private final String protocol;
27 private final String clusterName;
28 private final String remoteAdr;
29 private final int port;
31 public String getRemoteAddress() {
32 return this.remoteAdr;
35 public ClusterNodeInfo(String s) throws Exception {
36 final String regex = "([a-z.]*):\\/\\/([a-zA-Z0-9-]*)@([a-zA-Z0-9.-]*):([0-9]*)";
37 final Pattern pattern = Pattern.compile(regex);
38 final Matcher matcher = pattern.matcher(s);
40 throw new Exception("invalid seedNode format");
41 this.protocol = matcher.group(1);
42 this.clusterName = matcher.group(2);
43 this.remoteAdr = matcher.group(3);
44 this.port = Integer.parseInt(matcher.group(4));
47 public ClusterNodeInfo(String protocol, String clustername, String remoteadr, int port) {
48 this.protocol=protocol;
49 this.clusterName=clustername;
50 this.remoteAdr=remoteadr;
54 public static ClusterNodeInfo defaultSingleNodeInfo() {
55 return new ClusterNodeInfo("akka.tcp","opendaylight-cluster-data","127.0.0.1",2550);
59 public static class ClusterRoleInfo {
60 private final String Role;
61 private final int Index;
63 public ClusterRoleInfo(String s) throws Exception {
64 final String regex = "([a-zA-Z]*)-([0-9]*)";
65 final Pattern pattern = Pattern.compile(regex);
66 final Matcher matcher = pattern.matcher(s);
68 throw new Exception("unexpected role format:"+s);
69 this.Role = matcher.group(1);
70 this.Index = Integer.parseInt(matcher.group(2));
73 private ClusterRoleInfo(String role, int idx) {
79 public boolean equals(Object obj) {
80 if(obj instanceof ClusterRoleInfo)
81 return ((ClusterRoleInfo)obj).Index== this.Index && ((ClusterRoleInfo)obj).Role.equals(this.Role);
82 return super.equals(obj);
84 public static ClusterRoleInfo defaultSingleNodeRole() {
85 return new ClusterRoleInfo("member",1);
88 public String toString() {
89 return "ClusterRoleInfo [Role=" + Role + ", Index=" + Index + "]";
92 public static class ClusterRoleInfoCollection extends ArrayList<ClusterRoleInfo>
94 private static final long serialVersionUID = 1L;
95 public ClusterRoleInfo get(String role)
97 for(ClusterRoleInfo info:this)
99 if(info.Role.equals(role))
104 public boolean contains(ClusterRoleInfo info)
108 for(ClusterRoleInfo i:this)
116 public static class ClusterConfig {
118 public String toString() {
119 return "ClusterConfig [seedNodes=" + seedNodes + ", roles=" + roles + "]";
122 private final List<ClusterNodeInfo> seedNodes;
123 private final ClusterRoleInfoCollection roles;
124 private ClusterNodeInfo ismeInfo;
126 public static ClusterConfig defaultSingleNodeConfig()
128 ClusterConfig cfg=new ClusterConfig();
129 cfg.ismeInfo=ClusterNodeInfo.defaultSingleNodeInfo();
130 cfg.seedNodes.add(cfg.ismeInfo);
131 cfg.roles.add(ClusterRoleInfo.defaultSingleNodeRole());
134 private ClusterConfig()
136 this.seedNodes = new ArrayList<ClusterNodeInfo>();
137 this.roles = new ClusterRoleInfoCollection();
140 public ClusterConfig(Config o) throws Exception {
142 this.seedNodes = new ArrayList<ClusterNodeInfo>();
143 this.roles = new ClusterRoleInfoCollection();
144 List<String> a = o.getStringList("seed-nodes");
145 for (int i = 0; i < a.size(); i++) {
146 ClusterNodeInfo info = new ClusterNodeInfo(a.get(i));
147 this.seedNodes.add(info);
149 a = o.getStringList("roles");
150 for (int i = 0; i < a.size(); i++) {
151 ClusterRoleInfo s = new ClusterRoleInfo(a.get(i));
154 int idx = this.roles.get(0).Index - 1;
155 if (idx >= 0 && idx < this.seedNodes.size())
156 this.ismeInfo = this.seedNodes.get(idx);
158 this.ismeInfo = null;
163 public boolean isCluster() {
164 return this.seedNodes != null ? this.seedNodes.size() > 1 : false;
167 public boolean isMe(ClusterNodeInfo i) {
168 return this.ismeInfo != null ? this.ismeInfo.equals(i) : false;
171 public List<ClusterNodeInfo> getSeedNodes() {
172 return this.seedNodes;
175 public String getHostName(String defaultValue) {
176 if (getRoleMemberIndex() > 0 && getRoleMemberIndex() <= seedNodes.size())
177 return this.seedNodes.get(getRoleMemberIndex()-1).remoteAdr;
179 LOG.warn("Seednode not available for roleMemberIndex {}. Using default {}",getRoleMember(), defaultValue);
184 public String getDBClusterName(String defaultValue) {
186 if (this.seedNodes != null && this.seedNodes.size() > 0) {
187 r = String.format("cluster-%s.%d", this.seedNodes.get(0).remoteAdr, this.seedNodes.get(0).port);
189 if (r == null || r.isEmpty())
194 public int getRoleMemberIndex() {
196 ClusterRoleInfo role=this.roles.get("member");
197 return role!=null?role.Index:0;
199 public ClusterRoleInfo getRoleMember() {
200 return this.roles.get("member");
204 private static final String DEFAULT_FILENAME = "configuration/initial/akka.conf";
205 private final String filename;
206 private ClusterConfig cluserConfig;
208 public ClusterConfig getClusterConfig() {
209 return this.cluserConfig;
212 private AkkaConfig(String filename) {
213 this.filename = filename;
216 public AkkaConfig() {
221 public String toString() {
222 return "AkkaConfig [filename=" + filename + ", cluserConfig=" + cluserConfig + "]";
225 private void loadFromFile() throws Exception {
226 Config cfg = ConfigFactory.parseFile(new File(this.filename));
227 this.cluserConfig = new ClusterConfig(cfg.getConfig("odl-cluster-data").getConfig("akka").getConfig("cluster"));
230 public boolean isCluster() {
231 return this.cluserConfig != null ? this.cluserConfig.isCluster() : false;
234 public static AkkaConfig load() throws Exception {
235 return load(DEFAULT_FILENAME);
238 public static AkkaConfig load(String filename) throws Exception {
239 AkkaConfig cfg = new AkkaConfig(filename);
244 public boolean isSingleNode() {
245 return !this.isCluster();
247 public static AkkaConfig defaultSingleNodeConfig()
249 AkkaConfig cfg=new AkkaConfig();
250 cfg.cluserConfig=new ClusterConfig();
254 public static AkkaConfig parse(String content) throws Exception {
255 Config cfg = ConfigFactory.parseString(content);
256 AkkaConfig c=new AkkaConfig();
257 c.cluserConfig=new ClusterConfig(cfg.getConfig("odl-cluster-data").getConfig("akka").getConfig("cluster"));