blob: 52fca5854fb21e4ac350507c9c832e402c45e55d [file] [log] [blame]
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -08001/*
2 * Copyright 2014 Open Networking Laboratory
3 *
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 */
16package org.onlab.onos.rest;
17
18import com.fasterxml.jackson.databind.node.ObjectNode;
19import org.onlab.onos.net.Device;
20import org.onlab.onos.net.Port;
21import org.onlab.onos.net.device.DeviceService;
22
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;
30import static org.onlab.onos.net.DeviceId.deviceId;
31
32/**
33 * REST resource for interacting with the inventory of infrastructure devices.
34 */
35@Path("devices")
36public class DevicesWebResource extends AbstractWebResource {
37
38 public static final String DEVICE_NOT_FOUND = "Device is not found";
39
40 @GET
41 public Response getDevices() {
42 Iterable<Device> devices = get(DeviceService.class).getDevices();
43 return ok(encodeArray(Device.class, "devices", devices)).build();
44 }
45
46 @GET
47 @Path("{id}")
48 public Response getDevice(@PathParam("id") String id) {
49 Device device = nullIsNotFound(get(DeviceService.class).getDevice(deviceId(id)),
50 DEVICE_NOT_FOUND);
51 return ok(codec(Device.class).encode(device, this)).build();
52 }
53
54 @GET
55 @Path("{id}/ports")
56 public Response getDevicePorts(@PathParam("id") String id) {
57 DeviceService service = get(DeviceService.class);
58 Device device = nullIsNotFound(service.getDevice(deviceId(id)), DEVICE_NOT_FOUND);
59 List<Port> ports = checkNotNull(service.getPorts(deviceId(id)), "Ports could not be retrieved");
60 ObjectNode result = codec(Device.class).encode(device, this);
61 result.set("ports", codec(Port.class).encode(ports, this));
62 return ok(result).build();
63 }
64
65}