blob: 36bb0ad7b172b3db7c43d96977f545a267c77a35 [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;
24import javax.ws.rs.Produces;
25import javax.ws.rs.QueryParam;
26import javax.ws.rs.core.Context;
27import javax.ws.rs.core.MediaType;
28import javax.ws.rs.core.Response;
29import javax.ws.rs.core.UriBuilder;
30import javax.ws.rs.core.UriInfo;
31
32import org.onosproject.codec.JsonCodec;
33import org.onosproject.net.ConnectPoint;
34import org.onosproject.net.Link;
35import org.onosproject.net.link.LinkService;
36import org.onosproject.net.statistic.Load;
37import org.onosproject.net.statistic.StatisticService;
38import org.onosproject.rest.AbstractWebResource;
39
40import com.fasterxml.jackson.databind.node.ArrayNode;
41import com.fasterxml.jackson.databind.node.ObjectNode;
42
43import static org.onosproject.net.DeviceId.deviceId;
44import static org.onosproject.net.PortNumber.portNumber;
45
46/**
47 * Statistics REST APIs.
48 */
49@Path("statistics")
50public class StatisticsWebResource extends AbstractWebResource {
51 @Context
52 UriInfo uriInfo;
53
54 /**
55 * Gets the Load statistics for all links, or for a specific link.
56 *
57 * @param deviceId (optional) device ID for a specific link
58 * @param port (optional) port number for a specified link
59 * @return JSON encoded array lof Load objects
60 */
61 @GET
62 @Path("flows/link")
63 @Produces(MediaType.APPLICATION_JSON)
64 public Response getLoads(@QueryParam("device") String deviceId,
65 @QueryParam("port") String port) {
66 Iterable<Link> links;
67
68 if (deviceId == null || port == null) {
69 links = get(LinkService.class).getLinks();
70 } else {
71 ConnectPoint connectPoint = new ConnectPoint(deviceId(deviceId),
72 portNumber(port));
73 links = get(LinkService.class).getLinks(connectPoint);
74 }
75 ObjectNode result = mapper().createObjectNode();
76 ArrayNode loads = mapper().createArrayNode();
77 JsonCodec<Load> loadCodec = codec(Load.class);
78 StatisticService statsService = getService(StatisticService.class);
79
80
81 StreamSupport.stream(Spliterators.spliteratorUnknownSize(
82 links.iterator(), Spliterator.ORDERED), false)
83 .forEach(link -> {
84 ObjectNode loadNode = loadCodec.encode(statsService.load(link), this);
85
86 UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
87 .path("links")
88 .queryParam("device", link.src().deviceId().toString())
89 .queryParam("port", link.src().port().toString());
90 loadNode.put("link", locationBuilder.build().toString());
91 loads.add(loadNode);
92 });
93 result.set("loads", loads);
94 return ok(result).build();
95 }
96}