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