blob: ee4d8309f286b4130d1ee7382170875f99ea6e24 [file] [log] [blame]
Simon Hunt84f4c2a2015-09-23 17:52:45 -07001#set( $symbol_pound = '#' )
2#set( $symbol_dollar = '$' )
3#set( $symbol_escape = '\' )
4/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07005 * Copyright ${year}-present Open Networking Laboratory
Simon Hunt84f4c2a2015-09-23 17:52:45 -07006 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19package ${package};
20
21import com.fasterxml.jackson.databind.node.ObjectNode;
22import com.google.common.collect.ImmutableSet;
23import org.onosproject.ui.RequestHandler;
24import org.onosproject.ui.UiMessageHandler;
25import org.onosproject.ui.table.TableModel;
26import org.onosproject.ui.table.TableRequestHandler;
27import org.slf4j.Logger;
28import org.slf4j.LoggerFactory;
29
30import java.lang.Override;
31import java.util.ArrayList;
32import java.util.Collection;
33import java.util.List;
34
35/**
36 * Skeletal ONOS UI Table-View message handler.
37 */
Thomas Vachuska2b0fc462015-09-28 12:04:06 -070038public class AppUiTableMessageHandler extends UiMessageHandler {
Simon Hunt84f4c2a2015-09-23 17:52:45 -070039
40 private static final String SAMPLE_TABLE_DATA_REQ = "sampleTableDataRequest";
41 private static final String SAMPLE_TABLE_DATA_RESP = "sampleTableDataResponse";
42 private static final String SAMPLE_TABLES = "sampleTables";
43
44 private static final String SAMPLE_TABLE_DETAIL_REQ = "sampleTableDetailsRequest";
45 private static final String SAMPLE_TABLE_DETAIL_RESP = "sampleTableDetailsResponse";
46 private static final String DETAILS = "details";
47
Jian Li3dbc8882016-01-15 17:07:18 -080048 private static final String NO_ROWS_MESSAGE = "No items found";
49
Simon Hunt84f4c2a2015-09-23 17:52:45 -070050 private static final String ID = "id";
51 private static final String LABEL = "label";
52 private static final String CODE = "code";
53 private static final String COMMENT = "comment";
54 private static final String RESULT = "result";
55
56 private static final String[] COLUMN_IDS = { ID, LABEL, CODE };
57
58 private final Logger log = LoggerFactory.getLogger(getClass());
59
60
61 @Override
62 protected Collection<RequestHandler> createRequestHandlers() {
63 return ImmutableSet.of(
64 new SampleTableDataRequestHandler(),
65 new SampleTableDetailRequestHandler()
66 );
67 }
68
69 // handler for sample table requests
70 private final class SampleTableDataRequestHandler extends TableRequestHandler {
71
72 private SampleTableDataRequestHandler() {
73 super(SAMPLE_TABLE_DATA_REQ, SAMPLE_TABLE_DATA_RESP, SAMPLE_TABLES);
74 }
75
76 // if necessary, override defaultColumnId() -- if it isn't "id"
77
78 @Override
79 protected String[] getColumnIds() {
80 return COLUMN_IDS;
81 }
82
83 // if required, override createTableModel() to set column formatters / comparators
84
85 @Override
Jian Li3dbc8882016-01-15 17:07:18 -080086 protected String noRowsMessage(ObjectNode payload) {
87 return NO_ROWS_MESSAGE;
88 }
89
90 @Override
Simon Hunt84f4c2a2015-09-23 17:52:45 -070091 protected void populateTable(TableModel tm, ObjectNode payload) {
92 // === NOTE: the table model supplied here will have been created
93 // via a call to createTableModel(). To assign non-default
94 // cell formatters or comparators to the table model, override
95 // createTableModel() and set them there.
96
97 // === retrieve table row items from some service...
98 // SomeService ss = get(SomeService.class);
99 // List<Item> items = ss.getItems()
100
101 // fake data for demonstration purposes...
102 List<Item> items = getItems();
103 for (Item item: items) {
104 populateRow(tm.addRow(), item);
105 }
106 }
107
108 private void populateRow(TableModel.Row row, Item item) {
109 row.cell(ID, item.id())
110 .cell(LABEL, item.label())
111 .cell(CODE, item.code());
112 }
113 }
114
115
116 // handler for sample item details requests
117 private final class SampleTableDetailRequestHandler extends RequestHandler {
118
119 private SampleTableDetailRequestHandler() {
120 super(SAMPLE_TABLE_DETAIL_REQ);
121 }
122
123 @Override
Ray Milkey2c835652017-01-09 12:08:00 -0800124 public void process(ObjectNode payload) {
Simon Hunt84f4c2a2015-09-23 17:52:45 -0700125 String id = string(payload, ID, "(none)");
126
127 // SomeService ss = get(SomeService.class);
128 // Item item = ss.getItemDetails(id)
129
130 // fake data for demonstration purposes...
131 Item item = getItem(id);
132
Simon Hunt1035d6c2015-10-01 18:23:15 -0700133 ObjectNode rootNode = objectNode();
134 ObjectNode data = objectNode();
Simon Hunt84f4c2a2015-09-23 17:52:45 -0700135 rootNode.set(DETAILS, data);
136
137 if (item == null) {
138 rootNode.put(RESULT, "Item with id '" + id + "' not found");
139 log.warn("attempted to get item detail for id '{}'", id);
140
141 } else {
142 rootNode.put(RESULT, "Found item with id '" + id + "'");
143
144 data.put(ID, item.id());
145 data.put(LABEL, item.label());
146 data.put(CODE, item.code());
147 data.put(COMMENT, "Some arbitrary comment");
148 }
149
Ray Milkey2c835652017-01-09 12:08:00 -0800150 sendMessage(SAMPLE_TABLE_DETAIL_RESP, rootNode);
Simon Hunt84f4c2a2015-09-23 17:52:45 -0700151 }
152 }
153
154
155 // ===================================================================
156 // NOTE: The code below this line is to create fake data for this
157 // sample code. Normally you would use existing services to
158 // provide real data.
159
160 // Lookup a single item.
161 private static Item getItem(String id) {
162 // We realize this code is really inefficient, but
163 // it suffices for our purposes of demonstration...
164 for (Item item : getItems()) {
165 if (item.id().equals(id)) {
166 return item;
167 }
168 }
169 return null;
170 }
171
172 // Produce a list of items.
173 private static List<Item> getItems() {
174 List<Item> items = new ArrayList<>();
175 items.add(new Item("item-1", "foo", 42));
176 items.add(new Item("item-2", "bar", 99));
177 items.add(new Item("item-3", "baz", 65));
178 return items;
179 }
180
181 // Simple model class to provide sample data
182 private static class Item {
183 private final String id;
184 private final String label;
185 private final int code;
186
187 Item(String id, String label, int code) {
188 this.id = id;
189 this.label = label;
190 this.code = code;
191 }
192
193 String id() { return id; }
194 String label() { return label; }
195 int code() { return code; }
196 }
Ray Milkey2c835652017-01-09 12:08:00 -0800197}