1 package org.opendaylight.mwtn.config.impl;
4 import java.util.ArrayList;
7 import org.opendaylight.mwtn.config.impl.AkkaConfig.ClusterRoleInfo;
9 import com.typesafe.config.Config;
10 import com.typesafe.config.ConfigFactory;
12 public class GeoConfig {
15 public static class RolesTableEntry
18 public String toString() {
19 return "RolesTableEntry [role=" + role + ", ip=" + ip + "]";
21 private final AkkaConfig.ClusterRoleInfo role;
22 private final String ip;
23 public RolesTableEntry(Config c) throws Exception {
24 this.role = new ClusterRoleInfo(c.getString("role"));
25 this.ip=c.getString("ip");
28 public static class RolesTable extends ArrayList<RolesTableEntry>
33 private static final long serialVersionUID = -9146218864237487506L;
35 public RolesTable(List<? extends Config> configList) throws Exception {
36 for(Config c : configList)
38 this.add(new RolesTableEntry(c));
44 private static final String DEFAULT_FILENAME = "configuration/initial/geo.conf";
45 private static final String LUMINA_ROOTNODENAME = "lumina-geo-cluster";
46 private final String filename;
47 private final String rootNodename;
48 private AkkaConfig.ClusterRoleInfoCollection primaryRoles;
49 private AkkaConfig.ClusterRoleInfoCollection secondayRoles;
50 private RolesTable rolesTable;
56 public String toString() {
57 return "GeoConfig [filename=" + filename + ", rootNodename=" + rootNodename + ", primaryRoles=" + primaryRoles
58 + ", secondayRoles=" + secondayRoles + ", rolesTable=" + rolesTable + "]";
60 private GeoConfig(String filename)
62 this(filename,LUMINA_ROOTNODENAME);
64 private GeoConfig(String filename,String rootNodeName)
66 this.filename=filename;
67 this.rootNodename=rootNodeName;
69 public static boolean fileExists()
71 File f=new File(DEFAULT_FILENAME);
74 public static GeoConfig load() throws Exception
76 return load(DEFAULT_FILENAME);
78 public static GeoConfig load(String filename) throws Exception
80 GeoConfig cfg=new GeoConfig(filename);
84 private void _load() throws Exception
86 this._load(ConfigFactory.parseFile(new File(this.filename)));
88 private void _load(Config cfg) throws Exception {
89 this.primaryRoles=new AkkaConfig.ClusterRoleInfoCollection();
90 List<String> a=cfg.getConfig(this.rootNodename).getStringList("primary_roles");
92 for (int i = 0; i < a.size(); i++) {
93 ClusterRoleInfo s = new ClusterRoleInfo(a.get(i));
94 this.primaryRoles.add(s);
96 this.secondayRoles=new AkkaConfig.ClusterRoleInfoCollection();
97 a=cfg.getConfig(this.rootNodename).getStringList("secondary_roles");
98 for (int i = 0; i < a.size(); i++) {
99 ClusterRoleInfo s = new ClusterRoleInfo(a.get(i));
100 this.secondayRoles.add(s);
102 this.checkDuplicateRoleEntries();
103 this.rolesTable = new RolesTable(cfg.getConfig(this.rootNodename).getConfigList("ip_roles_table"));
105 private void checkDuplicateRoleEntries() throws Exception {
106 AkkaConfig.ClusterRoleInfoCollection duplicateEntries=new AkkaConfig.ClusterRoleInfoCollection();
107 for(ClusterRoleInfo primaryRole: this.primaryRoles)
109 if(this.secondayRoles.contains(primaryRole))
110 duplicateEntries.add(primaryRole);
112 if(duplicateEntries.size()>0)
113 throw new Exception("duplicate entries found: "+duplicateEntries.toString());
116 public static GeoConfig parse(String content) throws Exception {
117 GeoConfig cfg=new GeoConfig();
118 cfg._load(ConfigFactory.parseString(content));
121 public AkkaConfig.ClusterRoleInfoCollection getPrimaryRoles() {
122 return this.primaryRoles;
124 public AkkaConfig.ClusterRoleInfoCollection getSecondaryRoles() {
125 return this.secondayRoles;
127 public boolean isPrimary(ClusterRoleInfo roleMember) {
128 return !this.isSecondary(roleMember);
130 private boolean isSecondary(ClusterRoleInfo roleMember) {
133 for(ClusterRoleInfo info:this.secondayRoles)
135 if(info.equals(roleMember))