blob: f90c18730404b0130e39473f4994e343b4a870d9 [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;
20import org.onosproject.ui.RequestHandler;
21
22import java.util.Arrays;
23
24/**
25 * Message handler specifically for table views.
26 */
27public abstract class TableRequestHandler extends RequestHandler {
28
29 private final String respType;
30 private final String nodeName;
31
32 /**
33 * Constructs a table request handler for a specific table view. When
34 * table requests come in, the handler will generate the appropriate
35 * table rows, sort them according the the request sort parameters, and
36 * send back the response to the client.
37 *
38 * @param reqType type of the request event
39 * @param respType type of the response event
40 * @param nodeName name of JSON node holding row data
41 */
42 public TableRequestHandler(String reqType, String respType, String nodeName) {
43 super(reqType);
44 this.respType = respType;
45 this.nodeName = nodeName;
46 }
47
48 @Override
49 public void process(long sid, ObjectNode payload) {
50 RowComparator rc = TableUtils.createRowComparator(payload, defaultColId());
51 TableRow[] rows = generateTableRows(payload);
52 Arrays.sort(rows, rc);
53 ObjectNode rootNode = MAPPER.createObjectNode();
54 rootNode.set(nodeName, TableUtils.generateArrayNode(rows));
55 sendMessage(respType, 0, rootNode);
56 }
57
58 /**
59 * Returns the default column ID, when one is not supplied in the payload
60 * defining the column on which to sort. This implementation returns "id".
61 *
62 * @return default sort column id
63 */
64 protected String defaultColId() {
65 return "id";
66 }
67
68 /**
69 * Subclasses should generate table rows for their specific table instance.
70 *
71 * @param payload provided in case custom parameters are present
72 * @return generated table rows
73 */
74 protected abstract TableRow[] generateTableRows(ObjectNode payload);
75}