Authorization fixes for Northbound bundles
[controller.git] / opendaylight / northbound / subnets / src / main / java / org / opendaylight / controller / subnets / northbound / SubnetsNorthboundJAXRS.java
index 1e5336257de13187790ea2fc1b2997fc32ccfc63..05bb5b1dd938ff9208eb5fcda84c6d262e50be33 100644 (file)
@@ -8,7 +8,6 @@
 package org.opendaylight.controller.subnets.northbound;
 
 import java.util.ArrayList;
-import java.util.List;
 
 import javax.ws.rs.DELETE;
 import javax.ws.rs.GET;
@@ -17,8 +16,10 @@ import javax.ws.rs.Path;
 import javax.ws.rs.PathParam;
 import javax.ws.rs.Produces;
 import javax.ws.rs.QueryParam;
+import javax.ws.rs.core.Context;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
+import javax.ws.rs.core.SecurityContext;
 
 import org.codehaus.enunciate.jaxrs.ResponseCode;
 import org.codehaus.enunciate.jaxrs.StatusCodes;
@@ -26,6 +27,9 @@ import org.codehaus.enunciate.jaxrs.TypeHint;
 import org.opendaylight.controller.northbound.commons.RestMessages;
 import org.opendaylight.controller.northbound.commons.exception.InternalServerErrorException;
 import org.opendaylight.controller.northbound.commons.exception.ResourceNotFoundException;
+import org.opendaylight.controller.northbound.commons.exception.UnauthorizedException;
+import org.opendaylight.controller.northbound.commons.utils.NorthboundUtils;
+import org.opendaylight.controller.sal.authorization.Privilege;
 import org.opendaylight.controller.sal.utils.ServiceHelper;
 import org.opendaylight.controller.sal.utils.Status;
 import org.opendaylight.controller.switchmanager.ISwitchManager;
@@ -38,60 +42,86 @@ public class SubnetsNorthboundJAXRS {
     protected static final Logger logger = LoggerFactory
             .getLogger(SubnetsNorthboundJAXRS.class);
 
+    private String username;
+
+    @Context
+    public void setSecurityContext(SecurityContext context) {
+        username = context.getUserPrincipal().getName();
+    }
+
+    protected String getUserName() {
+        return username;
+    }
+
     /**
      * List all the subnets in a given container
-     *
-     * @param containerName container in which we want to query the subnets
-     *
+     * 
+     * @param containerName
+     *            container in which we want to query the subnets
+     * 
      * @return a List of SubnetConfig
      */
     @Path("/{containerName}")
     @GET
-    @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
-    @StatusCodes( { @ResponseCode(code = 404, condition = "The containerName passed was not found") })
+    @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
+    @StatusCodes({ @ResponseCode(code = 404, condition = "The containerName passed was not found") })
     @TypeHint(SubnetConfigs.class)
     public SubnetConfigs listSubnets(
             @PathParam("containerName") String containerName) {
+        if (!NorthboundUtils.isAuthorized(
+                getUserName(), containerName, Privilege.READ, this)) {
+            throw new UnauthorizedException(
+                    "User is not authorized to perform this operation on container "
+                            + containerName);
+        }
         ISwitchManager switchManager = null;
         switchManager = (ISwitchManager) ServiceHelper.getInstance(
                 ISwitchManager.class, containerName, this);
         if (switchManager == null) {
-            throw new ResourceNotFoundException(RestMessages.NOCONTAINER
-                    .toString());
+            throw new ResourceNotFoundException(
+                    RestMessages.NOCONTAINER.toString());
         }
         return new SubnetConfigs(switchManager.getSubnetsConfigList());
     }
 
     /**
      * List the configuration of a subnet in a given container
-     *
-     * @param containerName container in which we want to query the
-     * subnet
-     * @param subnetName of the subnet being queried
-     *
+     * 
+     * @param containerName
+     *            container in which we want to query the subnet
+     * @param subnetName
+     *            of the subnet being queried
+     * 
      * @return a SubnetConfig
      */
     @Path("/{containerName}/{subnetName}")
     @GET
-    @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
-    @StatusCodes( {
+    @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
+    @StatusCodes({
             @ResponseCode(code = 404, condition = "The containerName passed was not found"),
             @ResponseCode(code = 404, condition = "Subnet does not exist") })
     @TypeHint(SubnetConfig.class)
     public SubnetConfig listSubnet(
             @PathParam("containerName") String containerName,
             @PathParam("subnetName") String subnetName) {
+
+        if (!NorthboundUtils.isAuthorized(
+                getUserName(), containerName, Privilege.READ, this)) {
+            throw new UnauthorizedException(
+                    "User is not authorized to perform this operation on container "
+                            + containerName);
+        }
         ISwitchManager switchManager = null;
         switchManager = (ISwitchManager) ServiceHelper.getInstance(
                 ISwitchManager.class, containerName, this);
         if (switchManager == null) {
-            throw new ResourceNotFoundException(RestMessages.NOCONTAINER
-                    .toString());
+            throw new ResourceNotFoundException(
+                    RestMessages.NOCONTAINER.toString());
         }
         SubnetConfig res = switchManager.getSubnetConfig(subnetName);
         if (res == null) {
-            throw new ResourceNotFoundException(RestMessages.NOSUBNET
-                    .toString());
+            throw new ResourceNotFoundException(
+                    RestMessages.NOSUBNET.toString());
         } else {
             return res;
         }
@@ -99,17 +129,19 @@ public class SubnetsNorthboundJAXRS {
 
     /**
      * Add/Update a subnet to a container
-     *
-     * @param containerName container in which we want to add/update the
-     * subnet
-     * @param subnetName that has to be added
-     * @param subnet pair default gateway IP/mask that identify the
-     * subnet being added modified
-     *
+     * 
+     * @param containerName
+     *            container in which we want to add/update the subnet
+     * @param subnetName
+     *            that has to be added
+     * @param subnet
+     *            pair default gateway IP/mask that identify the subnet being
+     *            added modified
+     * 
      */
     @Path("/{containerName}/{subnetName}")
     @POST
-    @StatusCodes( {
+    @StatusCodes({
             @ResponseCode(code = 404, condition = "The containerName passed was not found"),
             @ResponseCode(code = 404, condition = "Invalid Data passed"),
             @ResponseCode(code = 201, condition = "Subnet added/modified"),
@@ -117,17 +149,27 @@ public class SubnetsNorthboundJAXRS {
     public Response addSubnet(@PathParam("containerName") String containerName,
             @PathParam("subnetName") String subnetName,
             @QueryParam("subnet") String subnet) {
+
+        if (!NorthboundUtils.isAuthorized(
+                getUserName(), containerName, Privilege.WRITE, this)) {
+            throw new UnauthorizedException(
+                    "User is not authorized to perform this operation on container "
+                            + containerName);
+        }
         if (subnetName == null) {
-            throw new ResourceNotFoundException(RestMessages.INVALIDDATA.toString());
+            throw new ResourceNotFoundException(
+                    RestMessages.INVALIDDATA.toString());
         }
         if (subnet == null) {
-            throw new ResourceNotFoundException(RestMessages.INVALIDDATA.toString());
+            throw new ResourceNotFoundException(
+                    RestMessages.INVALIDDATA.toString());
         }
         ISwitchManager switchManager = null;
         switchManager = (ISwitchManager) ServiceHelper.getInstance(
                 ISwitchManager.class, containerName, this);
         if (switchManager == null) {
-            throw new ResourceNotFoundException(RestMessages.NOCONTAINER.toString());
+            throw new ResourceNotFoundException(
+                    RestMessages.NOCONTAINER.toString());
         }
 
         SubnetConfig cfgObject = new SubnetConfig(subnetName, subnet,
@@ -141,30 +183,39 @@ public class SubnetsNorthboundJAXRS {
 
     /**
      * Delete a subnet from a container
-     *
-     * @param containerName container in which we want to delete the
-     * subnet by name
-     * @param subnetName of the subnet to be remove.
-     *
+     * 
+     * @param containerName
+     *            container in which we want to delete the subnet by name
+     * @param subnetName
+     *            of the subnet to be remove.
+     * 
      */
     @Path("/{containerName}/{subnetName}")
     @DELETE
-    @StatusCodes( {
+    @StatusCodes({
             @ResponseCode(code = 404, condition = "The containerName passed was not found"),
             @ResponseCode(code = 500, condition = "Removal of subnet failed") })
     public Response removeSubnet(
             @PathParam("containerName") String containerName,
             @PathParam("subnetName") String subnetName) {
         if (subnetName == null) {
-            throw new ResourceNotFoundException(RestMessages.INVALIDDATA
-                    .toString());
+            throw new ResourceNotFoundException(
+                    RestMessages.INVALIDDATA.toString());
         }
+
+        if (!NorthboundUtils.isAuthorized(
+                getUserName(), containerName, Privilege.WRITE, this)) {
+            throw new UnauthorizedException(
+                    "User is not authorized to perform this operation on container "
+                            + containerName);
+        }
+
         ISwitchManager switchManager = null;
         switchManager = (ISwitchManager) ServiceHelper.getInstance(
                 ISwitchManager.class, containerName, this);
         if (switchManager == null) {
-            throw new ResourceNotFoundException(RestMessages.NOCONTAINER
-                    .toString());
+            throw new ResourceNotFoundException(
+                    RestMessages.NOCONTAINER.toString());
         }
         Status status = switchManager.removeSubnet(subnetName);
         if (status.isSuccess()) {
@@ -173,58 +224,57 @@ public class SubnetsNorthboundJAXRS {
         throw new InternalServerErrorException(status.getDescription());
     }
 
-    /* /\** */
-    /*  * */
-    /*  * Add or remove switch ports to a subnet */
-    /*  * */
-    /*  * POST subnets/green/sw */
-    /*  * */
-    /*  * @param model */
-    /*  * @param containerName */
-    /*  * @param name */
-    /*  * @param subnet: the subnet name name */
-    /*  * @param switchports: datapath ID/port list => xx:xx:xx:xx:xx:xx:xx:xx/a,b,c-m,r-t,y */
-    /*  * @return */
-    /*  *\/ */
-    /* @RequestMapping(value = "/{containerName}/{name}", method = RequestMethod.POST) */
-    /* public View addSwitchports(Map<String, Object> model, */
-    /*         @PathVariable(value = "containerName") String containerName, */
-    /*         @PathVariable(value = "name") String name, */
-    /*         @RequestParam(value = "nodeports") String nodePorts, */
-    /*         @RequestParam(value = "action") String action) { */
-
-    /*     checkDefaultDisabled(containerName); */
-
-    /*     ISwitchManager switchManager = null; */
-    /*     try { */
-    /*         BundleContext bCtx = FrameworkUtil.getBundle(this.getClass()) */
-    /*                 .getBundleContext(); */
-
-    /*         ServiceReference[] services = bCtx.getServiceReferences( */
-    /*                 ISwitchManager.class.getName(), "(containerName=" */
-    /*                         + containerName + ")"); */
-
-    /*         if (services != null) { */
-    /*             switchManager = (ISwitchManager) bCtx.getService(services[0]); */
-    /*             logger.debug("Switch manager reference is:" + switchManager); */
-    /*         } */
-    /*     } catch (Exception e) { */
-    /*         logger.error("Switch Manager reference is NULL"); */
-    /*     } */
-
-    /*     checkContainerExists(switchManager); */
-
-    /*     String ret; */
-    /*     if (action.equals("add")) { */
-    /*         ret = switchManager.addPortsToSubnet(name, nodePorts); */
-    /*     } else if (action.equals("remove")) { */
-    /*         ret = switchManager.removePortsFromSubnet(name, nodePorts); */
-    /*     } else { */
-    /*         throw new UnsupportedMediaTypeException(RestMessages.UNKNOWNACTION */
-    /*                 .toString() */
-    /*                 + ": " + action); */
-    /*     } */
-
-    /*     return returnViewOrThrowConflicEx(model, ret); */
-    /* } */
+    /*
+     * 
+     * Add or remove switch ports to a subnet POST subnets/green/sw
+     * 
+     * @param model
+     * 
+     * @param containerName
+     * 
+     * @param name
+     * 
+     * @param subnet: the subnet name name
+     * 
+     * @param switchports: datapath ID/port list =>
+     * xx:xx:xx:xx:xx:xx:xx:xx/a,b,c-m,r-t,y
+     * 
+     * @return
+     * 
+     * @RequestMapping(value = "/{containerName}/{name}", method =
+     * RequestMethod.POST)
+     * 
+     * public View addSwitchports(Map<String, Object> model,
+     * 
+     * @PathVariable(value = "containerName") String containerName,
+     * 
+     * @PathVariable(value = "name") String name,
+     * 
+     * @RequestParam(value = "nodeports") String nodePorts,
+     * 
+     * @RequestParam(value = "action") String action) {
+     * 
+     * checkDefaultDisabled(containerName); ISwitchManager switchManager = null;
+     * try { BundleContext bCtx = FrameworkUtil.getBundle(this.getClass())
+     * .getBundleContext();
+     * 
+     * ServiceReference[] services = bCtx.getServiceReferences(
+     * ISwitchManager.class.getName(), "(containerName=" + containerName + ")");
+     * 
+     * if (services != null) { switchManager = (ISwitchManager)
+     * bCtx.getService(services[0]); logger.debug("Switch manager reference is:"
+     * + switchManager); } } catch (Exception e) {
+     * logger.error("Switch Manager reference is NULL"); }
+     * 
+     * checkContainerExists(switchManager);
+     * 
+     * String ret; if (action.equals("add")) { ret =
+     * switchManager.addPortsToSubnet(name, nodePorts); } else if
+     * (action.equals("remove")) { ret =
+     * switchManager.removePortsFromSubnet(name, nodePorts); } else { throw new
+     * UnsupportedMediaTypeException(RestMessages.UNKNOWNACTION .toString() +
+     * ": " + action); }
+     * 
+     * return returnViewOrThrowConflicEx(model, ret); }
+     */
 }