Assign image keyname and pubkey at vnf level
[ccsdk/apps.git] / sdnr / wireless-transport / code-Carbon-SR1 / apps / websocketmanager / impl / src / main / java / org / opendaylight / mwtn / impl / utils / AkkaConfig.java
1 package org.opendaylight.mwtn.impl.utils;
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 com.typesafe.config.Config;
10 import com.typesafe.config.ConfigFactory;
11
12 public class AkkaConfig{
13
14
15
16         public static class ClusterNodeInfo
17         {
18                 @Override
19                 public String toString() {
20                         return "ClusterNodeInfo [protocol=" + protocol + ", clusterName=" + clusterName + ", remoteAdr=" + remoteAdr
21                                         + ", port=" + port + "]";
22                 }
23                 private final String protocol;
24                 private final String clusterName;
25                 private final String remoteAdr;
26                 private final int port;
27
28                 public String getRemoteAddress() {return this.remoteAdr;}
29                 public ClusterNodeInfo(String s) throws Exception
30                 {
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);
34                         if(!matcher.find())
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));
40                 }
41         }
42         public static class ClusterRoleInfo
43         {
44                 @Override
45                 public String toString() {
46                         return "ClusterRoleInfo [Role=" + Role + ", Index=" + Index + "]";
47                 }
48
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);
55                         if(!matcher.find())
56                                 throw new Exception("invalid role format");
57                         this.Role=matcher.group(1);
58                         this.Index=Integer.parseInt(matcher.group(2));
59                 }
60
61         }
62         public static class ClusterConfig
63         {
64                 @Override
65                 public String toString() {
66                         return "ClusterConfig [seedNodes=" + seedNodes + ", roles=" + roles + "]";
67                 }
68                 private final List<ClusterNodeInfo> seedNodes;
69                 private final List<ClusterRoleInfo> roles;
70                 private final ClusterNodeInfo ismeInfo;
71                 public ClusterConfig(Config o) throws Exception {
72                         {
73                         this.seedNodes = new ArrayList<ClusterNodeInfo>();
74                         List<String> a= o.getStringList("seed-nodes");
75                         for(int i=0;i<a.size();i++)
76                         {
77                                 ClusterNodeInfo info=new ClusterNodeInfo(a.get(i));
78                                 this.seedNodes.add(info);
79                         }
80                         this.roles=new ArrayList<ClusterRoleInfo>();
81                         a=o.getStringList("roles");
82                         for(int i=0;i<a.size();i++)
83                         {
84                                 ClusterRoleInfo s=new ClusterRoleInfo(a.get(i));
85                                 this.roles.add(s);
86                         }
87                         int idx=this.roles.get(0).Index-1;
88                         if(idx>=0 && idx<this.seedNodes.size())
89                                 this.ismeInfo=this.seedNodes.get(idx);
90                         else
91                                 this.ismeInfo=null;
92                 }
93
94         }
95                 public boolean isCluster() {
96                         return this.seedNodes!=null?this.seedNodes.size()>1:false;
97                 }
98                 public boolean isMe(ClusterNodeInfo i) {
99                         return this.ismeInfo!=null?this.ismeInfo.equals(i):false;
100                 }
101                 public List<ClusterNodeInfo> getSeedNodes() {
102                         return this.seedNodes;
103                 }
104         }
105
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;}
110
111         private AkkaConfig(String filename) {
112                 this.filename = filename;
113         }
114         @Override
115         public String toString() {
116                 return "AkkaConfig [filename=" + filename + ", cluserConfig=" + cluserConfig + "]";
117         }
118
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"));
122         }
123
124         public boolean isCluster()
125         {
126                 return this.cluserConfig!=null?this.cluserConfig.isCluster():false;
127         }
128         public static AkkaConfig load() throws Exception
129         {
130                 return load(DEFAULT_FILENAME);
131         }
132
133         public static AkkaConfig load(String filename) throws Exception
134         {
135                 AkkaConfig cfg=new AkkaConfig(filename);
136                 cfg.loadFromFile();
137
138                 return cfg;
139         }
140
141
142
143
144 }