blob: 212dbeb41e76508b451ccdf03eacbd2500715f68 [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
27import javax.ws.rs.Consumes;
28import javax.ws.rs.DELETE;
29import javax.ws.rs.GET;
30import javax.ws.rs.POST;
31import javax.ws.rs.Path;
Jonathan Hart6921ceb2016-05-23 10:39:14 -070032import javax.ws.rs.PathParam;
Jonathan Hart07eb0412016-02-08 16:42:29 -080033import javax.ws.rs.Produces;
34import javax.ws.rs.core.MediaType;
35import javax.ws.rs.core.Response;
36import java.io.IOException;
37import java.io.InputStream;
38import java.net.URI;
39import java.util.Set;
40
41/**
42 * Manage the multicast routing information.
43 */
Jonathan Hart6921ceb2016-05-23 10:39:14 -070044@Beta
Jonathan Hart07eb0412016-02-08 16:42:29 -080045@Path("mcast")
46public class MulticastRouteWebResource extends AbstractWebResource {
47
48 /**
49 * Get all multicast routes.
50 * Returns array of all known multicast routes.
51 *
Jian Licc730a62016-05-10 16:36:16 -070052 * @return 200 OK with array of all known multicast routes
Jonathan Hart07eb0412016-02-08 16:42:29 -080053 * @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 Lic2a542b2016-05-10 11:48:19 -070095 * @return 204 NO CONTENT
Jonathan Hart07eb0412016-02-08 16:42:29 -080096 * @onos.rsModel McastRoutePost
97 */
98 @DELETE
99 @Consumes(MediaType.APPLICATION_JSON)
Jian Lic2a542b2016-05-10 11:48:19 -0700100 public Response deleteRoute(InputStream stream) {
Jonathan Hart07eb0412016-02-08 16:42:29 -0800101 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 Lic2a542b2016-05-10 11:48:19 -0700109 return Response.noContent().build();
Jonathan Hart07eb0412016-02-08 16:42:29 -0800110 }
Jonathan Hart6921ceb2016-05-23 10:39:14 -0700111
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 Hart07eb0412016-02-08 16:42:29 -0800147}