blob: 479b6a0bec3ed0dc1024ca6c6b31e3d575726435 [file] [log] [blame]
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -08001/*
2 * Copyright 2014 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.onlab.onos.rest;
17
18import org.onlab.onos.net.ConnectPoint;
19import org.onlab.onos.net.DeviceId;
20import org.onlab.onos.net.Link;
21import org.onlab.onos.net.link.LinkService;
22
23import javax.ws.rs.GET;
24import javax.ws.rs.Path;
25import javax.ws.rs.QueryParam;
26import javax.ws.rs.core.Response;
27
28import static org.onlab.onos.net.DeviceId.deviceId;
29import static org.onlab.onos.net.PortNumber.portNumber;
30import static org.onlab.onos.rest.LinksWebResource.Direction.valueOf;
31
32/**
33 * REST resource for interacting with the inventory of infrastructure links.
34 */
35@Path("links")
36public class LinksWebResource extends AbstractWebResource {
37
38 enum Direction { ALL, INGRESS, EGRESS }
39
40 @GET
41 public Response getLinks(@QueryParam("device") String deviceId,
42 @QueryParam("port") String port,
43 @QueryParam("direction") String direction) {
44 LinkService service = get(LinkService.class);
45 Iterable<Link> links;
46
47 if (deviceId != null && port != null) {
48 links = getConnectPointLinks(new ConnectPoint(deviceId(deviceId),
49 portNumber(port)),
50 direction, service);
51 } else if (deviceId != null) {
52 links = getDeviceLinks(deviceId(deviceId), direction, service);
53 } else {
54 links = service.getLinks();
55 }
56 return ok(encodeArray(Link.class, "links", links)).build();
57 }
58
59 private Iterable<Link> getConnectPointLinks(ConnectPoint point,
60 String direction,
61 LinkService service) {
62 Direction dir = direction != null ?
63 valueOf(direction.toUpperCase()) : Direction.ALL;
64 switch (dir) {
65 case INGRESS:
66 return service.getIngressLinks(point);
67 case EGRESS:
68 return service.getEgressLinks(point);
69 default:
70 return service.getLinks(point);
71 }
72 }
73
74 private Iterable<Link> getDeviceLinks(DeviceId deviceId,
75 String direction,
76 LinkService service) {
77 Direction dir = direction != null ?
78 valueOf(direction.toUpperCase()) : Direction.ALL;
79 switch (dir) {
80 case INGRESS:
81 return service.getDeviceIngressLinks(deviceId);
82 case EGRESS:
83 return service.getDeviceEgressLinks(deviceId);
84 default:
85 return service.getDeviceLinks(deviceId);
86 }
87 }
88
89}