blob: 7f6c2537f60b8f0a6cee1988486df8c5d2fd7796 [file] [log] [blame]
Thomas Vachuskae586b792015-03-26 13:59:38 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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;
Yi Tseng1802f552017-08-23 10:14:05 -070035import org.onosproject.net.pi.model.PiPipeconfId;
Carmelo Cascone39c28ca2017-11-15 13:03:57 -080036import org.onosproject.net.pi.service.PiPipeconfService;
Simon Huntd2747a02015-04-30 22:41:16 -070037import org.onosproject.ui.RequestHandler;
Simon Hunta0ddb022015-05-01 09:53:01 -070038import org.onosproject.ui.UiMessageHandler;
Simon Hunt3d1b0652015-05-05 17:27:24 -070039import org.onosproject.ui.table.TableModel;
Simon Huntabd16f62015-05-01 13:14:40 -070040import org.onosproject.ui.table.TableRequestHandler;
Simon Huntd5579252015-10-06 15:09:14 -070041import org.slf4j.Logger;
42import org.slf4j.LoggerFactory;
Thomas Vachuskae586b792015-03-26 13:59:38 -070043
44import java.util.ArrayList;
Simon Huntd2747a02015-04-30 22:41:16 -070045import java.util.Collection;
Thomas Vachuskae586b792015-03-26 13:59:38 -070046import java.util.List;
Yi Tseng1802f552017-08-23 10:14:05 -070047import java.util.Optional;
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -070048import java.util.Set;
Bri Prebilic Cole264c5ec2015-04-07 10:22:26 -070049
Thomas Vachuska8f62f572015-10-06 12:17:44 -070050import static com.google.common.base.Strings.emptyToNull;
51import static com.google.common.base.Strings.isNullOrEmpty;
Bri Prebilic Cole9467a232015-05-06 16:59:05 -070052import static org.apache.commons.lang.WordUtils.capitalizeFully;
Thomas Vachuska8f62f572015-10-06 12:17:44 -070053import static org.onosproject.net.DeviceId.deviceId;
Bri Prebilic Cole9467a232015-05-06 16:59:05 -070054
Thomas Vachuskae586b792015-03-26 13:59:38 -070055/**
56 * Message handler for device view related messages.
57 */
Simon Hunta0ddb022015-05-01 09:53:01 -070058public class DeviceViewMessageHandler extends UiMessageHandler {
Simon Huntd2747a02015-04-30 22:41:16 -070059
60 private static final String DEV_DATA_REQ = "deviceDataRequest";
Simon Huntabd16f62015-05-01 13:14:40 -070061 private static final String DEV_DATA_RESP = "deviceDataResponse";
62 private static final String DEVICES = "devices";
Alan Deikman8d858752017-02-14 18:08:29 -080063 private static final String DEVICE = "device";
Simon Huntabd16f62015-05-01 13:14:40 -070064
65 private static final String DEV_DETAILS_REQ = "deviceDetailsRequest";
66 private static final String DEV_DETAILS_RESP = "deviceDetailsResponse";
67 private static final String DETAILS = "details";
Thomas Vachuskae586b792015-03-26 13:59:38 -070068
Thomas Vachuska8f62f572015-10-06 12:17:44 -070069 private static final String DEV_NAME_CHANGE_REQ = "deviceNameChangeRequest";
70 private static final String DEV_NAME_CHANGE_RESP = "deviceNameChangeResponse";
71
Simon Huntd5579252015-10-06 15:09:14 -070072 private static final String ZERO_URI = "of:0000000000000000";
73
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -070074 private static final String ID = "id";
75 private static final String TYPE = "type";
76 private static final String AVAILABLE = "available";
77 private static final String AVAILABLE_IID = "_iconid_available";
78 private static final String TYPE_IID = "_iconid_type";
79 private static final String DEV_ICON_PREFIX = "devIcon_";
80 private static final String NUM_PORTS = "num_ports";
81 private static final String LINK_DEST = "elinks_dest";
82 private static final String MFR = "mfr";
83 private static final String HW = "hw";
84 private static final String SW = "sw";
85 private static final String PROTOCOL = "protocol";
Yi Tseng1802f552017-08-23 10:14:05 -070086 private static final String PIPECONF = "pipeconf";
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -070087 private static final String MASTER_ID = "masterid";
88 private static final String CHASSIS_ID = "chassisid";
89 private static final String SERIAL = "serial";
90 private static final String PORTS = "ports";
91 private static final String ENABLED = "enabled";
92 private static final String SPEED = "speed";
Thomas Vachuskab52a0142015-04-21 17:48:15 -070093 private static final String NAME = "name";
Simon Huntd5579252015-10-06 15:09:14 -070094 private static final String WARN = "warn";
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -070095
Thomas Vachuskac4178cc2015-12-10 11:43:32 -080096 private static final String NONE = "none";
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -070097
Simon Hunt3d1b0652015-05-05 17:27:24 -070098 private static final String[] COL_IDS = {
Simon Huntd5579252015-10-06 15:09:14 -070099 AVAILABLE, AVAILABLE_IID, TYPE_IID,
100 NAME, ID, MASTER_ID, NUM_PORTS, MFR, HW, SW,
Simon Hunt3d1b0652015-05-05 17:27:24 -0700101 PROTOCOL, CHASSIS_ID, SERIAL
102 };
103
104 private static final String ICON_ID_ONLINE = "active";
105 private static final String ICON_ID_OFFLINE = "inactive";
106
Simon Huntd5579252015-10-06 15:09:14 -0700107 private final Logger log = LoggerFactory.getLogger(getClass());
108
109
Thomas Vachuskae586b792015-03-26 13:59:38 -0700110 @Override
Simon Huntda580882015-05-12 20:58:18 -0700111 protected Collection<RequestHandler> createRequestHandlers() {
Simon Huntd2747a02015-04-30 22:41:16 -0700112 return ImmutableSet.of(
113 new DataRequestHandler(),
Thomas Vachuska8f62f572015-10-06 12:17:44 -0700114 new NameChangeHandler(),
Simon Huntd2747a02015-04-30 22:41:16 -0700115 new DetailRequestHandler()
116 );
117 }
118
Simon Huntd5579252015-10-06 15:09:14 -0700119 // Get friendly name of the device from the annotations
120 private static String deviceName(Device device) {
121 String name = device.annotations().value(AnnotationKeys.NAME);
122 return isNullOrEmpty(name) ? device.id().toString() : name;
123 }
124
125 private static String deviceProtocol(Device device) {
126 String protocol = device.annotations().value(PROTOCOL);
127 return protocol != null ? protocol : "";
128 }
129
Simon Hunt3d1b0652015-05-05 17:27:24 -0700130 private static String getTypeIconId(Device d) {
Sean Condon794d2502019-01-31 20:54:53 +0000131 String uiType = d.annotations().value(AnnotationKeys.UI_TYPE);
132 if (uiType != null && !uiType.isEmpty()) {
133 return uiType;
134 }
Simon Hunt3d1b0652015-05-05 17:27:24 -0700135 return DEV_ICON_PREFIX + d.type().toString();
136 }
137
Simon Huntabd16f62015-05-01 13:14:40 -0700138 // handler for device table requests
139 private final class DataRequestHandler extends TableRequestHandler {
Jian Li69f66632016-01-15 12:27:42 -0800140 private static final String NO_ROWS_MESSAGE = "No devices found";
141
Simon Huntd2747a02015-04-30 22:41:16 -0700142 private DataRequestHandler() {
Simon Huntabd16f62015-05-01 13:14:40 -0700143 super(DEV_DATA_REQ, DEV_DATA_RESP, DEVICES);
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700144 }
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700145
Simon Huntd2747a02015-04-30 22:41:16 -0700146 @Override
Simon Hunt3d1b0652015-05-05 17:27:24 -0700147 protected String[] getColumnIds() {
148 return COL_IDS;
149 }
150
151 @Override
Jian Li8baf4472016-01-15 15:08:09 -0800152 protected String noRowsMessage(ObjectNode payload) {
Jian Li69f66632016-01-15 12:27:42 -0800153 return NO_ROWS_MESSAGE;
154 }
155
156 @Override
Simon Hunt3d1b0652015-05-05 17:27:24 -0700157 protected void populateTable(TableModel tm, ObjectNode payload) {
158 DeviceService ds = get(DeviceService.class);
159 MastershipService ms = get(MastershipService.class);
160 for (Device dev : ds.getDevices()) {
161 populateRow(tm.addRow(), dev, ds, ms);
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700162 }
Simon Hunt3d1b0652015-05-05 17:27:24 -0700163 }
164
165 private void populateRow(TableModel.Row row, Device dev,
166 DeviceService ds, MastershipService ms) {
167 DeviceId id = dev.id();
168 boolean available = ds.isAvailable(id);
169 String iconId = available ? ICON_ID_ONLINE : ICON_ID_OFFLINE;
170
171 row.cell(ID, id)
Simon Huntd5579252015-10-06 15:09:14 -0700172 .cell(NAME, deviceName(dev))
Simon Hunt3d1b0652015-05-05 17:27:24 -0700173 .cell(AVAILABLE, available)
174 .cell(AVAILABLE_IID, iconId)
175 .cell(TYPE_IID, getTypeIconId(dev))
176 .cell(MFR, dev.manufacturer())
177 .cell(HW, dev.hwVersion())
178 .cell(SW, dev.swVersion())
Simon Huntd5579252015-10-06 15:09:14 -0700179 .cell(PROTOCOL, deviceProtocol(dev))
Simon Hunt3d1b0652015-05-05 17:27:24 -0700180 .cell(NUM_PORTS, ds.getPorts(id).size())
181 .cell(MASTER_ID, ms.getMasterFor(id));
Simon Huntd2747a02015-04-30 22:41:16 -0700182 }
183 }
184
Simon Huntabd16f62015-05-01 13:14:40 -0700185 // handler for selected device detail requests
Simon Huntd2747a02015-04-30 22:41:16 -0700186 private final class DetailRequestHandler extends RequestHandler {
187 private DetailRequestHandler() {
Simon Huntabd16f62015-05-01 13:14:40 -0700188 super(DEV_DETAILS_REQ);
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700189 }
190
Simon Huntd2747a02015-04-30 22:41:16 -0700191 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800192 public void process(ObjectNode payload) {
Simon Huntd5579252015-10-06 15:09:14 -0700193 String id = string(payload, ID, ZERO_URI);
Simon Huntd2747a02015-04-30 22:41:16 -0700194
Thomas Vachuska8f62f572015-10-06 12:17:44 -0700195 DeviceId deviceId = deviceId(id);
Simon Huntd2747a02015-04-30 22:41:16 -0700196 DeviceService service = get(DeviceService.class);
197 MastershipService ms = get(MastershipService.class);
198 Device device = service.getDevice(deviceId);
Simon Huntd5579252015-10-06 15:09:14 -0700199 ObjectNode data = objectNode();
Thomas Vachuskac4178cc2015-12-10 11:43:32 -0800200 NodeId masterFor = ms.getMasterFor(deviceId);
Simon Huntd2747a02015-04-30 22:41:16 -0700201
202 data.put(ID, deviceId.toString());
Simon Huntd5579252015-10-06 15:09:14 -0700203 data.put(NAME, deviceName(device));
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700204 data.put(TYPE, capitalizeFully(device.type().toString()));
Simon Huntd2747a02015-04-30 22:41:16 -0700205 data.put(TYPE_IID, getTypeIconId(device));
206 data.put(MFR, device.manufacturer());
207 data.put(HW, device.hwVersion());
208 data.put(SW, device.swVersion());
209 data.put(SERIAL, device.serialNumber());
210 data.put(CHASSIS_ID, device.chassisId().toString());
Thomas Vachuskac4178cc2015-12-10 11:43:32 -0800211 data.put(MASTER_ID, masterFor != null ? masterFor.toString() : NONE);
Simon Huntd5579252015-10-06 15:09:14 -0700212 data.put(PROTOCOL, deviceProtocol(device));
Yi Tseng1802f552017-08-23 10:14:05 -0700213 data.put(PIPECONF, devicePipeconf(device));
Simon Huntd2747a02015-04-30 22:41:16 -0700214
Simon Huntd5579252015-10-06 15:09:14 -0700215 ArrayNode ports = arrayNode();
Simon Huntd2747a02015-04-30 22:41:16 -0700216
217 List<Port> portList = new ArrayList<>(service.getPorts(deviceId));
Simon Hunt8a0429a2017-01-06 16:52:47 -0800218 portList.sort((p1, p2) -> {
Simon Huntd2747a02015-04-30 22:41:16 -0700219 long delta = p1.number().toLong() - p2.number().toLong();
220 return delta == 0 ? 0 : (delta < 0 ? -1 : +1);
221 });
222
223 for (Port p : portList) {
224 ports.add(portData(p, deviceId));
225 }
226 data.set(PORTS, ports);
227
Simon Huntd5579252015-10-06 15:09:14 -0700228 ObjectNode rootNode = objectNode();
Simon Huntabd16f62015-05-01 13:14:40 -0700229 rootNode.set(DETAILS, data);
Alan Deikman8d858752017-02-14 18:08:29 -0800230
Simon Huntcc035c52017-02-22 21:12:51 -0800231 // NOTE: ... an alternate way of getting all the details of an item:
232 // Use the codec context to get a JSON of the device. See ONOS-5976.
Alan Deikman8d858752017-02-14 18:08:29 -0800233 rootNode.set(DEVICE, getJsonCodecContext().encode(device, Device.class));
234
Simon Hunt8a0429a2017-01-06 16:52:47 -0800235 sendMessage(DEV_DETAILS_RESP, rootNode);
Simon Huntd2747a02015-04-30 22:41:16 -0700236 }
237
238 private ObjectNode portData(Port p, DeviceId id) {
Simon Huntd5579252015-10-06 15:09:14 -0700239 ObjectNode port = objectNode();
Simon Huntd2747a02015-04-30 22:41:16 -0700240 LinkService ls = get(LinkService.class);
241 String name = p.annotations().value(AnnotationKeys.PORT_NAME);
242
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700243 port.put(ID, capitalizeFully(p.number().toString()));
244 port.put(TYPE, capitalizeFully(p.type().toString()));
Simon Huntd2747a02015-04-30 22:41:16 -0700245 port.put(SPEED, p.portSpeed());
246 port.put(ENABLED, p.isEnabled());
247 port.put(NAME, name != null ? name : "");
248
Thomas Vachuska018110c2016-07-05 12:21:38 -0700249 ConnectPoint connectPoint = new ConnectPoint(id, p.number());
250 Set<Link> links = ls.getEgressLinks(connectPoint);
Simon Huntd2747a02015-04-30 22:41:16 -0700251 if (!links.isEmpty()) {
252 StringBuilder egressLinks = new StringBuilder();
253 for (Link l : links) {
254 ConnectPoint dest = l.dst();
255 egressLinks.append(dest.elementId()).append("/")
256 .append(dest.port()).append(" ");
257 }
258 port.put(LINK_DEST, egressLinks.toString());
Thomas Vachuska018110c2016-07-05 12:21:38 -0700259 } else {
260 HostService hs = get(HostService.class);
261 Set<Host> hosts = hs.getConnectedHosts(connectPoint);
262 if (hosts != null && !hosts.isEmpty()) {
263 port.put(LINK_DEST, hosts.iterator().next().id().toString());
264 }
Simon Huntd2747a02015-04-30 22:41:16 -0700265 }
266
267 return port;
268 }
Thomas Vachuska8f62f572015-10-06 12:17:44 -0700269
Yi Tseng1802f552017-08-23 10:14:05 -0700270 private String devicePipeconf(Device device) {
271 PiPipeconfService service = get(PiPipeconfService.class);
272 Optional<PiPipeconfId> pipeconfId = service.ofDevice(device.id());
273 if (pipeconfId.isPresent()) {
274 return pipeconfId.get().id();
275 } else {
276 return NONE;
277 }
278 }
279 }
Simon Huntd5579252015-10-06 15:09:14 -0700280
Thomas Vachuska8f62f572015-10-06 12:17:44 -0700281 // handler for changing device friendly name
282 private final class NameChangeHandler extends RequestHandler {
283 private NameChangeHandler() {
284 super(DEV_NAME_CHANGE_REQ);
285 }
286
287 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800288 public void process(ObjectNode payload) {
Simon Huntd5579252015-10-06 15:09:14 -0700289 DeviceId deviceId = deviceId(string(payload, ID, ZERO_URI));
290 String name = emptyToNull(string(payload, NAME, null));
291 log.debug("Name change request: {} -- '{}'", deviceId, name);
Thomas Vachuska8f62f572015-10-06 12:17:44 -0700292
Simon Huntd5579252015-10-06 15:09:14 -0700293 NetworkConfigService service = get(NetworkConfigService.class);
294 BasicDeviceConfig cfg =
295 service.addConfig(deviceId, BasicDeviceConfig.class);
296
297 // Name attribute missing from the payload (or empty string)
298 // means that the friendly name should be unset.
299 cfg.name(name);
Thomas Vachuska8f62f572015-10-06 12:17:44 -0700300 cfg.apply();
Simon Hunt8a0429a2017-01-06 16:52:47 -0800301 sendMessage(DEV_NAME_CHANGE_RESP, payload);
Thomas Vachuska8f62f572015-10-06 12:17:44 -0700302 }
303 }
Thomas Vachuskae586b792015-03-26 13:59:38 -0700304}