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 | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 32 | import org.onosproject.ui.table.TableModel; |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 33 | import org.onosproject.ui.table.TableRequestHandler; |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 34 | import org.onosproject.ui.table.cell.IntComparator; |
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 | |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 76 | private static final String[] COL_IDS = { |
| 77 | AVAILABLE, AVAILABLE_IID, TYPE_IID, ID, |
| 78 | NUM_PORTS, MASTER_ID, MFR, HW, SW, |
| 79 | PROTOCOL, CHASSIS_ID, SERIAL |
| 80 | }; |
| 81 | |
| 82 | private static final String ICON_ID_ONLINE = "active"; |
| 83 | private static final String ICON_ID_OFFLINE = "inactive"; |
| 84 | |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 85 | @Override |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 86 | protected Collection<RequestHandler> getHandlers() { |
| 87 | return ImmutableSet.of( |
| 88 | new DataRequestHandler(), |
| 89 | new DetailRequestHandler() |
| 90 | ); |
| 91 | } |
| 92 | |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 93 | private static String getTypeIconId(Device d) { |
| 94 | return DEV_ICON_PREFIX + d.type().toString(); |
| 95 | } |
| 96 | |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 97 | // handler for device table requests |
| 98 | private final class DataRequestHandler extends TableRequestHandler { |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 99 | private DataRequestHandler() { |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 100 | super(DEV_DATA_REQ, DEV_DATA_RESP, DEVICES); |
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 | @Override |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 104 | protected String[] getColumnIds() { |
| 105 | return COL_IDS; |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | protected TableModel createTableModel() { |
| 110 | TableModel tm = super.createTableModel(); |
| 111 | tm.setComparator(NUM_PORTS, IntComparator.INSTANCE); |
| 112 | return tm; |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | protected void populateTable(TableModel tm, ObjectNode payload) { |
| 117 | DeviceService ds = get(DeviceService.class); |
| 118 | MastershipService ms = get(MastershipService.class); |
| 119 | for (Device dev : ds.getDevices()) { |
| 120 | populateRow(tm.addRow(), dev, ds, ms); |
Bri Prebilic Cole | 0feedc0 | 2015-04-09 14:17:37 -0700 | [diff] [blame] | 121 | } |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | private void populateRow(TableModel.Row row, Device dev, |
| 125 | DeviceService ds, MastershipService ms) { |
| 126 | DeviceId id = dev.id(); |
| 127 | boolean available = ds.isAvailable(id); |
| 128 | String iconId = available ? ICON_ID_ONLINE : ICON_ID_OFFLINE; |
| 129 | |
| 130 | row.cell(ID, id) |
| 131 | .cell(AVAILABLE, available) |
| 132 | .cell(AVAILABLE_IID, iconId) |
| 133 | .cell(TYPE_IID, getTypeIconId(dev)) |
| 134 | .cell(MFR, dev.manufacturer()) |
| 135 | .cell(HW, dev.hwVersion()) |
| 136 | .cell(SW, dev.swVersion()) |
| 137 | .cell(PROTOCOL, dev.annotations().value(PROTOCOL)) |
| 138 | .cell(NUM_PORTS, ds.getPorts(id).size()) |
| 139 | .cell(MASTER_ID, ms.getMasterFor(id)); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 143 | // handler for selected device detail requests |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 144 | private final class DetailRequestHandler extends RequestHandler { |
| 145 | private DetailRequestHandler() { |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 146 | super(DEV_DETAILS_REQ); |
Bri Prebilic Cole | 0feedc0 | 2015-04-09 14:17:37 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 149 | @Override |
| 150 | public void process(long sid, ObjectNode payload) { |
| 151 | String id = string(payload, "id", "of:0000000000000000"); |
| 152 | |
| 153 | DeviceId deviceId = DeviceId.deviceId(id); |
| 154 | DeviceService service = get(DeviceService.class); |
| 155 | MastershipService ms = get(MastershipService.class); |
| 156 | Device device = service.getDevice(deviceId); |
| 157 | ObjectNode data = MAPPER.createObjectNode(); |
| 158 | |
| 159 | data.put(ID, deviceId.toString()); |
| 160 | data.put(TYPE, device.type().toString()); |
| 161 | data.put(TYPE_IID, getTypeIconId(device)); |
| 162 | data.put(MFR, device.manufacturer()); |
| 163 | data.put(HW, device.hwVersion()); |
| 164 | data.put(SW, device.swVersion()); |
| 165 | data.put(SERIAL, device.serialNumber()); |
| 166 | data.put(CHASSIS_ID, device.chassisId().toString()); |
| 167 | data.put(MASTER_ID, ms.getMasterFor(deviceId).toString()); |
| 168 | data.put(PROTOCOL, device.annotations().value(PROTOCOL)); |
| 169 | |
| 170 | ArrayNode ports = MAPPER.createArrayNode(); |
| 171 | |
| 172 | List<Port> portList = new ArrayList<>(service.getPorts(deviceId)); |
| 173 | Collections.sort(portList, (p1, p2) -> { |
| 174 | long delta = p1.number().toLong() - p2.number().toLong(); |
| 175 | return delta == 0 ? 0 : (delta < 0 ? -1 : +1); |
| 176 | }); |
| 177 | |
| 178 | for (Port p : portList) { |
| 179 | ports.add(portData(p, deviceId)); |
| 180 | } |
| 181 | data.set(PORTS, ports); |
| 182 | |
| 183 | ObjectNode rootNode = MAPPER.createObjectNode(); |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 184 | rootNode.set(DETAILS, data); |
| 185 | sendMessage(DEV_DETAILS_RESP, 0, rootNode); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | private ObjectNode portData(Port p, DeviceId id) { |
| 189 | ObjectNode port = MAPPER.createObjectNode(); |
| 190 | LinkService ls = get(LinkService.class); |
| 191 | String name = p.annotations().value(AnnotationKeys.PORT_NAME); |
| 192 | |
| 193 | port.put(ID, p.number().toString()); |
| 194 | port.put(TYPE, p.type().toString()); |
| 195 | port.put(SPEED, p.portSpeed()); |
| 196 | port.put(ENABLED, p.isEnabled()); |
| 197 | port.put(NAME, name != null ? name : ""); |
| 198 | |
| 199 | Set<Link> links = ls.getEgressLinks(new ConnectPoint(id, p.number())); |
| 200 | if (!links.isEmpty()) { |
| 201 | StringBuilder egressLinks = new StringBuilder(); |
| 202 | for (Link l : links) { |
| 203 | ConnectPoint dest = l.dst(); |
| 204 | egressLinks.append(dest.elementId()).append("/") |
| 205 | .append(dest.port()).append(" "); |
| 206 | } |
| 207 | port.put(LINK_DEST, egressLinks.toString()); |
| 208 | } |
| 209 | |
| 210 | return port; |
| 211 | } |
Bri Prebilic Cole | 0feedc0 | 2015-04-09 14:17:37 -0700 | [diff] [blame] | 212 | } |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 213 | } |