Harshada Chaundkar | b78db19 | 2019-02-19 16:18:16 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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 | * |
| 16 | */ |
| 17 | |
| 18 | package org.onosproject.fpm.web; |
| 19 | |
| 20 | import org.onlab.packet.IpAddress; |
| 21 | |
| 22 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 23 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 24 | import org.onosproject.rest.AbstractWebResource; |
| 25 | import org.onosproject.routing.fpm.FpmPeerInfo; |
| 26 | import org.onosproject.routing.fpm.FpmInfoService; |
| 27 | import org.onosproject.routing.fpm.FpmPeer; |
William Davies | 5bae505 | 2019-10-19 01:25:01 -0700 | [diff] [blame^] | 28 | import org.onosproject.routing.fpm.FpmPeerAcceptRoutes; |
| 29 | import org.slf4j.Logger; |
| 30 | import org.slf4j.LoggerFactory; |
Harshada Chaundkar | b78db19 | 2019-02-19 16:18:16 -0500 | [diff] [blame] | 31 | |
William Davies | 5bae505 | 2019-10-19 01:25:01 -0700 | [diff] [blame^] | 32 | import java.io.IOException; |
| 33 | import static org.onlab.util.Tools.nullIsIllegal; |
| 34 | import static org.onlab.util.Tools.readTreeFromStream; |
Harshada Chaundkar | b78db19 | 2019-02-19 16:18:16 -0500 | [diff] [blame] | 35 | import javax.ws.rs.GET; |
| 36 | import javax.ws.rs.Path; |
| 37 | import javax.ws.rs.Produces; |
William Davies | 5bae505 | 2019-10-19 01:25:01 -0700 | [diff] [blame^] | 38 | import javax.ws.rs.PathParam; |
| 39 | import javax.ws.rs.Consumes; |
| 40 | import javax.ws.rs.POST; |
| 41 | import java.io.InputStream; |
Harshada Chaundkar | b78db19 | 2019-02-19 16:18:16 -0500 | [diff] [blame] | 42 | import javax.ws.rs.core.MediaType; |
| 43 | import javax.ws.rs.core.Response; |
Harshada Chaundkar | b78db19 | 2019-02-19 16:18:16 -0500 | [diff] [blame] | 44 | import java.util.Comparator; |
| 45 | import java.util.Map; |
William Davies | 5bae505 | 2019-10-19 01:25:01 -0700 | [diff] [blame^] | 46 | import java.util.List; |
Harshada Chaundkar | b78db19 | 2019-02-19 16:18:16 -0500 | [diff] [blame] | 47 | |
| 48 | /** |
| 49 | * FPM REST API. |
| 50 | */ |
| 51 | @Path("") |
| 52 | public class FpmWebResource extends AbstractWebResource { |
| 53 | |
William Davies | 5bae505 | 2019-10-19 01:25:01 -0700 | [diff] [blame^] | 54 | private static final String ACCEPT_ROUTES = "acceptRoutes"; |
| 55 | private static final String PEER_ADDRESS = "peerAddress"; |
| 56 | private static final String PEER_PORT = "peerPort"; |
| 57 | protected static final String PEERS = "peers"; |
| 58 | protected static final String PEERS_KEY_ERROR = "Peers key must be present"; |
| 59 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 60 | |
Harshada Chaundkar | b78db19 | 2019-02-19 16:18:16 -0500 | [diff] [blame] | 61 | /** |
| 62 | * To get all fpm connections. |
| 63 | * @return 200 OK with component properties of given component and variable. |
William Davies | 5bae505 | 2019-10-19 01:25:01 -0700 | [diff] [blame^] | 64 | * @onos.rsModel FpmConnectionsGet |
Harshada Chaundkar | b78db19 | 2019-02-19 16:18:16 -0500 | [diff] [blame] | 65 | */ |
| 66 | @GET |
| 67 | @Produces(MediaType.APPLICATION_JSON) |
| 68 | @Path("connections/") |
| 69 | public Response getFpmConnections() { |
| 70 | ObjectNode node = getFpmConnectionsJsonOutput(); |
| 71 | return Response.status(200).entity(node).build(); |
| 72 | } |
| 73 | |
William Davies | 5bae505 | 2019-10-19 01:25:01 -0700 | [diff] [blame^] | 74 | /** |
| 75 | * Performs disabling of FPM Peer. |
| 76 | * |
| 77 | * @param stream array of peer address and accept route flag |
| 78 | * @return 200 OK disable peer. |
| 79 | * @onos.rsModel FpmPeerSetAcceptRouteFlag |
| 80 | */ |
| 81 | @POST |
| 82 | @Path("acceptRoutes") |
| 83 | @Consumes(MediaType.APPLICATION_JSON) |
| 84 | @Produces(MediaType.APPLICATION_JSON) |
| 85 | public Response updateAcceptRouteFlagForConnection(InputStream stream) { |
| 86 | FpmInfoService fpmService = get(FpmInfoService.class); |
| 87 | try { |
| 88 | ObjectNode jsonTree = readTreeFromStream(mapper(), stream); |
| 89 | ArrayNode peersArray = nullIsIllegal((ArrayNode) jsonTree.get(PEERS), |
| 90 | PEERS_KEY_ERROR); |
| 91 | List<FpmPeerAcceptRoutes> fpmPeerRouteInfo = (new FpmAcceptRoutesCodec()).decode(peersArray, this); |
| 92 | fpmService.updateAcceptRouteFlag(fpmPeerRouteInfo); |
| 93 | } catch (IOException ex) { |
| 94 | throw new IllegalArgumentException(ex); |
| 95 | } |
| 96 | |
| 97 | return Response.ok().build(); |
| 98 | |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Gets peers acceptRoute Flag details. |
| 103 | * @param peerAddress peer identifier |
| 104 | * @return 200 OK with a collection of peerInfo |
| 105 | * @onos.rsModel FpmPeerGetAcceptRoutes |
| 106 | */ |
| 107 | @GET |
| 108 | @Produces(MediaType.APPLICATION_JSON) |
| 109 | @Path("acceptRoutes/{peerAddress}") |
| 110 | public Response getPeerAcceptRouteInfo(@PathParam("peerAddress") String peerAddress) { |
| 111 | ObjectNode node = getFpmPeerAcceptFlagInfoJsonOutput(peerAddress); |
| 112 | return Response.status(200).entity(node).build(); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Gets all peers acceptRoute Flag details. |
| 117 | * @return 200 OK with a collection of peerInfo |
| 118 | * @onos.rsModel FpmGetAcceptRoutes |
| 119 | */ |
| 120 | @GET |
| 121 | @Produces(MediaType.APPLICATION_JSON) |
| 122 | @Path("acceptRoutes/") |
| 123 | public Response getAllPeerAcceptRouteInfo() { |
| 124 | ObjectNode node = getFpmPeerRouteInfoJsonOutput(); |
| 125 | return Response.status(200).entity(node).build(); |
| 126 | } |
| 127 | |
Harshada Chaundkar | b78db19 | 2019-02-19 16:18:16 -0500 | [diff] [blame] | 128 | |
| 129 | private ObjectNode getFpmConnectionsJsonOutput() { |
| 130 | |
| 131 | FpmInfoService fpmService = get(FpmInfoService.class); |
| 132 | ObjectNode node = mapper().createObjectNode(); |
| 133 | ArrayNode connectionArray = mapper().createArrayNode(); |
| 134 | |
| 135 | Map<FpmPeer, FpmPeerInfo> fpmPeers = fpmService.peers(); |
| 136 | |
| 137 | fpmPeers.entrySet().stream() |
| 138 | .sorted(Comparator.<Map.Entry<FpmPeer, FpmPeerInfo>, IpAddress>comparing(e -> e.getKey().address()) |
| 139 | .thenComparing(e -> e.getKey().port())) |
| 140 | .map(Map.Entry::getValue) |
| 141 | .forEach(fpmPeerInfo -> connectionArray.add((new FpmCodec()).encode(fpmPeerInfo, this))); |
| 142 | |
| 143 | node.put("fpm-connections", connectionArray); |
| 144 | return node; |
| 145 | |
| 146 | } |
William Davies | 5bae505 | 2019-10-19 01:25:01 -0700 | [diff] [blame^] | 147 | |
| 148 | private ObjectNode getFpmPeerRouteInfoJsonOutput() { |
| 149 | |
| 150 | FpmInfoService fpmService = get(FpmInfoService.class); |
| 151 | ObjectNode node = mapper().createObjectNode(); |
| 152 | ArrayNode connectionArray = mapper().createArrayNode(); |
| 153 | Map<FpmPeer, FpmPeerInfo> fpmPeers = fpmService.peers(); |
| 154 | fpmPeers.entrySet().stream() |
| 155 | .sorted(Comparator.<Map.Entry<FpmPeer, FpmPeerInfo>, IpAddress>comparing(e -> e.getKey().address()) |
| 156 | .thenComparing(e -> e.getKey().port())) |
| 157 | .map(Map.Entry::getValue) |
| 158 | .forEach(fpmPeerInfo -> { |
| 159 | fpmPeerInfo.connections().forEach(connection -> { |
| 160 | ObjectNode fpmNode = mapper().createObjectNode(); |
| 161 | fpmNode.put(PEER_ADDRESS, connection.peer().address().toString()); |
| 162 | fpmNode.put(PEER_PORT, connection.peer().port()); |
| 163 | fpmNode.put(ACCEPT_ROUTES, connection.isAcceptRoutes()); |
| 164 | connectionArray.add(fpmNode); |
| 165 | }); |
| 166 | |
| 167 | }); |
| 168 | |
| 169 | node.put("fpm-peer-info", connectionArray); |
| 170 | return node; |
| 171 | |
| 172 | |
| 173 | } |
| 174 | |
| 175 | private ObjectNode getFpmPeerAcceptFlagInfoJsonOutput(String address) { |
| 176 | |
| 177 | FpmInfoService fpmService = get(FpmInfoService.class); |
| 178 | ObjectNode fpmNode = mapper().createObjectNode(); |
| 179 | Map<FpmPeer, FpmPeerInfo> fpmPeers = fpmService.peers(); |
| 180 | IpAddress peerAddress = IpAddress.valueOf(address); |
| 181 | fpmPeers.entrySet().stream() |
| 182 | .filter(peer -> peer.getKey().address().equals(peerAddress)) |
| 183 | .map(Map.Entry::getValue) |
| 184 | .forEach(fpmPeerInfo -> { |
| 185 | fpmPeerInfo.connections().forEach(connection -> { |
| 186 | fpmNode.put(ACCEPT_ROUTES, connection.isAcceptRoutes()); |
| 187 | }); |
| 188 | }); |
| 189 | |
| 190 | |
| 191 | return fpmNode; |
| 192 | |
| 193 | |
| 194 | } |
Harshada Chaundkar | b78db19 | 2019-02-19 16:18:16 -0500 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | |