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