blob: 6fadeb3347ffe185f6654f74532661a3b5fee8de [file] [log] [blame]
Simon Huntabd16f62015-05-01 13:14:40 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Simon Huntabd16f62015-05-01 13:14:40 -07003 *
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 Hunt051e9fa2016-01-19 15:54:22 -080023import static org.onosproject.ui.table.TableModel.sortDir;
24
Simon Huntabd16f62015-05-01 13:14:40 -070025/**
26 * Message handler specifically for table views.
27 */
28public abstract class TableRequestHandler extends RequestHandler {
29
Simon Hunt051e9fa2016-01-19 15:54:22 -080030 private static final String FIRST_COL = "firstCol";
31 private static final String FIRST_DIR = "firstDir";
32 private static final String SECOND_COL = "secondCol";
33 private static final String SECOND_DIR = "secondDir";
34
35 private static final String ASC = "asc";
36
Jian Li69f66632016-01-15 12:27:42 -080037 private static final String ANNOTS = "annots";
38 private static final String NO_ROWS_MSG_KEY = "no_rows_msg";
Simon Hunt051e9fa2016-01-19 15:54:22 -080039
Simon Huntabd16f62015-05-01 13:14:40 -070040 private final String respType;
41 private final String nodeName;
42
43 /**
44 * Constructs a table request handler for a specific table view. When
45 * table requests come in, the handler will generate the appropriate
46 * table rows, sort them according the the request sort parameters, and
47 * send back the response to the client.
48 *
49 * @param reqType type of the request event
50 * @param respType type of the response event
51 * @param nodeName name of JSON node holding row data
52 */
53 public TableRequestHandler(String reqType, String respType, String nodeName) {
54 super(reqType);
55 this.respType = respType;
56 this.nodeName = nodeName;
57 }
58
59 @Override
60 public void process(long sid, ObjectNode payload) {
Simon Hunt3d1b0652015-05-05 17:27:24 -070061 TableModel tm = createTableModel();
62 populateTable(tm, payload);
63
Simon Hunt051e9fa2016-01-19 15:54:22 -080064 String firstCol = JsonUtils.string(payload, FIRST_COL, defaultColumnId());
65 String firstDir = JsonUtils.string(payload, FIRST_DIR, ASC);
66 String secondCol = JsonUtils.string(payload, SECOND_COL, null);
67 String secondDir = JsonUtils.string(payload, SECOND_DIR, null);
68 tm.sort(firstCol, sortDir(firstDir), secondCol, sortDir(secondDir));
Simon Hunt3d1b0652015-05-05 17:27:24 -070069
Jian Li8baf4472016-01-15 15:08:09 -080070 addTableConfigAnnotations(tm, payload);
Jian Li69f66632016-01-15 12:27:42 -080071
Simon Huntabd16f62015-05-01 13:14:40 -070072 ObjectNode rootNode = MAPPER.createObjectNode();
Jian Li69f66632016-01-15 12:27:42 -080073 rootNode.set(nodeName, TableUtils.generateRowArrayNode(tm));
74 rootNode.set(ANNOTS, TableUtils.generateAnnotObjectNode(tm));
Simon Huntabd16f62015-05-01 13:14:40 -070075 sendMessage(respType, 0, rootNode);
76 }
77
78 /**
Simon Hunt3d1b0652015-05-05 17:27:24 -070079 * Creates the table model (devoid of data) using {@link #getColumnIds()}
80 * to initialize it, ready to be populated.
81 * <p>
82 * This default implementation returns a table model with default
83 * formatters and comparators for all columns.
Simon Huntabd16f62015-05-01 13:14:40 -070084 *
Simon Hunt3d1b0652015-05-05 17:27:24 -070085 * @return an empty table model
Simon Huntabd16f62015-05-01 13:14:40 -070086 */
Simon Hunt3d1b0652015-05-05 17:27:24 -070087 protected TableModel createTableModel() {
88 return new TableModel(getColumnIds());
89 }
90
91 /**
Jian Li8baf4472016-01-15 15:08:09 -080092 * Adds table configuration specific annotations to table model.
Jian Li69f66632016-01-15 12:27:42 -080093 *
94 * @param tm a table model
Jian Li8baf4472016-01-15 15:08:09 -080095 * @param payload the event payload from the client
Jian Li69f66632016-01-15 12:27:42 -080096 */
Jian Li8baf4472016-01-15 15:08:09 -080097 protected void addTableConfigAnnotations(TableModel tm, ObjectNode payload) {
98 tm.addAnnotation(NO_ROWS_MSG_KEY, noRowsMessage(payload));
Jian Li69f66632016-01-15 12:27:42 -080099 }
100
101 /**
Simon Hunt3d1b0652015-05-05 17:27:24 -0700102 * Returns the default column ID to be used when one is not supplied in
103 * the payload as the column on which to sort.
104 * <p>
105 * This default implementation returns "id".
106 *
107 * @return default sort column identifier
108 */
109 protected String defaultColumnId() {
Simon Huntabd16f62015-05-01 13:14:40 -0700110 return "id";
111 }
112
113 /**
Simon Hunt3d1b0652015-05-05 17:27:24 -0700114 * Subclasses should return the array of column IDs with which
115 * to initialize their table model.
Simon Huntabd16f62015-05-01 13:14:40 -0700116 *
Simon Hunt3d1b0652015-05-05 17:27:24 -0700117 * @return the column IDs
Simon Huntabd16f62015-05-01 13:14:40 -0700118 */
Simon Hunt3d1b0652015-05-05 17:27:24 -0700119 protected abstract String[] getColumnIds();
120
121 /**
Jian Li69f66632016-01-15 12:27:42 -0800122 * Subclasses should return the message to display in the table when there
123 * are no rows to display. For example, a host table might return
124 * "No hosts found".
125 *
Jian Li8baf4472016-01-15 15:08:09 -0800126 * @param payload request payload
Jian Li69f66632016-01-15 12:27:42 -0800127 * @return the message
128 */
Jian Li8baf4472016-01-15 15:08:09 -0800129 protected abstract String noRowsMessage(ObjectNode payload);
Jian Li69f66632016-01-15 12:27:42 -0800130
131 /**
Simon Hunt3d1b0652015-05-05 17:27:24 -0700132 * Subclasses should populate the table model by adding
133 * {@link TableModel.Row rows}.
134 * <pre>
135 * tm.addRow()
136 * .cell(COL_ONE, ...)
137 * .cell(COL_TWO, ...)
138 * ... ;
139 * </pre>
140 * The request payload is provided in case there are request filtering
141 * parameters (other than sort column and sort direction) that are required
142 * to generate the appropriate data.
143 *
144 * @param tm the table model
145 * @param payload request payload
146 */
147 protected abstract void populateTable(TableModel tm, ObjectNode payload);
Jian Li69f66632016-01-15 12:27:42 -0800148}