Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 1 | /* |
| 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 Hart | 9bb32ab | 2015-05-05 18:17:31 -0700 | [diff] [blame] | 16 | package org.onosproject.rest.resources; |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 17 | |
Marc De Leenheer | 118f671 | 2015-10-21 16:06:21 -0700 | [diff] [blame] | 18 | import org.onosproject.net.Direction; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 19 | import org.onosproject.net.ConnectPoint; |
| 20 | import org.onosproject.net.DeviceId; |
| 21 | import org.onosproject.net.Link; |
| 22 | import org.onosproject.net.link.LinkService; |
Jonathan Hart | 9bb32ab | 2015-05-05 18:17:31 -0700 | [diff] [blame] | 23 | import org.onosproject.rest.AbstractWebResource; |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 24 | |
| 25 | import javax.ws.rs.GET; |
| 26 | import javax.ws.rs.Path; |
| 27 | import javax.ws.rs.QueryParam; |
| 28 | import javax.ws.rs.core.Response; |
| 29 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 30 | import static org.onosproject.net.DeviceId.deviceId; |
| 31 | import static org.onosproject.net.PortNumber.portNumber; |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 32 | |
| 33 | /** |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 34 | * Manage inventory of infrastructure links. |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 35 | */ |
| 36 | @Path("links") |
| 37 | public class LinksWebResource extends AbstractWebResource { |
| 38 | |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 39 | /** |
| 40 | * Get infrastructure links. |
| 41 | * Returns array of all links, or links for the specified device or port. |
Andrea Campanella | 10c4adc | 2015-12-03 15:27:54 -0800 | [diff] [blame] | 42 | * @onos.rsModel LinksGet |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 43 | * @param deviceId (optional) device identifier |
| 44 | * @param port (optional) port number |
| 45 | * @param direction (optional) direction qualifier |
| 46 | * @return 200 OK |
| 47 | */ |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 48 | @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 Hart | 9bb32ab | 2015-05-05 18:17:31 -0700 | [diff] [blame] | 71 | Direction.valueOf(direction.toUpperCase()) : Direction.ALL; |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 72 | 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 Hart | 9bb32ab | 2015-05-05 18:17:31 -0700 | [diff] [blame] | 86 | Direction.valueOf(direction.toUpperCase()) : Direction.ALL; |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 87 | 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 | } |