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;
37 public class DatabaseClass extends BaseLoggingClass {
39 private static Singleton<Dmaap> dmaap;
40 private static Map<String, DcaeLocation> dcaeLocations;
41 private static Map<String, DR_Node> dr_nodes;
42 private static Map<String, DR_Pub> dr_pubs;
43 private static Map<String, DR_Sub> dr_subs;
44 private static Map<String, MR_Client> mr_clients;
45 private static Map<String, MR_Cluster> mr_clusters;
46 private static Map<String, Feed> feeds;
47 private static Map<String, Topic> topics;
48 private static Map<String, MirrorMaker> mirrors;
50 private static long lastTime = 0L;
54 private static class MirrorTopicsHandler implements DBFieldHandler.SqlOp {
55 public Object get(ResultSet rs, int index) throws Exception {
56 String val = rs.getString(index);
60 List<String> rv = new ArrayList<>();
61 for (String s: val.split(",")) {
62 rv.add(new String(s));
66 public void set(PreparedStatement ps, int index, Object val) throws Exception {
68 ps.setString(index, null);
71 @SuppressWarnings("unchecked")
72 List<String> xv = (List<String>)val;
73 StringBuilder sb = new StringBuilder();
76 String rv = (String)o;
77 sb.append(sep).append(DBFieldHandler.fesc(rv));
80 ps.setString(index, sb.toString());
83 private static class TopicReplicationTypeHandler implements DBFieldHandler.SqlOp {
84 public Object get(ResultSet rs, int index) throws Exception {
85 int val = rs.getInt(index);
87 return (ReplicationType.valueOf(val));
89 public void set(PreparedStatement ps, int index, Object val) throws Exception {
94 @SuppressWarnings("unchecked")
95 ReplicationType rep = (ReplicationType) val;
96 ps.setInt(index, rep.getValue());
99 public static Singleton<Dmaap> getDmaap() {
105 public static Map<String, DcaeLocation> getDcaeLocations() {
106 return dcaeLocations;
109 public static Map<String, DR_Node> getDr_nodes() {
113 public static Map<String, DR_Sub> getDr_subs() {
116 public static Map<String, DR_Pub> getDr_pubs() {
120 public static Map<String, MR_Client> getMr_clients() {
125 public static Map<String, MR_Cluster> getMr_clusters() {
129 public static Map<String, Feed> getFeeds() {
132 public static Map<String, Topic> getTopics() {
135 public static Map<String, MirrorMaker> getMirrorMakers() {
141 appLogger.info( "begin static initialization");
142 appLogger.info( "initializing dmaap" );
143 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
144 if ("true".equalsIgnoreCase(p.getProperty("UsePGSQL", "false"))) {
145 appLogger.info("Data from database");
147 LoadSchema.upgrade();
148 } catch (Exception e) {
149 appLogger.warn("Problem updating DB schema", e);
152 dmaap = new DBSingleton<>(Dmaap.class, "dmaap");
153 dcaeLocations = new DBMap<>(DcaeLocation.class, "dcae_location", "dcae_location_name");
154 dr_nodes = new DBMap<>(DR_Node.class, "dr_node", "fqdn");
155 dr_pubs = new DBMap<>(DR_Pub.class, "dr_pub", "pub_id");
156 dr_subs = new DBMap<>(DR_Sub.class, "dr_sub", "sub_id");
157 mr_clients = new DBMap<>(MR_Client.class, "mr_client", "mr_client_id");
158 mr_clusters = new DBMap<>(MR_Cluster.class, "mr_cluster", "dcae_location_name");
159 feeds = new DBMap<>(Feed.class, "feed", "feed_id");
160 TableHandler.setSpecialCase("topic", "replication_case", new TopicReplicationTypeHandler());
161 topics = new DBMap<>(Topic.class, "topic", "fqtn");
162 TableHandler.setSpecialCase("mirror_maker", "topics", new MirrorTopicsHandler());
163 mirrors = new DBMap<>(MirrorMaker.class, "mirror_maker", "mm_name");
164 } catch (Exception e) {
165 errorLogger.error("Error initializing database access " + e, e);
169 appLogger.info("Data from memory");
170 dmaap = new Singleton<Dmaap>() {
172 public void remove() {
175 public void init(Dmaap val) {
183 public void update(Dmaap nd) {
184 dmaap.setVersion(nd.getVersion());
185 dmaap.setTopicNsRoot(nd.getTopicNsRoot());
186 dmaap.setDmaapName(nd.getDmaapName());
187 dmaap.setDrProvUrl(nd.getDrProvUrl());
188 dmaap.setBridgeAdminTopic(nd.getBridgeAdminTopic());
189 dmaap.setLoggingUrl(nd.getLoggingUrl());
190 dmaap.setNodeKey(nd.getNodeKey());
191 dmaap.setAccessKeyOwner(nd.getAccessKeyOwner());
194 dcaeLocations = new HashMap<>();
195 dr_nodes = new HashMap<>();
196 dr_pubs = new HashMap<>();
197 dr_subs = new HashMap<>();
198 mr_clients = new HashMap<>();
199 mr_clusters = new HashMap<>();
200 feeds = new HashMap<>();
201 topics = new HashMap<>();
202 mirrors = new HashMap<>();
204 dmaap.init(new Dmaap("0", "", "", "", "", "", "", ""));
205 // force initial read from DB, if it exists
206 @SuppressWarnings("unused")
207 Dmaap dmx = dmaap.get();
209 // old code in this spot would read from properties file as part of init.
210 // but all those properties are now set via /dmaap API
212 } catch (Exception e) {
213 errorLogger.error("Error", e);
214 errorLogger.error(DmaapbcLogMessageEnum.DB_UPDATE_ERROR, e.getMessage());
219 public static synchronized String getNextClientId() {
221 long id = System.currentTimeMillis();
222 if ( id <= lastTime ) {
226 return Long.toString(id);