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.impl.conf.odlAkka;
20 import java.util.ArrayList;
21 import java.util.List;
23 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.conf.odlGeo.ClusterRoleInfo;
24 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.conf.odlGeo.ClusterRoleInfoCollection;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 import com.typesafe.config.Config;
30 public class ClusterConfig {
32 private static final Logger LOG = LoggerFactory.getLogger(ClusterConfig.class);
34 private final List<ClusterNodeInfo> seedNodes;
35 private final ClusterRoleInfoCollection roles;
36 private ClusterNodeInfo ismeInfo;
38 public static ClusterConfig defaultSingleNodeConfig()
40 ClusterConfig cfg=new ClusterConfig();
41 cfg.ismeInfo=ClusterNodeInfo.defaultSingleNodeInfo();
42 cfg.seedNodes.add(cfg.ismeInfo);
43 cfg.roles.add(ClusterRoleInfo.defaultSingleNodeRole());
46 public ClusterConfig()
48 this.seedNodes = new ArrayList<>();
49 this.roles = new ClusterRoleInfoCollection();
52 public ClusterConfig(Config o) throws Exception {
54 this.seedNodes = new ArrayList<>();
55 this.roles = new ClusterRoleInfoCollection();
56 List<String> a = o.getStringList("seed-nodes");
57 for (int i = 0; i < a.size(); i++) {
58 ClusterNodeInfo info = new ClusterNodeInfo(a.get(i));
59 this.seedNodes.add(info);
61 a = o.getStringList("roles");
62 for (int i = 0; i < a.size(); i++) {
63 ClusterRoleInfo s = new ClusterRoleInfo(a.get(i));
66 int idx = this.roles.get(0).getIndex() - 1;
67 if (idx >= 0 && idx < this.seedNodes.size()) {
68 this.ismeInfo = this.seedNodes.get(idx);
76 public boolean isCluster() {
77 return this.seedNodes != null ? this.seedNodes.size() > 1 : false;
80 public boolean isMe(ClusterNodeInfo i) {
81 return this.ismeInfo != null ? this.ismeInfo.equals(i) : false;
84 public List<ClusterNodeInfo> getSeedNodes() {
85 return this.seedNodes;
88 public String getHostName(String defaultValue) {
89 if (getRoleMemberIndex() > 0 && getRoleMemberIndex() <= seedNodes.size()) {
90 return this.seedNodes.get(getRoleMemberIndex()-1).getRemoteAddress();
92 LOG.warn("Seednode not available for roleMemberIndex {}. Using default {}",getRoleMember(), defaultValue);
97 public String getDBClusterName(String defaultValue) {
99 if (this.seedNodes != null && this.seedNodes.size() > 0) {
100 r = String.format("cluster-%s.%d", this.seedNodes.get(0).getRemoteAddress(), this.seedNodes.get(0).getPort());
102 if (r == null || r.isEmpty()) {
107 public String getClusterSeedNodeName() {
108 return this.getClusterSeedNodeName("");
110 public String getClusterSeedNodeName(String defaultValue) {
111 int idx=this.getRoleMemberIndex()-1;
113 if(this.seedNodes!=null && idx>=0 && this.seedNodes.size()>0 && this.seedNodes.size()>idx)
115 r=this.seedNodes.get(idx).getSeedNodeName();
117 if (r == null || r.isEmpty()) {
122 public int getRoleMemberIndex() {
124 ClusterRoleInfo role=this.roles.get("member");
125 return role!=null?role.getIndex():0;
127 public ClusterRoleInfo getRoleMember() {
128 return this.roles.get("member");
132 public String toString() {
133 return "ClusterConfig [seedNodes=" + seedNodes + ", roles=" + roles + ", ismeInfo=" + ismeInfo + "]";