Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | ce2a03d | 2017-08-03 19:21:03 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -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 | */ |
| 16 | package org.onosproject.ui.impl; |
| 17 | |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 19 | import com.google.common.collect.ImmutableSet; |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 20 | import org.onlab.packet.IpAddress; |
Thomas Vachuska | 58eded6 | 2015-03-31 12:04:03 -0700 | [diff] [blame] | 21 | import org.onosproject.net.AnnotationKeys; |
Bri Prebilic Cole | 19a32dd | 2015-03-26 18:00:03 -0700 | [diff] [blame] | 22 | import org.onosproject.net.Host; |
Bri Prebilic Cole | 19a32dd | 2015-03-26 18:00:03 -0700 | [diff] [blame] | 23 | import org.onosproject.net.host.HostService; |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 24 | import org.onosproject.ui.RequestHandler; |
Simon Hunt | a0ddb02 | 2015-05-01 09:53:01 -0700 | [diff] [blame] | 25 | import org.onosproject.ui.UiMessageHandler; |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 26 | import org.onosproject.ui.table.CellFormatter; |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 27 | import org.onosproject.ui.table.TableModel; |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 28 | import org.onosproject.ui.table.TableRequestHandler; |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 29 | import org.onosproject.ui.table.cell.HostLocationFormatter; |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 30 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 31 | import java.util.Collection; |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 32 | import java.util.Set; |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 33 | |
Thomas Vachuska | 58eded6 | 2015-03-31 12:04:03 -0700 | [diff] [blame] | 34 | import static com.google.common.base.Strings.isNullOrEmpty; |
| 35 | |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 36 | /** |
| 37 | * Message handler for host view related messages. |
| 38 | */ |
Simon Hunt | a0ddb02 | 2015-05-01 09:53:01 -0700 | [diff] [blame] | 39 | public class HostViewMessageHandler extends UiMessageHandler { |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 40 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 41 | private static final String HOST_DATA_REQ = "hostDataRequest"; |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 42 | private static final String HOST_DATA_RESP = "hostDataResponse"; |
| 43 | private static final String HOSTS = "hosts"; |
| 44 | |
| 45 | private static final String TYPE_IID = "_iconid_type"; |
| 46 | private static final String ID = "id"; |
| 47 | private static final String MAC = "mac"; |
| 48 | private static final String VLAN = "vlan"; |
| 49 | private static final String IPS = "ips"; |
| 50 | private static final String LOCATION = "location"; |
| 51 | |
| 52 | private static final String HOST_ICON_PREFIX = "hostIcon_"; |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 53 | |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 54 | private static final String[] COL_IDS = { |
| 55 | TYPE_IID, ID, MAC, VLAN, IPS, LOCATION |
| 56 | }; |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 57 | |
| 58 | @Override |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 59 | protected Collection<RequestHandler> createRequestHandlers() { |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 60 | return ImmutableSet.of(new HostDataRequest()); |
| 61 | } |
| 62 | |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 63 | // handler for host table requests |
| 64 | private final class HostDataRequest extends TableRequestHandler { |
Jian Li | 69f6663 | 2016-01-15 12:27:42 -0800 | [diff] [blame] | 65 | private static final String NO_ROWS_MESSAGE = "No hosts found"; |
| 66 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 67 | private HostDataRequest() { |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 68 | super(HOST_DATA_REQ, HOST_DATA_RESP, HOSTS); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | @Override |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 72 | protected String[] getColumnIds() { |
| 73 | return COL_IDS; |
Simon Hunt | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame] | 74 | } |
Simon Hunt | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame] | 75 | |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 76 | @Override |
Jian Li | 8baf447 | 2016-01-15 15:08:09 -0800 | [diff] [blame] | 77 | protected String noRowsMessage(ObjectNode payload) { |
Jian Li | 69f6663 | 2016-01-15 12:27:42 -0800 | [diff] [blame] | 78 | return NO_ROWS_MESSAGE; |
| 79 | } |
| 80 | |
| 81 | @Override |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 82 | protected TableModel createTableModel() { |
| 83 | TableModel tm = super.createTableModel(); |
| 84 | tm.setFormatter(LOCATION, HostLocationFormatter.INSTANCE); |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 85 | tm.setFormatter(IPS, new IpSetFormatter()); |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 86 | return tm; |
| 87 | } |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 88 | |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 89 | @Override |
| 90 | protected void populateTable(TableModel tm, ObjectNode payload) { |
| 91 | HostService hs = get(HostService.class); |
| 92 | for (Host host : hs.getHosts()) { |
| 93 | populateRow(tm.addRow(), host); |
| 94 | } |
| 95 | } |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 96 | |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 97 | private void populateRow(TableModel.Row row, Host host) { |
| 98 | row.cell(TYPE_IID, getTypeIconId(host)) |
| 99 | .cell(ID, host.id()) |
| 100 | .cell(MAC, host.mac()) |
| 101 | .cell(VLAN, host.vlan()) |
| 102 | .cell(IPS, host.ipAddresses()) |
| 103 | .cell(LOCATION, host.location()); |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Thomas Vachuska | 58eded6 | 2015-03-31 12:04:03 -0700 | [diff] [blame] | 106 | private String getTypeIconId(Host host) { |
| 107 | String hostType = host.annotations().value(AnnotationKeys.TYPE); |
| 108 | return HOST_ICON_PREFIX + |
| 109 | (isNullOrEmpty(hostType) ? "endstation" : hostType); |
| 110 | } |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 111 | |
| 112 | private final class IpSetFormatter implements CellFormatter { |
| 113 | private static final String COMMA = ", "; |
| 114 | |
| 115 | @Override |
| 116 | public String format(Object value) { |
| 117 | Set<IpAddress> ips = (Set<IpAddress>) value; |
| 118 | if (ips.isEmpty()) { |
| 119 | return "(No IP Addresses for this host)"; |
| 120 | } |
| 121 | StringBuilder sb = new StringBuilder(); |
| 122 | for (IpAddress ip : ips) { |
| 123 | sb.append(ip.toString()) |
| 124 | .append(COMMA); |
| 125 | } |
| 126 | removeTrailingComma(sb); |
| 127 | return sb.toString(); |
| 128 | } |
| 129 | |
| 130 | private StringBuilder removeTrailingComma(StringBuilder sb) { |
| 131 | int pos = sb.lastIndexOf(COMMA); |
| 132 | sb.delete(pos, sb.length()); |
| 133 | return sb; |
| 134 | } |
| 135 | } |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 136 | } |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 137 | } |