blob: 806e8c6e37e53cda136e00d22a00dbb1dc84372f [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 Hunt44aa2f82015-04-30 15:01:35 -070032import org.onosproject.ui.table.AbstractTableRow;
Simon Huntabd16f62015-05-01 13:14:40 -070033import org.onosproject.ui.table.TableRequestHandler;
Simon Hunt44aa2f82015-04-30 15:01:35 -070034import org.onosproject.ui.table.TableRow;
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
Thomas Vachuskae586b792015-03-26 13:59:38 -070042/**
43 * Message handler for device view related messages.
44 */
Simon Hunta0ddb022015-05-01 09:53:01 -070045public class DeviceViewMessageHandler extends UiMessageHandler {
Simon Huntd2747a02015-04-30 22:41:16 -070046
47 private static final String DEV_DATA_REQ = "deviceDataRequest";
Simon Huntabd16f62015-05-01 13:14:40 -070048 private static final String DEV_DATA_RESP = "deviceDataResponse";
49 private static final String DEVICES = "devices";
50
51 private static final String DEV_DETAILS_REQ = "deviceDetailsRequest";
52 private static final String DEV_DETAILS_RESP = "deviceDetailsResponse";
53 private static final String DETAILS = "details";
Thomas Vachuskae586b792015-03-26 13:59:38 -070054
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -070055 private static final String ID = "id";
56 private static final String TYPE = "type";
57 private static final String AVAILABLE = "available";
58 private static final String AVAILABLE_IID = "_iconid_available";
59 private static final String TYPE_IID = "_iconid_type";
60 private static final String DEV_ICON_PREFIX = "devIcon_";
61 private static final String NUM_PORTS = "num_ports";
62 private static final String LINK_DEST = "elinks_dest";
63 private static final String MFR = "mfr";
64 private static final String HW = "hw";
65 private static final String SW = "sw";
66 private static final String PROTOCOL = "protocol";
67 private static final String MASTER_ID = "masterid";
68 private static final String CHASSIS_ID = "chassisid";
69 private static final String SERIAL = "serial";
70 private static final String PORTS = "ports";
71 private static final String ENABLED = "enabled";
72 private static final String SPEED = "speed";
Thomas Vachuskab52a0142015-04-21 17:48:15 -070073 private static final String NAME = "name";
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -070074
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -070075
Thomas Vachuskae586b792015-03-26 13:59:38 -070076 @Override
Simon Huntd2747a02015-04-30 22:41:16 -070077 protected Collection<RequestHandler> getHandlers() {
78 return ImmutableSet.of(
79 new DataRequestHandler(),
80 new DetailRequestHandler()
81 );
82 }
83
Simon Huntabd16f62015-05-01 13:14:40 -070084 // handler for device table requests
85 private final class DataRequestHandler extends TableRequestHandler {
Simon Huntd2747a02015-04-30 22:41:16 -070086 private DataRequestHandler() {
Simon Huntabd16f62015-05-01 13:14:40 -070087 super(DEV_DATA_REQ, DEV_DATA_RESP, DEVICES);
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -070088 }
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -070089
Simon Huntd2747a02015-04-30 22:41:16 -070090 @Override
Simon Huntabd16f62015-05-01 13:14:40 -070091 protected TableRow[] generateTableRows(ObjectNode payload) {
Simon Huntd2747a02015-04-30 22:41:16 -070092 DeviceService service = get(DeviceService.class);
93 MastershipService mastershipService = get(MastershipService.class);
Simon Huntd2747a02015-04-30 22:41:16 -070094 List<TableRow> list = new ArrayList<>();
95 for (Device dev : service.getDevices()) {
96 list.add(new DeviceTableRow(service, mastershipService, dev));
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -070097 }
Simon Huntd2747a02015-04-30 22:41:16 -070098 return list.toArray(new TableRow[list.size()]);
99 }
100 }
101
Simon Huntabd16f62015-05-01 13:14:40 -0700102 // handler for selected device detail requests
Simon Huntd2747a02015-04-30 22:41:16 -0700103 private final class DetailRequestHandler extends RequestHandler {
104 private DetailRequestHandler() {
Simon Huntabd16f62015-05-01 13:14:40 -0700105 super(DEV_DETAILS_REQ);
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700106 }
107
Simon Huntd2747a02015-04-30 22:41:16 -0700108 @Override
109 public void process(long sid, ObjectNode payload) {
110 String id = string(payload, "id", "of:0000000000000000");
111
112 DeviceId deviceId = DeviceId.deviceId(id);
113 DeviceService service = get(DeviceService.class);
114 MastershipService ms = get(MastershipService.class);
115 Device device = service.getDevice(deviceId);
116 ObjectNode data = MAPPER.createObjectNode();
117
118 data.put(ID, deviceId.toString());
119 data.put(TYPE, device.type().toString());
120 data.put(TYPE_IID, getTypeIconId(device));
121 data.put(MFR, device.manufacturer());
122 data.put(HW, device.hwVersion());
123 data.put(SW, device.swVersion());
124 data.put(SERIAL, device.serialNumber());
125 data.put(CHASSIS_ID, device.chassisId().toString());
126 data.put(MASTER_ID, ms.getMasterFor(deviceId).toString());
127 data.put(PROTOCOL, device.annotations().value(PROTOCOL));
128
129 ArrayNode ports = MAPPER.createArrayNode();
130
131 List<Port> portList = new ArrayList<>(service.getPorts(deviceId));
132 Collections.sort(portList, (p1, p2) -> {
133 long delta = p1.number().toLong() - p2.number().toLong();
134 return delta == 0 ? 0 : (delta < 0 ? -1 : +1);
135 });
136
137 for (Port p : portList) {
138 ports.add(portData(p, deviceId));
139 }
140 data.set(PORTS, ports);
141
142 ObjectNode rootNode = MAPPER.createObjectNode();
Simon Huntabd16f62015-05-01 13:14:40 -0700143 rootNode.set(DETAILS, data);
144 sendMessage(DEV_DETAILS_RESP, 0, rootNode);
Simon Huntd2747a02015-04-30 22:41:16 -0700145 }
146
147 private ObjectNode portData(Port p, DeviceId id) {
148 ObjectNode port = MAPPER.createObjectNode();
149 LinkService ls = get(LinkService.class);
150 String name = p.annotations().value(AnnotationKeys.PORT_NAME);
151
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());
156 port.put(NAME, name != null ? name : "");
157
158 Set<Link> links = ls.getEgressLinks(new ConnectPoint(id, p.number()));
159 if (!links.isEmpty()) {
160 StringBuilder egressLinks = new StringBuilder();
161 for (Link l : links) {
162 ConnectPoint dest = l.dst();
163 egressLinks.append(dest.elementId()).append("/")
164 .append(dest.port()).append(" ");
165 }
166 port.put(LINK_DEST, egressLinks.toString());
167 }
168
169 return port;
170 }
171
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700172 }
173
Bri Prebilic Coleb699a162015-04-13 12:01:39 -0700174 private static String getTypeIconId(Device d) {
175 return DEV_ICON_PREFIX + d.type().toString();
176 }
177
Thomas Vachuskae586b792015-03-26 13:59:38 -0700178 /**
179 * TableRow implementation for {@link Device devices}.
180 */
181 private static class DeviceTableRow extends AbstractTableRow {
182
Thomas Vachuskae586b792015-03-26 13:59:38 -0700183 private static final String[] COL_IDS = {
Bri Prebilic Cole264c5ec2015-04-07 10:22:26 -0700184 AVAILABLE, AVAILABLE_IID, TYPE_IID, ID,
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700185 NUM_PORTS, MASTER_ID, MFR, HW, SW,
186 PROTOCOL, CHASSIS_ID, SERIAL
Thomas Vachuskae586b792015-03-26 13:59:38 -0700187 };
188
Bri Prebilic Coleab582b82015-04-14 15:08:22 -0700189 private static final String ICON_ID_ONLINE = "active";
190 private static final String ICON_ID_OFFLINE = "inactive";
Thomas Vachuskae586b792015-03-26 13:59:38 -0700191
Bri Prebilic Coleae65c962015-04-02 16:24:49 -0700192 public DeviceTableRow(DeviceService service,
193 MastershipService ms,
194 Device d) {
Thomas Vachuskae586b792015-03-26 13:59:38 -0700195 boolean available = service.isAvailable(d.id());
196 String iconId = available ? ICON_ID_ONLINE : ICON_ID_OFFLINE;
Bri Prebilic Cole264c5ec2015-04-07 10:22:26 -0700197 DeviceId id = d.id();
198 List<Port> ports = service.getPorts(id);
Thomas Vachuskae586b792015-03-26 13:59:38 -0700199
Bri Prebilic Cole264c5ec2015-04-07 10:22:26 -0700200 add(ID, id.toString());
Thomas Vachuskae586b792015-03-26 13:59:38 -0700201 add(AVAILABLE, Boolean.toString(available));
202 add(AVAILABLE_IID, iconId);
203 add(TYPE_IID, getTypeIconId(d));
Thomas Vachuskae586b792015-03-26 13:59:38 -0700204 add(MFR, d.manufacturer());
205 add(HW, d.hwVersion());
206 add(SW, d.swVersion());
Thomas Vachuskae586b792015-03-26 13:59:38 -0700207 add(PROTOCOL, d.annotations().value(PROTOCOL));
Bri Prebilic Cole264c5ec2015-04-07 10:22:26 -0700208 add(NUM_PORTS, Integer.toString(ports.size()));
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700209 add(MASTER_ID, ms.getMasterFor(d.id()).toString());
Thomas Vachuskae586b792015-03-26 13:59:38 -0700210 }
211
Thomas Vachuskae586b792015-03-26 13:59:38 -0700212 @Override
213 protected String[] columnIds() {
214 return COL_IDS;
215 }
216 }
217
218}