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