b61462b4999fb36c57141f5015fe6d2ee958ce9a
[ccsdk/features.git] /
1 /*
2  * ============LICENSE_START========================================================================
3  * ONAP : ccsdk feature sdnr wt
4  * =================================================================================================
5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
6  * =================================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  * 
10  * http://www.apache.org/licenses/LICENSE-2.0
11  * 
12  * Unless required by applicable law or agreed to in writing, software distributed under the License
13  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14  * or implied. See the License for the specific language governing permissions and limitations under
15  * the License.
16  * ============LICENSE_END==========================================================================
17  */
18 package org.onap.ccsdk.features.sdnr.wt.websocketmanager2.utils;
19
20 import java.io.File;
21 import java.util.ArrayList;
22 import java.util.List;
23 import java.util.regex.Matcher;
24 import java.util.regex.Pattern;
25 import org.onap.ccsdk.features.sdnr.wt.websocketmanager2.WebSocketManager;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 import com.typesafe.config.Config;
30 import com.typesafe.config.ConfigFactory;
31
32 public class AkkaConfig {
33
34     private static final Logger LOG = LoggerFactory.getLogger(WebSocketManager.class.getName());
35
36     public static class ClusterNodeInfo {
37         @Override
38         public String toString() {
39             return "ClusterNodeInfo [protocol=" + protocol + ", clusterName=" + clusterName + ", remoteAdr=" + remoteAdr
40                     + ", port=" + port + "]";
41         }
42
43         private final String protocol;
44         private final String clusterName;
45         private final String remoteAdr;
46         private final int port;
47
48         public String getRemoteAddress() {
49             return this.remoteAdr;
50         }
51
52         public ClusterNodeInfo(String s) throws Exception {
53             final String regex = "([a-z.]*):\\/\\/([a-zA-Z0-9-]*)@([a-zA-Z0-9.-]*):([0-9]*)";
54             final Pattern pattern = Pattern.compile(regex);
55             final Matcher matcher = pattern.matcher(s);
56             if (!matcher.find()) {
57                 throw new Exception("invalid seedNode format");
58             }
59             this.protocol = matcher.group(1);
60             this.clusterName = matcher.group(2);
61             this.remoteAdr = matcher.group(3);
62             this.port = Integer.parseInt(matcher.group(4));
63         }
64     }
65     public static class ClusterRoleInfo {
66         @Override
67         public String toString() {
68             return "ClusterRoleInfo [Role=" + Role + ", Index=" + Index + "]";
69         }
70
71         private final String Role;
72         private final int Index;
73
74         public ClusterRoleInfo(String s) throws Exception {
75             final String regex = "([a-z]*)-([0-9]*)";
76             final Pattern pattern = Pattern.compile(regex);
77             final Matcher matcher = pattern.matcher(s);
78             if (!matcher.find()) {
79                 throw new Exception("invalid role format");
80             }
81             this.Role = matcher.group(1);
82             this.Index = Integer.parseInt(matcher.group(2));
83         }
84
85     }
86     public static class ClusterConfig {
87         @Override
88         public String toString() {
89             return "ClusterConfig [seedNodes=" + seedNodes + ", roles=" + roles + "]";
90         }
91
92         private final List<ClusterNodeInfo> seedNodes;
93         private final List<ClusterRoleInfo> roles;
94         private final ClusterNodeInfo ismeInfo;
95
96         public ClusterConfig(Config o) throws Exception {
97             {
98                 this.seedNodes = new ArrayList<>();
99                 List<String> a = o.getStringList("seed-nodes");
100                 for (int i = 0; i < a.size(); i++) {
101                     ClusterNodeInfo info = new ClusterNodeInfo(a.get(i));
102                     this.seedNodes.add(info);
103                 }
104                 this.roles = new ArrayList<>();
105                 a = o.getStringList("roles");
106                 for (int i = 0; i < a.size(); i++) {
107                     ClusterRoleInfo s = new ClusterRoleInfo(a.get(i));
108                     this.roles.add(s);
109                 }
110                 int idx = this.roles.get(0).Index - 1;
111                 if (idx >= 0 && idx < this.seedNodes.size()) {
112                     this.ismeInfo = this.seedNodes.get(idx);
113                 } else {
114                     this.ismeInfo = null;
115                 }
116             }
117
118         }
119
120         public boolean isCluster() {
121             return this.seedNodes != null ? this.seedNodes.size() > 1 : false;
122         }
123
124         public boolean isMe(ClusterNodeInfo i) {
125             return this.ismeInfo != null ? this.ismeInfo.equals(i) : false;
126         }
127
128         public List<ClusterNodeInfo> getSeedNodes() {
129             return this.seedNodes;
130         }
131     }
132
133     private static final String DEFAULT_FILENAME = "configuration/initial/akka.conf";
134     private final File file;
135     private final String resourceFilename;
136     private final String fileContent;
137     private ClusterConfig cluserConfig;
138
139     public ClusterConfig getClusterConfig() {
140         return this.cluserConfig;
141     }
142
143     private AkkaConfig(File file, boolean isResource) {
144         this.file = isResource ? null : file;
145         this.fileContent = null;
146         this.resourceFilename = isResource ? file.getName() : null;
147     }
148
149     private AkkaConfig(String fileContent) {
150         this.file = null;
151         this.fileContent = fileContent;
152         this.resourceFilename = null;
153     }
154
155
156     @Override
157     public String toString() {
158         return "AkkaConfig [filename=" + file + ", cluserConfig=" + cluserConfig + "]";
159     }
160
161     private void loadFromFile() throws Exception {
162         Config cfg = null;
163         if (this.file != null) {
164             cfg = ConfigFactory.parseFile(this.file);
165         } else if (this.fileContent != null) {
166             cfg = ConfigFactory.parseString(this.fileContent);
167         } else if (this.resourceFilename != null) {
168             cfg = ConfigFactory.parseResources(this.getClass(), this.resourceFilename);
169         }
170
171         if (cfg != null) {
172             this.cluserConfig =
173                     new ClusterConfig(cfg.getConfig("odl-cluster-data").getConfig("akka").getConfig("cluster"));
174         } else {
175             LOG.warn("unable to parse config file");
176             this.cluserConfig = null;
177         }
178     }
179
180     public boolean isCluster() {
181         return this.cluserConfig != null ? this.cluserConfig.isCluster() : false;
182     }
183
184     public static AkkaConfig load() throws Exception {
185         return load(DEFAULT_FILENAME);
186     }
187
188     public static AkkaConfig load(String filename) throws Exception {
189         return load(filename, false);
190     }
191
192     public static AkkaConfig load(String filename, boolean isResource) throws Exception {
193         AkkaConfig cfg = new AkkaConfig(new File(filename), isResource);
194         cfg.loadFromFile();
195
196         return cfg;
197     }
198
199     public static AkkaConfig loadContent(String content) throws Exception {
200         AkkaConfig cfg = new AkkaConfig(content);
201         cfg.loadFromFile();
202
203         return cfg;
204     }
205
206
207
208 }