/** * static method to convert host to JsonObject * * @param host * @return */ public static JSONObject psHost2Json(PsHost host, String detailLevel) { JSONObject json = new JSONObject(); json.put(PsHost.ID, int2String(host.getId())); json.put(PsHost.HOSTNAME, host.getHostname()); if (!PsApi.DETAIL_LEVEL_LOW.equals(detailLevel)) { json.put(PsHost.IPV4, host.getIpv4()); json.put(PsHost.IPV6, host.getIpv6()); JSONArray services = new JSONArray(); Iterator<PsService> iter = host.serviceIterator(); while (iter.hasNext()) { PsService service = (PsService) iter.next(); if (PsApi.DETAIL_LEVEL_MEDIUM.equals(detailLevel)) { JSONObject serviceObject = toJson(service, PsApi.DETAIL_LEVEL_LOW); services.add(serviceObject); } if (PsApi.DETAIL_LEVEL_HIGH.equals(detailLevel)) { JSONObject serviceObject = toJson(service, PsApi.DETAIL_LEVEL_HIGH); services.add(serviceObject); } } json.put(PsHost.SERVICES, services); } return json; }
public static JSONObject toJson(PsSite site, String detailLevel) { JSONObject json = new JSONObject(); if (site != null) { json.put(PsSite.ID, int2String(site.getId())); json.put(PsSite.NAME, site.getName()); if (!PsApi.DETAIL_LEVEL_LOW.equals(detailLevel)) { json.put(PsSite.DESCRIPTION, site.getDescription()); json.put(PsSite.STATUS, site.getStatus()); JSONArray listOfHosts = new JSONArray(); Collection<PsHost> listOfHostsInSite = site.getHosts(); Iterator<PsHost> iter = listOfHostsInSite.iterator(); while (iter.hasNext()) { PsHost currentHost = (PsHost) iter.next(); if (PsApi.DETAIL_LEVEL_MEDIUM.equals(detailLevel)) { JSONObject currentHostJson = toJson(currentHost, PsApi.DETAIL_LEVEL_LOW); listOfHosts.add(currentHostJson); } if (PsApi.DETAIL_LEVEL_HIGH.equals(detailLevel)) { JSONObject currentHostJson = toJson(currentHost, PsApi.DETAIL_LEVEL_HIGH); listOfHosts.add(currentHostJson); } } json.put(PsSite.HOSTS, listOfHosts); } } return json; }
public static JSONObject toJson(PsCloud cloud, String detailLevel) { JSONObject json = new JSONObject(); if (cloud != null) { json.put(PsCloud.ID, int2String(cloud.getId())); json.put(PsCloud.NAME, cloud.getName()); if (!PsApi.DETAIL_LEVEL_LOW.equals(detailLevel)) { json.put(PsCloud.STATUS, cloud.getStatus()); JSONArray sites = new JSONArray(); Iterator<PsSite> iter = cloud.sitesIterator(); while (iter.hasNext()) { PsSite currentSite = (PsSite) iter.next(); if (PsApi.DETAIL_LEVEL_MEDIUM.equals(detailLevel)) { sites.add(toJson(currentSite, PsApi.DETAIL_LEVEL_LOW)); } if (PsApi.DETAIL_LEVEL_HIGH.equals(detailLevel)) { sites.add(toJson(currentSite, PsApi.DETAIL_LEVEL_HIGH)); } } json.put(PsCloud.SITES, sites); JSONArray matrices = new JSONArray(); Iterator<PsMatrix> iter2 = cloud.matrixIterator(); while (iter2.hasNext()) { PsMatrix currentMatrix = (PsMatrix) iter2.next(); if (PsApi.DETAIL_LEVEL_MEDIUM.equals(detailLevel)) { matrices.add(toJson(currentMatrix, PsApi.DETAIL_LEVEL_LOW)); } if (PsApi.DETAIL_LEVEL_HIGH.equals(detailLevel)) { matrices.add(toJson(currentMatrix, PsApi.DETAIL_LEVEL_HIGH)); } } json.put(PsCloud.MATRICES, matrices); } } return json; }
public static JSONObject toJson(PsMatrix matrix, String detailLevel) { JSONObject json = new JSONObject(); if (matrix != null) { json.put(PsMatrix.ID, int2String(matrix.getId())); json.put(PsMatrix.NAME, matrix.getName()); if (!PsApi.DETAIL_LEVEL_LOW.equals(detailLevel)) { json.put(PsMatrix.DETAIL_LEVEL, matrix.getDetailLevel()); json.put(PsMatrix.SERVICE_TYPE_ID, matrix.getMatrixType().getServiceTypeId()); List<String> statusLabels = matrix.getStatusLabels(); JSONArray jsonArray = new JSONArray(); if (statusLabels != null) { if (!statusLabels.isEmpty()) { for (int i = 0; i < statusLabels.size(); i = i + 1) { jsonArray.add(statusLabels.get(i)); } } } json.put(PsMatrix.STATUS_LABELS, jsonArray); Date lastUpdateTime = matrix.getLastUpdateTime(); json.put(PsMatrix.LAST_UPDATE_TIME, IsoDateConverter.dateToString(lastUpdateTime)); JSONArray rows = new JSONArray(); for (int i = 0; i < matrix.getNumberOfRows(); i = i + 1) { PsHost currentHost = matrix.getHostInRow(i); JSONObject hostAsJson = toJson(currentHost, PsApi.DETAIL_LEVEL_LOW); // rows.add(currentHost.getHostname()); rows.add(hostAsJson); } json.put(PsMatrix.ROWS, rows); JSONArray columns = new JSONArray(); for (int i = 0; i < matrix.getNumberOfColumns(); i = i + 1) { PsHost currentHost = matrix.getHostInColumn(i); JSONObject hostAsJson = toJson(currentHost, PsApi.DETAIL_LEVEL_LOW); // columns.add(currentHost.getHostname()); columns.add(hostAsJson); } json.put(PsMatrix.COLUMNS, columns); JSONArray serviceNames = new JSONArray(); for (int i = 0; i < matrix.getNumberOfServiceNames(); i = i + 1) { serviceNames.add(matrix.getServiceNames().get(i)); } json.put(PsMatrix.SERVICE_NAMES, serviceNames); JSONArray matrixArray = new JSONArray(); for (int i = 0; i < matrix.getNumberOfColumns(); i = i + 1) { JSONArray rowsArray = new JSONArray(); for (int j = 0; j < matrix.getNumberOfRows(); j = j + 1) { JSONArray serviceArray = new JSONArray(); for (int k = 0; k < matrix.getNumberOfServiceNames(); k = k + 1) { PsService currentService = matrix.getService(i, j, k); String serviceDetailLevel = PsApi.DETAIL_LEVEL_HIGH; if (PsApi.DETAIL_LEVEL_MEDIUM.equals(detailLevel)) { serviceDetailLevel = PsApi.DETAIL_LEVEL_LOW; } if (PsApi.DETAIL_LEVEL_HIGH.equals(detailLevel)) { serviceDetailLevel = PsApi.DETAIL_LEVEL_HIGH; } JSONObject currentServiceJson = JsonConverter.toJson(currentService, serviceDetailLevel); serviceArray.add(currentServiceJson); } rowsArray.add(serviceArray); } matrixArray.add(rowsArray); } json.put(PsMatrix.MATRIX, matrixArray); } } return json; }