Thomas Vachuska | bbf1050 | 2015-12-09 13:41:58 -0800 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.drivermatrix; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 19 | import com.google.common.collect.ImmutableSet; |
| 20 | import org.onosproject.ui.RequestHandler; |
| 21 | import org.onosproject.ui.UiMessageHandler; |
| 22 | import org.onosproject.ui.table.TableModel; |
| 23 | import org.onosproject.ui.table.TableRequestHandler; |
| 24 | import org.slf4j.Logger; |
| 25 | import org.slf4j.LoggerFactory; |
| 26 | |
| 27 | import java.util.ArrayList; |
| 28 | import java.util.Collection; |
| 29 | import java.util.List; |
| 30 | |
| 31 | /** |
| 32 | * Skeletal ONOS UI Table-View message handler. |
| 33 | */ |
| 34 | public 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 | |
Jian Li | 69f6663 | 2016-01-15 12:27:42 -0800 | [diff] [blame] | 66 | private static final String NO_ROWS_MESSAGE = "No data found"; |
| 67 | |
Thomas Vachuska | bbf1050 | 2015-12-09 13:41:58 -0800 | [diff] [blame] | 68 | private SampleTableDataRequestHandler() { |
| 69 | super(SAMPLE_TABLE_DATA_REQ, SAMPLE_TABLE_DATA_RESP, SAMPLE_TABLES); |
| 70 | } |
| 71 | |
| 72 | // if necessary, override defaultColumnId() -- if it isn't "id" |
| 73 | |
| 74 | @Override |
| 75 | protected String[] getColumnIds() { |
| 76 | return COLUMN_IDS; |
| 77 | } |
| 78 | |
Jian Li | 69f6663 | 2016-01-15 12:27:42 -0800 | [diff] [blame] | 79 | @Override |
Jian Li | 8baf447 | 2016-01-15 15:08:09 -0800 | [diff] [blame] | 80 | protected String noRowsMessage(ObjectNode payload) { |
Jian Li | 69f6663 | 2016-01-15 12:27:42 -0800 | [diff] [blame] | 81 | return NO_ROWS_MESSAGE; |
| 82 | } |
| 83 | |
Thomas Vachuska | bbf1050 | 2015-12-09 13:41:58 -0800 | [diff] [blame] | 84 | // if required, override createTableModel() to set column formatters / comparators |
| 85 | |
| 86 | @Override |
| 87 | protected void populateTable(TableModel tm, ObjectNode payload) { |
| 88 | // === NOTE: the table model supplied here will have been created |
| 89 | // via a call to createTableModel(). To assign non-default |
| 90 | // cell formatters or comparators to the table model, override |
| 91 | // createTableModel() and set them there. |
| 92 | |
| 93 | // === retrieve table row items from some service... |
| 94 | // SomeService ss = get(SomeService.class); |
| 95 | // List<Item> items = ss.getItems() |
| 96 | |
| 97 | // fake data for demonstration purposes... |
| 98 | List<Item> items = getItems(); |
| 99 | for (Item item : items) { |
| 100 | populateRow(tm.addRow(), item); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | private void populateRow(TableModel.Row row, Item item) { |
| 105 | row.cell(ID, item.id()) |
| 106 | .cell(LABEL, item.label()) |
| 107 | .cell(CODE, item.code()); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | |
| 112 | // handler for sample item details requests |
| 113 | private final class SampleTableDetailRequestHandler extends RequestHandler { |
| 114 | |
| 115 | private SampleTableDetailRequestHandler() { |
| 116 | super(SAMPLE_TABLE_DETAIL_REQ); |
| 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public void process(long sid, ObjectNode payload) { |
| 121 | String id = string(payload, ID, "(none)"); |
| 122 | |
| 123 | // SomeService ss = get(SomeService.class); |
| 124 | // Item item = ss.getItemDetails(id) |
| 125 | |
| 126 | // fake data for demonstration purposes... |
| 127 | Item item = getItem(id); |
| 128 | |
| 129 | ObjectNode rootNode = objectNode(); |
| 130 | ObjectNode data = objectNode(); |
| 131 | rootNode.set(DETAILS, data); |
| 132 | |
| 133 | if (item == null) { |
| 134 | rootNode.put(RESULT, "Item with id '" + id + "' not found"); |
| 135 | log.warn("attempted to get item detail for id '{}'", id); |
| 136 | |
| 137 | } else { |
| 138 | rootNode.put(RESULT, "Found item with id '" + id + "'"); |
| 139 | |
| 140 | data.put(ID, item.id()); |
| 141 | data.put(LABEL, item.label()); |
| 142 | data.put(CODE, item.code()); |
| 143 | data.put(COMMENT, "Some arbitrary comment"); |
| 144 | } |
| 145 | |
| 146 | sendMessage(SAMPLE_TABLE_DETAIL_RESP, 0, rootNode); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | |
| 151 | // =================================================================== |
| 152 | // NOTE: The code below this line is to create fake data for this |
| 153 | // sample code. Normally you would use existing services to |
| 154 | // provide real data. |
| 155 | |
| 156 | // Lookup a single item. |
| 157 | private static Item getItem(String id) { |
| 158 | // We realize this code is really inefficient, but |
| 159 | // it suffices for our purposes of demonstration... |
| 160 | for (Item item : getItems()) { |
| 161 | if (item.id().equals(id)) { |
| 162 | return item; |
| 163 | } |
| 164 | } |
| 165 | return null; |
| 166 | } |
| 167 | |
| 168 | // Produce a list of items. |
| 169 | private static List<Item> getItems() { |
| 170 | List<Item> items = new ArrayList<>(); |
| 171 | items.add(new Item("item-1", "foo", 42)); |
| 172 | items.add(new Item("item-2", "bar", 99)); |
| 173 | items.add(new Item("item-3", "baz", 65)); |
| 174 | return items; |
| 175 | } |
| 176 | |
| 177 | // Simple model class to provide sample data |
| 178 | private static class Item { |
| 179 | private final String id; |
| 180 | private final String label; |
| 181 | private final int code; |
| 182 | |
| 183 | Item(String id, String label, int code) { |
| 184 | this.id = id; |
| 185 | this.label = label; |
| 186 | this.code = code; |
| 187 | } |
| 188 | |
| 189 | String id() { |
| 190 | return id; |
| 191 | } |
| 192 | |
| 193 | String label() { |
| 194 | return label; |
| 195 | } |
| 196 | |
| 197 | int code() { |
| 198 | return code; |
| 199 | } |
| 200 | } |
| 201 | } |