blob: 5cbf01ff9a62059e39e811a7b712cf9d4ce5018c [file] [log] [blame]
Simon Huntabd16f62015-05-01 13:14:40 -07001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onosproject.ui.table;
18
19import com.fasterxml.jackson.databind.node.ObjectNode;
Simon Hunt3d1b0652015-05-05 17:27:24 -070020import org.onosproject.ui.JsonUtils;
Simon Huntabd16f62015-05-01 13:14:40 -070021import org.onosproject.ui.RequestHandler;
22
Simon Huntabd16f62015-05-01 13:14:40 -070023/**
24 * Message handler specifically for table views.
25 */
26public abstract class TableRequestHandler extends RequestHandler {
27
Jian Li69f66632016-01-15 12:27:42 -080028 private static final String ANNOTS = "annots";
29 private static final String NO_ROWS_MSG_KEY = "no_rows_msg";
Simon Huntabd16f62015-05-01 13:14:40 -070030 private final String respType;
31 private final String nodeName;
32
33 /**
34 * Constructs a table request handler for a specific table view. When
35 * table requests come in, the handler will generate the appropriate
36 * table rows, sort them according the the request sort parameters, and
37 * send back the response to the client.
38 *
39 * @param reqType type of the request event
40 * @param respType type of the response event
41 * @param nodeName name of JSON node holding row data
42 */
43 public TableRequestHandler(String reqType, String respType, String nodeName) {
44 super(reqType);
45 this.respType = respType;
46 this.nodeName = nodeName;
47 }
48
49 @Override
50 public void process(long sid, ObjectNode payload) {
Simon Hunt3d1b0652015-05-05 17:27:24 -070051 TableModel tm = createTableModel();
52 populateTable(tm, payload);
53
54 String sortCol = JsonUtils.string(payload, "sortCol", defaultColumnId());
55 String sortDir = JsonUtils.string(payload, "sortDir", "asc");
56 tm.sort(sortCol, TableModel.sortDir(sortDir));
57
Jian Li8baf4472016-01-15 15:08:09 -080058 addTableConfigAnnotations(tm, payload);
Jian Li69f66632016-01-15 12:27:42 -080059
Simon Huntabd16f62015-05-01 13:14:40 -070060 ObjectNode rootNode = MAPPER.createObjectNode();
Jian Li69f66632016-01-15 12:27:42 -080061 rootNode.set(nodeName, TableUtils.generateRowArrayNode(tm));
62 rootNode.set(ANNOTS, TableUtils.generateAnnotObjectNode(tm));
Simon Huntabd16f62015-05-01 13:14:40 -070063 sendMessage(respType, 0, rootNode);
64 }
65
66 /**
Simon Hunt3d1b0652015-05-05 17:27:24 -070067 * Creates the table model (devoid of data) using {@link #getColumnIds()}
68 * to initialize it, ready to be populated.
69 * <p>
70 * This default implementation returns a table model with default
71 * formatters and comparators for all columns.
Simon Huntabd16f62015-05-01 13:14:40 -070072 *
Simon Hunt3d1b0652015-05-05 17:27:24 -070073 * @return an empty table model
Simon Huntabd16f62015-05-01 13:14:40 -070074 */
Simon Hunt3d1b0652015-05-05 17:27:24 -070075 protected TableModel createTableModel() {
76 return new TableModel(getColumnIds());
77 }
78
79 /**
Jian Li8baf4472016-01-15 15:08:09 -080080 * Adds table configuration specific annotations to table model.
Jian Li69f66632016-01-15 12:27:42 -080081 *
82 * @param tm a table model
Jian Li8baf4472016-01-15 15:08:09 -080083 * @param payload the event payload from the client
Jian Li69f66632016-01-15 12:27:42 -080084 */
Jian Li8baf4472016-01-15 15:08:09 -080085 protected void addTableConfigAnnotations(TableModel tm, ObjectNode payload) {
86 tm.addAnnotation(NO_ROWS_MSG_KEY, noRowsMessage(payload));
Jian Li69f66632016-01-15 12:27:42 -080087 }
88
89 /**
Simon Hunt3d1b0652015-05-05 17:27:24 -070090 * Returns the default column ID to be used when one is not supplied in
91 * the payload as the column on which to sort.
92 * <p>
93 * This default implementation returns "id".
94 *
95 * @return default sort column identifier
96 */
97 protected String defaultColumnId() {
Simon Huntabd16f62015-05-01 13:14:40 -070098 return "id";
99 }
100
101 /**
Simon Hunt3d1b0652015-05-05 17:27:24 -0700102 * Subclasses should return the array of column IDs with which
103 * to initialize their table model.
Simon Huntabd16f62015-05-01 13:14:40 -0700104 *
Simon Hunt3d1b0652015-05-05 17:27:24 -0700105 * @return the column IDs
Simon Huntabd16f62015-05-01 13:14:40 -0700106 */
Simon Hunt3d1b0652015-05-05 17:27:24 -0700107 protected abstract String[] getColumnIds();
108
109 /**
Jian Li69f66632016-01-15 12:27:42 -0800110 * Subclasses should return the message to display in the table when there
111 * are no rows to display. For example, a host table might return
112 * "No hosts found".
113 *
Jian Li8baf4472016-01-15 15:08:09 -0800114 * @param payload request payload
Jian Li69f66632016-01-15 12:27:42 -0800115 * @return the message
116 */
Jian Li8baf4472016-01-15 15:08:09 -0800117 protected abstract String noRowsMessage(ObjectNode payload);
Jian Li69f66632016-01-15 12:27:42 -0800118
119 /**
Simon Hunt3d1b0652015-05-05 17:27:24 -0700120 * Subclasses should populate the table model by adding
121 * {@link TableModel.Row rows}.
122 * <pre>
123 * tm.addRow()
124 * .cell(COL_ONE, ...)
125 * .cell(COL_TWO, ...)
126 * ... ;
127 * </pre>
128 * The request payload is provided in case there are request filtering
129 * parameters (other than sort column and sort direction) that are required
130 * to generate the appropriate data.
131 *
132 * @param tm the table model
133 * @param payload request payload
134 */
135 protected abstract void populateTable(TableModel tm, ObjectNode payload);
Jian Li69f66632016-01-15 12:27:42 -0800136}