7f430a35bc649149bf0a8439cd0cdb8c577bd970
[ccsdk/apps.git] / sdnr / wireless-transport / code-Carbon-SR1 / apps / devicemanager / impl / src / main / java / org / opendaylight / mwtn / config / impl / AkkaConfig.java
1 package org.opendaylight.mwtn.config.impl;
2
3 import java.io.File;
4 import java.util.ArrayList;
5 import java.util.List;
6 import java.util.regex.Matcher;
7 import java.util.regex.Pattern;
8
9 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory;
11
12 import com.typesafe.config.Config;
13 import com.typesafe.config.ConfigFactory;
14
15 public class AkkaConfig {
16
17         private static final Logger LOG = LoggerFactory.getLogger(AkkaConfig.class);
18
19         public static class ClusterNodeInfo {
20                 @Override
21                 public String toString() {
22                         return "ClusterNodeInfo [protocol=" + protocol + ", clusterName=" + clusterName + ", remoteAdr=" + remoteAdr
23                                         + ", port=" + port + "]";
24                 }
25
26                 private final String protocol;
27                 private final String clusterName;
28                 private final String remoteAdr;
29                 private final int port;
30
31                 public String getRemoteAddress() {
32                         return this.remoteAdr;
33                 }
34
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);
39                         if (!matcher.find())
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));
45                 }
46
47                 public ClusterNodeInfo(String protocol, String clustername, String remoteadr, int port) {
48                         this.protocol=protocol;
49                         this.clusterName=clustername;
50                         this.remoteAdr=remoteadr;
51                         this.port=port;
52                 }
53
54                 public static ClusterNodeInfo defaultSingleNodeInfo() {
55                         return new ClusterNodeInfo("akka.tcp","opendaylight-cluster-data","127.0.0.1",2550);
56                 }
57         }
58
59         public static class ClusterRoleInfo {
60                 private final String Role;
61                 private final int Index;
62
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);
67                         if (!matcher.find())
68                                 throw new Exception("unexpected role format:"+s);
69                         this.Role = matcher.group(1);
70                         this.Index = Integer.parseInt(matcher.group(2));
71                 }
72
73                 private ClusterRoleInfo(String role, int idx) {
74                         this.Role=role;
75                         this.Index=idx;
76                 }
77
78                 @Override
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);
83                 }
84                 public static ClusterRoleInfo defaultSingleNodeRole() {
85                         return new ClusterRoleInfo("member",1);
86                 }
87                 @Override
88                 public String toString() {
89                         return "ClusterRoleInfo [Role=" + Role + ", Index=" + Index + "]";
90                 }
91         }
92         public static class ClusterRoleInfoCollection extends ArrayList<ClusterRoleInfo>
93         {
94                 private static final long serialVersionUID = 1L;
95                 public ClusterRoleInfo get(String role)
96                 {
97                         for(ClusterRoleInfo info:this)
98                         {
99                                 if(info.Role.equals(role))
100                                         return info;
101                         }
102                         return null;
103                 }
104                 public boolean contains(ClusterRoleInfo info)
105                 {
106                         if(info==null)
107                                 return false;
108                         for(ClusterRoleInfo i:this)
109                         {
110                                 if(i.equals(info))
111                                         return true;
112                         }
113                         return false;
114                 }
115         }
116         public static class ClusterConfig {
117                 @Override
118                 public String toString() {
119                         return "ClusterConfig [seedNodes=" + seedNodes + ", roles=" + roles + "]";
120                 }
121
122                 private final List<ClusterNodeInfo> seedNodes;
123                 private final ClusterRoleInfoCollection roles;
124                 private ClusterNodeInfo ismeInfo;
125
126                 public static ClusterConfig defaultSingleNodeConfig()
127                 {
128                         ClusterConfig cfg=new ClusterConfig();
129                         cfg.ismeInfo=ClusterNodeInfo.defaultSingleNodeInfo();
130                         cfg.seedNodes.add(cfg.ismeInfo);
131                         cfg.roles.add(ClusterRoleInfo.defaultSingleNodeRole());
132                         return cfg;
133                 }
134                 private ClusterConfig()
135                 {
136                         this.seedNodes = new ArrayList<ClusterNodeInfo>();
137                         this.roles = new ClusterRoleInfoCollection();
138
139                 }
140                 public ClusterConfig(Config o) throws Exception {
141                         {
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);
148                                 }
149                                 a = o.getStringList("roles");
150                                 for (int i = 0; i < a.size(); i++) {
151                                         ClusterRoleInfo s = new ClusterRoleInfo(a.get(i));
152                                         this.roles.add(s);
153                                 }
154                                 int idx = this.roles.get(0).Index - 1;
155                                 if (idx >= 0 && idx < this.seedNodes.size())
156                                         this.ismeInfo = this.seedNodes.get(idx);
157                                 else
158                                         this.ismeInfo = null;
159                         }
160
161                 }
162
163                 public boolean isCluster() {
164                         return this.seedNodes != null ? this.seedNodes.size() > 1 : false;
165                 }
166
167                 public boolean isMe(ClusterNodeInfo i) {
168                         return this.ismeInfo != null ? this.ismeInfo.equals(i) : false;
169                 }
170
171                 public List<ClusterNodeInfo> getSeedNodes() {
172                         return this.seedNodes;
173                 }
174
175                 public String getHostName(String defaultValue) {
176                         if (getRoleMemberIndex() > 0 && getRoleMemberIndex() <= seedNodes.size())
177                                 return this.seedNodes.get(getRoleMemberIndex()-1).remoteAdr;
178                         else {
179                                 LOG.warn("Seednode not available for roleMemberIndex {}. Using default {}",getRoleMember(), defaultValue);
180                                 return defaultValue;
181                         }
182                 }
183
184                 public String getDBClusterName(String defaultValue) {
185                         String r = null;
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);
188                         }
189                         if (r == null || r.isEmpty())
190                                 r = defaultValue;
191                         return r;
192                 }
193
194                 public int getRoleMemberIndex() {
195
196                         ClusterRoleInfo role=this.roles.get("member");
197                         return role!=null?role.Index:0;
198                 }
199                 public ClusterRoleInfo getRoleMember() {
200                         return this.roles.get("member");
201                 }
202         }
203
204         private static final String DEFAULT_FILENAME = "configuration/initial/akka.conf";
205         private final String filename;
206         private ClusterConfig cluserConfig;
207
208         public ClusterConfig getClusterConfig() {
209                 return this.cluserConfig;
210         }
211
212         private AkkaConfig(String filename) {
213                 this.filename = filename;
214         }
215
216         public AkkaConfig() {
217                 this(null);
218         }
219
220         @Override
221         public String toString() {
222                 return "AkkaConfig [filename=" + filename + ", cluserConfig=" + cluserConfig + "]";
223         }
224
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"));
228         }
229
230         public boolean isCluster() {
231                 return this.cluserConfig != null ? this.cluserConfig.isCluster() : false;
232         }
233
234         public static AkkaConfig load() throws Exception {
235                 return load(DEFAULT_FILENAME);
236         }
237
238         public static AkkaConfig load(String filename) throws Exception {
239                 AkkaConfig cfg = new AkkaConfig(filename);
240                 cfg.loadFromFile();
241                 return cfg;
242         }
243
244         public boolean isSingleNode() {
245                 return !this.isCluster();
246         }
247         public static AkkaConfig defaultSingleNodeConfig()
248         {
249                 AkkaConfig cfg=new AkkaConfig();
250                 cfg.cluserConfig=new ClusterConfig();
251                 return cfg;
252         }
253
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"));
258                 return c;
259         }
260 }