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.netconfnodestateservice.impl.conf.odlGeo;
21 import java.util.ArrayList;
22 import java.util.List;
24 import com.typesafe.config.Config;
25 import com.typesafe.config.ConfigFactory;
27 public class GeoConfig {
29 private static final String DEFAULT_FILENAME = "configuration/initial/geo.conf";
30 private static final String LUMINA_ROOTNODENAME = "lumina-geo-cluster";
31 private final String filename;
32 private final String rootNodename;
33 private ClusterRoleInfoCollection primaryRoles;
34 private ClusterRoleInfoCollection secondayRoles;
35 private RolesTable rolesTable;
41 private GeoConfig(String filename) {
42 this(filename, LUMINA_ROOTNODENAME);
45 private GeoConfig(String filename, String rootNodeName) {
46 this.filename = filename;
47 this.rootNodename = rootNodeName;
50 public static boolean fileExists() {
51 File f = new File(DEFAULT_FILENAME);
55 public static GeoConfig load() throws Exception {
56 return load(DEFAULT_FILENAME);
59 public static GeoConfig load(String filename) throws Exception {
60 GeoConfig cfg = new GeoConfig(filename);
65 private void _load() throws Exception {
66 this._load(ConfigFactory.parseFile(new File(this.filename)));
69 private void _load(Config cfg) throws Exception {
70 this.primaryRoles = new ClusterRoleInfoCollection();
71 List<String> a = cfg.getConfig(this.rootNodename).getStringList("primary_roles");
73 for (int i = 0; i < a.size(); i++) {
74 ClusterRoleInfo s = new ClusterRoleInfo(a.get(i));
75 this.primaryRoles.add(s);
77 this.secondayRoles = new ClusterRoleInfoCollection();
78 a = cfg.getConfig(this.rootNodename).getStringList("secondary_roles");
79 for (int i = 0; i < a.size(); i++) {
80 ClusterRoleInfo s = new ClusterRoleInfo(a.get(i));
81 this.secondayRoles.add(s);
83 this.checkDuplicateRoleEntries();
84 this.rolesTable = new RolesTable(cfg.getConfig(this.rootNodename).getConfigList("ip_roles_table"));
87 private void checkDuplicateRoleEntries() throws Exception {
88 ClusterRoleInfoCollection duplicateEntries = new ClusterRoleInfoCollection();
89 for (ClusterRoleInfo primaryRole : this.primaryRoles) {
90 if (this.secondayRoles.contains(primaryRole)) {
91 duplicateEntries.add(primaryRole);
94 if (duplicateEntries.size() > 0) {
95 throw new Exception("duplicate entries found: " + duplicateEntries.toString());
100 public static GeoConfig parse(String content) throws Exception {
101 GeoConfig cfg = new GeoConfig();
102 cfg._load(ConfigFactory.parseString(content));
106 public ClusterRoleInfoCollection getPrimaryRoles() {
107 return this.primaryRoles;
110 public ClusterRoleInfoCollection getSecondaryRoles() {
111 return this.secondayRoles;
114 public boolean isPrimary(ClusterRoleInfo roleMember) {
115 return !this.isSecondary(roleMember);
118 private boolean isSecondary(ClusterRoleInfo roleMember) {
119 if (roleMember == null) {
122 for (ClusterRoleInfo info : this.secondayRoles) {
123 if (info.equals(roleMember)) {
131 public String toString() {
132 return "GeoConfig [filename=" + filename + ", rootNodename=" + rootNodename + ", primaryRoles=" + primaryRoles
133 + ", secondayRoles=" + secondayRoles + ", rolesTable=" + rolesTable + "]";
136 public static class RolesTableEntry {
137 private final ClusterRoleInfo role;
138 private final String ip;
140 public RolesTableEntry(Config c) throws Exception {
141 this.role = new ClusterRoleInfo(c.getString("role"));
142 this.ip = c.getString("ip");
146 public String toString() {
147 return "RolesTableEntry [role=" + role + ", ip=" + ip + "]";
150 public static class RolesTable extends ArrayList<RolesTableEntry> {
151 private static final long serialVersionUID = -9146218864237487506L;
153 public RolesTable(List<? extends Config> configList) throws Exception {
154 for (Config c : configList) {
155 this.add(new RolesTableEntry(c));