blob: fa03537630932ed962c119947e2ad00f3d35324f [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) {
131 return DEV_ICON_PREFIX + d.type().toString();
132 }
133
Simon Huntabd16f62015-05-01 13:14:40 -0700134 // handler for device table requests
135 private final class DataRequestHandler extends TableRequestHandler {
Jian Li69f66632016-01-15 12:27:42 -0800136 private static final String NO_ROWS_MESSAGE = "No devices found";
137
Simon Huntd2747a02015-04-30 22:41:16 -0700138 private DataRequestHandler() {
Simon Huntabd16f62015-05-01 13:14:40 -0700139 super(DEV_DATA_REQ, DEV_DATA_RESP, DEVICES);
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700140 }
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700141
Simon Huntd2747a02015-04-30 22:41:16 -0700142 @Override
Simon Hunt3d1b0652015-05-05 17:27:24 -0700143 protected String[] getColumnIds() {
144 return COL_IDS;
145 }
146
147 @Override
Jian Li8baf4472016-01-15 15:08:09 -0800148 protected String noRowsMessage(ObjectNode payload) {
Jian Li69f66632016-01-15 12:27:42 -0800149 return NO_ROWS_MESSAGE;
150 }
151
152 @Override
Simon Hunt3d1b0652015-05-05 17:27:24 -0700153 protected void populateTable(TableModel tm, ObjectNode payload) {
154 DeviceService ds = get(DeviceService.class);
155 MastershipService ms = get(MastershipService.class);
156 for (Device dev : ds.getDevices()) {
157 populateRow(tm.addRow(), dev, ds, ms);
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700158 }
Simon Hunt3d1b0652015-05-05 17:27:24 -0700159 }
160
161 private void populateRow(TableModel.Row row, Device dev,
162 DeviceService ds, MastershipService ms) {
163 DeviceId id = dev.id();
164 boolean available = ds.isAvailable(id);
165 String iconId = available ? ICON_ID_ONLINE : ICON_ID_OFFLINE;
166
167 row.cell(ID, id)
Simon Huntd5579252015-10-06 15:09:14 -0700168 .cell(NAME, deviceName(dev))
Simon Hunt3d1b0652015-05-05 17:27:24 -0700169 .cell(AVAILABLE, available)
170 .cell(AVAILABLE_IID, iconId)
171 .cell(TYPE_IID, getTypeIconId(dev))
172 .cell(MFR, dev.manufacturer())
173 .cell(HW, dev.hwVersion())
174 .cell(SW, dev.swVersion())
Simon Huntd5579252015-10-06 15:09:14 -0700175 .cell(PROTOCOL, deviceProtocol(dev))
Simon Hunt3d1b0652015-05-05 17:27:24 -0700176 .cell(NUM_PORTS, ds.getPorts(id).size())
177 .cell(MASTER_ID, ms.getMasterFor(id));
Simon Huntd2747a02015-04-30 22:41:16 -0700178 }
179 }
180
Simon Huntabd16f62015-05-01 13:14:40 -0700181 // handler for selected device detail requests
Simon Huntd2747a02015-04-30 22:41:16 -0700182 private final class DetailRequestHandler extends RequestHandler {
183 private DetailRequestHandler() {
Simon Huntabd16f62015-05-01 13:14:40 -0700184 super(DEV_DETAILS_REQ);
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700185 }
186
Simon Huntd2747a02015-04-30 22:41:16 -0700187 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800188 public void process(ObjectNode payload) {
Simon Huntd5579252015-10-06 15:09:14 -0700189 String id = string(payload, ID, ZERO_URI);
Simon Huntd2747a02015-04-30 22:41:16 -0700190
Thomas Vachuska8f62f572015-10-06 12:17:44 -0700191 DeviceId deviceId = deviceId(id);
Simon Huntd2747a02015-04-30 22:41:16 -0700192 DeviceService service = get(DeviceService.class);
193 MastershipService ms = get(MastershipService.class);
194 Device device = service.getDevice(deviceId);
Simon Huntd5579252015-10-06 15:09:14 -0700195 ObjectNode data = objectNode();
Thomas Vachuskac4178cc2015-12-10 11:43:32 -0800196 NodeId masterFor = ms.getMasterFor(deviceId);
Simon Huntd2747a02015-04-30 22:41:16 -0700197
198 data.put(ID, deviceId.toString());
Simon Huntd5579252015-10-06 15:09:14 -0700199 data.put(NAME, deviceName(device));
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700200 data.put(TYPE, capitalizeFully(device.type().toString()));
Simon Huntd2747a02015-04-30 22:41:16 -0700201 data.put(TYPE_IID, getTypeIconId(device));
202 data.put(MFR, device.manufacturer());
203 data.put(HW, device.hwVersion());
204 data.put(SW, device.swVersion());
205 data.put(SERIAL, device.serialNumber());
206 data.put(CHASSIS_ID, device.chassisId().toString());
Thomas Vachuskac4178cc2015-12-10 11:43:32 -0800207 data.put(MASTER_ID, masterFor != null ? masterFor.toString() : NONE);
Simon Huntd5579252015-10-06 15:09:14 -0700208 data.put(PROTOCOL, deviceProtocol(device));
Yi Tseng1802f552017-08-23 10:14:05 -0700209 data.put(PIPECONF, devicePipeconf(device));
Simon Huntd2747a02015-04-30 22:41:16 -0700210
Simon Huntd5579252015-10-06 15:09:14 -0700211 ArrayNode ports = arrayNode();
Simon Huntd2747a02015-04-30 22:41:16 -0700212
213 List<Port> portList = new ArrayList<>(service.getPorts(deviceId));
Simon Hunt8a0429a2017-01-06 16:52:47 -0800214 portList.sort((p1, p2) -> {
Simon Huntd2747a02015-04-30 22:41:16 -0700215 long delta = p1.number().toLong() - p2.number().toLong();
216 return delta == 0 ? 0 : (delta < 0 ? -1 : +1);
217 });
218
219 for (Port p : portList) {
220 ports.add(portData(p, deviceId));
221 }
222 data.set(PORTS, ports);
223
Simon Huntd5579252015-10-06 15:09:14 -0700224 ObjectNode rootNode = objectNode();
Simon Huntabd16f62015-05-01 13:14:40 -0700225 rootNode.set(DETAILS, data);
Alan Deikman8d858752017-02-14 18:08:29 -0800226
Simon Huntcc035c52017-02-22 21:12:51 -0800227 // NOTE: ... an alternate way of getting all the details of an item:
228 // Use the codec context to get a JSON of the device. See ONOS-5976.
Alan Deikman8d858752017-02-14 18:08:29 -0800229 rootNode.set(DEVICE, getJsonCodecContext().encode(device, Device.class));
230
Simon Hunt8a0429a2017-01-06 16:52:47 -0800231 sendMessage(DEV_DETAILS_RESP, rootNode);
Simon Huntd2747a02015-04-30 22:41:16 -0700232 }
233
234 private ObjectNode portData(Port p, DeviceId id) {
Simon Huntd5579252015-10-06 15:09:14 -0700235 ObjectNode port = objectNode();
Simon Huntd2747a02015-04-30 22:41:16 -0700236 LinkService ls = get(LinkService.class);
237 String name = p.annotations().value(AnnotationKeys.PORT_NAME);
238
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700239 port.put(ID, capitalizeFully(p.number().toString()));
240 port.put(TYPE, capitalizeFully(p.type().toString()));
Simon Huntd2747a02015-04-30 22:41:16 -0700241 port.put(SPEED, p.portSpeed());
242 port.put(ENABLED, p.isEnabled());
243 port.put(NAME, name != null ? name : "");
244
Thomas Vachuska018110c2016-07-05 12:21:38 -0700245 ConnectPoint connectPoint = new ConnectPoint(id, p.number());
246 Set<Link> links = ls.getEgressLinks(connectPoint);
Simon Huntd2747a02015-04-30 22:41:16 -0700247 if (!links.isEmpty()) {
248 StringBuilder egressLinks = new StringBuilder();
249 for (Link l : links) {
250 ConnectPoint dest = l.dst();
251 egressLinks.append(dest.elementId()).append("/")
252 .append(dest.port()).append(" ");
253 }
254 port.put(LINK_DEST, egressLinks.toString());
Thomas Vachuska018110c2016-07-05 12:21:38 -0700255 } else {
256 HostService hs = get(HostService.class);
257 Set<Host> hosts = hs.getConnectedHosts(connectPoint);
258 if (hosts != null && !hosts.isEmpty()) {
259 port.put(LINK_DEST, hosts.iterator().next().id().toString());
260 }
Simon Huntd2747a02015-04-30 22:41:16 -0700261 }
262
263 return port;
264 }
Thomas Vachuska8f62f572015-10-06 12:17:44 -0700265
Yi Tseng1802f552017-08-23 10:14:05 -0700266 private String devicePipeconf(Device device) {
267 PiPipeconfService service = get(PiPipeconfService.class);
268 Optional<PiPipeconfId> pipeconfId = service.ofDevice(device.id());
269 if (pipeconfId.isPresent()) {
270 return pipeconfId.get().id();
271 } else {
272 return NONE;
273 }
274 }
275 }
Simon Huntd5579252015-10-06 15:09:14 -0700276
Thomas Vachuska8f62f572015-10-06 12:17:44 -0700277 // handler for changing device friendly name
278 private final class NameChangeHandler extends RequestHandler {
279 private NameChangeHandler() {
280 super(DEV_NAME_CHANGE_REQ);
281 }
282
283 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800284 public void process(ObjectNode payload) {
Simon Huntd5579252015-10-06 15:09:14 -0700285 DeviceId deviceId = deviceId(string(payload, ID, ZERO_URI));
286 String name = emptyToNull(string(payload, NAME, null));
287 log.debug("Name change request: {} -- '{}'", deviceId, name);
Thomas Vachuska8f62f572015-10-06 12:17:44 -0700288
Simon Huntd5579252015-10-06 15:09:14 -0700289 NetworkConfigService service = get(NetworkConfigService.class);
290 BasicDeviceConfig cfg =
291 service.addConfig(deviceId, BasicDeviceConfig.class);
292
293 // Name attribute missing from the payload (or empty string)
294 // means that the friendly name should be unset.
295 cfg.name(name);
Thomas Vachuska8f62f572015-10-06 12:17:44 -0700296 cfg.apply();
Simon Hunt8a0429a2017-01-06 16:52:47 -0800297 sendMessage(DEV_NAME_CHANGE_RESP, payload);
Thomas Vachuska8f62f572015-10-06 12:17:44 -0700298 }
299 }
Thomas Vachuskae586b792015-03-26 13:59:38 -0700300}