blob: 2645c32be94171bc42296b73fa711e52c18c43f0 [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'Connora09fe5b2017-08-03 21:12:30 -07005 * Copyright ${year}-present Open Networking Foundation
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
Simon Hunt84f4c2a2015-09-23 17:52:45 -070030import java.util.ArrayList;
31import java.util.Collection;
32import java.util.List;
33
34/**
35 * Skeletal ONOS UI Table-View message handler.
36 */
Thomas Vachuska2b0fc462015-09-28 12:04:06 -070037public class AppUiTableMessageHandler extends UiMessageHandler {
Simon Hunt84f4c2a2015-09-23 17:52:45 -070038
39 private static final String SAMPLE_TABLE_DATA_REQ = "sampleTableDataRequest";
40 private static final String SAMPLE_TABLE_DATA_RESP = "sampleTableDataResponse";
41 private static final String SAMPLE_TABLES = "sampleTables";
42
43 private static final String SAMPLE_TABLE_DETAIL_REQ = "sampleTableDetailsRequest";
44 private static final String SAMPLE_TABLE_DETAIL_RESP = "sampleTableDetailsResponse";
45 private static final String DETAILS = "details";
46
Jian Li3dbc8882016-01-15 17:07:18 -080047 private static final String NO_ROWS_MESSAGE = "No items found";
48
Simon Hunt84f4c2a2015-09-23 17:52:45 -070049 private static final String ID = "id";
50 private static final String LABEL = "label";
51 private static final String CODE = "code";
52 private static final String COMMENT = "comment";
53 private static final String RESULT = "result";
54
Thomas Vachuska09b16792020-01-06 15:11:50 -080055 private static final String[] COLUMN_IDS = {ID, LABEL, CODE};
Simon Hunt84f4c2a2015-09-23 17:52:45 -070056
57 private final Logger log = LoggerFactory.getLogger(getClass());
58
59
60 @Override
61 protected Collection<RequestHandler> createRequestHandlers() {
62 return ImmutableSet.of(
63 new SampleTableDataRequestHandler(),
64 new SampleTableDetailRequestHandler()
65 );
66 }
67
68 // handler for sample table requests
69 private final class SampleTableDataRequestHandler extends TableRequestHandler {
70
71 private SampleTableDataRequestHandler() {
72 super(SAMPLE_TABLE_DATA_REQ, SAMPLE_TABLE_DATA_RESP, SAMPLE_TABLES);
73 }
74
75 // if necessary, override defaultColumnId() -- if it isn't "id"
76
77 @Override
78 protected String[] getColumnIds() {
79 return COLUMN_IDS;
80 }
81
82 // if required, override createTableModel() to set column formatters / comparators
83
84 @Override
Jian Li3dbc8882016-01-15 17:07:18 -080085 protected String noRowsMessage(ObjectNode payload) {
86 return NO_ROWS_MESSAGE;
87 }
88
89 @Override
Simon Hunt84f4c2a2015-09-23 17:52:45 -070090 protected void populateTable(TableModel tm, ObjectNode payload) {
91 // === NOTE: the table model supplied here will have been created
92 // via a call to createTableModel(). To assign non-default
93 // cell formatters or comparators to the table model, override
94 // createTableModel() and set them there.
95
96 // === retrieve table row items from some service...
97 // SomeService ss = get(SomeService.class);
98 // List<Item> items = ss.getItems()
99
100 // fake data for demonstration purposes...
101 List<Item> items = getItems();
102 for (Item item: items) {
103 populateRow(tm.addRow(), item);
104 }
105 }
106
107 private void populateRow(TableModel.Row row, Item item) {
108 row.cell(ID, item.id())
109 .cell(LABEL, item.label())
110 .cell(CODE, item.code());
111 }
112 }
113
114
115 // handler for sample item details requests
116 private final class SampleTableDetailRequestHandler extends RequestHandler {
117
118 private SampleTableDetailRequestHandler() {
119 super(SAMPLE_TABLE_DETAIL_REQ);
120 }
121
122 @Override
Ray Milkey2c835652017-01-09 12:08:00 -0800123 public void process(ObjectNode payload) {
Simon Hunt84f4c2a2015-09-23 17:52:45 -0700124 String id = string(payload, ID, "(none)");
125
126 // SomeService ss = get(SomeService.class);
127 // Item item = ss.getItemDetails(id)
128
129 // fake data for demonstration purposes...
130 Item item = getItem(id);
131
Simon Hunt1035d6c2015-10-01 18:23:15 -0700132 ObjectNode rootNode = objectNode();
133 ObjectNode data = objectNode();
Simon Hunt84f4c2a2015-09-23 17:52:45 -0700134 rootNode.set(DETAILS, data);
135
136 if (item == null) {
137 rootNode.put(RESULT, "Item with id '" + id + "' not found");
138 log.warn("attempted to get item detail for id '{}'", id);
139
140 } else {
141 rootNode.put(RESULT, "Found item with id '" + id + "'");
142
143 data.put(ID, item.id());
144 data.put(LABEL, item.label());
145 data.put(CODE, item.code());
146 data.put(COMMENT, "Some arbitrary comment");
147 }
148
Ray Milkey2c835652017-01-09 12:08:00 -0800149 sendMessage(SAMPLE_TABLE_DETAIL_RESP, rootNode);
Simon Hunt84f4c2a2015-09-23 17:52:45 -0700150 }
151 }
152
153
154 // ===================================================================
155 // NOTE: The code below this line is to create fake data for this
156 // sample code. Normally you would use existing services to
157 // provide real data.
158
159 // Lookup a single item.
160 private static Item getItem(String id) {
161 // We realize this code is really inefficient, but
162 // it suffices for our purposes of demonstration...
163 for (Item item : getItems()) {
164 if (item.id().equals(id)) {
165 return item;
166 }
167 }
168 return null;
169 }
170
171 // Produce a list of items.
172 private static List<Item> getItems() {
173 List<Item> items = new ArrayList<>();
174 items.add(new Item("item-1", "foo", 42));
175 items.add(new Item("item-2", "bar", 99));
176 items.add(new Item("item-3", "baz", 65));
177 return items;
178 }
179
180 // Simple model class to provide sample data
181 private static class Item {
182 private final String id;
183 private final String label;
184 private final int code;
185
186 Item(String id, String label, int code) {
187 this.id = id;
188 this.label = label;
189 this.code = code;
190 }
191
Thomas Vachuska09b16792020-01-06 15:11:50 -0800192 String id() {
193 return id;
194 }
195
196 String label() {
197 return label;
198 }
199
200 int code() {
201 return code;
202 }
Simon Hunt84f4c2a2015-09-23 17:52:45 -0700203 }
Ray Milkey2c835652017-01-09 12:08:00 -0800204}