Ray Milkey | b9af946 | 2015-07-17 11:12:09 -0700 | [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 | */ |
| 16 | package org.onosproject.rest.resources; |
| 17 | |
| 18 | import java.util.Spliterator; |
| 19 | import java.util.Spliterators; |
| 20 | import java.util.stream.StreamSupport; |
| 21 | |
| 22 | import javax.ws.rs.GET; |
| 23 | import javax.ws.rs.Path; |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 24 | import javax.ws.rs.PathParam; |
Ray Milkey | b9af946 | 2015-07-17 11:12:09 -0700 | [diff] [blame] | 25 | import javax.ws.rs.Produces; |
| 26 | import javax.ws.rs.QueryParam; |
| 27 | import javax.ws.rs.core.Context; |
| 28 | import javax.ws.rs.core.MediaType; |
| 29 | import javax.ws.rs.core.Response; |
| 30 | import javax.ws.rs.core.UriBuilder; |
| 31 | import javax.ws.rs.core.UriInfo; |
| 32 | |
| 33 | import org.onosproject.codec.JsonCodec; |
| 34 | import org.onosproject.net.ConnectPoint; |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 35 | import org.onosproject.net.Device; |
| 36 | import org.onosproject.net.DeviceId; |
Ray Milkey | b9af946 | 2015-07-17 11:12:09 -0700 | [diff] [blame] | 37 | import org.onosproject.net.Link; |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 38 | import org.onosproject.net.device.DeviceService; |
| 39 | import org.onosproject.net.flow.FlowRuleService; |
| 40 | import org.onosproject.net.flow.TableStatisticsEntry; |
Ray Milkey | b9af946 | 2015-07-17 11:12:09 -0700 | [diff] [blame] | 41 | import org.onosproject.net.link.LinkService; |
| 42 | import org.onosproject.net.statistic.Load; |
| 43 | import org.onosproject.net.statistic.StatisticService; |
| 44 | import org.onosproject.rest.AbstractWebResource; |
| 45 | |
| 46 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 47 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 48 | |
| 49 | import static org.onosproject.net.DeviceId.deviceId; |
| 50 | import static org.onosproject.net.PortNumber.portNumber; |
| 51 | |
| 52 | /** |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 53 | * Query flow statistics. |
Ray Milkey | b9af946 | 2015-07-17 11:12:09 -0700 | [diff] [blame] | 54 | */ |
| 55 | @Path("statistics") |
| 56 | public class StatisticsWebResource extends AbstractWebResource { |
| 57 | @Context |
| 58 | UriInfo uriInfo; |
| 59 | |
| 60 | /** |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 61 | * Get load statistics for all links or for a specific link. |
andrea | 1ce2bc8 | 2015-11-18 16:58:10 -0800 | [diff] [blame] | 62 | * @rsModel StatisticsFlowsLink |
Ray Milkey | b9af946 | 2015-07-17 11:12:09 -0700 | [diff] [blame] | 63 | * @param deviceId (optional) device ID for a specific link |
| 64 | * @param port (optional) port number for a specified link |
| 65 | * @return JSON encoded array lof Load objects |
| 66 | */ |
| 67 | @GET |
| 68 | @Path("flows/link") |
| 69 | @Produces(MediaType.APPLICATION_JSON) |
| 70 | public Response getLoads(@QueryParam("device") String deviceId, |
| 71 | @QueryParam("port") String port) { |
| 72 | Iterable<Link> links; |
| 73 | |
| 74 | if (deviceId == null || port == null) { |
| 75 | links = get(LinkService.class).getLinks(); |
| 76 | } else { |
| 77 | ConnectPoint connectPoint = new ConnectPoint(deviceId(deviceId), |
| 78 | portNumber(port)); |
| 79 | links = get(LinkService.class).getLinks(connectPoint); |
| 80 | } |
| 81 | ObjectNode result = mapper().createObjectNode(); |
| 82 | ArrayNode loads = mapper().createArrayNode(); |
| 83 | JsonCodec<Load> loadCodec = codec(Load.class); |
| 84 | StatisticService statsService = getService(StatisticService.class); |
| 85 | |
Ray Milkey | b9af946 | 2015-07-17 11:12:09 -0700 | [diff] [blame] | 86 | StreamSupport.stream(Spliterators.spliteratorUnknownSize( |
| 87 | links.iterator(), Spliterator.ORDERED), false) |
| 88 | .forEach(link -> { |
| 89 | ObjectNode loadNode = loadCodec.encode(statsService.load(link), this); |
| 90 | |
| 91 | UriBuilder locationBuilder = uriInfo.getBaseUriBuilder() |
| 92 | .path("links") |
| 93 | .queryParam("device", link.src().deviceId().toString()) |
| 94 | .queryParam("port", link.src().port().toString()); |
| 95 | loadNode.put("link", locationBuilder.build().toString()); |
| 96 | loads.add(loadNode); |
| 97 | }); |
| 98 | result.set("loads", loads); |
| 99 | return ok(result).build(); |
| 100 | } |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 101 | |
| 102 | /** |
| 103 | * Get table statistics for all tables of all devices. |
andrea | 1ce2bc8 | 2015-11-18 16:58:10 -0800 | [diff] [blame] | 104 | * @rsModel StatisticsFlowsTables |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 105 | * @return JSON encoded array of table statistics |
| 106 | */ |
| 107 | @GET |
| 108 | @Path("flows/tables") |
| 109 | @Produces(MediaType.APPLICATION_JSON) |
| 110 | public Response getTableStatistics() { |
| 111 | final FlowRuleService service = get(FlowRuleService.class); |
| 112 | final Iterable<Device> devices = get(DeviceService.class).getDevices(); |
| 113 | final ObjectNode root = mapper().createObjectNode(); |
andrea | 1ce2bc8 | 2015-11-18 16:58:10 -0800 | [diff] [blame] | 114 | final ArrayNode rootArrayNode = root.putArray("statistics"); |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 115 | for (final Device device : devices) { |
| 116 | final ObjectNode deviceStatsNode = mapper().createObjectNode(); |
| 117 | deviceStatsNode.put("device", device.id().toString()); |
andrea | 1ce2bc8 | 2015-11-18 16:58:10 -0800 | [diff] [blame] | 118 | final ArrayNode statisticsNode = deviceStatsNode.putArray("table"); |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 119 | final Iterable<TableStatisticsEntry> tableStatsEntries = service.getFlowTableStatistics(device.id()); |
| 120 | if (tableStatsEntries != null) { |
| 121 | for (final TableStatisticsEntry entry : tableStatsEntries) { |
| 122 | statisticsNode.add(codec(TableStatisticsEntry.class).encode(entry, this)); |
| 123 | } |
| 124 | } |
| 125 | rootArrayNode.add(deviceStatsNode); |
| 126 | } |
| 127 | |
| 128 | return ok(root).build(); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Get table statistics for all tables of a specified device. |
andrea | 1ce2bc8 | 2015-11-18 16:58:10 -0800 | [diff] [blame] | 133 | * @rsModel StatisticsFlowsTables |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 134 | * @param deviceId device ID |
| 135 | * @return JSON encoded array of table statistics |
| 136 | */ |
| 137 | @GET |
| 138 | @Path("flows/tables/{deviceId}") |
| 139 | @Produces(MediaType.APPLICATION_JSON) |
| 140 | public Response getTableStatisticsByDeviceId(@PathParam("deviceId") String deviceId) { |
| 141 | final FlowRuleService service = get(FlowRuleService.class); |
| 142 | final Iterable<TableStatisticsEntry> tableStatisticsEntries = |
| 143 | service.getFlowTableStatistics(DeviceId.deviceId(deviceId)); |
| 144 | final ObjectNode root = mapper().createObjectNode(); |
andrea | 1ce2bc8 | 2015-11-18 16:58:10 -0800 | [diff] [blame] | 145 | final ArrayNode rootArrayNode = root.putArray("statistics"); |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 146 | |
| 147 | final ObjectNode deviceStatsNode = mapper().createObjectNode(); |
| 148 | deviceStatsNode.put("device", deviceId); |
andrea | 1ce2bc8 | 2015-11-18 16:58:10 -0800 | [diff] [blame] | 149 | final ArrayNode statisticsNode = deviceStatsNode.putArray("table"); |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 150 | for (final TableStatisticsEntry entry : tableStatisticsEntries) { |
| 151 | statisticsNode.add(codec(TableStatisticsEntry.class).encode(entry, this)); |
| 152 | } |
| 153 | rootArrayNode.add(deviceStatsNode); |
| 154 | return ok(root).build(); |
| 155 | } |
Ray Milkey | b9af946 | 2015-07-17 11:12:09 -0700 | [diff] [blame] | 156 | } |