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.DmaapBuilder().setVer("0").setTnr("").setDn("").setDpu("").setLu("").setBat("").setNk("").setAko("").createDmaap());
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) {
173 public void update(Dmaap nd) {
174 dmaap.setVersion(nd.getVersion());
175 dmaap.setTopicNsRoot(nd.getTopicNsRoot());
176 dmaap.setDmaapName(nd.getDmaapName());
177 dmaap.setDrProvUrl(nd.getDrProvUrl());
178 dmaap.setBridgeAdminTopic(nd.getBridgeAdminTopic());
179 dmaap.setLoggingUrl(nd.getLoggingUrl());
180 dmaap.setNodeKey(nd.getNodeKey());
181 dmaap.setAccessKeyOwner(nd.getAccessKeyOwner());
184 initMemoryDatabase();
187 private static void databaseResourceInit() {
188 appLogger.info("Data from database");
190 LoadSchema.upgrade();
191 } catch (Exception e) {
192 appLogger.warn("Problem updating DB schema", e);
195 dmaap = new DBSingleton<>(Dmaap.class, "dmaap");
196 TableHandler.setSpecialCase("topic", "replication_case", new TopicReplicationTypeHandler());
197 TableHandler.setSpecialCase("mirror_maker", "topics", new MirrorTopicsHandler());
199 } catch (Exception e) {
200 errorLogger.error("Error initializing database access " + e, e);
205 private static class MirrorTopicsHandler implements DBFieldHandler.SqlOp {
207 public Object get(ResultSet rs, int index) throws Exception {
208 String val = rs.getString(index);
212 List<String> rv = new ArrayList<>();
213 for (String s : val.split(",")) {
214 rv.add(new String(s));
219 public void set(PreparedStatement ps, int index, Object val) throws Exception {
221 ps.setString(index, null);
224 @SuppressWarnings("unchecked")
225 List<String> xv = (List<String>) val;
226 StringBuilder sb = new StringBuilder();
228 for (Object o : xv) {
229 String rv = (String) o;
230 sb.append(sep).append(DBFieldHandler.fesc(rv));
233 ps.setString(index, sb.toString());
237 private static class TopicReplicationTypeHandler implements DBFieldHandler.SqlOp {
239 public Object get(ResultSet rs, int index) throws Exception {
240 int val = rs.getInt(index);
242 return (ReplicationType.valueOf(val));
245 public void set(PreparedStatement ps, int index, Object val) throws Exception {
250 @SuppressWarnings("unchecked")
251 ReplicationType rep = (ReplicationType) val;
252 ps.setInt(index, rep.getValue());
256 private static void initMemoryDatabase() {
257 dcaeLocations = new HashMap<>();
258 dr_nodes = new HashMap<>();
259 dr_pubs = new HashMap<>();
260 dr_subs = new HashMap<>();
261 mr_clients = new HashMap<>();
262 mr_clusters = new HashMap<>();
263 feeds = new HashMap<>();
264 topics = new HashMap<>();
265 mirrors = new HashMap<>();
268 private static void initDatabase() throws Exception {
269 dcaeLocations = new DBMap<>(DcaeLocation.class, "dcae_location", "dcae_location_name");
270 dr_nodes = new DBMap<>(DR_Node.class, "dr_node", "fqdn");
271 dr_pubs = new DBMap<>(DR_Pub.class, "dr_pub", "pub_id");
272 dr_subs = new DBMap<>(DR_Sub.class, "dr_sub", "sub_id");
273 mr_clients = new DBMap<>(MR_Client.class, "mr_client", "mr_client_id");
274 mr_clusters = new DBMap<>(MR_Cluster.class, "mr_cluster", "dcae_location_name");
275 feeds = new DBMap<>(Feed.class, "feed", "feed_id");
276 topics = new DBMap<>(Topic.class, "topic", "fqtn");
277 mirrors = new DBMap<>(MirrorMaker.class, "mirror_maker", "mm_name");
280 private static void determineDatabaseType() {
281 DmaapConfig dmaapConfig = (DmaapConfig) DmaapConfig.getConfig();
282 String isPgSQLset = dmaapConfig.getProperty("UsePGSQL", "false");
283 databaseType = isPgSQLset.equalsIgnoreCase("true") ? DBType.PGSQL : DBType.MEMORY;