blob: 7937c6b968527ce7d4dfd13913fd7a49c1afb9a6 [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;
Thomas Vachuskab52a0142015-04-21 17:48:15 -070023import org.onosproject.net.AnnotationKeys;
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -070024import org.onosproject.net.ConnectPoint;
Thomas Vachuskae586b792015-03-26 13:59:38 -070025import org.onosproject.net.Device;
Bri Prebilic Cole264c5ec2015-04-07 10:22:26 -070026import org.onosproject.net.DeviceId;
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -070027import org.onosproject.net.Link;
Bri Prebilic Cole264c5ec2015-04-07 10:22:26 -070028import org.onosproject.net.Port;
Thomas Vachuskae586b792015-03-26 13:59:38 -070029import org.onosproject.net.device.DeviceService;
Bri Prebilic Cole264c5ec2015-04-07 10:22:26 -070030import org.onosproject.net.link.LinkService;
Thomas Vachuskae586b792015-03-26 13:59:38 -070031
32import java.util.ArrayList;
33import java.util.Arrays;
Thomas Vachuskab52a0142015-04-21 17:48:15 -070034import java.util.Collections;
Thomas Vachuskae586b792015-03-26 13:59:38 -070035import java.util.List;
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -070036import java.util.Set;
Bri Prebilic Cole264c5ec2015-04-07 10:22:26 -070037
Thomas Vachuskae586b792015-03-26 13:59:38 -070038/**
39 * Message handler for device view related messages.
40 */
41public class DeviceViewMessageHandler extends AbstractTabularViewMessageHandler {
42
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -070043 private static final String ID = "id";
44 private static final String TYPE = "type";
45 private static final String AVAILABLE = "available";
46 private static final String AVAILABLE_IID = "_iconid_available";
47 private static final String TYPE_IID = "_iconid_type";
48 private static final String DEV_ICON_PREFIX = "devIcon_";
49 private static final String NUM_PORTS = "num_ports";
50 private static final String LINK_DEST = "elinks_dest";
51 private static final String MFR = "mfr";
52 private static final String HW = "hw";
53 private static final String SW = "sw";
54 private static final String PROTOCOL = "protocol";
55 private static final String MASTER_ID = "masterid";
56 private static final String CHASSIS_ID = "chassisid";
57 private static final String SERIAL = "serial";
58 private static final String PORTS = "ports";
59 private static final String ENABLED = "enabled";
60 private static final String SPEED = "speed";
Thomas Vachuskab52a0142015-04-21 17:48:15 -070061 private static final String NAME = "name";
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -070062
63 private static final ObjectMapper MAPPER = new ObjectMapper();
64
65
Thomas Vachuskae586b792015-03-26 13:59:38 -070066 /**
67 * Creates a new message handler for the device messages.
68 */
69 protected DeviceViewMessageHandler() {
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -070070 super(ImmutableSet.of("deviceDataRequest", "deviceDetailsRequest"));
Thomas Vachuskae586b792015-03-26 13:59:38 -070071 }
72
73 @Override
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -070074 public void process(ObjectNode event) {
75 String type = string(event, "event", "unknown");
76 if (type.equals("deviceDataRequest")) {
77 dataRequest(event);
78 } else if (type.equals("deviceDetailsRequest")) {
79 detailsRequest(event);
80 }
81 }
82
83 private void dataRequest(ObjectNode event) {
84 ObjectNode payload = payload(event);
Thomas Vachuskae586b792015-03-26 13:59:38 -070085 String sortCol = string(payload, "sortCol", "id");
86 String sortDir = string(payload, "sortDir", "asc");
87
88 DeviceService service = get(DeviceService.class);
Bri Prebilic Coleae65c962015-04-02 16:24:49 -070089 MastershipService mastershipService = get(MastershipService.class);
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -070090
Bri Prebilic Coleb699a162015-04-13 12:01:39 -070091 TableRow[] rows = generateTableRows(service, mastershipService);
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());
Bri Prebilic Coleb699a162015-04-13 12:01:39 -0700114 data.put(TYPE_IID, getTypeIconId(device));
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700115 data.put(MFR, device.manufacturer());
116 data.put(HW, device.hwVersion());
117 data.put(SW, device.swVersion());
118 data.put(SERIAL, device.serialNumber());
119 data.put(CHASSIS_ID, device.chassisId().toString());
120 data.put(MASTER_ID, ms.getMasterFor(deviceId).toString());
121 data.put(PROTOCOL, device.annotations().value(PROTOCOL));
122
123 ArrayNode ports = MAPPER.createArrayNode();
Thomas Vachuskab52a0142015-04-21 17:48:15 -0700124
125 List<Port> portList = new ArrayList<>(service.getPorts(deviceId));
126 Collections.sort(portList, (p1, p2) -> {
127 long delta = p1.number().toLong() - p2.number().toLong();
128 return delta == 0 ? 0 : (delta < 0 ? -1 : +1);
129 });
130
131 for (Port p : portList) {
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700132 ports.add(portData(p, deviceId));
133 }
134 data.set(PORTS, ports);
135
136 ObjectNode rootNode = mapper.createObjectNode();
137 rootNode.set("details", data);
138 connection().sendMessage("deviceDetailsResponse", 0, rootNode);
139 }
140
Bri Prebilic Coleae65c962015-04-02 16:24:49 -0700141 private TableRow[] generateTableRows(DeviceService service,
Bri Prebilic Coleb699a162015-04-13 12:01:39 -0700142 MastershipService mastershipService) {
Thomas Vachuskae586b792015-03-26 13:59:38 -0700143 List<TableRow> list = new ArrayList<>();
144 for (Device dev : service.getDevices()) {
Bri Prebilic Cole264c5ec2015-04-07 10:22:26 -0700145 list.add(new DeviceTableRow(service,
146 mastershipService,
Bri Prebilic Cole264c5ec2015-04-07 10:22:26 -0700147 dev));
Thomas Vachuskae586b792015-03-26 13:59:38 -0700148 }
149 return list.toArray(new TableRow[list.size()]);
150 }
151
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700152 private ObjectNode portData(Port p, DeviceId id) {
153 ObjectNode port = MAPPER.createObjectNode();
154 LinkService ls = get(LinkService.class);
Thomas Vachuskab52a0142015-04-21 17:48:15 -0700155 String name = p.annotations().value(AnnotationKeys.PORT_NAME);
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700156
157 port.put(ID, p.number().toString());
158 port.put(TYPE, p.type().toString());
159 port.put(SPEED, p.portSpeed());
160 port.put(ENABLED, p.isEnabled());
Thomas Vachuskab52a0142015-04-21 17:48:15 -0700161 port.put(NAME, name != null ? name : "");
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700162
163 Set<Link> links = ls.getEgressLinks(new ConnectPoint(id, p.number()));
164 if (!links.isEmpty()) {
165 String egressLinks = "";
166 for (Link l : links) {
167 ConnectPoint dest = l.dst();
168 egressLinks += dest.elementId().toString()
169 + "/" + dest.port().toString() + " ";
170 }
171 port.put(LINK_DEST, egressLinks);
172 }
173
174 return port;
175 }
176
Bri Prebilic Coleb699a162015-04-13 12:01:39 -0700177 private static String getTypeIconId(Device d) {
178 return DEV_ICON_PREFIX + d.type().toString();
179 }
180
Thomas Vachuskae586b792015-03-26 13:59:38 -0700181 /**
182 * TableRow implementation for {@link Device devices}.
183 */
184 private static class DeviceTableRow extends AbstractTableRow {
185
Thomas Vachuskae586b792015-03-26 13:59:38 -0700186 private static final String[] COL_IDS = {
Bri Prebilic Cole264c5ec2015-04-07 10:22:26 -0700187 AVAILABLE, AVAILABLE_IID, TYPE_IID, ID,
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700188 NUM_PORTS, MASTER_ID, MFR, HW, SW,
189 PROTOCOL, CHASSIS_ID, SERIAL
Thomas Vachuskae586b792015-03-26 13:59:38 -0700190 };
191
Bri Prebilic Coleab582b82015-04-14 15:08:22 -0700192 private static final String ICON_ID_ONLINE = "active";
193 private static final String ICON_ID_OFFLINE = "inactive";
Thomas Vachuskae586b792015-03-26 13:59:38 -0700194
Bri Prebilic Coleae65c962015-04-02 16:24:49 -0700195 public DeviceTableRow(DeviceService service,
196 MastershipService ms,
197 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
Thomas Vachuskae586b792015-03-26 13:59:38 -0700215 @Override
216 protected String[] columnIds() {
217 return COL_IDS;
218 }
219 }
220
221}