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