Ray Milkey | b9af946 | 2015-07-17 11:12:09 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Ray Milkey | b9af946 | 2015-07-17 11:12:09 -0700 | [diff] [blame] | 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; |
Srikanth Vavilapalli | d120f5c | 2015-11-24 14:15:01 -0800 | [diff] [blame] | 39 | import org.onosproject.net.device.PortStatistics; |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 40 | import org.onosproject.net.flow.FlowRuleService; |
| 41 | import org.onosproject.net.flow.TableStatisticsEntry; |
Ray Milkey | b9af946 | 2015-07-17 11:12:09 -0700 | [diff] [blame] | 42 | import org.onosproject.net.link.LinkService; |
| 43 | import org.onosproject.net.statistic.Load; |
| 44 | import org.onosproject.net.statistic.StatisticService; |
| 45 | import org.onosproject.rest.AbstractWebResource; |
| 46 | |
| 47 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 48 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 49 | |
| 50 | import static org.onosproject.net.DeviceId.deviceId; |
| 51 | import static org.onosproject.net.PortNumber.portNumber; |
| 52 | |
| 53 | /** |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 54 | * Query flow statistics. |
Ray Milkey | b9af946 | 2015-07-17 11:12:09 -0700 | [diff] [blame] | 55 | */ |
| 56 | @Path("statistics") |
| 57 | public class StatisticsWebResource extends AbstractWebResource { |
| 58 | @Context |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 59 | private UriInfo uriInfo; |
Ray Milkey | b9af946 | 2015-07-17 11:12:09 -0700 | [diff] [blame] | 60 | |
| 61 | /** |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 62 | * Gets load statistics for all links or for a specific link. |
| 63 | * |
Andrea Campanella | 10c4adc | 2015-12-03 15:27:54 -0800 | [diff] [blame] | 64 | * @onos.rsModel StatisticsFlowsLink |
Ray Milkey | b9af946 | 2015-07-17 11:12:09 -0700 | [diff] [blame] | 65 | * @param deviceId (optional) device ID for a specific link |
| 66 | * @param port (optional) port number for a specified link |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 67 | * @return 200 OK with JSON encoded array of Load objects |
Ray Milkey | b9af946 | 2015-07-17 11:12:09 -0700 | [diff] [blame] | 68 | */ |
| 69 | @GET |
| 70 | @Path("flows/link") |
| 71 | @Produces(MediaType.APPLICATION_JSON) |
| 72 | public Response getLoads(@QueryParam("device") String deviceId, |
| 73 | @QueryParam("port") String port) { |
| 74 | Iterable<Link> links; |
| 75 | |
| 76 | if (deviceId == null || port == null) { |
| 77 | links = get(LinkService.class).getLinks(); |
| 78 | } else { |
| 79 | ConnectPoint connectPoint = new ConnectPoint(deviceId(deviceId), |
| 80 | portNumber(port)); |
| 81 | links = get(LinkService.class).getLinks(connectPoint); |
| 82 | } |
| 83 | ObjectNode result = mapper().createObjectNode(); |
| 84 | ArrayNode loads = mapper().createArrayNode(); |
| 85 | JsonCodec<Load> loadCodec = codec(Load.class); |
| 86 | StatisticService statsService = getService(StatisticService.class); |
| 87 | |
Ray Milkey | b9af946 | 2015-07-17 11:12:09 -0700 | [diff] [blame] | 88 | StreamSupport.stream(Spliterators.spliteratorUnknownSize( |
| 89 | links.iterator(), Spliterator.ORDERED), false) |
| 90 | .forEach(link -> { |
| 91 | ObjectNode loadNode = loadCodec.encode(statsService.load(link), this); |
| 92 | |
| 93 | UriBuilder locationBuilder = uriInfo.getBaseUriBuilder() |
| 94 | .path("links") |
| 95 | .queryParam("device", link.src().deviceId().toString()) |
| 96 | .queryParam("port", link.src().port().toString()); |
| 97 | loadNode.put("link", locationBuilder.build().toString()); |
| 98 | loads.add(loadNode); |
| 99 | }); |
| 100 | result.set("loads", loads); |
| 101 | return ok(result).build(); |
| 102 | } |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 103 | |
| 104 | /** |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 105 | * Gets table statistics for all tables of all devices. |
| 106 | * |
Andrea Campanella | 10c4adc | 2015-12-03 15:27:54 -0800 | [diff] [blame] | 107 | * @onos.rsModel StatisticsFlowsTables |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 108 | * @return 200 OK with JSON encoded array of table statistics |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 109 | */ |
| 110 | @GET |
| 111 | @Path("flows/tables") |
| 112 | @Produces(MediaType.APPLICATION_JSON) |
| 113 | public Response getTableStatistics() { |
| 114 | final FlowRuleService service = get(FlowRuleService.class); |
| 115 | final Iterable<Device> devices = get(DeviceService.class).getDevices(); |
| 116 | final ObjectNode root = mapper().createObjectNode(); |
andrea | 1ce2bc8 | 2015-11-18 16:58:10 -0800 | [diff] [blame] | 117 | final ArrayNode rootArrayNode = root.putArray("statistics"); |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 118 | for (final Device device : devices) { |
| 119 | final ObjectNode deviceStatsNode = mapper().createObjectNode(); |
| 120 | deviceStatsNode.put("device", device.id().toString()); |
andrea | 1ce2bc8 | 2015-11-18 16:58:10 -0800 | [diff] [blame] | 121 | final ArrayNode statisticsNode = deviceStatsNode.putArray("table"); |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 122 | final Iterable<TableStatisticsEntry> tableStatsEntries = service.getFlowTableStatistics(device.id()); |
| 123 | if (tableStatsEntries != null) { |
| 124 | for (final TableStatisticsEntry entry : tableStatsEntries) { |
| 125 | statisticsNode.add(codec(TableStatisticsEntry.class).encode(entry, this)); |
| 126 | } |
| 127 | } |
| 128 | rootArrayNode.add(deviceStatsNode); |
| 129 | } |
| 130 | |
| 131 | return ok(root).build(); |
| 132 | } |
| 133 | |
| 134 | /** |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 135 | * Gets table statistics for all tables of a specified device. |
| 136 | * |
Andrea Campanella | 10c4adc | 2015-12-03 15:27:54 -0800 | [diff] [blame] | 137 | * @onos.rsModel StatisticsFlowsTables |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 138 | * @param deviceId device ID |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 139 | * @return 200 OK with JSON encoded array of table statistics |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 140 | */ |
| 141 | @GET |
| 142 | @Path("flows/tables/{deviceId}") |
| 143 | @Produces(MediaType.APPLICATION_JSON) |
| 144 | public Response getTableStatisticsByDeviceId(@PathParam("deviceId") String deviceId) { |
| 145 | final FlowRuleService service = get(FlowRuleService.class); |
| 146 | final Iterable<TableStatisticsEntry> tableStatisticsEntries = |
| 147 | service.getFlowTableStatistics(DeviceId.deviceId(deviceId)); |
| 148 | final ObjectNode root = mapper().createObjectNode(); |
andrea | 1ce2bc8 | 2015-11-18 16:58:10 -0800 | [diff] [blame] | 149 | final ArrayNode rootArrayNode = root.putArray("statistics"); |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 150 | |
| 151 | final ObjectNode deviceStatsNode = mapper().createObjectNode(); |
| 152 | deviceStatsNode.put("device", deviceId); |
andrea | 1ce2bc8 | 2015-11-18 16:58:10 -0800 | [diff] [blame] | 153 | final ArrayNode statisticsNode = deviceStatsNode.putArray("table"); |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 154 | for (final TableStatisticsEntry entry : tableStatisticsEntries) { |
| 155 | statisticsNode.add(codec(TableStatisticsEntry.class).encode(entry, this)); |
| 156 | } |
| 157 | rootArrayNode.add(deviceStatsNode); |
| 158 | return ok(root).build(); |
| 159 | } |
Srikanth Vavilapalli | d120f5c | 2015-11-24 14:15:01 -0800 | [diff] [blame] | 160 | |
| 161 | /** |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 162 | * Gets port statistics of all devices. |
Andrea Campanella | 10c4adc | 2015-12-03 15:27:54 -0800 | [diff] [blame] | 163 | * @onos.rsModel StatisticsPorts |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 164 | * @return 200 OK with JSON encoded array of port statistics |
Srikanth Vavilapalli | d120f5c | 2015-11-24 14:15:01 -0800 | [diff] [blame] | 165 | */ |
| 166 | @GET |
| 167 | @Path("ports") |
| 168 | @Produces(MediaType.APPLICATION_JSON) |
| 169 | public Response getPortStatistics() { |
| 170 | final DeviceService service = get(DeviceService.class); |
| 171 | final Iterable<Device> devices = service.getDevices(); |
| 172 | final ObjectNode root = mapper().createObjectNode(); |
| 173 | final ArrayNode rootArrayNode = root.putArray("statistics"); |
| 174 | for (final Device device : devices) { |
| 175 | final ObjectNode deviceStatsNode = mapper().createObjectNode(); |
| 176 | deviceStatsNode.put("device", device.id().toString()); |
| 177 | final ArrayNode statisticsNode = deviceStatsNode.putArray("ports"); |
| 178 | final Iterable<PortStatistics> portStatsEntries = service.getPortStatistics(device.id()); |
| 179 | if (portStatsEntries != null) { |
| 180 | for (final PortStatistics entry : portStatsEntries) { |
| 181 | statisticsNode.add(codec(PortStatistics.class).encode(entry, this)); |
| 182 | } |
| 183 | } |
| 184 | rootArrayNode.add(deviceStatsNode); |
| 185 | } |
| 186 | |
| 187 | return ok(root).build(); |
| 188 | } |
| 189 | |
| 190 | /** |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 191 | * Gets port statistics of a specified devices. |
Andrea Campanella | 10c4adc | 2015-12-03 15:27:54 -0800 | [diff] [blame] | 192 | * @onos.rsModel StatisticsPorts |
Srikanth Vavilapalli | d120f5c | 2015-11-24 14:15:01 -0800 | [diff] [blame] | 193 | * @param deviceId device ID |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 194 | * @return 200 OK with JSON encoded array of port statistics |
Srikanth Vavilapalli | d120f5c | 2015-11-24 14:15:01 -0800 | [diff] [blame] | 195 | */ |
| 196 | @GET |
| 197 | @Path("ports/{deviceId}") |
| 198 | @Produces(MediaType.APPLICATION_JSON) |
| 199 | public Response getPortStatisticsByDeviceId(@PathParam("deviceId") String deviceId) { |
| 200 | final DeviceService service = get(DeviceService.class); |
| 201 | final Iterable<PortStatistics> portStatsEntries = |
| 202 | service.getPortStatistics(DeviceId.deviceId(deviceId)); |
| 203 | final ObjectNode root = mapper().createObjectNode(); |
| 204 | final ArrayNode rootArrayNode = root.putArray("statistics"); |
| 205 | final ObjectNode deviceStatsNode = mapper().createObjectNode(); |
| 206 | deviceStatsNode.put("device", deviceId); |
| 207 | final ArrayNode statisticsNode = deviceStatsNode.putArray("ports"); |
| 208 | if (portStatsEntries != null) { |
| 209 | for (final PortStatistics entry : portStatsEntries) { |
| 210 | statisticsNode.add(codec(PortStatistics.class).encode(entry, this)); |
| 211 | } |
| 212 | } |
| 213 | rootArrayNode.add(deviceStatsNode); |
| 214 | |
| 215 | return ok(root).build(); |
| 216 | } |
| 217 | |
Ray Milkey | b9af946 | 2015-07-17 11:12:09 -0700 | [diff] [blame] | 218 | } |