Jonathan Hart | 07eb041 | 2016-02-08 16:42:29 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Jonathan Hart | 07eb041 | 2016-02-08 16:42:29 -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 | */ |
| 16 | |
| 17 | package org.onosproject.rest.resources; |
| 18 | |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Jonathan Hart | 6921ceb | 2016-05-23 10:39:14 -0700 | [diff] [blame] | 20 | import com.google.common.annotations.Beta; |
| 21 | import org.onlab.packet.IpAddress; |
| 22 | import org.onosproject.net.ConnectPoint; |
Jonathan Hart | 07eb041 | 2016-02-08 16:42:29 -0800 | [diff] [blame] | 23 | import org.onosproject.net.mcast.McastRoute; |
| 24 | import org.onosproject.net.mcast.MulticastRouteService; |
| 25 | import org.onosproject.rest.AbstractWebResource; |
| 26 | |
| 27 | import javax.ws.rs.Consumes; |
| 28 | import javax.ws.rs.DELETE; |
| 29 | import javax.ws.rs.GET; |
| 30 | import javax.ws.rs.POST; |
| 31 | import javax.ws.rs.Path; |
Jonathan Hart | 6921ceb | 2016-05-23 10:39:14 -0700 | [diff] [blame] | 32 | import javax.ws.rs.PathParam; |
Jonathan Hart | 07eb041 | 2016-02-08 16:42:29 -0800 | [diff] [blame] | 33 | import javax.ws.rs.Produces; |
| 34 | import javax.ws.rs.core.MediaType; |
| 35 | import javax.ws.rs.core.Response; |
| 36 | import java.io.IOException; |
| 37 | import java.io.InputStream; |
| 38 | import java.net.URI; |
| 39 | import java.util.Set; |
| 40 | |
| 41 | /** |
| 42 | * Manage the multicast routing information. |
| 43 | */ |
Jonathan Hart | 6921ceb | 2016-05-23 10:39:14 -0700 | [diff] [blame] | 44 | @Beta |
Jonathan Hart | 07eb041 | 2016-02-08 16:42:29 -0800 | [diff] [blame] | 45 | @Path("mcast") |
| 46 | public class MulticastRouteWebResource extends AbstractWebResource { |
| 47 | |
| 48 | /** |
| 49 | * Get all multicast routes. |
| 50 | * Returns array of all known multicast routes. |
| 51 | * |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 52 | * @return 200 OK with array of all known multicast routes |
Jonathan Hart | 07eb041 | 2016-02-08 16:42:29 -0800 | [diff] [blame] | 53 | * @onos.rsModel McastRoutesGet |
| 54 | */ |
| 55 | @GET |
| 56 | @Produces(MediaType.APPLICATION_JSON) |
| 57 | public Response getRoutes() { |
| 58 | Set<McastRoute> routes = get(MulticastRouteService.class).getRoutes(); |
| 59 | ObjectNode root = encodeArray(McastRoute.class, "routes", routes); |
| 60 | return ok(root).build(); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Create new multicast route. |
| 65 | * Creates a new route in the multicast RIB. |
| 66 | * |
| 67 | * @onos.rsModel McastRoutePost |
| 68 | * @param stream multicast route JSON |
| 69 | * @return status of the request - CREATED if the JSON is correct, |
| 70 | * BAD_REQUEST if the JSON is invalid |
| 71 | */ |
| 72 | @POST |
| 73 | @Consumes(MediaType.APPLICATION_JSON) |
| 74 | @Produces(MediaType.APPLICATION_JSON) |
| 75 | public Response createRoute(InputStream stream) { |
| 76 | MulticastRouteService service = get(MulticastRouteService.class); |
| 77 | try { |
| 78 | ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); |
| 79 | McastRoute route = codec(McastRoute.class).decode(jsonTree, this); |
| 80 | service.add(route); |
| 81 | } catch (IOException ex) { |
| 82 | throw new IllegalArgumentException(ex); |
| 83 | } |
| 84 | |
| 85 | return Response |
| 86 | .created(URI.create("")) |
| 87 | .build(); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Remove a multicast route. |
| 92 | * Removes a route from the multicast RIB. |
| 93 | * |
| 94 | * @param stream multicast route JSON |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 95 | * @return 204 NO CONTENT |
Jonathan Hart | 07eb041 | 2016-02-08 16:42:29 -0800 | [diff] [blame] | 96 | * @onos.rsModel McastRoutePost |
| 97 | */ |
| 98 | @DELETE |
| 99 | @Consumes(MediaType.APPLICATION_JSON) |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 100 | public Response deleteRoute(InputStream stream) { |
Jonathan Hart | 07eb041 | 2016-02-08 16:42:29 -0800 | [diff] [blame] | 101 | MulticastRouteService service = get(MulticastRouteService.class); |
| 102 | try { |
| 103 | ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); |
| 104 | McastRoute route = codec(McastRoute.class).decode(jsonTree, this); |
| 105 | service.remove(route); |
| 106 | } catch (IOException ex) { |
| 107 | throw new IllegalArgumentException(ex); |
| 108 | } |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 109 | return Response.noContent().build(); |
Jonathan Hart | 07eb041 | 2016-02-08 16:42:29 -0800 | [diff] [blame] | 110 | } |
Jonathan Hart | 6921ceb | 2016-05-23 10:39:14 -0700 | [diff] [blame] | 111 | |
| 112 | /** |
| 113 | * Create a sink for a multicast route. |
| 114 | * Creates a new sink for an existing multicast route. |
| 115 | * |
| 116 | * @onos.rsModel McastSinkPost |
| 117 | * @param group group IP address |
| 118 | * @param source source IP address |
| 119 | * @param stream sink JSON |
| 120 | * @return status of the request - CREATED if the JSON is correct, |
| 121 | * BAD_REQUEST if the JSON is invalid |
| 122 | */ |
| 123 | @POST |
| 124 | @Consumes(MediaType.APPLICATION_JSON) |
| 125 | @Produces(MediaType.APPLICATION_JSON) |
| 126 | @Path("sinks/{group}/{source}") |
| 127 | public Response addSinks(@PathParam("group") String group, |
| 128 | @PathParam("source") String source, |
| 129 | InputStream stream) { |
| 130 | MulticastRouteService service = get(MulticastRouteService.class); |
| 131 | try { |
| 132 | McastRoute route = new McastRoute(IpAddress.valueOf(source), IpAddress.valueOf(group), |
| 133 | McastRoute.Type.STATIC); |
| 134 | ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); |
| 135 | jsonTree.path("sinks").forEach(node -> { |
| 136 | ConnectPoint sink = ConnectPoint.deviceConnectPoint(node.asText()); |
| 137 | service.addSink(route, sink); |
| 138 | }); |
| 139 | } catch (IOException ex) { |
| 140 | throw new IllegalArgumentException(ex); |
| 141 | } |
| 142 | |
| 143 | return Response |
| 144 | .created(URI.create("")) |
| 145 | .build(); |
| 146 | } |
Jonathan Hart | 07eb041 | 2016-02-08 16:42:29 -0800 | [diff] [blame] | 147 | } |