Simon Hunt | 1f170b8 | 2015-01-16 14:30:20 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 3 | * |
| 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 | */ |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 16 | package org.onosproject.ui.impl; |
Simon Hunt | 1f170b8 | 2015-01-16 14:30:20 -0800 | [diff] [blame] | 17 | |
| 18 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 19 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 21 | import org.onlab.rest.BaseResource; |
Simon Hunt | 1f170b8 | 2015-01-16 14:30:20 -0800 | [diff] [blame] | 22 | import org.onosproject.net.Device; |
| 23 | import org.onosproject.net.device.DeviceService; |
Simon Hunt | 1f170b8 | 2015-01-16 14:30:20 -0800 | [diff] [blame] | 24 | |
Bri Prebilic Cole | 506ce0a | 2015-02-11 17:21:55 -0800 | [diff] [blame] | 25 | import javax.ws.rs.DefaultValue; |
Simon Hunt | 1f170b8 | 2015-01-16 14:30:20 -0800 | [diff] [blame] | 26 | import javax.ws.rs.GET; |
| 27 | import javax.ws.rs.Path; |
| 28 | import javax.ws.rs.Produces; |
Bri Prebilic Cole | 506ce0a | 2015-02-11 17:21:55 -0800 | [diff] [blame] | 29 | import javax.ws.rs.QueryParam; |
Simon Hunt | 1f170b8 | 2015-01-16 14:30:20 -0800 | [diff] [blame] | 30 | import javax.ws.rs.core.Response; |
Bri Prebilic Cole | 506ce0a | 2015-02-11 17:21:55 -0800 | [diff] [blame] | 31 | import java.util.ArrayList; |
Bri Prebilic Cole | 468bc1d | 2015-02-12 12:11:13 -0800 | [diff] [blame] | 32 | import java.util.Arrays; |
| 33 | import java.util.List; |
Simon Hunt | 1f170b8 | 2015-01-16 14:30:20 -0800 | [diff] [blame] | 34 | |
| 35 | /** |
| 36 | * UI REST resource for interacting with the inventory of infrastructure devices. |
| 37 | */ |
| 38 | @Path("device") |
| 39 | public class DeviceGuiResource extends BaseResource { |
| 40 | |
Bri Prebilic Cole | 468bc1d | 2015-02-12 12:11:13 -0800 | [diff] [blame] | 41 | private static final String DEVICES = "devices"; |
Simon Hunt | 1f170b8 | 2015-01-16 14:30:20 -0800 | [diff] [blame] | 42 | |
Bri Prebilic Cole | 468bc1d | 2015-02-12 12:11:13 -0800 | [diff] [blame] | 43 | private static final ObjectMapper MAPPER = new ObjectMapper(); |
Simon Hunt | 1f170b8 | 2015-01-16 14:30:20 -0800 | [diff] [blame] | 44 | |
| 45 | |
Bri Prebilic Cole | dea0974 | 2015-02-12 15:33:50 -0800 | [diff] [blame] | 46 | // return the list of devices in appropriate sorted order |
Simon Hunt | 1f170b8 | 2015-01-16 14:30:20 -0800 | [diff] [blame] | 47 | @GET |
| 48 | @Produces("application/json") |
Bri Prebilic Cole | 506ce0a | 2015-02-11 17:21:55 -0800 | [diff] [blame] | 49 | public Response getDevices( |
Bri Prebilic Cole | 468bc1d | 2015-02-12 12:11:13 -0800 | [diff] [blame] | 50 | @DefaultValue("id") @QueryParam("sortCol") String colId, |
| 51 | @DefaultValue("asc") @QueryParam("sortDir") String dir |
Bri Prebilic Cole | 506ce0a | 2015-02-11 17:21:55 -0800 | [diff] [blame] | 52 | ) { |
Simon Hunt | 1f170b8 | 2015-01-16 14:30:20 -0800 | [diff] [blame] | 53 | DeviceService service = get(DeviceService.class); |
Bri Prebilic Cole | 468bc1d | 2015-02-12 12:11:13 -0800 | [diff] [blame] | 54 | TableRow[] rows = generateTableRows(service); |
| 55 | RowComparator rc = new RowComparator(colId, RowComparator.direction(dir)); |
| 56 | Arrays.sort(rows, rc); |
| 57 | ArrayNode devices = generateArrayNode(rows); |
| 58 | ObjectNode rootNode = MAPPER.createObjectNode(); |
| 59 | rootNode.set(DEVICES, devices); |
Simon Hunt | 1f170b8 | 2015-01-16 14:30:20 -0800 | [diff] [blame] | 60 | |
Simon Hunt | 1f170b8 | 2015-01-16 14:30:20 -0800 | [diff] [blame] | 61 | return Response.ok(rootNode.toString()).build(); |
| 62 | } |
| 63 | |
Bri Prebilic Cole | 468bc1d | 2015-02-12 12:11:13 -0800 | [diff] [blame] | 64 | private ArrayNode generateArrayNode(TableRow[] rows) { |
| 65 | ArrayNode devices = MAPPER.createArrayNode(); |
| 66 | for (TableRow r : rows) { |
| 67 | devices.add(r.toJsonNode()); |
Simon Hunt | 1f170b8 | 2015-01-16 14:30:20 -0800 | [diff] [blame] | 68 | } |
Bri Prebilic Cole | 468bc1d | 2015-02-12 12:11:13 -0800 | [diff] [blame] | 69 | return devices; |
Simon Hunt | 1f170b8 | 2015-01-16 14:30:20 -0800 | [diff] [blame] | 70 | } |
| 71 | |
Bri Prebilic Cole | 468bc1d | 2015-02-12 12:11:13 -0800 | [diff] [blame] | 72 | private TableRow[] generateTableRows(DeviceService service) { |
| 73 | List<TableRow> list = new ArrayList<>(); |
| 74 | for (Device dev : service.getDevices()) { |
| 75 | list.add(new DeviceTableRow(service, dev)); |
| 76 | } |
| 77 | return list.toArray(new TableRow[list.size()]); |
| 78 | } |
Simon Hunt | 1f170b8 | 2015-01-16 14:30:20 -0800 | [diff] [blame] | 79 | } |