blob: 1922d039c2a5ad545fc89be54983dd1647a57165 [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 */
Jonathan Hart9bb32ab2015-05-05 18:17:31 -070016package org.onosproject.rest.resources;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080017
Brian O'Connorabafb502014-12-02 22:26:20 -080018import org.onosproject.net.ConnectPoint;
19import org.onosproject.net.DeviceId;
20import org.onosproject.net.Link;
21import org.onosproject.net.link.LinkService;
Jonathan Hart9bb32ab2015-05-05 18:17:31 -070022import org.onosproject.rest.AbstractWebResource;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080023
24import javax.ws.rs.GET;
25import javax.ws.rs.Path;
26import javax.ws.rs.QueryParam;
27import javax.ws.rs.core.Response;
28
Brian O'Connorabafb502014-12-02 22:26:20 -080029import static org.onosproject.net.DeviceId.deviceId;
30import static org.onosproject.net.PortNumber.portNumber;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080031
32/**
33 * REST resource for interacting with the inventory of infrastructure links.
34 */
35@Path("links")
36public class LinksWebResource extends AbstractWebResource {
37
Ray Milkey5d915f42015-08-13 10:27:53 -070038 enum Direction {
39 ALL,
40 INGRESS,
41 EGRESS
42 }
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080043
44 @GET
45 public Response getLinks(@QueryParam("device") String deviceId,
46 @QueryParam("port") String port,
47 @QueryParam("direction") String direction) {
48 LinkService service = get(LinkService.class);
49 Iterable<Link> links;
50
51 if (deviceId != null && port != null) {
52 links = getConnectPointLinks(new ConnectPoint(deviceId(deviceId),
53 portNumber(port)),
54 direction, service);
55 } else if (deviceId != null) {
56 links = getDeviceLinks(deviceId(deviceId), direction, service);
57 } else {
58 links = service.getLinks();
59 }
60 return ok(encodeArray(Link.class, "links", links)).build();
61 }
62
63 private Iterable<Link> getConnectPointLinks(ConnectPoint point,
64 String direction,
65 LinkService service) {
66 Direction dir = direction != null ?
Jonathan Hart9bb32ab2015-05-05 18:17:31 -070067 Direction.valueOf(direction.toUpperCase()) : Direction.ALL;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080068 switch (dir) {
69 case INGRESS:
70 return service.getIngressLinks(point);
71 case EGRESS:
72 return service.getEgressLinks(point);
73 default:
74 return service.getLinks(point);
75 }
76 }
77
78 private Iterable<Link> getDeviceLinks(DeviceId deviceId,
79 String direction,
80 LinkService service) {
81 Direction dir = direction != null ?
Jonathan Hart9bb32ab2015-05-05 18:17:31 -070082 Direction.valueOf(direction.toUpperCase()) : Direction.ALL;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080083 switch (dir) {
84 case INGRESS:
85 return service.getDeviceIngressLinks(deviceId);
86 case EGRESS:
87 return service.getDeviceEgressLinks(deviceId);
88 default:
89 return service.getDeviceLinks(deviceId);
90 }
91 }
92
93}