Refactor Prov DB handling
[dmaap/datarouter.git] / datarouter-prov / src / main / java / org / onap / dmaap / datarouter / provisioning / utils / DRRouteCLI.java
index af8bd6d..187364f 100644 (file)
  * *\r
  ******************************************************************************/\r
 \r
-\r
 package org.onap.dmaap.datarouter.provisioning.utils;\r
 \r
+import static java.lang.System.exit;\r
+\r
+import com.att.eelf.configuration.EELFLogger;\r
+import com.att.eelf.configuration.EELFManager;\r
 import java.io.File;\r
 import java.io.FileInputStream;\r
 import java.io.IOException;\r
@@ -50,6 +53,7 @@ import org.apache.http.util.EntityUtils;
 import org.json.JSONArray;\r
 import org.json.JSONObject;\r
 import org.json.JSONTokener;\r
+import org.onap.dmaap.datarouter.provisioning.ProvRunner;\r
 \r
 /**\r
  * This class provides a Command Line Interface for the routing tables in the DR Release 2.0 DB.\r
@@ -75,10 +79,11 @@ public class DRRouteCLI {
         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
+            String[] str = new String[args.length - 2];\r
+            if (str.length > 0) {\r
+                System.arraycopy(args, 2, str, 0, str.length);\r
+            }\r
+            args = str;\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
@@ -86,17 +91,18 @@ public class DRRouteCLI {
         }\r
         DRRouteCLI cli = new DRRouteCLI(server);\r
         if (args.length > 0) {\r
-            boolean b = cli.runCommand(args);\r
-            System.exit(b ? 0 : 1);\r
+            boolean bool = cli.runCommand(args);\r
+            System.exit(bool ? 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
+    private static final String ENV_VAR = "PROVSRVR";\r
+    private static final String PROMPT = "dr-route> ";\r
+    private static final String DEFAULT_TRUSTSTORE_PATH = /* $JAVA_HOME + */ "/jre/lib/security/cacerts";\r
+    private static final EELFLogger intlogger = EELFManager.getInstance().getLogger("InternalLog");\r
 \r
     private final String server;\r
     private int width = 120;        // screen width (for list)\r
@@ -106,36 +112,46 @@ public class DRRouteCLI {
      * Create a DRRouteCLI object connecting to the specified server.\r
      *\r
      * @param server the server to send command to\r
-     * @throws Exception\r
+     * @throws Exception generic 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("org.onap.dmaap.datarouter.provserver.truststore.path");\r
-        String truststore_pw = p.getProperty("org.onap.dmaap.datarouter.provserver.truststore.password");\r
+        Properties provProperties = ProvRunner.getProvProperties();\r
+        try {\r
+            AafPropsUtils.init(new File(provProperties.getProperty(\r
+                "org.onap.dmaap.datarouter.provserver.aafprops.path",\r
+                "/opt/app/osaaf/local/org.onap.dmaap-dr.props")));\r
+        } catch (IOException e) {\r
+            intlogger.error("NODE0314 Failed to load AAF props. Exiting", e);\r
+            exit(1);\r
+        }\r
+\r
+        String truststoreFile = AafPropsUtils.getInstance().getTruststorePathProperty();\r
+        String truststorePw = AafPropsUtils.getInstance().getTruststorePassProperty();\r
 \r
         KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());\r
-        if (truststore_file == null || truststore_file.equals("")) {\r
+        if (truststoreFile == null || truststoreFile.equals("")) {\r
             String jhome = System.getenv("JAVA_HOME");\r
-            if (jhome == null || jhome.equals(""))\r
+            if (jhome == null || jhome.equals("")) {\r
                 jhome = "/opt/java/jdk/jdk180";\r
-            truststore_file = jhome + DEFAULT_TRUSTSTORE_PATH;\r
+            }\r
+            truststoreFile = jhome + DEFAULT_TRUSTSTORE_PATH;\r
         }\r
-        File f = new File(truststore_file);\r
-        if (f.exists()) {\r
-            FileInputStream instream = new FileInputStream(f);\r
+        File file = new File(truststoreFile);\r
+        if (file.exists()) {\r
+            FileInputStream instream = new FileInputStream(file);\r
             try {\r
-                trustStore.load(instream, truststore_pw.toCharArray());\r
+                trustStore.load(instream, truststorePw.toCharArray());\r
             } catch (Exception x) {\r
-                System.err.println("Problem reading truststore: " + x);\r
+                intlogger.error("Problem reading truststore: " + x.getMessage(), x);\r
                 throw x;\r
             } finally {\r
                 try {\r
                     instream.close();\r
-                } catch (Exception ignore) {\r
+                } catch (Exception e) {\r
+                    intlogger.error("Ignore error closing input stream: " + e.getMessage(), e);\r
                 }\r
             }\r
         }\r
@@ -150,16 +166,20 @@ public class DRRouteCLI {
         while (true) {\r
             System.out.print(PROMPT);\r
             String line = in.readLine();\r
-            if (line == null)\r
+            if (line == null) {\r
                 return;\r
+            }\r
             line = line.trim();\r
-            if (line.equalsIgnoreCase("exit"))    // "exit" may only be used in interactive mode\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
+            }\r
+            if (line.equalsIgnoreCase("quit")) {   // "quit" may only be used in interactive mode\r
                 return;\r
+            }\r
             String[] args = line.split("[ \t]+");\r
-            if (args.length > 0)\r
+            if (args.length > 0) {\r
                 runCommand(args);\r
+            }\r
         }\r
     }\r
 \r
@@ -169,7 +189,7 @@ public class DRRouteCLI {
      * @param args The command line arguments.\r
      * @return true if the command was valid and succeeded\r
      */\r
-    public boolean runCommand(String[] args) {\r
+    boolean runCommand(String[] args) {\r
         String cmd = args[0].trim().toLowerCase();\r
         if (cmd.equals("add")) {\r
             if (args.length > 2) {\r
@@ -238,8 +258,9 @@ public class DRRouteCLI {
 \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
+        if (args.length > 6) {\r
             url += "&seq=" + args[6];\r
+        }\r
         return doPost(url);\r
     }\r
 \r
@@ -281,31 +302,40 @@ public class DRRouteCLI {
         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
+            int cw1 = 6;\r
+            int cw2 = 6;\r
+            int cw3 = 6;\r
+            int 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
+                JSONObject jsonObject = irt.getJSONObject(i);\r
+                cw1 = Math.max(cw1, ("" + jsonObject.getInt("seq")).length());\r
+                cw2 = Math.max(cw2, ("" + jsonObject.getInt("feedid")).length());\r
+                String str = jsonObject.optString("user");\r
+                cw3 = Math.max(cw3, (str == null) ? 1 : str.length());\r
+                str = jsonObject.optString("subnet");\r
+                cw4 = Math.max(cw4, (str == null) ? 1 : str.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
+            sb.append(String.format("%s  %s  %s  %s  Nodes\n", ext("Seq", cw1),\r
+                    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
+                JSONObject jsonObject = irt.getJSONObject(i);\r
+                String seq = "" + jsonObject.getInt("seq");\r
+                String feedid = "" + jsonObject.getInt("feedid");\r
+                String user = jsonObject.optString("user");\r
+                String subnet = jsonObject.optString("subnet");\r
+                if (user.equals("")) {\r
+                    user = "-";\r
+                }\r
+                if (subnet.equals("")) {\r
+                    subnet = "-";\r
+                }\r
+                JSONArray nodes = jsonObject.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
+                sb.append(String.format("%s  %s  %s  %s  ", ext(seq, cw1),\r
+                        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
@@ -326,20 +356,22 @@ public class DRRouteCLI {
             // 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
+            if (subs == null) {\r
                 subs = new String[0];\r
+            }\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
+            if (sb.length() > 0) {\r
                 sb.append("\n");\r
+            }\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
-                if(ert!=null&&ert.length()!=0) {\r
+                if (ert != null && ert.length() != 0 ) {\r
                     String node = ert.getString(subs[i]);\r
                     sb.append(String.format("%s  %s\n", ext(subs[i], cw1), node));\r
                 }\r
@@ -349,24 +381,26 @@ public class DRRouteCLI {
         if (tbl.startsWith("al") || tbl.startsWith("ne")) {\r
             // Display the NRT\r
             JSONArray nrt = jo.optJSONArray("routing");\r
-            int cw1 = 4, cw2 = 4;\r
+            int cw1 = 4;\r
+            int 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
+                JSONObject jsonObject = nrt.getJSONObject(i);\r
+                String from = jsonObject.getString("from");\r
+                String to = jsonObject.getString("to");\r
                 cw1 = Math.max(cw1, from.length());\r
                 cw2 = Math.max(cw2, to.length());\r
             }\r
 \r
-            if (sb.length() > 0)\r
+            if (sb.length() > 0) {\r
                 sb.append("\n");\r
+            }\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
+                JSONObject jsonObject = nrt.getJSONObject(i);\r
+                String from = jsonObject.getString("from");\r
+                String to = jsonObject.getString("to");\r
+                String via = jsonObject.getString("via");\r
                 sb.append(String.format("%s  %s  %s\n", ext(from, cw1), ext(to, cw2), via));\r
             }\r
         }\r
@@ -374,12 +408,14 @@ public class DRRouteCLI {
         return true;\r
     }\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
+    private String ext(String str, int num) {\r
+        if (str == null) {\r
+            str = "-";\r
+        }\r
+        while (str.length() < num) {\r
+            str += " ";\r
+        }\r
+        return str;\r
     }\r
 \r
     private boolean doDelete(String url) {\r
@@ -397,6 +433,7 @@ public class DRRouteCLI {
                 printErrorText(entity);\r
             }\r
         } catch (Exception e) {\r
+            intlogger.error("PROV0006 doDelete: " + e.getMessage(), e);\r
         } finally {\r
             meth.releaseConnection();\r
         }\r
@@ -416,7 +453,7 @@ public class DRRouteCLI {
                 printErrorText(entity);\r
             }\r
         } catch (Exception e) {\r
-            System.err.println(e);\r
+            intlogger.error("PROV0005 doGet: " + e.getMessage(), e);\r
         } finally {\r
             meth.releaseConnection();\r
         }\r
@@ -438,28 +475,31 @@ public class DRRouteCLI {
                 printErrorText(entity);\r
             }\r
         } catch (Exception e) {\r
+            intlogger.error("PROV0009 doPost: " + e.getMessage(), e);\r
         } finally {\r
             meth.releaseConnection();\r
         }\r
         return rv;\r
     }\r
 \r
-    private void printErrorText(HttpEntity entity) throws IllegalStateException, IOException {\r
+    private void printErrorText(HttpEntity entity) throws 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
+        byte[] bite = new byte[512];\r
+        int num;\r
+        while ((num = is.read(bite)) > 0) {\r
+            sb.append(new String(bite, 0, num));\r
         }\r
         is.close();\r
         int ix = sb.indexOf("<pre>");\r
-        if (ix > 0)\r
+        if (ix > 0) {\r
             sb.delete(0, ix + 5);\r
+        }\r
         ix = sb.indexOf("</pre>");\r
-        if (ix > 0)\r
+        if (ix > 0) {\r
             sb.delete(ix, sb.length());\r
+        }\r
         System.err.println(sb.toString());\r
     }\r
 }\r