blob: 374605b6341ebc66834310e8283e36938b138114 [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 Kima3fb8e82017-10-05 11:56:14 +000027import static org.onlab.util.Tools.nullIsNotFound;
28
Jonathan Hart07eb0412016-02-08 16:42:29 -080029import javax.ws.rs.Consumes;
30import javax.ws.rs.DELETE;
31import javax.ws.rs.GET;
32import javax.ws.rs.POST;
33import javax.ws.rs.Path;
Jonathan Hart6921ceb2016-05-23 10:39:14 -070034import javax.ws.rs.PathParam;
Jonathan Hart07eb0412016-02-08 16:42:29 -080035import javax.ws.rs.Produces;
36import javax.ws.rs.core.MediaType;
37import javax.ws.rs.core.Response;
38import java.io.IOException;
39import java.io.InputStream;
40import java.net.URI;
41import java.util.Set;
42
43/**
44 * Manage the multicast routing information.
45 */
Jonathan Hart6921ceb2016-05-23 10:39:14 -070046@Beta
Jonathan Hart07eb0412016-02-08 16:42:29 -080047@Path("mcast")
48public class MulticastRouteWebResource extends AbstractWebResource {
49
50 /**
51 * Get all multicast routes.
52 * Returns array of all known multicast routes.
53 *
Jian Licc730a62016-05-10 16:36:16 -070054 * @return 200 OK with array of all known multicast routes
Jonathan Hart07eb0412016-02-08 16:42:29 -080055 * @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 Kima3fb8e82017-10-05 11:56:14 +000078
79 final String ingressStr = "ingress";
Jonathan Hart07eb0412016-02-08 16:42:29 -080080 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 Kima3fb8e82017-10-05 11:56:14 +000085 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 Hart07eb0412016-02-08 16:42:29 -080091 } 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 Lic2a542b2016-05-10 11:48:19 -0700105 * @return 204 NO CONTENT
Jonathan Hart07eb0412016-02-08 16:42:29 -0800106 * @onos.rsModel McastRoutePost
107 */
108 @DELETE
109 @Consumes(MediaType.APPLICATION_JSON)
Jian Lic2a542b2016-05-10 11:48:19 -0700110 public Response deleteRoute(InputStream stream) {
Jonathan Hart07eb0412016-02-08 16:42:29 -0800111 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 Lic2a542b2016-05-10 11:48:19 -0700119 return Response.noContent().build();
Jonathan Hart07eb0412016-02-08 16:42:29 -0800120 }
Jonathan Hart6921ceb2016-05-23 10:39:14 -0700121
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 Kima3fb8e82017-10-05 11:56:14 +0000145
Jonathan Hart6921ceb2016-05-23 10:39:14 -0700146 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 Hart07eb0412016-02-08 16:42:29 -0800158}