blob: 1604752a116c23ab74f6e8fb779c657ce00b0fc9 [file] [log] [blame]
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -08001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 Open Networking Laboratory
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -08003 *
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 */
Jonathan Hart9bb32ab2015-05-05 18:17:31 -070016package org.onosproject.rest.resources;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080017
18import com.fasterxml.jackson.databind.node.ObjectNode;
Brian O'Connorabafb502014-12-02 22:26:20 -080019import org.onosproject.net.Device;
20import org.onosproject.net.Port;
Thomas Vachuska82e95a52015-07-25 13:08:00 -070021import org.onosproject.net.device.DeviceAdminService;
Brian O'Connorabafb502014-12-02 22:26:20 -080022import org.onosproject.net.device.DeviceService;
Jonathan Hart9bb32ab2015-05-05 18:17:31 -070023import org.onosproject.rest.AbstractWebResource;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080024
Thomas Vachuska82e95a52015-07-25 13:08:00 -070025import javax.ws.rs.DELETE;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080026import javax.ws.rs.GET;
27import javax.ws.rs.Path;
28import javax.ws.rs.PathParam;
29import javax.ws.rs.core.Response;
30import java.util.List;
31
32import static com.google.common.base.Preconditions.checkNotNull;
Thomas Vachuskaf8cac482015-04-08 19:40:12 -070033import static org.onlab.util.Tools.nullIsNotFound;
Brian O'Connorabafb502014-12-02 22:26:20 -080034import static org.onosproject.net.DeviceId.deviceId;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080035
36/**
37 * REST resource for interacting with the inventory of infrastructure devices.
38 */
39@Path("devices")
40public class DevicesWebResource extends AbstractWebResource {
41
42 public static final String DEVICE_NOT_FOUND = "Device is not found";
43
44 @GET
45 public Response getDevices() {
46 Iterable<Device> devices = get(DeviceService.class).getDevices();
47 return ok(encodeArray(Device.class, "devices", devices)).build();
48 }
49
50 @GET
51 @Path("{id}")
52 public Response getDevice(@PathParam("id") String id) {
53 Device device = nullIsNotFound(get(DeviceService.class).getDevice(deviceId(id)),
54 DEVICE_NOT_FOUND);
55 return ok(codec(Device.class).encode(device, this)).build();
56 }
57
Thomas Vachuska82e95a52015-07-25 13:08:00 -070058 @DELETE
59 @Path("{id}")
60 public Response removeDevice(@PathParam("id") String id) {
61 Device device = nullIsNotFound(get(DeviceService.class).getDevice(deviceId(id)),
62 DEVICE_NOT_FOUND);
63 get(DeviceAdminService.class).removeDevice(deviceId(id));
64 return ok(codec(Device.class).encode(device, this)).build();
65 }
66
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080067 @GET
68 @Path("{id}/ports")
69 public Response getDevicePorts(@PathParam("id") String id) {
70 DeviceService service = get(DeviceService.class);
71 Device device = nullIsNotFound(service.getDevice(deviceId(id)), DEVICE_NOT_FOUND);
72 List<Port> ports = checkNotNull(service.getPorts(deviceId(id)), "Ports could not be retrieved");
73 ObjectNode result = codec(Device.class).encode(device, this);
74 result.set("ports", codec(Port.class).encode(ports, this));
75 return ok(result).build();
76 }
77
78}