blob: 2a3cd8dadd8e53fa5c73c41062eefc6279a3c640 [file] [log] [blame]
Jonathan Hart07eb0412016-02-08 16:42:29 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
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;
20import org.onosproject.net.mcast.McastRoute;
21import org.onosproject.net.mcast.MulticastRouteService;
22import org.onosproject.rest.AbstractWebResource;
23
24import javax.ws.rs.Consumes;
25import javax.ws.rs.DELETE;
26import javax.ws.rs.GET;
27import javax.ws.rs.POST;
28import javax.ws.rs.Path;
29import javax.ws.rs.Produces;
30import javax.ws.rs.core.MediaType;
31import javax.ws.rs.core.Response;
32import java.io.IOException;
33import java.io.InputStream;
34import java.net.URI;
35import java.util.Set;
36
37/**
38 * Manage the multicast routing information.
39 */
40@Path("mcast")
41public class MulticastRouteWebResource extends AbstractWebResource {
42
43 /**
44 * Get all multicast routes.
45 * Returns array of all known multicast routes.
46 *
47 * @return 200 OK
48 * @onos.rsModel McastRoutesGet
49 */
50 @GET
51 @Produces(MediaType.APPLICATION_JSON)
52 public Response getRoutes() {
53 Set<McastRoute> routes = get(MulticastRouteService.class).getRoutes();
54 ObjectNode root = encodeArray(McastRoute.class, "routes", routes);
55 return ok(root).build();
56 }
57
58 /**
59 * Create new multicast route.
60 * Creates a new route in the multicast RIB.
61 *
62 * @onos.rsModel McastRoutePost
63 * @param stream multicast route JSON
64 * @return status of the request - CREATED if the JSON is correct,
65 * BAD_REQUEST if the JSON is invalid
66 */
67 @POST
68 @Consumes(MediaType.APPLICATION_JSON)
69 @Produces(MediaType.APPLICATION_JSON)
70 public Response createRoute(InputStream stream) {
71 MulticastRouteService service = get(MulticastRouteService.class);
72 try {
73 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
74 McastRoute route = codec(McastRoute.class).decode(jsonTree, this);
75 service.add(route);
76 } catch (IOException ex) {
77 throw new IllegalArgumentException(ex);
78 }
79
80 return Response
81 .created(URI.create(""))
82 .build();
83 }
84
85 /**
86 * Remove a multicast route.
87 * Removes a route from the multicast RIB.
88 *
89 * @param stream multicast route JSON
Jian Lic2a542b2016-05-10 11:48:19 -070090 * @return 204 NO CONTENT
Jonathan Hart07eb0412016-02-08 16:42:29 -080091 * @onos.rsModel McastRoutePost
92 */
93 @DELETE
94 @Consumes(MediaType.APPLICATION_JSON)
Jian Lic2a542b2016-05-10 11:48:19 -070095 public Response deleteRoute(InputStream stream) {
Jonathan Hart07eb0412016-02-08 16:42:29 -080096 MulticastRouteService service = get(MulticastRouteService.class);
97 try {
98 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
99 McastRoute route = codec(McastRoute.class).decode(jsonTree, this);
100 service.remove(route);
101 } catch (IOException ex) {
102 throw new IllegalArgumentException(ex);
103 }
Jian Lic2a542b2016-05-10 11:48:19 -0700104 return Response.noContent().build();
Jonathan Hart07eb0412016-02-08 16:42:29 -0800105 }
106}