blob: cb4f0a4c70734284f4e15073c625acd5c825a5a5 [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;
Ravi Dewangan5edc84a2019-06-05 21:06:12 +000026import javax.ws.rs.POST;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080027import javax.ws.rs.Path;
Ravi Dewangan5edc84a2019-06-05 21:06:12 +000028import javax.ws.rs.Consumes;
Jian Licc730a62016-05-10 16:36:16 -070029import javax.ws.rs.Produces;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080030import javax.ws.rs.QueryParam;
Jian Licc730a62016-05-10 16:36:16 -070031import javax.ws.rs.core.MediaType;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080032import javax.ws.rs.core.Response;
33
Brian O'Connorabafb502014-12-02 22:26:20 -080034import static org.onosproject.net.DeviceId.deviceId;
35import static org.onosproject.net.PortNumber.portNumber;
Ravi Dewangan5edc84a2019-06-05 21:06:12 +000036import org.onosproject.cfg.ComponentConfigService;
37import org.onosproject.cfg.ConfigProperty;
38
39import com.fasterxml.jackson.databind.node.ObjectNode;
40import java.io.IOException;
41import java.io.InputStream;
42
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080043
44/**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070045 * Manage inventory of infrastructure links.
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080046 */
47@Path("links")
48public class LinksWebResource extends AbstractWebResource {
49
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070050 /**
Jian Licc730a62016-05-10 16:36:16 -070051 * Gets infrastructure links.
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070052 * Returns array of all links, or links for the specified device or port.
Andrea Campanella10c4adc2015-12-03 15:27:54 -080053 * @onos.rsModel LinksGet
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070054 * @param deviceId (optional) device identifier
55 * @param port (optional) port number
56 * @param direction (optional) direction qualifier
Jian Licc730a62016-05-10 16:36:16 -070057 * @return 200 OK with array of all links, or links for the specified device or port
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070058 */
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080059 @GET
Jian Licc730a62016-05-10 16:36:16 -070060 @Produces(MediaType.APPLICATION_JSON)
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080061 public Response getLinks(@QueryParam("device") String deviceId,
62 @QueryParam("port") String port,
63 @QueryParam("direction") String direction) {
64 LinkService service = get(LinkService.class);
65 Iterable<Link> links;
66
67 if (deviceId != null && port != null) {
68 links = getConnectPointLinks(new ConnectPoint(deviceId(deviceId),
69 portNumber(port)),
70 direction, service);
71 } else if (deviceId != null) {
72 links = getDeviceLinks(deviceId(deviceId), direction, service);
73 } else {
74 links = service.getLinks();
75 }
76 return ok(encodeArray(Link.class, "links", links)).build();
77 }
78
79 private Iterable<Link> getConnectPointLinks(ConnectPoint point,
80 String direction,
81 LinkService service) {
82 Direction dir = direction != null ?
Jonathan Hart9bb32ab2015-05-05 18:17:31 -070083 Direction.valueOf(direction.toUpperCase()) : Direction.ALL;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080084 switch (dir) {
85 case INGRESS:
86 return service.getIngressLinks(point);
87 case EGRESS:
88 return service.getEgressLinks(point);
89 default:
90 return service.getLinks(point);
91 }
92 }
93
94 private Iterable<Link> getDeviceLinks(DeviceId deviceId,
95 String direction,
96 LinkService service) {
97 Direction dir = direction != null ?
Jonathan Hart9bb32ab2015-05-05 18:17:31 -070098 Direction.valueOf(direction.toUpperCase()) : Direction.ALL;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080099 switch (dir) {
100 case INGRESS:
101 return service.getDeviceIngressLinks(deviceId);
102 case EGRESS:
103 return service.getDeviceEgressLinks(deviceId);
104 default:
105 return service.getDeviceLinks(deviceId);
106 }
107 }
108
Ravi Dewangan5edc84a2019-06-05 21:06:12 +0000109 /**
110 * Get useStaleLinkAge active status.
111 * Returns current status of the VanishedStaleLink.
112 *
113 * @onos.rsModel VanishedLink
114 * @return 200 ok with the VanishedStaleLink status.
115 */
116 @GET
117 @Path("{usestalelinkage}")
118 @Produces(MediaType.APPLICATION_JSON)
119 public Response getVanishStaleLink() {
120 ObjectNode root = mapper().createObjectNode();
121 ComponentConfigService useStaleLink = get(ComponentConfigService.class);
122
123 for (ConfigProperty prop : useStaleLink.getProperties("org.onosproject.provider.lldp.impl.LldpLinkProvider")) {
124 if (prop.name().equals("useStaleLinkAge")) {
125 root.put("active", Boolean.valueOf(prop.value()));
126 break;
127 }
128 }
129 return ok(root).build();
130 }
131
132 /**
133 * Set useStaleLinkAge status.
134 *
135 * @onos.rsModel VanishedLink
136 * @param stream input JSON
137 * @return 200 ok.
138 * BAD_REQUEST if the JSON is invalid
139 */
140 @POST
141 @Path("{usestalelinkage}")
142 @Consumes(MediaType.APPLICATION_JSON)
143 @Produces(MediaType.APPLICATION_JSON)
144 public Response setVanishStaleLink(InputStream stream) {
145 try {
146 // Parse the input stream
147 ObjectNode root = (ObjectNode) mapper().readTree(stream);
148 if (root.has("active")) {
149 ComponentConfigService useStaleLink = get(ComponentConfigService.class);
150 useStaleLink.setProperty("org.onosproject.provider.lldp.impl.LldpLinkProvider",
151 "useStaleLinkAge", String.valueOf(root.get("active")));
152 }
153 } catch (IOException ex) {
154 throw new IllegalArgumentException(ex);
155 }
156 return Response
157 .ok()
158 .build();
159 }
160
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -0800161}