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