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