blob: a04f6162defad4b7e06e82ab151457cce6e191f5 [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";
60
61 private static final String DEV_DETAILS_REQ = "deviceDetailsRequest";
62 private static final String DEV_DETAILS_RESP = "deviceDetailsResponse";
63 private static final String DETAILS = "details";
Thomas Vachuskae586b792015-03-26 13:59:38 -070064
Thomas Vachuska8f62f572015-10-06 12:17:44 -070065 private static final String DEV_NAME_CHANGE_REQ = "deviceNameChangeRequest";
66 private static final String DEV_NAME_CHANGE_RESP = "deviceNameChangeResponse";
67
Simon Huntd5579252015-10-06 15:09:14 -070068 private static final String ZERO_URI = "of:0000000000000000";
69
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -070070 private static final String ID = "id";
71 private static final String TYPE = "type";
72 private static final String AVAILABLE = "available";
73 private static final String AVAILABLE_IID = "_iconid_available";
74 private static final String TYPE_IID = "_iconid_type";
75 private static final String DEV_ICON_PREFIX = "devIcon_";
76 private static final String NUM_PORTS = "num_ports";
77 private static final String LINK_DEST = "elinks_dest";
78 private static final String MFR = "mfr";
79 private static final String HW = "hw";
80 private static final String SW = "sw";
81 private static final String PROTOCOL = "protocol";
82 private static final String MASTER_ID = "masterid";
83 private static final String CHASSIS_ID = "chassisid";
84 private static final String SERIAL = "serial";
85 private static final String PORTS = "ports";
86 private static final String ENABLED = "enabled";
87 private static final String SPEED = "speed";
Thomas Vachuskab52a0142015-04-21 17:48:15 -070088 private static final String NAME = "name";
Simon Huntd5579252015-10-06 15:09:14 -070089 private static final String WARN = "warn";
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -070090
Thomas Vachuskac4178cc2015-12-10 11:43:32 -080091 private static final String NONE = "none";
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -070092
Simon Hunt3d1b0652015-05-05 17:27:24 -070093 private static final String[] COL_IDS = {
Simon Huntd5579252015-10-06 15:09:14 -070094 AVAILABLE, AVAILABLE_IID, TYPE_IID,
95 NAME, ID, MASTER_ID, NUM_PORTS, MFR, HW, SW,
Simon Hunt3d1b0652015-05-05 17:27:24 -070096 PROTOCOL, CHASSIS_ID, SERIAL
97 };
98
99 private static final String ICON_ID_ONLINE = "active";
100 private static final String ICON_ID_OFFLINE = "inactive";
101
Simon Huntd5579252015-10-06 15:09:14 -0700102 private final Logger log = LoggerFactory.getLogger(getClass());
103
104
Thomas Vachuskae586b792015-03-26 13:59:38 -0700105 @Override
Simon Huntda580882015-05-12 20:58:18 -0700106 protected Collection<RequestHandler> createRequestHandlers() {
Simon Huntd2747a02015-04-30 22:41:16 -0700107 return ImmutableSet.of(
108 new DataRequestHandler(),
Thomas Vachuska8f62f572015-10-06 12:17:44 -0700109 new NameChangeHandler(),
Simon Huntd2747a02015-04-30 22:41:16 -0700110 new DetailRequestHandler()
111 );
112 }
113
Simon Huntd5579252015-10-06 15:09:14 -0700114 // Get friendly name of the device from the annotations
115 private static String deviceName(Device device) {
116 String name = device.annotations().value(AnnotationKeys.NAME);
117 return isNullOrEmpty(name) ? device.id().toString() : name;
118 }
119
120 private static String deviceProtocol(Device device) {
121 String protocol = device.annotations().value(PROTOCOL);
122 return protocol != null ? protocol : "";
123 }
124
Simon Hunt3d1b0652015-05-05 17:27:24 -0700125 private static String getTypeIconId(Device d) {
126 return DEV_ICON_PREFIX + d.type().toString();
127 }
128
Simon Huntabd16f62015-05-01 13:14:40 -0700129 // handler for device table requests
130 private final class DataRequestHandler extends TableRequestHandler {
Jian Li69f66632016-01-15 12:27:42 -0800131 private static final String NO_ROWS_MESSAGE = "No devices found";
132
Simon Huntd2747a02015-04-30 22:41:16 -0700133 private DataRequestHandler() {
Simon Huntabd16f62015-05-01 13:14:40 -0700134 super(DEV_DATA_REQ, DEV_DATA_RESP, DEVICES);
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700135 }
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700136
Simon Huntd2747a02015-04-30 22:41:16 -0700137 @Override
Simon Hunt3d1b0652015-05-05 17:27:24 -0700138 protected String[] getColumnIds() {
139 return COL_IDS;
140 }
141
142 @Override
Jian Li8baf4472016-01-15 15:08:09 -0800143 protected String noRowsMessage(ObjectNode payload) {
Jian Li69f66632016-01-15 12:27:42 -0800144 return NO_ROWS_MESSAGE;
145 }
146
147 @Override
Simon Hunt3d1b0652015-05-05 17:27:24 -0700148 protected void populateTable(TableModel tm, ObjectNode payload) {
149 DeviceService ds = get(DeviceService.class);
150 MastershipService ms = get(MastershipService.class);
151 for (Device dev : ds.getDevices()) {
152 populateRow(tm.addRow(), dev, ds, ms);
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700153 }
Simon Hunt3d1b0652015-05-05 17:27:24 -0700154 }
155
156 private void populateRow(TableModel.Row row, Device dev,
157 DeviceService ds, MastershipService ms) {
158 DeviceId id = dev.id();
159 boolean available = ds.isAvailable(id);
160 String iconId = available ? ICON_ID_ONLINE : ICON_ID_OFFLINE;
161
162 row.cell(ID, id)
Simon Huntd5579252015-10-06 15:09:14 -0700163 .cell(NAME, deviceName(dev))
Simon Hunt3d1b0652015-05-05 17:27:24 -0700164 .cell(AVAILABLE, available)
165 .cell(AVAILABLE_IID, iconId)
166 .cell(TYPE_IID, getTypeIconId(dev))
167 .cell(MFR, dev.manufacturer())
168 .cell(HW, dev.hwVersion())
169 .cell(SW, dev.swVersion())
Simon Huntd5579252015-10-06 15:09:14 -0700170 .cell(PROTOCOL, deviceProtocol(dev))
Simon Hunt3d1b0652015-05-05 17:27:24 -0700171 .cell(NUM_PORTS, ds.getPorts(id).size())
172 .cell(MASTER_ID, ms.getMasterFor(id));
Simon Huntd2747a02015-04-30 22:41:16 -0700173 }
174 }
175
Simon Huntabd16f62015-05-01 13:14:40 -0700176 // handler for selected device detail requests
Simon Huntd2747a02015-04-30 22:41:16 -0700177 private final class DetailRequestHandler extends RequestHandler {
178 private DetailRequestHandler() {
Simon Huntabd16f62015-05-01 13:14:40 -0700179 super(DEV_DETAILS_REQ);
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700180 }
181
Simon Huntd2747a02015-04-30 22:41:16 -0700182 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800183 public void process(ObjectNode payload) {
Simon Huntd5579252015-10-06 15:09:14 -0700184 String id = string(payload, ID, ZERO_URI);
Simon Huntd2747a02015-04-30 22:41:16 -0700185
Thomas Vachuska8f62f572015-10-06 12:17:44 -0700186 DeviceId deviceId = deviceId(id);
Simon Huntd2747a02015-04-30 22:41:16 -0700187 DeviceService service = get(DeviceService.class);
188 MastershipService ms = get(MastershipService.class);
189 Device device = service.getDevice(deviceId);
Simon Huntd5579252015-10-06 15:09:14 -0700190 ObjectNode data = objectNode();
Thomas Vachuskac4178cc2015-12-10 11:43:32 -0800191 NodeId masterFor = ms.getMasterFor(deviceId);
Simon Huntd2747a02015-04-30 22:41:16 -0700192
193 data.put(ID, deviceId.toString());
Simon Huntd5579252015-10-06 15:09:14 -0700194 data.put(NAME, deviceName(device));
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700195 data.put(TYPE, capitalizeFully(device.type().toString()));
Simon Huntd2747a02015-04-30 22:41:16 -0700196 data.put(TYPE_IID, getTypeIconId(device));
197 data.put(MFR, device.manufacturer());
198 data.put(HW, device.hwVersion());
199 data.put(SW, device.swVersion());
200 data.put(SERIAL, device.serialNumber());
201 data.put(CHASSIS_ID, device.chassisId().toString());
Thomas Vachuskac4178cc2015-12-10 11:43:32 -0800202 data.put(MASTER_ID, masterFor != null ? masterFor.toString() : NONE);
Simon Huntd5579252015-10-06 15:09:14 -0700203 data.put(PROTOCOL, deviceProtocol(device));
Simon Huntd2747a02015-04-30 22:41:16 -0700204
Simon Huntd5579252015-10-06 15:09:14 -0700205 ArrayNode ports = arrayNode();
Simon Huntd2747a02015-04-30 22:41:16 -0700206
207 List<Port> portList = new ArrayList<>(service.getPorts(deviceId));
Simon Hunt8a0429a2017-01-06 16:52:47 -0800208 portList.sort((p1, p2) -> {
Simon Huntd2747a02015-04-30 22:41:16 -0700209 long delta = p1.number().toLong() - p2.number().toLong();
210 return delta == 0 ? 0 : (delta < 0 ? -1 : +1);
211 });
212
213 for (Port p : portList) {
214 ports.add(portData(p, deviceId));
215 }
216 data.set(PORTS, ports);
217
Simon Huntd5579252015-10-06 15:09:14 -0700218 ObjectNode rootNode = objectNode();
Simon Huntabd16f62015-05-01 13:14:40 -0700219 rootNode.set(DETAILS, data);
Simon Hunt8a0429a2017-01-06 16:52:47 -0800220 sendMessage(DEV_DETAILS_RESP, rootNode);
Simon Huntd2747a02015-04-30 22:41:16 -0700221 }
222
223 private ObjectNode portData(Port p, DeviceId id) {
Simon Huntd5579252015-10-06 15:09:14 -0700224 ObjectNode port = objectNode();
Simon Huntd2747a02015-04-30 22:41:16 -0700225 LinkService ls = get(LinkService.class);
226 String name = p.annotations().value(AnnotationKeys.PORT_NAME);
227
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700228 port.put(ID, capitalizeFully(p.number().toString()));
229 port.put(TYPE, capitalizeFully(p.type().toString()));
Simon Huntd2747a02015-04-30 22:41:16 -0700230 port.put(SPEED, p.portSpeed());
231 port.put(ENABLED, p.isEnabled());
232 port.put(NAME, name != null ? name : "");
233
Thomas Vachuska018110c2016-07-05 12:21:38 -0700234 ConnectPoint connectPoint = new ConnectPoint(id, p.number());
235 Set<Link> links = ls.getEgressLinks(connectPoint);
Simon Huntd2747a02015-04-30 22:41:16 -0700236 if (!links.isEmpty()) {
237 StringBuilder egressLinks = new StringBuilder();
238 for (Link l : links) {
239 ConnectPoint dest = l.dst();
240 egressLinks.append(dest.elementId()).append("/")
241 .append(dest.port()).append(" ");
242 }
243 port.put(LINK_DEST, egressLinks.toString());
Thomas Vachuska018110c2016-07-05 12:21:38 -0700244 } else {
245 HostService hs = get(HostService.class);
246 Set<Host> hosts = hs.getConnectedHosts(connectPoint);
247 if (hosts != null && !hosts.isEmpty()) {
248 port.put(LINK_DEST, hosts.iterator().next().id().toString());
249 }
Simon Huntd2747a02015-04-30 22:41:16 -0700250 }
251
252 return port;
253 }
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700254 }
Thomas Vachuska8f62f572015-10-06 12:17:44 -0700255
Simon Huntd5579252015-10-06 15:09:14 -0700256
Thomas Vachuska8f62f572015-10-06 12:17:44 -0700257 // handler for changing device friendly name
258 private final class NameChangeHandler extends RequestHandler {
259 private NameChangeHandler() {
260 super(DEV_NAME_CHANGE_REQ);
261 }
262
263 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800264 public void process(ObjectNode payload) {
Simon Huntd5579252015-10-06 15:09:14 -0700265 DeviceId deviceId = deviceId(string(payload, ID, ZERO_URI));
266 String name = emptyToNull(string(payload, NAME, null));
267 log.debug("Name change request: {} -- '{}'", deviceId, name);
Thomas Vachuska8f62f572015-10-06 12:17:44 -0700268
Simon Huntd5579252015-10-06 15:09:14 -0700269 NetworkConfigService service = get(NetworkConfigService.class);
270 BasicDeviceConfig cfg =
271 service.addConfig(deviceId, BasicDeviceConfig.class);
272
273 // Name attribute missing from the payload (or empty string)
274 // means that the friendly name should be unset.
275 cfg.name(name);
Thomas Vachuska8f62f572015-10-06 12:17:44 -0700276 cfg.apply();
Simon Hunt8a0429a2017-01-06 16:52:47 -0800277 sendMessage(DEV_NAME_CHANGE_RESP, payload);
Thomas Vachuska8f62f572015-10-06 12:17:44 -0700278 }
279 }
Thomas Vachuskae586b792015-03-26 13:59:38 -0700280}