Andreas Pantelopoulos | cd33959 | 2018-02-23 14:18:00 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015-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 | package org.onosproject.segmentrouting.web; |
| 17 | |
Andreas Pantelopoulos | ff691b7 | 2018-03-12 16:30:20 -0700 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.JsonNode; |
Andreas Pantelopoulos | cd33959 | 2018-02-23 14:18:00 -0800 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
Andreas Pantelopoulos | ff691b7 | 2018-03-12 16:30:20 -0700 | [diff] [blame] | 20 | import com.fasterxml.jackson.databind.node.ArrayNode; |
Andreas Pantelopoulos | cd33959 | 2018-02-23 14:18:00 -0800 | [diff] [blame] | 21 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Andreas Pantelopoulos | ff691b7 | 2018-03-12 16:30:20 -0700 | [diff] [blame] | 22 | import org.onlab.util.ItemNotFoundException; |
Andreas Pantelopoulos | cd33959 | 2018-02-23 14:18:00 -0800 | [diff] [blame] | 23 | import org.onosproject.rest.AbstractWebResource; |
| 24 | import org.onosproject.segmentrouting.SegmentRoutingService; |
| 25 | import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelDescription; |
| 26 | import org.onosproject.segmentrouting.pwaas.L2TunnelPolicy; |
| 27 | import org.onosproject.segmentrouting.pwaas.L2Tunnel; |
| 28 | import org.onosproject.segmentrouting.pwaas.L2TunnelHandler; |
| 29 | import org.slf4j.Logger; |
| 30 | import org.slf4j.LoggerFactory; |
| 31 | |
| 32 | import javax.ws.rs.Consumes; |
| 33 | import javax.ws.rs.DELETE; |
| 34 | import javax.ws.rs.GET; |
| 35 | import javax.ws.rs.POST; |
| 36 | import javax.ws.rs.Path; |
| 37 | import javax.ws.rs.Produces; |
| 38 | import javax.ws.rs.core.MediaType; |
| 39 | import javax.ws.rs.core.Response; |
| 40 | import java.io.IOException; |
| 41 | import java.io.InputStream; |
Andreas Pantelopoulos | ff691b7 | 2018-03-12 16:30:20 -0700 | [diff] [blame] | 42 | import java.util.ArrayList; |
Andreas Pantelopoulos | cd33959 | 2018-02-23 14:18:00 -0800 | [diff] [blame] | 43 | import java.util.List; |
| 44 | import java.util.stream.Collectors; |
| 45 | |
Andreas Pantelopoulos | ff691b7 | 2018-03-12 16:30:20 -0700 | [diff] [blame] | 46 | import static org.onlab.util.Tools.nullIsIllegal; |
Ray Milkey | 86ee5e8 | 2018-04-02 15:33:07 -0700 | [diff] [blame^] | 47 | import static org.onlab.util.Tools.readTreeFromStream; |
Andreas Pantelopoulos | ff691b7 | 2018-03-12 16:30:20 -0700 | [diff] [blame] | 48 | |
Andreas Pantelopoulos | cd33959 | 2018-02-23 14:18:00 -0800 | [diff] [blame] | 49 | /** |
| 50 | * Query, create and remove pseudowires. |
| 51 | */ |
| 52 | @Path("pseudowire") |
| 53 | public class PseudowireWebResource extends AbstractWebResource { |
| 54 | |
| 55 | private static final PseudowireCodec PSEUDOWIRE_CODEC = new PseudowireCodec(); |
Andreas Pantelopoulos | ff691b7 | 2018-03-12 16:30:20 -0700 | [diff] [blame] | 56 | public static final String PWS = "pseudowires"; |
| 57 | private static final String PWS_KEY_ERROR = "Pseudowires key must be present."; |
Andreas Pantelopoulos | cd33959 | 2018-02-23 14:18:00 -0800 | [diff] [blame] | 58 | |
| 59 | private static Logger log = LoggerFactory |
| 60 | .getLogger(PseudowireWebResource.class); |
| 61 | |
| 62 | /** |
| 63 | * Get all pseudowires. |
| 64 | * Returns an array of pseudowires. |
| 65 | * |
| 66 | * @return status of OK |
| 67 | */ |
| 68 | @GET |
| 69 | @Produces(MediaType.APPLICATION_JSON) |
| 70 | public Response getPseudowire() { |
| 71 | SegmentRoutingService srService = get(SegmentRoutingService.class); |
| 72 | |
| 73 | log.debug("Fetching pseudowires form rest api!"); |
| 74 | |
| 75 | List<L2TunnelPolicy> policies = srService.getL2Policies(); |
| 76 | List<L2Tunnel> tunnels = srService.getL2Tunnels(); |
| 77 | List<DefaultL2TunnelDescription> pseudowires = tunnels.stream() |
| 78 | .map(l2Tunnel -> { |
| 79 | L2TunnelPolicy policy = null; |
| 80 | for (L2TunnelPolicy l2Policy : policies) { |
| 81 | if (l2Policy.tunnelId() == l2Tunnel.tunnelId()) { |
| 82 | policy = l2Policy; |
| 83 | break; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | // return a copy |
| 88 | return new DefaultL2TunnelDescription(l2Tunnel, policy); |
| 89 | }) |
| 90 | .collect(Collectors.toList()); |
| 91 | |
| 92 | ObjectNode result = new ObjectMapper().createObjectNode(); |
| 93 | result.set("pseudowires", new PseudowireCodec().encode(pseudowires, this)); |
| 94 | |
| 95 | return ok(result.toString()).build(); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Create a new pseudowire. |
| 100 | * |
| 101 | * @param input JSON stream for pseudowire to create |
| 102 | * @return Response with appropriate status |
| 103 | * @throws IOException Throws IO exception. |
| 104 | * @onos.rsModel PseudowireCreate |
| 105 | */ |
| 106 | @POST |
| 107 | @Consumes(MediaType.APPLICATION_JSON) |
| 108 | public Response createPseudowire(InputStream input) throws IOException { |
| 109 | |
| 110 | ObjectMapper mapper = new ObjectMapper(); |
Ray Milkey | 86ee5e8 | 2018-04-02 15:33:07 -0700 | [diff] [blame^] | 111 | ObjectNode pseudowireJson = readTreeFromStream(mapper, input); |
Andreas Pantelopoulos | cd33959 | 2018-02-23 14:18:00 -0800 | [diff] [blame] | 112 | SegmentRoutingService srService = get(SegmentRoutingService.class); |
| 113 | |
| 114 | DefaultL2TunnelDescription pseudowire = PSEUDOWIRE_CODEC.decode(pseudowireJson, this); |
| 115 | if (pseudowire == null) { |
| 116 | return Response.serverError().status(Response.Status.BAD_REQUEST).build(); |
| 117 | } |
| 118 | |
Andreas Pantelopoulos | ff691b7 | 2018-03-12 16:30:20 -0700 | [diff] [blame] | 119 | log.debug("Creating pseudowire {} from rest api!", pseudowire.l2Tunnel().tunnelId()); |
Andreas Pantelopoulos | cd33959 | 2018-02-23 14:18:00 -0800 | [diff] [blame] | 120 | |
| 121 | L2TunnelHandler.Result res = srService.addPseudowire(pseudowire); |
| 122 | switch (res) { |
| 123 | case ADDITION_ERROR: |
| 124 | log.error("Pseudowire {} could not be added, error in configuration," + |
| 125 | " please check logs for more details!", |
| 126 | pseudowire.l2Tunnel().tunnelId()); |
| 127 | return Response.serverError().status(Response.Status.INTERNAL_SERVER_ERROR).build(); |
| 128 | |
| 129 | case SUCCESS: |
Andreas Pantelopoulos | ff691b7 | 2018-03-12 16:30:20 -0700 | [diff] [blame] | 130 | log.debug("Pseudowire {} succesfully deployed!", pseudowire.l2Tunnel().tunnelId()); |
| 131 | return Response.ok().build(); |
| 132 | default: |
| 133 | return Response.ok().build(); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Create a bulk of pseudowires. |
| 139 | * |
| 140 | * @param input JSON stream for pseudowires to create |
| 141 | * @return Response with appropriate status |
| 142 | * @throws IOException Throws IO exception. |
| 143 | * @onos.rsModel PseudowireCreateBulk |
| 144 | */ |
| 145 | @POST |
| 146 | @Path("/bulk") |
| 147 | @Consumes(MediaType.APPLICATION_JSON) |
| 148 | public Response createPseudowiresBulk(InputStream input) throws IOException { |
| 149 | |
| 150 | ObjectMapper mapper = new ObjectMapper(); |
Ray Milkey | 86ee5e8 | 2018-04-02 15:33:07 -0700 | [diff] [blame^] | 151 | ObjectNode pseudowireJson = readTreeFromStream(mapper, input); |
Andreas Pantelopoulos | ff691b7 | 2018-03-12 16:30:20 -0700 | [diff] [blame] | 152 | SegmentRoutingService srService = get(SegmentRoutingService.class); |
| 153 | List<DefaultL2TunnelDescription> pseudowires; |
| 154 | |
| 155 | try { |
| 156 | ArrayNode pseudowiresArray = nullIsIllegal((ArrayNode) pseudowireJson.get(PWS), PWS_KEY_ERROR); |
| 157 | pseudowires = PSEUDOWIRE_CODEC.decode(pseudowiresArray, this); |
| 158 | } catch (ItemNotFoundException e) { |
| 159 | return Response.serverError().status(Response.Status.BAD_REQUEST).build(); |
| 160 | } |
| 161 | |
| 162 | log.debug("Creating pseudowires {} from rest api!", pseudowires); |
| 163 | |
| 164 | L2TunnelHandler.Result res = srService.addPseudowiresBulk(pseudowires); |
| 165 | switch (res) { |
| 166 | case ADDITION_ERROR: |
| 167 | log.error("Bulk of pseudowires {} could not be added, error in configuration," + |
| 168 | " please check logs for more details!", |
| 169 | pseudowires); |
| 170 | return Response.serverError().status(Response.Status.INTERNAL_SERVER_ERROR).build(); |
| 171 | |
| 172 | case SUCCESS: |
| 173 | log.debug("Bulk of pseudowires {} succesfully deployed!", pseudowires); |
Andreas Pantelopoulos | cd33959 | 2018-02-23 14:18:00 -0800 | [diff] [blame] | 174 | return Response.ok().build(); |
| 175 | default: |
| 176 | return Response.ok().build(); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Delete a pseudowire. |
| 182 | * |
| 183 | * @param input JSON stream for pseudowire to delete |
| 184 | * @return Response with appropriate status |
| 185 | * @throws IOException Throws IO exception. |
| 186 | * @onos.rsModel PseudowireDelete |
| 187 | */ |
| 188 | @DELETE |
| 189 | @Consumes(MediaType.APPLICATION_JSON) |
| 190 | public Response removePseudowire(InputStream input) throws IOException { |
| 191 | |
| 192 | ObjectMapper mapper = new ObjectMapper(); |
Ray Milkey | 86ee5e8 | 2018-04-02 15:33:07 -0700 | [diff] [blame^] | 193 | ObjectNode pseudowireJson = readTreeFromStream(mapper, input); |
Andreas Pantelopoulos | cd33959 | 2018-02-23 14:18:00 -0800 | [diff] [blame] | 194 | SegmentRoutingService srService = get(SegmentRoutingService.class); |
| 195 | |
| 196 | Integer pseudowireId = PSEUDOWIRE_CODEC.decodeId(pseudowireJson); |
| 197 | if (pseudowireId == null) { |
| 198 | return Response.serverError().status(Response.Status.BAD_REQUEST).build(); |
| 199 | } |
| 200 | |
Andreas Pantelopoulos | ff691b7 | 2018-03-12 16:30:20 -0700 | [diff] [blame] | 201 | log.debug("Deleting pseudowire {} from rest api!", pseudowireId); |
Andreas Pantelopoulos | cd33959 | 2018-02-23 14:18:00 -0800 | [diff] [blame] | 202 | |
| 203 | L2TunnelHandler.Result res = srService.removePseudowire(pseudowireId); |
| 204 | switch (res) { |
| 205 | case REMOVAL_ERROR: |
| 206 | log.error("Pseudowire {} could not be removed, error in configuration," + |
| 207 | " please check logs for more details!", |
| 208 | pseudowireId); |
| 209 | |
| 210 | return Response.noContent().build(); |
| 211 | case SUCCESS: |
Andreas Pantelopoulos | ff691b7 | 2018-03-12 16:30:20 -0700 | [diff] [blame] | 212 | log.debug("Pseudowire {} was removed succesfully!", pseudowireId); |
Andreas Pantelopoulos | cd33959 | 2018-02-23 14:18:00 -0800 | [diff] [blame] | 213 | return Response.noContent().build(); |
| 214 | default: |
| 215 | return Response.noContent().build(); |
| 216 | } |
| 217 | } |
Andreas Pantelopoulos | ff691b7 | 2018-03-12 16:30:20 -0700 | [diff] [blame] | 218 | |
| 219 | /** |
| 220 | * Delete a bulk of pseudowires. |
| 221 | * |
| 222 | * @param input JSON stream for pseudowires to delete |
| 223 | * @return Response with appropriate status |
| 224 | * @throws IOException Throws IO exception. |
| 225 | * @onos.rsModel PseudowireDeleteBulk |
| 226 | */ |
| 227 | @DELETE |
| 228 | @Path("/bulk") |
| 229 | @Consumes(MediaType.APPLICATION_JSON) |
| 230 | public Response removePseudowiresBulk(InputStream input) throws IOException { |
| 231 | |
| 232 | ObjectMapper mapper = new ObjectMapper(); |
Ray Milkey | 86ee5e8 | 2018-04-02 15:33:07 -0700 | [diff] [blame^] | 233 | ObjectNode pseudowireJson = readTreeFromStream(mapper, input); |
Andreas Pantelopoulos | ff691b7 | 2018-03-12 16:30:20 -0700 | [diff] [blame] | 234 | SegmentRoutingService srService = get(SegmentRoutingService.class); |
| 235 | |
| 236 | List<Integer> ids = new ArrayList<>(); |
| 237 | |
| 238 | // try to parse all ids, if key is not present, or if an id is not an int |
| 239 | // throw an exception and stop process |
| 240 | try { |
| 241 | for (JsonNode node : pseudowireJson.withArray(PWS)) { |
| 242 | Integer idToDelete = PseudowireCodec.decodeId((ObjectNode) node); |
| 243 | if (idToDelete == null) { |
| 244 | log.error("Error when parsing pseudowire for deletion in REST API."); |
| 245 | throw new IllegalArgumentException("Id of pseudowire should be an integer!"); |
| 246 | } |
| 247 | ids.add(idToDelete); |
| 248 | } |
| 249 | } catch (IllegalArgumentException e) { |
| 250 | log.error("Pseudowire ID should be an integer."); |
| 251 | return Response.serverError().status(Response.Status.BAD_REQUEST).build(); |
| 252 | } catch (UnsupportedOperationException e) { |
| 253 | log.error("Pseudowires for deletion should be an array of pseudowire ids."); |
| 254 | return Response.serverError().status(Response.Status.BAD_REQUEST).build(); |
| 255 | } |
| 256 | |
| 257 | for (Integer pseudowireId : ids) { |
| 258 | L2TunnelHandler.Result res = srService.removePseudowire(pseudowireId); |
| 259 | switch (res) { |
| 260 | case REMOVAL_ERROR: |
| 261 | log.error("Pseudowire {} could not be removed, error in configuration," + |
| 262 | " please check logs for more details!", |
| 263 | pseudowireId); |
Ray Milkey | b994f8d | 2018-03-14 09:55:13 -0700 | [diff] [blame] | 264 | break; |
Andreas Pantelopoulos | ff691b7 | 2018-03-12 16:30:20 -0700 | [diff] [blame] | 265 | case SUCCESS: |
| 266 | log.debug("Pseudowire {} was removed succesfully!", pseudowireId); |
Ray Milkey | b994f8d | 2018-03-14 09:55:13 -0700 | [diff] [blame] | 267 | break; |
Andreas Pantelopoulos | ff691b7 | 2018-03-12 16:30:20 -0700 | [diff] [blame] | 268 | default: |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | return Response.noContent().build(); |
| 273 | } |
Andreas Pantelopoulos | cd33959 | 2018-02-23 14:18:00 -0800 | [diff] [blame] | 274 | } |