Alternative MR replication method
[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                                 //String[] f = s.split(";");
61                                 //if (f.length < 3) {
62                                 //      continue;
63                                 //}
64                                 rv.add(new String(s));
65                         }
66                         return(rv);
67                 }
68                 public void set(PreparedStatement ps, int index, Object val) throws Exception {
69                         if (val == null) {
70                                 ps.setString(index, null);
71                                 return;
72                         }
73                         @SuppressWarnings("unchecked")
74                         List<String> xv = (List<String>)val;
75                         StringBuffer sb = new StringBuffer();
76                         String sep = "";
77                         for (Object o: xv) {
78                                 String rv = (String)o;
79                                 sb.append(sep).append(DBFieldHandler.fesc(rv));
80                                 sep = ",";
81                         }
82                         ps.setString(index, sb.toString());
83                 }
84         }
85         private static class TopicReplicationTypeHandler implements DBFieldHandler.SqlOp {
86                 public Object get(ResultSet rs, int index) throws Exception {
87                         int val = rs.getInt(index);
88
89                         return (ReplicationType.valueOf(val));
90                 }
91                 public void set(PreparedStatement ps, int index, Object val) throws Exception {
92                         if (val == null) {
93                                 ps.setInt(index, 0);
94                                 return;
95                         }
96                         @SuppressWarnings("unchecked")
97                         ReplicationType rep = (ReplicationType) val;
98                         ps.setInt(index, rep.getValue());
99                 }       
100         }
101         public static Singleton<Dmaap> getDmaap() {
102                 return dmaap;
103         }
104         
105
106         
107         public static Map<String, DcaeLocation> getDcaeLocations() {
108                 return dcaeLocations;
109         }
110         
111         public static Map<String, DR_Node> getDr_nodes() {
112                 return dr_nodes;
113         }
114         
115         public static Map<String, DR_Sub> getDr_subs() {
116                 return dr_subs;
117         }
118         public static Map<String, DR_Pub> getDr_pubs() {
119                 return dr_pubs;
120         }
121
122         public static Map<String, MR_Client> getMr_clients() {
123                 return mr_clients;
124         }
125
126
127         public static Map<String, MR_Cluster> getMr_clusters() {
128                 return mr_clusters;
129         }
130         
131         public static Map<String, Feed> getFeeds() {
132                 return feeds;
133         }
134         public static Map<String, Topic> getTopics() {
135                 return topics;
136         }
137         public static Map<String, MirrorMaker> getMirrorMakers() {
138                 return mirrors;
139         }
140
141         static {
142                 try {
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");
148                         try {
149                                 LoadSchema.upgrade();
150                         } catch (Exception e) {
151                                 appLogger.warn("Problem updating DB schema", e);
152                         }
153                         try {
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);
168                                 System.exit(1);
169                         }
170                 } else {
171                         appLogger.info("Data from memory");
172                         dmaap = new Singleton<Dmaap>() {
173                                 private Dmaap dmaap;
174                                 public void remove() {
175                                         dmaap = null;
176                                 }
177                                 public void init(Dmaap val) {
178                                         if (dmaap == null) {
179                                                 dmaap = val;
180                                         }
181                                 }
182                                 public Dmaap get() {
183                                         return(dmaap);
184                                 }
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());
194                                 }
195                         };
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>();
205                 }
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())) {
210
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");
216
217                         dmaap.update(dmx);
218                 }
219                 } catch (Exception e) {
220                         errorLogger.error(DmaapbcLogMessageEnum.DB_UPDATE_ERROR, e.getMessage());
221                 }
222         }
223         
224         public synchronized static String getNextClientId() {
225                 
226                 long id = System.currentTimeMillis();
227                 if ( id <= lastTime ) {
228                         id = lastTime + 1;
229                 }
230                 lastTime = id;
231                 return Long.toString(id);
232         }
233
234         
235
236
237 }