blob: 7a452044ad3c0a2dbc282ee5219730655aef57df [file] [log] [blame]
Ray Milkeyb9af9462015-07-17 11:12:09 -07001/*
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.rest.resources;
17
18import java.util.Spliterator;
19import java.util.Spliterators;
20import java.util.stream.StreamSupport;
21
22import javax.ws.rs.GET;
23import javax.ws.rs.Path;
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -070024import javax.ws.rs.PathParam;
Ray Milkeyb9af9462015-07-17 11:12:09 -070025import javax.ws.rs.Produces;
26import javax.ws.rs.QueryParam;
27import javax.ws.rs.core.Context;
28import javax.ws.rs.core.MediaType;
29import javax.ws.rs.core.Response;
30import javax.ws.rs.core.UriBuilder;
31import javax.ws.rs.core.UriInfo;
32
33import org.onosproject.codec.JsonCodec;
34import org.onosproject.net.ConnectPoint;
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -070035import org.onosproject.net.Device;
36import org.onosproject.net.DeviceId;
Ray Milkeyb9af9462015-07-17 11:12:09 -070037import org.onosproject.net.Link;
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -070038import org.onosproject.net.device.DeviceService;
39import org.onosproject.net.flow.FlowRuleService;
40import org.onosproject.net.flow.TableStatisticsEntry;
Ray Milkeyb9af9462015-07-17 11:12:09 -070041import org.onosproject.net.link.LinkService;
42import org.onosproject.net.statistic.Load;
43import org.onosproject.net.statistic.StatisticService;
44import org.onosproject.rest.AbstractWebResource;
45
46import com.fasterxml.jackson.databind.node.ArrayNode;
47import com.fasterxml.jackson.databind.node.ObjectNode;
48
49import static org.onosproject.net.DeviceId.deviceId;
50import static org.onosproject.net.PortNumber.portNumber;
51
52/**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070053 * Query flow statistics.
Ray Milkeyb9af9462015-07-17 11:12:09 -070054 */
55@Path("statistics")
56public class StatisticsWebResource extends AbstractWebResource {
57 @Context
58 UriInfo uriInfo;
59
60 /**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070061 * Get load statistics for all links or for a specific link.
andrea1ce2bc82015-11-18 16:58:10 -080062 * @rsModel StatisticsFlowsLink
Ray Milkeyb9af9462015-07-17 11:12:09 -070063 * @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 Milkeyb9af9462015-07-17 11:12:09 -070086 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 Vavilapalli95810f52015-09-14 15:49:56 -0700101
102 /**
103 * Get table statistics for all tables of all devices.
andrea1ce2bc82015-11-18 16:58:10 -0800104 * @rsModel StatisticsFlowsTables
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700105 * @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();
andrea1ce2bc82015-11-18 16:58:10 -0800114 final ArrayNode rootArrayNode = root.putArray("statistics");
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700115 for (final Device device : devices) {
116 final ObjectNode deviceStatsNode = mapper().createObjectNode();
117 deviceStatsNode.put("device", device.id().toString());
andrea1ce2bc82015-11-18 16:58:10 -0800118 final ArrayNode statisticsNode = deviceStatsNode.putArray("table");
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700119 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.
andrea1ce2bc82015-11-18 16:58:10 -0800133 * @rsModel StatisticsFlowsTables
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700134 * @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();
andrea1ce2bc82015-11-18 16:58:10 -0800145 final ArrayNode rootArrayNode = root.putArray("statistics");
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700146
147 final ObjectNode deviceStatsNode = mapper().createObjectNode();
148 deviceStatsNode.put("device", deviceId);
andrea1ce2bc82015-11-18 16:58:10 -0800149 final ArrayNode statisticsNode = deviceStatsNode.putArray("table");
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700150 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 Milkeyb9af9462015-07-17 11:12:09 -0700156}