Bri Prebilic Cole
Committed by Gerrit Code Review

GUI -- Implemented Java backend device list sorting for tables.

Change-Id: I0ed18ce473e71dfc1b9188be47fe2f5062dd384f
......@@ -19,10 +19,8 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.onlab.rest.BaseResource;
import org.onosproject.net.Annotations;
import org.onosproject.net.Device;
import org.onosproject.net.device.DeviceService;
import org.slf4j.Logger;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
......@@ -31,8 +29,8 @@ import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;
import java.util.ArrayList;
import static org.slf4j.LoggerFactory.getLogger;
import java.util.Arrays;
import java.util.List;
/**
* UI REST resource for interacting with the inventory of infrastructure devices.
......@@ -40,87 +38,42 @@ import static org.slf4j.LoggerFactory.getLogger;
@Path("device")
public class DeviceGuiResource extends BaseResource {
private static final String ICON_ID_ONLINE = "deviceOnline";
private static final String ICON_ID_OFFLINE = "deviceOffline";
private static final Logger log = getLogger(DeviceGuiResource.class);
private static final String DEVICES = "devices";
private final ObjectMapper mapper = new ObjectMapper();
private static final ObjectMapper MAPPER = new ObjectMapper();
// return list of devices
@GET
@Produces("application/json")
public Response getDevices(
@DefaultValue("none") @QueryParam("sortCol") String colId,
@DefaultValue("none") @QueryParam("sortDir") String dir
@DefaultValue("id") @QueryParam("sortCol") String colId,
@DefaultValue("asc") @QueryParam("sortDir") String dir
) {
ObjectNode rootNode = mapper.createObjectNode();
ArrayNode devices = mapper.createArrayNode();
DeviceService service = get(DeviceService.class);
TableRow[] rows = generateTableRows(service);
RowComparator rc = new RowComparator(colId, RowComparator.direction(dir));
Arrays.sort(rows, rc);
ArrayNode devices = generateArrayNode(rows);
ObjectNode rootNode = MAPPER.createObjectNode();
rootNode.set(DEVICES, devices);
// if no query parameters were given, get the data in whatever order
if (colId.equals("none") || dir.equals("none")) {
for (Device dev : service.getDevices()) {
devices.add(deviceJson(service, dev));
}
} else {
ArrayList<Device> sortedDevices = new ArrayList<>();
for (Device dev : service.getDevices()) {
sortedDevices.add(dev);
}
// now sort the arrayList based on the query parameters
// then put each item into the ArrayNode devices
// (pass in each device to deviceJson)
// at this point, the sortedDevices list will be sorted
for (Device dev : sortedDevices) {
devices.add(deviceJson(service, dev));
}
}
rootNode.set("devices", devices);
return Response.ok(rootNode.toString()).build();
}
/**
* Returns a JSON node representing the specified device.
*
* @param device infrastructure device
* @return JSON node
*/
private ObjectNode deviceJson(DeviceService service, Device device) {
boolean available = service.isAvailable(device.id());
// pick the appropriate id for the icon to appear in the table row
String iconId = available ? ICON_ID_ONLINE : ICON_ID_OFFLINE;
ObjectNode result = mapper.createObjectNode();
result.put("id", device.id().toString())
.put("available", available)
.put("_iconid_available", iconId)
.put("type", device.type().toString())
.put("role", service.getRole(device.id()).toString())
.put("mfr", device.manufacturer())
.put("hw", device.hwVersion())
.put("sw", device.swVersion())
.put("serial", device.serialNumber())
.set("annotations", annotations(mapper, device.annotations()));
return result;
private ArrayNode generateArrayNode(TableRow[] rows) {
ArrayNode devices = MAPPER.createArrayNode();
for (TableRow r : rows) {
devices.add(r.toJsonNode());
}
return devices;
}
/**
* Produces a JSON object from the specified key/value annotations.
*
* @param mapper ObjectMapper to use while converting to JSON
* @param annotations key/value annotations
* @return JSON object
*/
private static ObjectNode annotations(ObjectMapper mapper, Annotations annotations) {
ObjectNode result = mapper.createObjectNode();
for (String key : annotations.keys()) {
result.put(key, annotations.value(key));
private TableRow[] generateTableRows(DeviceService service) {
List<TableRow> list = new ArrayList<>();
for (Device dev : service.getDevices()) {
list.add(new DeviceTableRow(service, dev));
}
return result;
return list.toArray(new TableRow[list.size()]);
}
}
......
/*
* Copyright 2015 Open Networking Laboratory
*
* 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.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.gui;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.onosproject.net.Device;
import org.onosproject.net.device.DeviceService;
import java.util.HashMap;
import java.util.Map;
public class DeviceTableRow implements TableRow {
private static final String ID = "id";
private static final String AVAILABLE = "available";
private static final String AVAILABLE_IID = "_iconid_available";
private static final String TYPE = "type";
private static final String ROLE = "role";
private static final String MFR = "mfr";
private static final String HW = "hw";
private static final String SW = "sw";
private static final String SERIAL = "serial";
private static final String PROTOCOL = "protocol";
private static final String ICON_ID_ONLINE = "deviceOnline";
private static final String ICON_ID_OFFLINE = "deviceOffline";
private static final ObjectMapper MAPPER = new ObjectMapper();
private final Map<String, String> data = new HashMap<>();
public DeviceTableRow(DeviceService service, Device d) {
boolean available = service.isAvailable(d.id());
String iconId = available ? ICON_ID_ONLINE : ICON_ID_OFFLINE;
data.put(ID, d.id().toString());
data.put(AVAILABLE, Boolean.toString(available));
data.put(AVAILABLE_IID, iconId);
data.put(TYPE, d.type().toString());
data.put(ROLE, service.getRole(d.id()).toString());
data.put(MFR, d.manufacturer());
data.put(HW, d.hwVersion());
data.put(SW, d.swVersion());
data.put(SERIAL, d.serialNumber());
data.put(PROTOCOL, d.annotations().value(PROTOCOL));
}
@Override
public String get(String key) {
return data.get(key);
}
@Override
public ObjectNode toJsonNode() {
ObjectNode result = MAPPER.createObjectNode();
result.put(ID, data.get(ID));
result.put(AVAILABLE, data.get(AVAILABLE));
result.put(AVAILABLE_IID, data.get(AVAILABLE_IID));
result.put(TYPE, data.get(TYPE));
result.put(ROLE, data.get(ROLE));
result.put(MFR, data.get(MFR));
result.put(HW, data.get(HW));
result.put(SW, data.get(SW));
result.put(SERIAL, data.get(SERIAL));
result.put(PROTOCOL, data.get(PROTOCOL));
return result;
}
}
/*
* Copyright 2015 Open Networking Laboratory
*
* 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.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.gui;
import java.util.Comparator;
/**
* Comparator for {@link TableRow}.
*/
public class RowComparator implements Comparator<TableRow> {
public static enum Direction { ASC, DESC }
public static final String DESC_STR = "desc";
private final String colId;
private final Direction dir;
public RowComparator(String colId, Direction dir) {
if (colId == null || dir == null) {
throw new NullPointerException("Null parameters not allowed");
}
this.colId = colId;
this.dir = dir;
}
@Override
public int compare(TableRow a, TableRow b) {
String cellA = a.get(colId);
String cellB = b.get(colId);
if (dir.equals(Direction.ASC)) {
return cellA.compareTo(cellB);
}
return cellB.compareTo(cellA);
}
/**
* Returns the sort direction constant for the given string.
* The expected strings are "asc" and "desc"; defaults to "asc".
*
* @param s the direction as a string
* @return the constant
*/
public static Direction direction(String s) {
return DESC_STR.equals(s) ? Direction.DESC : Direction.ASC;
}
}
/*
* Copyright 2015 Open Networking Laboratory
*
* 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.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.gui;
import com.fasterxml.jackson.databind.node.ObjectNode;
/**
* Defines a table row abstraction to support sortable tables on the GUI.
*/
public interface TableRow {
/**
* Returns the value of the cell for the given column ID.
*
* @param key the column ID
* @return the cell value
*/
String get(String key);
/**
* Returns this table row in the form of a JSON object.
*
* @return the JSON node
*/
ObjectNode toJsonNode();
}
......@@ -9,7 +9,7 @@
<thead>
<tr>
<th colId="available"></th>
<th colId="id" sortable>URI</th>
<th colId="id" sortable>Device ID</th>
<th colId="mfr" sortable>Vendor</th>
<th colId="hw" sortable>Hardware Version</th>
<th colId="sw" sortable>Software Version</th>
......@@ -27,7 +27,7 @@
<td>{{dev.hw}}</td>
<td>{{dev.sw}}</td>
<td>{{dev.serial}}</td>
<td>{{dev.annotations.protocol}}</td>
<td>{{dev.protocol}}</td>
</tr>
</tbody>
</table>
......