Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.ui.impl; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 20 | import com.google.common.collect.ImmutableSet; |
Bri Prebilic Cole | 19a32dd | 2015-03-26 18:00:03 -0700 | [diff] [blame] | 21 | import org.onosproject.net.Host; |
| 22 | import org.onosproject.net.HostLocation; |
| 23 | import org.onosproject.net.host.HostService; |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 24 | |
| 25 | import java.util.ArrayList; |
| 26 | import java.util.Arrays; |
| 27 | import java.util.List; |
| 28 | |
| 29 | /** |
| 30 | * Message handler for host view related messages. |
| 31 | */ |
| 32 | public 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 Cole | 19a32dd | 2015-03-26 18:00:03 -0700 | [diff] [blame] | 47 | HostService service = get(HostService.class); |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 48 | TableRow[] rows = generateTableRows(service); |
Bri Prebilic Cole | 19a32dd | 2015-03-26 18:00:03 -0700 | [diff] [blame] | 49 | RowComparator rc = |
| 50 | new RowComparator(sortCol, RowComparator.direction(sortDir)); |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 51 | Arrays.sort(rows, rc); |
Bri Prebilic Cole | 19a32dd | 2015-03-26 18:00:03 -0700 | [diff] [blame] | 52 | ArrayNode hosts = generateArrayNode(rows); |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 53 | ObjectNode rootNode = mapper.createObjectNode(); |
Bri Prebilic Cole | 19a32dd | 2015-03-26 18:00:03 -0700 | [diff] [blame] | 54 | rootNode.set("hosts", hosts); |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 55 | |
| 56 | connection().sendMessage("hostDataResponse", 0, rootNode); |
| 57 | } |
| 58 | |
Bri Prebilic Cole | 19a32dd | 2015-03-26 18:00:03 -0700 | [diff] [blame] | 59 | private TableRow[] generateTableRows(HostService service) { |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 60 | List<TableRow> list = new ArrayList<>(); |
Bri Prebilic Cole | 19a32dd | 2015-03-26 18:00:03 -0700 | [diff] [blame] | 61 | for (Host host : service.getHosts()) { |
| 62 | list.add(new HostTableRow(host)); |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 63 | } |
| 64 | return list.toArray(new TableRow[list.size()]); |
| 65 | } |
| 66 | |
| 67 | /** |
Bri Prebilic Cole | 19a32dd | 2015-03-26 18:00:03 -0700 | [diff] [blame] | 68 | * TableRow implementation for {@link Host hosts}. |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 69 | */ |
| 70 | private static class HostTableRow extends AbstractTableRow { |
| 71 | |
| 72 | private static final String ID = "id"; |
Bri Prebilic Cole | 19a32dd | 2015-03-26 18:00:03 -0700 | [diff] [blame] | 73 | 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 Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 77 | |
| 78 | private static final String[] COL_IDS = { |
Bri Prebilic Cole | 19a32dd | 2015-03-26 18:00:03 -0700 | [diff] [blame] | 79 | ID, MAC, VLAN, IPS, LOCATION |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 80 | }; |
| 81 | |
Bri Prebilic Cole | 19a32dd | 2015-03-26 18:00:03 -0700 | [diff] [blame] | 82 | public HostTableRow(Host h) { |
| 83 | HostLocation location = h.location(); |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 84 | |
Bri Prebilic Cole | 19a32dd | 2015-03-26 18:00:03 -0700 | [diff] [blame] | 85 | 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 Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | @Override |
| 94 | protected String[] columnIds() { |
| 95 | return COL_IDS; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | } |