blob: abbe0ac47863c4df930c50c6d9876e5d708b2b97 [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
Marc De Leenheer118f6712015-10-21 16:06:21 -070018import org.onosproject.net.Direction;
Brian O'Connorabafb502014-12-02 22:26:20 -080019import org.onosproject.net.ConnectPoint;
20import org.onosproject.net.DeviceId;
21import org.onosproject.net.Link;
22import org.onosproject.net.link.LinkService;
Jonathan Hart9bb32ab2015-05-05 18:17:31 -070023import org.onosproject.rest.AbstractWebResource;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080024
25import javax.ws.rs.GET;
26import javax.ws.rs.Path;
27import javax.ws.rs.QueryParam;
28import javax.ws.rs.core.Response;
29
Brian O'Connorabafb502014-12-02 22:26:20 -080030import static org.onosproject.net.DeviceId.deviceId;
31import static org.onosproject.net.PortNumber.portNumber;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080032
33/**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070034 * Manage inventory of infrastructure links.
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080035 */
36@Path("links")
37public class LinksWebResource extends AbstractWebResource {
38
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070039 /**
40 * Get infrastructure links.
41 * Returns array of all links, or links for the specified device or port.
Andrea Campanella10c4adc2015-12-03 15:27:54 -080042 * @onos.rsModel LinksGet
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070043 * @param deviceId (optional) device identifier
44 * @param port (optional) port number
45 * @param direction (optional) direction qualifier
46 * @return 200 OK
47 */
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080048 @GET
49 public Response getLinks(@QueryParam("device") String deviceId,
50 @QueryParam("port") String port,
51 @QueryParam("direction") String direction) {
52 LinkService service = get(LinkService.class);
53 Iterable<Link> links;
54
55 if (deviceId != null && port != null) {
56 links = getConnectPointLinks(new ConnectPoint(deviceId(deviceId),
57 portNumber(port)),
58 direction, service);
59 } else if (deviceId != null) {
60 links = getDeviceLinks(deviceId(deviceId), direction, service);
61 } else {
62 links = service.getLinks();
63 }
64 return ok(encodeArray(Link.class, "links", links)).build();
65 }
66
67 private Iterable<Link> getConnectPointLinks(ConnectPoint point,
68 String direction,
69 LinkService service) {
70 Direction dir = direction != null ?
Jonathan Hart9bb32ab2015-05-05 18:17:31 -070071 Direction.valueOf(direction.toUpperCase()) : Direction.ALL;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080072 switch (dir) {
73 case INGRESS:
74 return service.getIngressLinks(point);
75 case EGRESS:
76 return service.getEgressLinks(point);
77 default:
78 return service.getLinks(point);
79 }
80 }
81
82 private Iterable<Link> getDeviceLinks(DeviceId deviceId,
83 String direction,
84 LinkService service) {
85 Direction dir = direction != null ?
Jonathan Hart9bb32ab2015-05-05 18:17:31 -070086 Direction.valueOf(direction.toUpperCase()) : Direction.ALL;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080087 switch (dir) {
88 case INGRESS:
89 return service.getDeviceIngressLinks(deviceId);
90 case EGRESS:
91 return service.getDeviceEgressLinks(deviceId);
92 default:
93 return service.getDeviceLinks(deviceId);
94 }
95 }
96
97}