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 | |
Bri Prebilic Cole | 0feedc0 | 2015-04-09 14:17:37 -0700 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.ObjectMapper; |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 21 | import com.google.common.collect.ImmutableSet; |
Bri Prebilic Cole | ae65c96 | 2015-04-02 16:24:49 -0700 | [diff] [blame] | 22 | import org.onosproject.mastership.MastershipService; |
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; |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 30 | |
| 31 | import java.util.ArrayList; |
| 32 | import java.util.Arrays; |
| 33 | import java.util.List; |
Bri Prebilic Cole | 0feedc0 | 2015-04-09 14:17:37 -0700 | [diff] [blame] | 34 | import java.util.Set; |
Bri Prebilic Cole | 264c5ec | 2015-04-07 10:22:26 -0700 | [diff] [blame] | 35 | |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 36 | /** |
| 37 | * Message handler for device view related messages. |
| 38 | */ |
| 39 | public class DeviceViewMessageHandler extends AbstractTabularViewMessageHandler { |
| 40 | |
Bri Prebilic Cole | 0feedc0 | 2015-04-09 14:17:37 -0700 | [diff] [blame] | 41 | private static final String ID = "id"; |
| 42 | private static final String TYPE = "type"; |
| 43 | private static final String AVAILABLE = "available"; |
| 44 | private static final String AVAILABLE_IID = "_iconid_available"; |
| 45 | private static final String TYPE_IID = "_iconid_type"; |
| 46 | private static final String DEV_ICON_PREFIX = "devIcon_"; |
| 47 | private static final String NUM_PORTS = "num_ports"; |
| 48 | private static final String LINK_DEST = "elinks_dest"; |
| 49 | private static final String MFR = "mfr"; |
| 50 | private static final String HW = "hw"; |
| 51 | private static final String SW = "sw"; |
| 52 | private static final String PROTOCOL = "protocol"; |
| 53 | private static final String MASTER_ID = "masterid"; |
| 54 | private static final String CHASSIS_ID = "chassisid"; |
| 55 | private static final String SERIAL = "serial"; |
| 56 | private static final String PORTS = "ports"; |
| 57 | private static final String ENABLED = "enabled"; |
| 58 | private static final String SPEED = "speed"; |
| 59 | |
| 60 | private static final ObjectMapper MAPPER = new ObjectMapper(); |
| 61 | |
| 62 | |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 63 | /** |
| 64 | * Creates a new message handler for the device messages. |
| 65 | */ |
| 66 | protected DeviceViewMessageHandler() { |
Bri Prebilic Cole | 0feedc0 | 2015-04-09 14:17:37 -0700 | [diff] [blame] | 67 | super(ImmutableSet.of("deviceDataRequest", "deviceDetailsRequest")); |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | @Override |
Bri Prebilic Cole | 0feedc0 | 2015-04-09 14:17:37 -0700 | [diff] [blame] | 71 | public void process(ObjectNode event) { |
| 72 | String type = string(event, "event", "unknown"); |
| 73 | if (type.equals("deviceDataRequest")) { |
| 74 | dataRequest(event); |
| 75 | } else if (type.equals("deviceDetailsRequest")) { |
| 76 | detailsRequest(event); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | private void dataRequest(ObjectNode event) { |
| 81 | ObjectNode payload = payload(event); |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 82 | String sortCol = string(payload, "sortCol", "id"); |
| 83 | String sortDir = string(payload, "sortDir", "asc"); |
| 84 | |
| 85 | DeviceService service = get(DeviceService.class); |
Bri Prebilic Cole | ae65c96 | 2015-04-02 16:24:49 -0700 | [diff] [blame] | 86 | MastershipService mastershipService = get(MastershipService.class); |
Bri Prebilic Cole | 264c5ec | 2015-04-07 10:22:26 -0700 | [diff] [blame] | 87 | LinkService linkService = get(LinkService.class); |
Bri Prebilic Cole | 0feedc0 | 2015-04-09 14:17:37 -0700 | [diff] [blame] | 88 | |
Bri Prebilic Cole | 264c5ec | 2015-04-07 10:22:26 -0700 | [diff] [blame] | 89 | TableRow[] rows = generateTableRows(service, |
| 90 | mastershipService, |
| 91 | linkService); |
Bri Prebilic Cole | ae65c96 | 2015-04-02 16:24:49 -0700 | [diff] [blame] | 92 | RowComparator rc = |
| 93 | new RowComparator(sortCol, RowComparator.direction(sortDir)); |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 94 | Arrays.sort(rows, rc); |
| 95 | ArrayNode devices = generateArrayNode(rows); |
| 96 | ObjectNode rootNode = mapper.createObjectNode(); |
| 97 | rootNode.set("devices", devices); |
| 98 | |
| 99 | connection().sendMessage("deviceDataResponse", 0, rootNode); |
| 100 | } |
| 101 | |
Bri Prebilic Cole | 0feedc0 | 2015-04-09 14:17:37 -0700 | [diff] [blame] | 102 | private void detailsRequest(ObjectNode event) { |
| 103 | ObjectNode payload = payload(event); |
| 104 | String id = string(payload, "id", "of:0000000000000000"); |
| 105 | |
| 106 | DeviceId deviceId = DeviceId.deviceId(id); |
| 107 | DeviceService service = get(DeviceService.class); |
| 108 | MastershipService ms = get(MastershipService.class); |
| 109 | Device device = service.getDevice(deviceId); |
| 110 | ObjectNode data = MAPPER.createObjectNode(); |
| 111 | |
| 112 | data.put(ID, deviceId.toString()); |
| 113 | data.put(TYPE, device.type().toString()); |
| 114 | data.put(MFR, device.manufacturer()); |
| 115 | data.put(HW, device.hwVersion()); |
| 116 | data.put(SW, device.swVersion()); |
| 117 | data.put(SERIAL, device.serialNumber()); |
| 118 | data.put(CHASSIS_ID, device.chassisId().toString()); |
| 119 | data.put(MASTER_ID, ms.getMasterFor(deviceId).toString()); |
| 120 | data.put(PROTOCOL, device.annotations().value(PROTOCOL)); |
| 121 | |
| 122 | ArrayNode ports = MAPPER.createArrayNode(); |
| 123 | for (Port p : service.getPorts(deviceId)) { |
| 124 | ports.add(portData(p, deviceId)); |
| 125 | } |
| 126 | data.set(PORTS, ports); |
| 127 | |
| 128 | ObjectNode rootNode = mapper.createObjectNode(); |
| 129 | rootNode.set("details", data); |
| 130 | connection().sendMessage("deviceDetailsResponse", 0, rootNode); |
| 131 | } |
| 132 | |
Bri Prebilic Cole | ae65c96 | 2015-04-02 16:24:49 -0700 | [diff] [blame] | 133 | private TableRow[] generateTableRows(DeviceService service, |
Bri Prebilic Cole | 264c5ec | 2015-04-07 10:22:26 -0700 | [diff] [blame] | 134 | MastershipService mastershipService, |
| 135 | LinkService linkService) { |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 136 | List<TableRow> list = new ArrayList<>(); |
| 137 | for (Device dev : service.getDevices()) { |
Bri Prebilic Cole | 264c5ec | 2015-04-07 10:22:26 -0700 | [diff] [blame] | 138 | list.add(new DeviceTableRow(service, |
| 139 | mastershipService, |
| 140 | linkService, |
| 141 | dev)); |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 142 | } |
| 143 | return list.toArray(new TableRow[list.size()]); |
| 144 | } |
| 145 | |
Bri Prebilic Cole | 0feedc0 | 2015-04-09 14:17:37 -0700 | [diff] [blame] | 146 | private ObjectNode portData(Port p, DeviceId id) { |
| 147 | ObjectNode port = MAPPER.createObjectNode(); |
| 148 | LinkService ls = get(LinkService.class); |
| 149 | |
| 150 | port.put(ID, p.number().toString()); |
| 151 | port.put(TYPE, p.type().toString()); |
| 152 | port.put(SPEED, p.portSpeed()); |
| 153 | port.put(ENABLED, p.isEnabled()); |
| 154 | |
| 155 | Set<Link> links = ls.getEgressLinks(new ConnectPoint(id, p.number())); |
| 156 | if (!links.isEmpty()) { |
| 157 | String egressLinks = ""; |
| 158 | for (Link l : links) { |
| 159 | ConnectPoint dest = l.dst(); |
| 160 | egressLinks += dest.elementId().toString() |
| 161 | + "/" + dest.port().toString() + " "; |
| 162 | } |
| 163 | port.put(LINK_DEST, egressLinks); |
| 164 | } |
| 165 | |
| 166 | return port; |
| 167 | } |
| 168 | |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 169 | /** |
| 170 | * TableRow implementation for {@link Device devices}. |
| 171 | */ |
| 172 | private static class DeviceTableRow extends AbstractTableRow { |
| 173 | |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 174 | private static final String[] COL_IDS = { |
Bri Prebilic Cole | 264c5ec | 2015-04-07 10:22:26 -0700 | [diff] [blame] | 175 | AVAILABLE, AVAILABLE_IID, TYPE_IID, ID, |
Bri Prebilic Cole | 0feedc0 | 2015-04-09 14:17:37 -0700 | [diff] [blame] | 176 | NUM_PORTS, MASTER_ID, MFR, HW, SW, |
| 177 | PROTOCOL, CHASSIS_ID, SERIAL |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 178 | }; |
| 179 | |
| 180 | private static final String ICON_ID_ONLINE = "deviceOnline"; |
| 181 | private static final String ICON_ID_OFFLINE = "deviceOffline"; |
| 182 | |
Bri Prebilic Cole | 264c5ec | 2015-04-07 10:22:26 -0700 | [diff] [blame] | 183 | // TODO: use in details pane |
Bri Prebilic Cole | 264c5ec | 2015-04-07 10:22:26 -0700 | [diff] [blame] | 184 | // private String getEgressLinks(Set<Link> links) { |
| 185 | // String formattedString = ""; |
| 186 | // |
| 187 | // for (Link l : links) { |
| 188 | // formattedString += l.dst().port().toString() + ", "; |
| 189 | // } |
| 190 | // return formattedString; |
| 191 | // } |
| 192 | |
| 193 | // TODO: include "extra" backend information in device details pane |
Bri Prebilic Cole | ae65c96 | 2015-04-02 16:24:49 -0700 | [diff] [blame] | 194 | public DeviceTableRow(DeviceService service, |
| 195 | MastershipService ms, |
Bri Prebilic Cole | 264c5ec | 2015-04-07 10:22:26 -0700 | [diff] [blame] | 196 | LinkService ls, |
Bri Prebilic Cole | ae65c96 | 2015-04-02 16:24:49 -0700 | [diff] [blame] | 197 | Device d) { |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 198 | boolean available = service.isAvailable(d.id()); |
| 199 | String iconId = available ? ICON_ID_ONLINE : ICON_ID_OFFLINE; |
Bri Prebilic Cole | 264c5ec | 2015-04-07 10:22:26 -0700 | [diff] [blame] | 200 | DeviceId id = d.id(); |
| 201 | List<Port> ports = service.getPorts(id); |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 202 | |
Bri Prebilic Cole | 264c5ec | 2015-04-07 10:22:26 -0700 | [diff] [blame] | 203 | add(ID, id.toString()); |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 204 | add(AVAILABLE, Boolean.toString(available)); |
| 205 | add(AVAILABLE_IID, iconId); |
| 206 | add(TYPE_IID, getTypeIconId(d)); |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 207 | add(MFR, d.manufacturer()); |
| 208 | add(HW, d.hwVersion()); |
| 209 | add(SW, d.swVersion()); |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 210 | add(PROTOCOL, d.annotations().value(PROTOCOL)); |
Bri Prebilic Cole | 264c5ec | 2015-04-07 10:22:26 -0700 | [diff] [blame] | 211 | add(NUM_PORTS, Integer.toString(ports.size())); |
Bri Prebilic Cole | 0feedc0 | 2015-04-09 14:17:37 -0700 | [diff] [blame] | 212 | add(MASTER_ID, ms.getMasterFor(d.id()).toString()); |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | private String getTypeIconId(Device d) { |
| 216 | return DEV_ICON_PREFIX + d.type().toString(); |
| 217 | } |
| 218 | |
| 219 | @Override |
| 220 | protected String[] columnIds() { |
| 221 | return COL_IDS; |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | } |