Thomas Vachuska | 58de416 | 2015-09-10 16:15:33 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Thomas Vachuska | 58de416 | 2015-09-10 16:15:33 -0700 | [diff] [blame] | 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 | */ |
chengfan | b466a7e | 2015-08-21 09:59:29 -0500 | [diff] [blame] | 16 | package org.onosproject.ui.impl; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 19 | import com.google.common.collect.ImmutableSet; |
| 20 | import org.onosproject.incubator.net.tunnel.Tunnel; |
chengfan | 392e8f9 | 2015-08-21 13:20:02 -0500 | [diff] [blame] | 21 | import org.onosproject.incubator.net.tunnel.TunnelEndPointFormatter; |
chengfan | b466a7e | 2015-08-21 09:59:29 -0500 | [diff] [blame] | 22 | import org.onosproject.incubator.net.tunnel.TunnelService; |
| 23 | import org.onosproject.ui.RequestHandler; |
| 24 | import org.onosproject.ui.UiMessageHandler; |
| 25 | import org.onosproject.ui.table.TableModel; |
| 26 | import org.onosproject.ui.table.TableRequestHandler; |
| 27 | import org.onosproject.ui.table.cell.EnumFormatter; |
| 28 | |
| 29 | import java.util.Collection; |
| 30 | |
| 31 | public class TunnelViewMessageHandler extends UiMessageHandler { |
| 32 | private static final String TUNNEL_DATA_REQ = "tunnelDataRequest"; |
| 33 | private static final String TUNNEL_DATA_RESP = "tunnelDataResponse"; |
| 34 | private static final String TUNNELS = "tunnels"; |
| 35 | private static final String ID = "id"; |
| 36 | private static final String NAME = "name"; |
| 37 | private static final String ONE = "one"; |
| 38 | private static final String TWO = "two"; |
| 39 | private static final String TYPE = "type"; |
| 40 | private static final String GROUP_ID = "group_id"; |
| 41 | |
| 42 | private static final String BANDWIDTH = "bandwidth"; |
| 43 | private static final String PATH = "path"; |
| 44 | |
| 45 | |
| 46 | private static final String[] COL_IDS = { |
| 47 | ID, NAME, ONE, TWO, TYPE, GROUP_ID, |
| 48 | BANDWIDTH, PATH |
| 49 | }; |
| 50 | |
| 51 | @Override |
| 52 | protected Collection<RequestHandler> createRequestHandlers() { |
| 53 | return ImmutableSet.of(new TunnelDataRequestHandler()); |
| 54 | } |
| 55 | |
| 56 | private final class TunnelDataRequestHandler extends TableRequestHandler { |
| 57 | |
Jian Li | 69f6663 | 2016-01-15 12:27:42 -0800 | [diff] [blame] | 58 | private static final String NO_ROWS_MESSAGE = "No tunnels found"; |
| 59 | |
chengfan | b466a7e | 2015-08-21 09:59:29 -0500 | [diff] [blame] | 60 | public TunnelDataRequestHandler() { |
| 61 | super(TUNNEL_DATA_REQ, TUNNEL_DATA_RESP, TUNNELS); |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | protected String[] getColumnIds() { |
| 66 | return COL_IDS; |
| 67 | } |
| 68 | |
| 69 | @Override |
Jian Li | 8baf447 | 2016-01-15 15:08:09 -0800 | [diff] [blame] | 70 | protected String noRowsMessage(ObjectNode payload) { |
Jian Li | 69f6663 | 2016-01-15 12:27:42 -0800 | [diff] [blame] | 71 | return NO_ROWS_MESSAGE; |
| 72 | } |
| 73 | |
| 74 | @Override |
chengfan | b466a7e | 2015-08-21 09:59:29 -0500 | [diff] [blame] | 75 | protected TableModel createTableModel() { |
| 76 | TableModel tm = super.createTableModel(); |
| 77 | //TODO add more formater class so that we can get a more readable table |
chengfan | 392e8f9 | 2015-08-21 13:20:02 -0500 | [diff] [blame] | 78 | tm.setFormatter(ONE, TunnelEndPointFormatter.INSTANCE); |
| 79 | tm.setFormatter(TWO, TunnelEndPointFormatter.INSTANCE); |
chengfan | b466a7e | 2015-08-21 09:59:29 -0500 | [diff] [blame] | 80 | tm.setFormatter(TYPE, EnumFormatter.INSTANCE); |
| 81 | return tm; |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | protected void populateTable(TableModel tm, ObjectNode payload) { |
| 86 | TunnelService ts = get(TunnelService.class); |
chengfan | 392e8f9 | 2015-08-21 13:20:02 -0500 | [diff] [blame] | 87 | ts.queryAllTunnels().forEach(tunnel -> populateRow(tm.addRow(), tunnel)); |
chengfan | b466a7e | 2015-08-21 09:59:29 -0500 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | } |
| 91 | |
| 92 | private void populateRow(TableModel.Row row, Tunnel tunnel) { |
| 93 | row.cell(ID, tunnel.tunnelId().id()) |
| 94 | .cell(NAME, tunnel.tunnelName().value()) |
| 95 | .cell(ONE, tunnel.src()) |
| 96 | .cell(TWO, tunnel.dst()) |
| 97 | .cell(TYPE, tunnel.type()) |
| 98 | .cell(GROUP_ID, tunnel.groupId().id()) |
| 99 | .cell(BANDWIDTH, tunnel.annotations().value(BANDWIDTH)) |
| 100 | .cell(PATH, tunnel.path()); |
| 101 | } |
| 102 | } |