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 | |
Kalhee Kim | 4b2dc72 | 2017-10-05 11:56:14 +0000 | [diff] [blame] | 27 | import static org.onlab.util.Tools.nullIsNotFound; |
Ray Milkey | 86ee5e8 | 2018-04-02 15:33:07 -0700 | [diff] [blame] | 28 | import static org.onlab.util.Tools.readTreeFromStream; |
Kalhee Kim | 4b2dc72 | 2017-10-05 11:56:14 +0000 | [diff] [blame] | 29 | |
Jonathan Hart | 07eb041 | 2016-02-08 16:42:29 -0800 | [diff] [blame] | 30 | import javax.ws.rs.Consumes; |
| 31 | import javax.ws.rs.DELETE; |
| 32 | import javax.ws.rs.GET; |
| 33 | import javax.ws.rs.POST; |
| 34 | import javax.ws.rs.Path; |
Jonathan Hart | 6921ceb | 2016-05-23 10:39:14 -0700 | [diff] [blame] | 35 | import javax.ws.rs.PathParam; |
Jonathan Hart | 07eb041 | 2016-02-08 16:42:29 -0800 | [diff] [blame] | 36 | import javax.ws.rs.Produces; |
| 37 | import javax.ws.rs.core.MediaType; |
| 38 | import javax.ws.rs.core.Response; |
| 39 | import java.io.IOException; |
| 40 | import java.io.InputStream; |
| 41 | import java.net.URI; |
| 42 | import java.util.Set; |
| 43 | |
| 44 | /** |
| 45 | * Manage the multicast routing information. |
| 46 | */ |
Jonathan Hart | 6921ceb | 2016-05-23 10:39:14 -0700 | [diff] [blame] | 47 | @Beta |
Jonathan Hart | 07eb041 | 2016-02-08 16:42:29 -0800 | [diff] [blame] | 48 | @Path("mcast") |
| 49 | public class MulticastRouteWebResource extends AbstractWebResource { |
| 50 | |
| 51 | /** |
| 52 | * Get all multicast routes. |
| 53 | * Returns array of all known multicast routes. |
| 54 | * |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 55 | * @return 200 OK with array of all known multicast routes |
Jonathan Hart | 07eb041 | 2016-02-08 16:42:29 -0800 | [diff] [blame] | 56 | * @onos.rsModel McastRoutesGet |
| 57 | */ |
| 58 | @GET |
| 59 | @Produces(MediaType.APPLICATION_JSON) |
| 60 | public Response getRoutes() { |
| 61 | Set<McastRoute> routes = get(MulticastRouteService.class).getRoutes(); |
| 62 | ObjectNode root = encodeArray(McastRoute.class, "routes", routes); |
| 63 | return ok(root).build(); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Create new multicast route. |
| 68 | * Creates a new route in the multicast RIB. |
| 69 | * |
| 70 | * @onos.rsModel McastRoutePost |
| 71 | * @param stream multicast route JSON |
| 72 | * @return status of the request - CREATED if the JSON is correct, |
| 73 | * BAD_REQUEST if the JSON is invalid |
| 74 | */ |
| 75 | @POST |
| 76 | @Consumes(MediaType.APPLICATION_JSON) |
| 77 | @Produces(MediaType.APPLICATION_JSON) |
| 78 | public Response createRoute(InputStream stream) { |
Kalhee Kim | 4b2dc72 | 2017-10-05 11:56:14 +0000 | [diff] [blame] | 79 | |
| 80 | final String ingressStr = "ingress"; |
Jonathan Hart | 07eb041 | 2016-02-08 16:42:29 -0800 | [diff] [blame] | 81 | MulticastRouteService service = get(MulticastRouteService.class); |
| 82 | try { |
Ray Milkey | 86ee5e8 | 2018-04-02 15:33:07 -0700 | [diff] [blame] | 83 | ObjectNode jsonTree = readTreeFromStream(mapper(), stream); |
Jonathan Hart | 07eb041 | 2016-02-08 16:42:29 -0800 | [diff] [blame] | 84 | McastRoute route = codec(McastRoute.class).decode(jsonTree, this); |
| 85 | service.add(route); |
Kalhee Kim | 4b2dc72 | 2017-10-05 11:56:14 +0000 | [diff] [blame] | 86 | if (jsonTree.has(ingressStr)) { |
| 87 | String ingressPathStr = jsonTree.path(ingressStr).asText(); |
| 88 | ConnectPoint ingressConnectPoint = nullIsNotFound(ConnectPoint.deviceConnectPoint(ingressPathStr), |
| 89 | "ingress connection point cannot be null!"); |
| 90 | service.addSource(route, ingressConnectPoint); |
| 91 | } |
Jonathan Hart | 07eb041 | 2016-02-08 16:42:29 -0800 | [diff] [blame] | 92 | } catch (IOException ex) { |
| 93 | throw new IllegalArgumentException(ex); |
| 94 | } |
| 95 | |
| 96 | return Response |
| 97 | .created(URI.create("")) |
| 98 | .build(); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Remove a multicast route. |
| 103 | * Removes a route from the multicast RIB. |
| 104 | * |
| 105 | * @param stream multicast route JSON |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 106 | * @return 204 NO CONTENT |
Jonathan Hart | 07eb041 | 2016-02-08 16:42:29 -0800 | [diff] [blame] | 107 | * @onos.rsModel McastRoutePost |
| 108 | */ |
| 109 | @DELETE |
| 110 | @Consumes(MediaType.APPLICATION_JSON) |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 111 | public Response deleteRoute(InputStream stream) { |
Jonathan Hart | 07eb041 | 2016-02-08 16:42:29 -0800 | [diff] [blame] | 112 | MulticastRouteService service = get(MulticastRouteService.class); |
| 113 | try { |
Ray Milkey | 86ee5e8 | 2018-04-02 15:33:07 -0700 | [diff] [blame] | 114 | ObjectNode jsonTree = readTreeFromStream(mapper(), stream); |
Jonathan Hart | 07eb041 | 2016-02-08 16:42:29 -0800 | [diff] [blame] | 115 | McastRoute route = codec(McastRoute.class).decode(jsonTree, this); |
| 116 | service.remove(route); |
| 117 | } catch (IOException ex) { |
| 118 | throw new IllegalArgumentException(ex); |
| 119 | } |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 120 | return Response.noContent().build(); |
Jonathan Hart | 07eb041 | 2016-02-08 16:42:29 -0800 | [diff] [blame] | 121 | } |
Jonathan Hart | 6921ceb | 2016-05-23 10:39:14 -0700 | [diff] [blame] | 122 | |
| 123 | /** |
| 124 | * Create a sink for a multicast route. |
| 125 | * Creates a new sink for an existing multicast route. |
| 126 | * |
| 127 | * @onos.rsModel McastSinkPost |
| 128 | * @param group group IP address |
| 129 | * @param source source IP address |
| 130 | * @param stream sink JSON |
| 131 | * @return status of the request - CREATED if the JSON is correct, |
| 132 | * BAD_REQUEST if the JSON is invalid |
| 133 | */ |
| 134 | @POST |
| 135 | @Consumes(MediaType.APPLICATION_JSON) |
| 136 | @Produces(MediaType.APPLICATION_JSON) |
| 137 | @Path("sinks/{group}/{source}") |
| 138 | public Response addSinks(@PathParam("group") String group, |
| 139 | @PathParam("source") String source, |
| 140 | InputStream stream) { |
| 141 | MulticastRouteService service = get(MulticastRouteService.class); |
| 142 | try { |
| 143 | McastRoute route = new McastRoute(IpAddress.valueOf(source), IpAddress.valueOf(group), |
| 144 | McastRoute.Type.STATIC); |
Ray Milkey | 86ee5e8 | 2018-04-02 15:33:07 -0700 | [diff] [blame] | 145 | ObjectNode jsonTree = readTreeFromStream(mapper(), stream); |
Kalhee Kim | 4b2dc72 | 2017-10-05 11:56:14 +0000 | [diff] [blame] | 146 | |
Jonathan Hart | 6921ceb | 2016-05-23 10:39:14 -0700 | [diff] [blame] | 147 | jsonTree.path("sinks").forEach(node -> { |
| 148 | ConnectPoint sink = ConnectPoint.deviceConnectPoint(node.asText()); |
| 149 | service.addSink(route, sink); |
| 150 | }); |
| 151 | } catch (IOException ex) { |
| 152 | throw new IllegalArgumentException(ex); |
| 153 | } |
| 154 | |
| 155 | return Response |
| 156 | .created(URI.create("")) |
| 157 | .build(); |
| 158 | } |
Jonathan Hart | 07eb041 | 2016-02-08 16:42:29 -0800 | [diff] [blame] | 159 | } |