blob: 5a5b42e4e5376efd9ab6b7fcfa6f768e5d7a8e44 [file] [log] [blame]
Thomas Vachuskae586b792015-03-26 13:59:38 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Thomas Vachuskae586b792015-03-26 13:59:38 -07003 *
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;
Thomas Vachuskac4178cc2015-12-10 11:43:32 -080021import org.onosproject.cluster.NodeId;
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;
Thomas Vachuska018110c2016-07-05 12:21:38 -070027import org.onosproject.net.Host;
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -070028import org.onosproject.net.Link;
Bri Prebilic Cole264c5ec2015-04-07 10:22:26 -070029import org.onosproject.net.Port;
Thomas Vachuska8f62f572015-10-06 12:17:44 -070030import org.onosproject.net.config.NetworkConfigService;
31import org.onosproject.net.config.basics.BasicDeviceConfig;
Thomas Vachuskae586b792015-03-26 13:59:38 -070032import org.onosproject.net.device.DeviceService;
Thomas Vachuska018110c2016-07-05 12:21:38 -070033import org.onosproject.net.host.HostService;
Bri Prebilic Cole264c5ec2015-04-07 10:22:26 -070034import org.onosproject.net.link.LinkService;
Simon Huntd2747a02015-04-30 22:41:16 -070035import org.onosproject.ui.RequestHandler;
Simon Hunta0ddb022015-05-01 09:53:01 -070036import org.onosproject.ui.UiMessageHandler;
Simon Hunt3d1b0652015-05-05 17:27:24 -070037import org.onosproject.ui.table.TableModel;
Simon Huntabd16f62015-05-01 13:14:40 -070038import org.onosproject.ui.table.TableRequestHandler;
Simon Huntd5579252015-10-06 15:09:14 -070039import org.slf4j.Logger;
40import org.slf4j.LoggerFactory;
Thomas Vachuskae586b792015-03-26 13:59:38 -070041
42import java.util.ArrayList;
Simon Huntd2747a02015-04-30 22:41:16 -070043import java.util.Collection;
Thomas Vachuskae586b792015-03-26 13:59:38 -070044import java.util.List;
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -070045import java.util.Set;
Bri Prebilic Cole264c5ec2015-04-07 10:22:26 -070046
Thomas Vachuska8f62f572015-10-06 12:17:44 -070047import static com.google.common.base.Strings.emptyToNull;
48import static com.google.common.base.Strings.isNullOrEmpty;
Bri Prebilic Cole9467a232015-05-06 16:59:05 -070049import static org.apache.commons.lang.WordUtils.capitalizeFully;
Thomas Vachuska8f62f572015-10-06 12:17:44 -070050import static org.onosproject.net.DeviceId.deviceId;
Bri Prebilic Cole9467a232015-05-06 16:59:05 -070051
Thomas Vachuskae586b792015-03-26 13:59:38 -070052/**
53 * Message handler for device view related messages.
54 */
Simon Hunta0ddb022015-05-01 09:53:01 -070055public class DeviceViewMessageHandler extends UiMessageHandler {
Simon Huntd2747a02015-04-30 22:41:16 -070056
57 private static final String DEV_DATA_REQ = "deviceDataRequest";
Simon Huntabd16f62015-05-01 13:14:40 -070058 private static final String DEV_DATA_RESP = "deviceDataResponse";
59 private static final String DEVICES = "devices";
Alan Deikman8d858752017-02-14 18:08:29 -080060 private static final String DEVICE = "device";
Simon Huntabd16f62015-05-01 13:14:40 -070061
62 private static final String DEV_DETAILS_REQ = "deviceDetailsRequest";
63 private static final String DEV_DETAILS_RESP = "deviceDetailsResponse";
64 private static final String DETAILS = "details";
Thomas Vachuskae586b792015-03-26 13:59:38 -070065
Thomas Vachuska8f62f572015-10-06 12:17:44 -070066 private static final String DEV_NAME_CHANGE_REQ = "deviceNameChangeRequest";
67 private static final String DEV_NAME_CHANGE_RESP = "deviceNameChangeResponse";
68
Simon Huntd5579252015-10-06 15:09:14 -070069 private static final String ZERO_URI = "of:0000000000000000";
70
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -070071 private static final String ID = "id";
72 private static final String TYPE = "type";
73 private static final String AVAILABLE = "available";
74 private static final String AVAILABLE_IID = "_iconid_available";
75 private static final String TYPE_IID = "_iconid_type";
76 private static final String DEV_ICON_PREFIX = "devIcon_";
77 private static final String NUM_PORTS = "num_ports";
78 private static final String LINK_DEST = "elinks_dest";
79 private static final String MFR = "mfr";
80 private static final String HW = "hw";
81 private static final String SW = "sw";
82 private static final String PROTOCOL = "protocol";
83 private static final String MASTER_ID = "masterid";
84 private static final String CHASSIS_ID = "chassisid";
85 private static final String SERIAL = "serial";
86 private static final String PORTS = "ports";
87 private static final String ENABLED = "enabled";
88 private static final String SPEED = "speed";
Thomas Vachuskab52a0142015-04-21 17:48:15 -070089 private static final String NAME = "name";
Simon Huntd5579252015-10-06 15:09:14 -070090 private static final String WARN = "warn";
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -070091
Thomas Vachuskac4178cc2015-12-10 11:43:32 -080092 private static final String NONE = "none";
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -070093
Simon Hunt3d1b0652015-05-05 17:27:24 -070094 private static final String[] COL_IDS = {
Simon Huntd5579252015-10-06 15:09:14 -070095 AVAILABLE, AVAILABLE_IID, TYPE_IID,
96 NAME, ID, MASTER_ID, NUM_PORTS, MFR, HW, SW,
Simon Hunt3d1b0652015-05-05 17:27:24 -070097 PROTOCOL, CHASSIS_ID, SERIAL
98 };
99
100 private static final String ICON_ID_ONLINE = "active";
101 private static final String ICON_ID_OFFLINE = "inactive";
102
Simon Huntd5579252015-10-06 15:09:14 -0700103 private final Logger log = LoggerFactory.getLogger(getClass());
104
105
Thomas Vachuskae586b792015-03-26 13:59:38 -0700106 @Override
Simon Huntda580882015-05-12 20:58:18 -0700107 protected Collection<RequestHandler> createRequestHandlers() {
Simon Huntd2747a02015-04-30 22:41:16 -0700108 return ImmutableSet.of(
109 new DataRequestHandler(),
Thomas Vachuska8f62f572015-10-06 12:17:44 -0700110 new NameChangeHandler(),
Simon Huntd2747a02015-04-30 22:41:16 -0700111 new DetailRequestHandler()
112 );
113 }
114
Simon Huntd5579252015-10-06 15:09:14 -0700115 // Get friendly name of the device from the annotations
116 private static String deviceName(Device device) {
117 String name = device.annotations().value(AnnotationKeys.NAME);
118 return isNullOrEmpty(name) ? device.id().toString() : name;
119 }
120
121 private static String deviceProtocol(Device device) {
122 String protocol = device.annotations().value(PROTOCOL);
123 return protocol != null ? protocol : "";
124 }
125
Simon Hunt3d1b0652015-05-05 17:27:24 -0700126 private static String getTypeIconId(Device d) {
127 return DEV_ICON_PREFIX + d.type().toString();
128 }
129
Simon Huntabd16f62015-05-01 13:14:40 -0700130 // handler for device table requests
131 private final class DataRequestHandler extends TableRequestHandler {
Jian Li69f66632016-01-15 12:27:42 -0800132 private static final String NO_ROWS_MESSAGE = "No devices found";
133
Simon Huntd2747a02015-04-30 22:41:16 -0700134 private DataRequestHandler() {
Simon Huntabd16f62015-05-01 13:14:40 -0700135 super(DEV_DATA_REQ, DEV_DATA_RESP, DEVICES);
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700136 }
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700137
Simon Huntd2747a02015-04-30 22:41:16 -0700138 @Override
Simon Hunt3d1b0652015-05-05 17:27:24 -0700139 protected String[] getColumnIds() {
140 return COL_IDS;
141 }
142
143 @Override
Jian Li8baf4472016-01-15 15:08:09 -0800144 protected String noRowsMessage(ObjectNode payload) {
Jian Li69f66632016-01-15 12:27:42 -0800145 return NO_ROWS_MESSAGE;
146 }
147
148 @Override
Simon Hunt3d1b0652015-05-05 17:27:24 -0700149 protected void populateTable(TableModel tm, ObjectNode payload) {
150 DeviceService ds = get(DeviceService.class);
151 MastershipService ms = get(MastershipService.class);
152 for (Device dev : ds.getDevices()) {
153 populateRow(tm.addRow(), dev, ds, ms);
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700154 }
Simon Hunt3d1b0652015-05-05 17:27:24 -0700155 }
156
157 private void populateRow(TableModel.Row row, Device dev,
158 DeviceService ds, MastershipService ms) {
159 DeviceId id = dev.id();
160 boolean available = ds.isAvailable(id);
161 String iconId = available ? ICON_ID_ONLINE : ICON_ID_OFFLINE;
162
163 row.cell(ID, id)
Simon Huntd5579252015-10-06 15:09:14 -0700164 .cell(NAME, deviceName(dev))
Simon Hunt3d1b0652015-05-05 17:27:24 -0700165 .cell(AVAILABLE, available)
166 .cell(AVAILABLE_IID, iconId)
167 .cell(TYPE_IID, getTypeIconId(dev))
168 .cell(MFR, dev.manufacturer())
169 .cell(HW, dev.hwVersion())
170 .cell(SW, dev.swVersion())
Simon Huntd5579252015-10-06 15:09:14 -0700171 .cell(PROTOCOL, deviceProtocol(dev))
Simon Hunt3d1b0652015-05-05 17:27:24 -0700172 .cell(NUM_PORTS, ds.getPorts(id).size())
173 .cell(MASTER_ID, ms.getMasterFor(id));
Simon Huntd2747a02015-04-30 22:41:16 -0700174 }
175 }
176
Simon Huntabd16f62015-05-01 13:14:40 -0700177 // handler for selected device detail requests
Simon Huntd2747a02015-04-30 22:41:16 -0700178 private final class DetailRequestHandler extends RequestHandler {
179 private DetailRequestHandler() {
Simon Huntabd16f62015-05-01 13:14:40 -0700180 super(DEV_DETAILS_REQ);
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700181 }
182
Simon Huntd2747a02015-04-30 22:41:16 -0700183 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800184 public void process(ObjectNode payload) {
Simon Huntd5579252015-10-06 15:09:14 -0700185 String id = string(payload, ID, ZERO_URI);
Simon Huntd2747a02015-04-30 22:41:16 -0700186
Thomas Vachuska8f62f572015-10-06 12:17:44 -0700187 DeviceId deviceId = deviceId(id);
Simon Huntd2747a02015-04-30 22:41:16 -0700188 DeviceService service = get(DeviceService.class);
189 MastershipService ms = get(MastershipService.class);
190 Device device = service.getDevice(deviceId);
Simon Huntd5579252015-10-06 15:09:14 -0700191 ObjectNode data = objectNode();
Thomas Vachuskac4178cc2015-12-10 11:43:32 -0800192 NodeId masterFor = ms.getMasterFor(deviceId);
Simon Huntd2747a02015-04-30 22:41:16 -0700193
194 data.put(ID, deviceId.toString());
Simon Huntd5579252015-10-06 15:09:14 -0700195 data.put(NAME, deviceName(device));
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700196 data.put(TYPE, capitalizeFully(device.type().toString()));
Simon Huntd2747a02015-04-30 22:41:16 -0700197 data.put(TYPE_IID, getTypeIconId(device));
198 data.put(MFR, device.manufacturer());
199 data.put(HW, device.hwVersion());
200 data.put(SW, device.swVersion());
201 data.put(SERIAL, device.serialNumber());
202 data.put(CHASSIS_ID, device.chassisId().toString());
Thomas Vachuskac4178cc2015-12-10 11:43:32 -0800203 data.put(MASTER_ID, masterFor != null ? masterFor.toString() : NONE);
Simon Huntd5579252015-10-06 15:09:14 -0700204 data.put(PROTOCOL, deviceProtocol(device));
Simon Huntd2747a02015-04-30 22:41:16 -0700205
Simon Huntd5579252015-10-06 15:09:14 -0700206 ArrayNode ports = arrayNode();
Simon Huntd2747a02015-04-30 22:41:16 -0700207
208 List<Port> portList = new ArrayList<>(service.getPorts(deviceId));
Simon Hunt8a0429a2017-01-06 16:52:47 -0800209 portList.sort((p1, p2) -> {
Simon Huntd2747a02015-04-30 22:41:16 -0700210 long delta = p1.number().toLong() - p2.number().toLong();
211 return delta == 0 ? 0 : (delta < 0 ? -1 : +1);
212 });
213
214 for (Port p : portList) {
215 ports.add(portData(p, deviceId));
216 }
217 data.set(PORTS, ports);
218
Simon Huntd5579252015-10-06 15:09:14 -0700219 ObjectNode rootNode = objectNode();
Simon Huntabd16f62015-05-01 13:14:40 -0700220 rootNode.set(DETAILS, data);
Alan Deikman8d858752017-02-14 18:08:29 -0800221
Simon Huntcc035c52017-02-22 21:12:51 -0800222 // NOTE: ... an alternate way of getting all the details of an item:
223 // Use the codec context to get a JSON of the device. See ONOS-5976.
Alan Deikman8d858752017-02-14 18:08:29 -0800224 rootNode.set(DEVICE, getJsonCodecContext().encode(device, Device.class));
225
Simon Hunt8a0429a2017-01-06 16:52:47 -0800226 sendMessage(DEV_DETAILS_RESP, rootNode);
Simon Huntd2747a02015-04-30 22:41:16 -0700227 }
228
229 private ObjectNode portData(Port p, DeviceId id) {
Simon Huntd5579252015-10-06 15:09:14 -0700230 ObjectNode port = objectNode();
Simon Huntd2747a02015-04-30 22:41:16 -0700231 LinkService ls = get(LinkService.class);
232 String name = p.annotations().value(AnnotationKeys.PORT_NAME);
233
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700234 port.put(ID, capitalizeFully(p.number().toString()));
235 port.put(TYPE, capitalizeFully(p.type().toString()));
Simon Huntd2747a02015-04-30 22:41:16 -0700236 port.put(SPEED, p.portSpeed());
237 port.put(ENABLED, p.isEnabled());
238 port.put(NAME, name != null ? name : "");
239
Thomas Vachuska018110c2016-07-05 12:21:38 -0700240 ConnectPoint connectPoint = new ConnectPoint(id, p.number());
241 Set<Link> links = ls.getEgressLinks(connectPoint);
Simon Huntd2747a02015-04-30 22:41:16 -0700242 if (!links.isEmpty()) {
243 StringBuilder egressLinks = new StringBuilder();
244 for (Link l : links) {
245 ConnectPoint dest = l.dst();
246 egressLinks.append(dest.elementId()).append("/")
247 .append(dest.port()).append(" ");
248 }
249 port.put(LINK_DEST, egressLinks.toString());
Thomas Vachuska018110c2016-07-05 12:21:38 -0700250 } else {
251 HostService hs = get(HostService.class);
252 Set<Host> hosts = hs.getConnectedHosts(connectPoint);
253 if (hosts != null && !hosts.isEmpty()) {
254 port.put(LINK_DEST, hosts.iterator().next().id().toString());
255 }
Simon Huntd2747a02015-04-30 22:41:16 -0700256 }
257
258 return port;
259 }
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700260 }
Thomas Vachuska8f62f572015-10-06 12:17:44 -0700261
Simon Huntd5579252015-10-06 15:09:14 -0700262
Thomas Vachuska8f62f572015-10-06 12:17:44 -0700263 // handler for changing device friendly name
264 private final class NameChangeHandler extends RequestHandler {
265 private NameChangeHandler() {
266 super(DEV_NAME_CHANGE_REQ);
267 }
268
269 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800270 public void process(ObjectNode payload) {
Simon Huntd5579252015-10-06 15:09:14 -0700271 DeviceId deviceId = deviceId(string(payload, ID, ZERO_URI));
272 String name = emptyToNull(string(payload, NAME, null));
273 log.debug("Name change request: {} -- '{}'", deviceId, name);
Thomas Vachuska8f62f572015-10-06 12:17:44 -0700274
Simon Huntd5579252015-10-06 15:09:14 -0700275 NetworkConfigService service = get(NetworkConfigService.class);
276 BasicDeviceConfig cfg =
277 service.addConfig(deviceId, BasicDeviceConfig.class);
278
279 // Name attribute missing from the payload (or empty string)
280 // means that the friendly name should be unset.
281 cfg.name(name);
Thomas Vachuska8f62f572015-10-06 12:17:44 -0700282 cfg.apply();
Simon Hunt8a0429a2017-01-06 16:52:47 -0800283 sendMessage(DEV_NAME_CHANGE_RESP, payload);
Thomas Vachuska8f62f572015-10-06 12:17:44 -0700284 }
285 }
Thomas Vachuskae586b792015-03-26 13:59:38 -0700286}