Thomas Vachuska

Sketched out server-side GUI code for setting/getting device friendly name.

Change-Id: If9cfe6b549fc312495f429f05f0907e0e24f2ee7
...@@ -25,6 +25,8 @@ import org.onosproject.net.Device; ...@@ -25,6 +25,8 @@ import org.onosproject.net.Device;
25 import org.onosproject.net.DeviceId; 25 import org.onosproject.net.DeviceId;
26 import org.onosproject.net.Link; 26 import org.onosproject.net.Link;
27 import org.onosproject.net.Port; 27 import org.onosproject.net.Port;
28 +import org.onosproject.net.config.NetworkConfigService;
29 +import org.onosproject.net.config.basics.BasicDeviceConfig;
28 import org.onosproject.net.device.DeviceService; 30 import org.onosproject.net.device.DeviceService;
29 import org.onosproject.net.link.LinkService; 31 import org.onosproject.net.link.LinkService;
30 import org.onosproject.ui.RequestHandler; 32 import org.onosproject.ui.RequestHandler;
...@@ -38,7 +40,10 @@ import java.util.Collections; ...@@ -38,7 +40,10 @@ import java.util.Collections;
38 import java.util.List; 40 import java.util.List;
39 import java.util.Set; 41 import java.util.Set;
40 42
43 +import static com.google.common.base.Strings.emptyToNull;
44 +import static com.google.common.base.Strings.isNullOrEmpty;
41 import static org.apache.commons.lang.WordUtils.capitalizeFully; 45 import static org.apache.commons.lang.WordUtils.capitalizeFully;
46 +import static org.onosproject.net.DeviceId.deviceId;
42 47
43 /** 48 /**
44 * Message handler for device view related messages. 49 * Message handler for device view related messages.
...@@ -53,6 +58,9 @@ public class DeviceViewMessageHandler extends UiMessageHandler { ...@@ -53,6 +58,9 @@ public class DeviceViewMessageHandler extends UiMessageHandler {
53 private static final String DEV_DETAILS_RESP = "deviceDetailsResponse"; 58 private static final String DEV_DETAILS_RESP = "deviceDetailsResponse";
54 private static final String DETAILS = "details"; 59 private static final String DETAILS = "details";
55 60
61 + private static final String DEV_NAME_CHANGE_REQ = "deviceNameChangeRequest";
62 + private static final String DEV_NAME_CHANGE_RESP = "deviceNameChangeResponse";
63 +
56 private static final String ID = "id"; 64 private static final String ID = "id";
57 private static final String TYPE = "type"; 65 private static final String TYPE = "type";
58 private static final String AVAILABLE = "available"; 66 private static final String AVAILABLE = "available";
...@@ -87,6 +95,7 @@ public class DeviceViewMessageHandler extends UiMessageHandler { ...@@ -87,6 +95,7 @@ public class DeviceViewMessageHandler extends UiMessageHandler {
87 protected Collection<RequestHandler> createRequestHandlers() { 95 protected Collection<RequestHandler> createRequestHandlers() {
88 return ImmutableSet.of( 96 return ImmutableSet.of(
89 new DataRequestHandler(), 97 new DataRequestHandler(),
98 + new NameChangeHandler(),
90 new DetailRequestHandler() 99 new DetailRequestHandler()
91 ); 100 );
92 } 101 }
...@@ -146,7 +155,7 @@ public class DeviceViewMessageHandler extends UiMessageHandler { ...@@ -146,7 +155,7 @@ public class DeviceViewMessageHandler extends UiMessageHandler {
146 public void process(long sid, ObjectNode payload) { 155 public void process(long sid, ObjectNode payload) {
147 String id = string(payload, "id", "of:0000000000000000"); 156 String id = string(payload, "id", "of:0000000000000000");
148 157
149 - DeviceId deviceId = DeviceId.deviceId(id); 158 + DeviceId deviceId = deviceId(id);
150 DeviceService service = get(DeviceService.class); 159 DeviceService service = get(DeviceService.class);
151 MastershipService ms = get(MastershipService.class); 160 MastershipService ms = get(MastershipService.class);
152 Device device = service.getDevice(deviceId); 161 Device device = service.getDevice(deviceId);
...@@ -154,8 +163,9 @@ public class DeviceViewMessageHandler extends UiMessageHandler { ...@@ -154,8 +163,9 @@ public class DeviceViewMessageHandler extends UiMessageHandler {
154 163
155 data.put(ID, deviceId.toString()); 164 data.put(ID, deviceId.toString());
156 165
157 - // TODO: get friendly name from the device 166 + // Get friendly name of the device from the annotations
158 - data.put(NAME, deviceId.toString()); 167 + String name = device.annotations().value(AnnotationKeys.NAME);
168 + data.put(NAME, isNullOrEmpty(name) ? deviceId.toString() : name);
159 169
160 data.put(TYPE, capitalizeFully(device.type().toString())); 170 data.put(TYPE, capitalizeFully(device.type().toString()));
161 data.put(TYPE_IID, getTypeIconId(device)); 171 data.put(TYPE_IID, getTypeIconId(device));
...@@ -210,4 +220,24 @@ public class DeviceViewMessageHandler extends UiMessageHandler { ...@@ -210,4 +220,24 @@ public class DeviceViewMessageHandler extends UiMessageHandler {
210 return port; 220 return port;
211 } 221 }
212 } 222 }
223 +
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 + }
213 } 243 }
......