Committed by
Gerrit Code Review
CORD-12:Adding support for port statistics REST API
Change-Id: Ibf0f7848ed891500e797d8f66bf7cd785b41c29c
Showing
6 changed files
with
226 additions
and
0 deletions
... | @@ -38,6 +38,7 @@ import org.onosproject.net.driver.Driver; | ... | @@ -38,6 +38,7 @@ import org.onosproject.net.driver.Driver; |
38 | import org.onosproject.net.flow.FlowEntry; | 38 | import org.onosproject.net.flow.FlowEntry; |
39 | import org.onosproject.net.flow.FlowRule; | 39 | import org.onosproject.net.flow.FlowRule; |
40 | import org.onosproject.net.flow.TableStatisticsEntry; | 40 | import org.onosproject.net.flow.TableStatisticsEntry; |
41 | +import org.onosproject.net.device.PortStatistics; | ||
41 | import org.onosproject.net.flow.TrafficSelector; | 42 | import org.onosproject.net.flow.TrafficSelector; |
42 | import org.onosproject.net.flow.TrafficTreatment; | 43 | import org.onosproject.net.flow.TrafficTreatment; |
43 | import org.onosproject.net.flow.criteria.Criterion; | 44 | import org.onosproject.net.flow.criteria.Criterion; |
... | @@ -102,6 +103,7 @@ public class CodecManager implements CodecService { | ... | @@ -102,6 +103,7 @@ public class CodecManager implements CodecService { |
102 | registerCodec(GroupBucket.class, new GroupBucketCodec()); | 103 | registerCodec(GroupBucket.class, new GroupBucketCodec()); |
103 | registerCodec(Load.class, new LoadCodec()); | 104 | registerCodec(Load.class, new LoadCodec()); |
104 | registerCodec(TableStatisticsEntry.class, new TableStatisticsEntryCodec()); | 105 | registerCodec(TableStatisticsEntry.class, new TableStatisticsEntryCodec()); |
106 | + registerCodec(PortStatistics.class, new PortStatisticsCodec()); | ||
105 | log.info("Started"); | 107 | log.info("Started"); |
106 | } | 108 | } |
107 | 109 | ... | ... |
... | @@ -39,6 +39,7 @@ public final class FlowEntryCodec extends JsonCodec<FlowEntry> { | ... | @@ -39,6 +39,7 @@ public final class FlowEntryCodec extends JsonCodec<FlowEntry> { |
39 | 39 | ||
40 | final ObjectNode result = context.mapper().createObjectNode() | 40 | final ObjectNode result = context.mapper().createObjectNode() |
41 | .put("id", Long.toString(flowEntry.id().value())) | 41 | .put("id", Long.toString(flowEntry.id().value())) |
42 | + .put("tableId", flowEntry.tableId()) | ||
42 | .put("appId", service.getAppId(flowEntry.appId()).name()) | 43 | .put("appId", service.getAppId(flowEntry.appId()).name()) |
43 | .put("groupId", flowEntry.groupId().id()) | 44 | .put("groupId", flowEntry.groupId().id()) |
44 | .put("priority", flowEntry.priority()) | 45 | .put("priority", flowEntry.priority()) | ... | ... |
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.codec.impl; | ||
17 | + | ||
18 | +import org.onosproject.codec.CodecContext; | ||
19 | +import org.onosproject.codec.JsonCodec; | ||
20 | +import org.onosproject.net.device.PortStatistics; | ||
21 | + | ||
22 | +import com.fasterxml.jackson.databind.node.ObjectNode; | ||
23 | + | ||
24 | +import static com.google.common.base.Preconditions.checkNotNull; | ||
25 | + | ||
26 | +/** | ||
27 | + * Port statistics entry JSON codec. | ||
28 | + */ | ||
29 | +public final class PortStatisticsCodec extends JsonCodec<PortStatistics> { | ||
30 | + | ||
31 | + @Override | ||
32 | + public ObjectNode encode(PortStatistics entry, CodecContext context) { | ||
33 | + checkNotNull(entry, "Port Statistics cannot be null"); | ||
34 | + | ||
35 | + final ObjectNode result = context.mapper().createObjectNode() | ||
36 | + .put("port", entry.port()) | ||
37 | + .put("packetsReceived", entry.packetsReceived()) | ||
38 | + .put("packetsSent", entry.packetsSent()) | ||
39 | + .put("bytesReceived", entry.bytesReceived()) | ||
40 | + .put("bytesSent", entry.bytesSent()) | ||
41 | + .put("packetsRxDropped", entry.packetsRxDropped()) | ||
42 | + .put("packetsTxDropped", entry.packetsTxDropped()) | ||
43 | + .put("packetsRxErrors", entry.packetsRxErrors()) | ||
44 | + .put("packetsTxErrors", entry.packetsTxErrors()) | ||
45 | + .put("durationSec", entry.durationSec()); | ||
46 | + | ||
47 | + return result; | ||
48 | + } | ||
49 | + | ||
50 | +} | ||
51 | + |
... | @@ -36,6 +36,7 @@ import org.onosproject.net.Device; | ... | @@ -36,6 +36,7 @@ import org.onosproject.net.Device; |
36 | import org.onosproject.net.DeviceId; | 36 | import org.onosproject.net.DeviceId; |
37 | import org.onosproject.net.Link; | 37 | import org.onosproject.net.Link; |
38 | import org.onosproject.net.device.DeviceService; | 38 | import org.onosproject.net.device.DeviceService; |
39 | +import org.onosproject.net.device.PortStatistics; | ||
39 | import org.onosproject.net.flow.FlowRuleService; | 40 | import org.onosproject.net.flow.FlowRuleService; |
40 | import org.onosproject.net.flow.TableStatisticsEntry; | 41 | import org.onosproject.net.flow.TableStatisticsEntry; |
41 | import org.onosproject.net.link.LinkService; | 42 | import org.onosproject.net.link.LinkService; |
... | @@ -153,4 +154,62 @@ public class StatisticsWebResource extends AbstractWebResource { | ... | @@ -153,4 +154,62 @@ public class StatisticsWebResource extends AbstractWebResource { |
153 | rootArrayNode.add(deviceStatsNode); | 154 | rootArrayNode.add(deviceStatsNode); |
154 | return ok(root).build(); | 155 | return ok(root).build(); |
155 | } | 156 | } |
157 | + | ||
158 | + /** | ||
159 | + * Get port statistics of all devices. | ||
160 | + * @rsModel StatisticsPorts | ||
161 | + * @return JSON encoded array of port statistics | ||
162 | + */ | ||
163 | + @GET | ||
164 | + @Path("ports") | ||
165 | + @Produces(MediaType.APPLICATION_JSON) | ||
166 | + public Response getPortStatistics() { | ||
167 | + final DeviceService service = get(DeviceService.class); | ||
168 | + final Iterable<Device> devices = service.getDevices(); | ||
169 | + final ObjectNode root = mapper().createObjectNode(); | ||
170 | + final ArrayNode rootArrayNode = root.putArray("statistics"); | ||
171 | + for (final Device device : devices) { | ||
172 | + final ObjectNode deviceStatsNode = mapper().createObjectNode(); | ||
173 | + deviceStatsNode.put("device", device.id().toString()); | ||
174 | + final ArrayNode statisticsNode = deviceStatsNode.putArray("ports"); | ||
175 | + final Iterable<PortStatistics> portStatsEntries = service.getPortStatistics(device.id()); | ||
176 | + if (portStatsEntries != null) { | ||
177 | + for (final PortStatistics entry : portStatsEntries) { | ||
178 | + statisticsNode.add(codec(PortStatistics.class).encode(entry, this)); | ||
179 | + } | ||
180 | + } | ||
181 | + rootArrayNode.add(deviceStatsNode); | ||
182 | + } | ||
183 | + | ||
184 | + return ok(root).build(); | ||
185 | + } | ||
186 | + | ||
187 | + /** | ||
188 | + * Get port statistics of a specified devices. | ||
189 | + * @rsModel StatisticsPorts | ||
190 | + * @param deviceId device ID | ||
191 | + * @return JSON encoded array of port statistics | ||
192 | + */ | ||
193 | + @GET | ||
194 | + @Path("ports/{deviceId}") | ||
195 | + @Produces(MediaType.APPLICATION_JSON) | ||
196 | + public Response getPortStatisticsByDeviceId(@PathParam("deviceId") String deviceId) { | ||
197 | + final DeviceService service = get(DeviceService.class); | ||
198 | + final Iterable<PortStatistics> portStatsEntries = | ||
199 | + service.getPortStatistics(DeviceId.deviceId(deviceId)); | ||
200 | + final ObjectNode root = mapper().createObjectNode(); | ||
201 | + final ArrayNode rootArrayNode = root.putArray("statistics"); | ||
202 | + final ObjectNode deviceStatsNode = mapper().createObjectNode(); | ||
203 | + deviceStatsNode.put("device", deviceId); | ||
204 | + final ArrayNode statisticsNode = deviceStatsNode.putArray("ports"); | ||
205 | + if (portStatsEntries != null) { | ||
206 | + for (final PortStatistics entry : portStatsEntries) { | ||
207 | + statisticsNode.add(codec(PortStatistics.class).encode(entry, this)); | ||
208 | + } | ||
209 | + } | ||
210 | + rootArrayNode.add(deviceStatsNode); | ||
211 | + | ||
212 | + return ok(root).build(); | ||
213 | + } | ||
214 | + | ||
156 | } | 215 | } | ... | ... |
... | @@ -16,6 +16,7 @@ | ... | @@ -16,6 +16,7 @@ |
16 | "title": "flow", | 16 | "title": "flow", |
17 | "required": [ | 17 | "required": [ |
18 | "id", | 18 | "id", |
19 | + "tableId", | ||
19 | "appId", | 20 | "appId", |
20 | "groupId", | 21 | "groupId", |
21 | "priority", | 22 | "priority", |
... | @@ -33,6 +34,11 @@ | ... | @@ -33,6 +34,11 @@ |
33 | "type": "string", | 34 | "type": "string", |
34 | "example": "12103425214920339" | 35 | "example": "12103425214920339" |
35 | }, | 36 | }, |
37 | + "tableId": { | ||
38 | + "type": "integer", | ||
39 | + "format": "int64", | ||
40 | + "example": 3 | ||
41 | + }, | ||
36 | "appId": { | 42 | "appId": { |
37 | "type": "string", | 43 | "type": "string", |
38 | "example": "org.onosproject.core" | 44 | "example": "org.onosproject.core" | ... | ... |
1 | +{ | ||
2 | + "type": "object", | ||
3 | + "title": "all-port-statistics", | ||
4 | + "required": [ | ||
5 | + "statistics" | ||
6 | + ], | ||
7 | + "properties": { | ||
8 | + "statistics": { | ||
9 | + "type": "array", | ||
10 | + "required": [ | ||
11 | + "statistics" | ||
12 | + ], | ||
13 | + "xml": { | ||
14 | + "name": "statistics", | ||
15 | + "wrapped": true | ||
16 | + }, | ||
17 | + "items": { | ||
18 | + "type": "object", | ||
19 | + "title": "statistics", | ||
20 | + "required": [ | ||
21 | + "ports" | ||
22 | + ], | ||
23 | + "properties": { | ||
24 | + "deviceId": { | ||
25 | + "type": "string", | ||
26 | + "example": "of:0000000000000001" | ||
27 | + }, | ||
28 | + "ports": { | ||
29 | + "type": "array", | ||
30 | + "xml": { | ||
31 | + "name": "ports", | ||
32 | + "wrapped": true | ||
33 | + }, | ||
34 | + "items": { | ||
35 | + "type": "object", | ||
36 | + "title": "ports", | ||
37 | + "required": [ | ||
38 | + "port", | ||
39 | + "packetsReceived", | ||
40 | + "packetsSent", | ||
41 | + "bytesReceived", | ||
42 | + "bytesSent", | ||
43 | + "packetsRxDropped", | ||
44 | + "packetsTxDropped", | ||
45 | + "packetsRxErrors", | ||
46 | + "packetsTxErrors", | ||
47 | + "durationSec" | ||
48 | + ], | ||
49 | + "properties": { | ||
50 | + "port": { | ||
51 | + "type": "integer", | ||
52 | + "format": "int64", | ||
53 | + "example": 1 | ||
54 | + }, | ||
55 | + "packetsReceived": { | ||
56 | + "type": "integer", | ||
57 | + "format": "int64", | ||
58 | + "example": 98 | ||
59 | + }, | ||
60 | + "packetsSent": { | ||
61 | + "type": "integer", | ||
62 | + "format": "int64", | ||
63 | + "example": 98 | ||
64 | + }, | ||
65 | + "bytesReceived": { | ||
66 | + "type": "integer", | ||
67 | + "format": "int64", | ||
68 | + "example": 9162 | ||
69 | + }, | ||
70 | + "bytesSent": { | ||
71 | + "type": "integer", | ||
72 | + "format": "int64", | ||
73 | + "example": 9162 | ||
74 | + }, | ||
75 | + "packetsRxDropped": { | ||
76 | + "type": "integer", | ||
77 | + "format": "int64", | ||
78 | + "example": 0 | ||
79 | + }, | ||
80 | + "packetsTxDropped": { | ||
81 | + "type": "integer", | ||
82 | + "format": "int64", | ||
83 | + "example": 0 | ||
84 | + }, | ||
85 | + "packetsRxErrors": { | ||
86 | + "type": "integer", | ||
87 | + "format": "int64", | ||
88 | + "example": 0 | ||
89 | + }, | ||
90 | + "packetsTxErrors": { | ||
91 | + "type": "integer", | ||
92 | + "format": "int64", | ||
93 | + "example": 0 | ||
94 | + }, | ||
95 | + "durationSec": { | ||
96 | + "type": "integer", | ||
97 | + "format": "int64", | ||
98 | + "example": 90 | ||
99 | + } | ||
100 | + } | ||
101 | + } | ||
102 | + } | ||
103 | + } | ||
104 | + } | ||
105 | + } | ||
106 | + } | ||
107 | +} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
-
Please register or login to post a comment