blob: a9b2b4acbd4ae27871086df2dc1b5f6f638107c6 [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
18import com.fasterxml.jackson.databind.node.ArrayNode;
19import com.fasterxml.jackson.databind.node.ObjectNode;
20import com.google.common.collect.ImmutableSet;
Bri Prebilic Cole19a32dd2015-03-26 18:00:03 -070021import org.onosproject.net.Host;
22import org.onosproject.net.HostLocation;
23import org.onosproject.net.host.HostService;
Thomas Vachuskae586b792015-03-26 13:59:38 -070024
25import java.util.ArrayList;
26import java.util.Arrays;
27import java.util.List;
28
29/**
30 * Message handler for host view related messages.
31 */
32public class HostViewMessageHandler extends AbstractTabularViewMessageHandler {
33
34 /**
35 * Creates a new message handler for the host messages.
36 */
37 protected HostViewMessageHandler() {
38 super(ImmutableSet.of("hostDataRequest"));
39 }
40
41 @Override
42 public void process(ObjectNode message) {
43 ObjectNode payload = payload(message);
44 String sortCol = string(payload, "sortCol", "id");
45 String sortDir = string(payload, "sortDir", "asc");
46
Bri Prebilic Cole19a32dd2015-03-26 18:00:03 -070047 HostService service = get(HostService.class);
Thomas Vachuskae586b792015-03-26 13:59:38 -070048 TableRow[] rows = generateTableRows(service);
Bri Prebilic Cole19a32dd2015-03-26 18:00:03 -070049 RowComparator rc =
50 new RowComparator(sortCol, RowComparator.direction(sortDir));
Thomas Vachuskae586b792015-03-26 13:59:38 -070051 Arrays.sort(rows, rc);
Bri Prebilic Cole19a32dd2015-03-26 18:00:03 -070052 ArrayNode hosts = generateArrayNode(rows);
Thomas Vachuskae586b792015-03-26 13:59:38 -070053 ObjectNode rootNode = mapper.createObjectNode();
Bri Prebilic Cole19a32dd2015-03-26 18:00:03 -070054 rootNode.set("hosts", hosts);
Thomas Vachuskae586b792015-03-26 13:59:38 -070055
56 connection().sendMessage("hostDataResponse", 0, rootNode);
57 }
58
Bri Prebilic Cole19a32dd2015-03-26 18:00:03 -070059 private TableRow[] generateTableRows(HostService service) {
Thomas Vachuskae586b792015-03-26 13:59:38 -070060 List<TableRow> list = new ArrayList<>();
Bri Prebilic Cole19a32dd2015-03-26 18:00:03 -070061 for (Host host : service.getHosts()) {
62 list.add(new HostTableRow(host));
Thomas Vachuskae586b792015-03-26 13:59:38 -070063 }
64 return list.toArray(new TableRow[list.size()]);
65 }
66
67 /**
Bri Prebilic Cole19a32dd2015-03-26 18:00:03 -070068 * TableRow implementation for {@link Host hosts}.
Thomas Vachuskae586b792015-03-26 13:59:38 -070069 */
70 private static class HostTableRow extends AbstractTableRow {
71
72 private static final String ID = "id";
Bri Prebilic Cole19a32dd2015-03-26 18:00:03 -070073 private static final String MAC = "mac";
74 private static final String VLAN = "vlan";
75 private static final String IPS = "ips";
76 private static final String LOCATION = "location";
Thomas Vachuskae586b792015-03-26 13:59:38 -070077
78 private static final String[] COL_IDS = {
Bri Prebilic Cole19a32dd2015-03-26 18:00:03 -070079 ID, MAC, VLAN, IPS, LOCATION
Thomas Vachuskae586b792015-03-26 13:59:38 -070080 };
81
Bri Prebilic Cole19a32dd2015-03-26 18:00:03 -070082 public HostTableRow(Host h) {
83 HostLocation location = h.location();
Thomas Vachuskae586b792015-03-26 13:59:38 -070084
Bri Prebilic Cole19a32dd2015-03-26 18:00:03 -070085 add(ID, h.id().toString());
86 add(MAC, h.mac().toString());
87 add(VLAN, h.vlan().toString());
88 add(IPS, h.ipAddresses().toString());
89 add(LOCATION, (location.deviceId().toString() + '/' +
90 location.port().toString()));
Thomas Vachuskae586b792015-03-26 13:59:38 -070091 }
92
93 @Override
94 protected String[] columnIds() {
95 return COL_IDS;
96 }
97 }
98
99}