blob: 1ef6b3f86db1e4597eb2ca686eab7276fcc6861e [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
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 ?
Jonathan Hart9bb32ab2015-05-05 18:17:31 -070063 Direction.valueOf(direction.toUpperCase()) : Direction.ALL;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080064 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 ?
Jonathan Hart9bb32ab2015-05-05 18:17:31 -070078 Direction.valueOf(direction.toUpperCase()) : Direction.ALL;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080079 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}