blob: b7fb40d5ac1464f89ba8baea6296b956509ec33b [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
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -070018import com.fasterxml.jackson.databind.ObjectMapper;
Thomas Vachuskae586b792015-03-26 13:59:38 -070019import com.fasterxml.jackson.databind.node.ArrayNode;
20import com.fasterxml.jackson.databind.node.ObjectNode;
21import com.google.common.collect.ImmutableSet;
Bri Prebilic Coleae65c962015-04-02 16:24:49 -070022import org.onosproject.mastership.MastershipService;
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;
Thomas Vachuskae586b792015-03-26 13:59:38 -070030
31import java.util.ArrayList;
32import java.util.Arrays;
33import java.util.List;
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -070034import java.util.Set;
Bri Prebilic Cole264c5ec2015-04-07 10:22:26 -070035
Thomas Vachuskae586b792015-03-26 13:59:38 -070036/**
37 * Message handler for device view related messages.
38 */
39public class DeviceViewMessageHandler extends AbstractTabularViewMessageHandler {
40
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -070041 private static final String ID = "id";
42 private static final String TYPE = "type";
43 private static final String AVAILABLE = "available";
44 private static final String AVAILABLE_IID = "_iconid_available";
45 private static final String TYPE_IID = "_iconid_type";
46 private static final String DEV_ICON_PREFIX = "devIcon_";
47 private static final String NUM_PORTS = "num_ports";
48 private static final String LINK_DEST = "elinks_dest";
49 private static final String MFR = "mfr";
50 private static final String HW = "hw";
51 private static final String SW = "sw";
52 private static final String PROTOCOL = "protocol";
53 private static final String MASTER_ID = "masterid";
54 private static final String CHASSIS_ID = "chassisid";
55 private static final String SERIAL = "serial";
56 private static final String PORTS = "ports";
57 private static final String ENABLED = "enabled";
58 private static final String SPEED = "speed";
59
60 private static final ObjectMapper MAPPER = new ObjectMapper();
61
62
Thomas Vachuskae586b792015-03-26 13:59:38 -070063 /**
64 * Creates a new message handler for the device messages.
65 */
66 protected DeviceViewMessageHandler() {
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -070067 super(ImmutableSet.of("deviceDataRequest", "deviceDetailsRequest"));
Thomas Vachuskae586b792015-03-26 13:59:38 -070068 }
69
70 @Override
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -070071 public void process(ObjectNode event) {
72 String type = string(event, "event", "unknown");
73 if (type.equals("deviceDataRequest")) {
74 dataRequest(event);
75 } else if (type.equals("deviceDetailsRequest")) {
76 detailsRequest(event);
77 }
78 }
79
80 private void dataRequest(ObjectNode event) {
81 ObjectNode payload = payload(event);
Thomas Vachuskae586b792015-03-26 13:59:38 -070082 String sortCol = string(payload, "sortCol", "id");
83 String sortDir = string(payload, "sortDir", "asc");
84
85 DeviceService service = get(DeviceService.class);
Bri Prebilic Coleae65c962015-04-02 16:24:49 -070086 MastershipService mastershipService = get(MastershipService.class);
Bri Prebilic Cole264c5ec2015-04-07 10:22:26 -070087 LinkService linkService = get(LinkService.class);
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -070088
Bri Prebilic Cole264c5ec2015-04-07 10:22:26 -070089 TableRow[] rows = generateTableRows(service,
90 mastershipService,
91 linkService);
Bri Prebilic Coleae65c962015-04-02 16:24:49 -070092 RowComparator rc =
93 new RowComparator(sortCol, RowComparator.direction(sortDir));
Thomas Vachuskae586b792015-03-26 13:59:38 -070094 Arrays.sort(rows, rc);
95 ArrayNode devices = generateArrayNode(rows);
96 ObjectNode rootNode = mapper.createObjectNode();
97 rootNode.set("devices", devices);
98
99 connection().sendMessage("deviceDataResponse", 0, rootNode);
100 }
101
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700102 private void detailsRequest(ObjectNode event) {
103 ObjectNode payload = payload(event);
104 String id = string(payload, "id", "of:0000000000000000");
105
106 DeviceId deviceId = DeviceId.deviceId(id);
107 DeviceService service = get(DeviceService.class);
108 MastershipService ms = get(MastershipService.class);
109 Device device = service.getDevice(deviceId);
110 ObjectNode data = MAPPER.createObjectNode();
111
112 data.put(ID, deviceId.toString());
113 data.put(TYPE, device.type().toString());
114 data.put(MFR, device.manufacturer());
115 data.put(HW, device.hwVersion());
116 data.put(SW, device.swVersion());
117 data.put(SERIAL, device.serialNumber());
118 data.put(CHASSIS_ID, device.chassisId().toString());
119 data.put(MASTER_ID, ms.getMasterFor(deviceId).toString());
120 data.put(PROTOCOL, device.annotations().value(PROTOCOL));
121
122 ArrayNode ports = MAPPER.createArrayNode();
123 for (Port p : service.getPorts(deviceId)) {
124 ports.add(portData(p, deviceId));
125 }
126 data.set(PORTS, ports);
127
128 ObjectNode rootNode = mapper.createObjectNode();
129 rootNode.set("details", data);
130 connection().sendMessage("deviceDetailsResponse", 0, rootNode);
131 }
132
Bri Prebilic Coleae65c962015-04-02 16:24:49 -0700133 private TableRow[] generateTableRows(DeviceService service,
Bri Prebilic Cole264c5ec2015-04-07 10:22:26 -0700134 MastershipService mastershipService,
135 LinkService linkService) {
Thomas Vachuskae586b792015-03-26 13:59:38 -0700136 List<TableRow> list = new ArrayList<>();
137 for (Device dev : service.getDevices()) {
Bri Prebilic Cole264c5ec2015-04-07 10:22:26 -0700138 list.add(new DeviceTableRow(service,
139 mastershipService,
140 linkService,
141 dev));
Thomas Vachuskae586b792015-03-26 13:59:38 -0700142 }
143 return list.toArray(new TableRow[list.size()]);
144 }
145
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700146 private ObjectNode portData(Port p, DeviceId id) {
147 ObjectNode port = MAPPER.createObjectNode();
148 LinkService ls = get(LinkService.class);
149
150 port.put(ID, p.number().toString());
151 port.put(TYPE, p.type().toString());
152 port.put(SPEED, p.portSpeed());
153 port.put(ENABLED, p.isEnabled());
154
155 Set<Link> links = ls.getEgressLinks(new ConnectPoint(id, p.number()));
156 if (!links.isEmpty()) {
157 String egressLinks = "";
158 for (Link l : links) {
159 ConnectPoint dest = l.dst();
160 egressLinks += dest.elementId().toString()
161 + "/" + dest.port().toString() + " ";
162 }
163 port.put(LINK_DEST, egressLinks);
164 }
165
166 return port;
167 }
168
Thomas Vachuskae586b792015-03-26 13:59:38 -0700169 /**
170 * TableRow implementation for {@link Device devices}.
171 */
172 private static class DeviceTableRow extends AbstractTableRow {
173
Thomas Vachuskae586b792015-03-26 13:59:38 -0700174 private static final String[] COL_IDS = {
Bri Prebilic Cole264c5ec2015-04-07 10:22:26 -0700175 AVAILABLE, AVAILABLE_IID, TYPE_IID, ID,
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700176 NUM_PORTS, MASTER_ID, MFR, HW, SW,
177 PROTOCOL, CHASSIS_ID, SERIAL
Thomas Vachuskae586b792015-03-26 13:59:38 -0700178 };
179
180 private static final String ICON_ID_ONLINE = "deviceOnline";
181 private static final String ICON_ID_OFFLINE = "deviceOffline";
182
Bri Prebilic Cole264c5ec2015-04-07 10:22:26 -0700183 // TODO: use in details pane
Bri Prebilic Cole264c5ec2015-04-07 10:22:26 -0700184// private String getEgressLinks(Set<Link> links) {
185// String formattedString = "";
186//
187// for (Link l : links) {
188// formattedString += l.dst().port().toString() + ", ";
189// }
190// return formattedString;
191// }
192
193 // TODO: include "extra" backend information in device details pane
Bri Prebilic Coleae65c962015-04-02 16:24:49 -0700194 public DeviceTableRow(DeviceService service,
195 MastershipService ms,
Bri Prebilic Cole264c5ec2015-04-07 10:22:26 -0700196 LinkService ls,
Bri Prebilic Coleae65c962015-04-02 16:24:49 -0700197 Device d) {
Thomas Vachuskae586b792015-03-26 13:59:38 -0700198 boolean available = service.isAvailable(d.id());
199 String iconId = available ? ICON_ID_ONLINE : ICON_ID_OFFLINE;
Bri Prebilic Cole264c5ec2015-04-07 10:22:26 -0700200 DeviceId id = d.id();
201 List<Port> ports = service.getPorts(id);
Thomas Vachuskae586b792015-03-26 13:59:38 -0700202
Bri Prebilic Cole264c5ec2015-04-07 10:22:26 -0700203 add(ID, id.toString());
Thomas Vachuskae586b792015-03-26 13:59:38 -0700204 add(AVAILABLE, Boolean.toString(available));
205 add(AVAILABLE_IID, iconId);
206 add(TYPE_IID, getTypeIconId(d));
Thomas Vachuskae586b792015-03-26 13:59:38 -0700207 add(MFR, d.manufacturer());
208 add(HW, d.hwVersion());
209 add(SW, d.swVersion());
Thomas Vachuskae586b792015-03-26 13:59:38 -0700210 add(PROTOCOL, d.annotations().value(PROTOCOL));
Bri Prebilic Cole264c5ec2015-04-07 10:22:26 -0700211 add(NUM_PORTS, Integer.toString(ports.size()));
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700212 add(MASTER_ID, ms.getMasterFor(d.id()).toString());
Thomas Vachuskae586b792015-03-26 13:59:38 -0700213 }
214
215 private String getTypeIconId(Device d) {
216 return DEV_ICON_PREFIX + d.type().toString();
217 }
218
219 @Override
220 protected String[] columnIds() {
221 return COL_IDS;
222 }
223 }
224
225}