blob: 76641da1ced71c2068721a807cc157c8b381aab3 [file] [log] [blame]
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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
18import com.fasterxml.jackson.databind.node.ObjectNode;
Brian O'Connorabafb502014-12-02 22:26:20 -080019import org.onosproject.net.Device;
20import org.onosproject.net.Port;
Thomas Vachuska82e95a52015-07-25 13:08:00 -070021import org.onosproject.net.device.DeviceAdminService;
Brian O'Connorabafb502014-12-02 22:26:20 -080022import org.onosproject.net.device.DeviceService;
Jonathan Hart9bb32ab2015-05-05 18:17:31 -070023import org.onosproject.rest.AbstractWebResource;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080024
Thomas Vachuska82e95a52015-07-25 13:08:00 -070025import javax.ws.rs.DELETE;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080026import javax.ws.rs.GET;
27import javax.ws.rs.Path;
28import javax.ws.rs.PathParam;
Jian Licc730a62016-05-10 16:36:16 -070029import javax.ws.rs.Produces;
30import javax.ws.rs.core.MediaType;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080031import javax.ws.rs.core.Response;
32import java.util.List;
33
34import static com.google.common.base.Preconditions.checkNotNull;
Thomas Vachuskaf8cac482015-04-08 19:40:12 -070035import static org.onlab.util.Tools.nullIsNotFound;
Brian O'Connorabafb502014-12-02 22:26:20 -080036import static org.onosproject.net.DeviceId.deviceId;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080037
38/**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070039 * Manage inventory of infrastructure devices.
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080040 */
41@Path("devices")
42public class DevicesWebResource extends AbstractWebResource {
43
Jian Licc730a62016-05-10 16:36:16 -070044 private static final String DEVICE_NOT_FOUND = "Device is not found";
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080045
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070046 /**
Jian Licc730a62016-05-10 16:36:16 -070047 * Gets all infrastructure devices.
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070048 * Returns array of all discovered infrastructure devices.
49 *
Jian Licc730a62016-05-10 16:36:16 -070050 * @return 200 OK with a collection of devices
Andrea Campanella10c4adc2015-12-03 15:27:54 -080051 * @onos.rsModel DevicesGet
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070052 */
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080053 @GET
Jian Licc730a62016-05-10 16:36:16 -070054 @Produces(MediaType.APPLICATION_JSON)
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080055 public Response getDevices() {
56 Iterable<Device> devices = get(DeviceService.class).getDevices();
57 return ok(encodeArray(Device.class, "devices", devices)).build();
58 }
59
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070060 /**
Jian Licc730a62016-05-10 16:36:16 -070061 * Gets details of infrastructure device.
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070062 * Returns details of the specified infrastructure device.
63 *
64 * @param id device identifier
Jian Licc730a62016-05-10 16:36:16 -070065 * @return 200 OK with a device
Andrea Campanella10c4adc2015-12-03 15:27:54 -080066 * @onos.rsModel DeviceGet
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070067 */
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080068 @GET
69 @Path("{id}")
Jian Licc730a62016-05-10 16:36:16 -070070 @Produces(MediaType.APPLICATION_JSON)
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080071 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 Vachuska0fa2aa12015-08-18 12:53:04 -070077 /**
Jian Licc730a62016-05-10 16:36:16 -070078 * Removes infrastructure device.
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070079 * Administratively deletes the specified device from the inventory of
80 * known devices.
81 *
82 * @param id device identifier
Jian Licc730a62016-05-10 16:36:16 -070083 * @return 200 OK with the removed device
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070084 */
Thomas Vachuska82e95a52015-07-25 13:08:00 -070085 @DELETE
86 @Path("{id}")
Jian Licc730a62016-05-10 16:36:16 -070087 @Produces(MediaType.APPLICATION_JSON)
Thomas Vachuska82e95a52015-07-25 13:08:00 -070088 public Response removeDevice(@PathParam("id") String id) {
89 Device device = nullIsNotFound(get(DeviceService.class).getDevice(deviceId(id)),
90 DEVICE_NOT_FOUND);
91 get(DeviceAdminService.class).removeDevice(deviceId(id));
92 return ok(codec(Device.class).encode(device, this)).build();
93 }
94
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070095 /**
Jian Licc730a62016-05-10 16:36:16 -070096 * Gets ports of infrastructure device.
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070097 * Returns details of the specified infrastructure device.
98 *
Andrea Campanella10c4adc2015-12-03 15:27:54 -080099 * @onos.rsModel DeviceGetPorts
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700100 * @param id device identifier
Jian Licc730a62016-05-10 16:36:16 -0700101 * @return 200 OK with a collection of ports of the given device
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700102 */
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -0800103 @GET
104 @Path("{id}/ports")
Jian Licc730a62016-05-10 16:36:16 -0700105 @Produces(MediaType.APPLICATION_JSON)
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -0800106 public Response getDevicePorts(@PathParam("id") String id) {
107 DeviceService service = get(DeviceService.class);
108 Device device = nullIsNotFound(service.getDevice(deviceId(id)), DEVICE_NOT_FOUND);
109 List<Port> ports = checkNotNull(service.getPorts(deviceId(id)), "Ports could not be retrieved");
110 ObjectNode result = codec(Device.class).encode(device, this);
111 result.set("ports", codec(Port.class).encode(ports, this));
112 return ok(result).build();
113 }
114
115}