7d3174bce146391ca035a27b7a2e2a1464ffe113
[ccsdk/features.git] /
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
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
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
15  * the License.
16  * ============LICENSE_END==========================================================================
17  ******************************************************************************/
18 package org.onap.ccsdk.features.sdnr.wt.devicemanager.config.util;
19
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;
25
26 public class ClusterConfig {
27
28     private static final Logger LOG = LoggerFactory.getLogger(ClusterConfig.class);
29
30     private final List<ClusterNodeInfo> seedNodes;
31     private final ClusterRoleInfoCollection roles;
32     private ClusterNodeInfo ismeInfo;
33
34     public static ClusterConfig defaultSingleNodeConfig()
35     {
36         ClusterConfig cfg=new ClusterConfig();
37         cfg.ismeInfo=ClusterNodeInfo.defaultSingleNodeInfo();
38         cfg.seedNodes.add(cfg.ismeInfo);
39         cfg.roles.add(ClusterRoleInfo.defaultSingleNodeRole());
40         return cfg;
41     }
42     public ClusterConfig()
43     {
44         this.seedNodes = new ArrayList<>();
45         this.roles = new ClusterRoleInfoCollection();
46
47     }
48     public ClusterConfig(Config o) throws Exception {
49         {
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);
56             }
57             a = o.getStringList("roles");
58             for (int i = 0; i < a.size(); i++) {
59                 ClusterRoleInfo s = new ClusterRoleInfo(a.get(i));
60                 this.roles.add(s);
61             }
62             int idx = this.roles.get(0).getIndex() - 1;
63             if (idx >= 0 && idx < this.seedNodes.size()) {
64                 this.ismeInfo = this.seedNodes.get(idx);
65             } else {
66                 this.ismeInfo = null;
67             }
68         }
69
70     }
71
72     public boolean isCluster() {
73         return this.seedNodes != null ? this.seedNodes.size() > 1 : false;
74     }
75
76     public boolean isMe(ClusterNodeInfo i) {
77         return this.ismeInfo != null ? this.ismeInfo.equals(i) : false;
78     }
79
80     public List<ClusterNodeInfo> getSeedNodes() {
81         return this.seedNodes;
82     }
83
84     public String getHostName(String defaultValue) {
85         if (getRoleMemberIndex() > 0 && getRoleMemberIndex() <= seedNodes.size()) {
86             return this.seedNodes.get(getRoleMemberIndex()-1).getRemoteAddress();
87         } else {
88             LOG.warn("Seednode not available for roleMemberIndex {}. Using default {}",getRoleMember(), defaultValue);
89             return defaultValue;
90         }
91     }
92
93     public String getDBClusterName(String defaultValue) {
94         String r = null;
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());
97         }
98         if (r == null || r.isEmpty()) {
99             r = defaultValue;
100         }
101         return r;
102     }
103     public String getClusterSeedNodeName() {
104         return this.getClusterSeedNodeName("");
105     }
106     public String getClusterSeedNodeName(String defaultValue) {
107         int idx=this.getRoleMemberIndex()-1;
108         String r=null;
109         if(this.seedNodes!=null && idx>=0 && this.seedNodes.size()>0 && this.seedNodes.size()>idx)
110         {
111             r=this.seedNodes.get(idx).getSeedNodeName();
112         }
113         if (r == null || r.isEmpty()) {
114             r = defaultValue;
115         }
116         return r;
117     }
118     public int getRoleMemberIndex() {
119
120         ClusterRoleInfo role=this.roles.get("member");
121         return role!=null?role.getIndex():0;
122     }
123     public ClusterRoleInfo getRoleMember() {
124         return this.roles.get("member");
125     }
126
127     @Override
128     public String toString() {
129         return "ClusterConfig [seedNodes=" + seedNodes + ", roles=" + roles + ", ismeInfo=" + ismeInfo + "]";
130     }
131
132
133 }