SDN Controller Programming Guide

179
if (id == null) {
throw new NullPointerException("id cannot be null");
}
devices.remove(id);
}
}
SwitchResource.java Delegating to Business Logic:
package com.hp.hm.rs;
...
@Path("switches")
public class SwitchResource extends ControllerResource {
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response list() {
SwitchService service = get(SwitchService.class);
List<Switch> switches = service.find(null, null);
jsonService jsonService = get(JsonService.class);
String json = jsonService.toJsonList(switches, Switch.class, true);
return ok(json).build();
}
@GET
@Path("{id}")
@Produces(MediaType.APPLICATION_JSON)
public Response get(@PathParam("id") long id) {
Id<Switch, Long> deviceId = Id.valueOf(id);
SwitchService service = get(SwitchService.class);
Switch device = service.get(deviceId);
JsonService jsonService = get(JsonService.class);
String json = jsonService.toJson(device, true);
return ok(json).build();
}
@POST
@Produces(MediaType.APPLICATION_JSON)
public Response add(String request) {
JsonService jsonService = get(JsonService.class);
Switch device = jsonService.fromJson(request, Switch.class);
SwitchService service = get(SwitchService.class);
device = service.add(device);