X-Git-Url: https://gerrit.onap.org/r/gitweb?p=dmaap%2Fdatarouter.git;a=blobdiff_plain;f=datarouter-prov%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fdmaap%2Fdatarouter%2Fprovisioning%2FPoker.java;h=f1660f9479113c610ccee9f2ab32c5b655665c9c;hp=1d9f7143075fafab0ea36fd2dbcda1ff1ef7ca9a;hb=6c78b3e6a0a67c73f931337356a172cc69cee0e8;hpb=e4b20cc6f7c31f48ddd0de5bcd054b09a35cd510 diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/Poker.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/Poker.java index 1d9f7143..f1660f94 100644 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/Poker.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/Poker.java @@ -7,9 +7,9 @@ * * Licensed under the Apache License, Version 2.0 (the "License"); * * you may not use this file except in compliance with the License. * * You may obtain a copy of the License at - * * + * * * * http://www.apache.org/licenses/LICENSE-2.0 - * * + * * * * Unless required by applicable law or agreed to in writing, software * * distributed under the License is distributed on an "AS IS" BASIS, * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -23,9 +23,7 @@ package org.onap.dmaap.datarouter.provisioning; -import java.io.FileInputStream; import java.io.IOException; -import java.io.InputStream; import java.net.HttpURLConnection; import java.net.InetAddress; import java.net.MalformedURLException; @@ -34,15 +32,13 @@ import java.net.UnknownHostException; import java.util.Arrays; import java.util.HashSet; import java.util.Map; -import java.util.Properties; import java.util.Set; import java.util.Timer; import java.util.TimerTask; import java.util.TreeSet; -import javax.servlet.ServletException; - -import org.apache.log4j.Logger; +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; import org.json.JSONException; import org.json.JSONObject; import org.json.JSONTokener; @@ -53,265 +49,280 @@ import org.onap.dmaap.datarouter.provisioning.beans.IngressRoute; import org.onap.dmaap.datarouter.provisioning.beans.NetworkRoute; import org.onap.dmaap.datarouter.provisioning.beans.Parameters; import org.onap.dmaap.datarouter.provisioning.beans.Subscription; -import org.onap.dmaap.datarouter.provisioning.utils.*; +import org.onap.dmaap.datarouter.provisioning.utils.DB; /** - * This class handles the two timers (described in R1 Design Notes), and takes care of issuing - * the GET to each node of the URL to "poke". + * This class handles the two timers (described in R1 Design Notes), and takes care of issuing the GET to each node of + * the URL to "poke". * * @author Robert Eby * @version $Id: Poker.java,v 1.11 2014/01/08 16:13:47 eby Exp $ */ public class Poker extends TimerTask { - /** Template used to generate the URL to issue the GET against */ - public static final String POKE_URL_TEMPLATE = "http://%s/internal/fetchProv"; - - - - /** This is a singleton -- there is only one Poker object in the server */ - private static Poker p; + /** + * Template used to generate the URL to issue the GET against + */ + private static final String POKE_URL_TEMPLATE = "http://%s/internal/fetchProv"; + + private static final Object lock = new Object(); + private static final String CARRIAGE_RETURN = "\n],\n"; + + /** + * This is a singleton -- there is only one Poker object in the server + */ + private static Poker poker; + private long timer1; + private long timer2; + private String thisPod; // DNS name of this machine + private EELFLogger logger; + private String provString; + + + private Poker() { + timer1 = timer2 = 0; + Timer rolex = new Timer(); + logger = EELFManager.getInstance().getLogger("InternalLog"); + try { + thisPod = InetAddress.getLocalHost().getHostName(); + } catch (UnknownHostException e) { + thisPod = "*UNKNOWN_POD*"; // not a major problem + logger.info("UnknownHostException: Setting thisPod to \"*UNKNOWN_POD*\"", e); + } + provString = buildProvisioningString(); + + rolex.scheduleAtFixedRate(this, 0L, 1000L); // Run once a second to check the timers + } + + /** + * Get the singleton Poker object. + * + * @return the Poker + */ + public static synchronized Poker getPoker() { + if (poker == null) { + poker = new Poker(); + } + return poker; + } + + /** + * This method sets the two timers described in the design notes. + * + * @param t1 the first timer controls how long to wait after a provisioning request before poking each node This + * timer can be reset if it has not "gone off". + * @param t2 the second timer set the outer bound on how long to wait. It cannot be reset. + */ + public void setTimers(long t1, long t2) { + synchronized (lock) { + if (timer1 == 0 || t1 > timer1) { + timer1 = t1; + } + if (timer2 == 0) { + timer2 = t2; + } + } + if (logger.isDebugEnabled()) { + logger.debug("Poker timers set to " + timer1 + " and " + timer2); + } + - /** - * Get the singleton Poker object. - * @return the Poker - */ - public static synchronized Poker getPoker() { - if (p == null) - p = new Poker(); - return p; - } + } - private long timer1; - private long timer2; - private Timer rolex; - private String this_pod; // DNS name of this machine - private Logger logger; - private String provstring; + /** + * Return the last provisioning string built. + * + * @return the last provisioning string built. + */ + public String getProvisioningString() { + return provString; + } - private Poker() { - timer1 = timer2 = 0; - rolex = new Timer(); - logger = Logger.getLogger("org.onap.dmaap.datarouter.provisioning.internal"); - try { - this_pod = InetAddress.getLocalHost().getHostName(); - } catch (UnknownHostException e) { - this_pod = "*UNKNOWN*"; // not a major problem - } - provstring = buildProvisioningString(); + /** + * The method to run at the predefined interval (once per second). This method checks to see if either of the two + * timers has expired, and if so, will rebuild the provisioning string, and poke all the nodes and other PODs. The + * timers are then reset to 0. + */ + @Override + public void run() { + try { + if (timer1 > 0) { + long now = System.currentTimeMillis(); + boolean fire = false; + synchronized (lock) { + if (now > timer1 || now > timer2) { + timer1 = timer2 = 0; + fire = true; + } + } + if (fire) { + pokeNodes(); + } + } + } catch (Exception e) { + logger.warn("PROV0020: Caught exception in Poker: " + e); + } + } - rolex.scheduleAtFixedRate(this, 0L, 1000L); // Run once a second to check the timers - } + private void pokeNodes() { + // Rebuild the prov string + provString = buildProvisioningString(); + // Only the active POD should poke nodes, etc. + boolean active = SynchronizerTask.getSynchronizer().isActive(); + if (active) { + // Poke all the DR nodes + for (String n : BaseServlet.getNodes()) { + pokeNode(n); + } + // Poke the pod that is not us + for (String n : BaseServlet.getPods()) { + if (n.length() > 0 && !n.equals(thisPod)) { + pokeNode(n); + } + } + } + } - /** - * This method sets the two timers described in the design notes. - * @param t1 the first timer controls how long to wait after a provisioning request before poking each node - * This timer can be reset if it has not "gone off". - * @param t2 the second timer set the outer bound on how long to wait. It cannot be reset. - */ - public void setTimers(long t1, long t2) { - synchronized (this_pod) { - if (timer1 == 0 || t1 > timer1) - timer1 = t1; - if (timer2 == 0) - timer2 = t2; - } - if (logger.isDebugEnabled()) - logger.debug("Poker timers set to " + timer1 + " and " + timer2); - - - } + private void pokeNode(final String nodename) { + logger.debug("PROV0012 Poking node " + nodename + " ..."); + String nodeUrl = String.format(POKE_URL_TEMPLATE, nodename + ":" + DB.getHttpPort()); + Runnable r = () -> { + try { + URL url = new URL(nodeUrl); + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + conn.setConnectTimeout(60000); //Fixes for Itrack DATARTR-3, poke timeout + conn.connect(); + conn.getContentLength(); // Force the GET through + conn.disconnect(); + } catch (MalformedURLException e) { + logger.warn( + "PROV0013 MalformedURLException Error poking node at " + nodeUrl + " : " + e + .getMessage(), e); + } catch (IOException e) { + logger.warn("PROV0013 IOException Error poking node at " + nodeUrl + " : " + e + .getMessage(), e); + } + }; + r.run(); + } - /** - * Return the last provisioning string built. - * @return the last provisioning string built. - */ - public String getProvisioningString() { - return provstring; - } + private String buildProvisioningString() { + StringBuilder sb = new StringBuilder("{\n"); - /** - * The method to run at the predefined interval (once per second). This method checks - * to see if either of the two timers has expired, and if so, will rebuild the provisioning - * string, and poke all the nodes and other PODs. The timers are then reset to 0. - */ - @Override - public void run() { - try { - if (timer1 > 0) { - long now = System.currentTimeMillis(); - boolean fire = false; - synchronized (this_pod) { - if (now > timer1 || now > timer2) { - timer1 = timer2 = 0; - fire = true; - } - } - if (fire) { - // Rebuild the prov string - provstring = buildProvisioningString(); + // Append Feeds to the string + String pfx = "\n"; + sb.append("\"feeds\": ["); + for (Feed f : Feed.getAllFeeds()) { + sb.append(pfx); + sb.append(f.asJSONObject().toString()); + pfx = ",\n"; + } + sb.append(CARRIAGE_RETURN); - // Only the active POD should poke nodes, etc. - boolean active = SynchronizerTask.getSynchronizer().isActive(); - if (active) { - // Poke all the DR nodes - for (String n : BaseServlet.getNodes()) { - pokeNode(n); - } - // Poke the pod that is not us - for (String n : BaseServlet.getPods()) { - if (n.length() > 0 && !n.equals(this_pod)) - pokeNode(n); - } - } - } - } - } catch (Exception e) { - logger.warn("PROV0020: Caught exception in Poker: "+e); - e.printStackTrace(); - } - } - private void pokeNode(final String nodename) { - logger.debug("PROV0012 Poking node " + nodename + " ..."); - Runnable r = new Runnable() { - @Override - public void run() { - - try { - String u = String.format(POKE_URL_TEMPLATE, nodename+":"+DB.HTTP_PORT); - URL url = new URL(u); - HttpURLConnection conn = (HttpURLConnection) url.openConnection(); - conn.setConnectTimeout(60000); //Fixes for Itrack DATARTR-3, poke timeout - conn.connect(); - conn.getContentLength(); // Force the GET through - conn.disconnect(); - } catch (MalformedURLException e) { - logger.warn("PROV0013 MalformedURLException Error poking node "+nodename+": " + e.getMessage()); - } catch (IOException e) { - logger.warn("PROV0013 IOException Error poking node "+nodename+": " + e.getMessage()); - } - } - }; -// Thread t = new Thread(r); -// t.start(); - r.run(); - } - @SuppressWarnings("unused") - private String buildProvisioningString() { - StringBuilder sb = new StringBuilder("{\n"); + //Append groups to the string - Rally:US708115 - 1610 + pfx = "\n"; + sb.append("\"groups\": ["); + for (Group s : Group.getAllgroups()) { + sb.append(pfx); + sb.append(s.asJSONObject().toString()); + pfx = ",\n"; + } + sb.append(CARRIAGE_RETURN); - // Append Feeds to the string - String pfx = "\n"; - sb.append("\"feeds\": ["); - for (Feed f : Feed.getAllFeeds()) { - sb.append(pfx); - sb.append(f.asJSONObject().toString()); - pfx = ",\n"; - } - sb.append("\n],\n"); - - //Append groups to the string - Rally:US708115 - 1610 - pfx = "\n"; - sb.append("\"groups\": ["); - for (Group s : Group.getAllgroups()) { - sb.append(pfx); - sb.append(s.asJSONObject().toString()); - pfx = ",\n"; - } - sb.append("\n],\n"); - + // Append Subscriptions to the string + pfx = "\n"; + sb.append("\"subscriptions\": ["); + for (Subscription s : Subscription.getAllSubscriptions()) { + sb.append(pfx); + if (s != null) { + sb.append(s.asJSONObject().toString()); + } + pfx = ",\n"; + } + sb.append(CARRIAGE_RETURN); - // Append Subscriptions to the string - pfx = "\n"; - sb.append("\"subscriptions\": ["); - for (Subscription s : Subscription.getAllSubscriptions()) { - sb.append(pfx); - if(s!=null) - sb.append(s.asJSONObject().toString()); - pfx = ",\n"; - } - sb.append("\n],\n"); + // Append Parameters to the string + pfx = "\n"; + sb.append("\"parameters\": {"); + Map props = Parameters.getParameters(); + Set ivals = new HashSet<>(); + String intv = props.get("_INT_VALUES"); + if (intv != null) { + ivals.addAll(Arrays.asList(intv.split("\\|"))); + } + for (String key : new TreeSet(props.keySet())) { + String v = props.get(key); + sb.append(pfx); + sb.append(" \"").append(key).append("\": "); + if (ivals.contains(key)) { + // integer value + sb.append(v); + } else if (key.endsWith("S")) { + // Split and append array of strings + String[] pp = v.split("\\|"); + String p2 = ""; + sb.append("["); + for (String t : pp) { + sb.append(p2).append("\"").append(quote(t)).append("\""); + p2 = ","; + } + sb.append("]"); + } else { + sb.append("\"").append(quote(v)).append("\""); + } + pfx = ",\n"; + } + sb.append("\n},\n"); - // Append Parameters to the string - pfx = "\n"; - sb.append("\"parameters\": {"); - Map props = Parameters.getParameters(); - Set ivals = new HashSet(); - String intv = props.get("_INT_VALUES"); - if (intv != null) - ivals.addAll(Arrays.asList(intv.split("\\|"))); - for (String key : new TreeSet(props.keySet())) { - String v = props.get(key); - sb.append(pfx); - sb.append(" \"").append(key).append("\": "); - if (ivals.contains(key)) { - // integer value - sb.append(v); - } else if (key.endsWith("S")) { - // Split and append array of strings - String[] pp = v.split("\\|"); - String p2 = ""; - sb.append("["); - for (String t : pp) { - sb.append(p2).append("\"").append(quote(t)).append("\""); - p2 = ","; - } - sb.append("]"); - } else { - sb.append("\"").append(quote(v)).append("\""); - } - pfx = ",\n"; - } - sb.append("\n},\n"); + // Append Routes to the string + pfx = "\n"; + sb.append("\"ingress\": ["); + for (IngressRoute in : IngressRoute.getAllIngressRoutes()) { + sb.append(pfx); + sb.append(in.asJSONObject().toString()); + pfx = ",\n"; + } + sb.append(CARRIAGE_RETURN); - // Append Routes to the string - pfx = "\n"; - sb.append("\"ingress\": ["); - for (IngressRoute in : IngressRoute.getAllIngressRoutes()) { - sb.append(pfx); - sb.append(in.asJSONObject().toString()); - pfx = ",\n"; - } - sb.append("\n],\n"); + pfx = "\n"; + sb.append("\"egress\": {"); + for (EgressRoute eg : EgressRoute.getAllEgressRoutes()) { + sb.append(pfx); + String t = eg.asJSONObject().toString(); + t = t.substring(1, t.length() - 1); + sb.append(t); + pfx = ",\n"; + } + sb.append("\n},\n"); - pfx = "\n"; - sb.append("\"egress\": {"); - for (EgressRoute eg : EgressRoute.getAllEgressRoutes()) { - sb.append(pfx); - String t = eg.asJSONObject().toString(); - t = t.substring(1, t.length()-1); - sb.append(t); - pfx = ",\n"; - } - sb.append("\n},\n"); + pfx = "\n"; + sb.append("\"routing\": ["); + for (NetworkRoute ne : NetworkRoute.getAllNetworkRoutes()) { + sb.append(pfx); + sb.append(ne.asJSONObject().toString()); + pfx = ",\n"; + } + sb.append("\n]"); + sb.append("\n}"); - pfx = "\n"; - sb.append("\"routing\": ["); - for (NetworkRoute ne : NetworkRoute.getAllNetworkRoutes()) { - sb.append(pfx); - sb.append(ne.asJSONObject().toString()); - pfx = ",\n"; - } - sb.append("\n]"); - sb.append("\n}"); + // Convert to string and verify it is valid JSON + String tempProvString = sb.toString(); + try { + new JSONObject(new JSONTokener(tempProvString)); + } catch (JSONException e) { + logger.warn("PROV0016: Possible invalid prov string: " + e); + } + return tempProvString; + } - // Convert to string and verify it is valid JSON - String provstring = sb.toString(); - try { - new JSONObject(new JSONTokener(provstring)); - } catch (JSONException e) { - logger.warn("PROV0016: Possible invalid prov string: "+e); - } - return provstring; - } - private String quote(String s) { - StringBuilder sb = new StringBuilder(); - for (char ch : s.toCharArray()) { - if (ch == '\\' || ch == '"') { - sb.append('\\'); - } - sb.append(ch); - } - return sb.toString(); - } + private String quote(String s) { + StringBuilder sb = new StringBuilder(); + for (char ch : s.toCharArray()) { + if (ch == '\\' || ch == '"') { + sb.append('\\'); + } + sb.append(ch); + } + return sb.toString(); + } }