Merge "Fix NodeServlet Vulnerabilities"
[dmaap/datarouter.git] / datarouter-prov / src / main / java / org / onap / dmaap / datarouter / provisioning / Poker.java
1 /*******************************************************************************
2  * ============LICENSE_START==================================================
3  * * org.onap.dmaap
4  * * ===========================================================================
5  * * Copyright © 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  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  * *
22  ******************************************************************************/
23
24 package org.onap.dmaap.datarouter.provisioning;
25
26 import java.io.IOException;
27 import java.net.HttpURLConnection;
28 import java.net.InetAddress;
29 import java.net.MalformedURLException;
30 import java.net.URL;
31 import java.net.UnknownHostException;
32 import java.util.Arrays;
33 import java.util.HashSet;
34 import java.util.Map;
35 import java.util.Set;
36 import java.util.Timer;
37 import java.util.TimerTask;
38 import java.util.TreeSet;
39
40 import org.apache.log4j.Logger;
41 import org.json.JSONException;
42 import org.json.JSONObject;
43 import org.json.JSONTokener;
44 import org.onap.dmaap.datarouter.provisioning.beans.EgressRoute;
45 import org.onap.dmaap.datarouter.provisioning.beans.Feed;
46 import org.onap.dmaap.datarouter.provisioning.beans.Group;
47 import org.onap.dmaap.datarouter.provisioning.beans.IngressRoute;
48 import org.onap.dmaap.datarouter.provisioning.beans.NetworkRoute;
49 import org.onap.dmaap.datarouter.provisioning.beans.Parameters;
50 import org.onap.dmaap.datarouter.provisioning.beans.Subscription;
51 import org.onap.dmaap.datarouter.provisioning.utils.*;
52
53 /**
54  * This class handles the two timers (described in R1 Design Notes), and takes care of issuing the GET to each node of
55  * the URL to "poke".
56  *
57  * @author Robert Eby
58  * @version $Id: Poker.java,v 1.11 2014/01/08 16:13:47 eby Exp $
59  */
60 public class Poker extends TimerTask {
61
62     /**
63      * Template used to generate the URL to issue the GET against
64      */
65     private static final String POKE_URL_TEMPLATE = "http://%s/internal/fetchProv";
66
67     private static final Object lock = new Object();
68
69     /**
70      * This is a singleton -- there is only one Poker object in the server
71      */
72     private static Poker poker;
73
74     /**
75      * Get the singleton Poker object.
76      *
77      * @return the Poker
78      */
79     public static synchronized Poker getPoker() {
80         if (poker == null) {
81             poker = new Poker();
82         }
83         return poker;
84     }
85
86     private long timer1;
87     private long timer2;
88     private String thisPod;        // DNS name of this machine
89     private Logger logger;
90     private String provString;
91
92     private Poker() {
93         timer1 = timer2 = 0;
94         Timer rolex = new Timer();
95         logger = Logger.getLogger("org.onap.dmaap.datarouter.provisioning.internal");
96         try {
97             thisPod = InetAddress.getLocalHost().getHostName();
98         } catch (UnknownHostException e) {
99             thisPod = "*UNKNOWN*";    // not a major problem
100         }
101         provString = buildProvisioningString();
102
103         rolex.scheduleAtFixedRate(this, 0L, 1000L);    // Run once a second to check the timers
104     }
105
106     /**
107      * This method sets the two timers described in the design notes.
108      *
109      * @param t1 the first timer controls how long to wait after a provisioning request before poking each node This
110      * timer can be reset if it has not "gone off".
111      * @param t2 the second timer set the outer bound on how long to wait.  It cannot be reset.
112      */
113     public void setTimers(long t1, long t2) {
114         synchronized (lock) {
115             if (timer1 == 0 || t1 > timer1) {
116                 timer1 = t1;
117             }
118             if (timer2 == 0) {
119                 timer2 = t2;
120             }
121         }
122         if (logger.isDebugEnabled()) {
123             logger.debug("Poker timers set to " + timer1 + " and " + timer2);
124         }
125
126
127     }
128
129     /**
130      * Return the last provisioning string built.
131      *
132      * @return the last provisioning string built.
133      */
134     public String getProvisioningString() {
135         return provString;
136     }
137
138     /**
139      * The method to run at the predefined interval (once per second).  This method checks to see if either of the two
140      * timers has expired, and if so, will rebuild the provisioning string, and poke all the nodes and other PODs.  The
141      * timers are then reset to 0.
142      */
143     @Override
144     public void run() {
145         try {
146             if (timer1 > 0) {
147                 long now = System.currentTimeMillis();
148                 boolean fire = false;
149                 synchronized (lock) {
150                     if (now > timer1 || now > timer2) {
151                         timer1 = timer2 = 0;
152                         fire = true;
153                     }
154                 }
155                 if (fire) {
156                     pokeNodes();
157                 }
158             }
159         } catch (Exception e) {
160             logger.warn("PROV0020: Caught exception in Poker: " + e);
161             e.printStackTrace();
162         }
163     }
164
165     private void pokeNodes() {
166         // Rebuild the prov string
167         provString = buildProvisioningString();
168         // Only the active POD should poke nodes, etc.
169         boolean active = SynchronizerTask.getSynchronizer().isActive();
170         if (active) {
171             // Poke all the DR nodes
172             for (String n : BaseServlet.getNodes()) {
173                 pokeNode(n);
174             }
175             // Poke the pod that is not us
176             for (String n : BaseServlet.getPods()) {
177                 if (n.length() > 0 && !n.equals(thisPod)) {
178                     pokeNode(n);
179                 }
180             }
181         }
182     }
183
184     private void pokeNode(final String nodename) {
185         logger.debug("PROV0012 Poking node " + nodename + " ...");
186         String nodeUrl = String.format(POKE_URL_TEMPLATE, nodename + ":" + DB.HTTP_PORT);
187         Runnable r = () -> {
188             try {
189                 URL url = new URL(nodeUrl);
190                 HttpURLConnection conn = (HttpURLConnection) url.openConnection();
191                 conn.setConnectTimeout(60000);    //Fixes for Itrack DATARTR-3, poke timeout
192                 conn.connect();
193                 conn.getContentLength();    // Force the GET through
194                 conn.disconnect();
195             } catch (MalformedURLException e) {
196                 logger.warn("PROV0013 MalformedURLException Error poking node at " + nodeUrl + " : " + e.getMessage());
197             } catch (IOException e) {
198                 logger.warn("PROV0013 IOException Error poking node at " + nodeUrl + " : " + e.getMessage());
199             }
200         };
201         r.run();
202     }
203
204     private String buildProvisioningString() {
205         StringBuilder sb = new StringBuilder("{\n");
206
207         // Append Feeds to the string
208         String pfx = "\n";
209         sb.append("\"feeds\": [");
210         for (Feed f : Feed.getAllFeeds()) {
211             sb.append(pfx);
212             sb.append(f.asJSONObject().toString());
213             pfx = ",\n";
214         }
215         sb.append("\n],\n");
216
217         //Append groups to the string - Rally:US708115  - 1610
218         pfx = "\n";
219         sb.append("\"groups\": [");
220         for (Group s : Group.getAllgroups()) {
221             sb.append(pfx);
222             sb.append(s.asJSONObject().toString());
223             pfx = ",\n";
224         }
225         sb.append("\n],\n");
226
227         // Append Subscriptions to the string
228         pfx = "\n";
229         sb.append("\"subscriptions\": [");
230         for (Subscription s : Subscription.getAllSubscriptions()) {
231             sb.append(pfx);
232             if (s != null) {
233                 sb.append(s.asJSONObject().toString());
234             }
235             pfx = ",\n";
236         }
237         sb.append("\n],\n");
238
239         // Append Parameters to the string
240         pfx = "\n";
241         sb.append("\"parameters\": {");
242         Map<String, String> props = Parameters.getParameters();
243         Set<String> ivals = new HashSet<String>();
244         String intv = props.get("_INT_VALUES");
245         if (intv != null) {
246             ivals.addAll(Arrays.asList(intv.split("\\|")));
247         }
248         for (String key : new TreeSet<String>(props.keySet())) {
249             String v = props.get(key);
250             sb.append(pfx);
251             sb.append("  \"").append(key).append("\": ");
252             if (ivals.contains(key)) {
253                 // integer value
254                 sb.append(v);
255             } else if (key.endsWith("S")) {
256                 // Split and append array of strings
257                 String[] pp = v.split("\\|");
258                 String p2 = "";
259                 sb.append("[");
260                 for (String t : pp) {
261                     sb.append(p2).append("\"").append(quote(t)).append("\"");
262                     p2 = ",";
263                 }
264                 sb.append("]");
265             } else {
266                 sb.append("\"").append(quote(v)).append("\"");
267             }
268             pfx = ",\n";
269         }
270         sb.append("\n},\n");
271
272         // Append Routes to the string
273         pfx = "\n";
274         sb.append("\"ingress\": [");
275         for (IngressRoute in : IngressRoute.getAllIngressRoutes()) {
276             sb.append(pfx);
277             sb.append(in.asJSONObject().toString());
278             pfx = ",\n";
279         }
280         sb.append("\n],\n");
281
282         pfx = "\n";
283         sb.append("\"egress\": {");
284         for (EgressRoute eg : EgressRoute.getAllEgressRoutes()) {
285             sb.append(pfx);
286             String t = eg.asJSONObject().toString();
287             t = t.substring(1, t.length() - 1);
288             sb.append(t);
289             pfx = ",\n";
290         }
291         sb.append("\n},\n");
292
293         pfx = "\n";
294         sb.append("\"routing\": [");
295         for (NetworkRoute ne : NetworkRoute.getAllNetworkRoutes()) {
296             sb.append(pfx);
297             sb.append(ne.asJSONObject().toString());
298             pfx = ",\n";
299         }
300         sb.append("\n]");
301         sb.append("\n}");
302
303         // Convert to string and verify it is valid JSON
304         String tempProvString = sb.toString();
305         try {
306             new JSONObject(new JSONTokener(tempProvString));
307         } catch (JSONException e) {
308             logger.warn("PROV0016: Possible invalid prov string: " + e);
309         }
310         return tempProvString;
311     }
312
313     private String quote(String s) {
314         StringBuilder sb = new StringBuilder();
315         for (char ch : s.toCharArray()) {
316             if (ch == '\\' || ch == '"') {
317                 sb.append('\\');
318             }
319             sb.append(ch);
320         }
321         return sb.toString();
322     }
323 }