summaryrefslogtreecommitdiffstats
path: root/framework/src/onos/apps/mfwd/src/main/java/org/onosproject/mfwd/rest/McastResource.java
blob: 608e0447fa245162bfe4f1e2dd96e0ea276a91a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
 * Copyright 2015 Open Networking Laboratory
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.onosproject.mfwd.rest;

import java.io.IOException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;

import javax.ws.rs.DefaultValue;
import javax.ws.rs.QueryParam;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.DELETE;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.onosproject.mfwd.impl.McastConnectPoint;
import org.onosproject.mfwd.impl.McastRouteTable;
import org.onosproject.mfwd.impl.McastRouteBase;
import org.onosproject.mfwd.impl.MRibCodec;
import org.onosproject.rest.AbstractWebResource;

import org.slf4j.Logger;
import static org.slf4j.LoggerFactory.getLogger;

/**
 * Rest API for Multicast Forwarding.
 */
@Path("mcast")
public class McastResource extends AbstractWebResource  {

    private final Logger log = getLogger(getClass());
    private static final String SOURCE_ADDRESS = "sourceAddress";
    private static final String GROUP_ADDRESS = "groupAddress";
    private static final String INGRESS_POINT = "ingressPoint";
    private static final String EGRESS_POINT = "egressPoint";
    private static final String MCAST_GROUP = "mcastGroup";

    /**
     * Retrieve the multicast route table.
     *
     * @return the multicast route table.
     * @throws IOException if an error occurs
     */
    @Path("show")
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Response showAll() throws IOException {
        McastRouteTable mrt = McastRouteTable.getInstance();
        ObjectNode pushContent = new MRibCodec().encode(mrt , this);
        return ok(pushContent.toString()).build();
    }

    /**
     * Static join a multicast flow.
     *
     * @param sAddr source address to join
     * @param gAddr group address to join
     * @param ports ingress and egress ConnectPoints to join
     * @return the Result of the join
     * @throws IOException if something failed with the join command
     */
    @Path("/join")
    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.TEXT_PLAIN)
    public Response join(@QueryParam("src") String sAddr,
                    @QueryParam("grp") String gAddr,
                    @DefaultValue("") @QueryParam("ports") String ports)
                    throws IOException {

        ObjectMapper mapper = new ObjectMapper();
        log.debug("Source IP Address: " + sAddr);
        log.debug("Destination IP Address: " + gAddr);
        log.debug("Ingress and Egress ports: " + ports);

        String output = "Insertion Faild";
        if (sAddr != null && gAddr != null && ports != null) {

            String[] portArr = ports.split(",");
            log.debug("Port Array Length: " + portArr.length);
            McastRouteTable mrt = McastRouteTable.getInstance();
            McastRouteBase mr = mrt.addRoute(sAddr, gAddr);

            // Port format "of:0000000000000023/4"
            log.debug("checking inside outer if: " + portArr.length);

            if (mr != null && portArr != null && portArr.length > 0) {

                String inCP = portArr[0];
                log.debug("Ingress port provided: " + inCP);
                mr.addIngressPoint(inCP);

                for (int i = 1; i < portArr.length; i++) {
                    String egCP = portArr[i];
                    log.debug("Egress port provided: " + egCP);
                    mr.addEgressPoint(egCP, McastConnectPoint.JoinSource.STATIC);
                }
                mrt.printMcastRouteTable();
                output = "Successfully Inserted";
            }
        } else {
            output = "Please Insert the rest uri correctly";
        }
        return Response.ok(output).build();
    }

    /**
     * Delete multicast state.
     *
     * @param src address to be deleted
     * @param grp address to be deleted
     * @return status of delete if successful
     */
    @Path("/delete")
    @DELETE
    @Consumes(MediaType.TEXT_PLAIN)
    @Produces(MediaType.TEXT_PLAIN)
    public Response removeMcastFlow(@QueryParam("src") String src,
                    @QueryParam("grp") String grp) {

        String resp = "Failed to delete";
        log.info("Source IP Address to delete: " + src);
        log.info("Destination IP Address to delete: " + grp);
        McastRouteTable mrt = McastRouteTable.getInstance();
        if (src != null && grp != null) {
            mrt.removeRoute(src, grp);
            resp = "Deleted flow for src " + src + " and grp " + grp;
        }

        return Response.ok(resp).build();
    }
}