blob: 403b8c62afa938ad914cb56c28e040a863a53611 [file] [log] [blame]
Simon Hunt1f170b82015-01-16 14:30:20 -08001/*
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 */
16package org.onosproject.gui;
17
18import com.fasterxml.jackson.databind.ObjectMapper;
19import com.fasterxml.jackson.databind.node.ArrayNode;
20import com.fasterxml.jackson.databind.node.ObjectNode;
21import org.onlab.rest.BaseResource;
Simon Hunt1f170b82015-01-16 14:30:20 -080022import org.onosproject.net.Device;
23import org.onosproject.net.device.DeviceService;
Simon Hunt1f170b82015-01-16 14:30:20 -080024
Bri Prebilic Cole506ce0a2015-02-11 17:21:55 -080025import javax.ws.rs.DefaultValue;
Simon Hunt1f170b82015-01-16 14:30:20 -080026import javax.ws.rs.GET;
27import javax.ws.rs.Path;
28import javax.ws.rs.Produces;
Bri Prebilic Cole506ce0a2015-02-11 17:21:55 -080029import javax.ws.rs.QueryParam;
Simon Hunt1f170b82015-01-16 14:30:20 -080030import javax.ws.rs.core.Response;
Bri Prebilic Cole506ce0a2015-02-11 17:21:55 -080031import java.util.ArrayList;
Bri Prebilic Cole468bc1d2015-02-12 12:11:13 -080032import java.util.Arrays;
33import java.util.List;
Simon Hunt1f170b82015-01-16 14:30:20 -080034
35/**
36 * UI REST resource for interacting with the inventory of infrastructure devices.
37 */
38@Path("device")
39public class DeviceGuiResource extends BaseResource {
40
Bri Prebilic Cole468bc1d2015-02-12 12:11:13 -080041 private static final String DEVICES = "devices";
Simon Hunt1f170b82015-01-16 14:30:20 -080042
Bri Prebilic Cole468bc1d2015-02-12 12:11:13 -080043 private static final ObjectMapper MAPPER = new ObjectMapper();
Simon Hunt1f170b82015-01-16 14:30:20 -080044
45
Bri Prebilic Coledea09742015-02-12 15:33:50 -080046 // return the list of devices in appropriate sorted order
Simon Hunt1f170b82015-01-16 14:30:20 -080047 @GET
48 @Produces("application/json")
Bri Prebilic Cole506ce0a2015-02-11 17:21:55 -080049 public Response getDevices(
Bri Prebilic Cole468bc1d2015-02-12 12:11:13 -080050 @DefaultValue("id") @QueryParam("sortCol") String colId,
51 @DefaultValue("asc") @QueryParam("sortDir") String dir
Bri Prebilic Cole506ce0a2015-02-11 17:21:55 -080052 ) {
Simon Hunt1f170b82015-01-16 14:30:20 -080053 DeviceService service = get(DeviceService.class);
Bri Prebilic Cole468bc1d2015-02-12 12:11:13 -080054 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 Hunt1f170b82015-01-16 14:30:20 -080060
Simon Hunt1f170b82015-01-16 14:30:20 -080061 return Response.ok(rootNode.toString()).build();
62 }
63
Bri Prebilic Cole468bc1d2015-02-12 12:11:13 -080064 private ArrayNode generateArrayNode(TableRow[] rows) {
65 ArrayNode devices = MAPPER.createArrayNode();
66 for (TableRow r : rows) {
67 devices.add(r.toJsonNode());
Simon Hunt1f170b82015-01-16 14:30:20 -080068 }
Bri Prebilic Cole468bc1d2015-02-12 12:11:13 -080069 return devices;
Simon Hunt1f170b82015-01-16 14:30:20 -080070 }
71
Bri Prebilic Cole468bc1d2015-02-12 12:11:13 -080072 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 Hunt1f170b82015-01-16 14:30:20 -080079}