blob: 643fe40c6e300d04f3bd378d318ed46793e9671e [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;
Thomas Vachuska58eded62015-03-31 12:04:03 -070020import org.onosproject.net.AnnotationKeys;
Bri Prebilic Cole19a32dd2015-03-26 18:00:03 -070021import org.onosproject.net.Host;
Bri Prebilic Cole19a32dd2015-03-26 18:00:03 -070022import org.onosproject.net.host.HostService;
Simon Huntd2747a02015-04-30 22:41:16 -070023import org.onosproject.ui.RequestHandler;
Simon Hunta0ddb022015-05-01 09:53:01 -070024import org.onosproject.ui.UiMessageHandler;
Simon Hunt3d1b0652015-05-05 17:27:24 -070025import org.onosproject.ui.table.TableModel;
Simon Huntabd16f62015-05-01 13:14:40 -070026import org.onosproject.ui.table.TableRequestHandler;
Simon Hunt3d1b0652015-05-05 17:27:24 -070027import org.onosproject.ui.table.cell.HostLocationFormatter;
Thomas Vachuskae586b792015-03-26 13:59:38 -070028
Simon Huntd2747a02015-04-30 22:41:16 -070029import java.util.Collection;
Thomas Vachuskae586b792015-03-26 13:59:38 -070030
Thomas Vachuska58eded62015-03-31 12:04:03 -070031import static com.google.common.base.Strings.isNullOrEmpty;
32
Thomas Vachuskae586b792015-03-26 13:59:38 -070033/**
34 * Message handler for host view related messages.
35 */
Simon Hunta0ddb022015-05-01 09:53:01 -070036public class HostViewMessageHandler extends UiMessageHandler {
Thomas Vachuskae586b792015-03-26 13:59:38 -070037
Simon Huntd2747a02015-04-30 22:41:16 -070038 private static final String HOST_DATA_REQ = "hostDataRequest";
Simon Huntabd16f62015-05-01 13:14:40 -070039 private static final String HOST_DATA_RESP = "hostDataResponse";
40 private static final String HOSTS = "hosts";
41
42 private static final String TYPE_IID = "_iconid_type";
43 private static final String ID = "id";
44 private static final String MAC = "mac";
45 private static final String VLAN = "vlan";
46 private static final String IPS = "ips";
47 private static final String LOCATION = "location";
48
49 private static final String HOST_ICON_PREFIX = "hostIcon_";
Simon Huntd2747a02015-04-30 22:41:16 -070050
Simon Hunt3d1b0652015-05-05 17:27:24 -070051 private static final String[] COL_IDS = {
52 TYPE_IID, ID, MAC, VLAN, IPS, LOCATION
53 };
Thomas Vachuskae586b792015-03-26 13:59:38 -070054
55 @Override
Simon Huntd2747a02015-04-30 22:41:16 -070056 protected Collection<RequestHandler> getHandlers() {
57 return ImmutableSet.of(new HostDataRequest());
58 }
59
Simon Huntabd16f62015-05-01 13:14:40 -070060 // handler for host table requests
61 private final class HostDataRequest extends TableRequestHandler {
Simon Huntd2747a02015-04-30 22:41:16 -070062 private HostDataRequest() {
Simon Huntabd16f62015-05-01 13:14:40 -070063 super(HOST_DATA_REQ, HOST_DATA_RESP, HOSTS);
Simon Huntd2747a02015-04-30 22:41:16 -070064 }
65
66 @Override
Simon Hunt3d1b0652015-05-05 17:27:24 -070067 protected String[] getColumnIds() {
68 return COL_IDS;
Simon Hunt44aa2f82015-04-30 15:01:35 -070069 }
Simon Hunt44aa2f82015-04-30 15:01:35 -070070
Simon Hunt3d1b0652015-05-05 17:27:24 -070071 @Override
72 protected TableModel createTableModel() {
73 TableModel tm = super.createTableModel();
74 tm.setFormatter(LOCATION, HostLocationFormatter.INSTANCE);
75 return tm;
76 }
Thomas Vachuskae586b792015-03-26 13:59:38 -070077
Simon Hunt3d1b0652015-05-05 17:27:24 -070078 @Override
79 protected void populateTable(TableModel tm, ObjectNode payload) {
80 HostService hs = get(HostService.class);
81 for (Host host : hs.getHosts()) {
82 populateRow(tm.addRow(), host);
83 }
84 }
Thomas Vachuskae586b792015-03-26 13:59:38 -070085
Simon Hunt3d1b0652015-05-05 17:27:24 -070086 private void populateRow(TableModel.Row row, Host host) {
87 row.cell(TYPE_IID, getTypeIconId(host))
88 .cell(ID, host.id())
89 .cell(MAC, host.mac())
90 .cell(VLAN, host.vlan())
91 .cell(IPS, host.ipAddresses())
92 .cell(LOCATION, host.location());
Thomas Vachuskae586b792015-03-26 13:59:38 -070093 }
94
Thomas Vachuska58eded62015-03-31 12:04:03 -070095 private String getTypeIconId(Host host) {
96 String hostType = host.annotations().value(AnnotationKeys.TYPE);
97 return HOST_ICON_PREFIX +
98 (isNullOrEmpty(hostType) ? "endstation" : hostType);
99 }
Thomas Vachuskae586b792015-03-26 13:59:38 -0700100 }
Thomas Vachuskae586b792015-03-26 13:59:38 -0700101}