blob: 43700dbecd67dd54ce6316c3b7861efb3183c5b2 [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 Li69f66632016-01-15 12:27:42 -080058 addTableConfigAnnotations(tm);
59
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 Li69f66632016-01-15 12:27:42 -080080 * Adds all annotations to table model.
81 *
82 * @param tm a table model
83 */
84 protected void addTableConfigAnnotations(TableModel tm) {
85 tm.addAnnotation(NO_ROWS_MSG_KEY, noRowsMessage());
86 }
87
88 /**
Simon Hunt3d1b0652015-05-05 17:27:24 -070089 * Returns the default column ID to be used when one is not supplied in
90 * the payload as the column on which to sort.
91 * <p>
92 * This default implementation returns "id".
93 *
94 * @return default sort column identifier
95 */
96 protected String defaultColumnId() {
Simon Huntabd16f62015-05-01 13:14:40 -070097 return "id";
98 }
99
100 /**
Simon Hunt3d1b0652015-05-05 17:27:24 -0700101 * Subclasses should return the array of column IDs with which
102 * to initialize their table model.
Simon Huntabd16f62015-05-01 13:14:40 -0700103 *
Simon Hunt3d1b0652015-05-05 17:27:24 -0700104 * @return the column IDs
Simon Huntabd16f62015-05-01 13:14:40 -0700105 */
Simon Hunt3d1b0652015-05-05 17:27:24 -0700106 protected abstract String[] getColumnIds();
107
108 /**
Jian Li69f66632016-01-15 12:27:42 -0800109 * Subclasses should return the message to display in the table when there
110 * are no rows to display. For example, a host table might return
111 * "No hosts found".
112 *
113 * @return the message
114 */
115 protected abstract String noRowsMessage();
116
117 /**
Simon Hunt3d1b0652015-05-05 17:27:24 -0700118 * Subclasses should populate the table model by adding
119 * {@link TableModel.Row rows}.
120 * <pre>
121 * tm.addRow()
122 * .cell(COL_ONE, ...)
123 * .cell(COL_TWO, ...)
124 * ... ;
125 * </pre>
126 * The request payload is provided in case there are request filtering
127 * parameters (other than sort column and sort direction) that are required
128 * to generate the appropriate data.
129 *
130 * @param tm the table model
131 * @param payload request payload
132 */
133 protected abstract void populateTable(TableModel tm, ObjectNode payload);
Jian Li69f66632016-01-15 12:27:42 -0800134}