Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 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; |
Ravi Dewangan | 5edc84a | 2019-06-05 21:06:12 +0000 | [diff] [blame] | 26 | import javax.ws.rs.POST; |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 27 | import javax.ws.rs.Path; |
Ravi Dewangan | 5edc84a | 2019-06-05 21:06:12 +0000 | [diff] [blame] | 28 | import javax.ws.rs.Consumes; |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 29 | import javax.ws.rs.Produces; |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 30 | import javax.ws.rs.QueryParam; |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 31 | import javax.ws.rs.core.MediaType; |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 32 | import javax.ws.rs.core.Response; |
| 33 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 34 | import static org.onosproject.net.DeviceId.deviceId; |
| 35 | import static org.onosproject.net.PortNumber.portNumber; |
Ravi Dewangan | 5edc84a | 2019-06-05 21:06:12 +0000 | [diff] [blame] | 36 | import org.onosproject.cfg.ComponentConfigService; |
| 37 | import org.onosproject.cfg.ConfigProperty; |
| 38 | |
| 39 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 40 | import java.io.IOException; |
| 41 | import java.io.InputStream; |
| 42 | |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 43 | |
| 44 | /** |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 45 | * Manage inventory of infrastructure links. |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 46 | */ |
| 47 | @Path("links") |
| 48 | public class LinksWebResource extends AbstractWebResource { |
| 49 | |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 50 | /** |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 51 | * Gets infrastructure links. |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 52 | * 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] | 53 | * @onos.rsModel LinksGet |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 54 | * @param deviceId (optional) device identifier |
| 55 | * @param port (optional) port number |
| 56 | * @param direction (optional) direction qualifier |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 57 | * @return 200 OK with array of all links, or links for the specified device or port |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 58 | */ |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 59 | @GET |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 60 | @Produces(MediaType.APPLICATION_JSON) |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 61 | 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 Hart | 9bb32ab | 2015-05-05 18:17:31 -0700 | [diff] [blame] | 83 | Direction.valueOf(direction.toUpperCase()) : Direction.ALL; |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 84 | 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 Hart | 9bb32ab | 2015-05-05 18:17:31 -0700 | [diff] [blame] | 98 | Direction.valueOf(direction.toUpperCase()) : Direction.ALL; |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 99 | 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 Dewangan | 5edc84a | 2019-06-05 21:06:12 +0000 | [diff] [blame] | 109 | /** |
| 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 Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 161 | } |