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