blob: 70899bca5d35817fc11c8a5b92a3ad7b332313ad [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 Huntd2747a02015-04-30 22:41:16 -070030import org.onosproject.ui.RequestHandler;
Simon Hunta0ddb022015-05-01 09:53:01 -070031import org.onosproject.ui.UiMessageHandler;
Simon Hunt3d1b0652015-05-05 17:27:24 -070032import org.onosproject.ui.table.TableModel;
Simon Huntabd16f62015-05-01 13:14:40 -070033import org.onosproject.ui.table.TableRequestHandler;
Simon Hunt3d1b0652015-05-05 17:27:24 -070034import org.onosproject.ui.table.cell.IntComparator;
Thomas Vachuskae586b792015-03-26 13:59:38 -070035
36import java.util.ArrayList;
Simon Huntd2747a02015-04-30 22:41:16 -070037import java.util.Collection;
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
Bri Prebilic Cole9467a232015-05-06 16:59:05 -070042import static org.apache.commons.lang.WordUtils.capitalizeFully;
43
Thomas Vachuskae586b792015-03-26 13:59:38 -070044/**
45 * Message handler for device view related messages.
46 */
Simon Hunta0ddb022015-05-01 09:53:01 -070047public class DeviceViewMessageHandler extends UiMessageHandler {
Simon Huntd2747a02015-04-30 22:41:16 -070048
49 private static final String DEV_DATA_REQ = "deviceDataRequest";
Simon Huntabd16f62015-05-01 13:14:40 -070050 private static final String DEV_DATA_RESP = "deviceDataResponse";
51 private static final String DEVICES = "devices";
52
53 private static final String DEV_DETAILS_REQ = "deviceDetailsRequest";
54 private static final String DEV_DETAILS_RESP = "deviceDetailsResponse";
55 private static final String DETAILS = "details";
Thomas Vachuskae586b792015-03-26 13:59:38 -070056
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -070057 private static final String ID = "id";
58 private static final String TYPE = "type";
59 private static final String AVAILABLE = "available";
60 private static final String AVAILABLE_IID = "_iconid_available";
61 private static final String TYPE_IID = "_iconid_type";
62 private static final String DEV_ICON_PREFIX = "devIcon_";
63 private static final String NUM_PORTS = "num_ports";
64 private static final String LINK_DEST = "elinks_dest";
65 private static final String MFR = "mfr";
66 private static final String HW = "hw";
67 private static final String SW = "sw";
68 private static final String PROTOCOL = "protocol";
69 private static final String MASTER_ID = "masterid";
70 private static final String CHASSIS_ID = "chassisid";
71 private static final String SERIAL = "serial";
72 private static final String PORTS = "ports";
73 private static final String ENABLED = "enabled";
74 private static final String SPEED = "speed";
Thomas Vachuskab52a0142015-04-21 17:48:15 -070075 private static final String NAME = "name";
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -070076
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -070077
Simon Hunt3d1b0652015-05-05 17:27:24 -070078 private static final String[] COL_IDS = {
79 AVAILABLE, AVAILABLE_IID, TYPE_IID, ID,
80 NUM_PORTS, MASTER_ID, MFR, HW, SW,
81 PROTOCOL, CHASSIS_ID, SERIAL
82 };
83
84 private static final String ICON_ID_ONLINE = "active";
85 private static final String ICON_ID_OFFLINE = "inactive";
86
Thomas Vachuskae586b792015-03-26 13:59:38 -070087 @Override
Simon Huntd2747a02015-04-30 22:41:16 -070088 protected Collection<RequestHandler> getHandlers() {
89 return ImmutableSet.of(
90 new DataRequestHandler(),
91 new DetailRequestHandler()
92 );
93 }
94
Simon Hunt3d1b0652015-05-05 17:27:24 -070095 private static String getTypeIconId(Device d) {
96 return DEV_ICON_PREFIX + d.type().toString();
97 }
98
Simon Huntabd16f62015-05-01 13:14:40 -070099 // handler for device table requests
100 private final class DataRequestHandler extends TableRequestHandler {
Simon Huntd2747a02015-04-30 22:41:16 -0700101 private DataRequestHandler() {
Simon Huntabd16f62015-05-01 13:14:40 -0700102 super(DEV_DATA_REQ, DEV_DATA_RESP, DEVICES);
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700103 }
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700104
Simon Huntd2747a02015-04-30 22:41:16 -0700105 @Override
Simon Hunt3d1b0652015-05-05 17:27:24 -0700106 protected String[] getColumnIds() {
107 return COL_IDS;
108 }
109
110 @Override
111 protected TableModel createTableModel() {
112 TableModel tm = super.createTableModel();
113 tm.setComparator(NUM_PORTS, IntComparator.INSTANCE);
114 return tm;
115 }
116
117 @Override
118 protected void populateTable(TableModel tm, ObjectNode payload) {
119 DeviceService ds = get(DeviceService.class);
120 MastershipService ms = get(MastershipService.class);
121 for (Device dev : ds.getDevices()) {
122 populateRow(tm.addRow(), dev, ds, ms);
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700123 }
Simon Hunt3d1b0652015-05-05 17:27:24 -0700124 }
125
126 private void populateRow(TableModel.Row row, Device dev,
127 DeviceService ds, MastershipService ms) {
128 DeviceId id = dev.id();
129 boolean available = ds.isAvailable(id);
130 String iconId = available ? ICON_ID_ONLINE : ICON_ID_OFFLINE;
131
132 row.cell(ID, id)
133 .cell(AVAILABLE, available)
134 .cell(AVAILABLE_IID, iconId)
135 .cell(TYPE_IID, getTypeIconId(dev))
136 .cell(MFR, dev.manufacturer())
137 .cell(HW, dev.hwVersion())
138 .cell(SW, dev.swVersion())
139 .cell(PROTOCOL, dev.annotations().value(PROTOCOL))
140 .cell(NUM_PORTS, ds.getPorts(id).size())
141 .cell(MASTER_ID, ms.getMasterFor(id));
Simon Huntd2747a02015-04-30 22:41:16 -0700142 }
143 }
144
Simon Huntabd16f62015-05-01 13:14:40 -0700145 // handler for selected device detail requests
Simon Huntd2747a02015-04-30 22:41:16 -0700146 private final class DetailRequestHandler extends RequestHandler {
147 private DetailRequestHandler() {
Simon Huntabd16f62015-05-01 13:14:40 -0700148 super(DEV_DETAILS_REQ);
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700149 }
150
Simon Huntd2747a02015-04-30 22:41:16 -0700151 @Override
152 public void process(long sid, ObjectNode payload) {
153 String id = string(payload, "id", "of:0000000000000000");
154
155 DeviceId deviceId = DeviceId.deviceId(id);
156 DeviceService service = get(DeviceService.class);
157 MastershipService ms = get(MastershipService.class);
158 Device device = service.getDevice(deviceId);
159 ObjectNode data = MAPPER.createObjectNode();
160
161 data.put(ID, deviceId.toString());
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700162 data.put(TYPE, capitalizeFully(device.type().toString()));
Simon Huntd2747a02015-04-30 22:41:16 -0700163 data.put(TYPE_IID, getTypeIconId(device));
164 data.put(MFR, device.manufacturer());
165 data.put(HW, device.hwVersion());
166 data.put(SW, device.swVersion());
167 data.put(SERIAL, device.serialNumber());
168 data.put(CHASSIS_ID, device.chassisId().toString());
169 data.put(MASTER_ID, ms.getMasterFor(deviceId).toString());
170 data.put(PROTOCOL, device.annotations().value(PROTOCOL));
171
172 ArrayNode ports = MAPPER.createArrayNode();
173
174 List<Port> portList = new ArrayList<>(service.getPorts(deviceId));
175 Collections.sort(portList, (p1, p2) -> {
176 long delta = p1.number().toLong() - p2.number().toLong();
177 return delta == 0 ? 0 : (delta < 0 ? -1 : +1);
178 });
179
180 for (Port p : portList) {
181 ports.add(portData(p, deviceId));
182 }
183 data.set(PORTS, ports);
184
185 ObjectNode rootNode = MAPPER.createObjectNode();
Simon Huntabd16f62015-05-01 13:14:40 -0700186 rootNode.set(DETAILS, data);
187 sendMessage(DEV_DETAILS_RESP, 0, rootNode);
Simon Huntd2747a02015-04-30 22:41:16 -0700188 }
189
190 private ObjectNode portData(Port p, DeviceId id) {
191 ObjectNode port = MAPPER.createObjectNode();
192 LinkService ls = get(LinkService.class);
193 String name = p.annotations().value(AnnotationKeys.PORT_NAME);
194
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700195 port.put(ID, capitalizeFully(p.number().toString()));
196 port.put(TYPE, capitalizeFully(p.type().toString()));
Simon Huntd2747a02015-04-30 22:41:16 -0700197 port.put(SPEED, p.portSpeed());
198 port.put(ENABLED, p.isEnabled());
199 port.put(NAME, name != null ? name : "");
200
201 Set<Link> links = ls.getEgressLinks(new ConnectPoint(id, p.number()));
202 if (!links.isEmpty()) {
203 StringBuilder egressLinks = new StringBuilder();
204 for (Link l : links) {
205 ConnectPoint dest = l.dst();
206 egressLinks.append(dest.elementId()).append("/")
207 .append(dest.port()).append(" ");
208 }
209 port.put(LINK_DEST, egressLinks.toString());
210 }
211
212 return port;
213 }
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700214 }
Thomas Vachuskae586b792015-03-26 13:59:38 -0700215}