blob: e20b3e113cdcbeda5d5357b95da062b1ebe928f3 [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;
22import org.onosproject.net.HostLocation;
23import 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;
Simon Hunt44aa2f82015-04-30 15:01:35 -070026import org.onosproject.ui.table.AbstractTableRow;
Simon Huntabd16f62015-05-01 13:14:40 -070027import org.onosproject.ui.table.TableRequestHandler;
Simon Hunt44aa2f82015-04-30 15:01:35 -070028import org.onosproject.ui.table.TableRow;
Thomas Vachuskae586b792015-03-26 13:59:38 -070029
30import java.util.ArrayList;
Simon Huntd2747a02015-04-30 22:41:16 -070031import java.util.Collection;
Thomas Vachuskae586b792015-03-26 13:59:38 -070032import java.util.List;
33
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
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 Huntabd16f62015-05-01 13:14:40 -070067 protected TableRow[] generateTableRows(ObjectNode payload) {
Simon Huntd2747a02015-04-30 22:41:16 -070068 HostService service = get(HostService.class);
Simon Huntd2747a02015-04-30 22:41:16 -070069 List<TableRow> list = new ArrayList<>();
70 for (Host host : service.getHosts()) {
71 list.add(new HostTableRow(host));
72 }
73 return list.toArray(new TableRow[list.size()]);
Simon Hunt44aa2f82015-04-30 15:01:35 -070074 }
75 }
76
Thomas Vachuskae586b792015-03-26 13:59:38 -070077 /**
Bri Prebilic Cole19a32dd2015-03-26 18:00:03 -070078 * TableRow implementation for {@link Host hosts}.
Thomas Vachuskae586b792015-03-26 13:59:38 -070079 */
80 private static class HostTableRow extends AbstractTableRow {
81
Thomas Vachuskae586b792015-03-26 13:59:38 -070082 private static final String[] COL_IDS = {
Thomas Vachuska58eded62015-03-31 12:04:03 -070083 TYPE_IID, ID, MAC, VLAN, IPS, LOCATION
Thomas Vachuskae586b792015-03-26 13:59:38 -070084 };
85
Bri Prebilic Cole19a32dd2015-03-26 18:00:03 -070086 public HostTableRow(Host h) {
87 HostLocation location = h.location();
Thomas Vachuskae586b792015-03-26 13:59:38 -070088
Thomas Vachuska58eded62015-03-31 12:04:03 -070089 add(TYPE_IID, getTypeIconId(h));
Simon Hunt44aa2f82015-04-30 15:01:35 -070090 add(ID, h.id());
91 add(MAC, h.mac());
92 add(VLAN, h.vlan());
93 add(IPS, h.ipAddresses());
94 add(LOCATION, concat(location.deviceId(), "/", location.port()));
Thomas Vachuskae586b792015-03-26 13:59:38 -070095 }
96
Thomas Vachuska58eded62015-03-31 12:04:03 -070097 private String getTypeIconId(Host host) {
98 String hostType = host.annotations().value(AnnotationKeys.TYPE);
99 return HOST_ICON_PREFIX +
100 (isNullOrEmpty(hostType) ? "endstation" : hostType);
101 }
102
Thomas Vachuskae586b792015-03-26 13:59:38 -0700103 @Override
104 protected String[] columnIds() {
105 return COL_IDS;
106 }
107 }
108
109}