blob: cdfd76ee588c465b238d9c2a41965a50016de602 [file] [log] [blame]
Jonathan Hart07eb0412016-02-08 16:42:29 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Jonathan Hart07eb0412016-02-08 16:42:29 -08003 *
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
17package org.onosproject.rest.resources;
18
19import com.fasterxml.jackson.databind.node.ObjectNode;
Jonathan Hart6921ceb2016-05-23 10:39:14 -070020import com.google.common.annotations.Beta;
21import org.onlab.packet.IpAddress;
22import org.onosproject.net.ConnectPoint;
Jonathan Hart07eb0412016-02-08 16:42:29 -080023import org.onosproject.net.mcast.McastRoute;
24import org.onosproject.net.mcast.MulticastRouteService;
25import org.onosproject.rest.AbstractWebResource;
26
Kalhee Kim4b2dc722017-10-05 11:56:14 +000027import static org.onlab.util.Tools.nullIsNotFound;
Ray Milkey86ee5e82018-04-02 15:33:07 -070028import static org.onlab.util.Tools.readTreeFromStream;
Kalhee Kim4b2dc722017-10-05 11:56:14 +000029
Jonathan Hart07eb0412016-02-08 16:42:29 -080030import javax.ws.rs.Consumes;
31import javax.ws.rs.DELETE;
32import javax.ws.rs.GET;
33import javax.ws.rs.POST;
34import javax.ws.rs.Path;
Jonathan Hart6921ceb2016-05-23 10:39:14 -070035import javax.ws.rs.PathParam;
Jonathan Hart07eb0412016-02-08 16:42:29 -080036import javax.ws.rs.Produces;
37import javax.ws.rs.core.MediaType;
38import javax.ws.rs.core.Response;
39import java.io.IOException;
40import java.io.InputStream;
41import java.net.URI;
42import java.util.Set;
43
44/**
45 * Manage the multicast routing information.
46 */
Jonathan Hart6921ceb2016-05-23 10:39:14 -070047@Beta
Jonathan Hart07eb0412016-02-08 16:42:29 -080048@Path("mcast")
49public class MulticastRouteWebResource extends AbstractWebResource {
50
51 /**
52 * Get all multicast routes.
53 * Returns array of all known multicast routes.
54 *
Jian Licc730a62016-05-10 16:36:16 -070055 * @return 200 OK with array of all known multicast routes
Jonathan Hart07eb0412016-02-08 16:42:29 -080056 * @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 Kim4b2dc722017-10-05 11:56:14 +000079
80 final String ingressStr = "ingress";
Jonathan Hart07eb0412016-02-08 16:42:29 -080081 MulticastRouteService service = get(MulticastRouteService.class);
82 try {
Ray Milkey86ee5e82018-04-02 15:33:07 -070083 ObjectNode jsonTree = readTreeFromStream(mapper(), stream);
Jonathan Hart07eb0412016-02-08 16:42:29 -080084 McastRoute route = codec(McastRoute.class).decode(jsonTree, this);
85 service.add(route);
Kalhee Kim4b2dc722017-10-05 11:56:14 +000086 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 Hart07eb0412016-02-08 16:42:29 -080092 } 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 Lic2a542b2016-05-10 11:48:19 -0700106 * @return 204 NO CONTENT
Jonathan Hart07eb0412016-02-08 16:42:29 -0800107 * @onos.rsModel McastRoutePost
108 */
109 @DELETE
110 @Consumes(MediaType.APPLICATION_JSON)
Jian Lic2a542b2016-05-10 11:48:19 -0700111 public Response deleteRoute(InputStream stream) {
Jonathan Hart07eb0412016-02-08 16:42:29 -0800112 MulticastRouteService service = get(MulticastRouteService.class);
113 try {
Ray Milkey86ee5e82018-04-02 15:33:07 -0700114 ObjectNode jsonTree = readTreeFromStream(mapper(), stream);
Jonathan Hart07eb0412016-02-08 16:42:29 -0800115 McastRoute route = codec(McastRoute.class).decode(jsonTree, this);
116 service.remove(route);
117 } catch (IOException ex) {
118 throw new IllegalArgumentException(ex);
119 }
Jian Lic2a542b2016-05-10 11:48:19 -0700120 return Response.noContent().build();
Jonathan Hart07eb0412016-02-08 16:42:29 -0800121 }
Jonathan Hart6921ceb2016-05-23 10:39:14 -0700122
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 Milkey86ee5e82018-04-02 15:33:07 -0700145 ObjectNode jsonTree = readTreeFromStream(mapper(), stream);
Kalhee Kim4b2dc722017-10-05 11:56:14 +0000146
Jonathan Hart6921ceb2016-05-23 10:39:14 -0700147 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 Hart07eb0412016-02-08 16:42:29 -0800159}