2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.dmaap.dbcapi.database;
26 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
27 import org.onap.dmaap.dbcapi.logging.DmaapbcLogMessageEnum;
28 import org.onap.dmaap.dbcapi.model.*;
29 import org.onap.dmaap.dbcapi.util.DmaapConfig;
30 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;
52 private static class MirrorTopicsHandler implements DBFieldHandler.SqlOp {
53 public Object get(ResultSet rs, int index) throws Exception {
54 String val = rs.getString(index);
58 List<String> rv = new ArrayList<String>();
59 for (String s: val.split(",")) {
60 rv.add(new String(s));
64 public void set(PreparedStatement ps, int index, Object val) throws Exception {
66 ps.setString(index, null);
69 @SuppressWarnings("unchecked")
70 List<String> xv = (List<String>)val;
71 StringBuilder sb = new StringBuilder();
74 String rv = (String)o;
75 sb.append(sep).append(DBFieldHandler.fesc(rv));
78 ps.setString(index, sb.toString());
81 private static class TopicReplicationTypeHandler implements DBFieldHandler.SqlOp {
82 public Object get(ResultSet rs, int index) throws Exception {
83 int val = rs.getInt(index);
85 return (ReplicationType.valueOf(val));
87 public void set(PreparedStatement ps, int index, Object val) throws Exception {
92 @SuppressWarnings("unchecked")
93 ReplicationType rep = (ReplicationType) val;
94 ps.setInt(index, rep.getValue());
97 public static Singleton<Dmaap> getDmaap() {
103 public static Map<String, DcaeLocation> getDcaeLocations() {
104 return dcaeLocations;
107 public static Map<String, DR_Node> getDr_nodes() {
111 public static Map<String, DR_Sub> getDr_subs() {
114 public static Map<String, DR_Pub> getDr_pubs() {
118 public static Map<String, MR_Client> getMr_clients() {
123 public static Map<String, MR_Cluster> getMr_clusters() {
127 public static Map<String, Feed> getFeeds() {
130 public static Map<String, Topic> getTopics() {
133 public static Map<String, MirrorMaker> getMirrorMakers() {
139 appLogger.info( "begin static initialization");
140 appLogger.info( "initializing dmaap" );
141 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
142 if ("true".equalsIgnoreCase(p.getProperty("UsePGSQL", "false"))) {
143 appLogger.info("Data from database");
145 LoadSchema.upgrade();
146 } catch (Exception e) {
147 appLogger.warn("Problem updating DB schema", e);
150 dmaap = new DBSingleton<Dmaap>(Dmaap.class, "dmaap");
151 dcaeLocations = new DBMap<>(DcaeLocation.class, "dcae_location", "dcae_location_name");
152 dr_nodes = new DBMap<>(DR_Node.class, "dr_node", "fqdn");
153 dr_pubs = new DBMap<>(DR_Pub.class, "dr_pub", "pub_id");
154 dr_subs = new DBMap<>(DR_Sub.class, "dr_sub", "sub_id");
155 mr_clients = new DBMap<>(MR_Client.class, "mr_client", "mr_client_id");
156 mr_clusters = new DBMap<>(MR_Cluster.class, "mr_cluster", "dcae_location_name");
157 feeds = new DBMap<>(Feed.class, "feed", "feed_id");
158 TableHandler.setSpecialCase("topic", "replication_case", new TopicReplicationTypeHandler());
159 topics = new DBMap<>(Topic.class, "topic", "fqtn");
160 TableHandler.setSpecialCase("mirror_maker", "topics", new MirrorTopicsHandler());
161 mirrors = new DBMap<>(MirrorMaker.class, "mirror_maker", "mm_name");
162 } catch (Exception e) {
163 errorLogger.error("Error initializing database access " + e, e);
167 appLogger.info("Data from memory");
168 dmaap = new Singleton<Dmaap>() {
170 public void remove() {
173 public void init(Dmaap val) {
181 public void update(Dmaap nd) {
182 dmaap.setVersion(nd.getVersion());
183 dmaap.setTopicNsRoot(nd.getTopicNsRoot());
184 dmaap.setDmaapName(nd.getDmaapName());
185 dmaap.setDrProvUrl(nd.getDrProvUrl());
186 dmaap.setBridgeAdminTopic(nd.getBridgeAdminTopic());
187 dmaap.setLoggingUrl(nd.getLoggingUrl());
188 dmaap.setNodeKey(nd.getNodeKey());
189 dmaap.setAccessKeyOwner(nd.getAccessKeyOwner());
192 dcaeLocations = new HashMap<>();
193 dr_nodes = new HashMap<>();
194 dr_pubs = new HashMap<>();
195 dr_subs = new HashMap<>();
196 mr_clients = new HashMap<>();
197 mr_clusters = new HashMap<>();
198 feeds = new HashMap<>();
199 topics = new HashMap<>();
200 mirrors = new HashMap<>();
202 dmaap.init(new Dmaap("0", "", "", "", "", "", "", ""));
203 // force initial read from DB, if it exists
204 @SuppressWarnings("unused")
205 Dmaap dmx = dmaap.get();
207 // old code in this spot would read from properties file as part of init.
208 // but all those properties are now set via /dmaap API
210 } catch (Exception e) {
211 errorLogger.error("Error", e);
212 errorLogger.error(DmaapbcLogMessageEnum.DB_UPDATE_ERROR, e.getMessage());
217 public static synchronized String getNextClientId() {
219 long id = System.currentTimeMillis();
220 if ( id <= lastTime ) {
224 return Long.toString(id);