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.impl;
20 import com.typesafe.config.Config;
21 import com.typesafe.config.ConfigFactory;
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;
28 public class GeoConfig {
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;
42 private GeoConfig(String filename) {
43 this(filename, LUMINA_ROOTNODENAME);
46 private GeoConfig(String filename, String rootNodeName) {
47 this.filename = filename;
48 this.rootNodename = rootNodeName;
51 public static boolean fileExists() {
52 File f = new File(DEFAULT_FILENAME);
56 public static GeoConfig load() throws Exception {
57 return load(DEFAULT_FILENAME);
60 public static GeoConfig load(String filename) throws Exception {
61 GeoConfig cfg = new GeoConfig(filename);
66 private void _load() throws Exception {
67 this._load(ConfigFactory.parseFile(new File(this.filename)));
70 private void _load(Config cfg) throws Exception {
71 this.primaryRoles = new ClusterRoleInfoCollection();
72 List<String> a = cfg.getConfig(this.rootNodename).getStringList("primary_roles");
74 for (int i = 0; i < a.size(); i++) {
75 ClusterRoleInfo s = new ClusterRoleInfo(a.get(i));
76 this.primaryRoles.add(s);
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);
84 this.checkDuplicateRoleEntries();
85 this.rolesTable = new RolesTable(cfg.getConfig(this.rootNodename).getConfigList("ip_roles_table"));
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);
95 if (duplicateEntries.size() > 0) {
96 throw new Exception("duplicate entries found: " + duplicateEntries.toString());
101 public static GeoConfig parse(String content) throws Exception {
102 GeoConfig cfg = new GeoConfig();
103 cfg._load(ConfigFactory.parseString(content));
107 public ClusterRoleInfoCollection getPrimaryRoles() {
108 return this.primaryRoles;
111 public ClusterRoleInfoCollection getSecondaryRoles() {
112 return this.secondayRoles;
115 public boolean isPrimary(ClusterRoleInfo roleMember) {
116 return !this.isSecondary(roleMember);
119 private boolean isSecondary(ClusterRoleInfo roleMember) {
120 if (roleMember == null) {
123 for (ClusterRoleInfo info : this.secondayRoles) {
124 if (info.equals(roleMember)) {
132 public String toString() {
133 return "GeoConfig [filename=" + filename + ", rootNodename=" + rootNodename + ", primaryRoles=" + primaryRoles
134 + ", secondayRoles=" + secondayRoles + ", rolesTable=" + rolesTable + "]";
137 public static class RolesTableEntry {
138 private final ClusterRoleInfo role;
139 private final String ip;
141 public RolesTableEntry(Config c) throws Exception {
142 this.role = new ClusterRoleInfo(c.getString("role"));
143 this.ip = c.getString("ip");
147 public String toString() {
148 return "RolesTableEntry [role=" + role + ", ip=" + ip + "]";
151 public static class RolesTable extends ArrayList<RolesTableEntry> {
152 private static final long serialVersionUID = -9146218864237487506L;
154 public RolesTable(List<? extends Config> configList) throws Exception {
155 for (Config c : configList) {
156 this.add(new RolesTableEntry(c));