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; |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame^] | 27 | import org.onosproject.ui.table.TableRequestHandler; |
Simon Hunt | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame] | 28 | import org.onosproject.ui.table.TableRow; |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 29 | |
| 30 | import java.util.ArrayList; |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 31 | import java.util.Collection; |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 32 | import java.util.List; |
| 33 | |
Thomas Vachuska | 58eded6 | 2015-03-31 12:04:03 -0700 | [diff] [blame] | 34 | import static com.google.common.base.Strings.isNullOrEmpty; |
| 35 | |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 36 | /** |
| 37 | * Message handler for host view related messages. |
| 38 | */ |
Simon Hunt | a0ddb02 | 2015-05-01 09:53:01 -0700 | [diff] [blame] | 39 | public class HostViewMessageHandler extends UiMessageHandler { |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 40 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 41 | private static final String HOST_DATA_REQ = "hostDataRequest"; |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame^] | 42 | private static final String HOST_DATA_RESP = "hostDataResponse"; |
| 43 | private static final String HOSTS = "hosts"; |
| 44 | |
| 45 | private static final String TYPE_IID = "_iconid_type"; |
| 46 | private static final String ID = "id"; |
| 47 | private static final String MAC = "mac"; |
| 48 | private static final String VLAN = "vlan"; |
| 49 | private static final String IPS = "ips"; |
| 50 | private static final String LOCATION = "location"; |
| 51 | |
| 52 | private static final String HOST_ICON_PREFIX = "hostIcon_"; |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 53 | |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 54 | |
| 55 | @Override |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 56 | protected Collection<RequestHandler> getHandlers() { |
| 57 | return ImmutableSet.of(new HostDataRequest()); |
| 58 | } |
| 59 | |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame^] | 60 | // handler for host table requests |
| 61 | private final class HostDataRequest extends TableRequestHandler { |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 62 | private HostDataRequest() { |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame^] | 63 | super(HOST_DATA_REQ, HOST_DATA_RESP, HOSTS); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | @Override |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame^] | 67 | protected TableRow[] generateTableRows(ObjectNode payload) { |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 68 | HostService service = get(HostService.class); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 69 | List<TableRow> list = new ArrayList<>(); |
| 70 | for (Host host : service.getHosts()) { |
| 71 | list.add(new HostTableRow(host)); |
| 72 | } |
| 73 | return list.toArray(new TableRow[list.size()]); |
Simon Hunt | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 77 | /** |
Bri Prebilic Cole | 19a32dd | 2015-03-26 18:00:03 -0700 | [diff] [blame] | 78 | * TableRow implementation for {@link Host hosts}. |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 79 | */ |
| 80 | private static class HostTableRow extends AbstractTableRow { |
| 81 | |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 82 | private static final String[] COL_IDS = { |
Thomas Vachuska | 58eded6 | 2015-03-31 12:04:03 -0700 | [diff] [blame] | 83 | TYPE_IID, ID, MAC, VLAN, IPS, LOCATION |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 84 | }; |
| 85 | |
Bri Prebilic Cole | 19a32dd | 2015-03-26 18:00:03 -0700 | [diff] [blame] | 86 | public HostTableRow(Host h) { |
| 87 | HostLocation location = h.location(); |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 88 | |
Thomas Vachuska | 58eded6 | 2015-03-31 12:04:03 -0700 | [diff] [blame] | 89 | add(TYPE_IID, getTypeIconId(h)); |
Simon Hunt | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame] | 90 | add(ID, h.id()); |
| 91 | add(MAC, h.mac()); |
| 92 | add(VLAN, h.vlan()); |
| 93 | add(IPS, h.ipAddresses()); |
| 94 | add(LOCATION, concat(location.deviceId(), "/", location.port())); |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Thomas Vachuska | 58eded6 | 2015-03-31 12:04:03 -0700 | [diff] [blame] | 97 | private String getTypeIconId(Host host) { |
| 98 | String hostType = host.annotations().value(AnnotationKeys.TYPE); |
| 99 | return HOST_ICON_PREFIX + |
| 100 | (isNullOrEmpty(hostType) ? "endstation" : hostType); |
| 101 | } |
| 102 | |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 103 | @Override |
| 104 | protected String[] columnIds() { |
| 105 | return COL_IDS; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | } |