Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 Open Networking Laboratory |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 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 | */ |
Jonathan Hart | 9bb32ab | 2015-05-05 18:17:31 -0700 | [diff] [blame] | 16 | package org.onosproject.rest.resources; |
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; |
Thomas Vachuska | 82e95a5 | 2015-07-25 13:08:00 -0700 | [diff] [blame] | 21 | import org.onosproject.net.device.DeviceAdminService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 22 | import org.onosproject.net.device.DeviceService; |
Jonathan Hart | 9bb32ab | 2015-05-05 18:17:31 -0700 | [diff] [blame] | 23 | import org.onosproject.rest.AbstractWebResource; |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 24 | |
Thomas Vachuska | 82e95a5 | 2015-07-25 13:08:00 -0700 | [diff] [blame] | 25 | import javax.ws.rs.DELETE; |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 26 | import javax.ws.rs.GET; |
| 27 | import javax.ws.rs.Path; |
| 28 | import javax.ws.rs.PathParam; |
| 29 | import javax.ws.rs.core.Response; |
| 30 | import java.util.List; |
| 31 | |
| 32 | import static com.google.common.base.Preconditions.checkNotNull; |
Thomas Vachuska | f8cac48 | 2015-04-08 19:40:12 -0700 | [diff] [blame] | 33 | import static org.onlab.util.Tools.nullIsNotFound; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 34 | import static org.onosproject.net.DeviceId.deviceId; |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 35 | |
| 36 | /** |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 37 | * Manage inventory of infrastructure devices. |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 38 | */ |
| 39 | @Path("devices") |
| 40 | public class DevicesWebResource extends AbstractWebResource { |
| 41 | |
| 42 | public static final String DEVICE_NOT_FOUND = "Device is not found"; |
| 43 | |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 44 | /** |
| 45 | * Get all infrastructure devices. |
| 46 | * Returns array of all discovered infrastructure devices. |
| 47 | * |
| 48 | * @return 200 OK |
Andrea Campanella | 10c4adc | 2015-12-03 15:27:54 -0800 | [diff] [blame] | 49 | * @onos.rsModel DevicesGet |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 50 | */ |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 51 | @GET |
| 52 | public Response getDevices() { |
| 53 | Iterable<Device> devices = get(DeviceService.class).getDevices(); |
| 54 | return ok(encodeArray(Device.class, "devices", devices)).build(); |
| 55 | } |
| 56 | |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 57 | /** |
| 58 | * Get details of infrastructure device. |
| 59 | * Returns details of the specified infrastructure device. |
| 60 | * |
| 61 | * @param id device identifier |
| 62 | * @return 200 OK |
Andrea Campanella | 10c4adc | 2015-12-03 15:27:54 -0800 | [diff] [blame] | 63 | * @onos.rsModel DeviceGet |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 64 | */ |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 65 | @GET |
| 66 | @Path("{id}") |
| 67 | public Response getDevice(@PathParam("id") String id) { |
| 68 | Device device = nullIsNotFound(get(DeviceService.class).getDevice(deviceId(id)), |
| 69 | DEVICE_NOT_FOUND); |
| 70 | return ok(codec(Device.class).encode(device, this)).build(); |
| 71 | } |
| 72 | |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 73 | /** |
| 74 | * Remove infrastructure device. |
| 75 | * Administratively deletes the specified device from the inventory of |
| 76 | * known devices. |
| 77 | * |
| 78 | * @param id device identifier |
| 79 | * @return 200 OK |
| 80 | */ |
Thomas Vachuska | 82e95a5 | 2015-07-25 13:08:00 -0700 | [diff] [blame] | 81 | @DELETE |
| 82 | @Path("{id}") |
| 83 | public Response removeDevice(@PathParam("id") String id) { |
| 84 | Device device = nullIsNotFound(get(DeviceService.class).getDevice(deviceId(id)), |
| 85 | DEVICE_NOT_FOUND); |
| 86 | get(DeviceAdminService.class).removeDevice(deviceId(id)); |
| 87 | return ok(codec(Device.class).encode(device, this)).build(); |
| 88 | } |
| 89 | |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 90 | /** |
| 91 | * Get ports of infrastructure device. |
| 92 | * Returns details of the specified infrastructure device. |
| 93 | * |
Andrea Campanella | 10c4adc | 2015-12-03 15:27:54 -0800 | [diff] [blame] | 94 | * @onos.rsModel DeviceGetPorts |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 95 | * @param id device identifier |
| 96 | * @return 200 OK |
| 97 | */ |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 98 | @GET |
| 99 | @Path("{id}/ports") |
| 100 | public Response getDevicePorts(@PathParam("id") String id) { |
| 101 | DeviceService service = get(DeviceService.class); |
| 102 | Device device = nullIsNotFound(service.getDevice(deviceId(id)), DEVICE_NOT_FOUND); |
| 103 | List<Port> ports = checkNotNull(service.getPorts(deviceId(id)), "Ports could not be retrieved"); |
| 104 | ObjectNode result = codec(Device.class).encode(device, this); |
| 105 | result.set("ports", codec(Port.class).encode(ports, this)); |
| 106 | return ok(result).build(); |
| 107 | } |
| 108 | |
| 109 | } |