blob: e99644487a1102f73fda7a36c1326007c195481a [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/**
Simon Hunt84f4c2a2015-09-23 17:52:45 -070036 * Skeletal ONOS UI Custom-View message handler.
Simon Hunte11ce5a2015-07-21 12:11:04 -070037 */
38public class AppUiMessageHandler extends UiMessageHandler {
Simon Hunt84f4c2a2015-09-23 17:52:45 -070039 // TODO: reduce the code down to just the custom view example
Simon Hunte11ce5a2015-07-21 12:11:04 -070040
Simon Hunt84f4c2a2015-09-23 17:52:45 -070041 private static final String SAMPLE_CUSTOM_DATA_REQ = "sampleCustomDataRequest";
42 private static final String SAMPLE_CUSTOM_DATA_RESP = "sampleCustomDataResponse";
43 private static final String SAMPLE_CUSTOMS = "sampleCustoms";
Simon Hunte11ce5a2015-07-21 12:11:04 -070044
Simon Hunt84f4c2a2015-09-23 17:52:45 -070045 private static final String SAMPLE_CUSTOM_DETAIL_REQ = "sampleCustomDetailsRequest";
46 private static final String SAMPLE_CUSTOM_DETAIL_RESP = "sampleCustomDetailsResponse";
Simon Hunte11ce5a2015-07-21 12:11:04 -070047 private static final String DETAILS = "details";
48
49 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
55 private static final String[] COLUMN_IDS = { ID, LABEL, CODE };
56
57 private final Logger log = LoggerFactory.getLogger(getClass());
58
59
60 @Override
61 protected Collection<RequestHandler> createRequestHandlers() {
62 return ImmutableSet.of(
Simon Hunt84f4c2a2015-09-23 17:52:45 -070063 new SampleCustomDataRequestHandler(),
64 new SampleCustomDetailRequestHandler()
Simon Hunte11ce5a2015-07-21 12:11:04 -070065 );
66 }
67
68 // handler for sample table requests
Simon Hunt84f4c2a2015-09-23 17:52:45 -070069 private final class SampleCustomDataRequestHandler extends TableRequestHandler {
Simon Hunte11ce5a2015-07-21 12:11:04 -070070
Simon Hunt84f4c2a2015-09-23 17:52:45 -070071 private SampleCustomDataRequestHandler() {
72 super(SAMPLE_CUSTOM_DATA_REQ, SAMPLE_CUSTOM_DATA_RESP, SAMPLE_CUSTOMS);
Simon Hunte11ce5a2015-07-21 12:11:04 -070073 }
74
Simon Hunte11ce5a2015-07-21 12:11:04 -070075 @Override
76 protected String[] getColumnIds() {
77 return COLUMN_IDS;
78 }
79
80 @Override
81 protected void populateTable(TableModel tm, ObjectNode payload) {
82 // === set custom column cell formatters/comparators if need be...
83 // tm.setFormatter(CODE, new CodeFormatter());
84 // tm.setComparator(CODE, new CodeComparator());
85
86 // === retrieve table row items from some service...
87 // SomeService ss = get(SomeService.class);
88 // List<Item> items = ss.getItems()
89
90 // fake data for demonstration purposes...
91 List<Item> items = getItems();
92 for (Item item: items) {
93 populateRow(tm.addRow(), item);
94 }
95 }
96
97 private void populateRow(TableModel.Row row, Item item) {
98 row.cell(ID, item.id())
99 .cell(LABEL, item.label())
100 .cell(CODE, item.code());
101 }
102 }
103
104
105 // handler for sample item details requests
Simon Hunt84f4c2a2015-09-23 17:52:45 -0700106 private final class SampleCustomDetailRequestHandler extends RequestHandler {
Simon Hunte11ce5a2015-07-21 12:11:04 -0700107
Simon Hunt84f4c2a2015-09-23 17:52:45 -0700108 private SampleCustomDetailRequestHandler() {
109 super(SAMPLE_CUSTOM_DETAIL_REQ);
Simon Hunte11ce5a2015-07-21 12:11:04 -0700110 }
111
112 @Override
113 public void process(long sid, ObjectNode payload) {
114 String id = string(payload, ID, "(none)");
115
116 // SomeService ss = get(SomeService.class);
117 // Item item = ss.getItemDetails(id)
118
119 // fake data for demonstration purposes...
120 Item item = getItem(id);
121
122 ObjectNode rootNode = MAPPER.createObjectNode();
123 ObjectNode data = MAPPER.createObjectNode();
124 rootNode.set(DETAILS, data);
125
126 if (item == null) {
127 rootNode.put(RESULT, "Item with id '" + id + "' not found");
128 log.warn("attempted to get item detail for id '{}'", id);
129
130 } else {
131 rootNode.put(RESULT, "Found item with id '" + id + "'");
132
133 data.put(ID, item.id());
134 data.put(LABEL, item.label());
135 data.put(CODE, item.code());
136 data.put(COMMENT, "Some arbitrary comment");
137 }
138
Simon Hunt84f4c2a2015-09-23 17:52:45 -0700139 sendMessage(SAMPLE_CUSTOM_DETAIL_RESP, 0, rootNode);
Simon Hunte11ce5a2015-07-21 12:11:04 -0700140 }
141 }
142
143
144 // ===================================================================
145 // NOTE: The code below this line is to create fake data for this
146 // sample code. Normally you would use existing services to
147 // provide real data.
148
149 // Lookup a single item.
150 private static Item getItem(String id) {
151 // We realize this code is really inefficient, but
152 // it suffices for our purposes of demonstration...
153 for (Item item : getItems()) {
154 if (item.id().equals(id)) {
155 return item;
156 }
157 }
158 return null;
159 }
160
161 // Produce a list of items.
162 private static List<Item> getItems() {
163 List<Item> items = new ArrayList<>();
164 items.add(new Item("item-1", "foo", 42));
165 items.add(new Item("item-2", "bar", 99));
166 items.add(new Item("item-3", "baz", 65));
167 return items;
168 }
169
170 // Simple model class to provide sample data
171 private static class Item {
172 private final String id;
173 private final String label;
174 private final int code;
175
176 Item(String id, String label, int code) {
177 this.id = id;
178 this.label = label;
179 this.code = code;
180 }
181
182 String id() { return id; }
183 String label() { return label; }
184 int code() { return code; }
185 }
186}