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; |
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 | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 29 | import org.onosproject.net.device.DeviceService; |
Bri Prebilic Cole | 264c5ec | 2015-04-07 10:22:26 -0700 | [diff] [blame] | 30 | import org.onosproject.net.link.LinkService; |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 31 | |
| 32 | import java.util.ArrayList; |
| 33 | import java.util.Arrays; |
Thomas Vachuska | b52a014 | 2015-04-21 17:48:15 -0700 | [diff] [blame^] | 34 | import java.util.Collections; |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 35 | import java.util.List; |
Bri Prebilic Cole | 0feedc0 | 2015-04-09 14:17:37 -0700 | [diff] [blame] | 36 | import java.util.Set; |
Bri Prebilic Cole | 264c5ec | 2015-04-07 10:22:26 -0700 | [diff] [blame] | 37 | |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 38 | /** |
| 39 | * Message handler for device view related messages. |
| 40 | */ |
| 41 | public class DeviceViewMessageHandler extends AbstractTabularViewMessageHandler { |
| 42 | |
Bri Prebilic Cole | 0feedc0 | 2015-04-09 14:17:37 -0700 | [diff] [blame] | 43 | private static final String ID = "id"; |
| 44 | private static final String TYPE = "type"; |
| 45 | private static final String AVAILABLE = "available"; |
| 46 | private static final String AVAILABLE_IID = "_iconid_available"; |
| 47 | private static final String TYPE_IID = "_iconid_type"; |
| 48 | private static final String DEV_ICON_PREFIX = "devIcon_"; |
| 49 | private static final String NUM_PORTS = "num_ports"; |
| 50 | private static final String LINK_DEST = "elinks_dest"; |
| 51 | private static final String MFR = "mfr"; |
| 52 | private static final String HW = "hw"; |
| 53 | private static final String SW = "sw"; |
| 54 | private static final String PROTOCOL = "protocol"; |
| 55 | private static final String MASTER_ID = "masterid"; |
| 56 | private static final String CHASSIS_ID = "chassisid"; |
| 57 | private static final String SERIAL = "serial"; |
| 58 | private static final String PORTS = "ports"; |
| 59 | private static final String ENABLED = "enabled"; |
| 60 | private static final String SPEED = "speed"; |
Thomas Vachuska | b52a014 | 2015-04-21 17:48:15 -0700 | [diff] [blame^] | 61 | private static final String NAME = "name"; |
Bri Prebilic Cole | 0feedc0 | 2015-04-09 14:17:37 -0700 | [diff] [blame] | 62 | |
| 63 | private static final ObjectMapper MAPPER = new ObjectMapper(); |
| 64 | |
| 65 | |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 66 | /** |
| 67 | * Creates a new message handler for the device messages. |
| 68 | */ |
| 69 | protected DeviceViewMessageHandler() { |
Bri Prebilic Cole | 0feedc0 | 2015-04-09 14:17:37 -0700 | [diff] [blame] | 70 | super(ImmutableSet.of("deviceDataRequest", "deviceDetailsRequest")); |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | @Override |
Bri Prebilic Cole | 0feedc0 | 2015-04-09 14:17:37 -0700 | [diff] [blame] | 74 | public void process(ObjectNode event) { |
| 75 | String type = string(event, "event", "unknown"); |
| 76 | if (type.equals("deviceDataRequest")) { |
| 77 | dataRequest(event); |
| 78 | } else if (type.equals("deviceDetailsRequest")) { |
| 79 | detailsRequest(event); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | private void dataRequest(ObjectNode event) { |
| 84 | ObjectNode payload = payload(event); |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 85 | String sortCol = string(payload, "sortCol", "id"); |
| 86 | String sortDir = string(payload, "sortDir", "asc"); |
| 87 | |
| 88 | DeviceService service = get(DeviceService.class); |
Bri Prebilic Cole | ae65c96 | 2015-04-02 16:24:49 -0700 | [diff] [blame] | 89 | MastershipService mastershipService = get(MastershipService.class); |
Bri Prebilic Cole | 0feedc0 | 2015-04-09 14:17:37 -0700 | [diff] [blame] | 90 | |
Bri Prebilic Cole | b699a16 | 2015-04-13 12:01:39 -0700 | [diff] [blame] | 91 | TableRow[] rows = generateTableRows(service, mastershipService); |
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()); |
Bri Prebilic Cole | b699a16 | 2015-04-13 12:01:39 -0700 | [diff] [blame] | 114 | data.put(TYPE_IID, getTypeIconId(device)); |
Bri Prebilic Cole | 0feedc0 | 2015-04-09 14:17:37 -0700 | [diff] [blame] | 115 | data.put(MFR, device.manufacturer()); |
| 116 | data.put(HW, device.hwVersion()); |
| 117 | data.put(SW, device.swVersion()); |
| 118 | data.put(SERIAL, device.serialNumber()); |
| 119 | data.put(CHASSIS_ID, device.chassisId().toString()); |
| 120 | data.put(MASTER_ID, ms.getMasterFor(deviceId).toString()); |
| 121 | data.put(PROTOCOL, device.annotations().value(PROTOCOL)); |
| 122 | |
| 123 | ArrayNode ports = MAPPER.createArrayNode(); |
Thomas Vachuska | b52a014 | 2015-04-21 17:48:15 -0700 | [diff] [blame^] | 124 | |
| 125 | List<Port> portList = new ArrayList<>(service.getPorts(deviceId)); |
| 126 | Collections.sort(portList, (p1, p2) -> { |
| 127 | long delta = p1.number().toLong() - p2.number().toLong(); |
| 128 | return delta == 0 ? 0 : (delta < 0 ? -1 : +1); |
| 129 | }); |
| 130 | |
| 131 | for (Port p : portList) { |
Bri Prebilic Cole | 0feedc0 | 2015-04-09 14:17:37 -0700 | [diff] [blame] | 132 | ports.add(portData(p, deviceId)); |
| 133 | } |
| 134 | data.set(PORTS, ports); |
| 135 | |
| 136 | ObjectNode rootNode = mapper.createObjectNode(); |
| 137 | rootNode.set("details", data); |
| 138 | connection().sendMessage("deviceDetailsResponse", 0, rootNode); |
| 139 | } |
| 140 | |
Bri Prebilic Cole | ae65c96 | 2015-04-02 16:24:49 -0700 | [diff] [blame] | 141 | private TableRow[] generateTableRows(DeviceService service, |
Bri Prebilic Cole | b699a16 | 2015-04-13 12:01:39 -0700 | [diff] [blame] | 142 | MastershipService mastershipService) { |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 143 | List<TableRow> list = new ArrayList<>(); |
| 144 | for (Device dev : service.getDevices()) { |
Bri Prebilic Cole | 264c5ec | 2015-04-07 10:22:26 -0700 | [diff] [blame] | 145 | list.add(new DeviceTableRow(service, |
| 146 | mastershipService, |
Bri Prebilic Cole | 264c5ec | 2015-04-07 10:22:26 -0700 | [diff] [blame] | 147 | dev)); |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 148 | } |
| 149 | return list.toArray(new TableRow[list.size()]); |
| 150 | } |
| 151 | |
Bri Prebilic Cole | 0feedc0 | 2015-04-09 14:17:37 -0700 | [diff] [blame] | 152 | private ObjectNode portData(Port p, DeviceId id) { |
| 153 | ObjectNode port = MAPPER.createObjectNode(); |
| 154 | LinkService ls = get(LinkService.class); |
Thomas Vachuska | b52a014 | 2015-04-21 17:48:15 -0700 | [diff] [blame^] | 155 | String name = p.annotations().value(AnnotationKeys.PORT_NAME); |
Bri Prebilic Cole | 0feedc0 | 2015-04-09 14:17:37 -0700 | [diff] [blame] | 156 | |
| 157 | port.put(ID, p.number().toString()); |
| 158 | port.put(TYPE, p.type().toString()); |
| 159 | port.put(SPEED, p.portSpeed()); |
| 160 | port.put(ENABLED, p.isEnabled()); |
Thomas Vachuska | b52a014 | 2015-04-21 17:48:15 -0700 | [diff] [blame^] | 161 | port.put(NAME, name != null ? name : ""); |
Bri Prebilic Cole | 0feedc0 | 2015-04-09 14:17:37 -0700 | [diff] [blame] | 162 | |
| 163 | Set<Link> links = ls.getEgressLinks(new ConnectPoint(id, p.number())); |
| 164 | if (!links.isEmpty()) { |
| 165 | String egressLinks = ""; |
| 166 | for (Link l : links) { |
| 167 | ConnectPoint dest = l.dst(); |
| 168 | egressLinks += dest.elementId().toString() |
| 169 | + "/" + dest.port().toString() + " "; |
| 170 | } |
| 171 | port.put(LINK_DEST, egressLinks); |
| 172 | } |
| 173 | |
| 174 | return port; |
| 175 | } |
| 176 | |
Bri Prebilic Cole | b699a16 | 2015-04-13 12:01:39 -0700 | [diff] [blame] | 177 | private static String getTypeIconId(Device d) { |
| 178 | return DEV_ICON_PREFIX + d.type().toString(); |
| 179 | } |
| 180 | |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 181 | /** |
| 182 | * TableRow implementation for {@link Device devices}. |
| 183 | */ |
| 184 | private static class DeviceTableRow extends AbstractTableRow { |
| 185 | |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 186 | private static final String[] COL_IDS = { |
Bri Prebilic Cole | 264c5ec | 2015-04-07 10:22:26 -0700 | [diff] [blame] | 187 | AVAILABLE, AVAILABLE_IID, TYPE_IID, ID, |
Bri Prebilic Cole | 0feedc0 | 2015-04-09 14:17:37 -0700 | [diff] [blame] | 188 | NUM_PORTS, MASTER_ID, MFR, HW, SW, |
| 189 | PROTOCOL, CHASSIS_ID, SERIAL |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 190 | }; |
| 191 | |
Bri Prebilic Cole | ab582b8 | 2015-04-14 15:08:22 -0700 | [diff] [blame] | 192 | private static final String ICON_ID_ONLINE = "active"; |
| 193 | private static final String ICON_ID_OFFLINE = "inactive"; |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 194 | |
Bri Prebilic Cole | ae65c96 | 2015-04-02 16:24:49 -0700 | [diff] [blame] | 195 | public DeviceTableRow(DeviceService service, |
| 196 | MastershipService ms, |
| 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 | |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 215 | @Override |
| 216 | protected String[] columnIds() { |
| 217 | return COL_IDS; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | } |