blob: 302863b8653e2cff204456fb87cc0d9a89c74c5a [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;
31import org.onosproject.ui.UiMessageHandlerTwo;
Simon Hunt44aa2f82015-04-30 15:01:35 -070032import org.onosproject.ui.table.AbstractTableRow;
33import org.onosproject.ui.table.RowComparator;
34import org.onosproject.ui.table.TableRow;
35import org.onosproject.ui.table.TableUtils;
Thomas Vachuskae586b792015-03-26 13:59:38 -070036
37import java.util.ArrayList;
38import java.util.Arrays;
Simon Huntd2747a02015-04-30 22:41:16 -070039import java.util.Collection;
Thomas Vachuskab52a0142015-04-21 17:48:15 -070040import java.util.Collections;
Thomas Vachuskae586b792015-03-26 13:59:38 -070041import java.util.List;
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -070042import java.util.Set;
Bri Prebilic Cole264c5ec2015-04-07 10:22:26 -070043
Thomas Vachuskae586b792015-03-26 13:59:38 -070044/**
45 * Message handler for device view related messages.
46 */
Simon Huntd2747a02015-04-30 22:41:16 -070047public class DeviceViewMessageHandler extends UiMessageHandlerTwo {
48
49 private static final String DEV_DATA_REQ = "deviceDataRequest";
50 private static final String DEV_DETAIL_REQ = "deviceDetailRequest";
Thomas Vachuskae586b792015-03-26 13:59:38 -070051
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -070052 private static final String ID = "id";
53 private static final String TYPE = "type";
54 private static final String AVAILABLE = "available";
55 private static final String AVAILABLE_IID = "_iconid_available";
56 private static final String TYPE_IID = "_iconid_type";
57 private static final String DEV_ICON_PREFIX = "devIcon_";
58 private static final String NUM_PORTS = "num_ports";
59 private static final String LINK_DEST = "elinks_dest";
60 private static final String MFR = "mfr";
61 private static final String HW = "hw";
62 private static final String SW = "sw";
63 private static final String PROTOCOL = "protocol";
64 private static final String MASTER_ID = "masterid";
65 private static final String CHASSIS_ID = "chassisid";
66 private static final String SERIAL = "serial";
67 private static final String PORTS = "ports";
68 private static final String ENABLED = "enabled";
69 private static final String SPEED = "speed";
Thomas Vachuskab52a0142015-04-21 17:48:15 -070070 private static final String NAME = "name";
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -070071
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -070072
Thomas Vachuskae586b792015-03-26 13:59:38 -070073 @Override
Simon Huntd2747a02015-04-30 22:41:16 -070074 protected Collection<RequestHandler> getHandlers() {
75 return ImmutableSet.of(
76 new DataRequestHandler(),
77 new DetailRequestHandler()
78 );
79 }
80
81 // ======================================================================
82
83 private final class DataRequestHandler extends RequestHandler {
84
85 private DataRequestHandler() {
86 super(DEV_DATA_REQ);
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -070087 }
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -070088
Simon Huntd2747a02015-04-30 22:41:16 -070089 @Override
90 public void process(long sid, ObjectNode payload) {
91 RowComparator rc = TableUtils.createRowComparator(payload);
Thomas Vachuskae586b792015-03-26 13:59:38 -070092
Simon Huntd2747a02015-04-30 22:41:16 -070093 DeviceService service = get(DeviceService.class);
94 MastershipService mastershipService = get(MastershipService.class);
95 TableRow[] rows = generateTableRows(service, mastershipService);
96 Arrays.sort(rows, rc);
97 ObjectNode rootNode = MAPPER.createObjectNode();
98 rootNode.set("devices", TableUtils.generateArrayNode(rows));
Thomas Vachuskae586b792015-03-26 13:59:38 -070099
Simon Huntd2747a02015-04-30 22:41:16 -0700100 sendMessage("deviceDataResponse", 0, rootNode);
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700101 }
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700102
Simon Huntd2747a02015-04-30 22:41:16 -0700103 private TableRow[] generateTableRows(DeviceService service,
104 MastershipService mastershipService) {
105 List<TableRow> list = new ArrayList<>();
106 for (Device dev : service.getDevices()) {
107 list.add(new DeviceTableRow(service, mastershipService, dev));
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700108 }
Simon Huntd2747a02015-04-30 22:41:16 -0700109 return list.toArray(new TableRow[list.size()]);
110 }
111 }
112
113 // ======================================================================
114
115 private final class DetailRequestHandler extends RequestHandler {
116 private DetailRequestHandler() {
117 super(DEV_DETAIL_REQ);
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700118 }
119
Simon Huntd2747a02015-04-30 22:41:16 -0700120 @Override
121 public void process(long sid, ObjectNode payload) {
122 String id = string(payload, "id", "of:0000000000000000");
123
124 DeviceId deviceId = DeviceId.deviceId(id);
125 DeviceService service = get(DeviceService.class);
126 MastershipService ms = get(MastershipService.class);
127 Device device = service.getDevice(deviceId);
128 ObjectNode data = MAPPER.createObjectNode();
129
130 data.put(ID, deviceId.toString());
131 data.put(TYPE, device.type().toString());
132 data.put(TYPE_IID, getTypeIconId(device));
133 data.put(MFR, device.manufacturer());
134 data.put(HW, device.hwVersion());
135 data.put(SW, device.swVersion());
136 data.put(SERIAL, device.serialNumber());
137 data.put(CHASSIS_ID, device.chassisId().toString());
138 data.put(MASTER_ID, ms.getMasterFor(deviceId).toString());
139 data.put(PROTOCOL, device.annotations().value(PROTOCOL));
140
141 ArrayNode ports = MAPPER.createArrayNode();
142
143 List<Port> portList = new ArrayList<>(service.getPorts(deviceId));
144 Collections.sort(portList, (p1, p2) -> {
145 long delta = p1.number().toLong() - p2.number().toLong();
146 return delta == 0 ? 0 : (delta < 0 ? -1 : +1);
147 });
148
149 for (Port p : portList) {
150 ports.add(portData(p, deviceId));
151 }
152 data.set(PORTS, ports);
153
154 ObjectNode rootNode = MAPPER.createObjectNode();
155 rootNode.set("details", data);
156 sendMessage("deviceDetailsResponse", 0, rootNode);
157 }
158
159 private ObjectNode portData(Port p, DeviceId id) {
160 ObjectNode port = MAPPER.createObjectNode();
161 LinkService ls = get(LinkService.class);
162 String name = p.annotations().value(AnnotationKeys.PORT_NAME);
163
164 port.put(ID, p.number().toString());
165 port.put(TYPE, p.type().toString());
166 port.put(SPEED, p.portSpeed());
167 port.put(ENABLED, p.isEnabled());
168 port.put(NAME, name != null ? name : "");
169
170 Set<Link> links = ls.getEgressLinks(new ConnectPoint(id, p.number()));
171 if (!links.isEmpty()) {
172 StringBuilder egressLinks = new StringBuilder();
173 for (Link l : links) {
174 ConnectPoint dest = l.dst();
175 egressLinks.append(dest.elementId()).append("/")
176 .append(dest.port()).append(" ");
177 }
178 port.put(LINK_DEST, egressLinks.toString());
179 }
180
181 return port;
182 }
183
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700184 }
185
Simon Huntd2747a02015-04-30 22:41:16 -0700186
Bri Prebilic Coleb699a162015-04-13 12:01:39 -0700187 private static String getTypeIconId(Device d) {
188 return DEV_ICON_PREFIX + d.type().toString();
189 }
190
Thomas Vachuskae586b792015-03-26 13:59:38 -0700191 /**
192 * TableRow implementation for {@link Device devices}.
193 */
194 private static class DeviceTableRow extends AbstractTableRow {
195
Thomas Vachuskae586b792015-03-26 13:59:38 -0700196 private static final String[] COL_IDS = {
Bri Prebilic Cole264c5ec2015-04-07 10:22:26 -0700197 AVAILABLE, AVAILABLE_IID, TYPE_IID, ID,
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700198 NUM_PORTS, MASTER_ID, MFR, HW, SW,
199 PROTOCOL, CHASSIS_ID, SERIAL
Thomas Vachuskae586b792015-03-26 13:59:38 -0700200 };
201
Bri Prebilic Coleab582b82015-04-14 15:08:22 -0700202 private static final String ICON_ID_ONLINE = "active";
203 private static final String ICON_ID_OFFLINE = "inactive";
Thomas Vachuskae586b792015-03-26 13:59:38 -0700204
Bri Prebilic Coleae65c962015-04-02 16:24:49 -0700205 public DeviceTableRow(DeviceService service,
206 MastershipService ms,
207 Device d) {
Thomas Vachuskae586b792015-03-26 13:59:38 -0700208 boolean available = service.isAvailable(d.id());
209 String iconId = available ? ICON_ID_ONLINE : ICON_ID_OFFLINE;
Bri Prebilic Cole264c5ec2015-04-07 10:22:26 -0700210 DeviceId id = d.id();
211 List<Port> ports = service.getPorts(id);
Thomas Vachuskae586b792015-03-26 13:59:38 -0700212
Bri Prebilic Cole264c5ec2015-04-07 10:22:26 -0700213 add(ID, id.toString());
Thomas Vachuskae586b792015-03-26 13:59:38 -0700214 add(AVAILABLE, Boolean.toString(available));
215 add(AVAILABLE_IID, iconId);
216 add(TYPE_IID, getTypeIconId(d));
Thomas Vachuskae586b792015-03-26 13:59:38 -0700217 add(MFR, d.manufacturer());
218 add(HW, d.hwVersion());
219 add(SW, d.swVersion());
Thomas Vachuskae586b792015-03-26 13:59:38 -0700220 add(PROTOCOL, d.annotations().value(PROTOCOL));
Bri Prebilic Cole264c5ec2015-04-07 10:22:26 -0700221 add(NUM_PORTS, Integer.toString(ports.size()));
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700222 add(MASTER_ID, ms.getMasterFor(d.id()).toString());
Thomas Vachuskae586b792015-03-26 13:59:38 -0700223 }
224
Thomas Vachuskae586b792015-03-26 13:59:38 -0700225 @Override
226 protected String[] columnIds() {
227 return COL_IDS;
228 }
229 }
230
231}