blob: 05390344f3887c59acf7ce590b11804ece648447 [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;
Jonathan Hart471bcbb2018-04-20 13:01:10 -070020import org.onosproject.net.ConnectPoint;
Brian O'Connorabafb502014-12-02 22:26:20 -080021import org.onosproject.net.Device;
Jonathan Hart471bcbb2018-04-20 13:01:10 -070022import org.onosproject.net.DeviceId;
Brian O'Connorabafb502014-12-02 22:26:20 -080023import org.onosproject.net.Port;
Jonathan Hart471bcbb2018-04-20 13:01:10 -070024import org.onosproject.net.PortNumber;
Thomas Vachuska82e95a52015-07-25 13:08:00 -070025import org.onosproject.net.device.DeviceAdminService;
Brian O'Connorabafb502014-12-02 22:26:20 -080026import org.onosproject.net.device.DeviceService;
Jonathan Hart9bb32ab2015-05-05 18:17:31 -070027import org.onosproject.rest.AbstractWebResource;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080028
Jonathan Hart471bcbb2018-04-20 13:01:10 -070029import javax.ws.rs.Consumes;
Thomas Vachuska82e95a52015-07-25 13:08:00 -070030import javax.ws.rs.DELETE;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080031import javax.ws.rs.GET;
Jonathan Hart471bcbb2018-04-20 13:01:10 -070032import javax.ws.rs.POST;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080033import javax.ws.rs.Path;
34import javax.ws.rs.PathParam;
Jian Licc730a62016-05-10 16:36:16 -070035import javax.ws.rs.Produces;
36import javax.ws.rs.core.MediaType;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080037import javax.ws.rs.core.Response;
Jonathan Hart471bcbb2018-04-20 13:01:10 -070038import java.io.IOException;
39import java.io.InputStream;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080040import java.util.List;
41
42import static com.google.common.base.Preconditions.checkNotNull;
Thomas Vachuskaf8cac482015-04-08 19:40:12 -070043import static org.onlab.util.Tools.nullIsNotFound;
Jonathan Hart471bcbb2018-04-20 13:01:10 -070044import static org.onlab.util.Tools.readTreeFromStream;
Brian O'Connorabafb502014-12-02 22:26:20 -080045import static org.onosproject.net.DeviceId.deviceId;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080046
47/**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070048 * Manage inventory of infrastructure devices.
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080049 */
50@Path("devices")
51public class DevicesWebResource extends AbstractWebResource {
52
Jonathan Hart471bcbb2018-04-20 13:01:10 -070053 private static final String ENABLED = "enabled";
54
Jian Licc730a62016-05-10 16:36:16 -070055 private static final String DEVICE_NOT_FOUND = "Device is not found";
Jonathan Hart471bcbb2018-04-20 13:01:10 -070056 private static final String INVALID_JSON = "Invalid JSON data";
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080057
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070058 /**
Jian Licc730a62016-05-10 16:36:16 -070059 * Gets all infrastructure devices.
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070060 * Returns array of all discovered infrastructure devices.
61 *
Jian Licc730a62016-05-10 16:36:16 -070062 * @return 200 OK with a collection of devices
Andrea Campanella10c4adc2015-12-03 15:27:54 -080063 * @onos.rsModel DevicesGet
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070064 */
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080065 @GET
Jian Licc730a62016-05-10 16:36:16 -070066 @Produces(MediaType.APPLICATION_JSON)
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080067 public Response getDevices() {
68 Iterable<Device> devices = get(DeviceService.class).getDevices();
69 return ok(encodeArray(Device.class, "devices", devices)).build();
70 }
71
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070072 /**
Jian Licc730a62016-05-10 16:36:16 -070073 * Gets details of infrastructure device.
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070074 * Returns details of the specified infrastructure device.
75 *
76 * @param id device identifier
Jian Licc730a62016-05-10 16:36:16 -070077 * @return 200 OK with a device
Andrea Campanella10c4adc2015-12-03 15:27:54 -080078 * @onos.rsModel DeviceGet
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070079 */
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080080 @GET
81 @Path("{id}")
Jian Licc730a62016-05-10 16:36:16 -070082 @Produces(MediaType.APPLICATION_JSON)
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080083 public Response getDevice(@PathParam("id") String id) {
84 Device device = nullIsNotFound(get(DeviceService.class).getDevice(deviceId(id)),
85 DEVICE_NOT_FOUND);
86 return ok(codec(Device.class).encode(device, this)).build();
87 }
88
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070089 /**
Jian Licc730a62016-05-10 16:36:16 -070090 * Removes infrastructure device.
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070091 * Administratively deletes the specified device from the inventory of
92 * known devices.
93 *
94 * @param id device identifier
Jian Licc730a62016-05-10 16:36:16 -070095 * @return 200 OK with the removed device
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070096 */
Thomas Vachuska82e95a52015-07-25 13:08:00 -070097 @DELETE
98 @Path("{id}")
Jian Licc730a62016-05-10 16:36:16 -070099 @Produces(MediaType.APPLICATION_JSON)
Thomas Vachuska82e95a52015-07-25 13:08:00 -0700100 public Response removeDevice(@PathParam("id") String id) {
101 Device device = nullIsNotFound(get(DeviceService.class).getDevice(deviceId(id)),
102 DEVICE_NOT_FOUND);
kalagesa42019542017-03-14 18:00:47 +0530103 ObjectNode result = codec(Device.class).encode(device, this);
Thomas Vachuska82e95a52015-07-25 13:08:00 -0700104 get(DeviceAdminService.class).removeDevice(deviceId(id));
kalagesa42019542017-03-14 18:00:47 +0530105 return ok(result).build();
Thomas Vachuska82e95a52015-07-25 13:08:00 -0700106 }
107
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700108 /**
Jian Licc730a62016-05-10 16:36:16 -0700109 * Gets ports of infrastructure device.
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700110 * Returns details of the specified infrastructure device.
111 *
Andrea Campanella10c4adc2015-12-03 15:27:54 -0800112 * @onos.rsModel DeviceGetPorts
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700113 * @param id device identifier
Jian Licc730a62016-05-10 16:36:16 -0700114 * @return 200 OK with a collection of ports of the given device
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700115 */
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -0800116 @GET
117 @Path("{id}/ports")
Jian Licc730a62016-05-10 16:36:16 -0700118 @Produces(MediaType.APPLICATION_JSON)
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -0800119 public Response getDevicePorts(@PathParam("id") String id) {
120 DeviceService service = get(DeviceService.class);
121 Device device = nullIsNotFound(service.getDevice(deviceId(id)), DEVICE_NOT_FOUND);
122 List<Port> ports = checkNotNull(service.getPorts(deviceId(id)), "Ports could not be retrieved");
123 ObjectNode result = codec(Device.class).encode(device, this);
124 result.set("ports", codec(Port.class).encode(ports, this));
125 return ok(result).build();
126 }
127
Jonathan Hart471bcbb2018-04-20 13:01:10 -0700128 /**
129 * Changes the administrative state of a port.
130 *
131 * @onos.rsModel PortAdministrativeState
132 * @param id device identifier
133 * @param portId port number
134 * @param stream input JSON
135 * @return 200 OK if the port state was set to the given value
136 */
137 @POST
138 @Path("{id}/portstate/{port_id}")
139 @Consumes(MediaType.APPLICATION_JSON)
140 @Produces(MediaType.APPLICATION_JSON)
141 public Response setPortState(@PathParam("id") String id,
142 @PathParam("port_id") String portId,
143 InputStream stream) {
144 try {
145 DeviceId deviceId = deviceId(id);
146 PortNumber portNumber = PortNumber.portNumber(portId);
147 nullIsNotFound(get(DeviceService.class).getPort(
148 new ConnectPoint(deviceId, portNumber)), DEVICE_NOT_FOUND);
149
150 ObjectNode root = readTreeFromStream(mapper(), stream);
151 JsonNode node = root.path(ENABLED);
152
153 if (!node.isMissingNode()) {
154 get(DeviceAdminService.class)
155 .changePortState(deviceId, portNumber, node.asBoolean());
156 return Response.ok().build();
157 }
158
159 throw new IllegalArgumentException(INVALID_JSON);
160 } catch (IOException ioe) {
161 throw new IllegalArgumentException(ioe);
162 }
163 }
164
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -0800165}