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; |
| 24 | import javax.ws.rs.Produces; |
| 25 | import javax.ws.rs.QueryParam; |
| 26 | import javax.ws.rs.core.Context; |
| 27 | import javax.ws.rs.core.MediaType; |
| 28 | import javax.ws.rs.core.Response; |
| 29 | import javax.ws.rs.core.UriBuilder; |
| 30 | import javax.ws.rs.core.UriInfo; |
| 31 | |
| 32 | import org.onosproject.codec.JsonCodec; |
| 33 | import org.onosproject.net.ConnectPoint; |
| 34 | import org.onosproject.net.Link; |
| 35 | import org.onosproject.net.link.LinkService; |
| 36 | import org.onosproject.net.statistic.Load; |
| 37 | import org.onosproject.net.statistic.StatisticService; |
| 38 | import org.onosproject.rest.AbstractWebResource; |
| 39 | |
| 40 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 41 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 42 | |
| 43 | import static org.onosproject.net.DeviceId.deviceId; |
| 44 | import static org.onosproject.net.PortNumber.portNumber; |
| 45 | |
| 46 | /** |
| 47 | * Statistics REST APIs. |
| 48 | */ |
| 49 | @Path("statistics") |
| 50 | public 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 | } |