GUI -- refactored all the table views (server side) to use the new TableModel method of data generation.
Change-Id: Ib8a188ad432ff335db6cff1e49e08dbaf039436b
diff --git a/core/api/src/main/java/org/onosproject/ui/table/AbstractTableRow.java b/core/api/src/main/java/org/onosproject/ui/table/AbstractTableRow.java
index 5dd11a4..9f456b9 100644
--- a/core/api/src/main/java/org/onosproject/ui/table/AbstractTableRow.java
+++ b/core/api/src/main/java/org/onosproject/ui/table/AbstractTableRow.java
@@ -26,6 +26,7 @@
/**
* Provides a partial implementation of {@link TableRow}.
*/
+@Deprecated
public abstract class AbstractTableRow implements TableRow {
private static final ObjectMapper MAPPER = new ObjectMapper();
diff --git a/core/api/src/main/java/org/onosproject/ui/table/RowComparator.java b/core/api/src/main/java/org/onosproject/ui/table/RowComparator.java
index 9859a65..86f25c1 100644
--- a/core/api/src/main/java/org/onosproject/ui/table/RowComparator.java
+++ b/core/api/src/main/java/org/onosproject/ui/table/RowComparator.java
@@ -21,6 +21,7 @@
/**
* Comparator for {@link TableRow}.
*/
+@Deprecated
public class RowComparator implements Comparator<TableRow> {
/** Designates the sort direction. */
public enum Direction {
diff --git a/core/api/src/main/java/org/onosproject/ui/table/TableModel.java b/core/api/src/main/java/org/onosproject/ui/table/TableModel.java
index 2c3ffc5..91e23be 100644
--- a/core/api/src/main/java/org/onosproject/ui/table/TableModel.java
+++ b/core/api/src/main/java/org/onosproject/ui/table/TableModel.java
@@ -46,8 +46,8 @@
*/
public class TableModel {
- private static final CellComparator DEF_CMP = new DefaultCellComparator();
- private static final CellFormatter DEF_FMT = new DefaultCellFormatter();
+ private static final CellComparator DEF_CMP = DefaultCellComparator.INSTANCE;
+ private static final CellFormatter DEF_FMT = DefaultCellFormatter.INSTANCE;
private final String[] columnIds;
private final Set<String> idSet;
@@ -99,13 +99,16 @@
}
/**
- * Returns the {@link TableRow} representation of the rows in this table.
+ * Returns the array of column IDs for this table model.
+ * <p>
+ * Implementation note: we are knowingly passing you a reference to
+ * our internal array to avoid copying. Don't mess with it. It's your
+ * table you'll break if you do!
*
- * @return formatted table rows
+ * @return the column identifiers
*/
- // TODO: still need to decide if we need this
- public TableRow[] getTableRows() {
- return new TableRow[0];
+ public String[] getColumnIds() {
+ return columnIds;
}
/**
@@ -113,7 +116,6 @@
*
* @return raw table rows
*/
- // TODO: still need to decide if we should expose this
public Row[] getRows() {
return rows.toArray(new Row[rows.size()]);
}
@@ -264,9 +266,22 @@
* @param columnId column identifier
* @return formatted cell value
*/
- public String getAsString(String columnId) {
+ String getAsString(String columnId) {
return getFormatter(columnId).format(get(columnId));
}
+
+ /**
+ * Returns the row as an array of formatted strings.
+ *
+ * @return the formatted row data
+ */
+ public String[] getAsFormattedStrings() {
+ List<String> formatted = new ArrayList<>(columnCount());
+ for (String c : columnIds) {
+ formatted.add(getAsString(c));
+ }
+ return formatted.toArray(new String[formatted.size()]);
+ }
}
private static final String DESC = "desc";
diff --git a/core/api/src/main/java/org/onosproject/ui/table/TableRequestHandler.java b/core/api/src/main/java/org/onosproject/ui/table/TableRequestHandler.java
index f90c187..b8d4857 100644
--- a/core/api/src/main/java/org/onosproject/ui/table/TableRequestHandler.java
+++ b/core/api/src/main/java/org/onosproject/ui/table/TableRequestHandler.java
@@ -17,10 +17,9 @@
package org.onosproject.ui.table;
import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.onosproject.ui.JsonUtils;
import org.onosproject.ui.RequestHandler;
-import java.util.Arrays;
-
/**
* Message handler specifically for table views.
*/
@@ -47,29 +46,66 @@
@Override
public void process(long sid, ObjectNode payload) {
- RowComparator rc = TableUtils.createRowComparator(payload, defaultColId());
- TableRow[] rows = generateTableRows(payload);
- Arrays.sort(rows, rc);
+ TableModel tm = createTableModel();
+ populateTable(tm, payload);
+
+ String sortCol = JsonUtils.string(payload, "sortCol", defaultColumnId());
+ String sortDir = JsonUtils.string(payload, "sortDir", "asc");
+ tm.sort(sortCol, TableModel.sortDir(sortDir));
+
ObjectNode rootNode = MAPPER.createObjectNode();
- rootNode.set(nodeName, TableUtils.generateArrayNode(rows));
+ rootNode.set(nodeName, TableUtils.generateArrayNode(tm));
sendMessage(respType, 0, rootNode);
}
/**
- * Returns the default column ID, when one is not supplied in the payload
- * defining the column on which to sort. This implementation returns "id".
+ * Creates the table model (devoid of data) using {@link #getColumnIds()}
+ * to initialize it, ready to be populated.
+ * <p>
+ * This default implementation returns a table model with default
+ * formatters and comparators for all columns.
*
- * @return default sort column id
+ * @return an empty table model
*/
- protected String defaultColId() {
+ protected TableModel createTableModel() {
+ return new TableModel(getColumnIds());
+ }
+
+ /**
+ * Returns the default column ID to be used when one is not supplied in
+ * the payload as the column on which to sort.
+ * <p>
+ * This default implementation returns "id".
+ *
+ * @return default sort column identifier
+ */
+ protected String defaultColumnId() {
return "id";
}
/**
- * Subclasses should generate table rows for their specific table instance.
+ * Subclasses should return the array of column IDs with which
+ * to initialize their table model.
*
- * @param payload provided in case custom parameters are present
- * @return generated table rows
+ * @return the column IDs
*/
- protected abstract TableRow[] generateTableRows(ObjectNode payload);
+ protected abstract String[] getColumnIds();
+
+ /**
+ * Subclasses should populate the table model by adding
+ * {@link TableModel.Row rows}.
+ * <pre>
+ * tm.addRow()
+ * .cell(COL_ONE, ...)
+ * .cell(COL_TWO, ...)
+ * ... ;
+ * </pre>
+ * The request payload is provided in case there are request filtering
+ * parameters (other than sort column and sort direction) that are required
+ * to generate the appropriate data.
+ *
+ * @param tm the table model
+ * @param payload request payload
+ */
+ protected abstract void populateTable(TableModel tm, ObjectNode payload);
}
diff --git a/core/api/src/main/java/org/onosproject/ui/table/TableRow.java b/core/api/src/main/java/org/onosproject/ui/table/TableRow.java
index 4775687..1824be8 100644
--- a/core/api/src/main/java/org/onosproject/ui/table/TableRow.java
+++ b/core/api/src/main/java/org/onosproject/ui/table/TableRow.java
@@ -22,6 +22,7 @@
/**
* Defines a table row abstraction to support sortable tables on the GUI.
*/
+@Deprecated
public interface TableRow {
// TODO: Define TableCell interface and return that, rather than String
diff --git a/core/api/src/main/java/org/onosproject/ui/table/TableUtils.java b/core/api/src/main/java/org/onosproject/ui/table/TableUtils.java
index 9f63cba..eb2dff7 100644
--- a/core/api/src/main/java/org/onosproject/ui/table/TableUtils.java
+++ b/core/api/src/main/java/org/onosproject/ui/table/TableUtils.java
@@ -16,10 +16,10 @@
package org.onosproject.ui.table;
+import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
-import org.onosproject.ui.JsonUtils;
/**
* Provides static utility methods for dealing with tables.
@@ -32,46 +32,27 @@
private TableUtils() { }
/**
- * Produces a JSON array node from the specified table rows.
+ * Generates a JSON array node from a table model.
*
- * @param rows table rows
- * @return JSON array
+ * @param tm the table model
+ * @return the array node representation
*/
- public static ArrayNode generateArrayNode(TableRow[] rows) {
+ public static ArrayNode generateArrayNode(TableModel tm) {
ArrayNode array = MAPPER.createArrayNode();
- for (TableRow r : rows) {
- array.add(r.toJsonNode());
+ for (TableModel.Row r : tm.getRows()) {
+ array.add(toJsonNode(r, tm));
}
return array;
}
- /**
- * Creates a row comparator for the given request. The ID of the column
- * to sort on is the payload's "sortCol" property (defaults to "id").
- * The direction for the sort is the payload's "sortDir" property
- * (defaults to "asc").
- *
- * @param payload the event payload
- * @return a row comparator
- */
- public static RowComparator createRowComparator(ObjectNode payload) {
- return createRowComparator(payload, "id");
- }
-
- /**
- * Creates a row comparator for the given request. The ID of the column to
- * sort on is the payload's "sortCol" property (or the specified default).
- * The direction for the sort is the payload's "sortDir" property
- * (defaults to "asc").
- *
- * @param payload the event payload
- * @param defColId the default column ID
- * @return a row comparator
- */
- public static RowComparator createRowComparator(ObjectNode payload,
- String defColId) {
- String sortCol = JsonUtils.string(payload, "sortCol", defColId);
- String sortDir = JsonUtils.string(payload, "sortDir", "asc");
- return new RowComparator(sortCol, RowComparator.direction(sortDir));
+ private static JsonNode toJsonNode(TableModel.Row row, TableModel tm) {
+ ObjectNode result = MAPPER.createObjectNode();
+ String[] keys = tm.getColumnIds();
+ String[] cells = row.getAsFormattedStrings();
+ int n = keys.length;
+ for (int i = 0; i < n; i++) {
+ result.put(keys[i], cells[i]);
+ }
+ return result;
}
}
diff --git a/core/api/src/main/java/org/onosproject/ui/table/cell/AppIdFormatter.java b/core/api/src/main/java/org/onosproject/ui/table/cell/AppIdFormatter.java
new file mode 100644
index 0000000..f6ccf2c0
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/ui/table/cell/AppIdFormatter.java
@@ -0,0 +1,38 @@
+/*
+ * 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.ui.table.cell;
+
+import org.onosproject.core.ApplicationId;
+import org.onosproject.ui.table.CellFormatter;
+
+/**
+ * Formats an application identifier as "(app-id) : (app-name)".
+ */
+public class AppIdFormatter extends AbstractCellFormatter {
+
+ @Override
+ protected String nonNullFormat(Object value) {
+ ApplicationId appId = (ApplicationId) value;
+ return appId.id() + " : " + appId.name();
+ }
+
+ /**
+ * An instance of this class.
+ */
+ public static final CellFormatter INSTANCE = new AppIdFormatter();
+}
diff --git a/core/api/src/main/java/org/onosproject/ui/table/cell/ConnectPointFormatter.java b/core/api/src/main/java/org/onosproject/ui/table/cell/ConnectPointFormatter.java
new file mode 100644
index 0000000..18f7233
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/ui/table/cell/ConnectPointFormatter.java
@@ -0,0 +1,38 @@
+/*
+ * 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.ui.table.cell;
+
+import org.onosproject.net.ConnectPoint;
+import org.onosproject.ui.table.CellFormatter;
+
+/**
+ * Formats a connect point as "(element-id)/(port)".
+ */
+public class ConnectPointFormatter extends AbstractCellFormatter {
+
+ @Override
+ protected String nonNullFormat(Object value) {
+ ConnectPoint cp = (ConnectPoint) value;
+ return cp.elementId() + "/" + cp.port();
+ }
+
+ /**
+ * An instance of this class.
+ */
+ public static final CellFormatter INSTANCE = new ConnectPointFormatter();
+}
diff --git a/core/api/src/main/java/org/onosproject/ui/table/cell/DefaultCellComparator.java b/core/api/src/main/java/org/onosproject/ui/table/cell/DefaultCellComparator.java
index 7846f89..fa49755 100644
--- a/core/api/src/main/java/org/onosproject/ui/table/cell/DefaultCellComparator.java
+++ b/core/api/src/main/java/org/onosproject/ui/table/cell/DefaultCellComparator.java
@@ -17,6 +17,8 @@
package org.onosproject.ui.table.cell;
+import org.onosproject.ui.table.CellComparator;
+
/**
* A default cell comparator. Implements a lexicographical compare function
* (i.e. string sorting). Uses the objects' toString() method and then
@@ -24,8 +26,14 @@
* are considered "smaller" than any non-null value.
*/
public class DefaultCellComparator extends AbstractCellComparator {
+
@Override
protected int nonNullCompare(Object o1, Object o2) {
return o1.toString().compareTo(o2.toString());
}
+
+ /**
+ * An instance of this class.
+ */
+ public static final CellComparator INSTANCE = new DefaultCellComparator();
}
diff --git a/core/api/src/main/java/org/onosproject/ui/table/cell/DefaultCellFormatter.java b/core/api/src/main/java/org/onosproject/ui/table/cell/DefaultCellFormatter.java
index c030236..5e4f5ba 100644
--- a/core/api/src/main/java/org/onosproject/ui/table/cell/DefaultCellFormatter.java
+++ b/core/api/src/main/java/org/onosproject/ui/table/cell/DefaultCellFormatter.java
@@ -17,12 +17,20 @@
package org.onosproject.ui.table.cell;
+import org.onosproject.ui.table.CellFormatter;
+
/**
* A default cell formatter. Uses the object's toString() method.
*/
public class DefaultCellFormatter extends AbstractCellFormatter {
+
@Override
public String nonNullFormat(Object value) {
return value.toString();
}
+
+ /**
+ * An instance of this class.
+ */
+ public static final CellFormatter INSTANCE = new DefaultCellFormatter();
}
diff --git a/core/api/src/main/java/org/onosproject/ui/table/cell/HexFormatter.java b/core/api/src/main/java/org/onosproject/ui/table/cell/HexFormatter.java
index d4aaade..8104f48 100644
--- a/core/api/src/main/java/org/onosproject/ui/table/cell/HexFormatter.java
+++ b/core/api/src/main/java/org/onosproject/ui/table/cell/HexFormatter.java
@@ -17,12 +17,20 @@
package org.onosproject.ui.table.cell;
+import org.onosproject.ui.table.CellFormatter;
+
/**
- * Formats integer values as hex strings.
+ * Formats integer values as hex strings with a "0x" prefix.
*/
public class HexFormatter extends AbstractCellFormatter {
+
@Override
protected String nonNullFormat(Object value) {
return "0x" + Integer.toHexString((Integer) value);
}
+
+ /**
+ * An instance of this class.
+ */
+ public static final CellFormatter INSTANCE = new HexFormatter();
}
diff --git a/core/api/src/main/java/org/onosproject/ui/table/cell/HostLocationFormatter.java b/core/api/src/main/java/org/onosproject/ui/table/cell/HostLocationFormatter.java
new file mode 100644
index 0000000..9d6024d
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/ui/table/cell/HostLocationFormatter.java
@@ -0,0 +1,38 @@
+/*
+ * 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.ui.table.cell;
+
+import org.onosproject.net.HostLocation;
+import org.onosproject.ui.table.CellFormatter;
+
+/**
+ * Formats a host location as "(device-id)/(port)".
+ */
+public class HostLocationFormatter extends AbstractCellFormatter {
+
+ @Override
+ protected String nonNullFormat(Object value) {
+ HostLocation loc = (HostLocation) value;
+ return loc.deviceId() + "/" + loc.port();
+ }
+
+ /**
+ * An instance of this class.
+ */
+ public static final CellFormatter INSTANCE = new HostLocationFormatter();
+}
diff --git a/core/api/src/main/java/org/onosproject/ui/table/cell/IntComparator.java b/core/api/src/main/java/org/onosproject/ui/table/cell/IntComparator.java
index c399d53..ddd90eb 100644
--- a/core/api/src/main/java/org/onosproject/ui/table/cell/IntComparator.java
+++ b/core/api/src/main/java/org/onosproject/ui/table/cell/IntComparator.java
@@ -17,14 +17,22 @@
package org.onosproject.ui.table.cell;
+import org.onosproject.ui.table.CellComparator;
+
/**
* An integer-based cell comparator.
* Note that null values are acceptable and are considered "smaller" than
* any non-null value.
*/
public class IntComparator extends AbstractCellComparator {
+
@Override
protected int nonNullCompare(Object o1, Object o2) {
return ((int) o1) - ((int) o2);
}
+
+ /**
+ * An instance of this class.
+ */
+ public static final CellComparator INSTANCE = new IntComparator();
}
diff --git a/core/api/src/main/java/org/onosproject/ui/table/cell/LongComparator.java b/core/api/src/main/java/org/onosproject/ui/table/cell/LongComparator.java
new file mode 100644
index 0000000..3234e50
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/ui/table/cell/LongComparator.java
@@ -0,0 +1,39 @@
+/*
+ * 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.ui.table.cell;
+
+import org.onosproject.ui.table.CellComparator;
+
+/**
+ * A long-based cell comparator.
+ * Note that null values are acceptable and are considered "smaller" than
+ * any non-null value.
+ */
+public class LongComparator extends AbstractCellComparator {
+
+ @Override
+ protected int nonNullCompare(Object o1, Object o2) {
+ long diff = ((long) o1) - ((long) o2);
+ return diff == 0 ? 0 : (diff < 0 ? -1 : 1);
+ }
+
+ /**
+ * An instance of this class.
+ */
+ public static final CellComparator INSTANCE = new LongComparator();
+}
diff --git a/core/api/src/main/java/org/onosproject/ui/table/cell/TimeFormatter.java b/core/api/src/main/java/org/onosproject/ui/table/cell/TimeFormatter.java
new file mode 100644
index 0000000..e0aec9d
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/ui/table/cell/TimeFormatter.java
@@ -0,0 +1,41 @@
+/*
+ * 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.ui.table.cell;
+
+import org.joda.time.DateTime;
+import org.joda.time.format.DateTimeFormat;
+import org.joda.time.format.DateTimeFormatter;
+import org.onosproject.ui.table.CellFormatter;
+
+/**
+ * Formats time values using {@link DateTimeFormatter}.
+ */
+public class TimeFormatter extends AbstractCellFormatter {
+
+ private static final DateTimeFormatter DTF = DateTimeFormat.longTime();
+
+ @Override
+ protected String nonNullFormat(Object value) {
+ return DTF.print((DateTime) value);
+ }
+
+ /**
+ * An instance of this class.
+ */
+ public static final CellFormatter INSTANCE = new TimeFormatter();
+}