ac9c3cb50eda0b6cffabada84c3434fdd28734a8
[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.impl;
19
20 import com.typesafe.config.Config;
21 import com.typesafe.config.ConfigFactory;
22 import java.io.File;
23 import java.util.ArrayList;
24 import java.util.List;
25 import org.onap.ccsdk.features.sdnr.wt.devicemanager.config.util.ClusterRoleInfo;
26 import org.onap.ccsdk.features.sdnr.wt.devicemanager.config.util.ClusterRoleInfoCollection;
27
28 public class GeoConfig {
29
30     private static final String DEFAULT_FILENAME = "configuration/initial/geo.conf";
31     private static final String LUMINA_ROOTNODENAME = "lumina-geo-cluster";
32     private final String filename;
33     private final String rootNodename;
34     private ClusterRoleInfoCollection primaryRoles;
35     private ClusterRoleInfoCollection secondayRoles;
36     private RolesTable rolesTable;
37
38     private GeoConfig() {
39         this(null);
40     }
41
42     private GeoConfig(String filename) {
43         this(filename, LUMINA_ROOTNODENAME);
44     }
45
46     private GeoConfig(String filename, String rootNodeName) {
47         this.filename = filename;
48         this.rootNodename = rootNodeName;
49     }
50
51     public static boolean fileExists() {
52         File f = new File(DEFAULT_FILENAME);
53         return f.exists();
54     }
55
56     public static GeoConfig load() throws Exception {
57         return load(DEFAULT_FILENAME);
58     }
59
60     public static GeoConfig load(String filename) throws Exception {
61         GeoConfig cfg = new GeoConfig(filename);
62         cfg._load();
63         return cfg;
64     }
65
66     private void _load() throws Exception {
67         this._load(ConfigFactory.parseFile(new File(this.filename)));
68     }
69
70     private void _load(Config cfg) throws Exception {
71         this.primaryRoles = new ClusterRoleInfoCollection();
72         List<String> a = cfg.getConfig(this.rootNodename).getStringList("primary_roles");
73
74         for (int i = 0; i < a.size(); i++) {
75             ClusterRoleInfo s = new ClusterRoleInfo(a.get(i));
76             this.primaryRoles.add(s);
77         }
78         this.secondayRoles = new ClusterRoleInfoCollection();
79         a = cfg.getConfig(this.rootNodename).getStringList("secondary_roles");
80         for (int i = 0; i < a.size(); i++) {
81             ClusterRoleInfo s = new ClusterRoleInfo(a.get(i));
82             this.secondayRoles.add(s);
83         }
84         this.checkDuplicateRoleEntries();
85         this.rolesTable = new RolesTable(cfg.getConfig(this.rootNodename).getConfigList("ip_roles_table"));
86     }
87
88     private void checkDuplicateRoleEntries() throws Exception {
89         ClusterRoleInfoCollection duplicateEntries = new ClusterRoleInfoCollection();
90         for (ClusterRoleInfo primaryRole : this.primaryRoles) {
91             if (this.secondayRoles.contains(primaryRole)) {
92                 duplicateEntries.add(primaryRole);
93             }
94         }
95         if (duplicateEntries.size() > 0) {
96             throw new Exception("duplicate entries found: " + duplicateEntries.toString());
97         }
98
99     }
100
101     public static GeoConfig parse(String content) throws Exception {
102         GeoConfig cfg = new GeoConfig();
103         cfg._load(ConfigFactory.parseString(content));
104         return cfg;
105     }
106
107     public ClusterRoleInfoCollection getPrimaryRoles() {
108         return this.primaryRoles;
109     }
110
111     public ClusterRoleInfoCollection getSecondaryRoles() {
112         return this.secondayRoles;
113     }
114
115     public boolean isPrimary(ClusterRoleInfo roleMember) {
116         return !this.isSecondary(roleMember);
117     }
118
119     private boolean isSecondary(ClusterRoleInfo roleMember) {
120         if (roleMember == null) {
121             return false;
122         }
123         for (ClusterRoleInfo info : this.secondayRoles) {
124             if (info.equals(roleMember)) {
125                 return true;
126             }
127         }
128         return false;
129     }
130
131     @Override
132     public String toString() {
133         return "GeoConfig [filename=" + filename + ", rootNodename=" + rootNodename + ", primaryRoles=" + primaryRoles
134                 + ", secondayRoles=" + secondayRoles + ", rolesTable=" + rolesTable + "]";
135     }
136
137     public static class RolesTableEntry {
138         private final ClusterRoleInfo role;
139         private final String ip;
140
141         public RolesTableEntry(Config c) throws Exception {
142             this.role = new ClusterRoleInfo(c.getString("role"));
143             this.ip = c.getString("ip");
144         }
145
146         @Override
147         public String toString() {
148             return "RolesTableEntry [role=" + role + ", ip=" + ip + "]";
149         }
150     }
151     public static class RolesTable extends ArrayList<RolesTableEntry> {
152         private static final long serialVersionUID = -9146218864237487506L;
153
154         public RolesTable(List<? extends Config> configList) throws Exception {
155             for (Config c : configList) {
156                 this.add(new RolesTableEntry(c));
157             }
158         }
159
160     }
161
162
163 }