blob: 9e311d909a3935a0a91715795235dc8390b2f9df [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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.rest;
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;
21import org.onosproject.net.device.DeviceService;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080022
23import javax.ws.rs.GET;
24import javax.ws.rs.Path;
25import javax.ws.rs.PathParam;
26import javax.ws.rs.core.Response;
27import java.util.List;
28
29import static com.google.common.base.Preconditions.checkNotNull;
Thomas Vachuskaf8cac482015-04-08 19:40:12 -070030import static org.onlab.util.Tools.nullIsNotFound;
Brian O'Connorabafb502014-12-02 22:26:20 -080031import static org.onosproject.net.DeviceId.deviceId;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080032
33/**
34 * REST resource for interacting with the inventory of infrastructure devices.
35 */
36@Path("devices")
37public class DevicesWebResource extends AbstractWebResource {
38
39 public static final String DEVICE_NOT_FOUND = "Device is not found";
40
41 @GET
42 public Response getDevices() {
43 Iterable<Device> devices = get(DeviceService.class).getDevices();
44 return ok(encodeArray(Device.class, "devices", devices)).build();
45 }
46
47 @GET
48 @Path("{id}")
49 public Response getDevice(@PathParam("id") String id) {
50 Device device = nullIsNotFound(get(DeviceService.class).getDevice(deviceId(id)),
51 DEVICE_NOT_FOUND);
52 return ok(codec(Device.class).encode(device, this)).build();
53 }
54
55 @GET
56 @Path("{id}/ports")
57 public Response getDevicePorts(@PathParam("id") String id) {
58 DeviceService service = get(DeviceService.class);
59 Device device = nullIsNotFound(service.getDevice(deviceId(id)), DEVICE_NOT_FOUND);
60 List<Port> ports = checkNotNull(service.getPorts(deviceId(id)), "Ports could not be retrieved");
61 ObjectNode result = codec(Device.class).encode(device, this);
62 result.set("ports", codec(Port.class).encode(ports, this));
63 return ok(result).build();
64 }
65
66}