blob: 42d7a1e8c022215a25538d8cd8adee1f34d22959 [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;
27import org.onosproject.ui.table.RowComparator;
28import org.onosproject.ui.table.TableRow;
29import org.onosproject.ui.table.TableUtils;
Thomas Vachuskae586b792015-03-26 13:59:38 -070030
31import java.util.ArrayList;
32import java.util.Arrays;
Simon Huntd2747a02015-04-30 22:41:16 -070033import java.util.Collection;
Thomas Vachuskae586b792015-03-26 13:59:38 -070034import java.util.List;
35
Thomas Vachuska58eded62015-03-31 12:04:03 -070036import static com.google.common.base.Strings.isNullOrEmpty;
37
Thomas Vachuskae586b792015-03-26 13:59:38 -070038/**
39 * Message handler for host view related messages.
40 */
Simon Hunta0ddb022015-05-01 09:53:01 -070041public class HostViewMessageHandler extends UiMessageHandler {
Thomas Vachuskae586b792015-03-26 13:59:38 -070042
Simon Huntd2747a02015-04-30 22:41:16 -070043 private static final String HOST_DATA_REQ = "hostDataRequest";
44
Thomas Vachuskae586b792015-03-26 13:59:38 -070045
46 @Override
Simon Huntd2747a02015-04-30 22:41:16 -070047 protected Collection<RequestHandler> getHandlers() {
48 return ImmutableSet.of(new HostDataRequest());
49 }
50
51 // ======================================================================
52
53 private final class HostDataRequest extends RequestHandler {
54
55 private HostDataRequest() {
56 super(HOST_DATA_REQ);
57 }
58
59 @Override
60 public void process(long sid, ObjectNode payload) {
61 RowComparator rc = TableUtils.createRowComparator(payload);
62
63 HostService service = get(HostService.class);
64 TableRow[] rows = generateTableRows(service);
65 Arrays.sort(rows, rc);
66 ObjectNode rootNode = MAPPER.createObjectNode();
67 rootNode.set("hosts", TableUtils.generateArrayNode(rows));
68
69 sendMessage("hostDataResponse", 0, rootNode);
70 }
71
72 private TableRow[] generateTableRows(HostService service) {
73 List<TableRow> list = new ArrayList<>();
74 for (Host host : service.getHosts()) {
75 list.add(new HostTableRow(host));
76 }
77 return list.toArray(new TableRow[list.size()]);
Simon Hunt44aa2f82015-04-30 15:01:35 -070078 }
79 }
80
Simon Huntd2747a02015-04-30 22:41:16 -070081 // ======================================================================
Thomas Vachuskae586b792015-03-26 13:59:38 -070082
83 /**
Bri Prebilic Cole19a32dd2015-03-26 18:00:03 -070084 * TableRow implementation for {@link Host hosts}.
Thomas Vachuskae586b792015-03-26 13:59:38 -070085 */
86 private static class HostTableRow extends AbstractTableRow {
87
Thomas Vachuska58eded62015-03-31 12:04:03 -070088 private static final String TYPE_IID = "_iconid_type";
Thomas Vachuskae586b792015-03-26 13:59:38 -070089 private static final String ID = "id";
Bri Prebilic Cole19a32dd2015-03-26 18:00:03 -070090 private static final String MAC = "mac";
91 private static final String VLAN = "vlan";
92 private static final String IPS = "ips";
93 private static final String LOCATION = "location";
Thomas Vachuskae586b792015-03-26 13:59:38 -070094
Thomas Vachuska58eded62015-03-31 12:04:03 -070095 private static final String HOST_ICON_PREFIX = "hostIcon_";
96
Thomas Vachuskae586b792015-03-26 13:59:38 -070097 private static final String[] COL_IDS = {
Thomas Vachuska58eded62015-03-31 12:04:03 -070098 TYPE_IID, ID, MAC, VLAN, IPS, LOCATION
Thomas Vachuskae586b792015-03-26 13:59:38 -070099 };
100
Bri Prebilic Cole19a32dd2015-03-26 18:00:03 -0700101 public HostTableRow(Host h) {
102 HostLocation location = h.location();
Thomas Vachuskae586b792015-03-26 13:59:38 -0700103
Thomas Vachuska58eded62015-03-31 12:04:03 -0700104 add(TYPE_IID, getTypeIconId(h));
Simon Hunt44aa2f82015-04-30 15:01:35 -0700105 add(ID, h.id());
106 add(MAC, h.mac());
107 add(VLAN, h.vlan());
108 add(IPS, h.ipAddresses());
109 add(LOCATION, concat(location.deviceId(), "/", location.port()));
Thomas Vachuskae586b792015-03-26 13:59:38 -0700110 }
111
Thomas Vachuska58eded62015-03-31 12:04:03 -0700112 private String getTypeIconId(Host host) {
113 String hostType = host.annotations().value(AnnotationKeys.TYPE);
114 return HOST_ICON_PREFIX +
115 (isNullOrEmpty(hostType) ? "endstation" : hostType);
116 }
117
Thomas Vachuskae586b792015-03-26 13:59:38 -0700118 @Override
119 protected String[] columnIds() {
120 return COL_IDS;
121 }
122 }
123
124}