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 | ae65c96 | 2015-04-02 16:24:49 -0700 | [diff] [blame] | 21 | import org.onosproject.mastership.MastershipService; |
Thomas Vachuska | b52a014 | 2015-04-21 17:48:15 -0700 | [diff] [blame] | 22 | import org.onosproject.net.AnnotationKeys; |
Bri Prebilic Cole | 0feedc0 | 2015-04-09 14:17:37 -0700 | [diff] [blame] | 23 | import org.onosproject.net.ConnectPoint; |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 24 | import org.onosproject.net.Device; |
Bri Prebilic Cole | 264c5ec | 2015-04-07 10:22:26 -0700 | [diff] [blame] | 25 | import org.onosproject.net.DeviceId; |
Bri Prebilic Cole | 0feedc0 | 2015-04-09 14:17:37 -0700 | [diff] [blame] | 26 | import org.onosproject.net.Link; |
Bri Prebilic Cole | 264c5ec | 2015-04-07 10:22:26 -0700 | [diff] [blame] | 27 | import org.onosproject.net.Port; |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 28 | import org.onosproject.net.device.DeviceService; |
Bri Prebilic Cole | 264c5ec | 2015-04-07 10:22:26 -0700 | [diff] [blame] | 29 | import org.onosproject.net.link.LinkService; |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 30 | import org.onosproject.ui.RequestHandler; |
Simon Hunt | a0ddb02 | 2015-05-01 09:53:01 -0700 | [diff] [blame] | 31 | import org.onosproject.ui.UiMessageHandler; |
Simon Hunt | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame] | 32 | import org.onosproject.ui.table.AbstractTableRow; |
| 33 | import org.onosproject.ui.table.RowComparator; |
| 34 | import org.onosproject.ui.table.TableRow; |
| 35 | import org.onosproject.ui.table.TableUtils; |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 36 | |
| 37 | import java.util.ArrayList; |
| 38 | import java.util.Arrays; |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 39 | import java.util.Collection; |
Thomas Vachuska | b52a014 | 2015-04-21 17:48:15 -0700 | [diff] [blame] | 40 | import java.util.Collections; |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 41 | import java.util.List; |
Bri Prebilic Cole | 0feedc0 | 2015-04-09 14:17:37 -0700 | [diff] [blame] | 42 | import java.util.Set; |
Bri Prebilic Cole | 264c5ec | 2015-04-07 10:22:26 -0700 | [diff] [blame] | 43 | |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 44 | /** |
| 45 | * Message handler for device view related messages. |
| 46 | */ |
Simon Hunt | a0ddb02 | 2015-05-01 09:53:01 -0700 | [diff] [blame] | 47 | public class DeviceViewMessageHandler extends UiMessageHandler { |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 48 | |
| 49 | private static final String DEV_DATA_REQ = "deviceDataRequest"; |
| 50 | private static final String DEV_DETAIL_REQ = "deviceDetailRequest"; |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 51 | |
Bri Prebilic Cole | 0feedc0 | 2015-04-09 14:17:37 -0700 | [diff] [blame] | 52 | private static final String ID = "id"; |
| 53 | private static final String TYPE = "type"; |
| 54 | private static final String AVAILABLE = "available"; |
| 55 | private static final String AVAILABLE_IID = "_iconid_available"; |
| 56 | private static final String TYPE_IID = "_iconid_type"; |
| 57 | private static final String DEV_ICON_PREFIX = "devIcon_"; |
| 58 | private static final String NUM_PORTS = "num_ports"; |
| 59 | private static final String LINK_DEST = "elinks_dest"; |
| 60 | private static final String MFR = "mfr"; |
| 61 | private static final String HW = "hw"; |
| 62 | private static final String SW = "sw"; |
| 63 | private static final String PROTOCOL = "protocol"; |
| 64 | private static final String MASTER_ID = "masterid"; |
| 65 | private static final String CHASSIS_ID = "chassisid"; |
| 66 | private static final String SERIAL = "serial"; |
| 67 | private static final String PORTS = "ports"; |
| 68 | private static final String ENABLED = "enabled"; |
| 69 | private static final String SPEED = "speed"; |
Thomas Vachuska | b52a014 | 2015-04-21 17:48:15 -0700 | [diff] [blame] | 70 | private static final String NAME = "name"; |
Bri Prebilic Cole | 0feedc0 | 2015-04-09 14:17:37 -0700 | [diff] [blame] | 71 | |
Bri Prebilic Cole | 0feedc0 | 2015-04-09 14:17:37 -0700 | [diff] [blame] | 72 | |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 73 | @Override |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 74 | protected Collection<RequestHandler> getHandlers() { |
| 75 | return ImmutableSet.of( |
| 76 | new DataRequestHandler(), |
| 77 | new DetailRequestHandler() |
| 78 | ); |
| 79 | } |
| 80 | |
| 81 | // ====================================================================== |
| 82 | |
| 83 | private final class DataRequestHandler extends RequestHandler { |
| 84 | |
| 85 | private DataRequestHandler() { |
| 86 | super(DEV_DATA_REQ); |
Bri Prebilic Cole | 0feedc0 | 2015-04-09 14:17:37 -0700 | [diff] [blame] | 87 | } |
Bri Prebilic Cole | 0feedc0 | 2015-04-09 14:17:37 -0700 | [diff] [blame] | 88 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 89 | @Override |
| 90 | public void process(long sid, ObjectNode payload) { |
| 91 | RowComparator rc = TableUtils.createRowComparator(payload); |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 92 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 93 | DeviceService service = get(DeviceService.class); |
| 94 | MastershipService mastershipService = get(MastershipService.class); |
| 95 | TableRow[] rows = generateTableRows(service, mastershipService); |
| 96 | Arrays.sort(rows, rc); |
| 97 | ObjectNode rootNode = MAPPER.createObjectNode(); |
| 98 | rootNode.set("devices", TableUtils.generateArrayNode(rows)); |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 99 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 100 | sendMessage("deviceDataResponse", 0, rootNode); |
Bri Prebilic Cole | 0feedc0 | 2015-04-09 14:17:37 -0700 | [diff] [blame] | 101 | } |
Bri Prebilic Cole | 0feedc0 | 2015-04-09 14:17:37 -0700 | [diff] [blame] | 102 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 103 | private TableRow[] generateTableRows(DeviceService service, |
| 104 | MastershipService mastershipService) { |
| 105 | List<TableRow> list = new ArrayList<>(); |
| 106 | for (Device dev : service.getDevices()) { |
| 107 | list.add(new DeviceTableRow(service, mastershipService, dev)); |
Bri Prebilic Cole | 0feedc0 | 2015-04-09 14:17:37 -0700 | [diff] [blame] | 108 | } |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 109 | return list.toArray(new TableRow[list.size()]); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | // ====================================================================== |
| 114 | |
| 115 | private final class DetailRequestHandler extends RequestHandler { |
| 116 | private DetailRequestHandler() { |
| 117 | super(DEV_DETAIL_REQ); |
Bri Prebilic Cole | 0feedc0 | 2015-04-09 14:17:37 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 120 | @Override |
| 121 | public void process(long sid, ObjectNode payload) { |
| 122 | String id = string(payload, "id", "of:0000000000000000"); |
| 123 | |
| 124 | DeviceId deviceId = DeviceId.deviceId(id); |
| 125 | DeviceService service = get(DeviceService.class); |
| 126 | MastershipService ms = get(MastershipService.class); |
| 127 | Device device = service.getDevice(deviceId); |
| 128 | ObjectNode data = MAPPER.createObjectNode(); |
| 129 | |
| 130 | data.put(ID, deviceId.toString()); |
| 131 | data.put(TYPE, device.type().toString()); |
| 132 | data.put(TYPE_IID, getTypeIconId(device)); |
| 133 | data.put(MFR, device.manufacturer()); |
| 134 | data.put(HW, device.hwVersion()); |
| 135 | data.put(SW, device.swVersion()); |
| 136 | data.put(SERIAL, device.serialNumber()); |
| 137 | data.put(CHASSIS_ID, device.chassisId().toString()); |
| 138 | data.put(MASTER_ID, ms.getMasterFor(deviceId).toString()); |
| 139 | data.put(PROTOCOL, device.annotations().value(PROTOCOL)); |
| 140 | |
| 141 | ArrayNode ports = MAPPER.createArrayNode(); |
| 142 | |
| 143 | List<Port> portList = new ArrayList<>(service.getPorts(deviceId)); |
| 144 | Collections.sort(portList, (p1, p2) -> { |
| 145 | long delta = p1.number().toLong() - p2.number().toLong(); |
| 146 | return delta == 0 ? 0 : (delta < 0 ? -1 : +1); |
| 147 | }); |
| 148 | |
| 149 | for (Port p : portList) { |
| 150 | ports.add(portData(p, deviceId)); |
| 151 | } |
| 152 | data.set(PORTS, ports); |
| 153 | |
| 154 | ObjectNode rootNode = MAPPER.createObjectNode(); |
| 155 | rootNode.set("details", data); |
| 156 | sendMessage("deviceDetailsResponse", 0, rootNode); |
| 157 | } |
| 158 | |
| 159 | private ObjectNode portData(Port p, DeviceId id) { |
| 160 | ObjectNode port = MAPPER.createObjectNode(); |
| 161 | LinkService ls = get(LinkService.class); |
| 162 | String name = p.annotations().value(AnnotationKeys.PORT_NAME); |
| 163 | |
| 164 | port.put(ID, p.number().toString()); |
| 165 | port.put(TYPE, p.type().toString()); |
| 166 | port.put(SPEED, p.portSpeed()); |
| 167 | port.put(ENABLED, p.isEnabled()); |
| 168 | port.put(NAME, name != null ? name : ""); |
| 169 | |
| 170 | Set<Link> links = ls.getEgressLinks(new ConnectPoint(id, p.number())); |
| 171 | if (!links.isEmpty()) { |
| 172 | StringBuilder egressLinks = new StringBuilder(); |
| 173 | for (Link l : links) { |
| 174 | ConnectPoint dest = l.dst(); |
| 175 | egressLinks.append(dest.elementId()).append("/") |
| 176 | .append(dest.port()).append(" "); |
| 177 | } |
| 178 | port.put(LINK_DEST, egressLinks.toString()); |
| 179 | } |
| 180 | |
| 181 | return port; |
| 182 | } |
| 183 | |
Bri Prebilic Cole | 0feedc0 | 2015-04-09 14:17:37 -0700 | [diff] [blame] | 184 | } |
| 185 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 186 | |
Bri Prebilic Cole | b699a16 | 2015-04-13 12:01:39 -0700 | [diff] [blame] | 187 | private static String getTypeIconId(Device d) { |
| 188 | return DEV_ICON_PREFIX + d.type().toString(); |
| 189 | } |
| 190 | |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 191 | /** |
| 192 | * TableRow implementation for {@link Device devices}. |
| 193 | */ |
| 194 | private static class DeviceTableRow extends AbstractTableRow { |
| 195 | |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 196 | private static final String[] COL_IDS = { |
Bri Prebilic Cole | 264c5ec | 2015-04-07 10:22:26 -0700 | [diff] [blame] | 197 | AVAILABLE, AVAILABLE_IID, TYPE_IID, ID, |
Bri Prebilic Cole | 0feedc0 | 2015-04-09 14:17:37 -0700 | [diff] [blame] | 198 | NUM_PORTS, MASTER_ID, MFR, HW, SW, |
| 199 | PROTOCOL, CHASSIS_ID, SERIAL |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 200 | }; |
| 201 | |
Bri Prebilic Cole | ab582b8 | 2015-04-14 15:08:22 -0700 | [diff] [blame] | 202 | private static final String ICON_ID_ONLINE = "active"; |
| 203 | private static final String ICON_ID_OFFLINE = "inactive"; |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 204 | |
Bri Prebilic Cole | ae65c96 | 2015-04-02 16:24:49 -0700 | [diff] [blame] | 205 | public DeviceTableRow(DeviceService service, |
| 206 | MastershipService ms, |
| 207 | Device d) { |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 208 | boolean available = service.isAvailable(d.id()); |
| 209 | String iconId = available ? ICON_ID_ONLINE : ICON_ID_OFFLINE; |
Bri Prebilic Cole | 264c5ec | 2015-04-07 10:22:26 -0700 | [diff] [blame] | 210 | DeviceId id = d.id(); |
| 211 | List<Port> ports = service.getPorts(id); |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 212 | |
Bri Prebilic Cole | 264c5ec | 2015-04-07 10:22:26 -0700 | [diff] [blame] | 213 | add(ID, id.toString()); |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 214 | add(AVAILABLE, Boolean.toString(available)); |
| 215 | add(AVAILABLE_IID, iconId); |
| 216 | add(TYPE_IID, getTypeIconId(d)); |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 217 | add(MFR, d.manufacturer()); |
| 218 | add(HW, d.hwVersion()); |
| 219 | add(SW, d.swVersion()); |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 220 | add(PROTOCOL, d.annotations().value(PROTOCOL)); |
Bri Prebilic Cole | 264c5ec | 2015-04-07 10:22:26 -0700 | [diff] [blame] | 221 | add(NUM_PORTS, Integer.toString(ports.size())); |
Bri Prebilic Cole | 0feedc0 | 2015-04-09 14:17:37 -0700 | [diff] [blame] | 222 | add(MASTER_ID, ms.getMasterFor(d.id()).toString()); |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 223 | } |
| 224 | |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 225 | @Override |
| 226 | protected String[] columnIds() { |
| 227 | return COL_IDS; |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | } |