Merge "Declare a property for commons-lang version in poms and use it."
[controller.git] / opendaylight / northbound / staticrouting / src / main / java / org / opendaylight / controller / forwarding / staticrouting / northbound / StaticRoutingNorthbound.java
index c48d7ec02684463962909ec2d60b600e3b270167..07f125dae3f79a8428d7609a18c1f4194b2553a1 100644 (file)
@@ -16,6 +16,7 @@ import javax.ws.rs.Consumes;
 import javax.ws.rs.DELETE;
 import javax.ws.rs.GET;
 import javax.ws.rs.POST;
+import javax.ws.rs.PUT;
 import javax.ws.rs.Path;
 import javax.ws.rs.PathParam;
 import javax.ws.rs.Produces;
@@ -23,6 +24,7 @@ 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 javax.ws.rs.core.UriInfo;
 import javax.xml.bind.JAXBElement;
 
 import org.codehaus.enunciate.jaxrs.ResponseCode;
@@ -44,33 +46,42 @@ import org.opendaylight.controller.sal.utils.ServiceHelper;
 import org.opendaylight.controller.sal.utils.Status;
 
 /**
- * Static Routing Northbound APIs
+ * <p>Static Routing Northbound API allows for the management of the static
+ * routes.</p>
+ * </br>
+ * An example request/response for retrieving the static routes may look like this: </br>
+ * <pre>
+ * GET http://localhost:8080/controller/nb/v2/staticroute/default HTTP/1.1
+ * Accept: application/json
+ *
+ * HTTP/1.1 200 OK
+ * Content-Type: application/json
+ *
+ * {"staticRoute":{"name":"route-1","prefix":"10.10.1.0/24","nextHop":"1.1.1.1"}}
+ *
+ * </pre>
  *
  * <br><br>
  * Authentication scheme : <b>HTTP Basic</b><br>
  * Authentication realm : <b>opendaylight</b><br>
  * Transport : <b>HTTP and HTTPS</b><br>
  * <br>
- * HTTPS Authentication is disabled by default. Administrator can enable it in tomcat-server.xml after adding 
- * a proper keystore / SSL certificate from a trusted authority.<br>
- * More info : http://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html#Configuration
  */
 @Path("/")
 public class StaticRoutingNorthbound {
 
+    private String username;
 
-       private String username;
-       
     @Context
     public void setSecurityContext(SecurityContext context) {
-       username = context.getUserPrincipal().getName();
+        if (context != null && context.getUserPrincipal() != null) username = context.getUserPrincipal().getName();
     }
     protected String getUserName() {
         return username;
     }
-       
 
-       
+
+
     private List<StaticRoute> getStaticRoutesInternal(String containerName) {
 
         IForwardingStaticRouting staticRouting = (IForwardingStaticRouting) ServiceHelper
@@ -94,12 +105,32 @@ public class StaticRoutingNorthbound {
     }
 
     /**
-     * Returns a list of static routes present on the given container
+     * Get a list of static routes present on the given container.
      *
      * @param containerName Name of the Container. The Container name for the base controller is "default".
      * @return List of configured static routes on the given container
+     *
+     * <pre>
+     * Example:
+     *
+     * Request URL:
+     * GET http://localhost:8080/controller/nb/v2/staticroute/default/routes
+     *
+     * Response in XML:
+     *  &lt;list&gt;
+     *   &lt;staticRoute&gt;
+     *     &lt;name&gt;route-1&lt;/name&gt;
+     *     &lt;prefix&gt;10.10.1.0/24&lt;/prefix&gt;
+     *     &lt;nextHop&gt;1.1.1.1&lt;/nextHop&gt;
+     *   &lt;/staticRoute&gt;
+     *  &lt;/list&gt;
+     *
+     * Response in JSON:
+     * {"staticRoute":{"name":"route-1","prefix":"10.10.1.0/24","nextHop":"1.1.1.1"}}
+     *
+     * </pre>
      */
-    @Path("/{containerName}")
+    @Path("/{containerName}/routes")
     @GET
     @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
     @TypeHint(StaticRoutes.class)
@@ -109,9 +140,9 @@ public class StaticRoutingNorthbound {
     public StaticRoutes getStaticRoutes(
             @PathParam("containerName") String containerName) {
 
-        if(!NorthboundUtils.isAuthorized(getUserName(), containerName, 
+        if(!NorthboundUtils.isAuthorized(getUserName(), containerName,
                 Privilege.WRITE, this)){
-            throw new 
+            throw new
                 UnauthorizedException("User is not authorized to perform this operation on container "
                             + containerName);
         }
@@ -122,10 +153,29 @@ public class StaticRoutingNorthbound {
      * Returns the static route for the provided configuration name on a given container
      *
      * @param containerName Name of the Container. The Container name for the base controller is "default".
-     * @param name Name of the Static Route configuration
+     * @param route Name of the Static Route configuration
      * @return Static route configured with the supplied Name.
+     *
+     * <pre>
+     * Example:
+     *
+     * Request URL:
+     * GET http://localhost:8080/controller/nb/v2/staticroute/default/route/route-1
+     *
+     * Response in XML:
+     *
+     *   &lt;staticRoute&gt;
+     *     &lt;name&gt;route-1&lt;/name&gt;
+     *     &lt;prefix&gt;10.10.1.0/24&lt;/prefix&gt;
+     *     &lt;nextHop&gt;1.1.1.1&lt;/nextHop&gt;
+     *   &lt;/staticRoute&gt;
+     *
+     * Response in JSON:
+     * {"name":"route-1","prefix":"10.10.1.0/24","nextHop":"1.1.1.1"}
+     *
+     * </pre>
      */
-    @Path("/{containerName}/{name}")
+    @Path("/{containerName}/route/{route}")
     @GET
     @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
     @TypeHint(StaticRoute.class)
@@ -134,18 +184,18 @@ public class StaticRoutingNorthbound {
             @ResponseCode(code = 404, condition = "The Container Name or Static Route Configuration name passed was not found") })
     public StaticRoute getStaticRoute(
             @PathParam("containerName") String containerName,
-            @PathParam("name") String name) {
+            @PathParam("route") String route) {
 
-        if(!NorthboundUtils.isAuthorized(getUserName(), containerName, 
+        if(!NorthboundUtils.isAuthorized(getUserName(), containerName,
                 Privilege.WRITE, this)){
-            throw new 
+            throw new
                 UnauthorizedException("User is not authorized to perform this operation on container "
                             + containerName);
         }
         List<StaticRoute> routes = this.getStaticRoutesInternal(containerName);
-        for (StaticRoute route : routes) {
-            if (route.getName().equalsIgnoreCase(name)) {
-                return route;
+        for (StaticRoute r : routes) {
+            if (r.getName().equalsIgnoreCase(route)) {
+                return r;
             }
         }
 
@@ -155,14 +205,26 @@ public class StaticRoutingNorthbound {
 
     /**
      *
-     * Add a new Static Route
+     * Add a new Static Route. If a route by the given name already exists, this
+     * method will return a non-successful status response.
      *
      * @param containerName Name of the Container. The Container name for the base controller is "default".
-     * @param name Name of the Static Route configuration
+     * @param route Name of the Static Route configuration
      * @return Response as dictated by the HTTP Response code
+     *
+     * <pre>
+     * Example:
+     *
+     * Request URL:
+     * PUT http://localhost:8080/controller/nb/v2/staticroute/default/route/route-1
+     *
+     * Request payload in JSON:
+     * {"name":"route-1","prefix":"10.10.1.0/24","nextHop":"1.1.1.1"}
+     *
+     * </pre>
      */
-    @Path("/{containerName}/{name}")
-    @POST
+    @Path("/{containerName}/route/{route}")
+    @PUT
     @Consumes( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
     @StatusCodes( {
             @ResponseCode(code = 201, condition = "Created Static Route successfully"),
@@ -170,14 +232,15 @@ public class StaticRoutingNorthbound {
             @ResponseCode(code = 406, condition = "Cannot operate on Default Container when other Containers are active"),
             @ResponseCode(code = 409, condition = "Failed to create Static Route entry due to Conflicting Name or Prefix."), })
     public Response addStaticRoute(
+            @Context UriInfo uriInfo,
             @PathParam(value = "containerName") String containerName,
-            @PathParam(value = "name") String name,
-            @TypeHint(StaticRoute.class) JAXBElement<StaticRoute> staticRouteData) {
+            @PathParam(value = "route") String route,
+            @TypeHint(StaticRoute.class) StaticRoute staticRouteData) {
+
 
-   
-        if(!NorthboundUtils.isAuthorized(getUserName(), containerName, 
+        if(!NorthboundUtils.isAuthorized(getUserName(), containerName,
                 Privilege.WRITE, this)){
-            throw new 
+            throw new
                 UnauthorizedException("User is not authorized to perform this operation on container "
                             + containerName);
         }
@@ -192,12 +255,13 @@ public class StaticRoutingNorthbound {
                     .toString());
         }
 
-        StaticRoute sRoute = staticRouteData.getValue();
+        StaticRoute sRoute = staticRouteData;
         StaticRouteConfig cfgObject = new StaticRouteConfig(sRoute.getName(),
                 sRoute.getPrefix(), sRoute.getNextHop());
         Status response = staticRouting.addStaticRoute(cfgObject);
         if (response.isSuccess()) {
-            return Response.status(Response.Status.CREATED).build();
+            NorthboundUtils.auditlog("Static Route", username, "added", route, containerName);
+            return Response.created(uriInfo.getRequestUri()).build();
         }
         throw new ResourceConflictException(response.getDescription());
     }
@@ -207,24 +271,31 @@ public class StaticRoutingNorthbound {
      * Delete a Static Route
      *
      * @param containerName Name of the Container. The Container name for the base controller is "default".
-     * @param name Name of the Static Route configuration to be removed
+     * @param route Name of the Static Route configuration to be removed
      *
      * @return Response as dictated by the HTTP Response code
+     *
+     * <pre>
+     * Example:
+     *
+     * Request URL:
+     * DELETE http://localhost:8080/controller/nb/v2/staticroute/default/route/route-1
+     *
+     * </pre>
      */
-
-    @Path("/{containerName}/{name}")
+    @Path("/{containerName}/route/{route}")
     @DELETE
     @StatusCodes( {
-            @ResponseCode(code = 200, condition = "Operation successful"),
+            @ResponseCode(code = 204, condition = "Static route removed successfully"),
             @ResponseCode(code = 404, condition = "Container Name or Configuration Name not found"),
             @ResponseCode(code = 406, condition = "Cannot operate on Default Container when other Containers are active") })
     public Response removeStaticRoute(
             @PathParam(value = "containerName") String containerName,
-            @PathParam(value = "name") String name) {
-        if(!NorthboundUtils.isAuthorized(getUserName(), containerName, 
+            @PathParam(value = "route") String route) {
+
+        if(!NorthboundUtils.isAuthorized(getUserName(), containerName,
                 Privilege.WRITE, this)){
-            throw new 
+            throw new
                 UnauthorizedException("User is not authorized to perform this operation on container "
                             + containerName);
         }
@@ -239,11 +310,12 @@ public class StaticRoutingNorthbound {
                     .toString());
         }
 
-        Status status = staticRouting.removeStaticRoute(name);
+        Status status = staticRouting.removeStaticRoute(route);
         if (status.isSuccess()) {
-            return Response.ok().build();
+            NorthboundUtils.auditlog("Static Route", username, "removed", route, containerName);
+            return Response.noContent().build();
         }
-        throw new ResourceNotFoundException(status.getDescription());
+        return NorthboundUtils.getResponse(status);
     }
 
     private void handleDefaultDisabled(String containerName) {