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==========================================================================
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 {
38 public String toString() {
39 return "ClusterNodeInfo [protocol=" + protocol + ", clusterName=" + clusterName + ", remoteAdr=" + remoteAdr
40 + ", 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() {
49 return this.remoteAdr;
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");
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));
65 public static class ClusterRoleInfo {
67 public String toString() {
68 return "ClusterRoleInfo [Role=" + Role + ", Index=" + Index + "]";
71 private final String Role;
72 private final int Index;
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");
81 this.Role = matcher.group(1);
82 this.Index = Integer.parseInt(matcher.group(2));
86 public static class ClusterConfig {
88 public String toString() {
89 return "ClusterConfig [seedNodes=" + seedNodes + ", roles=" + roles + "]";
92 private final List<ClusterNodeInfo> seedNodes;
93 private final List<ClusterRoleInfo> roles;
94 private final ClusterNodeInfo ismeInfo;
96 public ClusterConfig(Config o) throws Exception {
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);
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));
110 int idx = this.roles.get(0).Index - 1;
111 if (idx >= 0 && idx < this.seedNodes.size()) {
112 this.ismeInfo = this.seedNodes.get(idx);
114 this.ismeInfo = null;
120 public boolean isCluster() {
121 return this.seedNodes != null ? this.seedNodes.size() > 1 : false;
124 public boolean isMe(ClusterNodeInfo i) {
125 return this.ismeInfo != null ? this.ismeInfo.equals(i) : false;
128 public List<ClusterNodeInfo> getSeedNodes() {
129 return this.seedNodes;
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;
139 public ClusterConfig getClusterConfig() {
140 return this.cluserConfig;
143 private AkkaConfig(File file, boolean isResource) {
144 this.file = isResource ? null : file;
145 this.fileContent = null;
146 this.resourceFilename = isResource ? file.getName() : null;
149 private AkkaConfig(String fileContent) {
151 this.fileContent = fileContent;
152 this.resourceFilename = null;
157 public String toString() {
158 return "AkkaConfig [filename=" + file + ", cluserConfig=" + cluserConfig + "]";
161 private void loadFromFile() throws Exception {
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);
173 new ClusterConfig(cfg.getConfig("odl-cluster-data").getConfig("akka").getConfig("cluster"));
175 LOG.warn("unable to parse config file");
176 this.cluserConfig = null;
180 public boolean isCluster() {
181 return this.cluserConfig != null ? this.cluserConfig.isCluster() : false;
184 public static AkkaConfig load() throws Exception {
185 return load(DEFAULT_FILENAME);
188 public static AkkaConfig load(String filename) throws Exception {
189 return load(filename, false);
192 public static AkkaConfig load(String filename, boolean isResource) throws Exception {
193 AkkaConfig cfg = new AkkaConfig(new File(filename), isResource);
199 public static AkkaConfig loadContent(String content) throws Exception {
200 AkkaConfig cfg = new AkkaConfig(content);