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 | |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 19 | import com.google.common.collect.ImmutableSet; |
Thomas Vachuska | 58eded6 | 2015-03-31 12:04:03 -0700 | [diff] [blame] | 20 | import org.onosproject.net.AnnotationKeys; |
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; |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 24 | import org.onosproject.ui.RequestHandler; |
Simon Hunt | a0ddb02 | 2015-05-01 09:53:01 -0700 | [diff] [blame^] | 25 | import org.onosproject.ui.UiMessageHandler; |
Simon Hunt | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame] | 26 | import org.onosproject.ui.table.AbstractTableRow; |
| 27 | import org.onosproject.ui.table.RowComparator; |
| 28 | import org.onosproject.ui.table.TableRow; |
| 29 | import org.onosproject.ui.table.TableUtils; |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 30 | |
| 31 | import java.util.ArrayList; |
| 32 | import java.util.Arrays; |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 33 | import java.util.Collection; |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 34 | import java.util.List; |
| 35 | |
Thomas Vachuska | 58eded6 | 2015-03-31 12:04:03 -0700 | [diff] [blame] | 36 | import static com.google.common.base.Strings.isNullOrEmpty; |
| 37 | |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 38 | /** |
| 39 | * Message handler for host view related messages. |
| 40 | */ |
Simon Hunt | a0ddb02 | 2015-05-01 09:53:01 -0700 | [diff] [blame^] | 41 | public class HostViewMessageHandler extends UiMessageHandler { |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 42 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 43 | private static final String HOST_DATA_REQ = "hostDataRequest"; |
| 44 | |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 45 | |
| 46 | @Override |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 47 | 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 Hunt | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 81 | // ====================================================================== |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 82 | |
| 83 | /** |
Bri Prebilic Cole | 19a32dd | 2015-03-26 18:00:03 -0700 | [diff] [blame] | 84 | * TableRow implementation for {@link Host hosts}. |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 85 | */ |
| 86 | private static class HostTableRow extends AbstractTableRow { |
| 87 | |
Thomas Vachuska | 58eded6 | 2015-03-31 12:04:03 -0700 | [diff] [blame] | 88 | private static final String TYPE_IID = "_iconid_type"; |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 89 | private static final String ID = "id"; |
Bri Prebilic Cole | 19a32dd | 2015-03-26 18:00:03 -0700 | [diff] [blame] | 90 | 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 Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 94 | |
Thomas Vachuska | 58eded6 | 2015-03-31 12:04:03 -0700 | [diff] [blame] | 95 | private static final String HOST_ICON_PREFIX = "hostIcon_"; |
| 96 | |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 97 | private static final String[] COL_IDS = { |
Thomas Vachuska | 58eded6 | 2015-03-31 12:04:03 -0700 | [diff] [blame] | 98 | TYPE_IID, ID, MAC, VLAN, IPS, LOCATION |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 99 | }; |
| 100 | |
Bri Prebilic Cole | 19a32dd | 2015-03-26 18:00:03 -0700 | [diff] [blame] | 101 | public HostTableRow(Host h) { |
| 102 | HostLocation location = h.location(); |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 103 | |
Thomas Vachuska | 58eded6 | 2015-03-31 12:04:03 -0700 | [diff] [blame] | 104 | add(TYPE_IID, getTypeIconId(h)); |
Simon Hunt | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame] | 105 | 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 Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Thomas Vachuska | 58eded6 | 2015-03-31 12:04:03 -0700 | [diff] [blame] | 112 | 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 Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 118 | @Override |
| 119 | protected String[] columnIds() { |
| 120 | return COL_IDS; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | } |