alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 1 | /*
|
| 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 | */
|
| 16 | package org.onosproject.mfwd.rest;
|
| 17 |
|
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 18 | import java.io.IOException;
|
Rusty Eddy | c0e3fea | 2015-09-28 21:20:41 +0000 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.ObjectMapper;
|
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode;
|
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 21 |
|
Rusty Eddy | c0e3fea | 2015-09-28 21:20:41 +0000 | [diff] [blame] | 22 | import javax.ws.rs.DefaultValue;
|
| 23 | import javax.ws.rs.QueryParam;
|
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 24 | import javax.ws.rs.Consumes;
|
| 25 | import javax.ws.rs.GET;
|
| 26 | import javax.ws.rs.POST;
|
Rusty Eddy | c0e3fea | 2015-09-28 21:20:41 +0000 | [diff] [blame] | 27 | import javax.ws.rs.DELETE;
|
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 28 | import javax.ws.rs.Path;
|
| 29 | import javax.ws.rs.Produces;
|
| 30 | import javax.ws.rs.core.MediaType;
|
| 31 | import javax.ws.rs.core.Response;
|
| 32 |
|
Rusty Eddy | c0e3fea | 2015-09-28 21:20:41 +0000 | [diff] [blame] | 33 | import org.onosproject.mfwd.impl.McastConnectPoint;
|
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 34 | import org.onosproject.mfwd.impl.McastRouteTable;
|
Rusty Eddy | c0e3fea | 2015-09-28 21:20:41 +0000 | [diff] [blame] | 35 | import org.onosproject.mfwd.impl.McastRouteBase;
|
| 36 | import org.onosproject.mfwd.impl.MRibCodec;
|
| 37 | import org.onosproject.rest.AbstractWebResource;
|
| 38 |
|
| 39 | import org.slf4j.Logger;
|
| 40 | import static org.slf4j.LoggerFactory.getLogger;
|
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 41 |
|
| 42 | /**
|
| 43 | * Rest API for Multicast Forwarding.
|
| 44 | */
|
| 45 | @Path("mcast")
|
Rusty Eddy | c0e3fea | 2015-09-28 21:20:41 +0000 | [diff] [blame] | 46 | public 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";
|
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 54 |
|
| 55 | /**
|
| 56 | * Retrieve the multicast route table.
|
Rusty Eddy | c0e3fea | 2015-09-28 21:20:41 +0000 | [diff] [blame] | 57 | *
|
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 58 | * @return the multicast route table.
|
| 59 | * @throws IOException if an error occurs
|
| 60 | */
|
Rusty Eddy | c0e3fea | 2015-09-28 21:20:41 +0000 | [diff] [blame] | 61 | @Path("show")
|
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 62 | @GET
|
| 63 | @Produces(MediaType.APPLICATION_JSON)
|
| 64 | public Response showAll() throws IOException {
|
Rusty Eddy | c0e3fea | 2015-09-28 21:20:41 +0000 | [diff] [blame] | 65 | McastRouteTable mrt = McastRouteTable.getInstance();
|
| 66 | ObjectNode pushContent = new MRibCodec().encode(mrt , this);
|
| 67 | return ok(pushContent.toString()).build();
|
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 68 | }
|
| 69 |
|
| 70 | /**
|
Rusty Eddy | c0e3fea | 2015-09-28 21:20:41 +0000 | [diff] [blame] | 71 | * 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
|
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 78 | */
|
Rusty Eddy | c0e3fea | 2015-09-28 21:20:41 +0000 | [diff] [blame] | 79 | @Path("/join")
|
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 80 | @POST
|
| 81 | @Consumes(MediaType.APPLICATION_JSON)
|
Rusty Eddy | c0e3fea | 2015-09-28 21:20:41 +0000 | [diff] [blame] | 82 | @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 |
|
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 88 | ObjectMapper mapper = new ObjectMapper();
|
Rusty Eddy | c0e3fea | 2015-09-28 21:20:41 +0000 | [diff] [blame] | 89 | 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();
|
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 148 | }
|
| 149 | }
|