4381595a728e945c1369bc43c3e5cbdcc314f1e5
[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     {
38         @Override
39         public String toString() {
40             return "ClusterNodeInfo [protocol=" + protocol + ", clusterName=" + clusterName + ", remoteAdr=" + remoteAdr
41                     + ", port=" + port + "]";
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() {return this.remoteAdr;}
49         public ClusterNodeInfo(String s) throws Exception
50         {
51             final String regex ="([a-z.]*):\\/\\/([a-zA-Z0-9-]*)@([a-zA-Z0-9.-]*):([0-9]*)";
52             final Pattern pattern = Pattern.compile(regex);
53             final Matcher matcher = pattern.matcher(s);
54             if(!matcher.find()) {
55                 throw new Exception("invalid seedNode format");
56             }
57             this.protocol=matcher.group(1);
58             this.clusterName=matcher.group(2);
59             this.remoteAdr=matcher.group(3);
60             this.port=Integer.parseInt(matcher.group(4));
61         }
62     }
63     public static class ClusterRoleInfo
64     {
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         public ClusterRoleInfo(String s) throws Exception {
73             final String regex = "([a-z]*)-([0-9]*)";
74             final Pattern pattern = Pattern.compile(regex);
75             final Matcher matcher = pattern.matcher(s);
76             if(!matcher.find()) {
77                 throw new Exception("invalid role format");
78             }
79             this.Role=matcher.group(1);
80             this.Index=Integer.parseInt(matcher.group(2));
81         }
82
83     }
84     public static class ClusterConfig
85     {
86         @Override
87         public String toString() {
88             return "ClusterConfig [seedNodes=" + seedNodes + ", roles=" + roles + "]";
89         }
90         private final List<ClusterNodeInfo> seedNodes;
91         private final List<ClusterRoleInfo> roles;
92         private final ClusterNodeInfo ismeInfo;
93         public ClusterConfig(Config o) throws Exception {
94             {
95             this.seedNodes = new ArrayList<>();
96             List<String> a= o.getStringList("seed-nodes");
97             for(int i=0;i<a.size();i++)
98             {
99                 ClusterNodeInfo info=new ClusterNodeInfo(a.get(i));
100                 this.seedNodes.add(info);
101             }
102             this.roles=new ArrayList<>();
103             a=o.getStringList("roles");
104             for(int i=0;i<a.size();i++)
105             {
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         public boolean isCluster() {
119             return this.seedNodes!=null?this.seedNodes.size()>1:false;
120         }
121         public boolean isMe(ClusterNodeInfo i) {
122             return this.ismeInfo!=null?this.ismeInfo.equals(i):false;
123         }
124         public List<ClusterNodeInfo> getSeedNodes() {
125             return this.seedNodes;
126         }
127     }
128
129     private static final String DEFAULT_FILENAME = "configuration/initial/akka.conf";
130     private final File file;
131     private final String resourceFilename;
132     private final String fileContent;
133     private ClusterConfig cluserConfig;
134     public ClusterConfig getClusterConfig() {return this.cluserConfig;}
135
136     private AkkaConfig(File file,boolean isResource) {
137         this.file=isResource?null:file;
138         this.fileContent=null;
139         this.resourceFilename=isResource?file.getName():null;
140     }
141     private AkkaConfig(String fileContent) {
142         this.file = null;
143         this.fileContent = fileContent;
144         this.resourceFilename = null;
145     }
146
147
148     @Override
149     public String toString() {
150         return "AkkaConfig [filename=" + file + ", cluserConfig=" + cluserConfig + "]";
151     }
152
153     private void loadFromFile() throws Exception {
154         Config cfg=null;
155         if(this.file!=null) {
156             cfg=ConfigFactory.parseFile(this.file);
157         } else if(this.fileContent!=null) {
158             cfg=ConfigFactory.parseString(this.fileContent);
159         } else if(this.resourceFilename!=null) {
160             cfg=ConfigFactory.parseResources(this.getClass(), this.resourceFilename);
161         }
162
163         if(cfg!=null) {
164             this.cluserConfig=new ClusterConfig(cfg.getConfig("odl-cluster-data").getConfig("akka").getConfig("cluster"));
165         } else
166         {
167             LOG.warn("unable to parse config file");
168             this.cluserConfig=null;
169         }
170     }
171
172     public boolean isCluster()
173     {
174         return this.cluserConfig!=null?this.cluserConfig.isCluster():false;
175     }
176     public static AkkaConfig load() throws Exception
177     {
178         return load(DEFAULT_FILENAME);
179     }
180
181     public static AkkaConfig load(String filename) throws Exception
182     {
183         return load(filename,false);
184     }
185     public static AkkaConfig load(String filename,boolean isResource) throws Exception
186     {
187         AkkaConfig cfg=new AkkaConfig(new File(filename),isResource);
188         cfg.loadFromFile();
189
190         return cfg;
191     }
192     public static AkkaConfig loadContent(String content) throws Exception
193     {
194         AkkaConfig cfg=new AkkaConfig(content);
195         cfg.loadFromFile();
196
197         return cfg;
198     }
199
200
201
202
203
204
205
206 }