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 | |
Jonathan Hart | 471bcbb | 2018-04-20 13:01:10 -0700 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.JsonNode; |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
DongRyeol Cha | b1177cc | 2019-11-14 13:53:13 +0900 | [diff] [blame] | 20 | import com.google.common.collect.Lists; |
Jonathan Hart | 471bcbb | 2018-04-20 13:01:10 -0700 | [diff] [blame] | 21 | import org.onosproject.net.ConnectPoint; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 22 | import org.onosproject.net.Device; |
Jonathan Hart | 471bcbb | 2018-04-20 13:01:10 -0700 | [diff] [blame] | 23 | import org.onosproject.net.DeviceId; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 24 | import org.onosproject.net.Port; |
Jonathan Hart | 471bcbb | 2018-04-20 13:01:10 -0700 | [diff] [blame] | 25 | import org.onosproject.net.PortNumber; |
Thomas Vachuska | 82e95a5 | 2015-07-25 13:08:00 -0700 | [diff] [blame] | 26 | import org.onosproject.net.device.DeviceAdminService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 27 | import org.onosproject.net.device.DeviceService; |
Jonathan Hart | 9bb32ab | 2015-05-05 18:17:31 -0700 | [diff] [blame] | 28 | import org.onosproject.rest.AbstractWebResource; |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 29 | |
Jonathan Hart | 471bcbb | 2018-04-20 13:01:10 -0700 | [diff] [blame] | 30 | import javax.ws.rs.Consumes; |
Thomas Vachuska | 82e95a5 | 2015-07-25 13:08:00 -0700 | [diff] [blame] | 31 | import javax.ws.rs.DELETE; |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 32 | import javax.ws.rs.GET; |
Jonathan Hart | 471bcbb | 2018-04-20 13:01:10 -0700 | [diff] [blame] | 33 | import javax.ws.rs.POST; |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 34 | import javax.ws.rs.Path; |
| 35 | import javax.ws.rs.PathParam; |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 36 | import javax.ws.rs.Produces; |
| 37 | import javax.ws.rs.core.MediaType; |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 38 | import javax.ws.rs.core.Response; |
Jonathan Hart | 471bcbb | 2018-04-20 13:01:10 -0700 | [diff] [blame] | 39 | import java.io.IOException; |
| 40 | import java.io.InputStream; |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 41 | import java.util.List; |
DongRyeol Cha | b1177cc | 2019-11-14 13:53:13 +0900 | [diff] [blame] | 42 | import java.util.Optional; |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 43 | |
| 44 | import static com.google.common.base.Preconditions.checkNotNull; |
Thomas Vachuska | f8cac48 | 2015-04-08 19:40:12 -0700 | [diff] [blame] | 45 | import static org.onlab.util.Tools.nullIsNotFound; |
Jonathan Hart | 471bcbb | 2018-04-20 13:01:10 -0700 | [diff] [blame] | 46 | import static org.onlab.util.Tools.readTreeFromStream; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 47 | import static org.onosproject.net.DeviceId.deviceId; |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 48 | |
| 49 | /** |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 50 | * Manage inventory of infrastructure devices. |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 51 | */ |
| 52 | @Path("devices") |
| 53 | public class DevicesWebResource extends AbstractWebResource { |
| 54 | |
Jonathan Hart | 471bcbb | 2018-04-20 13:01:10 -0700 | [diff] [blame] | 55 | private static final String ENABLED = "enabled"; |
| 56 | |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 57 | private static final String DEVICE_NOT_FOUND = "Device is not found"; |
Jonathan Hart | 471bcbb | 2018-04-20 13:01:10 -0700 | [diff] [blame] | 58 | private static final String INVALID_JSON = "Invalid JSON data"; |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 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 all infrastructure devices. |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 62 | * Returns array of all discovered infrastructure devices. |
| 63 | * |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 64 | * @return 200 OK with a collection of devices |
Andrea Campanella | 10c4adc | 2015-12-03 15:27:54 -0800 | [diff] [blame] | 65 | * @onos.rsModel DevicesGet |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 66 | */ |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 67 | @GET |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 68 | @Produces(MediaType.APPLICATION_JSON) |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 69 | public Response getDevices() { |
| 70 | Iterable<Device> devices = get(DeviceService.class).getDevices(); |
| 71 | return ok(encodeArray(Device.class, "devices", devices)).build(); |
| 72 | } |
| 73 | |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 74 | /** |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 75 | * Gets details of infrastructure device. |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 76 | * Returns details of the specified infrastructure device. |
| 77 | * |
| 78 | * @param id device identifier |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 79 | * @return 200 OK with a device |
Andrea Campanella | 10c4adc | 2015-12-03 15:27:54 -0800 | [diff] [blame] | 80 | * @onos.rsModel DeviceGet |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 81 | */ |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 82 | @GET |
| 83 | @Path("{id}") |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 84 | @Produces(MediaType.APPLICATION_JSON) |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 85 | public Response getDevice(@PathParam("id") String id) { |
| 86 | Device device = nullIsNotFound(get(DeviceService.class).getDevice(deviceId(id)), |
| 87 | DEVICE_NOT_FOUND); |
| 88 | return ok(codec(Device.class).encode(device, this)).build(); |
| 89 | } |
| 90 | |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 91 | /** |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 92 | * Removes infrastructure device. |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 93 | * Administratively deletes the specified device from the inventory of |
| 94 | * known devices. |
| 95 | * |
| 96 | * @param id device identifier |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 97 | * @return 200 OK with the removed device |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 98 | */ |
Thomas Vachuska | 82e95a5 | 2015-07-25 13:08:00 -0700 | [diff] [blame] | 99 | @DELETE |
| 100 | @Path("{id}") |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 101 | @Produces(MediaType.APPLICATION_JSON) |
Thomas Vachuska | 82e95a5 | 2015-07-25 13:08:00 -0700 | [diff] [blame] | 102 | public Response removeDevice(@PathParam("id") String id) { |
| 103 | Device device = nullIsNotFound(get(DeviceService.class).getDevice(deviceId(id)), |
| 104 | DEVICE_NOT_FOUND); |
kalagesa | 4201954 | 2017-03-14 18:00:47 +0530 | [diff] [blame] | 105 | ObjectNode result = codec(Device.class).encode(device, this); |
Thomas Vachuska | 82e95a5 | 2015-07-25 13:08:00 -0700 | [diff] [blame] | 106 | get(DeviceAdminService.class).removeDevice(deviceId(id)); |
kalagesa | 4201954 | 2017-03-14 18:00:47 +0530 | [diff] [blame] | 107 | return ok(result).build(); |
Thomas Vachuska | 82e95a5 | 2015-07-25 13:08:00 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 110 | /** |
DongRyeol Cha | b1177cc | 2019-11-14 13:53:13 +0900 | [diff] [blame] | 111 | * Gets ports of all infrastructure devices. |
| 112 | * Returns port details of all infrastructure devices. |
| 113 | * |
| 114 | * @onos.rsModel DevicesGetPorts |
| 115 | * @return 200 OK with a collection of ports for all devices |
| 116 | */ |
| 117 | @GET |
| 118 | @Path("ports") |
| 119 | @Produces(MediaType.APPLICATION_JSON) |
| 120 | public Response getDevicesPorts() { |
| 121 | DeviceService service = get(DeviceService.class); |
| 122 | List<Port> result = Lists.newArrayList(); |
| 123 | service.getDevices().forEach(device -> { |
| 124 | Optional<List<Port>> list = Optional.ofNullable(service.getPorts(device.id())); |
| 125 | list.ifPresent(ports -> result.addAll(ports)); |
| 126 | }); |
| 127 | return ok(encodeArray(Port.class, "ports", result)).build(); |
| 128 | } |
| 129 | |
| 130 | /** |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 131 | * Gets ports of infrastructure device. |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 132 | * Returns details of the specified infrastructure device. |
| 133 | * |
Andrea Campanella | 10c4adc | 2015-12-03 15:27:54 -0800 | [diff] [blame] | 134 | * @onos.rsModel DeviceGetPorts |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 135 | * @param id device identifier |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 136 | * @return 200 OK with a collection of ports of the given device |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 137 | */ |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 138 | @GET |
| 139 | @Path("{id}/ports") |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 140 | @Produces(MediaType.APPLICATION_JSON) |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 141 | public Response getDevicePorts(@PathParam("id") String id) { |
| 142 | DeviceService service = get(DeviceService.class); |
| 143 | Device device = nullIsNotFound(service.getDevice(deviceId(id)), DEVICE_NOT_FOUND); |
| 144 | List<Port> ports = checkNotNull(service.getPorts(deviceId(id)), "Ports could not be retrieved"); |
| 145 | ObjectNode result = codec(Device.class).encode(device, this); |
| 146 | result.set("ports", codec(Port.class).encode(ports, this)); |
| 147 | return ok(result).build(); |
| 148 | } |
| 149 | |
Jonathan Hart | 471bcbb | 2018-04-20 13:01:10 -0700 | [diff] [blame] | 150 | /** |
| 151 | * Changes the administrative state of a port. |
| 152 | * |
| 153 | * @onos.rsModel PortAdministrativeState |
| 154 | * @param id device identifier |
| 155 | * @param portId port number |
| 156 | * @param stream input JSON |
| 157 | * @return 200 OK if the port state was set to the given value |
| 158 | */ |
| 159 | @POST |
| 160 | @Path("{id}/portstate/{port_id}") |
| 161 | @Consumes(MediaType.APPLICATION_JSON) |
| 162 | @Produces(MediaType.APPLICATION_JSON) |
| 163 | public Response setPortState(@PathParam("id") String id, |
| 164 | @PathParam("port_id") String portId, |
| 165 | InputStream stream) { |
| 166 | try { |
| 167 | DeviceId deviceId = deviceId(id); |
| 168 | PortNumber portNumber = PortNumber.portNumber(portId); |
| 169 | nullIsNotFound(get(DeviceService.class).getPort( |
| 170 | new ConnectPoint(deviceId, portNumber)), DEVICE_NOT_FOUND); |
| 171 | |
| 172 | ObjectNode root = readTreeFromStream(mapper(), stream); |
| 173 | JsonNode node = root.path(ENABLED); |
| 174 | |
| 175 | if (!node.isMissingNode()) { |
| 176 | get(DeviceAdminService.class) |
| 177 | .changePortState(deviceId, portNumber, node.asBoolean()); |
| 178 | return Response.ok().build(); |
| 179 | } |
| 180 | |
| 181 | throw new IllegalArgumentException(INVALID_JSON); |
| 182 | } catch (IOException ioe) { |
| 183 | throw new IllegalArgumentException(ioe); |
| 184 | } |
| 185 | } |
| 186 | |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 187 | } |