Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 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; |
Thomas Vachuska | c4178cc | 2015-12-10 11:43:32 -0800 | [diff] [blame] | 21 | import org.onosproject.cluster.NodeId; |
Bri Prebilic Cole | ae65c96 | 2015-04-02 16:24:49 -0700 | [diff] [blame] | 22 | import org.onosproject.mastership.MastershipService; |
Thomas Vachuska | b52a014 | 2015-04-21 17:48:15 -0700 | [diff] [blame] | 23 | import org.onosproject.net.AnnotationKeys; |
Bri Prebilic Cole | 0feedc0 | 2015-04-09 14:17:37 -0700 | [diff] [blame] | 24 | import org.onosproject.net.ConnectPoint; |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 25 | import org.onosproject.net.Device; |
Bri Prebilic Cole | 264c5ec | 2015-04-07 10:22:26 -0700 | [diff] [blame] | 26 | import org.onosproject.net.DeviceId; |
Bri Prebilic Cole | 0feedc0 | 2015-04-09 14:17:37 -0700 | [diff] [blame] | 27 | import org.onosproject.net.Link; |
Bri Prebilic Cole | 264c5ec | 2015-04-07 10:22:26 -0700 | [diff] [blame] | 28 | import org.onosproject.net.Port; |
Thomas Vachuska | 8f62f57 | 2015-10-06 12:17:44 -0700 | [diff] [blame] | 29 | import org.onosproject.net.config.NetworkConfigService; |
| 30 | import org.onosproject.net.config.basics.BasicDeviceConfig; |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 31 | import org.onosproject.net.device.DeviceService; |
Bri Prebilic Cole | 264c5ec | 2015-04-07 10:22:26 -0700 | [diff] [blame] | 32 | import org.onosproject.net.link.LinkService; |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 33 | import org.onosproject.ui.RequestHandler; |
Simon Hunt | a0ddb02 | 2015-05-01 09:53:01 -0700 | [diff] [blame] | 34 | import org.onosproject.ui.UiMessageHandler; |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 35 | import org.onosproject.ui.table.TableModel; |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 36 | import org.onosproject.ui.table.TableRequestHandler; |
Simon Hunt | d557925 | 2015-10-06 15:09:14 -0700 | [diff] [blame] | 37 | import org.slf4j.Logger; |
| 38 | import org.slf4j.LoggerFactory; |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 39 | |
| 40 | import java.util.ArrayList; |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 41 | import java.util.Collection; |
Thomas Vachuska | b52a014 | 2015-04-21 17:48:15 -0700 | [diff] [blame] | 42 | import java.util.Collections; |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 43 | import java.util.List; |
Bri Prebilic Cole | 0feedc0 | 2015-04-09 14:17:37 -0700 | [diff] [blame] | 44 | import java.util.Set; |
Bri Prebilic Cole | 264c5ec | 2015-04-07 10:22:26 -0700 | [diff] [blame] | 45 | |
Thomas Vachuska | 8f62f57 | 2015-10-06 12:17:44 -0700 | [diff] [blame] | 46 | import static com.google.common.base.Strings.emptyToNull; |
| 47 | import static com.google.common.base.Strings.isNullOrEmpty; |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 48 | import static org.apache.commons.lang.WordUtils.capitalizeFully; |
Thomas Vachuska | 8f62f57 | 2015-10-06 12:17:44 -0700 | [diff] [blame] | 49 | import static org.onosproject.net.DeviceId.deviceId; |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 50 | |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 51 | /** |
| 52 | * Message handler for device view related messages. |
| 53 | */ |
Simon Hunt | a0ddb02 | 2015-05-01 09:53:01 -0700 | [diff] [blame] | 54 | public class DeviceViewMessageHandler extends UiMessageHandler { |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 55 | |
| 56 | private static final String DEV_DATA_REQ = "deviceDataRequest"; |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 57 | private static final String DEV_DATA_RESP = "deviceDataResponse"; |
| 58 | private static final String DEVICES = "devices"; |
| 59 | |
| 60 | private static final String DEV_DETAILS_REQ = "deviceDetailsRequest"; |
| 61 | private static final String DEV_DETAILS_RESP = "deviceDetailsResponse"; |
| 62 | private static final String DETAILS = "details"; |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 63 | |
Thomas Vachuska | 8f62f57 | 2015-10-06 12:17:44 -0700 | [diff] [blame] | 64 | private static final String DEV_NAME_CHANGE_REQ = "deviceNameChangeRequest"; |
| 65 | private static final String DEV_NAME_CHANGE_RESP = "deviceNameChangeResponse"; |
| 66 | |
Simon Hunt | d557925 | 2015-10-06 15:09:14 -0700 | [diff] [blame] | 67 | private static final String ZERO_URI = "of:0000000000000000"; |
| 68 | |
Bri Prebilic Cole | 0feedc0 | 2015-04-09 14:17:37 -0700 | [diff] [blame] | 69 | private static final String ID = "id"; |
| 70 | private static final String TYPE = "type"; |
| 71 | private static final String AVAILABLE = "available"; |
| 72 | private static final String AVAILABLE_IID = "_iconid_available"; |
| 73 | private static final String TYPE_IID = "_iconid_type"; |
| 74 | private static final String DEV_ICON_PREFIX = "devIcon_"; |
| 75 | private static final String NUM_PORTS = "num_ports"; |
| 76 | private static final String LINK_DEST = "elinks_dest"; |
| 77 | private static final String MFR = "mfr"; |
| 78 | private static final String HW = "hw"; |
| 79 | private static final String SW = "sw"; |
| 80 | private static final String PROTOCOL = "protocol"; |
| 81 | private static final String MASTER_ID = "masterid"; |
| 82 | private static final String CHASSIS_ID = "chassisid"; |
| 83 | private static final String SERIAL = "serial"; |
| 84 | private static final String PORTS = "ports"; |
| 85 | private static final String ENABLED = "enabled"; |
| 86 | private static final String SPEED = "speed"; |
Thomas Vachuska | b52a014 | 2015-04-21 17:48:15 -0700 | [diff] [blame] | 87 | private static final String NAME = "name"; |
Simon Hunt | d557925 | 2015-10-06 15:09:14 -0700 | [diff] [blame] | 88 | private static final String WARN = "warn"; |
Bri Prebilic Cole | 0feedc0 | 2015-04-09 14:17:37 -0700 | [diff] [blame] | 89 | |
Thomas Vachuska | c4178cc | 2015-12-10 11:43:32 -0800 | [diff] [blame] | 90 | private static final String NONE = "none"; |
Bri Prebilic Cole | 0feedc0 | 2015-04-09 14:17:37 -0700 | [diff] [blame] | 91 | |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 92 | private static final String[] COL_IDS = { |
Simon Hunt | d557925 | 2015-10-06 15:09:14 -0700 | [diff] [blame] | 93 | AVAILABLE, AVAILABLE_IID, TYPE_IID, |
| 94 | NAME, ID, MASTER_ID, NUM_PORTS, MFR, HW, SW, |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 95 | PROTOCOL, CHASSIS_ID, SERIAL |
| 96 | }; |
| 97 | |
| 98 | private static final String ICON_ID_ONLINE = "active"; |
| 99 | private static final String ICON_ID_OFFLINE = "inactive"; |
| 100 | |
Simon Hunt | d557925 | 2015-10-06 15:09:14 -0700 | [diff] [blame] | 101 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 102 | |
| 103 | |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 104 | @Override |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 105 | protected Collection<RequestHandler> createRequestHandlers() { |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 106 | return ImmutableSet.of( |
| 107 | new DataRequestHandler(), |
Thomas Vachuska | 8f62f57 | 2015-10-06 12:17:44 -0700 | [diff] [blame] | 108 | new NameChangeHandler(), |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 109 | new DetailRequestHandler() |
| 110 | ); |
| 111 | } |
| 112 | |
Simon Hunt | d557925 | 2015-10-06 15:09:14 -0700 | [diff] [blame] | 113 | // Get friendly name of the device from the annotations |
| 114 | private static String deviceName(Device device) { |
| 115 | String name = device.annotations().value(AnnotationKeys.NAME); |
| 116 | return isNullOrEmpty(name) ? device.id().toString() : name; |
| 117 | } |
| 118 | |
| 119 | private static String deviceProtocol(Device device) { |
| 120 | String protocol = device.annotations().value(PROTOCOL); |
| 121 | return protocol != null ? protocol : ""; |
| 122 | } |
| 123 | |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 124 | private static String getTypeIconId(Device d) { |
| 125 | return DEV_ICON_PREFIX + d.type().toString(); |
| 126 | } |
| 127 | |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 128 | // handler for device table requests |
| 129 | private final class DataRequestHandler extends TableRequestHandler { |
Jian Li | 69f6663 | 2016-01-15 12:27:42 -0800 | [diff] [blame] | 130 | private static final String NO_ROWS_MESSAGE = "No devices found"; |
| 131 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 132 | private DataRequestHandler() { |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 133 | super(DEV_DATA_REQ, DEV_DATA_RESP, DEVICES); |
Bri Prebilic Cole | 0feedc0 | 2015-04-09 14:17:37 -0700 | [diff] [blame] | 134 | } |
Bri Prebilic Cole | 0feedc0 | 2015-04-09 14:17:37 -0700 | [diff] [blame] | 135 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 136 | @Override |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 137 | protected String[] getColumnIds() { |
| 138 | return COL_IDS; |
| 139 | } |
| 140 | |
| 141 | @Override |
Jian Li | 8baf447 | 2016-01-15 15:08:09 -0800 | [diff] [blame] | 142 | protected String noRowsMessage(ObjectNode payload) { |
Jian Li | 69f6663 | 2016-01-15 12:27:42 -0800 | [diff] [blame] | 143 | return NO_ROWS_MESSAGE; |
| 144 | } |
| 145 | |
| 146 | @Override |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 147 | protected void populateTable(TableModel tm, ObjectNode payload) { |
| 148 | DeviceService ds = get(DeviceService.class); |
| 149 | MastershipService ms = get(MastershipService.class); |
| 150 | for (Device dev : ds.getDevices()) { |
| 151 | populateRow(tm.addRow(), dev, ds, ms); |
Bri Prebilic Cole | 0feedc0 | 2015-04-09 14:17:37 -0700 | [diff] [blame] | 152 | } |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | private void populateRow(TableModel.Row row, Device dev, |
| 156 | DeviceService ds, MastershipService ms) { |
| 157 | DeviceId id = dev.id(); |
| 158 | boolean available = ds.isAvailable(id); |
| 159 | String iconId = available ? ICON_ID_ONLINE : ICON_ID_OFFLINE; |
| 160 | |
| 161 | row.cell(ID, id) |
Simon Hunt | d557925 | 2015-10-06 15:09:14 -0700 | [diff] [blame] | 162 | .cell(NAME, deviceName(dev)) |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 163 | .cell(AVAILABLE, available) |
| 164 | .cell(AVAILABLE_IID, iconId) |
| 165 | .cell(TYPE_IID, getTypeIconId(dev)) |
| 166 | .cell(MFR, dev.manufacturer()) |
| 167 | .cell(HW, dev.hwVersion()) |
| 168 | .cell(SW, dev.swVersion()) |
Simon Hunt | d557925 | 2015-10-06 15:09:14 -0700 | [diff] [blame] | 169 | .cell(PROTOCOL, deviceProtocol(dev)) |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 170 | .cell(NUM_PORTS, ds.getPorts(id).size()) |
| 171 | .cell(MASTER_ID, ms.getMasterFor(id)); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 175 | // handler for selected device detail requests |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 176 | private final class DetailRequestHandler extends RequestHandler { |
| 177 | private DetailRequestHandler() { |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 178 | super(DEV_DETAILS_REQ); |
Bri Prebilic Cole | 0feedc0 | 2015-04-09 14:17:37 -0700 | [diff] [blame] | 179 | } |
| 180 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 181 | @Override |
| 182 | public void process(long sid, ObjectNode payload) { |
Simon Hunt | d557925 | 2015-10-06 15:09:14 -0700 | [diff] [blame] | 183 | String id = string(payload, ID, ZERO_URI); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 184 | |
Thomas Vachuska | 8f62f57 | 2015-10-06 12:17:44 -0700 | [diff] [blame] | 185 | DeviceId deviceId = deviceId(id); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 186 | DeviceService service = get(DeviceService.class); |
| 187 | MastershipService ms = get(MastershipService.class); |
| 188 | Device device = service.getDevice(deviceId); |
Simon Hunt | d557925 | 2015-10-06 15:09:14 -0700 | [diff] [blame] | 189 | ObjectNode data = objectNode(); |
Thomas Vachuska | c4178cc | 2015-12-10 11:43:32 -0800 | [diff] [blame] | 190 | NodeId masterFor = ms.getMasterFor(deviceId); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 191 | |
| 192 | data.put(ID, deviceId.toString()); |
Simon Hunt | d557925 | 2015-10-06 15:09:14 -0700 | [diff] [blame] | 193 | data.put(NAME, deviceName(device)); |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 194 | data.put(TYPE, capitalizeFully(device.type().toString())); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 195 | data.put(TYPE_IID, getTypeIconId(device)); |
| 196 | data.put(MFR, device.manufacturer()); |
| 197 | data.put(HW, device.hwVersion()); |
| 198 | data.put(SW, device.swVersion()); |
| 199 | data.put(SERIAL, device.serialNumber()); |
| 200 | data.put(CHASSIS_ID, device.chassisId().toString()); |
Thomas Vachuska | c4178cc | 2015-12-10 11:43:32 -0800 | [diff] [blame] | 201 | data.put(MASTER_ID, masterFor != null ? masterFor.toString() : NONE); |
Simon Hunt | d557925 | 2015-10-06 15:09:14 -0700 | [diff] [blame] | 202 | data.put(PROTOCOL, deviceProtocol(device)); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 203 | |
Simon Hunt | d557925 | 2015-10-06 15:09:14 -0700 | [diff] [blame] | 204 | ArrayNode ports = arrayNode(); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 205 | |
| 206 | List<Port> portList = new ArrayList<>(service.getPorts(deviceId)); |
| 207 | Collections.sort(portList, (p1, p2) -> { |
| 208 | long delta = p1.number().toLong() - p2.number().toLong(); |
| 209 | return delta == 0 ? 0 : (delta < 0 ? -1 : +1); |
| 210 | }); |
| 211 | |
| 212 | for (Port p : portList) { |
| 213 | ports.add(portData(p, deviceId)); |
| 214 | } |
| 215 | data.set(PORTS, ports); |
| 216 | |
Simon Hunt | d557925 | 2015-10-06 15:09:14 -0700 | [diff] [blame] | 217 | ObjectNode rootNode = objectNode(); |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 218 | rootNode.set(DETAILS, data); |
| 219 | sendMessage(DEV_DETAILS_RESP, 0, rootNode); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | private ObjectNode portData(Port p, DeviceId id) { |
Simon Hunt | d557925 | 2015-10-06 15:09:14 -0700 | [diff] [blame] | 223 | ObjectNode port = objectNode(); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 224 | LinkService ls = get(LinkService.class); |
| 225 | String name = p.annotations().value(AnnotationKeys.PORT_NAME); |
| 226 | |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 227 | port.put(ID, capitalizeFully(p.number().toString())); |
| 228 | port.put(TYPE, capitalizeFully(p.type().toString())); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 229 | port.put(SPEED, p.portSpeed()); |
| 230 | port.put(ENABLED, p.isEnabled()); |
| 231 | port.put(NAME, name != null ? name : ""); |
| 232 | |
| 233 | Set<Link> links = ls.getEgressLinks(new ConnectPoint(id, p.number())); |
| 234 | if (!links.isEmpty()) { |
| 235 | StringBuilder egressLinks = new StringBuilder(); |
| 236 | for (Link l : links) { |
| 237 | ConnectPoint dest = l.dst(); |
| 238 | egressLinks.append(dest.elementId()).append("/") |
| 239 | .append(dest.port()).append(" "); |
| 240 | } |
| 241 | port.put(LINK_DEST, egressLinks.toString()); |
| 242 | } |
| 243 | |
| 244 | return port; |
| 245 | } |
Bri Prebilic Cole | 0feedc0 | 2015-04-09 14:17:37 -0700 | [diff] [blame] | 246 | } |
Thomas Vachuska | 8f62f57 | 2015-10-06 12:17:44 -0700 | [diff] [blame] | 247 | |
Simon Hunt | d557925 | 2015-10-06 15:09:14 -0700 | [diff] [blame] | 248 | |
Thomas Vachuska | 8f62f57 | 2015-10-06 12:17:44 -0700 | [diff] [blame] | 249 | // handler for changing device friendly name |
| 250 | private final class NameChangeHandler extends RequestHandler { |
| 251 | private NameChangeHandler() { |
| 252 | super(DEV_NAME_CHANGE_REQ); |
| 253 | } |
| 254 | |
| 255 | @Override |
| 256 | public void process(long sid, ObjectNode payload) { |
Simon Hunt | d557925 | 2015-10-06 15:09:14 -0700 | [diff] [blame] | 257 | DeviceId deviceId = deviceId(string(payload, ID, ZERO_URI)); |
| 258 | String name = emptyToNull(string(payload, NAME, null)); |
| 259 | log.debug("Name change request: {} -- '{}'", deviceId, name); |
Thomas Vachuska | 8f62f57 | 2015-10-06 12:17:44 -0700 | [diff] [blame] | 260 | |
Simon Hunt | d557925 | 2015-10-06 15:09:14 -0700 | [diff] [blame] | 261 | NetworkConfigService service = get(NetworkConfigService.class); |
| 262 | BasicDeviceConfig cfg = |
| 263 | service.addConfig(deviceId, BasicDeviceConfig.class); |
| 264 | |
| 265 | // Name attribute missing from the payload (or empty string) |
| 266 | // means that the friendly name should be unset. |
| 267 | cfg.name(name); |
Thomas Vachuska | 8f62f57 | 2015-10-06 12:17:44 -0700 | [diff] [blame] | 268 | cfg.apply(); |
| 269 | sendMessage(DEV_NAME_CHANGE_RESP, 0, payload); |
| 270 | } |
| 271 | } |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 272 | } |