blob: 47137ee6ad00424ed720b8ae7f401b3b3eb0b20e [file] [log] [blame]
Thomas Vachuskae586b792015-03-26 13:59:38 -07001/*
2 * Copyright 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.ui.impl;
17
Thomas Vachuskae586b792015-03-26 13:59:38 -070018import com.fasterxml.jackson.databind.node.ObjectNode;
19import com.google.common.collect.ImmutableSet;
Bri Prebilic Cole9467a232015-05-06 16:59:05 -070020import org.onlab.packet.IpAddress;
Thomas Vachuska58eded62015-03-31 12:04:03 -070021import org.onosproject.net.AnnotationKeys;
Bri Prebilic Cole19a32dd2015-03-26 18:00:03 -070022import org.onosproject.net.Host;
Bri Prebilic Cole19a32dd2015-03-26 18:00:03 -070023import org.onosproject.net.host.HostService;
Simon Huntd2747a02015-04-30 22:41:16 -070024import org.onosproject.ui.RequestHandler;
Simon Hunta0ddb022015-05-01 09:53:01 -070025import org.onosproject.ui.UiMessageHandler;
Bri Prebilic Cole9467a232015-05-06 16:59:05 -070026import org.onosproject.ui.table.CellFormatter;
Simon Hunt3d1b0652015-05-05 17:27:24 -070027import org.onosproject.ui.table.TableModel;
Simon Huntabd16f62015-05-01 13:14:40 -070028import org.onosproject.ui.table.TableRequestHandler;
Simon Hunt3d1b0652015-05-05 17:27:24 -070029import org.onosproject.ui.table.cell.HostLocationFormatter;
Thomas Vachuskae586b792015-03-26 13:59:38 -070030
Simon Huntd2747a02015-04-30 22:41:16 -070031import java.util.Collection;
Bri Prebilic Cole9467a232015-05-06 16:59:05 -070032import java.util.Set;
Thomas Vachuskae586b792015-03-26 13:59:38 -070033
Thomas Vachuska58eded62015-03-31 12:04:03 -070034import static com.google.common.base.Strings.isNullOrEmpty;
35
Thomas Vachuskae586b792015-03-26 13:59:38 -070036/**
37 * Message handler for host view related messages.
38 */
Simon Hunta0ddb022015-05-01 09:53:01 -070039public class HostViewMessageHandler extends UiMessageHandler {
Thomas Vachuskae586b792015-03-26 13:59:38 -070040
Simon Huntd2747a02015-04-30 22:41:16 -070041 private static final String HOST_DATA_REQ = "hostDataRequest";
Simon Huntabd16f62015-05-01 13:14:40 -070042 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 Huntd2747a02015-04-30 22:41:16 -070053
Simon Hunt3d1b0652015-05-05 17:27:24 -070054 private static final String[] COL_IDS = {
55 TYPE_IID, ID, MAC, VLAN, IPS, LOCATION
56 };
Thomas Vachuskae586b792015-03-26 13:59:38 -070057
58 @Override
Simon Huntda580882015-05-12 20:58:18 -070059 protected Collection<RequestHandler> createRequestHandlers() {
Simon Huntd2747a02015-04-30 22:41:16 -070060 return ImmutableSet.of(new HostDataRequest());
61 }
62
Simon Huntabd16f62015-05-01 13:14:40 -070063 // handler for host table requests
64 private final class HostDataRequest extends TableRequestHandler {
Jian Li69f66632016-01-15 12:27:42 -080065 private static final String NO_ROWS_MESSAGE = "No hosts found";
66
Simon Huntd2747a02015-04-30 22:41:16 -070067 private HostDataRequest() {
Simon Huntabd16f62015-05-01 13:14:40 -070068 super(HOST_DATA_REQ, HOST_DATA_RESP, HOSTS);
Simon Huntd2747a02015-04-30 22:41:16 -070069 }
70
71 @Override
Simon Hunt3d1b0652015-05-05 17:27:24 -070072 protected String[] getColumnIds() {
73 return COL_IDS;
Simon Hunt44aa2f82015-04-30 15:01:35 -070074 }
Simon Hunt44aa2f82015-04-30 15:01:35 -070075
Simon Hunt3d1b0652015-05-05 17:27:24 -070076 @Override
Jian Li69f66632016-01-15 12:27:42 -080077 protected String noRowsMessage() {
78 return NO_ROWS_MESSAGE;
79 }
80
81 @Override
Simon Hunt3d1b0652015-05-05 17:27:24 -070082 protected TableModel createTableModel() {
83 TableModel tm = super.createTableModel();
84 tm.setFormatter(LOCATION, HostLocationFormatter.INSTANCE);
Bri Prebilic Cole9467a232015-05-06 16:59:05 -070085 tm.setFormatter(IPS, new IpSetFormatter());
Simon Hunt3d1b0652015-05-05 17:27:24 -070086 return tm;
87 }
Thomas Vachuskae586b792015-03-26 13:59:38 -070088
Simon Hunt3d1b0652015-05-05 17:27:24 -070089 @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 Vachuskae586b792015-03-26 13:59:38 -070096
Simon Hunt3d1b0652015-05-05 17:27:24 -070097 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 Vachuskae586b792015-03-26 13:59:38 -0700104 }
105
Thomas Vachuska58eded62015-03-31 12:04:03 -0700106 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 Cole9467a232015-05-06 16:59:05 -0700111
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 Vachuskae586b792015-03-26 13:59:38 -0700136 }
Thomas Vachuskae586b792015-03-26 13:59:38 -0700137}