1 /*******************************************************************************
\r
2 * ============LICENSE_START==================================================
\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
11 * * http://www.apache.org/licenses/LICENSE-2.0
\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
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
22 ******************************************************************************/
\r
24 package com.att.research.datarouter.provisioning;
\r
26 import java.io.FileInputStream;
\r
27 import java.io.IOException;
\r
28 import java.io.InputStream;
\r
29 import java.net.HttpURLConnection;
\r
30 import java.net.InetAddress;
\r
31 import java.net.MalformedURLException;
\r
32 import java.net.URL;
\r
33 import java.net.UnknownHostException;
\r
34 import java.util.Arrays;
\r
35 import java.util.HashSet;
\r
36 import java.util.Map;
\r
37 import java.util.Properties;
\r
38 import java.util.Set;
\r
39 import java.util.Timer;
\r
40 import java.util.TimerTask;
\r
41 import java.util.TreeSet;
\r
43 import javax.servlet.ServletException;
\r
45 import org.apache.log4j.Logger;
\r
46 import org.json.JSONException;
\r
47 import org.json.JSONObject;
\r
48 import org.json.JSONTokener;
\r
50 import com.att.research.datarouter.provisioning.beans.EgressRoute;
\r
51 import com.att.research.datarouter.provisioning.beans.Feed;
\r
52 import com.att.research.datarouter.provisioning.beans.IngressRoute;
\r
53 import com.att.research.datarouter.provisioning.beans.NetworkRoute;
\r
54 import com.att.research.datarouter.provisioning.beans.Parameters;
\r
55 import com.att.research.datarouter.provisioning.beans.Subscription;
\r
56 import com.att.research.datarouter.provisioning.beans.Group; //Groups feature Rally:US708115 - 1610
\r
57 import com.att.research.datarouter.provisioning.utils.*;
\r
60 * This class handles the two timers (described in R1 Design Notes), and takes care of issuing
\r
61 * the GET to each node of the URL to "poke".
\r
63 * @author Robert Eby
\r
64 * @version $Id: Poker.java,v 1.11 2014/01/08 16:13:47 eby Exp $
\r
66 public class Poker extends TimerTask {
\r
67 /** Template used to generate the URL to issue the GET against */
\r
68 public static final String POKE_URL_TEMPLATE = "http://%s/internal/fetchProv";
\r
73 /** This is a singleton -- there is only one Poker object in the server */
\r
74 private static Poker p;
\r
77 * Get the singleton Poker object.
\r
80 public static synchronized Poker getPoker() {
\r
86 private long timer1;
\r
87 private long timer2;
\r
88 private Timer rolex;
\r
89 private String this_pod; // DNS name of this machine
\r
90 private Logger logger;
\r
91 private String provstring;
\r
94 timer1 = timer2 = 0;
\r
95 rolex = new Timer();
\r
96 logger = Logger.getLogger("com.att.research.datarouter.provisioning.internal");
\r
98 this_pod = InetAddress.getLocalHost().getHostName();
\r
99 } catch (UnknownHostException e) {
\r
100 this_pod = "*UNKNOWN*"; // not a major problem
\r
102 provstring = buildProvisioningString();
\r
104 rolex.scheduleAtFixedRate(this, 0L, 1000L); // Run once a second to check the timers
\r
108 * This method sets the two timers described in the design notes.
\r
109 * @param t1 the first timer controls how long to wait after a provisioning request before poking each node
\r
110 * This timer can be reset if it has not "gone off".
\r
111 * @param t2 the second timer set the outer bound on how long to wait. It cannot be reset.
\r
113 public void setTimers(long t1, long t2) {
\r
114 synchronized (this_pod) {
\r
115 if (timer1 == 0 || t1 > timer1)
\r
120 if (logger.isDebugEnabled())
\r
121 logger.debug("Poker timers set to " + timer1 + " and " + timer2);
\r
127 * Return the last provisioning string built.
\r
128 * @return the last provisioning string built.
\r
130 public String getProvisioningString() {
\r
135 * The method to run at the predefined interval (once per second). This method checks
\r
136 * to see if either of the two timers has expired, and if so, will rebuild the provisioning
\r
137 * string, and poke all the nodes and other PODs. The timers are then reset to 0.
\r
140 public void run() {
\r
143 long now = System.currentTimeMillis();
\r
144 boolean fire = false;
\r
145 synchronized (this_pod) {
\r
146 if (now > timer1 || now > timer2) {
\r
147 timer1 = timer2 = 0;
\r
152 // Rebuild the prov string
\r
153 provstring = buildProvisioningString();
\r
155 // Only the active POD should poke nodes, etc.
\r
156 boolean active = SynchronizerTask.getSynchronizer().isActive();
\r
158 // Poke all the DR nodes
\r
159 for (String n : BaseServlet.getNodes()) {
\r
162 // Poke the pod that is not us
\r
163 for (String n : BaseServlet.getPods()) {
\r
164 if (n.length() > 0 && !n.equals(this_pod))
\r
170 } catch (Exception e) {
\r
171 logger.warn("PROV0020: Caught exception in Poker: "+e);
\r
172 e.printStackTrace();
\r
175 private void pokeNode(final String nodename) {
\r
176 logger.debug("PROV0012 Poking node " + nodename + " ...");
\r
177 Runnable r = new Runnable() {
\r
179 public void run() {
\r
182 String u = String.format(POKE_URL_TEMPLATE, nodename+":"+DB.HTTP_PORT);
\r
183 URL url = new URL(u);
\r
184 HttpURLConnection conn = (HttpURLConnection) url.openConnection();
\r
185 conn.setConnectTimeout(60000); //Fixes for Itrack DATARTR-3, poke timeout
\r
187 conn.getContentLength(); // Force the GET through
\r
189 } catch (MalformedURLException e) {
\r
190 logger.warn("PROV0013 MalformedURLException Error poking node "+nodename+": " + e.getMessage());
\r
191 } catch (IOException e) {
\r
192 logger.warn("PROV0013 IOException Error poking node "+nodename+": " + e.getMessage());
\r
196 // Thread t = new Thread(r);
\r
200 @SuppressWarnings("unused")
\r
201 private String buildProvisioningString() {
\r
202 StringBuilder sb = new StringBuilder("{\n");
\r
204 // Append Feeds to the string
\r
206 sb.append("\"feeds\": [");
\r
207 for (Feed f : Feed.getAllFeeds()) {
\r
209 sb.append(f.asJSONObject().toString());
\r
212 sb.append("\n],\n");
\r
214 //Append groups to the string - Rally:US708115 - 1610
\r
216 sb.append("\"groups\": [");
\r
217 for (Group s : Group.getAllgroups()) {
\r
219 sb.append(s.asJSONObject().toString());
\r
222 sb.append("\n],\n");
\r
225 // Append Subscriptions to the string
\r
227 sb.append("\"subscriptions\": [");
\r
228 for (Subscription s : Subscription.getAllSubscriptions()) {
\r
231 sb.append(s.asJSONObject().toString());
\r
234 sb.append("\n],\n");
\r
236 // Append Parameters to the string
\r
238 sb.append("\"parameters\": {");
\r
239 Map<String,String> props = Parameters.getParameters();
\r
240 Set<String> ivals = new HashSet<String>();
\r
241 String intv = props.get("_INT_VALUES");
\r
243 ivals.addAll(Arrays.asList(intv.split("\\|")));
\r
244 for (String key : new TreeSet<String>(props.keySet())) {
\r
245 String v = props.get(key);
\r
247 sb.append(" \"").append(key).append("\": ");
\r
248 if (ivals.contains(key)) {
\r
251 } else if (key.endsWith("S")) {
\r
252 // Split and append array of strings
\r
253 String[] pp = v.split("\\|");
\r
256 for (String t : pp) {
\r
257 sb.append(p2).append("\"").append(quote(t)).append("\"");
\r
262 sb.append("\"").append(quote(v)).append("\"");
\r
266 sb.append("\n},\n");
\r
268 // Append Routes to the string
\r
270 sb.append("\"ingress\": [");
\r
271 for (IngressRoute in : IngressRoute.getAllIngressRoutes()) {
\r
273 sb.append(in.asJSONObject().toString());
\r
276 sb.append("\n],\n");
\r
279 sb.append("\"egress\": {");
\r
280 for (EgressRoute eg : EgressRoute.getAllEgressRoutes()) {
\r
282 String t = eg.asJSONObject().toString();
\r
283 t = t.substring(1, t.length()-1);
\r
287 sb.append("\n},\n");
\r
290 sb.append("\"routing\": [");
\r
291 for (NetworkRoute ne : NetworkRoute.getAllNetworkRoutes()) {
\r
293 sb.append(ne.asJSONObject().toString());
\r
299 // Convert to string and verify it is valid JSON
\r
300 String provstring = sb.toString();
\r
302 new JSONObject(new JSONTokener(provstring));
\r
303 } catch (JSONException e) {
\r
304 logger.warn("PROV0016: Possible invalid prov string: "+e);
\r
308 private String quote(String s) {
\r
309 StringBuilder sb = new StringBuilder();
\r
310 for (char ch : s.toCharArray()) {
\r
311 if (ch == '\\' || ch == '"') {
\r
316 return sb.toString();
\r