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; |
Thomas Vachuska | 58eded6 | 2015-03-31 12:04:03 -0700 | [diff] [blame] | 21 | import org.onosproject.net.AnnotationKeys; |
Bri Prebilic Cole | 19a32dd | 2015-03-26 18:00:03 -0700 | [diff] [blame] | 22 | import org.onosproject.net.Host; |
| 23 | import org.onosproject.net.HostLocation; |
| 24 | import org.onosproject.net.host.HostService; |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 25 | |
| 26 | import java.util.ArrayList; |
| 27 | import java.util.Arrays; |
| 28 | import java.util.List; |
| 29 | |
Thomas Vachuska | 58eded6 | 2015-03-31 12:04:03 -0700 | [diff] [blame] | 30 | import static com.google.common.base.Strings.isNullOrEmpty; |
| 31 | |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 32 | /** |
| 33 | * Message handler for host view related messages. |
| 34 | */ |
| 35 | public 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 Cole | 19a32dd | 2015-03-26 18:00:03 -0700 | [diff] [blame] | 50 | HostService service = get(HostService.class); |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 51 | TableRow[] rows = generateTableRows(service); |
Bri Prebilic Cole | 19a32dd | 2015-03-26 18:00:03 -0700 | [diff] [blame] | 52 | RowComparator rc = |
| 53 | new RowComparator(sortCol, RowComparator.direction(sortDir)); |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 54 | Arrays.sort(rows, rc); |
Bri Prebilic Cole | 19a32dd | 2015-03-26 18:00:03 -0700 | [diff] [blame] | 55 | ArrayNode hosts = generateArrayNode(rows); |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 56 | ObjectNode rootNode = mapper.createObjectNode(); |
Bri Prebilic Cole | 19a32dd | 2015-03-26 18:00:03 -0700 | [diff] [blame] | 57 | rootNode.set("hosts", hosts); |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 58 | |
| 59 | connection().sendMessage("hostDataResponse", 0, rootNode); |
| 60 | } |
| 61 | |
Bri Prebilic Cole | 19a32dd | 2015-03-26 18:00:03 -0700 | [diff] [blame] | 62 | private TableRow[] generateTableRows(HostService service) { |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 63 | List<TableRow> list = new ArrayList<>(); |
Bri Prebilic Cole | 19a32dd | 2015-03-26 18:00:03 -0700 | [diff] [blame] | 64 | for (Host host : service.getHosts()) { |
| 65 | list.add(new HostTableRow(host)); |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 66 | } |
| 67 | return list.toArray(new TableRow[list.size()]); |
| 68 | } |
| 69 | |
| 70 | /** |
Bri Prebilic Cole | 19a32dd | 2015-03-26 18:00:03 -0700 | [diff] [blame] | 71 | * TableRow implementation for {@link Host hosts}. |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 72 | */ |
| 73 | private static class HostTableRow extends AbstractTableRow { |
| 74 | |
Thomas Vachuska | 58eded6 | 2015-03-31 12:04:03 -0700 | [diff] [blame] | 75 | private static final String TYPE_IID = "_iconid_type"; |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 76 | private static final String ID = "id"; |
Bri Prebilic Cole | 19a32dd | 2015-03-26 18:00:03 -0700 | [diff] [blame] | 77 | 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 Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 81 | |
Thomas Vachuska | 58eded6 | 2015-03-31 12:04:03 -0700 | [diff] [blame] | 82 | private static final String HOST_ICON_PREFIX = "hostIcon_"; |
| 83 | |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 84 | private static final String[] COL_IDS = { |
Thomas Vachuska | 58eded6 | 2015-03-31 12:04:03 -0700 | [diff] [blame] | 85 | TYPE_IID, ID, MAC, VLAN, IPS, LOCATION |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 86 | }; |
| 87 | |
Bri Prebilic Cole | 19a32dd | 2015-03-26 18:00:03 -0700 | [diff] [blame] | 88 | public HostTableRow(Host h) { |
| 89 | HostLocation location = h.location(); |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 90 | |
Thomas Vachuska | 58eded6 | 2015-03-31 12:04:03 -0700 | [diff] [blame] | 91 | add(TYPE_IID, getTypeIconId(h)); |
Bri Prebilic Cole | 19a32dd | 2015-03-26 18:00:03 -0700 | [diff] [blame] | 92 | 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 Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Thomas Vachuska | 58eded6 | 2015-03-31 12:04:03 -0700 | [diff] [blame] | 100 | 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 Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 106 | @Override |
| 107 | protected String[] columnIds() { |
| 108 | return COL_IDS; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | } |