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.loadSchema();
191 } catch (Exception e) {
192 appLogger.warn("Problem updating DB schema", e);
196 dmaap = new DBSingleton<>(Dmaap.class, "dmaap");
197 TableHandler.setSpecialCase("topic", "replication_case", new TopicReplicationTypeHandler());
198 TableHandler.setSpecialCase("mirror_maker", "topics", new MirrorTopicsHandler());
200 } catch (Exception e) {
201 errorLogger.error("Error initializing database access " + e, e);
206 private static class MirrorTopicsHandler implements DBFieldHandler.SqlOp {
208 public Object get(ResultSet rs, int index) throws Exception {
209 String val = rs.getString(index);
213 List<String> rv = new ArrayList<>();
214 for (String s : val.split(",")) {
215 rv.add(new String(s));
220 public void set(PreparedStatement ps, int index, Object val) throws Exception {
222 ps.setString(index, null);
225 @SuppressWarnings("unchecked")
226 List<String> xv = (List<String>) val;
227 StringBuilder sb = new StringBuilder();
229 for (Object o : xv) {
230 String rv = (String) o;
231 sb.append(sep).append(DBFieldHandler.fesc(rv));
234 ps.setString(index, sb.toString());
238 private static class TopicReplicationTypeHandler implements DBFieldHandler.SqlOp {
240 public Object get(ResultSet rs, int index) throws Exception {
241 int val = rs.getInt(index);
243 return (ReplicationType.valueOf(val));
246 public void set(PreparedStatement ps, int index, Object val) throws Exception {
251 @SuppressWarnings("unchecked")
252 ReplicationType rep = (ReplicationType) val;
253 ps.setInt(index, rep.getValue());
257 private static void initMemoryDatabase() {
258 dcaeLocations = new HashMap<>();
259 dr_nodes = new HashMap<>();
260 dr_pubs = new HashMap<>();
261 dr_subs = new HashMap<>();
262 mr_clients = new HashMap<>();
263 mr_clusters = new HashMap<>();
264 feeds = new HashMap<>();
265 topics = new HashMap<>();
266 mirrors = new HashMap<>();
269 private static void initDatabase() throws Exception {
270 dcaeLocations = new DBMap<>(DcaeLocation.class, "dcae_location", "dcae_location_name");
271 dr_nodes = new DBMap<>(DR_Node.class, "dr_node", "fqdn");
272 dr_pubs = new DBMap<>(DR_Pub.class, "dr_pub", "pub_id");
273 dr_subs = new DBMap<>(DR_Sub.class, "dr_sub", "sub_id");
274 mr_clients = new DBMap<>(MR_Client.class, "mr_client", "mr_client_id");
275 mr_clusters = new DBMap<>(MR_Cluster.class, "mr_cluster", "dcae_location_name");
276 feeds = new DBMap<>(Feed.class, "feed", "feed_id");
277 topics = new DBMap<>(Topic.class, "topic", "fqtn");
278 mirrors = new DBMap<>(MirrorMaker.class, "mirror_maker", "mm_name");
281 private static void determineDatabaseType() {
282 DmaapConfig dmaapConfig = (DmaapConfig) DmaapConfig.getConfig();
283 String isPgSQLset = dmaapConfig.getProperty("UsePGSQL", "false");
284 databaseType = isPgSQLset.equalsIgnoreCase("true") ? DBType.PGSQL : DBType.MEMORY;