Update project structure to org.onap
[dmaap/datarouter.git] / datarouter-prov / src / main / java / org / onap / dmaap / datarouter / provisioning / utils / DRRouteCLI.java
diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/utils/DRRouteCLI.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/utils/DRRouteCLI.java
new file mode 100644 (file)
index 0000000..4c98f8e
--- /dev/null
@@ -0,0 +1,456 @@
+/*******************************************************************************\r
+ * ============LICENSE_START==================================================\r
+ * * org.onap.dmaap\r
+ * * ===========================================================================\r
+ * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
+ * * ===========================================================================\r
+ * * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * * you may not use this file except in compliance with the License.\r
+ * * You may obtain a copy of the License at\r
+ * * \r
+ *  *      http://www.apache.org/licenses/LICENSE-2.0\r
+ * * \r
+ *  * Unless required by applicable law or agreed to in writing, software\r
+ * * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * * See the License for the specific language governing permissions and\r
+ * * limitations under the License.\r
+ * * ============LICENSE_END====================================================\r
+ * *\r
+ * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
+ * *\r
+ ******************************************************************************/\r
+\r
+\r
+package org.onap.dmaap.datarouter.provisioning.utils;\r
+\r
+import java.io.File;\r
+import java.io.FileInputStream;\r
+import java.io.IOException;\r
+import java.io.InputStream;\r
+import java.io.InputStreamReader;\r
+import java.io.LineNumberReader;\r
+import java.security.KeyStore;\r
+import java.util.Arrays;\r
+import java.util.Properties;\r
+\r
+import javax.servlet.http.HttpServletResponse;\r
+\r
+import org.apache.http.HttpEntity;\r
+import org.apache.http.HttpResponse;\r
+import org.apache.http.StatusLine;\r
+import org.apache.http.client.methods.HttpDelete;\r
+import org.apache.http.client.methods.HttpGet;\r
+import org.apache.http.client.methods.HttpPost;\r
+import org.apache.http.conn.scheme.Scheme;\r
+import org.apache.http.conn.ssl.SSLSocketFactory;\r
+import org.apache.http.impl.client.AbstractHttpClient;\r
+import org.apache.http.impl.client.DefaultHttpClient;\r
+import org.apache.http.util.EntityUtils;\r
+import org.json.JSONArray;\r
+import org.json.JSONObject;\r
+import org.json.JSONTokener;\r
+\r
+/**\r
+ * This class provides a Command Line Interface for the routing tables in the DR Release 2.0 DB.\r
+ * A full description of this command is <a href="http://wiki.proto.research.att.com/doku.php?id=datarouter-route-cli">here</a>.\r
+ *\r
+ * @author Robert Eby\r
+ * @version $Id: DRRouteCLI.java,v 1.2 2013/11/05 15:54:16 eby Exp $\r
+ */\r
+public class DRRouteCLI {\r
+       /**\r
+        * Invoke the CLI.  The CLI can be run with a single command (given as command line arguments),\r
+        * or in an interactive mode where the user types a sequence of commands to the program.  The CLI is invoked via:\r
+        * <pre>\r
+        * java org.onap.dmaap.datarouter.provisioning.utils.DRRouteCLI [ -s <i>server</i> ] [ <i>command</i> ]\r
+        * </pre>\r
+        * A full description of the arguments to this command are\r
+        * <a href="http://wiki.proto.research.att.com/doku.php?id=datarouter-route-cli">here</a>.\r
+        *\r
+        * @param args command line arguments\r
+        * @throws Exception for any unrecoverable problem\r
+        */\r
+       public static void main(String[] args) throws Exception {\r
+               String server = System.getenv(ENV_VAR);\r
+               if (args.length >= 2 && args[0].equals("-s")) {\r
+                       server = args[1];\r
+                       String[] t = new String[args.length-2];\r
+                       if (t.length > 0)\r
+                               System.arraycopy(args, 2, t, 0, t.length);\r
+                       args = t;\r
+               }\r
+               if (server == null || server.equals("")) {\r
+                       System.err.println("dr-route: you need to specify a server, either via $PROVSRVR or the '-s' option.");\r
+                       System.exit(1);\r
+               }\r
+               DRRouteCLI cli = new DRRouteCLI(server);\r
+               if (args.length > 0) {\r
+                       boolean b = cli.runCommand(args);\r
+                       System.exit(b ? 0 : 1);\r
+               } else {\r
+                       cli.interactive();\r
+                       System.exit(0);\r
+               }\r
+       }\r
+\r
+       public static final String ENV_VAR = "PROVSRVR";\r
+       public static final String PROMPT = "dr-route> ";\r
+       public static final String DEFAULT_TRUSTSTORE_PATH = /* $JAVA_HOME + */ "/jre/lib/security/cacerts";\r
+\r
+       private final String server;\r
+       private int width = 120;                // screen width (for list)\r
+       private AbstractHttpClient httpclient;\r
+\r
+       /**\r
+        * Create a DRRouteCLI object connecting to the specified server.\r
+        * @param server the server to send command to\r
+        * @throws Exception\r
+        */\r
+       public DRRouteCLI(String server) throws Exception {\r
+               this.server = server;\r
+               this.width = 120;\r
+               this.httpclient = new DefaultHttpClient();\r
+\r
+               Properties p = (new DB()).getProperties();\r
+               String truststore_file = p.getProperty("com.att.research.datarouter.provserver.truststore.path");\r
+               String truststore_pw   = p.getProperty("com.att.research.datarouter.provserver.truststore.password");\r
+\r
+               KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());\r
+               if (truststore_file == null || truststore_file.equals("")) {\r
+                       String jhome = System.getenv("JAVA_HOME");\r
+                       if (jhome == null || jhome.equals(""))\r
+                               jhome = "/opt/java/jdk/jdk180";\r
+                       truststore_file = jhome + DEFAULT_TRUSTSTORE_PATH;\r
+               }\r
+               File f = new File(truststore_file);\r
+               if (f.exists()) {\r
+                       FileInputStream instream = new FileInputStream(f);\r
+                   try {\r
+                       trustStore.load(instream, truststore_pw.toCharArray());\r
+                   } catch (Exception x) {\r
+                       System.err.println("Problem reading truststore: "+x);\r
+                       throw x;\r
+                   } finally {\r
+                       try { instream.close(); } catch (Exception ignore) {}\r
+                   }\r
+               }\r
+\r
+               SSLSocketFactory socketFactory = new SSLSocketFactory(trustStore);\r
+               Scheme sch = new Scheme("https", 443, socketFactory);\r
+               httpclient.getConnectionManager().getSchemeRegistry().register(sch);\r
+       }\r
+\r
+       private void interactive() throws IOException {\r
+               LineNumberReader in = new LineNumberReader(new InputStreamReader(System.in));\r
+               while (true) {\r
+                       System.out.print(PROMPT);\r
+                       String line = in.readLine();\r
+                       if (line == null)\r
+                               return;\r
+                       line = line.trim();\r
+                       if (line.equalsIgnoreCase("exit"))      // "exit" may only be used in interactive mode\r
+                               return;\r
+                       if (line.equalsIgnoreCase("quit"))      // "quit" may only be used in interactive mode\r
+                               return;\r
+                       String[] args = line.split("[ \t]+");\r
+                       if (args.length > 0)\r
+                               runCommand(args);\r
+               }\r
+       }\r
+\r
+       /**\r
+        * Run the command specified by the arguments.\r
+        * @param args The command line arguments.\r
+        * @return true if the command was valid and succeeded\r
+        */\r
+       public boolean runCommand(String[] args) {\r
+               String cmd = args[0].trim().toLowerCase();\r
+               if (cmd.equals("add")) {\r
+                       if (args.length > 2) {\r
+                               if (args[1].startsWith("in") && args.length >= 6) {\r
+                                       return addIngress(args);\r
+                               }\r
+                               if (args[1].startsWith("eg") && args.length == 4) {\r
+                                       return addEgress(args);\r
+                               }\r
+                               if (args[1].startsWith("ne") && args.length == 5) {\r
+                                       return addRoute(args);\r
+                               }\r
+                       }\r
+                       System.err.println("Add command should be one of:");\r
+                       System.err.println("  add in[gress] feedid user subnet nodepatt [ seq ]");\r
+                       System.err.println("  add eg[ress]  subid node");\r
+                       System.err.println("  add ne[twork] fromnode tonode vianode");\r
+               } else if (cmd.startsWith("del")) {\r
+                       if (args.length > 2) {\r
+                               if (args[1].startsWith("in") && args.length == 5) {\r
+                                       return delIngress(args);\r
+                               }\r
+                               if (args[1].startsWith("in") && args.length == 3) {\r
+                                       return delIngress(args);\r
+                               }\r
+                               if (args[1].startsWith("eg") && args.length == 3) {\r
+                                       return delEgress(args);\r
+                               }\r
+                               if (args[1].startsWith("ne") && args.length == 4) {\r
+                                       return delRoute(args);\r
+                               }\r
+                       }\r
+                       System.err.println("Delete command should be one of:");\r
+                       System.err.println("  del in[gress] feedid user subnet");\r
+                       System.err.println("  del in[gress] seq");\r
+                       System.err.println("  del eg[ress]  subid");\r
+                       System.err.println("  del ne[twork] fromnode tonode");\r
+               } else if (cmd.startsWith("lis")) {\r
+                       return list(args);\r
+               } else if (cmd.startsWith("wid") && args.length > 1) {\r
+                       width = Integer.parseInt(args[1]);\r
+                       return true;\r
+               } else if (cmd.startsWith("?") || cmd.startsWith("hel") || cmd.startsWith("usa")) {\r
+                       usage();\r
+               } else if (cmd.startsWith("#")) {\r
+                       // comment -- ignore\r
+               } else {\r
+                       System.err.println("Command should be one of add, del, list, exit, quit");\r
+               }\r
+               return false;\r
+       }\r
+\r
+       private void usage() {\r
+               System.out.println("Enter one of the following commands:");\r
+               System.out.println("  add in[gress] feedid user subnet nodepatt [ seq ]");\r
+               System.out.println("  add eg[ress]  subid node");\r
+               System.out.println("  add ne[twork] fromnode tonode vianode");\r
+               System.out.println("  del in[gress] feedid user subnet");\r
+               System.out.println("  del in[gress] seq");\r
+               System.out.println("  del eg[ress]  subid");\r
+               System.out.println("  del ne[twork] fromnode tonode");\r
+               System.out.println("  list [ all | ingress | egress | network ]");\r
+               System.out.println("  exit");\r
+               System.out.println("  quit");\r
+       }\r
+\r
+       private boolean addIngress(String[] args) {\r
+               String url = String.format("https://%s/internal/route/ingress/?feed=%s&user=%s&subnet=%s&nodepatt=%s", server, args[2], args[3], args[4], args[5]);\r
+               if (args.length > 6)\r
+                       url += "&seq=" + args[6];\r
+               return doPost(url);\r
+       }\r
+\r
+       private boolean addEgress(String[] args) {\r
+               String url = String.format("https://%s/internal/route/egress/?sub=%s&node=%s", server, args[2], args[3]);\r
+               return doPost(url);\r
+       }\r
+\r
+       private boolean addRoute(String[] args) {\r
+               String url = String.format("https://%s/internal/route/network/?from=%s&to=%s&via=%s", server, args[2], args[3], args[4]);\r
+               return doPost(url);\r
+       }\r
+\r
+       private boolean delIngress(String[] args) {\r
+               String url;\r
+               if (args.length == 5) {\r
+                       String subnet = args[4].replaceAll("/", "!");   // replace the / with a !\r
+                       url = String.format("https://%s/internal/route/ingress/%s/%s/%s", server, args[2], args[3], subnet);\r
+               } else {\r
+                       url = String.format("https://%s/internal/route/ingress/%s", server, args[2]);\r
+               }\r
+               return doDelete(url);\r
+       }\r
+\r
+       private boolean delEgress(String[] args) {\r
+               String url = String.format("https://%s/internal/route/egress/%s", server, args[2]);\r
+               return doDelete(url);\r
+       }\r
+\r
+       private boolean delRoute(String[] args) {\r
+               String url = String.format("https://%s/internal/route/network/%s/%s", server, args[2], args[3]);\r
+               return doDelete(url);\r
+       }\r
+\r
+       private boolean list(String[] args) {\r
+               String tbl = (args.length == 1) ? "all" : args[1].toLowerCase();\r
+               JSONObject jo = doGet("https://"+server+"/internal/route/");    // Returns all 3 tables\r
+               StringBuilder sb = new StringBuilder();\r
+               if (tbl.startsWith("al") || tbl.startsWith("in")) {\r
+                       // Display the IRT\r
+                       JSONArray irt = jo.optJSONArray("ingress");\r
+                       int cw1 = 6, cw2 = 6, cw3 = 6, cw4 = 6;         // determine column widths for first 4 cols\r
+                       for (int i = 0; irt != null && i < irt.length(); i++) {\r
+                               JSONObject e  = irt.getJSONObject(i);\r
+                               cw1 = Math.max(cw1, (""+ e.getInt("seq")).length());\r
+                               cw2 = Math.max(cw2, (""+e.getInt("feedid")).length());\r
+                               String t = e.optString("user");\r
+                               cw3 = Math.max(cw3, (t == null) ? 1 : t.length());\r
+                               t = e.optString("subnet");\r
+                               cw4 = Math.max(cw4, (t == null) ? 1 : t.length());\r
+                       }\r
+\r
+                       int nblank = cw1 + cw2 + cw3 + cw4 + 8;\r
+                       sb.append("Ingress Routing Table\n");\r
+                       sb.append(String.format("%s  %s  %s  %s  Nodes\n", ext("Seq", cw1), ext("FeedID", cw2), ext("User", cw3), ext("Subnet", cw4)));\r
+                       for (int i = 0; irt != null && i < irt.length(); i++) {\r
+                               JSONObject e  = irt.getJSONObject(i);\r
+                               String seq    = ""+e.getInt("seq");\r
+                               String feedid = ""+e.getInt("feedid");\r
+                               String user   = e.optString("user");\r
+                               String subnet = e.optString("subnet");\r
+                               if (user.equals("")) user = "-";\r
+                               if (subnet.equals("")) subnet = "-";\r
+                               JSONArray nodes = e.getJSONArray("node");\r
+                               int sol = sb.length();\r
+                               sb.append(String.format("%s  %s  %s  %s  ", ext(seq, cw1), ext(feedid, cw2), ext(user, cw3), ext(subnet, cw4)));\r
+                               for (int j = 0; j < nodes.length(); j++) {\r
+                                       String nd = nodes.getString(j);\r
+                                       int cursor = sb.length() - sol;\r
+                                       if (j > 0 && (cursor + nd.length() > width)) {\r
+                                               sb.append("\n");\r
+                                               sol = sb.length();\r
+                                               sb.append(ext(" ", nblank));\r
+                                       }\r
+                                       sb.append(nd);\r
+                                       if ((j+1) < nodes.length()) {\r
+                                               sb.append(", ");\r
+                                       }\r
+                               }\r
+                               sb.append("\n");\r
+                       }\r
+               }\r
+               if (tbl.startsWith("al") || tbl.startsWith("eg")) {\r
+                       // Display the ERT\r
+                       JSONObject ert = jo.optJSONObject("egress");\r
+                       String[] subs = (ert == null) ? new String[0] : JSONObject.getNames(ert);\r
+                       if (subs == null)\r
+                               subs = new String[0];\r
+                       Arrays.sort(subs);\r
+                       int cw1 = 5;\r
+                       for (int i = 0; i < subs.length; i++) {\r
+                               cw1 = Math.max(cw1, subs[i].length());\r
+                       }\r
+\r
+                       if (sb.length() > 0)\r
+                               sb.append("\n");\r
+                       sb.append("Egress Routing Table\n");\r
+                       sb.append(String.format("%s  Node\n", ext("SubID", cw1)));\r
+                       for (int i = 0; i < subs.length; i++) {\r
+                               String node = ert.getString(subs[i]);\r
+                               sb.append(String.format("%s  %s\n", ext(subs[i], cw1), node));\r
+                       }\r
+               }\r
+               if (tbl.startsWith("al") || tbl.startsWith("ne")) {\r
+                       // Display the NRT\r
+                       JSONArray nrt = jo.optJSONArray("routing");\r
+                       int cw1 = 4, cw2 = 4;\r
+                       for (int i = 0; nrt != null && i < nrt.length(); i++) {\r
+                               JSONObject e = nrt.getJSONObject(i);\r
+                               String from = e.getString("from");\r
+                               String to   = e.getString("to");\r
+                               cw1 = Math.max(cw1, from.length());\r
+                               cw2 = Math.max(cw2, to.length());\r
+                       }\r
+\r
+                       if (sb.length() > 0)\r
+                               sb.append("\n");\r
+                       sb.append("Network Routing Table\n");\r
+                       sb.append(String.format("%s  %s  Via\n", ext("From", cw1), ext("To", cw2)));\r
+                       for (int i = 0; nrt != null && i < nrt.length(); i++) {\r
+                               JSONObject e = nrt.getJSONObject(i);\r
+                               String from = e.getString("from");\r
+                               String to   = e.getString("to");\r
+                               String via  = e.getString("via");\r
+                               sb.append(String.format("%s  %s  %s\n", ext(from, cw1), ext(to, cw2), via));\r
+                       }\r
+               }\r
+               System.out.print(sb.toString());\r
+               return true;\r
+       }\r
+       private String ext(String s, int n) {\r
+               if (s == null)\r
+                       s = "-";\r
+               while (s.length() < n)\r
+                       s += " ";\r
+               return s;\r
+       }\r
+\r
+       private boolean doDelete(String url) {\r
+               boolean rv = false;\r
+               HttpDelete meth = new HttpDelete(url);\r
+               try {\r
+                       HttpResponse response = httpclient.execute(meth);\r
+                       HttpEntity entity = response.getEntity();\r
+                       StatusLine sl = response.getStatusLine();\r
+                       rv = (sl.getStatusCode() == HttpServletResponse.SC_OK);\r
+                       if (rv) {\r
+                               System.out.println("Routing entry deleted.");\r
+                               EntityUtils.consume(entity);\r
+                       } else {\r
+                               printErrorText(entity);\r
+                       }\r
+               } catch (Exception e) {\r
+               } finally {\r
+                       meth.releaseConnection();\r
+               }\r
+               return rv;\r
+       }\r
+\r
+       private JSONObject doGet(String url) {\r
+               JSONObject rv = new JSONObject();\r
+               HttpGet meth = new HttpGet(url);\r
+               try {\r
+                       HttpResponse response = httpclient.execute(meth);\r
+                       HttpEntity entity = response.getEntity();\r
+                       StatusLine sl = response.getStatusLine();\r
+                       if (sl.getStatusCode() == HttpServletResponse.SC_OK) {\r
+                               rv = new JSONObject(new JSONTokener(entity.getContent()));\r
+                       } else {\r
+                               printErrorText(entity);\r
+                       }\r
+               } catch (Exception e) {\r
+                       System.err.println(e);\r
+               } finally {\r
+                       meth.releaseConnection();\r
+               }\r
+               return rv;\r
+       }\r
+\r
+       private boolean doPost(String url) {\r
+               boolean rv = false;\r
+               HttpPost meth = new HttpPost(url);\r
+               try {\r
+                       HttpResponse response = httpclient.execute(meth);\r
+                       HttpEntity entity = response.getEntity();\r
+                       StatusLine sl = response.getStatusLine();\r
+                       rv = (sl.getStatusCode() == HttpServletResponse.SC_OK);\r
+                       if (rv) {\r
+                               System.out.println("Routing entry added.");\r
+                               EntityUtils.consume(entity);\r
+                       } else {\r
+                               printErrorText(entity);\r
+                       }\r
+               } catch (Exception e) {\r
+               } finally {\r
+                       meth.releaseConnection();\r
+               }\r
+               return rv;\r
+       }\r
+\r
+       private void printErrorText(HttpEntity entity) throws IllegalStateException, IOException {\r
+               // Look for and print only the part of the output between <pre>...</pre>\r
+               InputStream is = entity.getContent();\r
+               StringBuilder sb = new StringBuilder();\r
+               byte[] b = new byte[512];\r
+               int n = 0;\r
+               while ((n = is.read(b)) > 0) {\r
+                       sb.append(new String(b, 0, n));\r
+               }\r
+               is.close();\r
+               int ix = sb.indexOf("<pre>");\r
+               if (ix > 0)\r
+                       sb.delete(0, ix+5);\r
+               ix = sb.indexOf("</pre>");\r
+               if (ix > 0)\r
+                       sb.delete(ix, sb.length());\r
+               System.err.println(sb.toString());\r
+       }\r
+}\r