Stop referencing unused properties
[dmaap/dbcapi.git] / src / main / java / org / onap / dmaap / dbcapi / database / DatabaseClass.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.onap.dmaap
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.dmaap.dbcapi.database;
22
23 import java.util.*;
24 import java.sql.*;
25
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;
31
32
33
34
35 public class DatabaseClass extends BaseLoggingClass {
36         
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;
47         
48         private static long lastTime = 0L;
49         
50
51
52         private static class MirrorTopicsHandler implements DBFieldHandler.SqlOp {
53                 public Object get(ResultSet rs, int index) throws Exception {
54                         String val = rs.getString(index);
55                         if (val == null) {
56                                 return(null);
57                         }
58                         List<String> rv = new ArrayList<String>();
59                         for (String s: val.split(",")) {
60                                 rv.add(new String(s));
61                         }
62                         return(rv);
63                 }
64                 public void set(PreparedStatement ps, int index, Object val) throws Exception {
65                         if (val == null) {
66                                 ps.setString(index, null);
67                                 return;
68                         }
69                         @SuppressWarnings("unchecked")
70                         List<String> xv = (List<String>)val;
71                         StringBuilder sb = new StringBuilder();
72                         String sep = "";
73                         for (Object o: xv) {
74                                 String rv = (String)o;
75                                 sb.append(sep).append(DBFieldHandler.fesc(rv));
76                                 sep = ",";
77                         }
78                         ps.setString(index, sb.toString());
79                 }
80         }
81         private static class TopicReplicationTypeHandler implements DBFieldHandler.SqlOp {
82                 public Object get(ResultSet rs, int index) throws Exception {
83                         int val = rs.getInt(index);
84
85                         return (ReplicationType.valueOf(val));
86                 }
87                 public void set(PreparedStatement ps, int index, Object val) throws Exception {
88                         if (val == null) {
89                                 ps.setInt(index, 0);
90                                 return;
91                         }
92                         @SuppressWarnings("unchecked")
93                         ReplicationType rep = (ReplicationType) val;
94                         ps.setInt(index, rep.getValue());
95                 }       
96         }
97         public static Singleton<Dmaap> getDmaap() {
98                 return dmaap;
99         }
100         
101
102         
103         public static Map<String, DcaeLocation> getDcaeLocations() {
104                 return dcaeLocations;
105         }
106         
107         public static Map<String, DR_Node> getDr_nodes() {
108                 return dr_nodes;
109         }
110         
111         public static Map<String, DR_Sub> getDr_subs() {
112                 return dr_subs;
113         }
114         public static Map<String, DR_Pub> getDr_pubs() {
115                 return dr_pubs;
116         }
117
118         public static Map<String, MR_Client> getMr_clients() {
119                 return mr_clients;
120         }
121
122
123         public static Map<String, MR_Cluster> getMr_clusters() {
124                 return mr_clusters;
125         }
126         
127         public static Map<String, Feed> getFeeds() {
128                 return feeds;
129         }
130         public static Map<String, Topic> getTopics() {
131                 return topics;
132         }
133         public static Map<String, MirrorMaker> getMirrorMakers() {
134                 return mirrors;
135         }
136
137         static {
138                 try {
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");
144                         try {
145                                 LoadSchema.upgrade();
146                         } catch (Exception e) {
147                                 appLogger.warn("Problem updating DB schema", e);
148                         }
149                         try {
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);
164                                 System.exit(1);
165                         }
166                 } else {
167                         appLogger.info("Data from memory");
168                         dmaap = new Singleton<Dmaap>() {
169                                 private Dmaap dmaap;
170                                 public void remove() {
171                                         dmaap = null;
172                                 }
173                                 public void init(Dmaap val) {
174                                         if (dmaap == null) {
175                                                 dmaap = val;
176                                         }
177                                 }
178                                 public Dmaap get() {
179                                         return(dmaap);
180                                 }
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());
190                                 }
191                         };
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<>();
201                 }
202                 dmaap.init(new Dmaap("0", "", "", "", "", "", "", ""));
203                 // force initial read from DB, if it exists
204                 @SuppressWarnings("unused")
205                 Dmaap dmx = dmaap.get();
206                 
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
209
210                 } catch (Exception e) {
211                         errorLogger.error("Error", e);
212                         errorLogger.error(DmaapbcLogMessageEnum.DB_UPDATE_ERROR, e.getMessage());
213                 }
214
215         }
216         
217         public static synchronized String getNextClientId() {
218                 
219                 long id = System.currentTimeMillis();
220                 if ( id <= lastTime ) {
221                         id = lastTime + 1;
222                 }
223                 lastTime = id;
224                 return Long.toString(id);
225         }
226
227         
228
229
230 }