2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
7 * Modifications Copyright (C) 2019 IBM.
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.dmaap.dbcapi.database;
28 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
29 import org.onap.dmaap.dbcapi.logging.DmaapbcLogMessageEnum;
30 import org.onap.dmaap.dbcapi.model.*;
31 import org.onap.dmaap.dbcapi.util.DmaapConfig;
32 import org.onap.dmaap.dbcapi.util.Singleton;
35 public class DatabaseClass extends BaseLoggingClass {
37 private static Singleton<Dmaap> dmaap;
38 private static Map<String, DcaeLocation> dcaeLocations;
39 private static Map<String, DR_Node> dr_nodes;
40 private static Map<String, DR_Pub> dr_pubs;
41 private static Map<String, DR_Sub> dr_subs;
42 private static Map<String, MR_Client> mr_clients;
43 private static Map<String, MR_Cluster> mr_clusters;
44 private static Map<String, Feed> feeds;
45 private static Map<String, Topic> topics;
46 private static Map<String, MirrorMaker> mirrors;
48 private static long lastTime = 0L;
49 private static DBType databaseType;
55 public static Singleton<Dmaap> getDmaap() {
60 public static Map<String, DcaeLocation> getDcaeLocations() {
64 public static Map<String, DR_Node> getDr_nodes() {
68 public static Map<String, DR_Sub> getDr_subs() {
72 public static Map<String, DR_Pub> getDr_pubs() {
76 public static Map<String, MR_Client> getMr_clients() {
81 public static Map<String, MR_Cluster> getMr_clusters() {
85 public static Map<String, Feed> getFeeds() {
89 public static Map<String, Topic> getTopics() {
93 public static Map<String, MirrorMaker> getMirrorMakers() {
99 appLogger.info("begin static initialization");
100 appLogger.info("initializing dmaap");
101 determineDatabaseType();
103 switch (databaseType) {
105 databaseResourceInit();
108 inMemoryResourceInit();
112 dmaap.init(new Dmaap("0", "", "", "", "", "", "", ""));
113 // force initial read from DB, if it exists
114 @SuppressWarnings("unused")
115 Dmaap dmx = dmaap.get();
117 // old code in this spot would read from properties file as part of init.
118 // but all those properties are now set via /dmaap API
120 } catch (Exception e) {
121 errorLogger.error("Error", e);
122 errorLogger.error(DmaapbcLogMessageEnum.DB_UPDATE_ERROR, e.getMessage());
127 public static synchronized String getNextClientId() {
129 long id = System.currentTimeMillis();
130 if (id <= lastTime) {
134 return Long.toString(id);
137 public static synchronized void clearDatabase() {
138 switch (databaseType) {
142 } catch (Exception e) {
143 errorLogger.error("Error initializing database access " + e, e);
147 initMemoryDatabase();
152 private static void inMemoryResourceInit() {
153 appLogger.info("Data from memory");
154 dmaap = new Singleton<Dmaap>() {
157 public void remove() {
161 public void init(Dmaap val) {
171 public void update(Dmaap nd) {
172 dmaap.setVersion(nd.getVersion());
173 dmaap.setTopicNsRoot(nd.getTopicNsRoot());
174 dmaap.setDmaapName(nd.getDmaapName());
175 dmaap.setDrProvUrl(nd.getDrProvUrl());
176 dmaap.setBridgeAdminTopic(nd.getBridgeAdminTopic());
177 dmaap.setLoggingUrl(nd.getLoggingUrl());
178 dmaap.setNodeKey(nd.getNodeKey());
179 dmaap.setAccessKeyOwner(nd.getAccessKeyOwner());
182 initMemoryDatabase();
185 private static void databaseResourceInit() {
186 appLogger.info("Data from database");
188 LoadSchema.upgrade();
189 } catch (Exception e) {
190 appLogger.warn("Problem updating DB schema", e);
193 dmaap = new DBSingleton<>(Dmaap.class, "dmaap");
194 TableHandler.setSpecialCase("topic", "replication_case", new TopicReplicationTypeHandler());
195 TableHandler.setSpecialCase("mirror_maker", "topics", new MirrorTopicsHandler());
197 } catch (Exception e) {
198 errorLogger.error("Error initializing database access " + e, e);
203 private static class MirrorTopicsHandler implements DBFieldHandler.SqlOp {
205 public Object get(ResultSet rs, int index) throws Exception {
206 String val = rs.getString(index);
210 List<String> rv = new ArrayList<>();
211 for (String s : val.split(",")) {
212 rv.add(new String(s));
217 public void set(PreparedStatement ps, int index, Object val) throws Exception {
219 ps.setString(index, null);
222 @SuppressWarnings("unchecked")
223 List<String> xv = (List<String>) val;
224 StringBuilder sb = new StringBuilder();
226 for (Object o : xv) {
227 String rv = (String) o;
228 sb.append(sep).append(DBFieldHandler.fesc(rv));
231 ps.setString(index, sb.toString());
235 private static class TopicReplicationTypeHandler implements DBFieldHandler.SqlOp {
237 public Object get(ResultSet rs, int index) throws Exception {
238 int val = rs.getInt(index);
240 return (ReplicationType.valueOf(val));
243 public void set(PreparedStatement ps, int index, Object val) throws Exception {
248 @SuppressWarnings("unchecked")
249 ReplicationType rep = (ReplicationType) val;
250 ps.setInt(index, rep.getValue());
254 private static void initMemoryDatabase() {
255 dcaeLocations = new HashMap<>();
256 dr_nodes = new HashMap<>();
257 dr_pubs = new HashMap<>();
258 dr_subs = new HashMap<>();
259 mr_clients = new HashMap<>();
260 mr_clusters = new HashMap<>();
261 feeds = new HashMap<>();
262 topics = new HashMap<>();
263 mirrors = new HashMap<>();
266 private static void initDatabase() throws Exception {
267 dcaeLocations = new DBMap<>(DcaeLocation.class, "dcae_location", "dcae_location_name");
268 dr_nodes = new DBMap<>(DR_Node.class, "dr_node", "fqdn");
269 dr_pubs = new DBMap<>(DR_Pub.class, "dr_pub", "pub_id");
270 dr_subs = new DBMap<>(DR_Sub.class, "dr_sub", "sub_id");
271 mr_clients = new DBMap<>(MR_Client.class, "mr_client", "mr_client_id");
272 mr_clusters = new DBMap<>(MR_Cluster.class, "mr_cluster", "dcae_location_name");
273 feeds = new DBMap<>(Feed.class, "feed", "feed_id");
274 topics = new DBMap<>(Topic.class, "topic", "fqtn");
275 mirrors = new DBMap<>(MirrorMaker.class, "mirror_maker", "mm_name");
278 private static void determineDatabaseType() {
279 DmaapConfig dmaapConfig = (DmaapConfig) DmaapConfig.getConfig();
280 String isPgSQLset = dmaapConfig.getProperty("UsePGSQL", "false");
281 databaseType = isPgSQLset.equalsIgnoreCase("true") ? DBType.PGSQL : DBType.MEMORY;