bdde30828ac7ee8329e4f2e237ecb08bb188cd29
[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.netconfnodestateservice.impl.conf.odlAkka;
19
20 import java.util.ArrayList;
21 import java.util.List;
22
23 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.impl.conf.odlGeo.ClusterRoleInfo;
24 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.impl.conf.odlGeo.ClusterRoleInfoCollection;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 import com.typesafe.config.Config;
29
30 public class ClusterConfig {
31
32     private static final Logger LOG = LoggerFactory.getLogger(ClusterConfig.class);
33
34     private final List<ClusterNodeInfo> seedNodes;
35     private final ClusterRoleInfoCollection roles;
36     private ClusterNodeInfo ismeInfo;
37
38     public static ClusterConfig defaultSingleNodeConfig()
39     {
40         ClusterConfig cfg=new ClusterConfig();
41         cfg.ismeInfo=ClusterNodeInfo.defaultSingleNodeInfo();
42         cfg.seedNodes.add(cfg.ismeInfo);
43         cfg.roles.add(ClusterRoleInfo.defaultSingleNodeRole());
44         return cfg;
45     }
46     public ClusterConfig()
47     {
48         this.seedNodes = new ArrayList<>();
49         this.roles = new ClusterRoleInfoCollection();
50
51     }
52     public ClusterConfig(Config o) throws Exception {
53         {
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);
60             }
61             a = o.getStringList("roles");
62             for (int i = 0; i < a.size(); i++) {
63                 ClusterRoleInfo s = new ClusterRoleInfo(a.get(i));
64                 this.roles.add(s);
65             }
66             int idx = this.roles.get(0).getIndex() - 1;
67             if (idx >= 0 && idx < this.seedNodes.size()) {
68                 this.ismeInfo = this.seedNodes.get(idx);
69             } else {
70                 this.ismeInfo = null;
71             }
72         }
73
74     }
75
76     public boolean isCluster() {
77         return this.seedNodes != null ? this.seedNodes.size() > 1 : false;
78     }
79
80     public boolean isMe(ClusterNodeInfo i) {
81         return this.ismeInfo != null ? this.ismeInfo.equals(i) : false;
82     }
83
84     public List<ClusterNodeInfo> getSeedNodes() {
85         return this.seedNodes;
86     }
87
88     public String getHostName(String defaultValue) {
89         if (getRoleMemberIndex() > 0 && getRoleMemberIndex() <= seedNodes.size()) {
90             return this.seedNodes.get(getRoleMemberIndex()-1).getRemoteAddress();
91         } else {
92             LOG.warn("Seednode not available for roleMemberIndex {}. Using default {}",getRoleMember(), defaultValue);
93             return defaultValue;
94         }
95     }
96
97     public String getDBClusterName(String defaultValue) {
98         String r = null;
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());
101         }
102         if (r == null || r.isEmpty()) {
103             r = defaultValue;
104         }
105         return r;
106     }
107     public String getClusterSeedNodeName() {
108         return this.getClusterSeedNodeName("");
109     }
110     public String getClusterSeedNodeName(String defaultValue) {
111         int idx=this.getRoleMemberIndex()-1;
112         String r=null;
113         if(this.seedNodes!=null && idx>=0 && this.seedNodes.size()>0 && this.seedNodes.size()>idx)
114         {
115             r=this.seedNodes.get(idx).getSeedNodeName();
116         }
117         if (r == null || r.isEmpty()) {
118             r = defaultValue;
119         }
120         return r;
121     }
122     public int getRoleMemberIndex() {
123
124         ClusterRoleInfo role=this.roles.get("member");
125         return role!=null?role.getIndex():0;
126     }
127     public ClusterRoleInfo getRoleMember() {
128         return this.roles.get("member");
129     }
130
131     @Override
132     public String toString() {
133         return "ClusterConfig [seedNodes=" + seedNodes + ", roles=" + roles + ", ismeInfo=" + ismeInfo + "]";
134     }
135
136
137 }