blob: edf9f028154d210ddd2ad8e9c15aeb086f8160ba [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/**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070033 * Manage inventory of infrastructure links.
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080034 */
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
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070044 /**
45 * Get infrastructure links.
46 * Returns array of all links, or links for the specified device or port.
Andrea Campanella10c4adc2015-12-03 15:27:54 -080047 * @onos.rsModel LinksGet
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070048 * @param deviceId (optional) device identifier
49 * @param port (optional) port number
50 * @param direction (optional) direction qualifier
51 * @return 200 OK
52 */
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080053 @GET
54 public Response getLinks(@QueryParam("device") String deviceId,
55 @QueryParam("port") String port,
56 @QueryParam("direction") String direction) {
57 LinkService service = get(LinkService.class);
58 Iterable<Link> links;
59
60 if (deviceId != null && port != null) {
61 links = getConnectPointLinks(new ConnectPoint(deviceId(deviceId),
62 portNumber(port)),
63 direction, service);
64 } else if (deviceId != null) {
65 links = getDeviceLinks(deviceId(deviceId), direction, service);
66 } else {
67 links = service.getLinks();
68 }
69 return ok(encodeArray(Link.class, "links", links)).build();
70 }
71
72 private Iterable<Link> getConnectPointLinks(ConnectPoint point,
73 String direction,
74 LinkService service) {
75 Direction dir = direction != null ?
Jonathan Hart9bb32ab2015-05-05 18:17:31 -070076 Direction.valueOf(direction.toUpperCase()) : Direction.ALL;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080077 switch (dir) {
78 case INGRESS:
79 return service.getIngressLinks(point);
80 case EGRESS:
81 return service.getEgressLinks(point);
82 default:
83 return service.getLinks(point);
84 }
85 }
86
87 private Iterable<Link> getDeviceLinks(DeviceId deviceId,
88 String direction,
89 LinkService service) {
90 Direction dir = direction != null ?
Jonathan Hart9bb32ab2015-05-05 18:17:31 -070091 Direction.valueOf(direction.toUpperCase()) : Direction.ALL;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080092 switch (dir) {
93 case INGRESS:
94 return service.getDeviceIngressLinks(deviceId);
95 case EGRESS:
96 return service.getDeviceEgressLinks(deviceId);
97 default:
98 return service.getDeviceLinks(deviceId);
99 }
100 }
101
102}