Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 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 | */ |
| 16 | package org.onosproject.ui.impl; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 20 | import com.google.common.collect.ImmutableSet; |
Thomas Vachuska | c4178cc | 2015-12-10 11:43:32 -0800 | [diff] [blame] | 21 | import org.onosproject.cluster.NodeId; |
Bri Prebilic Cole | ae65c96 | 2015-04-02 16:24:49 -0700 | [diff] [blame] | 22 | import org.onosproject.mastership.MastershipService; |
Thomas Vachuska | b52a014 | 2015-04-21 17:48:15 -0700 | [diff] [blame] | 23 | import org.onosproject.net.AnnotationKeys; |
Bri Prebilic Cole | 0feedc0 | 2015-04-09 14:17:37 -0700 | [diff] [blame] | 24 | import org.onosproject.net.ConnectPoint; |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 25 | import org.onosproject.net.Device; |
Bri Prebilic Cole | 264c5ec | 2015-04-07 10:22:26 -0700 | [diff] [blame] | 26 | import org.onosproject.net.DeviceId; |
Thomas Vachuska | 018110c | 2016-07-05 12:21:38 -0700 | [diff] [blame] | 27 | import org.onosproject.net.Host; |
Bri Prebilic Cole | 0feedc0 | 2015-04-09 14:17:37 -0700 | [diff] [blame] | 28 | import org.onosproject.net.Link; |
Bri Prebilic Cole | 264c5ec | 2015-04-07 10:22:26 -0700 | [diff] [blame] | 29 | import org.onosproject.net.Port; |
Thomas Vachuska | 8f62f57 | 2015-10-06 12:17:44 -0700 | [diff] [blame] | 30 | import org.onosproject.net.config.NetworkConfigService; |
| 31 | import org.onosproject.net.config.basics.BasicDeviceConfig; |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 32 | import org.onosproject.net.device.DeviceService; |
Thomas Vachuska | 018110c | 2016-07-05 12:21:38 -0700 | [diff] [blame] | 33 | import org.onosproject.net.host.HostService; |
Bri Prebilic Cole | 264c5ec | 2015-04-07 10:22:26 -0700 | [diff] [blame] | 34 | import org.onosproject.net.link.LinkService; |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 35 | import org.onosproject.ui.RequestHandler; |
Simon Hunt | a0ddb02 | 2015-05-01 09:53:01 -0700 | [diff] [blame] | 36 | import org.onosproject.ui.UiMessageHandler; |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 37 | import org.onosproject.ui.table.TableModel; |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 38 | import org.onosproject.ui.table.TableRequestHandler; |
Simon Hunt | d557925 | 2015-10-06 15:09:14 -0700 | [diff] [blame] | 39 | import org.slf4j.Logger; |
| 40 | import org.slf4j.LoggerFactory; |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 41 | |
| 42 | import java.util.ArrayList; |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 43 | import java.util.Collection; |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 44 | import java.util.List; |
Bri Prebilic Cole | 0feedc0 | 2015-04-09 14:17:37 -0700 | [diff] [blame] | 45 | import java.util.Set; |
Bri Prebilic Cole | 264c5ec | 2015-04-07 10:22:26 -0700 | [diff] [blame] | 46 | |
Thomas Vachuska | 8f62f57 | 2015-10-06 12:17:44 -0700 | [diff] [blame] | 47 | import static com.google.common.base.Strings.emptyToNull; |
| 48 | import static com.google.common.base.Strings.isNullOrEmpty; |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 49 | import static org.apache.commons.lang.WordUtils.capitalizeFully; |
Thomas Vachuska | 8f62f57 | 2015-10-06 12:17:44 -0700 | [diff] [blame] | 50 | import static org.onosproject.net.DeviceId.deviceId; |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 51 | |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 52 | /** |
| 53 | * Message handler for device view related messages. |
| 54 | */ |
Simon Hunt | a0ddb02 | 2015-05-01 09:53:01 -0700 | [diff] [blame] | 55 | public class DeviceViewMessageHandler extends UiMessageHandler { |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 56 | |
| 57 | private static final String DEV_DATA_REQ = "deviceDataRequest"; |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 58 | private static final String DEV_DATA_RESP = "deviceDataResponse"; |
| 59 | private static final String DEVICES = "devices"; |
Alan Deikman | 8d85875 | 2017-02-14 18:08:29 -0800 | [diff] [blame] | 60 | private static final String DEVICE = "device"; |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 61 | |
| 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 Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 65 | |
Thomas Vachuska | 8f62f57 | 2015-10-06 12:17:44 -0700 | [diff] [blame] | 66 | private static final String DEV_NAME_CHANGE_REQ = "deviceNameChangeRequest"; |
| 67 | private static final String DEV_NAME_CHANGE_RESP = "deviceNameChangeResponse"; |
| 68 | |
Simon Hunt | d557925 | 2015-10-06 15:09:14 -0700 | [diff] [blame] | 69 | private static final String ZERO_URI = "of:0000000000000000"; |
| 70 | |
Bri Prebilic Cole | 0feedc0 | 2015-04-09 14:17:37 -0700 | [diff] [blame] | 71 | 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 Vachuska | b52a014 | 2015-04-21 17:48:15 -0700 | [diff] [blame] | 89 | private static final String NAME = "name"; |
Simon Hunt | d557925 | 2015-10-06 15:09:14 -0700 | [diff] [blame] | 90 | private static final String WARN = "warn"; |
Bri Prebilic Cole | 0feedc0 | 2015-04-09 14:17:37 -0700 | [diff] [blame] | 91 | |
Thomas Vachuska | c4178cc | 2015-12-10 11:43:32 -0800 | [diff] [blame] | 92 | private static final String NONE = "none"; |
Bri Prebilic Cole | 0feedc0 | 2015-04-09 14:17:37 -0700 | [diff] [blame] | 93 | |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 94 | private static final String[] COL_IDS = { |
Simon Hunt | d557925 | 2015-10-06 15:09:14 -0700 | [diff] [blame] | 95 | AVAILABLE, AVAILABLE_IID, TYPE_IID, |
| 96 | NAME, ID, MASTER_ID, NUM_PORTS, MFR, HW, SW, |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 97 | 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 Hunt | d557925 | 2015-10-06 15:09:14 -0700 | [diff] [blame] | 103 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 104 | |
| 105 | |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 106 | @Override |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 107 | protected Collection<RequestHandler> createRequestHandlers() { |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 108 | return ImmutableSet.of( |
| 109 | new DataRequestHandler(), |
Thomas Vachuska | 8f62f57 | 2015-10-06 12:17:44 -0700 | [diff] [blame] | 110 | new NameChangeHandler(), |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 111 | new DetailRequestHandler() |
| 112 | ); |
| 113 | } |
| 114 | |
Simon Hunt | d557925 | 2015-10-06 15:09:14 -0700 | [diff] [blame] | 115 | // 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 Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 126 | private static String getTypeIconId(Device d) { |
| 127 | return DEV_ICON_PREFIX + d.type().toString(); |
| 128 | } |
| 129 | |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 130 | // handler for device table requests |
| 131 | private final class DataRequestHandler extends TableRequestHandler { |
Jian Li | 69f6663 | 2016-01-15 12:27:42 -0800 | [diff] [blame] | 132 | private static final String NO_ROWS_MESSAGE = "No devices found"; |
| 133 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 134 | private DataRequestHandler() { |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 135 | super(DEV_DATA_REQ, DEV_DATA_RESP, DEVICES); |
Bri Prebilic Cole | 0feedc0 | 2015-04-09 14:17:37 -0700 | [diff] [blame] | 136 | } |
Bri Prebilic Cole | 0feedc0 | 2015-04-09 14:17:37 -0700 | [diff] [blame] | 137 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 138 | @Override |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 139 | protected String[] getColumnIds() { |
| 140 | return COL_IDS; |
| 141 | } |
| 142 | |
| 143 | @Override |
Jian Li | 8baf447 | 2016-01-15 15:08:09 -0800 | [diff] [blame] | 144 | protected String noRowsMessage(ObjectNode payload) { |
Jian Li | 69f6663 | 2016-01-15 12:27:42 -0800 | [diff] [blame] | 145 | return NO_ROWS_MESSAGE; |
| 146 | } |
| 147 | |
| 148 | @Override |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 149 | 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 Cole | 0feedc0 | 2015-04-09 14:17:37 -0700 | [diff] [blame] | 154 | } |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 155 | } |
| 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 Hunt | d557925 | 2015-10-06 15:09:14 -0700 | [diff] [blame] | 164 | .cell(NAME, deviceName(dev)) |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 165 | .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 Hunt | d557925 | 2015-10-06 15:09:14 -0700 | [diff] [blame] | 171 | .cell(PROTOCOL, deviceProtocol(dev)) |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 172 | .cell(NUM_PORTS, ds.getPorts(id).size()) |
| 173 | .cell(MASTER_ID, ms.getMasterFor(id)); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 177 | // handler for selected device detail requests |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 178 | private final class DetailRequestHandler extends RequestHandler { |
| 179 | private DetailRequestHandler() { |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 180 | super(DEV_DETAILS_REQ); |
Bri Prebilic Cole | 0feedc0 | 2015-04-09 14:17:37 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 183 | @Override |
Simon Hunt | 8a0429a | 2017-01-06 16:52:47 -0800 | [diff] [blame] | 184 | public void process(ObjectNode payload) { |
Simon Hunt | d557925 | 2015-10-06 15:09:14 -0700 | [diff] [blame] | 185 | String id = string(payload, ID, ZERO_URI); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 186 | |
Thomas Vachuska | 8f62f57 | 2015-10-06 12:17:44 -0700 | [diff] [blame] | 187 | DeviceId deviceId = deviceId(id); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 188 | DeviceService service = get(DeviceService.class); |
| 189 | MastershipService ms = get(MastershipService.class); |
| 190 | Device device = service.getDevice(deviceId); |
Simon Hunt | d557925 | 2015-10-06 15:09:14 -0700 | [diff] [blame] | 191 | ObjectNode data = objectNode(); |
Thomas Vachuska | c4178cc | 2015-12-10 11:43:32 -0800 | [diff] [blame] | 192 | NodeId masterFor = ms.getMasterFor(deviceId); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 193 | |
| 194 | data.put(ID, deviceId.toString()); |
Simon Hunt | d557925 | 2015-10-06 15:09:14 -0700 | [diff] [blame] | 195 | data.put(NAME, deviceName(device)); |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 196 | data.put(TYPE, capitalizeFully(device.type().toString())); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 197 | 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 Vachuska | c4178cc | 2015-12-10 11:43:32 -0800 | [diff] [blame] | 203 | data.put(MASTER_ID, masterFor != null ? masterFor.toString() : NONE); |
Simon Hunt | d557925 | 2015-10-06 15:09:14 -0700 | [diff] [blame] | 204 | data.put(PROTOCOL, deviceProtocol(device)); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 205 | |
Simon Hunt | d557925 | 2015-10-06 15:09:14 -0700 | [diff] [blame] | 206 | ArrayNode ports = arrayNode(); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 207 | |
| 208 | List<Port> portList = new ArrayList<>(service.getPorts(deviceId)); |
Simon Hunt | 8a0429a | 2017-01-06 16:52:47 -0800 | [diff] [blame] | 209 | portList.sort((p1, p2) -> { |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 210 | 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 Hunt | d557925 | 2015-10-06 15:09:14 -0700 | [diff] [blame] | 219 | ObjectNode rootNode = objectNode(); |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 220 | rootNode.set(DETAILS, data); |
Alan Deikman | 8d85875 | 2017-02-14 18:08:29 -0800 | [diff] [blame] | 221 | |
Simon Hunt | cc035c5 | 2017-02-22 21:12:51 -0800 | [diff] [blame] | 222 | // 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 Deikman | 8d85875 | 2017-02-14 18:08:29 -0800 | [diff] [blame] | 224 | rootNode.set(DEVICE, getJsonCodecContext().encode(device, Device.class)); |
| 225 | |
Simon Hunt | 8a0429a | 2017-01-06 16:52:47 -0800 | [diff] [blame] | 226 | sendMessage(DEV_DETAILS_RESP, rootNode); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | private ObjectNode portData(Port p, DeviceId id) { |
Simon Hunt | d557925 | 2015-10-06 15:09:14 -0700 | [diff] [blame] | 230 | ObjectNode port = objectNode(); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 231 | LinkService ls = get(LinkService.class); |
| 232 | String name = p.annotations().value(AnnotationKeys.PORT_NAME); |
| 233 | |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 234 | port.put(ID, capitalizeFully(p.number().toString())); |
| 235 | port.put(TYPE, capitalizeFully(p.type().toString())); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 236 | port.put(SPEED, p.portSpeed()); |
| 237 | port.put(ENABLED, p.isEnabled()); |
| 238 | port.put(NAME, name != null ? name : ""); |
| 239 | |
Thomas Vachuska | 018110c | 2016-07-05 12:21:38 -0700 | [diff] [blame] | 240 | ConnectPoint connectPoint = new ConnectPoint(id, p.number()); |
| 241 | Set<Link> links = ls.getEgressLinks(connectPoint); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 242 | 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 Vachuska | 018110c | 2016-07-05 12:21:38 -0700 | [diff] [blame] | 250 | } 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 Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | return port; |
| 259 | } |
Bri Prebilic Cole | 0feedc0 | 2015-04-09 14:17:37 -0700 | [diff] [blame] | 260 | } |
Thomas Vachuska | 8f62f57 | 2015-10-06 12:17:44 -0700 | [diff] [blame] | 261 | |
Simon Hunt | d557925 | 2015-10-06 15:09:14 -0700 | [diff] [blame] | 262 | |
Thomas Vachuska | 8f62f57 | 2015-10-06 12:17:44 -0700 | [diff] [blame] | 263 | // 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 Hunt | 8a0429a | 2017-01-06 16:52:47 -0800 | [diff] [blame] | 270 | public void process(ObjectNode payload) { |
Simon Hunt | d557925 | 2015-10-06 15:09:14 -0700 | [diff] [blame] | 271 | 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 Vachuska | 8f62f57 | 2015-10-06 12:17:44 -0700 | [diff] [blame] | 274 | |
Simon Hunt | d557925 | 2015-10-06 15:09:14 -0700 | [diff] [blame] | 275 | 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 Vachuska | 8f62f57 | 2015-10-06 12:17:44 -0700 | [diff] [blame] | 282 | cfg.apply(); |
Simon Hunt | 8a0429a | 2017-01-06 16:52:47 -0800 | [diff] [blame] | 283 | sendMessage(DEV_NAME_CHANGE_RESP, payload); |
Thomas Vachuska | 8f62f57 | 2015-10-06 12:17:44 -0700 | [diff] [blame] | 284 | } |
| 285 | } |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 286 | } |