blob: 05756e5a56e96a25931cc623d5d418e8109fed23 [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
49 */
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080050 @GET
51 public Response getDevices() {
52 Iterable<Device> devices = get(DeviceService.class).getDevices();
53 return ok(encodeArray(Device.class, "devices", devices)).build();
54 }
55
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070056 /**
57 * Get details of infrastructure device.
58 * Returns details of the specified infrastructure device.
59 *
60 * @param id device identifier
61 * @return 200 OK
62 */
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080063 @GET
64 @Path("{id}")
65 public Response getDevice(@PathParam("id") String id) {
66 Device device = nullIsNotFound(get(DeviceService.class).getDevice(deviceId(id)),
67 DEVICE_NOT_FOUND);
68 return ok(codec(Device.class).encode(device, this)).build();
69 }
70
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070071 /**
72 * Remove infrastructure device.
73 * Administratively deletes the specified device from the inventory of
74 * known devices.
75 *
76 * @param id device identifier
77 * @return 200 OK
78 */
Thomas Vachuska82e95a52015-07-25 13:08:00 -070079 @DELETE
80 @Path("{id}")
81 public Response removeDevice(@PathParam("id") String id) {
82 Device device = nullIsNotFound(get(DeviceService.class).getDevice(deviceId(id)),
83 DEVICE_NOT_FOUND);
84 get(DeviceAdminService.class).removeDevice(deviceId(id));
85 return ok(codec(Device.class).encode(device, this)).build();
86 }
87
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070088 /**
89 * Get ports of infrastructure device.
90 * Returns details of the specified infrastructure device.
91 *
92 * @param id device identifier
93 * @return 200 OK
94 */
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080095 @GET
96 @Path("{id}/ports")
97 public Response getDevicePorts(@PathParam("id") String id) {
98 DeviceService service = get(DeviceService.class);
99 Device device = nullIsNotFound(service.getDevice(deviceId(id)), DEVICE_NOT_FOUND);
100 List<Port> ports = checkNotNull(service.getPorts(deviceId(id)), "Ports could not be retrieved");
101 ObjectNode result = codec(Device.class).encode(device, this);
102 result.set("ports", codec(Port.class).encode(ports, this));
103 return ok(result).build();
104 }
105
106}