blob: d1cbcb48a3f899584f2e63240cf8a54d69bfa978 [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;
Thomas Vachuskae586b792015-03-26 13:59:38 -070034
35import java.util.ArrayList;
Simon Huntd2747a02015-04-30 22:41:16 -070036import java.util.Collection;
Thomas Vachuskab52a0142015-04-21 17:48:15 -070037import java.util.Collections;
Thomas Vachuskae586b792015-03-26 13:59:38 -070038import java.util.List;
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -070039import java.util.Set;
Bri Prebilic Cole264c5ec2015-04-07 10:22:26 -070040
Bri Prebilic Cole9467a232015-05-06 16:59:05 -070041import static org.apache.commons.lang.WordUtils.capitalizeFully;
42
Thomas Vachuskae586b792015-03-26 13:59:38 -070043/**
44 * Message handler for device view related messages.
45 */
Simon Hunta0ddb022015-05-01 09:53:01 -070046public class DeviceViewMessageHandler extends UiMessageHandler {
Simon Huntd2747a02015-04-30 22:41:16 -070047
48 private static final String DEV_DATA_REQ = "deviceDataRequest";
Simon Huntabd16f62015-05-01 13:14:40 -070049 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 Vachuskae586b792015-03-26 13:59:38 -070055
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -070056 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 Vachuskab52a0142015-04-21 17:48:15 -070074 private static final String NAME = "name";
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -070075
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -070076
Simon Hunt3d1b0652015-05-05 17:27:24 -070077 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 Vachuskae586b792015-03-26 13:59:38 -070086 @Override
Simon Huntda580882015-05-12 20:58:18 -070087 protected Collection<RequestHandler> createRequestHandlers() {
Simon Huntd2747a02015-04-30 22:41:16 -070088 return ImmutableSet.of(
89 new DataRequestHandler(),
90 new DetailRequestHandler()
91 );
92 }
93
Simon Hunt3d1b0652015-05-05 17:27:24 -070094 private static String getTypeIconId(Device d) {
95 return DEV_ICON_PREFIX + d.type().toString();
96 }
97
Simon Huntabd16f62015-05-01 13:14:40 -070098 // handler for device table requests
99 private final class DataRequestHandler extends TableRequestHandler {
Simon Huntd2747a02015-04-30 22:41:16 -0700100 private DataRequestHandler() {
Simon Huntabd16f62015-05-01 13:14:40 -0700101 super(DEV_DATA_REQ, DEV_DATA_RESP, DEVICES);
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700102 }
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700103
Simon Huntd2747a02015-04-30 22:41:16 -0700104 @Override
Simon Hunt3d1b0652015-05-05 17:27:24 -0700105 protected String[] getColumnIds() {
106 return COL_IDS;
107 }
108
109 @Override
Simon Hunt3d1b0652015-05-05 17:27:24 -0700110 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 Cole0feedc02015-04-09 14:17:37 -0700115 }
Simon Hunt3d1b0652015-05-05 17:27:24 -0700116 }
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
124 row.cell(ID, id)
125 .cell(AVAILABLE, available)
126 .cell(AVAILABLE_IID, iconId)
127 .cell(TYPE_IID, getTypeIconId(dev))
128 .cell(MFR, dev.manufacturer())
129 .cell(HW, dev.hwVersion())
130 .cell(SW, dev.swVersion())
131 .cell(PROTOCOL, dev.annotations().value(PROTOCOL))
132 .cell(NUM_PORTS, ds.getPorts(id).size())
133 .cell(MASTER_ID, ms.getMasterFor(id));
Simon Huntd2747a02015-04-30 22:41:16 -0700134 }
135 }
136
Simon Huntabd16f62015-05-01 13:14:40 -0700137 // handler for selected device detail requests
Simon Huntd2747a02015-04-30 22:41:16 -0700138 private final class DetailRequestHandler extends RequestHandler {
139 private DetailRequestHandler() {
Simon Huntabd16f62015-05-01 13:14:40 -0700140 super(DEV_DETAILS_REQ);
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700141 }
142
Simon Huntd2747a02015-04-30 22:41:16 -0700143 @Override
144 public void process(long sid, ObjectNode payload) {
145 String id = string(payload, "id", "of:0000000000000000");
146
147 DeviceId deviceId = DeviceId.deviceId(id);
148 DeviceService service = get(DeviceService.class);
149 MastershipService ms = get(MastershipService.class);
150 Device device = service.getDevice(deviceId);
151 ObjectNode data = MAPPER.createObjectNode();
152
153 data.put(ID, deviceId.toString());
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700154 data.put(TYPE, capitalizeFully(device.type().toString()));
Simon Huntd2747a02015-04-30 22:41:16 -0700155 data.put(TYPE_IID, getTypeIconId(device));
156 data.put(MFR, device.manufacturer());
157 data.put(HW, device.hwVersion());
158 data.put(SW, device.swVersion());
159 data.put(SERIAL, device.serialNumber());
160 data.put(CHASSIS_ID, device.chassisId().toString());
161 data.put(MASTER_ID, ms.getMasterFor(deviceId).toString());
162 data.put(PROTOCOL, device.annotations().value(PROTOCOL));
163
164 ArrayNode ports = MAPPER.createArrayNode();
165
166 List<Port> portList = new ArrayList<>(service.getPorts(deviceId));
167 Collections.sort(portList, (p1, p2) -> {
168 long delta = p1.number().toLong() - p2.number().toLong();
169 return delta == 0 ? 0 : (delta < 0 ? -1 : +1);
170 });
171
172 for (Port p : portList) {
173 ports.add(portData(p, deviceId));
174 }
175 data.set(PORTS, ports);
176
177 ObjectNode rootNode = MAPPER.createObjectNode();
Simon Huntabd16f62015-05-01 13:14:40 -0700178 rootNode.set(DETAILS, data);
179 sendMessage(DEV_DETAILS_RESP, 0, rootNode);
Simon Huntd2747a02015-04-30 22:41:16 -0700180 }
181
182 private ObjectNode portData(Port p, DeviceId id) {
183 ObjectNode port = MAPPER.createObjectNode();
184 LinkService ls = get(LinkService.class);
185 String name = p.annotations().value(AnnotationKeys.PORT_NAME);
186
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700187 port.put(ID, capitalizeFully(p.number().toString()));
188 port.put(TYPE, capitalizeFully(p.type().toString()));
Simon Huntd2747a02015-04-30 22:41:16 -0700189 port.put(SPEED, p.portSpeed());
190 port.put(ENABLED, p.isEnabled());
191 port.put(NAME, name != null ? name : "");
192
193 Set<Link> links = ls.getEgressLinks(new ConnectPoint(id, p.number()));
194 if (!links.isEmpty()) {
195 StringBuilder egressLinks = new StringBuilder();
196 for (Link l : links) {
197 ConnectPoint dest = l.dst();
198 egressLinks.append(dest.elementId()).append("/")
199 .append(dest.port()).append(" ");
200 }
201 port.put(LINK_DEST, egressLinks.toString());
202 }
203
204 return port;
205 }
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700206 }
Thomas Vachuskae586b792015-03-26 13:59:38 -0700207}