1 package org.opendaylight.mwtn.impl.utils;
4 import java.util.ArrayList;
6 import java.util.regex.Matcher;
7 import java.util.regex.Pattern;
9 import com.typesafe.config.Config;
10 import com.typesafe.config.ConfigFactory;
12 public class AkkaConfig{
16 public static class ClusterNodeInfo
19 public String toString() {
20 return "ClusterNodeInfo [protocol=" + protocol + ", clusterName=" + clusterName + ", remoteAdr=" + remoteAdr
21 + ", port=" + port + "]";
23 private final String protocol;
24 private final String clusterName;
25 private final String remoteAdr;
26 private final int port;
28 public String getRemoteAddress() {return this.remoteAdr;}
29 public ClusterNodeInfo(String s) throws Exception
31 final String regex ="([a-z.]*):\\/\\/([a-zA-Z0-9-]*)@([a-zA-Z0-9.-]*):([0-9]*)";
32 final Pattern pattern = Pattern.compile(regex);
33 final Matcher matcher = pattern.matcher(s);
35 throw new Exception("invalid seedNode format");
36 this.protocol=matcher.group(1);
37 this.clusterName=matcher.group(2);
38 this.remoteAdr=matcher.group(3);
39 this.port=Integer.parseInt(matcher.group(4));
42 public static class ClusterRoleInfo
45 public String toString() {
46 return "ClusterRoleInfo [Role=" + Role + ", Index=" + Index + "]";
49 private final String Role;
50 private final int Index;
51 public ClusterRoleInfo(String s) throws Exception {
52 final String regex = "([a-z]*)-([0-9]*)";
53 final Pattern pattern = Pattern.compile(regex);
54 final Matcher matcher = pattern.matcher(s);
56 throw new Exception("invalid role format");
57 this.Role=matcher.group(1);
58 this.Index=Integer.parseInt(matcher.group(2));
62 public static class ClusterConfig
65 public String toString() {
66 return "ClusterConfig [seedNodes=" + seedNodes + ", roles=" + roles + "]";
68 private final List<ClusterNodeInfo> seedNodes;
69 private final List<ClusterRoleInfo> roles;
70 private final ClusterNodeInfo ismeInfo;
71 public ClusterConfig(Config o) throws Exception {
73 this.seedNodes = new ArrayList<ClusterNodeInfo>();
74 List<String> a= o.getStringList("seed-nodes");
75 for(int i=0;i<a.size();i++)
77 ClusterNodeInfo info=new ClusterNodeInfo(a.get(i));
78 this.seedNodes.add(info);
80 this.roles=new ArrayList<ClusterRoleInfo>();
81 a=o.getStringList("roles");
82 for(int i=0;i<a.size();i++)
84 ClusterRoleInfo s=new ClusterRoleInfo(a.get(i));
87 int idx=this.roles.get(0).Index-1;
88 if(idx>=0 && idx<this.seedNodes.size())
89 this.ismeInfo=this.seedNodes.get(idx);
95 public boolean isCluster() {
96 return this.seedNodes!=null?this.seedNodes.size()>1:false;
98 public boolean isMe(ClusterNodeInfo i) {
99 return this.ismeInfo!=null?this.ismeInfo.equals(i):false;
101 public List<ClusterNodeInfo> getSeedNodes() {
102 return this.seedNodes;
106 private static final String DEFAULT_FILENAME = "configuration/initial/akka.conf";
107 private final String filename;
108 private ClusterConfig cluserConfig;
109 public ClusterConfig getClusterConfig() {return this.cluserConfig;}
111 private AkkaConfig(String filename) {
112 this.filename = filename;
115 public String toString() {
116 return "AkkaConfig [filename=" + filename + ", cluserConfig=" + cluserConfig + "]";
119 private void loadFromFile() throws Exception {
120 Config cfg=ConfigFactory.parseFile(new File(this.filename));
121 this.cluserConfig=new ClusterConfig(cfg.getConfig("odl-cluster-data").getConfig("akka").getConfig("cluster"));
124 public boolean isCluster()
126 return this.cluserConfig!=null?this.cluserConfig.isCluster():false;
128 public static AkkaConfig load() throws Exception
130 return load(DEFAULT_FILENAME);
133 public static AkkaConfig load(String filename) throws Exception
135 AkkaConfig cfg=new AkkaConfig(filename);