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
10 * http://www.apache.org/licenses/LICENSE-2.0
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
16 * ============LICENSE_END==========================================================================
17 ******************************************************************************/
18 package org.onap.ccsdk.features.sdnr.wt.websocketmanager2.utils;
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;
29 import com.typesafe.config.Config;
30 import com.typesafe.config.ConfigFactory;
32 public class AkkaConfig{
34 private static final Logger LOG = LoggerFactory.getLogger(WebSocketManager.class.getName());
36 public static class ClusterNodeInfo
39 public String toString() {
40 return "ClusterNodeInfo [protocol=" + protocol + ", clusterName=" + clusterName + ", remoteAdr=" + remoteAdr
41 + ", port=" + port + "]";
43 private final String protocol;
44 private final String clusterName;
45 private final String remoteAdr;
46 private final int port;
48 public String getRemoteAddress() {return this.remoteAdr;}
49 public ClusterNodeInfo(String s) throws Exception
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);
55 throw new Exception("invalid seedNode format");
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));
63 public static class ClusterRoleInfo
66 public String toString() {
67 return "ClusterRoleInfo [Role=" + Role + ", Index=" + Index + "]";
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);
77 throw new Exception("invalid role format");
79 this.Role=matcher.group(1);
80 this.Index=Integer.parseInt(matcher.group(2));
84 public static class ClusterConfig
87 public String toString() {
88 return "ClusterConfig [seedNodes=" + seedNodes + ", roles=" + roles + "]";
90 private final List<ClusterNodeInfo> seedNodes;
91 private final List<ClusterRoleInfo> roles;
92 private final ClusterNodeInfo ismeInfo;
93 public ClusterConfig(Config o) throws Exception {
95 this.seedNodes = new ArrayList<>();
96 List<String> a= o.getStringList("seed-nodes");
97 for(int i=0;i<a.size();i++)
99 ClusterNodeInfo info=new ClusterNodeInfo(a.get(i));
100 this.seedNodes.add(info);
102 this.roles=new ArrayList<>();
103 a=o.getStringList("roles");
104 for(int i=0;i<a.size();i++)
106 ClusterRoleInfo s=new ClusterRoleInfo(a.get(i));
109 int idx=this.roles.get(0).Index-1;
110 if(idx>=0 && idx<this.seedNodes.size()) {
111 this.ismeInfo=this.seedNodes.get(idx);
118 public boolean isCluster() {
119 return this.seedNodes!=null?this.seedNodes.size()>1:false;
121 public boolean isMe(ClusterNodeInfo i) {
122 return this.ismeInfo!=null?this.ismeInfo.equals(i):false;
124 public List<ClusterNodeInfo> getSeedNodes() {
125 return this.seedNodes;
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;}
136 private AkkaConfig(File file,boolean isResource) {
137 this.file=isResource?null:file;
138 this.fileContent=null;
139 this.resourceFilename=isResource?file.getName():null;
141 private AkkaConfig(String fileContent) {
143 this.fileContent = fileContent;
144 this.resourceFilename = null;
149 public String toString() {
150 return "AkkaConfig [filename=" + file + ", cluserConfig=" + cluserConfig + "]";
153 private void loadFromFile() throws Exception {
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);
164 this.cluserConfig=new ClusterConfig(cfg.getConfig("odl-cluster-data").getConfig("akka").getConfig("cluster"));
167 LOG.warn("unable to parse config file");
168 this.cluserConfig=null;
172 public boolean isCluster()
174 return this.cluserConfig!=null?this.cluserConfig.isCluster():false;
176 public static AkkaConfig load() throws Exception
178 return load(DEFAULT_FILENAME);
181 public static AkkaConfig load(String filename) throws Exception
183 return load(filename,false);
185 public static AkkaConfig load(String filename,boolean isResource) throws Exception
187 AkkaConfig cfg=new AkkaConfig(new File(filename),isResource);
192 public static AkkaConfig loadContent(String content) throws Exception
194 AkkaConfig cfg=new AkkaConfig(content);