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.websocketmanager.utils;
20 import com.typesafe.config.Config;
21 import com.typesafe.config.ConfigFactory;
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;
31 public class AkkaConfig {
33 private static final Logger LOG = LoggerFactory.getLogger(WebSocketManager.class.getName());
35 public static class ClusterNodeInfo {
37 public String toString() {
38 return "ClusterNodeInfo [protocol=" + protocol + ", clusterName=" + clusterName + ", remoteAdr=" + remoteAdr
39 + ", port=" + port + "]";
42 private final String protocol;
43 private final String clusterName;
44 private final String remoteAdr;
45 private final int port;
47 public String getRemoteAddress() {
48 return this.remoteAdr;
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");
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));
64 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;
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");
80 this.Role = matcher.group(1);
81 this.Index = Integer.parseInt(matcher.group(2));
85 public static class ClusterConfig {
87 public String toString() {
88 return "ClusterConfig [seedNodes=" + seedNodes + ", roles=" + roles + "]";
91 private final List<ClusterNodeInfo> seedNodes;
92 private final List<ClusterRoleInfo> roles;
93 private final ClusterNodeInfo ismeInfo;
95 public ClusterConfig(Config o) throws Exception {
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);
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));
109 int idx = this.roles.get(0).Index - 1;
110 if (idx >= 0 && idx < this.seedNodes.size()) {
111 this.ismeInfo = this.seedNodes.get(idx);
113 this.ismeInfo = null;
119 public boolean isCluster() {
120 return this.seedNodes != null ? this.seedNodes.size() > 1 : false;
123 public boolean isMe(ClusterNodeInfo i) {
124 return this.ismeInfo != null ? this.ismeInfo.equals(i) : false;
127 public List<ClusterNodeInfo> getSeedNodes() {
128 return this.seedNodes;
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;
138 public ClusterConfig getClusterConfig() {
139 return this.cluserConfig;
142 private AkkaConfig(File file, boolean isResource) {
143 this.file = isResource ? null : file;
144 this.fileContent = null;
145 this.resourceFilename = isResource ? file.getName() : null;
148 private AkkaConfig(String fileContent) {
150 this.fileContent = fileContent;
151 this.resourceFilename = null;
156 public String toString() {
157 return "AkkaConfig [filename=" + file + ", cluserConfig=" + cluserConfig + "]";
160 private void loadFromFile() throws Exception {
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);
172 new ClusterConfig(cfg.getConfig("odl-cluster-data").getConfig("akka").getConfig("cluster"));
174 LOG.warn("unable to parse config file");
175 this.cluserConfig = null;
179 public boolean isCluster() {
180 return this.cluserConfig != null ? this.cluserConfig.isCluster() : false;
183 public static AkkaConfig load() throws Exception {
184 return load(DEFAULT_FILENAME);
187 public static AkkaConfig load(String filename) throws Exception {
188 return load(filename, false);
191 public static AkkaConfig load(String filename, boolean isResource) throws Exception {
192 AkkaConfig cfg = new AkkaConfig(new File(filename), isResource);
198 public static AkkaConfig loadContent(String content) throws Exception {
199 AkkaConfig cfg = new AkkaConfig(content);