blob: be1e5b63c4dab543f7602b66425f5dab30a1e70d [file] [log] [blame]
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -08001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 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;
29import javax.ws.rs.core.Response;
30import java.util.List;
31
32import static com.google.common.base.Preconditions.checkNotNull;
Thomas Vachuskaf8cac482015-04-08 19:40:12 -070033import static org.onlab.util.Tools.nullIsNotFound;
Brian O'Connorabafb502014-12-02 22:26:20 -080034import static org.onosproject.net.DeviceId.deviceId;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080035
36/**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070037 * Manage inventory of infrastructure devices.
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080038 */
39@Path("devices")
40public class DevicesWebResource extends AbstractWebResource {
41
42 public static final String DEVICE_NOT_FOUND = "Device is not found";
43
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070044 /**
45 * Get all infrastructure devices.
46 * Returns array of all discovered infrastructure devices.
47 *
48 * @return 200 OK
Andrea Campanella10c4adc2015-12-03 15:27:54 -080049 * @onos.rsModel DevicesGet
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070050 */
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080051 @GET
52 public Response getDevices() {
53 Iterable<Device> devices = get(DeviceService.class).getDevices();
54 return ok(encodeArray(Device.class, "devices", devices)).build();
55 }
56
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070057 /**
58 * Get details of infrastructure device.
59 * Returns details of the specified infrastructure device.
60 *
61 * @param id device identifier
62 * @return 200 OK
Andrea Campanella10c4adc2015-12-03 15:27:54 -080063 * @onos.rsModel DeviceGet
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070064 */
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080065 @GET
66 @Path("{id}")
67 public Response getDevice(@PathParam("id") String id) {
68 Device device = nullIsNotFound(get(DeviceService.class).getDevice(deviceId(id)),
69 DEVICE_NOT_FOUND);
70 return ok(codec(Device.class).encode(device, this)).build();
71 }
72
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070073 /**
74 * Remove infrastructure device.
75 * Administratively deletes the specified device from the inventory of
76 * known devices.
77 *
78 * @param id device identifier
79 * @return 200 OK
80 */
Thomas Vachuska82e95a52015-07-25 13:08:00 -070081 @DELETE
82 @Path("{id}")
83 public Response removeDevice(@PathParam("id") String id) {
84 Device device = nullIsNotFound(get(DeviceService.class).getDevice(deviceId(id)),
85 DEVICE_NOT_FOUND);
86 get(DeviceAdminService.class).removeDevice(deviceId(id));
87 return ok(codec(Device.class).encode(device, this)).build();
88 }
89
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070090 /**
91 * Get ports of infrastructure device.
92 * Returns details of the specified infrastructure device.
93 *
Andrea Campanella10c4adc2015-12-03 15:27:54 -080094 * @onos.rsModel DeviceGetPorts
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070095 * @param id device identifier
96 * @return 200 OK
97 */
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080098 @GET
99 @Path("{id}/ports")
100 public Response getDevicePorts(@PathParam("id") String id) {
101 DeviceService service = get(DeviceService.class);
102 Device device = nullIsNotFound(service.getDevice(deviceId(id)), DEVICE_NOT_FOUND);
103 List<Port> ports = checkNotNull(service.getPorts(deviceId(id)), "Ports could not be retrieved");
104 ObjectNode result = codec(Device.class).encode(device, this);
105 result.set("ports", codec(Port.class).encode(ports, this));
106 return ok(result).build();
107 }
108
109}