blob: a4e77f41b593ec0fb68325f2b6daef856fcc12d2 [file] [log] [blame]
Thomas Vachuskabbf10502015-12-09 13:41:58 -08001/*
2 * Copyright 2014,2015 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.drivermatrix;
17
18import com.fasterxml.jackson.databind.node.ObjectNode;
19import com.google.common.collect.ImmutableSet;
20import org.onosproject.ui.RequestHandler;
21import org.onosproject.ui.UiMessageHandler;
22import org.onosproject.ui.table.TableModel;
23import org.onosproject.ui.table.TableRequestHandler;
24import org.slf4j.Logger;
25import org.slf4j.LoggerFactory;
26
27import java.util.ArrayList;
28import java.util.Collection;
29import java.util.List;
30
31/**
32 * Skeletal ONOS UI Table-View message handler.
33 */
34public class DriverMatrixMessageHandler extends UiMessageHandler {
35
36 private static final String SAMPLE_TABLE_DATA_REQ = "driverMatrixDataRequest";
37 private static final String SAMPLE_TABLE_DATA_RESP = "driverMatrixDataResponse";
38 private static final String SAMPLE_TABLES = "driverMatrixs";
39
40 private static final String SAMPLE_TABLE_DETAIL_REQ = "driverMatrixDetailsRequest";
41 private static final String SAMPLE_TABLE_DETAIL_RESP = "driverMatrixDetailsResponse";
42 private static final String DETAILS = "details";
43
44 private static final String ID = "id";
45 private static final String LABEL = "label";
46 private static final String CODE = "code";
47 private static final String COMMENT = "comment";
48 private static final String RESULT = "result";
49
50 private static final String[] COLUMN_IDS = {ID, LABEL, CODE};
51
52 private final Logger log = LoggerFactory.getLogger(getClass());
53
54
55 @Override
56 protected Collection<RequestHandler> createRequestHandlers() {
57 return ImmutableSet.of(
58 new SampleTableDataRequestHandler(),
59 new SampleTableDetailRequestHandler()
60 );
61 }
62
63 // handler for sample table requests
64 private final class SampleTableDataRequestHandler extends TableRequestHandler {
65
66 private SampleTableDataRequestHandler() {
67 super(SAMPLE_TABLE_DATA_REQ, SAMPLE_TABLE_DATA_RESP, SAMPLE_TABLES);
68 }
69
70 // if necessary, override defaultColumnId() -- if it isn't "id"
71
72 @Override
73 protected String[] getColumnIds() {
74 return COLUMN_IDS;
75 }
76
77 // if required, override createTableModel() to set column formatters / comparators
78
79 @Override
80 protected void populateTable(TableModel tm, ObjectNode payload) {
81 // === NOTE: the table model supplied here will have been created
82 // via a call to createTableModel(). To assign non-default
83 // cell formatters or comparators to the table model, override
84 // createTableModel() and set them there.
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
106 private final class SampleTableDetailRequestHandler extends RequestHandler {
107
108 private SampleTableDetailRequestHandler() {
109 super(SAMPLE_TABLE_DETAIL_REQ);
110 }
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 = objectNode();
123 ObjectNode data = objectNode();
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
139 sendMessage(SAMPLE_TABLE_DETAIL_RESP, 0, rootNode);
140 }
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() {
183 return id;
184 }
185
186 String label() {
187 return label;
188 }
189
190 int code() {
191 return code;
192 }
193 }
194}