Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 1 | /* |
| 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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.rest; |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 17 | |
| 18 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 19 | import org.onosproject.net.Device; |
| 20 | import org.onosproject.net.Port; |
| 21 | import org.onosproject.net.device.DeviceService; |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 22 | |
| 23 | import javax.ws.rs.GET; |
| 24 | import javax.ws.rs.Path; |
| 25 | import javax.ws.rs.PathParam; |
| 26 | import javax.ws.rs.core.Response; |
| 27 | import java.util.List; |
| 28 | |
| 29 | import static com.google.common.base.Preconditions.checkNotNull; |
Thomas Vachuska | f8cac48 | 2015-04-08 19:40:12 -0700 | [diff] [blame] | 30 | import static org.onlab.util.Tools.nullIsNotFound; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 31 | import static org.onosproject.net.DeviceId.deviceId; |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * REST resource for interacting with the inventory of infrastructure devices. |
| 35 | */ |
| 36 | @Path("devices") |
| 37 | public 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 | } |