Jonathan Hart | 07eb041 | 2016-02-08 16:42:29 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Open Networking Laboratory |
| 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 | package org.onosproject.rest.resources; |
| 18 | |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 20 | import org.onosproject.net.mcast.McastRoute; |
| 21 | import org.onosproject.net.mcast.MulticastRouteService; |
| 22 | import org.onosproject.rest.AbstractWebResource; |
| 23 | |
| 24 | import javax.ws.rs.Consumes; |
| 25 | import javax.ws.rs.DELETE; |
| 26 | import javax.ws.rs.GET; |
| 27 | import javax.ws.rs.POST; |
| 28 | import javax.ws.rs.Path; |
| 29 | import javax.ws.rs.Produces; |
| 30 | import javax.ws.rs.core.MediaType; |
| 31 | import javax.ws.rs.core.Response; |
| 32 | import java.io.IOException; |
| 33 | import java.io.InputStream; |
| 34 | import java.net.URI; |
| 35 | import java.util.Set; |
| 36 | |
| 37 | /** |
| 38 | * Manage the multicast routing information. |
| 39 | */ |
| 40 | @Path("mcast") |
| 41 | public class MulticastRouteWebResource extends AbstractWebResource { |
| 42 | |
| 43 | /** |
| 44 | * Get all multicast routes. |
| 45 | * Returns array of all known multicast routes. |
| 46 | * |
| 47 | * @return 200 OK |
| 48 | * @onos.rsModel McastRoutesGet |
| 49 | */ |
| 50 | @GET |
| 51 | @Produces(MediaType.APPLICATION_JSON) |
| 52 | public Response getRoutes() { |
| 53 | Set<McastRoute> routes = get(MulticastRouteService.class).getRoutes(); |
| 54 | ObjectNode root = encodeArray(McastRoute.class, "routes", routes); |
| 55 | return ok(root).build(); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Create new multicast route. |
| 60 | * Creates a new route in the multicast RIB. |
| 61 | * |
| 62 | * @onos.rsModel McastRoutePost |
| 63 | * @param stream multicast route JSON |
| 64 | * @return status of the request - CREATED if the JSON is correct, |
| 65 | * BAD_REQUEST if the JSON is invalid |
| 66 | */ |
| 67 | @POST |
| 68 | @Consumes(MediaType.APPLICATION_JSON) |
| 69 | @Produces(MediaType.APPLICATION_JSON) |
| 70 | public Response createRoute(InputStream stream) { |
| 71 | MulticastRouteService service = get(MulticastRouteService.class); |
| 72 | try { |
| 73 | ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); |
| 74 | McastRoute route = codec(McastRoute.class).decode(jsonTree, this); |
| 75 | service.add(route); |
| 76 | } catch (IOException ex) { |
| 77 | throw new IllegalArgumentException(ex); |
| 78 | } |
| 79 | |
| 80 | return Response |
| 81 | .created(URI.create("")) |
| 82 | .build(); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Remove a multicast route. |
| 87 | * Removes a route from the multicast RIB. |
| 88 | * |
| 89 | * @param stream multicast route JSON |
| 90 | * @onos.rsModel McastRoutePost |
| 91 | */ |
| 92 | @DELETE |
| 93 | @Consumes(MediaType.APPLICATION_JSON) |
| 94 | @Produces(MediaType.APPLICATION_JSON) |
| 95 | public void deleteRoute(InputStream stream) { |
| 96 | MulticastRouteService service = get(MulticastRouteService.class); |
| 97 | try { |
| 98 | ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); |
| 99 | McastRoute route = codec(McastRoute.class).decode(jsonTree, this); |
| 100 | service.remove(route); |
| 101 | } catch (IOException ex) { |
| 102 | throw new IllegalArgumentException(ex); |
| 103 | } |
| 104 | } |
| 105 | } |