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.IOException;
27 import java.net.HttpURLConnection;
28 import java.net.InetAddress;
29 import java.net.MalformedURLException;
31 import java.net.UnknownHostException;
32 import java.util.Arrays;
33 import java.util.HashSet;
36 import java.util.Timer;
37 import java.util.TimerTask;
38 import java.util.TreeSet;
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.*;
54 * This class handles the two timers (described in R1 Design Notes), and takes care of issuing the GET to each node of
58 * @version $Id: Poker.java,v 1.11 2014/01/08 16:13:47 eby Exp $
60 public class Poker extends TimerTask {
63 * Template used to generate the URL to issue the GET against
65 private static final String POKE_URL_TEMPLATE = "http://%s/internal/fetchProv";
68 * This is a singleton -- there is only one Poker object in the server
70 private static Poker poker;
73 * Get the singleton Poker object.
77 public static synchronized Poker getPoker() {
86 private String thisPod; // DNS name of this machine
87 private Logger logger;
88 private String provString;
92 Timer rolex = new Timer();
93 logger = Logger.getLogger("org.onap.dmaap.datarouter.provisioning.internal");
95 thisPod = InetAddress.getLocalHost().getHostName();
96 } catch (UnknownHostException e) {
97 thisPod = "*UNKNOWN*"; // not a major problem
99 provString = buildProvisioningString();
101 rolex.scheduleAtFixedRate(this, 0L, 1000L); // Run once a second to check the timers
105 * This method sets the two timers described in the design notes.
107 * @param t1 the first timer controls how long to wait after a provisioning request before poking each node This
108 * timer can be reset if it has not "gone off".
109 * @param t2 the second timer set the outer bound on how long to wait. It cannot be reset.
111 public void setTimers(long t1, long t2) {
112 synchronized (thisPod) {
113 if (timer1 == 0 || t1 > timer1) {
120 if (logger.isDebugEnabled()) {
121 logger.debug("Poker timers set to " + timer1 + " and " + timer2);
128 * Return the last provisioning string built.
130 * @return the last provisioning string built.
132 public String getProvisioningString() {
137 * The method to run at the predefined interval (once per second). This method checks to see if either of the two
138 * timers has expired, and if so, will rebuild the provisioning string, and poke all the nodes and other PODs. The
139 * timers are then reset to 0.
145 long now = System.currentTimeMillis();
146 boolean fire = false;
147 synchronized (thisPod) {
148 if (now > timer1 || now > timer2) {
157 } catch (Exception e) {
158 logger.warn("PROV0020: Caught exception in Poker: " + e);
163 private void pokeNodes() {
164 // Rebuild the prov string
165 provString = buildProvisioningString();
166 // Only the active POD should poke nodes, etc.
167 boolean active = SynchronizerTask.getSynchronizer().isActive();
169 // Poke all the DR nodes
170 for (String n : BaseServlet.getNodes()) {
173 // Poke the pod that is not us
174 for (String n : BaseServlet.getPods()) {
175 if (n.length() > 0 && !n.equals(thisPod)) {
182 private void pokeNode(final String nodename) {
183 logger.debug("PROV0012 Poking node " + nodename + " ...");
184 String nodeUrl = String.format(POKE_URL_TEMPLATE, nodename + ":" + DB.HTTP_PORT);
187 URL url = new URL(nodeUrl);
188 HttpURLConnection conn = (HttpURLConnection) url.openConnection();
189 conn.setConnectTimeout(60000); //Fixes for Itrack DATARTR-3, poke timeout
191 conn.getContentLength(); // Force the GET through
193 } catch (MalformedURLException e) {
194 logger.warn("PROV0013 MalformedURLException Error poking node at " + nodeUrl + " : " + e.getMessage());
195 } catch (IOException e) {
196 logger.warn("PROV0013 IOException Error poking node at " + nodeUrl + " : " + e.getMessage());
202 private String buildProvisioningString() {
203 StringBuilder sb = new StringBuilder("{\n");
205 // Append Feeds to the string
207 sb.append("\"feeds\": [");
208 for (Feed f : Feed.getAllFeeds()) {
210 sb.append(f.asJSONObject().toString());
215 //Append groups to the string - Rally:US708115 - 1610
217 sb.append("\"groups\": [");
218 for (Group s : Group.getAllgroups()) {
220 sb.append(s.asJSONObject().toString());
225 // Append Subscriptions to the string
227 sb.append("\"subscriptions\": [");
228 for (Subscription s : Subscription.getAllSubscriptions()) {
231 sb.append(s.asJSONObject().toString());
237 // Append Parameters to the string
239 sb.append("\"parameters\": {");
240 Map<String, String> props = Parameters.getParameters();
241 Set<String> ivals = new HashSet<String>();
242 String intv = props.get("_INT_VALUES");
244 ivals.addAll(Arrays.asList(intv.split("\\|")));
246 for (String key : new TreeSet<String>(props.keySet())) {
247 String v = props.get(key);
249 sb.append(" \"").append(key).append("\": ");
250 if (ivals.contains(key)) {
253 } else if (key.endsWith("S")) {
254 // Split and append array of strings
255 String[] pp = v.split("\\|");
258 for (String t : pp) {
259 sb.append(p2).append("\"").append(quote(t)).append("\"");
264 sb.append("\"").append(quote(v)).append("\"");
270 // Append Routes to the string
272 sb.append("\"ingress\": [");
273 for (IngressRoute in : IngressRoute.getAllIngressRoutes()) {
275 sb.append(in.asJSONObject().toString());
281 sb.append("\"egress\": {");
282 for (EgressRoute eg : EgressRoute.getAllEgressRoutes()) {
284 String t = eg.asJSONObject().toString();
285 t = t.substring(1, t.length() - 1);
292 sb.append("\"routing\": [");
293 for (NetworkRoute ne : NetworkRoute.getAllNetworkRoutes()) {
295 sb.append(ne.asJSONObject().toString());
301 // Convert to string and verify it is valid JSON
302 String tempProvString = sb.toString();
304 new JSONObject(new JSONTokener(tempProvString));
305 } catch (JSONException e) {
306 logger.warn("PROV0016: Possible invalid prov string: " + e);
308 return tempProvString;
311 private String quote(String s) {
312 StringBuilder sb = new StringBuilder();
313 for (char ch : s.toCharArray()) {
314 if (ch == '\\' || ch == '"') {
319 return sb.toString();