blob: a42ed901507b1be29837e8f14a97fb1fd7309bdb [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 Huntd2747a02015-04-30 22:41:16 -070059 protected Collection<RequestHandler> getHandlers() {
60 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 {
Simon Huntd2747a02015-04-30 22:41:16 -070065 private HostDataRequest() {
Simon Huntabd16f62015-05-01 13:14:40 -070066 super(HOST_DATA_REQ, HOST_DATA_RESP, HOSTS);
Simon Huntd2747a02015-04-30 22:41:16 -070067 }
68
69 @Override
Simon Hunt3d1b0652015-05-05 17:27:24 -070070 protected String[] getColumnIds() {
71 return COL_IDS;
Simon Hunt44aa2f82015-04-30 15:01:35 -070072 }
Simon Hunt44aa2f82015-04-30 15:01:35 -070073
Simon Hunt3d1b0652015-05-05 17:27:24 -070074 @Override
75 protected TableModel createTableModel() {
76 TableModel tm = super.createTableModel();
77 tm.setFormatter(LOCATION, HostLocationFormatter.INSTANCE);
Bri Prebilic Cole9467a232015-05-06 16:59:05 -070078 tm.setFormatter(IPS, new IpSetFormatter());
Simon Hunt3d1b0652015-05-05 17:27:24 -070079 return tm;
80 }
Thomas Vachuskae586b792015-03-26 13:59:38 -070081
Simon Hunt3d1b0652015-05-05 17:27:24 -070082 @Override
83 protected void populateTable(TableModel tm, ObjectNode payload) {
84 HostService hs = get(HostService.class);
85 for (Host host : hs.getHosts()) {
86 populateRow(tm.addRow(), host);
87 }
88 }
Thomas Vachuskae586b792015-03-26 13:59:38 -070089
Simon Hunt3d1b0652015-05-05 17:27:24 -070090 private void populateRow(TableModel.Row row, Host host) {
91 row.cell(TYPE_IID, getTypeIconId(host))
92 .cell(ID, host.id())
93 .cell(MAC, host.mac())
94 .cell(VLAN, host.vlan())
95 .cell(IPS, host.ipAddresses())
96 .cell(LOCATION, host.location());
Thomas Vachuskae586b792015-03-26 13:59:38 -070097 }
98
Thomas Vachuska58eded62015-03-31 12:04:03 -070099 private String getTypeIconId(Host host) {
100 String hostType = host.annotations().value(AnnotationKeys.TYPE);
101 return HOST_ICON_PREFIX +
102 (isNullOrEmpty(hostType) ? "endstation" : hostType);
103 }
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700104
105 private final class IpSetFormatter implements CellFormatter {
106 private static final String COMMA = ", ";
107
108 @Override
109 public String format(Object value) {
110 Set<IpAddress> ips = (Set<IpAddress>) value;
111 if (ips.isEmpty()) {
112 return "(No IP Addresses for this host)";
113 }
114 StringBuilder sb = new StringBuilder();
115 for (IpAddress ip : ips) {
116 sb.append(ip.toString())
117 .append(COMMA);
118 }
119 removeTrailingComma(sb);
120 return sb.toString();
121 }
122
123 private StringBuilder removeTrailingComma(StringBuilder sb) {
124 int pos = sb.lastIndexOf(COMMA);
125 sb.delete(pos, sb.length());
126 return sb;
127 }
128 }
Thomas Vachuskae586b792015-03-26 13:59:38 -0700129 }
Thomas Vachuskae586b792015-03-26 13:59:38 -0700130}