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