blob: 67a9ee399c4984d0beef7b7e52509bbc4c9e8743 [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 Cole0feedc02015-04-09 14:17:37 -070087
Bri Prebilic Coleb699a162015-04-13 12:01:39 -070088 TableRow[] rows = generateTableRows(service, mastershipService);
Bri Prebilic Coleae65c962015-04-02 16:24:49 -070089 RowComparator rc =
90 new RowComparator(sortCol, RowComparator.direction(sortDir));
Thomas Vachuskae586b792015-03-26 13:59:38 -070091 Arrays.sort(rows, rc);
92 ArrayNode devices = generateArrayNode(rows);
93 ObjectNode rootNode = mapper.createObjectNode();
94 rootNode.set("devices", devices);
95
96 connection().sendMessage("deviceDataResponse", 0, rootNode);
97 }
98
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -070099 private void detailsRequest(ObjectNode event) {
100 ObjectNode payload = payload(event);
101 String id = string(payload, "id", "of:0000000000000000");
102
103 DeviceId deviceId = DeviceId.deviceId(id);
104 DeviceService service = get(DeviceService.class);
105 MastershipService ms = get(MastershipService.class);
106 Device device = service.getDevice(deviceId);
107 ObjectNode data = MAPPER.createObjectNode();
108
109 data.put(ID, deviceId.toString());
110 data.put(TYPE, device.type().toString());
Bri Prebilic Coleb699a162015-04-13 12:01:39 -0700111 data.put(TYPE_IID, getTypeIconId(device));
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700112 data.put(MFR, device.manufacturer());
113 data.put(HW, device.hwVersion());
114 data.put(SW, device.swVersion());
115 data.put(SERIAL, device.serialNumber());
116 data.put(CHASSIS_ID, device.chassisId().toString());
117 data.put(MASTER_ID, ms.getMasterFor(deviceId).toString());
118 data.put(PROTOCOL, device.annotations().value(PROTOCOL));
119
120 ArrayNode ports = MAPPER.createArrayNode();
121 for (Port p : service.getPorts(deviceId)) {
122 ports.add(portData(p, deviceId));
123 }
124 data.set(PORTS, ports);
125
126 ObjectNode rootNode = mapper.createObjectNode();
127 rootNode.set("details", data);
128 connection().sendMessage("deviceDetailsResponse", 0, rootNode);
129 }
130
Bri Prebilic Coleae65c962015-04-02 16:24:49 -0700131 private TableRow[] generateTableRows(DeviceService service,
Bri Prebilic Coleb699a162015-04-13 12:01:39 -0700132 MastershipService mastershipService) {
Thomas Vachuskae586b792015-03-26 13:59:38 -0700133 List<TableRow> list = new ArrayList<>();
134 for (Device dev : service.getDevices()) {
Bri Prebilic Cole264c5ec2015-04-07 10:22:26 -0700135 list.add(new DeviceTableRow(service,
136 mastershipService,
Bri Prebilic Cole264c5ec2015-04-07 10:22:26 -0700137 dev));
Thomas Vachuskae586b792015-03-26 13:59:38 -0700138 }
139 return list.toArray(new TableRow[list.size()]);
140 }
141
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700142 private ObjectNode portData(Port p, DeviceId id) {
143 ObjectNode port = MAPPER.createObjectNode();
144 LinkService ls = get(LinkService.class);
145
146 port.put(ID, p.number().toString());
147 port.put(TYPE, p.type().toString());
148 port.put(SPEED, p.portSpeed());
149 port.put(ENABLED, p.isEnabled());
150
151 Set<Link> links = ls.getEgressLinks(new ConnectPoint(id, p.number()));
152 if (!links.isEmpty()) {
153 String egressLinks = "";
154 for (Link l : links) {
155 ConnectPoint dest = l.dst();
156 egressLinks += dest.elementId().toString()
157 + "/" + dest.port().toString() + " ";
158 }
159 port.put(LINK_DEST, egressLinks);
160 }
161
162 return port;
163 }
164
Bri Prebilic Coleb699a162015-04-13 12:01:39 -0700165 private static String getTypeIconId(Device d) {
166 return DEV_ICON_PREFIX + d.type().toString();
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
Bri Prebilic Coleab582b82015-04-14 15:08:22 -0700180 private static final String ICON_ID_ONLINE = "active";
181 private static final String ICON_ID_OFFLINE = "inactive";
Thomas Vachuskae586b792015-03-26 13:59:38 -0700182
Bri Prebilic Coleae65c962015-04-02 16:24:49 -0700183 public DeviceTableRow(DeviceService service,
184 MastershipService ms,
185 Device d) {
Thomas Vachuskae586b792015-03-26 13:59:38 -0700186 boolean available = service.isAvailable(d.id());
187 String iconId = available ? ICON_ID_ONLINE : ICON_ID_OFFLINE;
Bri Prebilic Cole264c5ec2015-04-07 10:22:26 -0700188 DeviceId id = d.id();
189 List<Port> ports = service.getPorts(id);
Thomas Vachuskae586b792015-03-26 13:59:38 -0700190
Bri Prebilic Cole264c5ec2015-04-07 10:22:26 -0700191 add(ID, id.toString());
Thomas Vachuskae586b792015-03-26 13:59:38 -0700192 add(AVAILABLE, Boolean.toString(available));
193 add(AVAILABLE_IID, iconId);
194 add(TYPE_IID, getTypeIconId(d));
Thomas Vachuskae586b792015-03-26 13:59:38 -0700195 add(MFR, d.manufacturer());
196 add(HW, d.hwVersion());
197 add(SW, d.swVersion());
Thomas Vachuskae586b792015-03-26 13:59:38 -0700198 add(PROTOCOL, d.annotations().value(PROTOCOL));
Bri Prebilic Cole264c5ec2015-04-07 10:22:26 -0700199 add(NUM_PORTS, Integer.toString(ports.size()));
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700200 add(MASTER_ID, ms.getMasterFor(d.id()).toString());
Thomas Vachuskae586b792015-03-26 13:59:38 -0700201 }
202
Thomas Vachuskae586b792015-03-26 13:59:38 -0700203 @Override
204 protected String[] columnIds() {
205 return COL_IDS;
206 }
207 }
208
209}