blob: c544a1113434719b7c5cdeea13de5e8ba101e881 [file] [log] [blame]
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -08003 *
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 Hart9bb32ab2015-05-05 18:17:31 -070016package org.onosproject.rest.resources;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080017
Jonathan Hart471bcbb2018-04-20 13:01:10 -070018import com.fasterxml.jackson.databind.JsonNode;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080019import com.fasterxml.jackson.databind.node.ObjectNode;
DongRyeol Chab1177cc2019-11-14 13:53:13 +090020import com.google.common.collect.Lists;
Jonathan Hart471bcbb2018-04-20 13:01:10 -070021import org.onosproject.net.ConnectPoint;
Brian O'Connorabafb502014-12-02 22:26:20 -080022import org.onosproject.net.Device;
Jonathan Hart471bcbb2018-04-20 13:01:10 -070023import org.onosproject.net.DeviceId;
Brian O'Connorabafb502014-12-02 22:26:20 -080024import org.onosproject.net.Port;
Jonathan Hart471bcbb2018-04-20 13:01:10 -070025import org.onosproject.net.PortNumber;
Thomas Vachuska82e95a52015-07-25 13:08:00 -070026import org.onosproject.net.device.DeviceAdminService;
Brian O'Connorabafb502014-12-02 22:26:20 -080027import org.onosproject.net.device.DeviceService;
Jonathan Hart9bb32ab2015-05-05 18:17:31 -070028import org.onosproject.rest.AbstractWebResource;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080029
Jonathan Hart471bcbb2018-04-20 13:01:10 -070030import javax.ws.rs.Consumes;
Thomas Vachuska82e95a52015-07-25 13:08:00 -070031import javax.ws.rs.DELETE;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080032import javax.ws.rs.GET;
Jonathan Hart471bcbb2018-04-20 13:01:10 -070033import javax.ws.rs.POST;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080034import javax.ws.rs.Path;
35import javax.ws.rs.PathParam;
Jian Licc730a62016-05-10 16:36:16 -070036import javax.ws.rs.Produces;
37import javax.ws.rs.core.MediaType;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080038import javax.ws.rs.core.Response;
Jonathan Hart471bcbb2018-04-20 13:01:10 -070039import java.io.IOException;
40import java.io.InputStream;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080041import java.util.List;
DongRyeol Chab1177cc2019-11-14 13:53:13 +090042import java.util.Optional;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080043
44import static com.google.common.base.Preconditions.checkNotNull;
Thomas Vachuskaf8cac482015-04-08 19:40:12 -070045import static org.onlab.util.Tools.nullIsNotFound;
Jonathan Hart471bcbb2018-04-20 13:01:10 -070046import static org.onlab.util.Tools.readTreeFromStream;
Brian O'Connorabafb502014-12-02 22:26:20 -080047import static org.onosproject.net.DeviceId.deviceId;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080048
49/**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070050 * Manage inventory of infrastructure devices.
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080051 */
52@Path("devices")
53public class DevicesWebResource extends AbstractWebResource {
54
Jonathan Hart471bcbb2018-04-20 13:01:10 -070055 private static final String ENABLED = "enabled";
56
Jian Licc730a62016-05-10 16:36:16 -070057 private static final String DEVICE_NOT_FOUND = "Device is not found";
Jonathan Hart471bcbb2018-04-20 13:01:10 -070058 private static final String INVALID_JSON = "Invalid JSON data";
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080059
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070060 /**
Jian Licc730a62016-05-10 16:36:16 -070061 * Gets all infrastructure devices.
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070062 * Returns array of all discovered infrastructure devices.
63 *
Jian Licc730a62016-05-10 16:36:16 -070064 * @return 200 OK with a collection of devices
Andrea Campanella10c4adc2015-12-03 15:27:54 -080065 * @onos.rsModel DevicesGet
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070066 */
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080067 @GET
Jian Licc730a62016-05-10 16:36:16 -070068 @Produces(MediaType.APPLICATION_JSON)
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080069 public Response getDevices() {
70 Iterable<Device> devices = get(DeviceService.class).getDevices();
71 return ok(encodeArray(Device.class, "devices", devices)).build();
72 }
73
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070074 /**
Jian Licc730a62016-05-10 16:36:16 -070075 * Gets details of infrastructure device.
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070076 * Returns details of the specified infrastructure device.
77 *
78 * @param id device identifier
Jian Licc730a62016-05-10 16:36:16 -070079 * @return 200 OK with a device
Andrea Campanella10c4adc2015-12-03 15:27:54 -080080 * @onos.rsModel DeviceGet
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070081 */
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080082 @GET
83 @Path("{id}")
Jian Licc730a62016-05-10 16:36:16 -070084 @Produces(MediaType.APPLICATION_JSON)
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080085 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 Vachuska0fa2aa12015-08-18 12:53:04 -070091 /**
Jian Licc730a62016-05-10 16:36:16 -070092 * Removes infrastructure device.
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070093 * Administratively deletes the specified device from the inventory of
94 * known devices.
95 *
96 * @param id device identifier
Jian Licc730a62016-05-10 16:36:16 -070097 * @return 200 OK with the removed device
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070098 */
Thomas Vachuska82e95a52015-07-25 13:08:00 -070099 @DELETE
100 @Path("{id}")
Jian Licc730a62016-05-10 16:36:16 -0700101 @Produces(MediaType.APPLICATION_JSON)
Thomas Vachuska82e95a52015-07-25 13:08:00 -0700102 public Response removeDevice(@PathParam("id") String id) {
103 Device device = nullIsNotFound(get(DeviceService.class).getDevice(deviceId(id)),
104 DEVICE_NOT_FOUND);
kalagesa42019542017-03-14 18:00:47 +0530105 ObjectNode result = codec(Device.class).encode(device, this);
Thomas Vachuska82e95a52015-07-25 13:08:00 -0700106 get(DeviceAdminService.class).removeDevice(deviceId(id));
kalagesa42019542017-03-14 18:00:47 +0530107 return ok(result).build();
Thomas Vachuska82e95a52015-07-25 13:08:00 -0700108 }
109
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700110 /**
DongRyeol Chab1177cc2019-11-14 13:53:13 +0900111 * 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 Licc730a62016-05-10 16:36:16 -0700131 * Gets ports of infrastructure device.
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700132 * Returns details of the specified infrastructure device.
133 *
Andrea Campanella10c4adc2015-12-03 15:27:54 -0800134 * @onos.rsModel DeviceGetPorts
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700135 * @param id device identifier
Jian Licc730a62016-05-10 16:36:16 -0700136 * @return 200 OK with a collection of ports of the given device
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700137 */
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -0800138 @GET
139 @Path("{id}/ports")
Jian Licc730a62016-05-10 16:36:16 -0700140 @Produces(MediaType.APPLICATION_JSON)
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -0800141 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 Hart471bcbb2018-04-20 13:01:10 -0700150 /**
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 Vachuskaca60f2b2014-11-06 01:34:28 -0800187}