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.devicemanager.config.util;
20 import com.typesafe.config.Config;
21 import java.util.ArrayList;
22 import java.util.List;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
26 public class ClusterConfig {
28 private static final Logger LOG = LoggerFactory.getLogger(ClusterConfig.class);
30 private final List<ClusterNodeInfo> seedNodes;
31 private final ClusterRoleInfoCollection roles;
32 private ClusterNodeInfo ismeInfo;
34 public static ClusterConfig defaultSingleNodeConfig()
36 ClusterConfig cfg=new ClusterConfig();
37 cfg.ismeInfo=ClusterNodeInfo.defaultSingleNodeInfo();
38 cfg.seedNodes.add(cfg.ismeInfo);
39 cfg.roles.add(ClusterRoleInfo.defaultSingleNodeRole());
42 public ClusterConfig()
44 this.seedNodes = new ArrayList<>();
45 this.roles = new ClusterRoleInfoCollection();
48 public ClusterConfig(Config o) throws Exception {
50 this.seedNodes = new ArrayList<>();
51 this.roles = new ClusterRoleInfoCollection();
52 List<String> a = o.getStringList("seed-nodes");
53 for (int i = 0; i < a.size(); i++) {
54 ClusterNodeInfo info = new ClusterNodeInfo(a.get(i));
55 this.seedNodes.add(info);
57 a = o.getStringList("roles");
58 for (int i = 0; i < a.size(); i++) {
59 ClusterRoleInfo s = new ClusterRoleInfo(a.get(i));
62 int idx = this.roles.get(0).getIndex() - 1;
63 if (idx >= 0 && idx < this.seedNodes.size()) {
64 this.ismeInfo = this.seedNodes.get(idx);
72 public boolean isCluster() {
73 return this.seedNodes != null ? this.seedNodes.size() > 1 : false;
76 public boolean isMe(ClusterNodeInfo i) {
77 return this.ismeInfo != null ? this.ismeInfo.equals(i) : false;
80 public List<ClusterNodeInfo> getSeedNodes() {
81 return this.seedNodes;
84 public String getHostName(String defaultValue) {
85 if (getRoleMemberIndex() > 0 && getRoleMemberIndex() <= seedNodes.size()) {
86 return this.seedNodes.get(getRoleMemberIndex()-1).getRemoteAddress();
88 LOG.warn("Seednode not available for roleMemberIndex {}. Using default {}",getRoleMember(), defaultValue);
93 public String getDBClusterName(String defaultValue) {
95 if (this.seedNodes != null && this.seedNodes.size() > 0) {
96 r = String.format("cluster-%s.%d", this.seedNodes.get(0).getRemoteAddress(), this.seedNodes.get(0).getPort());
98 if (r == null || r.isEmpty()) {
103 public String getClusterSeedNodeName() {
104 return this.getClusterSeedNodeName("");
106 public String getClusterSeedNodeName(String defaultValue) {
107 int idx=this.getRoleMemberIndex()-1;
109 if(this.seedNodes!=null && idx>=0 && this.seedNodes.size()>0 && this.seedNodes.size()>idx)
111 r=this.seedNodes.get(idx).getSeedNodeName();
113 if (r == null || r.isEmpty()) {
118 public int getRoleMemberIndex() {
120 ClusterRoleInfo role=this.roles.get("member");
121 return role!=null?role.getIndex():0;
123 public ClusterRoleInfo getRoleMember() {
124 return this.roles.get("member");
128 public String toString() {
129 return "ClusterConfig [seedNodes=" + seedNodes + ", roles=" + roles + ", ismeInfo=" + ismeInfo + "]";