blob: 608e0447fa245162bfe4f1e2dd96e0ea276a91a4 [file] [log] [blame]
alshabibeff00542015-09-23 13:22:33 -07001/*
2 * Copyright 2015 Open Networking Laboratory
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 */
16package org.onosproject.mfwd.rest;
17
alshabibeff00542015-09-23 13:22:33 -070018import java.io.IOException;
Rusty Eddyc0e3fea2015-09-28 21:20:41 +000019import com.fasterxml.jackson.databind.ObjectMapper;
20import com.fasterxml.jackson.databind.node.ObjectNode;
alshabibeff00542015-09-23 13:22:33 -070021
Rusty Eddyc0e3fea2015-09-28 21:20:41 +000022import javax.ws.rs.DefaultValue;
23import javax.ws.rs.QueryParam;
alshabibeff00542015-09-23 13:22:33 -070024import javax.ws.rs.Consumes;
25import javax.ws.rs.GET;
26import javax.ws.rs.POST;
Rusty Eddyc0e3fea2015-09-28 21:20:41 +000027import javax.ws.rs.DELETE;
alshabibeff00542015-09-23 13:22:33 -070028import javax.ws.rs.Path;
29import javax.ws.rs.Produces;
30import javax.ws.rs.core.MediaType;
31import javax.ws.rs.core.Response;
32
Rusty Eddyc0e3fea2015-09-28 21:20:41 +000033import org.onosproject.mfwd.impl.McastConnectPoint;
alshabibeff00542015-09-23 13:22:33 -070034import org.onosproject.mfwd.impl.McastRouteTable;
Rusty Eddyc0e3fea2015-09-28 21:20:41 +000035import org.onosproject.mfwd.impl.McastRouteBase;
36import org.onosproject.mfwd.impl.MRibCodec;
37import org.onosproject.rest.AbstractWebResource;
38
39import org.slf4j.Logger;
40import static org.slf4j.LoggerFactory.getLogger;
alshabibeff00542015-09-23 13:22:33 -070041
42/**
43 * Rest API for Multicast Forwarding.
44 */
45@Path("mcast")
Rusty Eddyc0e3fea2015-09-28 21:20:41 +000046public class McastResource extends AbstractWebResource {
47
48 private final Logger log = getLogger(getClass());
49 private static final String SOURCE_ADDRESS = "sourceAddress";
50 private static final String GROUP_ADDRESS = "groupAddress";
51 private static final String INGRESS_POINT = "ingressPoint";
52 private static final String EGRESS_POINT = "egressPoint";
53 private static final String MCAST_GROUP = "mcastGroup";
alshabibeff00542015-09-23 13:22:33 -070054
55 /**
56 * Retrieve the multicast route table.
Rusty Eddyc0e3fea2015-09-28 21:20:41 +000057 *
alshabibeff00542015-09-23 13:22:33 -070058 * @return the multicast route table.
59 * @throws IOException if an error occurs
60 */
Rusty Eddyc0e3fea2015-09-28 21:20:41 +000061 @Path("show")
alshabibeff00542015-09-23 13:22:33 -070062 @GET
63 @Produces(MediaType.APPLICATION_JSON)
64 public Response showAll() throws IOException {
Rusty Eddyc0e3fea2015-09-28 21:20:41 +000065 McastRouteTable mrt = McastRouteTable.getInstance();
66 ObjectNode pushContent = new MRibCodec().encode(mrt , this);
67 return ok(pushContent.toString()).build();
alshabibeff00542015-09-23 13:22:33 -070068 }
69
70 /**
Rusty Eddyc0e3fea2015-09-28 21:20:41 +000071 * Static join a multicast flow.
72 *
73 * @param sAddr source address to join
74 * @param gAddr group address to join
75 * @param ports ingress and egress ConnectPoints to join
76 * @return the Result of the join
77 * @throws IOException if something failed with the join command
alshabibeff00542015-09-23 13:22:33 -070078 */
Rusty Eddyc0e3fea2015-09-28 21:20:41 +000079 @Path("/join")
alshabibeff00542015-09-23 13:22:33 -070080 @POST
81 @Consumes(MediaType.APPLICATION_JSON)
Rusty Eddyc0e3fea2015-09-28 21:20:41 +000082 @Produces(MediaType.TEXT_PLAIN)
83 public Response join(@QueryParam("src") String sAddr,
84 @QueryParam("grp") String gAddr,
85 @DefaultValue("") @QueryParam("ports") String ports)
86 throws IOException {
87
alshabibeff00542015-09-23 13:22:33 -070088 ObjectMapper mapper = new ObjectMapper();
Rusty Eddyc0e3fea2015-09-28 21:20:41 +000089 log.debug("Source IP Address: " + sAddr);
90 log.debug("Destination IP Address: " + gAddr);
91 log.debug("Ingress and Egress ports: " + ports);
92
93 String output = "Insertion Faild";
94 if (sAddr != null && gAddr != null && ports != null) {
95
96 String[] portArr = ports.split(",");
97 log.debug("Port Array Length: " + portArr.length);
98 McastRouteTable mrt = McastRouteTable.getInstance();
99 McastRouteBase mr = mrt.addRoute(sAddr, gAddr);
100
101 // Port format "of:0000000000000023/4"
102 log.debug("checking inside outer if: " + portArr.length);
103
104 if (mr != null && portArr != null && portArr.length > 0) {
105
106 String inCP = portArr[0];
107 log.debug("Ingress port provided: " + inCP);
108 mr.addIngressPoint(inCP);
109
110 for (int i = 1; i < portArr.length; i++) {
111 String egCP = portArr[i];
112 log.debug("Egress port provided: " + egCP);
113 mr.addEgressPoint(egCP, McastConnectPoint.JoinSource.STATIC);
114 }
115 mrt.printMcastRouteTable();
116 output = "Successfully Inserted";
117 }
118 } else {
119 output = "Please Insert the rest uri correctly";
120 }
121 return Response.ok(output).build();
122 }
123
124 /**
125 * Delete multicast state.
126 *
127 * @param src address to be deleted
128 * @param grp address to be deleted
129 * @return status of delete if successful
130 */
131 @Path("/delete")
132 @DELETE
133 @Consumes(MediaType.TEXT_PLAIN)
134 @Produces(MediaType.TEXT_PLAIN)
135 public Response removeMcastFlow(@QueryParam("src") String src,
136 @QueryParam("grp") String grp) {
137
138 String resp = "Failed to delete";
139 log.info("Source IP Address to delete: " + src);
140 log.info("Destination IP Address to delete: " + grp);
141 McastRouteTable mrt = McastRouteTable.getInstance();
142 if (src != null && grp != null) {
143 mrt.removeRoute(src, grp);
144 resp = "Deleted flow for src " + src + " and grp " + grp;
145 }
146
147 return Response.ok(resp).build();
alshabibeff00542015-09-23 13:22:33 -0700148 }
149}