1 /*******************************************************************************
2 * ============LICENSE_START==================================================
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
11 * * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 ******************************************************************************/
24 package org.onap.dmaap.datarouter.provisioning;
26 import java.io.FileInputStream;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.net.HttpURLConnection;
30 import java.net.InetAddress;
31 import java.net.MalformedURLException;
33 import java.net.UnknownHostException;
34 import java.util.Arrays;
35 import java.util.HashSet;
37 import java.util.Properties;
39 import java.util.Timer;
40 import java.util.TimerTask;
41 import java.util.TreeSet;
43 import javax.servlet.ServletException;
45 import org.apache.log4j.Logger;
46 import org.json.JSONException;
47 import org.json.JSONObject;
48 import org.json.JSONTokener;
49 import org.onap.dmaap.datarouter.provisioning.beans.EgressRoute;
50 import org.onap.dmaap.datarouter.provisioning.beans.Feed;
51 import org.onap.dmaap.datarouter.provisioning.beans.Group;
52 import org.onap.dmaap.datarouter.provisioning.beans.IngressRoute;
53 import org.onap.dmaap.datarouter.provisioning.beans.NetworkRoute;
54 import org.onap.dmaap.datarouter.provisioning.beans.Parameters;
55 import org.onap.dmaap.datarouter.provisioning.beans.Subscription;
56 import org.onap.dmaap.datarouter.provisioning.utils.*;
59 * This class handles the two timers (described in R1 Design Notes), and takes care of issuing
60 * the GET to each node of the URL to "poke".
63 * @version $Id: Poker.java,v 1.11 2014/01/08 16:13:47 eby Exp $
65 public class Poker extends TimerTask {
66 /** Template used to generate the URL to issue the GET against */
67 public static final String POKE_URL_TEMPLATE = "http://%s/internal/fetchProv";
72 /** This is a singleton -- there is only one Poker object in the server */
73 private static Poker p;
76 * Get the singleton Poker object.
79 public static synchronized Poker getPoker() {
88 private String this_pod; // DNS name of this machine
89 private Logger logger;
90 private String provstring;
95 logger = Logger.getLogger("org.onap.dmaap.datarouter.provisioning.internal");
97 this_pod = InetAddress.getLocalHost().getHostName();
98 } catch (UnknownHostException e) {
99 this_pod = "*UNKNOWN*"; // not a major problem
101 provstring = buildProvisioningString();
103 rolex.scheduleAtFixedRate(this, 0L, 1000L); // Run once a second to check the timers
107 * This method sets the two timers described in the design notes.
108 * @param t1 the first timer controls how long to wait after a provisioning request before poking each node
109 * This timer can be reset if it has not "gone off".
110 * @param t2 the second timer set the outer bound on how long to wait. It cannot be reset.
112 public void setTimers(long t1, long t2) {
113 synchronized (this_pod) {
114 if (timer1 == 0 || t1 > timer1)
119 if (logger.isDebugEnabled())
120 logger.debug("Poker timers set to " + timer1 + " and " + timer2);
126 * Return the last provisioning string built.
127 * @return the last provisioning string built.
129 public String getProvisioningString() {
134 * The method to run at the predefined interval (once per second). This method checks
135 * to see if either of the two timers has expired, and if so, will rebuild the provisioning
136 * string, and poke all the nodes and other PODs. The timers are then reset to 0.
142 long now = System.currentTimeMillis();
143 boolean fire = false;
144 synchronized (this_pod) {
145 if (now > timer1 || now > timer2) {
151 // Rebuild the prov string
152 provstring = buildProvisioningString();
154 // Only the active POD should poke nodes, etc.
155 boolean active = SynchronizerTask.getSynchronizer().isActive();
157 // Poke all the DR nodes
158 for (String n : BaseServlet.getNodes()) {
161 // Poke the pod that is not us
162 for (String n : BaseServlet.getPods()) {
163 if (n.length() > 0 && !n.equals(this_pod))
169 } catch (Exception e) {
170 logger.warn("PROV0020: Caught exception in Poker: "+e);
174 private void pokeNode(final String nodename) {
175 logger.debug("PROV0012 Poking node " + nodename + " ...");
176 Runnable r = new Runnable() {
181 String u = String.format(POKE_URL_TEMPLATE, nodename+":"+DB.HTTP_PORT);
182 URL url = new URL(u);
183 HttpURLConnection conn = (HttpURLConnection) url.openConnection();
184 conn.setConnectTimeout(60000); //Fixes for Itrack DATARTR-3, poke timeout
186 conn.getContentLength(); // Force the GET through
188 } catch (MalformedURLException e) {
189 logger.warn("PROV0013 MalformedURLException Error poking node "+nodename+": " + e.getMessage());
190 } catch (IOException e) {
191 logger.warn("PROV0013 IOException Error poking node "+nodename+": " + e.getMessage());
195 // Thread t = new Thread(r);
199 @SuppressWarnings("unused")
200 private String buildProvisioningString() {
201 StringBuilder sb = new StringBuilder("{\n");
203 // Append Feeds to the string
205 sb.append("\"feeds\": [");
206 for (Feed f : Feed.getAllFeeds()) {
208 sb.append(f.asJSONObject().toString());
213 //Append groups to the string - Rally:US708115 - 1610
215 sb.append("\"groups\": [");
216 for (Group s : Group.getAllgroups()) {
218 sb.append(s.asJSONObject().toString());
224 // Append Subscriptions to the string
226 sb.append("\"subscriptions\": [");
227 for (Subscription s : Subscription.getAllSubscriptions()) {
230 sb.append(s.asJSONObject().toString());
235 // Append Parameters to the string
237 sb.append("\"parameters\": {");
238 Map<String,String> props = Parameters.getParameters();
239 Set<String> ivals = new HashSet<String>();
240 String intv = props.get("_INT_VALUES");
242 ivals.addAll(Arrays.asList(intv.split("\\|")));
243 for (String key : new TreeSet<String>(props.keySet())) {
244 String v = props.get(key);
246 sb.append(" \"").append(key).append("\": ");
247 if (ivals.contains(key)) {
250 } else if (key.endsWith("S")) {
251 // Split and append array of strings
252 String[] pp = v.split("\\|");
255 for (String t : pp) {
256 sb.append(p2).append("\"").append(quote(t)).append("\"");
261 sb.append("\"").append(quote(v)).append("\"");
267 // Append Routes to the string
269 sb.append("\"ingress\": [");
270 for (IngressRoute in : IngressRoute.getAllIngressRoutes()) {
272 sb.append(in.asJSONObject().toString());
278 sb.append("\"egress\": {");
279 for (EgressRoute eg : EgressRoute.getAllEgressRoutes()) {
281 String t = eg.asJSONObject().toString();
282 t = t.substring(1, t.length()-1);
289 sb.append("\"routing\": [");
290 for (NetworkRoute ne : NetworkRoute.getAllNetworkRoutes()) {
292 sb.append(ne.asJSONObject().toString());
298 // Convert to string and verify it is valid JSON
299 String provstring = sb.toString();
301 new JSONObject(new JSONTokener(provstring));
302 } catch (JSONException e) {
303 logger.warn("PROV0016: Possible invalid prov string: "+e);
307 private String quote(String s) {
308 StringBuilder sb = new StringBuilder();
309 for (char ch : s.toCharArray()) {
310 if (ch == '\\' || ch == '"') {
315 return sb.toString();