diff --git a/buildSrc/src/main/java/com/r3/csde/CordaStatusQueries.java b/buildSrc/src/main/java/com/r3/csde/CordaStatusQueries.java index 95072ba..38bf834 100644 --- a/buildSrc/src/main/java/com/r3/csde/CordaStatusQueries.java +++ b/buildSrc/src/main/java/com/r3/csde/CordaStatusQueries.java @@ -1,44 +1,22 @@ package com.r3.csde; +import kong.unirest.HttpResponse; import kong.unirest.JsonNode; import kong.unirest.Unirest; import kong.unirest.json.JSONArray; import kong.unirest.json.JSONObject; -import kong.unirest.HttpResponse; + +import java.util.Arrays; +import java.util.LinkedList; +import java.util.List; // todo: This class needs refactoring, see https://r3-cev.atlassian.net/browse/CORE-11624 public class CordaStatusQueries { ProjectContext pc; - public CordaStatusQueries(ProjectContext _pc){ pc = _pc; } - - public HttpResponse getVNodeInfo() { - Unirest.config().verifySsl(false); - return Unirest.get(pc.baseURL + "/api/v1/virtualnode/") - .basicAuth(pc.rpcUser, pc.rpcPasswd) - .asJson(); - } - public void listVNodesVerbose() { - HttpResponse vnodeResponse = getVNodeInfo(); - pc.out.println("VNodes:\n" + vnodeResponse.getBody().toPrettyString()); - } - - // X500Name, shorthash, cpiname - public void listVNodes() { - HttpResponse vnodeResponse = getVNodeInfo(); - - JSONArray virtualNodesJson = (JSONArray) vnodeResponse.getBody().getObject().get("virtualNodes"); - pc.out.println("X500 Name\tHolding identity short hash\tCPI Name"); - for(Object o: virtualNodesJson){ - if(o instanceof JSONObject) { - JSONObject idObj = ((JSONObject) o).getJSONObject("holdingIdentity"); - JSONObject cpiObj = ((JSONObject) o).getJSONObject("cpiIdentifier"); - pc.out.print("\"" + idObj.get("x500Name") + "\""); - pc.out.print("\t\"" + idObj.get("shortHash") + "\""); - pc.out.println("\t\"" + cpiObj.get("cpiName") + "\""); - } - } + public CordaStatusQueries(ProjectContext _pc) { + pc = _pc; } public HttpResponse getCpiInfo() { @@ -48,17 +26,92 @@ public class CordaStatusQueries { .asJson(); } - public void listCPIs() { - HttpResponse cpiResponse = getCpiInfo(); - JSONArray jArray = (JSONArray) cpiResponse.getBody().getObject().get("cpis"); - - for(Object o: jArray){ - if(o instanceof JSONObject) { - JSONObject idObj = ((JSONObject) o).getJSONObject("id"); - pc.out.print("cpiName=" + idObj.get("cpiName")); - pc.out.println(", cpiVersion=" + idObj.get("cpiVersion")); - } - } + public HttpResponse getVNodeInfo() { + Unirest.config().verifySsl(false); + return Unirest.get(pc.baseURL + "/api/v1/virtualnode/") + .basicAuth(pc.rpcUser, pc.rpcPasswd) + .asJson(); } + // cpiName, cpiVersion + public void listCPIs() { + HttpResponse cpiResponse = getCpiInfo(); + + JSONArray cpisJson = (JSONArray) cpiResponse.getBody().getObject().get("cpis"); + + List> lines = new LinkedList<>(); + for (Object o : cpisJson) { + if (o instanceof JSONObject) { + JSONObject idObj = ((JSONObject) o).getJSONObject("id"); + String cpiName = idObj.get("cpiName").toString(); + String cpiVersion = idObj.get("cpiVersion").toString(); + + lines.add(Arrays.asList(cpiName, cpiVersion)); + } + } + List title = Arrays.asList("CPI Name", "CPI Version"); + List titleSizes = Arrays.asList(40, 20); + printTable(titleSizes, title, lines); + } + + public void listVNodesVerbose() { + HttpResponse vnodeResponse = getVNodeInfo(); + pc.out.println("VNodes:\n" + vnodeResponse.getBody().toPrettyString()); + } + + // x500Name, shortHash, cpiName + public void listVNodes() { + HttpResponse vnodeResponse = getVNodeInfo(); + + JSONArray virtualNodesJson = (JSONArray) vnodeResponse.getBody().getObject().get("virtualNodes"); + + List> lines = new LinkedList<>(); + for (Object o : virtualNodesJson) { + if (o instanceof JSONObject) { + JSONObject idObj = ((JSONObject) o).getJSONObject("holdingIdentity"); + String x500Name = idObj.get("x500Name").toString(); + String shortHash = idObj.get("shortHash").toString(); + + JSONObject cpiObj = ((JSONObject) o).getJSONObject("cpiIdentifier"); + String cpiName = cpiObj.get("cpiName").toString(); + + lines.add(Arrays.asList(x500Name, shortHash, cpiName)); + } + } + List title = Arrays.asList("X500 Name", "Holding identity short hash", "CPI Name"); + List titleSizes = Arrays.asList(60, 30, 40); + printTable(titleSizes, title, lines); + } + + public void printTable(List titleSizes, List title, List> lines) { + int width = titleSizes.stream().reduce(0, Integer::sum); + String separator = "-".repeat(width + 1); + pc.out.println(separator); + pc.out.println(formatLine(titleSizes, title)); + pc.out.println(separator); + for (List line : lines) { + pc.out.println(formatLine(titleSizes, line)); + } + pc.out.println(separator); + } + + public String formatLine(List titleSizes, List line) { + StringBuilder sb = new StringBuilder(); + int delta = 0; + for (int i = 0; i < titleSizes.size(); i++) { + String s = line.get(i); + sb.append("| ").append(s); + delta += titleSizes.get(i) - (2 + s.length()); + + if (delta > 0) { + sb.append(" ".repeat(delta)); + delta = 0; + } else { + sb.append(" "); + delta -= 1; + } + } + sb.append("|"); + return sb.toString(); + } }