blob: 689a266f2db3b902f9da7e1f99dba785fba77d7f [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;
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;
23import org.onosproject.net.HostLocation;
24import org.onosproject.net.host.HostService;
Thomas Vachuskae586b792015-03-26 13:59:38 -070025
26import java.util.ArrayList;
27import java.util.Arrays;
28import java.util.List;
29
Thomas Vachuska58eded62015-03-31 12:04:03 -070030import static com.google.common.base.Strings.isNullOrEmpty;
31
Thomas Vachuskae586b792015-03-26 13:59:38 -070032/**
33 * Message handler for host view related messages.
34 */
35public class HostViewMessageHandler extends AbstractTabularViewMessageHandler {
36
37 /**
38 * Creates a new message handler for the host messages.
39 */
40 protected HostViewMessageHandler() {
41 super(ImmutableSet.of("hostDataRequest"));
42 }
43
44 @Override
45 public void process(ObjectNode message) {
46 ObjectNode payload = payload(message);
47 String sortCol = string(payload, "sortCol", "id");
48 String sortDir = string(payload, "sortDir", "asc");
49
Bri Prebilic Cole19a32dd2015-03-26 18:00:03 -070050 HostService service = get(HostService.class);
Thomas Vachuskae586b792015-03-26 13:59:38 -070051 TableRow[] rows = generateTableRows(service);
Bri Prebilic Cole19a32dd2015-03-26 18:00:03 -070052 RowComparator rc =
53 new RowComparator(sortCol, RowComparator.direction(sortDir));
Thomas Vachuskae586b792015-03-26 13:59:38 -070054 Arrays.sort(rows, rc);
Bri Prebilic Cole19a32dd2015-03-26 18:00:03 -070055 ArrayNode hosts = generateArrayNode(rows);
Thomas Vachuskae586b792015-03-26 13:59:38 -070056 ObjectNode rootNode = mapper.createObjectNode();
Bri Prebilic Cole19a32dd2015-03-26 18:00:03 -070057 rootNode.set("hosts", hosts);
Thomas Vachuskae586b792015-03-26 13:59:38 -070058
59 connection().sendMessage("hostDataResponse", 0, rootNode);
60 }
61
Bri Prebilic Cole19a32dd2015-03-26 18:00:03 -070062 private TableRow[] generateTableRows(HostService service) {
Thomas Vachuskae586b792015-03-26 13:59:38 -070063 List<TableRow> list = new ArrayList<>();
Bri Prebilic Cole19a32dd2015-03-26 18:00:03 -070064 for (Host host : service.getHosts()) {
65 list.add(new HostTableRow(host));
Thomas Vachuskae586b792015-03-26 13:59:38 -070066 }
67 return list.toArray(new TableRow[list.size()]);
68 }
69
70 /**
Bri Prebilic Cole19a32dd2015-03-26 18:00:03 -070071 * TableRow implementation for {@link Host hosts}.
Thomas Vachuskae586b792015-03-26 13:59:38 -070072 */
73 private static class HostTableRow extends AbstractTableRow {
74
Thomas Vachuska58eded62015-03-31 12:04:03 -070075 private static final String TYPE_IID = "_iconid_type";
Thomas Vachuskae586b792015-03-26 13:59:38 -070076 private static final String ID = "id";
Bri Prebilic Cole19a32dd2015-03-26 18:00:03 -070077 private static final String MAC = "mac";
78 private static final String VLAN = "vlan";
79 private static final String IPS = "ips";
80 private static final String LOCATION = "location";
Thomas Vachuskae586b792015-03-26 13:59:38 -070081
Thomas Vachuska58eded62015-03-31 12:04:03 -070082 private static final String HOST_ICON_PREFIX = "hostIcon_";
83
Thomas Vachuskae586b792015-03-26 13:59:38 -070084 private static final String[] COL_IDS = {
Thomas Vachuska58eded62015-03-31 12:04:03 -070085 TYPE_IID, ID, MAC, VLAN, IPS, LOCATION
Thomas Vachuskae586b792015-03-26 13:59:38 -070086 };
87
Bri Prebilic Cole19a32dd2015-03-26 18:00:03 -070088 public HostTableRow(Host h) {
89 HostLocation location = h.location();
Thomas Vachuskae586b792015-03-26 13:59:38 -070090
Thomas Vachuska58eded62015-03-31 12:04:03 -070091 add(TYPE_IID, getTypeIconId(h));
Bri Prebilic Cole19a32dd2015-03-26 18:00:03 -070092 add(ID, h.id().toString());
93 add(MAC, h.mac().toString());
94 add(VLAN, h.vlan().toString());
95 add(IPS, h.ipAddresses().toString());
96 add(LOCATION, (location.deviceId().toString() + '/' +
97 location.port().toString()));
Thomas Vachuskae586b792015-03-26 13:59:38 -070098 }
99
Thomas Vachuska58eded62015-03-31 12:04:03 -0700100 private String getTypeIconId(Host host) {
101 String hostType = host.annotations().value(AnnotationKeys.TYPE);
102 return HOST_ICON_PREFIX +
103 (isNullOrEmpty(hostType) ? "endstation" : hostType);
104 }
105
Thomas Vachuskae586b792015-03-26 13:59:38 -0700106 @Override
107 protected String[] columnIds() {
108 return COL_IDS;
109 }
110 }
111
112}