update websocketmanager
[ccsdk/features.git] / sdnr / wt / websocketmanager / provider / src / main / java / org / onap / ccsdk / features / sdnr / wt / websocketmanager / utils / AkkaConfig.java
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.websocketmanager.utils;
19
20 import com.typesafe.config.Config;
21 import com.typesafe.config.ConfigFactory;
22 import java.io.File;
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.regex.Matcher;
26 import java.util.regex.Pattern;
27 import org.onap.ccsdk.features.sdnr.wt.websocketmanager.WebSocketManager;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 public class AkkaConfig {
32
33     private static final Logger LOG = LoggerFactory.getLogger(WebSocketManager.class.getName());
34
35     public static class ClusterNodeInfo {
36         @Override
37         public String toString() {
38             return "ClusterNodeInfo [protocol=" + protocol + ", clusterName=" + clusterName + ", remoteAdr=" + remoteAdr
39                     + ", port=" + port + "]";
40         }
41
42         private final String protocol;
43         private final String clusterName;
44         private final String remoteAdr;
45         private final int port;
46
47         public String getRemoteAddress() {
48             return this.remoteAdr;
49         }
50
51         public ClusterNodeInfo(String s) throws Exception {
52             final String regex = "([a-z.]*):\\/\\/([a-zA-Z0-9-]*)@([a-zA-Z0-9.-]*):([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 seedNode format");
57             }
58             this.protocol = matcher.group(1);
59             this.clusterName = matcher.group(2);
60             this.remoteAdr = matcher.group(3);
61             this.port = Integer.parseInt(matcher.group(4));
62         }
63     }
64     public static class ClusterRoleInfo {
65         @Override
66         public String toString() {
67             return "ClusterRoleInfo [Role=" + Role + ", Index=" + Index + "]";
68         }
69
70         private final String Role;
71         private final int Index;
72
73         public ClusterRoleInfo(String s) throws Exception {
74             final String regex = "([a-z]*)-([0-9]*)";
75             final Pattern pattern = Pattern.compile(regex);
76             final Matcher matcher = pattern.matcher(s);
77             if (!matcher.find()) {
78                 throw new Exception("invalid role format");
79             }
80             this.Role = matcher.group(1);
81             this.Index = Integer.parseInt(matcher.group(2));
82         }
83
84     }
85     public static class ClusterConfig {
86         @Override
87         public String toString() {
88             return "ClusterConfig [seedNodes=" + seedNodes + ", roles=" + roles + "]";
89         }
90
91         private final List<ClusterNodeInfo> seedNodes;
92         private final List<ClusterRoleInfo> roles;
93         private final ClusterNodeInfo ismeInfo;
94
95         public ClusterConfig(Config o) throws Exception {
96             {
97                 this.seedNodes = new ArrayList<>();
98                 List<String> a = o.getStringList("seed-nodes");
99                 for (int i = 0; i < a.size(); i++) {
100                     ClusterNodeInfo info = new ClusterNodeInfo(a.get(i));
101                     this.seedNodes.add(info);
102                 }
103                 this.roles = new ArrayList<>();
104                 a = o.getStringList("roles");
105                 for (int i = 0; i < a.size(); i++) {
106                     ClusterRoleInfo s = new ClusterRoleInfo(a.get(i));
107                     this.roles.add(s);
108                 }
109                 int idx = this.roles.get(0).Index - 1;
110                 if (idx >= 0 && idx < this.seedNodes.size()) {
111                     this.ismeInfo = this.seedNodes.get(idx);
112                 } else {
113                     this.ismeInfo = null;
114                 }
115             }
116
117         }
118
119         public boolean isCluster() {
120             return this.seedNodes != null ? this.seedNodes.size() > 1 : false;
121         }
122
123         public boolean isMe(ClusterNodeInfo i) {
124             return this.ismeInfo != null ? this.ismeInfo.equals(i) : false;
125         }
126
127         public List<ClusterNodeInfo> getSeedNodes() {
128             return this.seedNodes;
129         }
130     }
131
132     private static final String DEFAULT_FILENAME = "configuration/initial/akka.conf";
133     private final File file;
134     private final String resourceFilename;
135     private final String fileContent;
136     private ClusterConfig cluserConfig;
137
138     public ClusterConfig getClusterConfig() {
139         return this.cluserConfig;
140     }
141
142     private AkkaConfig(File file, boolean isResource) {
143         this.file = isResource ? null : file;
144         this.fileContent = null;
145         this.resourceFilename = isResource ? file.getName() : null;
146     }
147
148     private AkkaConfig(String fileContent) {
149         this.file = null;
150         this.fileContent = fileContent;
151         this.resourceFilename = null;
152     }
153
154
155     @Override
156     public String toString() {
157         return "AkkaConfig [filename=" + file + ", cluserConfig=" + cluserConfig + "]";
158     }
159
160     private void loadFromFile() throws Exception {
161         Config cfg = null;
162         if (this.file != null) {
163             cfg = ConfigFactory.parseFile(this.file);
164         } else if (this.fileContent != null) {
165             cfg = ConfigFactory.parseString(this.fileContent);
166         } else if (this.resourceFilename != null) {
167             cfg = ConfigFactory.parseResources(this.getClass(), this.resourceFilename);
168         }
169
170         if (cfg != null) {
171             this.cluserConfig =
172                     new ClusterConfig(cfg.getConfig("odl-cluster-data").getConfig("akka").getConfig("cluster"));
173         } else {
174             LOG.warn("unable to parse config file");
175             this.cluserConfig = null;
176         }
177     }
178
179     public boolean isCluster() {
180         return this.cluserConfig != null ? this.cluserConfig.isCluster() : false;
181     }
182
183     public static AkkaConfig load() throws Exception {
184         return load(DEFAULT_FILENAME);
185     }
186
187     public static AkkaConfig load(String filename) throws Exception {
188         return load(filename, false);
189     }
190
191     public static AkkaConfig load(String filename, boolean isResource) throws Exception {
192         AkkaConfig cfg = new AkkaConfig(new File(filename), isResource);
193         cfg.loadFromFile();
194
195         return cfg;
196     }
197
198     public static AkkaConfig loadContent(String content) throws Exception {
199         AkkaConfig cfg = new AkkaConfig(content);
200         cfg.loadFromFile();
201
202         return cfg;
203     }
204
205
206
207 }