blob: d9d68b535e1d96405512e1e5ecf04a564f4dd487 [file] [log] [blame]
Simon Hunte11ce5a2015-07-21 12:11:04 -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 message handler.
37 * <p>
38 * This example specifically supporting a "table" view.
39 */
40public class AppUiMessageHandler extends UiMessageHandler {
41
42 private static final String SAMPLE_DATA_REQ = "sampleDataRequest";
43 private static final String SAMPLE_DATA_RESP = "sampleDataResponse";
44 private static final String SAMPLES = "samples";
45
46 private static final String SAMPLE_DETAIL_REQ = "sampleDetailsRequest";
47 private static final String SAMPLE_DETAIL_RESP = "sampleDetailsResponse";
48 private static final String DETAILS = "details";
49
50 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 SampleDataRequestHandler(),
65 new SampleDetailRequestHandler()
66 );
67 }
68
69 // handler for sample table requests
70 private final class SampleDataRequestHandler extends TableRequestHandler {
71
72 private SampleDataRequestHandler() {
73 super(SAMPLE_DATA_REQ, SAMPLE_DATA_RESP, SAMPLES);
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 @Override
84 protected void populateTable(TableModel tm, ObjectNode payload) {
85 // === set custom column cell formatters/comparators if need be...
86 // tm.setFormatter(CODE, new CodeFormatter());
87 // tm.setComparator(CODE, new CodeComparator());
88
89 // === retrieve table row items from some service...
90 // SomeService ss = get(SomeService.class);
91 // List<Item> items = ss.getItems()
92
93 // fake data for demonstration purposes...
94 List<Item> items = getItems();
95 for (Item item: items) {
96 populateRow(tm.addRow(), item);
97 }
98 }
99
100 private void populateRow(TableModel.Row row, Item item) {
101 row.cell(ID, item.id())
102 .cell(LABEL, item.label())
103 .cell(CODE, item.code());
104 }
105 }
106
107
108 // handler for sample item details requests
109 private final class SampleDetailRequestHandler extends RequestHandler {
110
111 private SampleDetailRequestHandler() {
112 super(SAMPLE_DETAIL_REQ);
113 }
114
115 @Override
116 public void process(long sid, ObjectNode payload) {
117 String id = string(payload, ID, "(none)");
118
119 // SomeService ss = get(SomeService.class);
120 // Item item = ss.getItemDetails(id)
121
122 // fake data for demonstration purposes...
123 Item item = getItem(id);
124
125 ObjectNode rootNode = MAPPER.createObjectNode();
126 ObjectNode data = MAPPER.createObjectNode();
127 rootNode.set(DETAILS, data);
128
129 if (item == null) {
130 rootNode.put(RESULT, "Item with id '" + id + "' not found");
131 log.warn("attempted to get item detail for id '{}'", id);
132
133 } else {
134 rootNode.put(RESULT, "Found item with id '" + id + "'");
135
136 data.put(ID, item.id());
137 data.put(LABEL, item.label());
138 data.put(CODE, item.code());
139 data.put(COMMENT, "Some arbitrary comment");
140 }
141
142 sendMessage(SAMPLE_DETAIL_RESP, 0, rootNode);
143 }
144 }
145
146
147 // ===================================================================
148 // NOTE: The code below this line is to create fake data for this
149 // sample code. Normally you would use existing services to
150 // provide real data.
151
152 // Lookup a single item.
153 private static Item getItem(String id) {
154 // We realize this code is really inefficient, but
155 // it suffices for our purposes of demonstration...
156 for (Item item : getItems()) {
157 if (item.id().equals(id)) {
158 return item;
159 }
160 }
161 return null;
162 }
163
164 // Produce a list of items.
165 private static List<Item> getItems() {
166 List<Item> items = new ArrayList<>();
167 items.add(new Item("item-1", "foo", 42));
168 items.add(new Item("item-2", "bar", 99));
169 items.add(new Item("item-3", "baz", 65));
170 return items;
171 }
172
173 // Simple model class to provide sample data
174 private static class Item {
175 private final String id;
176 private final String label;
177 private final int code;
178
179 Item(String id, String label, int code) {
180 this.id = id;
181 this.label = label;
182 this.code = code;
183 }
184
185 String id() { return id; }
186 String label() { return label; }
187 int code() { return code; }
188 }
189}