SDN Controller Programming Guide
180
String json = jsonService.toJson(device, true);
return ok(json).build();
}
@PUT
@Path("{id}")
@Produces(MediaType.APPLICATION_JSON)
public Response update(@PathParam("id") long id, String request) {
JsonService jsonService = get(JsonService.class);
Switch source = jsonService.fromJson(request, Switch.class);
SwitchService service = get(SwitchService.class);
Id<Switch, Long> deviceId = Id.valueOf(id);
Switch target = service.get(deviceId);
if (target == null) {
throw new NotFoundException(
"device with id '" + id + "' not found");
}
target.setIpAddress(source.getIpAddress());
target.setFriendlyName(source.getFriendlyName());
target.setActiveState(source.getActiveState());
service.update(target);
String json = jsonService.toJson(target, true);
return ok(json).build();
}
@DELETE
@Path("{id}")
@Produces(MediaType.APPLICATION_JSON)
public Response delete(@PathParam("id") long id) {
Id<Switch, Long> deviceId = Id.valueOf(id);
SwitchService service = get(SwitchService.class);
Switch device = service.get(deviceId);
if (device == null) {
throw new NotFoundException(
"device with id '" + id + "' not found");
}
service.delete(deviceId);
return Response.ok().build();