blob: 3057981dfdc6df76b948c695c261a99a213974b1 [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
18import com.fasterxml.jackson.databind.node.ArrayNode;
19import com.fasterxml.jackson.databind.node.ObjectNode;
20import com.google.common.collect.ImmutableSet;
Bri Prebilic Coleae65c962015-04-02 16:24:49 -070021import org.onosproject.mastership.MastershipService;
Thomas Vachuskab52a0142015-04-21 17:48:15 -070022import org.onosproject.net.AnnotationKeys;
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -070023import org.onosproject.net.ConnectPoint;
Thomas Vachuskae586b792015-03-26 13:59:38 -070024import org.onosproject.net.Device;
Bri Prebilic Cole264c5ec2015-04-07 10:22:26 -070025import org.onosproject.net.DeviceId;
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -070026import org.onosproject.net.Link;
Bri Prebilic Cole264c5ec2015-04-07 10:22:26 -070027import org.onosproject.net.Port;
Thomas Vachuska8f62f572015-10-06 12:17:44 -070028import org.onosproject.net.config.NetworkConfigService;
29import org.onosproject.net.config.basics.BasicDeviceConfig;
Thomas Vachuskae586b792015-03-26 13:59:38 -070030import org.onosproject.net.device.DeviceService;
Bri Prebilic Cole264c5ec2015-04-07 10:22:26 -070031import org.onosproject.net.link.LinkService;
Simon Huntd2747a02015-04-30 22:41:16 -070032import org.onosproject.ui.RequestHandler;
Simon Hunta0ddb022015-05-01 09:53:01 -070033import org.onosproject.ui.UiMessageHandler;
Simon Hunt3d1b0652015-05-05 17:27:24 -070034import org.onosproject.ui.table.TableModel;
Simon Huntabd16f62015-05-01 13:14:40 -070035import org.onosproject.ui.table.TableRequestHandler;
Thomas Vachuskae586b792015-03-26 13:59:38 -070036
37import java.util.ArrayList;
Simon Huntd2747a02015-04-30 22:41:16 -070038import java.util.Collection;
Thomas Vachuskab52a0142015-04-21 17:48:15 -070039import java.util.Collections;
Thomas Vachuskae586b792015-03-26 13:59:38 -070040import java.util.List;
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -070041import java.util.Set;
Bri Prebilic Cole264c5ec2015-04-07 10:22:26 -070042
Thomas Vachuska8f62f572015-10-06 12:17:44 -070043import static com.google.common.base.Strings.emptyToNull;
44import static com.google.common.base.Strings.isNullOrEmpty;
Bri Prebilic Cole9467a232015-05-06 16:59:05 -070045import static org.apache.commons.lang.WordUtils.capitalizeFully;
Thomas Vachuska8f62f572015-10-06 12:17:44 -070046import static org.onosproject.net.DeviceId.deviceId;
Bri Prebilic Cole9467a232015-05-06 16:59:05 -070047
Thomas Vachuskae586b792015-03-26 13:59:38 -070048/**
49 * Message handler for device view related messages.
50 */
Simon Hunta0ddb022015-05-01 09:53:01 -070051public class DeviceViewMessageHandler extends UiMessageHandler {
Simon Huntd2747a02015-04-30 22:41:16 -070052
53 private static final String DEV_DATA_REQ = "deviceDataRequest";
Simon Huntabd16f62015-05-01 13:14:40 -070054 private static final String DEV_DATA_RESP = "deviceDataResponse";
55 private static final String DEVICES = "devices";
56
57 private static final String DEV_DETAILS_REQ = "deviceDetailsRequest";
58 private static final String DEV_DETAILS_RESP = "deviceDetailsResponse";
59 private static final String DETAILS = "details";
Thomas Vachuskae586b792015-03-26 13:59:38 -070060
Thomas Vachuska8f62f572015-10-06 12:17:44 -070061 private static final String DEV_NAME_CHANGE_REQ = "deviceNameChangeRequest";
62 private static final String DEV_NAME_CHANGE_RESP = "deviceNameChangeResponse";
63
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -070064 private static final String ID = "id";
65 private static final String TYPE = "type";
66 private static final String AVAILABLE = "available";
67 private static final String AVAILABLE_IID = "_iconid_available";
68 private static final String TYPE_IID = "_iconid_type";
69 private static final String DEV_ICON_PREFIX = "devIcon_";
70 private static final String NUM_PORTS = "num_ports";
71 private static final String LINK_DEST = "elinks_dest";
72 private static final String MFR = "mfr";
73 private static final String HW = "hw";
74 private static final String SW = "sw";
75 private static final String PROTOCOL = "protocol";
76 private static final String MASTER_ID = "masterid";
77 private static final String CHASSIS_ID = "chassisid";
78 private static final String SERIAL = "serial";
79 private static final String PORTS = "ports";
80 private static final String ENABLED = "enabled";
81 private static final String SPEED = "speed";
Thomas Vachuskab52a0142015-04-21 17:48:15 -070082 private static final String NAME = "name";
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -070083
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -070084
Simon Hunt3d1b0652015-05-05 17:27:24 -070085 private static final String[] COL_IDS = {
86 AVAILABLE, AVAILABLE_IID, TYPE_IID, ID,
87 NUM_PORTS, MASTER_ID, MFR, HW, SW,
88 PROTOCOL, CHASSIS_ID, SERIAL
89 };
90
91 private static final String ICON_ID_ONLINE = "active";
92 private static final String ICON_ID_OFFLINE = "inactive";
93
Thomas Vachuskae586b792015-03-26 13:59:38 -070094 @Override
Simon Huntda580882015-05-12 20:58:18 -070095 protected Collection<RequestHandler> createRequestHandlers() {
Simon Huntd2747a02015-04-30 22:41:16 -070096 return ImmutableSet.of(
97 new DataRequestHandler(),
Thomas Vachuska8f62f572015-10-06 12:17:44 -070098 new NameChangeHandler(),
Simon Huntd2747a02015-04-30 22:41:16 -070099 new DetailRequestHandler()
100 );
101 }
102
Simon Hunt3d1b0652015-05-05 17:27:24 -0700103 private static String getTypeIconId(Device d) {
104 return DEV_ICON_PREFIX + d.type().toString();
105 }
106
Simon Huntabd16f62015-05-01 13:14:40 -0700107 // handler for device table requests
108 private final class DataRequestHandler extends TableRequestHandler {
Simon Huntd2747a02015-04-30 22:41:16 -0700109 private DataRequestHandler() {
Simon Huntabd16f62015-05-01 13:14:40 -0700110 super(DEV_DATA_REQ, DEV_DATA_RESP, DEVICES);
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700111 }
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700112
Simon Huntd2747a02015-04-30 22:41:16 -0700113 @Override
Simon Hunt3d1b0652015-05-05 17:27:24 -0700114 protected String[] getColumnIds() {
115 return COL_IDS;
116 }
117
118 @Override
Simon Hunt3d1b0652015-05-05 17:27:24 -0700119 protected void populateTable(TableModel tm, ObjectNode payload) {
120 DeviceService ds = get(DeviceService.class);
121 MastershipService ms = get(MastershipService.class);
122 for (Device dev : ds.getDevices()) {
123 populateRow(tm.addRow(), dev, ds, ms);
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700124 }
Simon Hunt3d1b0652015-05-05 17:27:24 -0700125 }
126
127 private void populateRow(TableModel.Row row, Device dev,
128 DeviceService ds, MastershipService ms) {
129 DeviceId id = dev.id();
130 boolean available = ds.isAvailable(id);
131 String iconId = available ? ICON_ID_ONLINE : ICON_ID_OFFLINE;
132
Thomas Vachuska07992802015-05-14 20:45:41 -0700133 String protocol = dev.annotations().value(PROTOCOL);
134
Simon Hunt3d1b0652015-05-05 17:27:24 -0700135 row.cell(ID, id)
136 .cell(AVAILABLE, available)
137 .cell(AVAILABLE_IID, iconId)
138 .cell(TYPE_IID, getTypeIconId(dev))
139 .cell(MFR, dev.manufacturer())
140 .cell(HW, dev.hwVersion())
141 .cell(SW, dev.swVersion())
Thomas Vachuska07992802015-05-14 20:45:41 -0700142 .cell(PROTOCOL, protocol != null ? protocol : "")
Simon Hunt3d1b0652015-05-05 17:27:24 -0700143 .cell(NUM_PORTS, ds.getPorts(id).size())
144 .cell(MASTER_ID, ms.getMasterFor(id));
Simon Huntd2747a02015-04-30 22:41:16 -0700145 }
146 }
147
Simon Huntabd16f62015-05-01 13:14:40 -0700148 // handler for selected device detail requests
Simon Huntd2747a02015-04-30 22:41:16 -0700149 private final class DetailRequestHandler extends RequestHandler {
150 private DetailRequestHandler() {
Simon Huntabd16f62015-05-01 13:14:40 -0700151 super(DEV_DETAILS_REQ);
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700152 }
153
Simon Huntd2747a02015-04-30 22:41:16 -0700154 @Override
155 public void process(long sid, ObjectNode payload) {
156 String id = string(payload, "id", "of:0000000000000000");
157
Thomas Vachuska8f62f572015-10-06 12:17:44 -0700158 DeviceId deviceId = deviceId(id);
Simon Huntd2747a02015-04-30 22:41:16 -0700159 DeviceService service = get(DeviceService.class);
160 MastershipService ms = get(MastershipService.class);
161 Device device = service.getDevice(deviceId);
162 ObjectNode data = MAPPER.createObjectNode();
163
164 data.put(ID, deviceId.toString());
Simon Huntd766c312015-10-06 11:46:32 -0700165
Thomas Vachuska8f62f572015-10-06 12:17:44 -0700166 // Get friendly name of the device from the annotations
167 String name = device.annotations().value(AnnotationKeys.NAME);
168 data.put(NAME, isNullOrEmpty(name) ? deviceId.toString() : name);
Simon Huntd766c312015-10-06 11:46:32 -0700169
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700170 data.put(TYPE, capitalizeFully(device.type().toString()));
Simon Huntd2747a02015-04-30 22:41:16 -0700171 data.put(TYPE_IID, getTypeIconId(device));
172 data.put(MFR, device.manufacturer());
173 data.put(HW, device.hwVersion());
174 data.put(SW, device.swVersion());
175 data.put(SERIAL, device.serialNumber());
176 data.put(CHASSIS_ID, device.chassisId().toString());
177 data.put(MASTER_ID, ms.getMasterFor(deviceId).toString());
178 data.put(PROTOCOL, device.annotations().value(PROTOCOL));
179
180 ArrayNode ports = MAPPER.createArrayNode();
181
182 List<Port> portList = new ArrayList<>(service.getPorts(deviceId));
183 Collections.sort(portList, (p1, p2) -> {
184 long delta = p1.number().toLong() - p2.number().toLong();
185 return delta == 0 ? 0 : (delta < 0 ? -1 : +1);
186 });
187
188 for (Port p : portList) {
189 ports.add(portData(p, deviceId));
190 }
191 data.set(PORTS, ports);
192
193 ObjectNode rootNode = MAPPER.createObjectNode();
Simon Huntabd16f62015-05-01 13:14:40 -0700194 rootNode.set(DETAILS, data);
195 sendMessage(DEV_DETAILS_RESP, 0, rootNode);
Simon Huntd2747a02015-04-30 22:41:16 -0700196 }
197
198 private ObjectNode portData(Port p, DeviceId id) {
199 ObjectNode port = MAPPER.createObjectNode();
200 LinkService ls = get(LinkService.class);
201 String name = p.annotations().value(AnnotationKeys.PORT_NAME);
202
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700203 port.put(ID, capitalizeFully(p.number().toString()));
204 port.put(TYPE, capitalizeFully(p.type().toString()));
Simon Huntd2747a02015-04-30 22:41:16 -0700205 port.put(SPEED, p.portSpeed());
206 port.put(ENABLED, p.isEnabled());
207 port.put(NAME, name != null ? name : "");
208
209 Set<Link> links = ls.getEgressLinks(new ConnectPoint(id, p.number()));
210 if (!links.isEmpty()) {
211 StringBuilder egressLinks = new StringBuilder();
212 for (Link l : links) {
213 ConnectPoint dest = l.dst();
214 egressLinks.append(dest.elementId()).append("/")
215 .append(dest.port()).append(" ");
216 }
217 port.put(LINK_DEST, egressLinks.toString());
218 }
219
220 return port;
221 }
Bri Prebilic Cole0feedc02015-04-09 14:17:37 -0700222 }
Thomas Vachuska8f62f572015-10-06 12:17:44 -0700223
224 // handler for changing device friendly name
225 private final class NameChangeHandler extends RequestHandler {
226 private NameChangeHandler() {
227 super(DEV_NAME_CHANGE_REQ);
228 }
229
230 @Override
231 public void process(long sid, ObjectNode payload) {
232 DeviceId deviceId = deviceId(string(payload, "id", "of:0000000000000000"));
233 NetworkConfigService service = get(NetworkConfigService.class);
234 BasicDeviceConfig cfg = service.getConfig(deviceId, BasicDeviceConfig.class);
235
236 // Name attribute missing (or being empty) from the payload means
237 // that the friendly name should be unset.
238 cfg.name(emptyToNull(string(payload, "name", null)));
239 cfg.apply();
240 sendMessage(DEV_NAME_CHANGE_RESP, 0, payload);
241 }
242 }
Thomas Vachuskae586b792015-03-26 13:59:38 -0700243}