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 //String[] f = s.split(";");
64 rv.add(new String(s));
68 public void set(PreparedStatement ps, int index, Object val) throws Exception {
70 ps.setString(index, null);
73 @SuppressWarnings("unchecked")
74 List<String> xv = (List<String>)val;
75 StringBuffer sb = new StringBuffer();
78 String rv = (String)o;
79 sb.append(sep).append(DBFieldHandler.fesc(rv));
82 ps.setString(index, sb.toString());
85 private static class TopicReplicationTypeHandler implements DBFieldHandler.SqlOp {
86 public Object get(ResultSet rs, int index) throws Exception {
87 int val = rs.getInt(index);
89 return (ReplicationType.valueOf(val));
91 public void set(PreparedStatement ps, int index, Object val) throws Exception {
96 @SuppressWarnings("unchecked")
97 ReplicationType rep = (ReplicationType) val;
98 ps.setInt(index, rep.getValue());
101 public static Singleton<Dmaap> getDmaap() {
107 public static Map<String, DcaeLocation> getDcaeLocations() {
108 return dcaeLocations;
111 public static Map<String, DR_Node> getDr_nodes() {
115 public static Map<String, DR_Sub> getDr_subs() {
118 public static Map<String, DR_Pub> getDr_pubs() {
122 public static Map<String, MR_Client> getMr_clients() {
127 public static Map<String, MR_Cluster> getMr_clusters() {
131 public static Map<String, Feed> getFeeds() {
134 public static Map<String, Topic> getTopics() {
137 public static Map<String, MirrorMaker> getMirrorMakers() {
143 appLogger.info( "begin static initialization");
144 appLogger.info( "initializing dmaap" );
145 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
146 if ("true".equalsIgnoreCase(p.getProperty("UsePGSQL", "false"))) {
147 appLogger.info("Data from database");
149 LoadSchema.upgrade();
150 } catch (Exception e) {
151 appLogger.warn("Problem updating DB schema", e);
154 dmaap = new DBSingleton<Dmaap>(Dmaap.class, "dmaap");
155 dcaeLocations = new DBMap<DcaeLocation>(DcaeLocation.class, "dcae_location", "dcae_location_name");
156 dr_nodes = new DBMap<DR_Node>(DR_Node.class, "dr_node", "fqdn");
157 dr_pubs = new DBMap<DR_Pub>(DR_Pub.class, "dr_pub", "pub_id");
158 dr_subs = new DBMap<DR_Sub>(DR_Sub.class, "dr_sub", "sub_id");
159 mr_clients = new DBMap<MR_Client>(MR_Client.class, "mr_client", "mr_client_id");
160 mr_clusters = new DBMap<MR_Cluster>(MR_Cluster.class, "mr_cluster", "dcae_location_name");
161 feeds = new DBMap<Feed>(Feed.class, "feed", "feed_id");
162 TableHandler.setSpecialCase("topic", "replication_case", new TopicReplicationTypeHandler());
163 topics = new DBMap<Topic>(Topic.class, "topic", "fqtn");
164 TableHandler.setSpecialCase("mirror_maker", "topics", new MirrorTopicsHandler());
165 mirrors = new DBMap<MirrorMaker>(MirrorMaker.class, "mirror_maker", "mm_name");
166 } catch (Exception e) {
167 errorLogger.error("Error initializing database access " + e, e);
171 appLogger.info("Data from memory");
172 dmaap = new Singleton<Dmaap>() {
174 public void remove() {
177 public void init(Dmaap val) {
185 public void update(Dmaap nd) {
186 dmaap.setVersion(nd.getVersion());
187 dmaap.setTopicNsRoot(nd.getTopicNsRoot());
188 dmaap.setDmaapName(nd.getDmaapName());
189 dmaap.setDrProvUrl(nd.getDrProvUrl());
190 dmaap.setBridgeAdminTopic(nd.getBridgeAdminTopic());
191 dmaap.setLoggingUrl(nd.getLoggingUrl());
192 dmaap.setNodeKey(nd.getNodeKey());
193 dmaap.setAccessKeyOwner(nd.getAccessKeyOwner());
196 dcaeLocations = new HashMap<String, DcaeLocation>();
197 dr_nodes = new HashMap<String, DR_Node>();
198 dr_pubs = new HashMap<String, DR_Pub>();
199 dr_subs = new HashMap<String, DR_Sub>();
200 mr_clients = new HashMap<String, MR_Client>();
201 mr_clusters = new HashMap<String, MR_Cluster>();
202 feeds = new HashMap<String, Feed>();
203 topics = new HashMap<String, Topic>();
204 mirrors = new HashMap<String, MirrorMaker>();
206 dmaap.init(new Dmaap("0", "", "", "", "", "", "", ""));
207 // check for, and set up initial data, if it isn't already there
208 Dmaap dmx = dmaap.get();
209 if ("0".equals(dmx.getVersion())) {
211 dmx = new Dmaap("0", "", "", "", "", "", "", "");
212 dmx.setDmaapName(p.getProperty("DmaapName"));
213 dmx.setDrProvUrl("https://" + p.getProperty("DR.provhost", "notSet"));
214 dmx.setTopicNsRoot(p.getProperty("topicNsRoot"));
215 dmx.setBridgeAdminTopic("DCAE_MM_AGENT");
219 } catch (Exception e) {
220 errorLogger.error(DmaapbcLogMessageEnum.DB_UPDATE_ERROR, e.getMessage());
224 public synchronized static String getNextClientId() {
226 long id = System.currentTimeMillis();
227 if ( id <= lastTime ) {
231 return Long.toString(id);