blob: a673f1fd62217c5496f2b7550e76255c172382ea [file] [log] [blame]
Simon Hunt84f4c2a2015-09-23 17:52:45 -07001#set( $symbol_pound = '#' )
2#set( $symbol_dollar = '$' )
3#set( $symbol_escape = '\' )
4/*
5 * Copyright 2014,2015 Open Networking Laboratory
6 *
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
48 private static final String ID = "id";
49 private static final String LABEL = "label";
50 private static final String CODE = "code";
51 private static final String COMMENT = "comment";
52 private static final String RESULT = "result";
53
54 private static final String[] COLUMN_IDS = { ID, LABEL, CODE };
55
56 private final Logger log = LoggerFactory.getLogger(getClass());
57
58
59 @Override
60 protected Collection<RequestHandler> createRequestHandlers() {
61 return ImmutableSet.of(
62 new SampleTableDataRequestHandler(),
63 new SampleTableDetailRequestHandler()
64 );
65 }
66
67 // handler for sample table requests
68 private final class SampleTableDataRequestHandler extends TableRequestHandler {
69
70 private SampleTableDataRequestHandler() {
71 super(SAMPLE_TABLE_DATA_REQ, SAMPLE_TABLE_DATA_RESP, SAMPLE_TABLES);
72 }
73
74 // if necessary, override defaultColumnId() -- if it isn't "id"
75
76 @Override
77 protected String[] getColumnIds() {
78 return COLUMN_IDS;
79 }
80
81 // if required, override createTableModel() to set column formatters / comparators
82
83 @Override
84 protected void populateTable(TableModel tm, ObjectNode payload) {
85 // === NOTE: the table model supplied here will have been created
86 // via a call to createTableModel(). To assign non-default
87 // cell formatters or comparators to the table model, override
88 // createTableModel() and set them there.
89
90 // === retrieve table row items from some service...
91 // SomeService ss = get(SomeService.class);
92 // List<Item> items = ss.getItems()
93
94 // fake data for demonstration purposes...
95 List<Item> items = getItems();
96 for (Item item: items) {
97 populateRow(tm.addRow(), item);
98 }
99 }
100
101 private void populateRow(TableModel.Row row, Item item) {
102 row.cell(ID, item.id())
103 .cell(LABEL, item.label())
104 .cell(CODE, item.code());
105 }
106 }
107
108
109 // handler for sample item details requests
110 private final class SampleTableDetailRequestHandler extends RequestHandler {
111
112 private SampleTableDetailRequestHandler() {
113 super(SAMPLE_TABLE_DETAIL_REQ);
114 }
115
116 @Override
117 public void process(long sid, ObjectNode payload) {
118 String id = string(payload, ID, "(none)");
119
120 // SomeService ss = get(SomeService.class);
121 // Item item = ss.getItemDetails(id)
122
123 // fake data for demonstration purposes...
124 Item item = getItem(id);
125
Simon Hunt1035d6c2015-10-01 18:23:15 -0700126 ObjectNode rootNode = objectNode();
127 ObjectNode data = objectNode();
Simon Hunt84f4c2a2015-09-23 17:52:45 -0700128 rootNode.set(DETAILS, data);
129
130 if (item == null) {
131 rootNode.put(RESULT, "Item with id '" + id + "' not found");
132 log.warn("attempted to get item detail for id '{}'", id);
133
134 } else {
135 rootNode.put(RESULT, "Found item with id '" + id + "'");
136
137 data.put(ID, item.id());
138 data.put(LABEL, item.label());
139 data.put(CODE, item.code());
140 data.put(COMMENT, "Some arbitrary comment");
141 }
142
143 sendMessage(SAMPLE_TABLE_DETAIL_RESP, 0, rootNode);
144 }
145 }
146
147
148 // ===================================================================
149 // NOTE: The code below this line is to create fake data for this
150 // sample code. Normally you would use existing services to
151 // provide real data.
152
153 // Lookup a single item.
154 private static Item getItem(String id) {
155 // We realize this code is really inefficient, but
156 // it suffices for our purposes of demonstration...
157 for (Item item : getItems()) {
158 if (item.id().equals(id)) {
159 return item;
160 }
161 }
162 return null;
163 }
164
165 // Produce a list of items.
166 private static List<Item> getItems() {
167 List<Item> items = new ArrayList<>();
168 items.add(new Item("item-1", "foo", 42));
169 items.add(new Item("item-2", "bar", 99));
170 items.add(new Item("item-3", "baz", 65));
171 return items;
172 }
173
174 // Simple model class to provide sample data
175 private static class Item {
176 private final String id;
177 private final String label;
178 private final int code;
179
180 Item(String id, String label, int code) {
181 this.id = id;
182 this.label = label;
183 this.code = code;
184 }
185
186 String id() { return id; }
187 String label() { return label; }
188 int code() { return code; }
189 }
190}