[DMAAP-48] Initial code import
[dmaap/datarouter.git] / datarouter-prov / src / main / java / com / att / research / datarouter / provisioning / SynchronizerTask.java
1 /*******************************************************************************\r
2  * ============LICENSE_START==================================================\r
3  * * org.onap.dmaap\r
4  * * ===========================================================================\r
5  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * * ===========================================================================\r
7  * * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * * you may not use this file except in compliance with the License.\r
9  * * You may obtain a copy of the License at\r
10  * * \r
11  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * * \r
13  *  * Unless required by applicable law or agreed to in writing, software\r
14  * * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * * See the License for the specific language governing permissions and\r
17  * * limitations under the License.\r
18  * * ============LICENSE_END====================================================\r
19  * *\r
20  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
21  * *\r
22  ******************************************************************************/\r
23 \r
24 \r
25 package com.att.research.datarouter.provisioning;\r
26 \r
27 import java.io.ByteArrayOutputStream;\r
28 import java.io.File;\r
29 import java.io.FileInputStream;\r
30 import java.io.InputStream;\r
31 import java.net.InetAddress;\r
32 import java.net.UnknownHostException;\r
33 import java.nio.file.Files;\r
34 import java.nio.file.Path;\r
35 import java.nio.file.Paths;\r
36 import java.nio.file.StandardCopyOption;\r
37 import java.security.KeyStore;\r
38 import java.sql.Connection;\r
39 import java.sql.SQLException;\r
40 import java.util.ArrayList;\r
41 import java.util.Arrays;\r
42 import java.util.Collection;\r
43 import java.util.HashMap;\r
44 import java.util.Map;\r
45 import java.util.Properties;\r
46 import java.util.Set;\r
47 import java.util.Timer;\r
48 import java.util.TimerTask;\r
49 import java.util.TreeSet;\r
50 \r
51 import javax.servlet.http.HttpServletResponse;\r
52 \r
53 import org.apache.http.HttpEntity;\r
54 import org.apache.http.HttpResponse;\r
55 import org.apache.http.client.methods.HttpGet;\r
56 import org.apache.http.client.methods.HttpPost;\r
57 import org.apache.http.conn.scheme.Scheme;\r
58 import org.apache.http.conn.ssl.SSLSocketFactory;\r
59 import org.apache.http.entity.ByteArrayEntity;\r
60 import org.apache.http.entity.ContentType;\r
61 import org.apache.http.impl.client.AbstractHttpClient;\r
62 import org.apache.http.impl.client.DefaultHttpClient;\r
63 import org.apache.log4j.Logger;\r
64 import org.json.JSONArray;\r
65 import org.json.JSONException;\r
66 import org.json.JSONObject;\r
67 import org.json.JSONTokener;\r
68 \r
69 import com.att.research.datarouter.provisioning.beans.EgressRoute;\r
70 import com.att.research.datarouter.provisioning.beans.Feed;\r
71 import com.att.research.datarouter.provisioning.beans.IngressRoute;\r
72 import com.att.research.datarouter.provisioning.beans.NetworkRoute;\r
73 import com.att.research.datarouter.provisioning.beans.Parameters;\r
74 import com.att.research.datarouter.provisioning.beans.Subscription;\r
75 import com.att.research.datarouter.provisioning.beans.Syncable;\r
76 import com.att.research.datarouter.provisioning.utils.DB;\r
77 import com.att.research.datarouter.provisioning.utils.RLEBitSet;\r
78 import com.att.research.datarouter.provisioning.utils.LogfileLoader;\r
79 import com.att.research.datarouter.provisioning.utils.URLUtilities;\r
80 import com.att.research.datarouter.provisioning.beans.Group; //Groups feature Rally:US708115 - 1610     \r
81 \r
82 /**\r
83  * This class handles synchronization between provisioning servers (PODs).  It has three primary functions:\r
84  * <ol>\r
85  * <li>Checking DNS once per minute to see which POD the DNS CNAME points to. The CNAME will point to\r
86  * the active (master) POD.</li>\r
87  * <li>On non-master (standby) PODs, fetches provisioning data and logs in order to keep MySQL in sync.</li>\r
88  * <li>Providing information to other parts of the system as to the current role (ACTIVE, STANDBY, UNKNOWN)\r
89  * of this POD.</li>\r
90  * </ol>\r
91  * <p>For this to work correctly, the following code needs to be placed at the beginning of main().</p>\r
92  * <code>\r
93  *              Security.setProperty("networkaddress.cache.ttl", "10");\r
94  * </code>\r
95  *\r
96  * @author Robert Eby\r
97  * @version $Id: SynchronizerTask.java,v 1.10 2014/03/21 13:50:10 eby Exp $\r
98  */\r
99 public class SynchronizerTask extends TimerTask {\r
100         /** This is a singleton -- there is only one SynchronizerTask object in the server */\r
101         private static SynchronizerTask synctask;\r
102 \r
103         /** This POD is unknown -- not on the list of PODs */\r
104         public static final int UNKNOWN = 0;\r
105         /** This POD is active -- on the list of PODs, and the DNS CNAME points to us */\r
106         public static final int ACTIVE = 1;\r
107         /** This POD is standby -- on the list of PODs, and the DNS CNAME does not point to us */\r
108         public static final int STANDBY = 2;\r
109         private static final String[] stnames = { "UNKNOWN", "ACTIVE", "STANDBY" };\r
110         private static final long ONE_HOUR = 60 * 60 * 1000L;\r
111 \r
112         private final Logger logger;\r
113         private final Timer rolex;\r
114         private final String spooldir;\r
115         private int state;\r
116         private boolean doFetch;\r
117         private long nextsynctime;\r
118         private AbstractHttpClient httpclient = null;\r
119 \r
120         /**\r
121          * Get the singleton SynchronizerTask object.\r
122          * @return the SynchronizerTask\r
123          */\r
124         public static synchronized SynchronizerTask getSynchronizer() {\r
125                 if (synctask == null)\r
126                         synctask = new SynchronizerTask();\r
127                 return synctask;\r
128         }\r
129 \r
130         @SuppressWarnings("deprecation")\r
131         private SynchronizerTask() {\r
132                 logger = Logger.getLogger("com.att.research.datarouter.provisioning.internal");\r
133                 rolex = new Timer();\r
134                 spooldir = (new DB()).getProperties().getProperty("com.att.research.datarouter.provserver.spooldir");\r
135                 state = UNKNOWN;\r
136                 doFetch = true;         // start off with a fetch\r
137                 nextsynctime = 0;\r
138 \r
139                 logger.info("PROV5000: Sync task starting, server state is UNKNOWN");\r
140                 try {\r
141                         Properties props = (new DB()).getProperties();\r
142                         String type  = props.getProperty(Main.KEYSTORE_TYPE_PROPERTY, "jks");\r
143                         String store = props.getProperty(Main.KEYSTORE_PATH_PROPERTY);\r
144                         String pass  = props.getProperty(Main.KEYSTORE_PASSWORD_PROPERTY);\r
145                         KeyStore keyStore = KeyStore.getInstance(type);\r
146                         FileInputStream instream = new FileInputStream(new File(store));\r
147                         keyStore.load(instream, pass.toCharArray());\r
148                         instream.close();\r
149 \r
150                         store = props.getProperty(Main.TRUSTSTORE_PATH_PROPERTY);\r
151                         pass  = props.getProperty(Main.TRUSTSTORE_PASSWORD_PROPERTY);\r
152                         KeyStore trustStore = null;\r
153                         if (store != null && store.length() > 0) {\r
154                                 trustStore = KeyStore.getInstance(KeyStore.getDefaultType());\r
155                                 instream = new FileInputStream(new File(store));\r
156                                 trustStore.load(instream, pass.toCharArray());\r
157                                 instream.close();\r
158                         }\r
159 \r
160                         // We are connecting with the node name, but the certificate will have the CNAME\r
161                         // So we need to accept a non-matching certificate name\r
162                         String keystorepass  = props.getProperty(Main.KEYSTORE_PASSWORD_PROPERTY); //itrack.web.att.com/browse/DATARTR-6 for changing hard coded passphase ref\r
163                         AbstractHttpClient hc = new DefaultHttpClient();\r
164                         SSLSocketFactory socketFactory =\r
165                                 (trustStore == null)\r
166                                 ? new SSLSocketFactory(keyStore, keystorepass)\r
167                                 : new SSLSocketFactory(keyStore, keystorepass, trustStore);\r
168                         socketFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);\r
169                         Scheme sch = new Scheme("https", 443, socketFactory);\r
170                         hc.getConnectionManager().getSchemeRegistry().register(sch);\r
171                         httpclient = hc;\r
172 \r
173                         // Run once every 5 seconds to check DNS, etc.\r
174                         long interval = 0;\r
175                         try {\r
176                                 String s = props.getProperty("com.att.research.datarouter.provserver.sync_interval", "5000");\r
177                                 interval = Long.parseLong(s);\r
178                         } catch (NumberFormatException e) {\r
179                                 interval = 5000L;\r
180                         }\r
181                         rolex.scheduleAtFixedRate(this, 0L, interval);\r
182                 } catch (Exception e) {\r
183                         logger.warn("PROV5005: Problem starting the synchronizer: "+e);\r
184                 }\r
185         }\r
186 \r
187         /**\r
188          * What is the state of this POD?\r
189          * @return one of ACTIVE, STANDBY, UNKNOWN\r
190          */\r
191         public int getState() {\r
192                 return state;\r
193         }\r
194 \r
195         /**\r
196          * Is this the active POD?\r
197          * @return true if we are active (the master), false otherwise\r
198          */\r
199         public boolean isActive() {\r
200                 return state == ACTIVE;\r
201         }\r
202 \r
203         /**\r
204          * This method is used to signal that another POD (the active POD) has sent us a /fetchProv request,\r
205          * and that we should re-synchronize with the master.\r
206          */\r
207         public void doFetch() {\r
208                 doFetch = true;\r
209         }\r
210 \r
211         /**\r
212          * Runs once a minute in order to <ol>\r
213          * <li>lookup DNS names,</li>\r
214          * <li>determine the state of this POD,</li>\r
215          * <li>if this is a standby POD, and the fetch flag is set, perform a fetch of state from the active POD.</li>\r
216          * <li>if this is a standby POD, check if there are any new log records to be replicated.</li>\r
217          * </ol>\r
218          */\r
219         @Override\r
220         public void run() {\r
221                 try {\r
222                         state = lookupState();\r
223                         if (state == STANDBY) {\r
224                                 // Only copy provisioning data FROM the active server TO the standby\r
225                                 if (doFetch || (System.currentTimeMillis() >= nextsynctime)) {\r
226                                         logger.debug("Initiating a sync...");\r
227                                         JSONObject jo = readProvisioningJSON();\r
228                                         if (jo != null) {\r
229                                                 doFetch = false;\r
230                                                 syncFeeds( jo.getJSONArray("feeds"));\r
231                                                 syncSubs(  jo.getJSONArray("subscriptions"));\r
232                                                 syncGroups(  jo.getJSONArray("groups")); //Rally:US708115 - 1610\r
233                                                 syncParams(jo.getJSONObject("parameters"));\r
234                                                 // The following will not be present in a version=1.0 provfeed\r
235                                                 JSONArray ja = jo.optJSONArray("ingress");\r
236                                                 if (ja != null)\r
237                                                         syncIngressRoutes(ja);\r
238                                                 JSONObject j2 = jo.optJSONObject("egress");\r
239                                                 if (j2 != null)\r
240                                                         syncEgressRoutes( j2);\r
241                                                 ja = jo.optJSONArray("routing");\r
242                                                 if (ja != null)\r
243                                                         syncNetworkRoutes(ja);\r
244                                         }\r
245                                         logger.info("PROV5013: Sync completed.");\r
246                                         nextsynctime = System.currentTimeMillis() + ONE_HOUR;\r
247                                 }\r
248                         } else {\r
249                                 // Don't do fetches on non-standby PODs\r
250                                 doFetch = false;\r
251                         }\r
252 \r
253                         // Fetch DR logs as needed - server to server\r
254                         LogfileLoader lfl = LogfileLoader.getLoader();\r
255                         if (lfl.isIdle()) {\r
256                                 // Only fetch new logs if the loader is waiting for them.\r
257                                 logger.trace("Checking for logs to replicate...");\r
258                                 RLEBitSet local  = lfl.getBitSet();\r
259                                 RLEBitSet remote = readRemoteLoglist();\r
260                                 remote.andNot(local);\r
261                                 if (!remote.isEmpty()) {\r
262                                         logger.debug(" Replicating logs: "+remote);\r
263                                         replicateDRLogs(remote);\r
264                                 }\r
265                         }\r
266                 } catch (Exception e) {\r
267                         logger.warn("PROV0020: Caught exception in SynchronizerTask: "+e);\r
268                         e.printStackTrace();\r
269                 }\r
270         }\r
271 \r
272         /**\r
273          * This method is used to lookup the CNAME that points to the active server.\r
274          * It returns 0 (UNKNOWN), 1(ACTIVE), or 2 (STANDBY) to indicate the state of this server.\r
275          * @return the current state\r
276          */\r
277         private int lookupState() {\r
278                 int newstate = UNKNOWN;\r
279                 try {\r
280                         InetAddress myaddr = InetAddress.getLocalHost();\r
281                         if (logger.isTraceEnabled())\r
282                                 logger.trace("My address: "+myaddr);\r
283                         String this_pod = myaddr.getHostName();\r
284                         Set<String> pods = new TreeSet<String>(Arrays.asList(BaseServlet.getPods()));\r
285                         if (pods.contains(this_pod)) {\r
286                                 InetAddress pserver = InetAddress.getByName(BaseServlet.active_prov_name);\r
287                                 newstate = myaddr.equals(pserver) ? ACTIVE : STANDBY;\r
288                                 if (logger.isDebugEnabled() && System.currentTimeMillis() >= next_msg) {\r
289                                         logger.debug("Active POD = "+pserver+", Current state is "+stnames[newstate]);\r
290                                         next_msg = System.currentTimeMillis() + (5 * 60 * 1000L);\r
291                                 }\r
292                         } else {\r
293                                 logger.warn("PROV5003: My name ("+this_pod+") is missing from the list of provisioning servers.");\r
294                         }\r
295                 } catch (UnknownHostException e) {\r
296                         logger.warn("PROV5002: Cannot determine the name of this provisioning server.");\r
297                 }\r
298 \r
299                 if (newstate != state)\r
300                         logger.info(String.format("PROV5001: Server state changed from %s to %s", stnames[state], stnames[newstate]));\r
301                 return newstate;\r
302         }\r
303         private static long next_msg = 0;       // only display the "Current state" msg every 5 mins.\r
304         /** Synchronize the Feeds in the JSONArray, with the Feeds in the DB. */\r
305         private void syncFeeds(JSONArray ja) {\r
306                 Collection<Syncable> coll = new ArrayList<Syncable>();\r
307                 for (int n = 0; n < ja.length(); n++) {\r
308                         try {\r
309                                 Feed f = new Feed(ja.getJSONObject(n));\r
310                                 coll.add(f);\r
311                         } catch (Exception e) {\r
312                                 logger.warn("PROV5004: Invalid object in feed: "+ja.optJSONObject(n));\r
313                         }\r
314                 }\r
315                 if (sync(coll, Feed.getAllFeeds()))\r
316                         BaseServlet.provisioningDataChanged();\r
317         }\r
318         /** Synchronize the Subscriptions in the JSONArray, with the Subscriptions in the DB. */\r
319         private void syncSubs(JSONArray ja) {\r
320                 Collection<Syncable> coll = new ArrayList<Syncable>();\r
321                 for (int n = 0; n < ja.length(); n++) {\r
322                         try {\r
323                                 //Data Router Subscriber HTTPS Relaxation feature USERSTORYID:US674047.\r
324                                 JSONObject j = ja.getJSONObject(n);      \r
325                                 j.put("sync", "true");\r
326                                 Subscription s = new Subscription(j);\r
327                                 coll.add(s);\r
328                         } catch (Exception e) {\r
329                                 logger.warn("PROV5004: Invalid object in subscription: "+ja.optJSONObject(n));\r
330                         }\r
331                 }\r
332                 if (sync(coll, Subscription.getAllSubscriptions()))\r
333                         BaseServlet.provisioningDataChanged();\r
334         }\r
335 \r
336         /**  Rally:US708115  - Synchronize the Groups in the JSONArray, with the Groups in the DB. */           \r
337         private void syncGroups(JSONArray ja) {         \r
338                 Collection<Syncable> coll = new ArrayList<Syncable>();          \r
339                 for (int n = 0; n < ja.length(); n++) {         \r
340                         try {           \r
341                                 Group g = new Group(ja.getJSONObject(n));               \r
342                                 coll.add(g);            \r
343                         } catch (Exception e) {         \r
344                                 logger.warn("PROV5004: Invalid object in subscription: "+ja.optJSONObject(n));          \r
345                         }               \r
346                 }               \r
347                 if (sync(coll, Group.getAllgroups()))           \r
348                         BaseServlet.provisioningDataChanged();          \r
349         }\r
350 \r
351 \r
352         /** Synchronize the Parameters in the JSONObject, with the Parameters in the DB. */\r
353         private void syncParams(JSONObject jo) {\r
354                 Collection<Syncable> coll = new ArrayList<Syncable>();\r
355                 for (String k : jo.keySet()) {\r
356                         String v = "";\r
357                         try {\r
358                                 v = jo.getString(k);\r
359                         } catch (JSONException e) {\r
360                                 try {\r
361                                         v = ""+jo.getInt(k);\r
362                                 } catch (JSONException e1) {\r
363                                         JSONArray ja = jo.getJSONArray(k);\r
364                                         for (int i = 0; i < ja.length(); i++) {\r
365                                                 if (i > 0)\r
366                                                         v += "|";\r
367                                                 v += ja.getString(i);\r
368                                         }\r
369                                 }\r
370                         }\r
371                         coll.add(new Parameters(k, v));\r
372                 }\r
373                 if (sync(coll, Parameters.getParameterCollection())) {\r
374                         BaseServlet.provisioningDataChanged();\r
375                         BaseServlet.provisioningParametersChanged();\r
376                 }\r
377         }\r
378         private void syncIngressRoutes(JSONArray ja) {\r
379                 Collection<Syncable> coll = new ArrayList<Syncable>();\r
380                 for (int n = 0; n < ja.length(); n++) {\r
381                         try {\r
382                                 IngressRoute in = new IngressRoute(ja.getJSONObject(n));\r
383                                 coll.add(in);\r
384                         } catch (NumberFormatException e) {\r
385                                 logger.warn("PROV5004: Invalid object in ingress routes: "+ja.optJSONObject(n));\r
386                         }\r
387                 }\r
388                 if (sync(coll, IngressRoute.getAllIngressRoutes()))\r
389                         BaseServlet.provisioningDataChanged();\r
390         }\r
391         private void syncEgressRoutes(JSONObject jo) {\r
392                 Collection<Syncable> coll = new ArrayList<Syncable>();\r
393                 for (String key : jo.keySet()) {\r
394                         try {\r
395                                 int sub = Integer.parseInt(key);\r
396                                 String node = jo.getString(key);\r
397                                 EgressRoute er = new EgressRoute(sub, node);\r
398                                 coll.add(er);\r
399                         } catch (NumberFormatException e) {\r
400                                 logger.warn("PROV5004: Invalid subid in egress routes: "+key);\r
401                         } catch (IllegalArgumentException e) {\r
402                                 logger.warn("PROV5004: Invalid node name in egress routes: "+key);\r
403                         }\r
404                 }\r
405                 if (sync(coll, EgressRoute.getAllEgressRoutes()))\r
406                         BaseServlet.provisioningDataChanged();\r
407         }\r
408         private void syncNetworkRoutes(JSONArray ja) {\r
409                 Collection<Syncable> coll = new ArrayList<Syncable>();\r
410                 for (int n = 0; n < ja.length(); n++) {\r
411                         try {\r
412                                 NetworkRoute nr = new NetworkRoute(ja.getJSONObject(n));\r
413                                 coll.add(nr);\r
414                         } catch (JSONException e) {\r
415                                 logger.warn("PROV5004: Invalid object in network routes: "+ja.optJSONObject(n));\r
416                         }\r
417                 }\r
418                 if (sync(coll, NetworkRoute.getAllNetworkRoutes()))\r
419                         BaseServlet.provisioningDataChanged();\r
420         }\r
421         private boolean sync(Collection<? extends Syncable> newc, Collection<? extends Syncable> oldc) {\r
422                 boolean changes = false;\r
423                 try {\r
424                         Map<String, Syncable> newmap = getMap(newc);\r
425                         Map<String, Syncable> oldmap = getMap(oldc);\r
426                         Set<String> union = new TreeSet<String>(newmap.keySet());\r
427                         union.addAll(oldmap.keySet());\r
428                         DB db = new DB();\r
429                         @SuppressWarnings("resource")\r
430                         Connection conn = db.getConnection();\r
431                         for (String n : union) {\r
432                                 Syncable newobj = newmap.get(n);\r
433                                 Syncable oldobj = oldmap.get(n);\r
434                                 if (oldobj == null) {\r
435                                         if (logger.isDebugEnabled())\r
436                                                 logger.debug("  Inserting record: "+newobj);\r
437                                         newobj.doInsert(conn);\r
438                                         changes = true;\r
439                                 } else if (newobj == null) {\r
440                                         if (logger.isDebugEnabled())\r
441                                                 logger.debug("  Deleting record: "+oldobj);\r
442                                         oldobj.doDelete(conn);\r
443                                         changes = true;\r
444                                 } else if (!newobj.equals(oldobj)) {\r
445                                         if (logger.isDebugEnabled())\r
446                                                 logger.debug("  Updating record: "+newobj);\r
447                                         newobj.doUpdate(conn);\r
448 \r
449                                         /**Rally US708115\r
450                                          * Change Ownership of FEED - 1610, Syncronised with secondary DB.\r
451                                          * */\r
452                                         checkChnageOwner(newobj, oldobj);\r
453 \r
454                                         changes = true;\r
455                                 }\r
456                         }\r
457                         db.release(conn);\r
458                 } catch (SQLException e) {\r
459                         logger.warn("PROV5009: problem during sync, exception: "+e);\r
460                         e.printStackTrace();\r
461                 }\r
462                 return changes;\r
463         }\r
464         private Map<String, Syncable> getMap(Collection<? extends Syncable> c) {\r
465                 Map<String, Syncable> map = new HashMap<String, Syncable>();\r
466                 for (Syncable v : c) {\r
467                         map.put(v.getKey(), v);\r
468                 }\r
469                 return map;\r
470         }\r
471         \r
472 \r
473         /**Change owner of FEED/SUBSCRIPTION*/\r
474         /**Rally US708115\r
475          * Change Ownership of FEED - 1610\r
476          * \r
477          * */\r
478         private void checkChnageOwner(Syncable newobj, Syncable oldobj) {\r
479                 if(newobj instanceof Feed) {\r
480                         Feed oldfeed = (Feed) oldobj;\r
481                         Feed newfeed = (Feed) newobj;\r
482                         \r
483                         if(!oldfeed.getPublisher().equals(newfeed.getPublisher())){\r
484                                 logger.info("PROV5013 -  Previous publisher: "+oldfeed.getPublisher() +": New publisher-"+newfeed.getPublisher());\r
485                                 oldfeed.setPublisher(newfeed.getPublisher());\r
486                                 oldfeed.changeOwnerShip();\r
487                         }\r
488                 }\r
489                 else if(newobj instanceof Subscription) {\r
490                         Subscription oldsub = (Subscription) oldobj;\r
491                         Subscription newsub = (Subscription) newobj;\r
492                         \r
493                         if(!oldsub.getSubscriber().equals(newsub.getSubscriber())){\r
494                                 logger.info("PROV5013 -  Previous subscriber: "+oldsub.getSubscriber() +": New subscriber-"+newsub.getSubscriber());\r
495                                 oldsub.setSubscriber(newsub.getSubscriber());\r
496                                 oldsub.changeOwnerShip();\r
497                         }\r
498                 }\r
499                 \r
500         }\r
501 \r
502         /**\r
503          * Issue a GET on the peer POD's /internal/prov/ URL to get a copy of its provisioning data.\r
504          * @return the provisioning data (as a JONObject)\r
505          */\r
506         private synchronized JSONObject readProvisioningJSON() {\r
507                 String url  = URLUtilities.generatePeerProvURL();\r
508                 HttpGet get = new HttpGet(url);\r
509                 try {\r
510                         HttpResponse response = httpclient.execute(get);\r
511                         int code = response.getStatusLine().getStatusCode();\r
512                         if (code != HttpServletResponse.SC_OK) {\r
513                                 logger.warn("PROV5010: readProvisioningJSON failed, bad error code: "+code);\r
514                                 return null;\r
515                         }\r
516                         HttpEntity entity = response.getEntity();\r
517                         String ctype = entity.getContentType().getValue().trim();\r
518                         if (!ctype.equals(BaseServlet.PROVFULL_CONTENT_TYPE1) && !ctype.equals(BaseServlet.PROVFULL_CONTENT_TYPE2)) {\r
519                                 logger.warn("PROV5011: readProvisioningJSON failed, bad content type: "+ctype);\r
520                                 return null;\r
521                         }\r
522                         return new JSONObject(new JSONTokener(entity.getContent()));\r
523                 } catch (Exception e) {\r
524                         logger.warn("PROV5012: readProvisioningJSON failed, exception: "+e);\r
525                         return null;\r
526                 } finally {\r
527                         get.releaseConnection();\r
528                 }\r
529         }\r
530         /**\r
531          * Issue a GET on the peer POD's /internal/drlogs/ URL to get an RELBitSet representing the\r
532          * log records available in the remote database.\r
533          * @return the bitset\r
534          */\r
535         private RLEBitSet readRemoteLoglist() {\r
536                 RLEBitSet bs = new RLEBitSet();\r
537                 String url  = URLUtilities.generatePeerLogsURL();\r
538 \r
539                 //Fixing if only one Prov is configured, not to give exception to fill logs, return empty bitset.\r
540                 if(url.equals("")) {\r
541                         return bs;\r
542                 }\r
543                 //End of fix.\r
544 \r
545                 HttpGet get = new HttpGet(url);\r
546                 try {\r
547                         HttpResponse response = httpclient.execute(get);\r
548                         int code = response.getStatusLine().getStatusCode();\r
549                         if (code != HttpServletResponse.SC_OK) {\r
550                                 logger.warn("PROV5010: readRemoteLoglist failed, bad error code: "+code);\r
551                                 return bs;\r
552                         }\r
553                         HttpEntity entity = response.getEntity();\r
554                         String ctype = entity.getContentType().getValue().trim();\r
555                         if (!ctype.equals("text/plain")) {\r
556                                 logger.warn("PROV5011: readRemoteLoglist failed, bad content type: "+ctype);\r
557                                 return bs;\r
558                         }\r
559                         InputStream is = entity.getContent();\r
560                         ByteArrayOutputStream bos = new ByteArrayOutputStream();\r
561                         int ch = 0;\r
562                         while ((ch = is.read()) >= 0)\r
563                                 bos.write(ch);\r
564                         bs.set(bos.toString());\r
565                         is.close();\r
566                 } catch (Exception e) {\r
567                         logger.warn("PROV5012: readRemoteLoglist failed, exception: "+e);\r
568                         return bs;\r
569                 } finally {\r
570                         get.releaseConnection();\r
571                 }\r
572                 return bs;\r
573         }\r
574         /**\r
575          * Issue a POST on the peer POD's /internal/drlogs/ URL to fetch log records available\r
576          * in the remote database that we wish to copy to the local database.\r
577          * @param bs the bitset (an RELBitSet) of log records to fetch\r
578          */\r
579         private void replicateDRLogs(RLEBitSet bs) {\r
580                 String url  = URLUtilities.generatePeerLogsURL();\r
581                 HttpPost post = new HttpPost(url);\r
582                 try {\r
583                         String t = bs.toString();\r
584                         HttpEntity body = new ByteArrayEntity(t.getBytes(), ContentType.create("text/plain"));\r
585                         post.setEntity(body);\r
586                         if (logger.isDebugEnabled())\r
587                                 logger.debug("Requesting records: "+t);\r
588 \r
589                         HttpResponse response = httpclient.execute(post);\r
590                         int code = response.getStatusLine().getStatusCode();\r
591                         if (code != HttpServletResponse.SC_OK) {\r
592                                 logger.warn("PROV5010: replicateDRLogs failed, bad error code: "+code);\r
593                                 return;\r
594                         }\r
595                         HttpEntity entity = response.getEntity();\r
596                         String ctype = entity.getContentType().getValue().trim();\r
597                         if (!ctype.equals("text/plain")) {\r
598                                 logger.warn("PROV5011: replicateDRLogs failed, bad content type: "+ctype);\r
599                                 return;\r
600                         }\r
601 \r
602                         String spoolname = "" + System.currentTimeMillis();\r
603                         Path tmppath = Paths.get(spooldir, spoolname);\r
604                         Path donepath = Paths.get(spooldir, "IN."+spoolname);\r
605                         Files.copy(entity.getContent(), Paths.get(spooldir, spoolname), StandardCopyOption.REPLACE_EXISTING);\r
606                         Files.move(tmppath, donepath, StandardCopyOption.REPLACE_EXISTING);\r
607                         logger.info("Approximately "+bs.cardinality()+" records replicated.");\r
608                 } catch (Exception e) {\r
609                         logger.warn("PROV5012: replicateDRLogs failed, exception: "+e);\r
610                 } finally {\r
611                         post.releaseConnection();\r
612                 }\r
613         }\r
614 }\r