blob: 3c74d947ecdc41a979c4318f8ea1754c196acea0 [file] [log] [blame]
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -08003 *
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;
Jian Licc730a62016-05-10 16:36:16 -070027import javax.ws.rs.Produces;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080028import javax.ws.rs.QueryParam;
Jian Licc730a62016-05-10 16:36:16 -070029import javax.ws.rs.core.MediaType;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080030import javax.ws.rs.core.Response;
31
Brian O'Connorabafb502014-12-02 22:26:20 -080032import static org.onosproject.net.DeviceId.deviceId;
33import static org.onosproject.net.PortNumber.portNumber;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080034
35/**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070036 * Manage inventory of infrastructure links.
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080037 */
38@Path("links")
39public class LinksWebResource extends AbstractWebResource {
40
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070041 /**
Jian Licc730a62016-05-10 16:36:16 -070042 * Gets infrastructure links.
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070043 * Returns array of all links, or links for the specified device or port.
Andrea Campanella10c4adc2015-12-03 15:27:54 -080044 * @onos.rsModel LinksGet
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070045 * @param deviceId (optional) device identifier
46 * @param port (optional) port number
47 * @param direction (optional) direction qualifier
Jian Licc730a62016-05-10 16:36:16 -070048 * @return 200 OK with array of all links, or links for the specified device or port
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070049 */
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080050 @GET
Jian Licc730a62016-05-10 16:36:16 -070051 @Produces(MediaType.APPLICATION_JSON)
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080052 public Response getLinks(@QueryParam("device") String deviceId,
53 @QueryParam("port") String port,
54 @QueryParam("direction") String direction) {
55 LinkService service = get(LinkService.class);
56 Iterable<Link> links;
57
58 if (deviceId != null && port != null) {
59 links = getConnectPointLinks(new ConnectPoint(deviceId(deviceId),
60 portNumber(port)),
61 direction, service);
62 } else if (deviceId != null) {
63 links = getDeviceLinks(deviceId(deviceId), direction, service);
64 } else {
65 links = service.getLinks();
66 }
67 return ok(encodeArray(Link.class, "links", links)).build();
68 }
69
70 private Iterable<Link> getConnectPointLinks(ConnectPoint point,
71 String direction,
72 LinkService service) {
73 Direction dir = direction != null ?
Jonathan Hart9bb32ab2015-05-05 18:17:31 -070074 Direction.valueOf(direction.toUpperCase()) : Direction.ALL;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080075 switch (dir) {
76 case INGRESS:
77 return service.getIngressLinks(point);
78 case EGRESS:
79 return service.getEgressLinks(point);
80 default:
81 return service.getLinks(point);
82 }
83 }
84
85 private Iterable<Link> getDeviceLinks(DeviceId deviceId,
86 String direction,
87 LinkService service) {
88 Direction dir = direction != null ?
Jonathan Hart9bb32ab2015-05-05 18:17:31 -070089 Direction.valueOf(direction.toUpperCase()) : Direction.ALL;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080090 switch (dir) {
91 case INGRESS:
92 return service.getDeviceIngressLinks(deviceId);
93 case EGRESS:
94 return service.getDeviceEgressLinks(deviceId);
95 default:
96 return service.getDeviceLinks(deviceId);
97 }
98 }
99
100}