blob: 5015cb5b0fa651f64c541c8705ddd06c97ecd341 [file] [log] [blame]
Thomas Vachuskae586b792015-03-26 13:59:38 -07001/*
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 */
16package org.onosproject.ui.impl;
17
18import com.fasterxml.jackson.databind.node.ArrayNode;
19import com.fasterxml.jackson.databind.node.ObjectNode;
20import com.google.common.collect.ImmutableSet;
Bri Prebilic Coleae65c962015-04-02 16:24:49 -070021import org.onosproject.mastership.MastershipService;
Thomas Vachuskab52a0142015-04-21 17:48:15 -070022import org.onosproject.net.AnnotationKeys;
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -070023import org.onosproject.net.ConnectPoint;
Thomas Vachuskae586b792015-03-26 13:59:38 -070024import org.onosproject.net.Device;
Bri Prebilic Cole264c5ec2015-04-07 10:22:26 -070025import org.onosproject.net.DeviceId;
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -070026import org.onosproject.net.Link;
Bri Prebilic Cole264c5ec2015-04-07 10:22:26 -070027import org.onosproject.net.Port;
Thomas Vachuskae586b792015-03-26 13:59:38 -070028import org.onosproject.net.device.DeviceService;
Bri Prebilic Cole264c5ec2015-04-07 10:22:26 -070029import org.onosproject.net.link.LinkService;
Simon Hunt44aa2f82015-04-30 15:01:35 -070030import org.onosproject.ui.UiMessageHandler;
31import org.onosproject.ui.table.AbstractTableRow;
32import org.onosproject.ui.table.RowComparator;
33import org.onosproject.ui.table.TableRow;
34import org.onosproject.ui.table.TableUtils;
Thomas Vachuskae586b792015-03-26 13:59:38 -070035
36import java.util.ArrayList;
37import java.util.Arrays;
Thomas Vachuskab52a0142015-04-21 17:48:15 -070038import java.util.Collections;
Thomas Vachuskae586b792015-03-26 13:59:38 -070039import java.util.List;
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -070040import java.util.Set;
Bri Prebilic Cole264c5ec2015-04-07 10:22:26 -070041
Thomas Vachuskae586b792015-03-26 13:59:38 -070042/**
43 * Message handler for device view related messages.
44 */
Simon Hunt44aa2f82015-04-30 15:01:35 -070045public class DeviceViewMessageHandler extends UiMessageHandler {
Thomas Vachuskae586b792015-03-26 13:59:38 -070046
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -070047 private static final String ID = "id";
48 private static final String TYPE = "type";
49 private static final String AVAILABLE = "available";
50 private static final String AVAILABLE_IID = "_iconid_available";
51 private static final String TYPE_IID = "_iconid_type";
52 private static final String DEV_ICON_PREFIX = "devIcon_";
53 private static final String NUM_PORTS = "num_ports";
54 private static final String LINK_DEST = "elinks_dest";
55 private static final String MFR = "mfr";
56 private static final String HW = "hw";
57 private static final String SW = "sw";
58 private static final String PROTOCOL = "protocol";
59 private static final String MASTER_ID = "masterid";
60 private static final String CHASSIS_ID = "chassisid";
61 private static final String SERIAL = "serial";
62 private static final String PORTS = "ports";
63 private static final String ENABLED = "enabled";
64 private static final String SPEED = "speed";
Thomas Vachuskab52a0142015-04-21 17:48:15 -070065 private static final String NAME = "name";
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -070066
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -070067
Thomas Vachuskae586b792015-03-26 13:59:38 -070068 /**
69 * Creates a new message handler for the device messages.
70 */
71 protected DeviceViewMessageHandler() {
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -070072 super(ImmutableSet.of("deviceDataRequest", "deviceDetailsRequest"));
Thomas Vachuskae586b792015-03-26 13:59:38 -070073 }
74
75 @Override
Simon Hunt44aa2f82015-04-30 15:01:35 -070076 public void process(ObjectNode message) {
77 String type = eventType(message);
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -070078 if (type.equals("deviceDataRequest")) {
Simon Hunt44aa2f82015-04-30 15:01:35 -070079 dataRequest(message);
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -070080 } else if (type.equals("deviceDetailsRequest")) {
Simon Hunt44aa2f82015-04-30 15:01:35 -070081 detailsRequest(message);
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -070082 }
83 }
84
Simon Hunt44aa2f82015-04-30 15:01:35 -070085 private void dataRequest(ObjectNode message) {
86 ObjectNode payload = payload(message);
87 RowComparator rc = TableUtils.createRowComparator(payload);
Thomas Vachuskae586b792015-03-26 13:59:38 -070088
89 DeviceService service = get(DeviceService.class);
Bri Prebilic Coleae65c962015-04-02 16:24:49 -070090 MastershipService mastershipService = get(MastershipService.class);
Bri Prebilic Coleb699a162015-04-13 12:01:39 -070091 TableRow[] rows = generateTableRows(service, mastershipService);
Thomas Vachuskae586b792015-03-26 13:59:38 -070092 Arrays.sort(rows, rc);
Thomas Vachuskae586b792015-03-26 13:59:38 -070093 ObjectNode rootNode = mapper.createObjectNode();
Simon Hunt44aa2f82015-04-30 15:01:35 -070094 rootNode.set("devices", TableUtils.generateArrayNode(rows));
Thomas Vachuskae586b792015-03-26 13:59:38 -070095
96 connection().sendMessage("deviceDataResponse", 0, rootNode);
97 }
98
Simon Hunt44aa2f82015-04-30 15:01:35 -070099 private void detailsRequest(ObjectNode message) {
100 ObjectNode payload = payload(message);
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700101 String id = string(payload, "id", "of:0000000000000000");
102
103 DeviceId deviceId = DeviceId.deviceId(id);
104 DeviceService service = get(DeviceService.class);
105 MastershipService ms = get(MastershipService.class);
106 Device device = service.getDevice(deviceId);
Simon Huntc00295c2015-04-30 10:52:57 -0700107 ObjectNode data = mapper.createObjectNode();
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700108
109 data.put(ID, deviceId.toString());
110 data.put(TYPE, device.type().toString());
Bri Prebilic Coleb699a162015-04-13 12:01:39 -0700111 data.put(TYPE_IID, getTypeIconId(device));
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700112 data.put(MFR, device.manufacturer());
113 data.put(HW, device.hwVersion());
114 data.put(SW, device.swVersion());
115 data.put(SERIAL, device.serialNumber());
116 data.put(CHASSIS_ID, device.chassisId().toString());
117 data.put(MASTER_ID, ms.getMasterFor(deviceId).toString());
118 data.put(PROTOCOL, device.annotations().value(PROTOCOL));
119
Simon Huntc00295c2015-04-30 10:52:57 -0700120 ArrayNode ports = mapper.createArrayNode();
Thomas Vachuskab52a0142015-04-21 17:48:15 -0700121
122 List<Port> portList = new ArrayList<>(service.getPorts(deviceId));
123 Collections.sort(portList, (p1, p2) -> {
124 long delta = p1.number().toLong() - p2.number().toLong();
125 return delta == 0 ? 0 : (delta < 0 ? -1 : +1);
126 });
127
128 for (Port p : portList) {
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700129 ports.add(portData(p, deviceId));
130 }
131 data.set(PORTS, ports);
132
133 ObjectNode rootNode = mapper.createObjectNode();
134 rootNode.set("details", data);
135 connection().sendMessage("deviceDetailsResponse", 0, rootNode);
136 }
137
Bri Prebilic Coleae65c962015-04-02 16:24:49 -0700138 private TableRow[] generateTableRows(DeviceService service,
Bri Prebilic Coleb699a162015-04-13 12:01:39 -0700139 MastershipService mastershipService) {
Thomas Vachuskae586b792015-03-26 13:59:38 -0700140 List<TableRow> list = new ArrayList<>();
141 for (Device dev : service.getDevices()) {
Simon Hunt44aa2f82015-04-30 15:01:35 -0700142 list.add(new DeviceTableRow(service, mastershipService, dev));
Thomas Vachuskae586b792015-03-26 13:59:38 -0700143 }
144 return list.toArray(new TableRow[list.size()]);
145 }
146
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700147 private ObjectNode portData(Port p, DeviceId id) {
Simon Huntc00295c2015-04-30 10:52:57 -0700148 ObjectNode port = mapper.createObjectNode();
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700149 LinkService ls = get(LinkService.class);
Thomas Vachuskab52a0142015-04-21 17:48:15 -0700150 String name = p.annotations().value(AnnotationKeys.PORT_NAME);
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700151
152 port.put(ID, p.number().toString());
153 port.put(TYPE, p.type().toString());
154 port.put(SPEED, p.portSpeed());
155 port.put(ENABLED, p.isEnabled());
Thomas Vachuskab52a0142015-04-21 17:48:15 -0700156 port.put(NAME, name != null ? name : "");
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700157
158 Set<Link> links = ls.getEgressLinks(new ConnectPoint(id, p.number()));
159 if (!links.isEmpty()) {
Simon Hunt44aa2f82015-04-30 15:01:35 -0700160 StringBuilder egressLinks = new StringBuilder();
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700161 for (Link l : links) {
162 ConnectPoint dest = l.dst();
Simon Hunt44aa2f82015-04-30 15:01:35 -0700163 egressLinks.append(dest.elementId()).append("/")
164 .append(dest.port()).append(" ");
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700165 }
Simon Hunt44aa2f82015-04-30 15:01:35 -0700166 port.put(LINK_DEST, egressLinks.toString());
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700167 }
168
169 return port;
170 }
171
Bri Prebilic Coleb699a162015-04-13 12:01:39 -0700172 private static String getTypeIconId(Device d) {
173 return DEV_ICON_PREFIX + d.type().toString();
174 }
175
Thomas Vachuskae586b792015-03-26 13:59:38 -0700176 /**
177 * TableRow implementation for {@link Device devices}.
178 */
179 private static class DeviceTableRow extends AbstractTableRow {
180
Thomas Vachuskae586b792015-03-26 13:59:38 -0700181 private static final String[] COL_IDS = {
Bri Prebilic Cole264c5ec2015-04-07 10:22:26 -0700182 AVAILABLE, AVAILABLE_IID, TYPE_IID, ID,
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700183 NUM_PORTS, MASTER_ID, MFR, HW, SW,
184 PROTOCOL, CHASSIS_ID, SERIAL
Thomas Vachuskae586b792015-03-26 13:59:38 -0700185 };
186
Bri Prebilic Coleab582b82015-04-14 15:08:22 -0700187 private static final String ICON_ID_ONLINE = "active";
188 private static final String ICON_ID_OFFLINE = "inactive";
Thomas Vachuskae586b792015-03-26 13:59:38 -0700189
Bri Prebilic Coleae65c962015-04-02 16:24:49 -0700190 public DeviceTableRow(DeviceService service,
191 MastershipService ms,
192 Device d) {
Thomas Vachuskae586b792015-03-26 13:59:38 -0700193 boolean available = service.isAvailable(d.id());
194 String iconId = available ? ICON_ID_ONLINE : ICON_ID_OFFLINE;
Bri Prebilic Cole264c5ec2015-04-07 10:22:26 -0700195 DeviceId id = d.id();
196 List<Port> ports = service.getPorts(id);
Thomas Vachuskae586b792015-03-26 13:59:38 -0700197
Bri Prebilic Cole264c5ec2015-04-07 10:22:26 -0700198 add(ID, id.toString());
Thomas Vachuskae586b792015-03-26 13:59:38 -0700199 add(AVAILABLE, Boolean.toString(available));
200 add(AVAILABLE_IID, iconId);
201 add(TYPE_IID, getTypeIconId(d));
Thomas Vachuskae586b792015-03-26 13:59:38 -0700202 add(MFR, d.manufacturer());
203 add(HW, d.hwVersion());
204 add(SW, d.swVersion());
Thomas Vachuskae586b792015-03-26 13:59:38 -0700205 add(PROTOCOL, d.annotations().value(PROTOCOL));
Bri Prebilic Cole264c5ec2015-04-07 10:22:26 -0700206 add(NUM_PORTS, Integer.toString(ports.size()));
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700207 add(MASTER_ID, ms.getMasterFor(d.id()).toString());
Thomas Vachuskae586b792015-03-26 13:59:38 -0700208 }
209
Thomas Vachuskae586b792015-03-26 13:59:38 -0700210 @Override
211 protected String[] columnIds() {
212 return COL_IDS;
213 }
214 }
215
216}