Log all configuration(add/modify/delete) changes to a new log file audit.log
[controller.git] / opendaylight / northbound / staticrouting / src / main / java / org / opendaylight / controller / forwarding / staticrouting / northbound / StaticRoutingNorthbound.java
1
2 /*
3  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
4  *
5  * This program and the accompanying materials are made available under the
6  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7  * and is available at http://www.eclipse.org/legal/epl-v10.html
8  */
9
10 package org.opendaylight.controller.forwarding.staticrouting.northbound;
11
12 import java.util.ArrayList;
13 import java.util.List;
14
15 import javax.ws.rs.Consumes;
16 import javax.ws.rs.DELETE;
17 import javax.ws.rs.GET;
18 import javax.ws.rs.POST;
19 import javax.ws.rs.Path;
20 import javax.ws.rs.PathParam;
21 import javax.ws.rs.Produces;
22 import javax.ws.rs.core.Context;
23 import javax.ws.rs.core.MediaType;
24 import javax.ws.rs.core.Response;
25 import javax.ws.rs.core.SecurityContext;
26 import javax.xml.bind.JAXBElement;
27
28 import org.codehaus.enunciate.jaxrs.ResponseCode;
29 import org.codehaus.enunciate.jaxrs.StatusCodes;
30 import org.codehaus.enunciate.jaxrs.TypeHint;
31 import org.opendaylight.controller.containermanager.IContainerManager;
32 import org.opendaylight.controller.forwarding.staticrouting.IForwardingStaticRouting;
33 import org.opendaylight.controller.forwarding.staticrouting.StaticRouteConfig;
34 import org.opendaylight.controller.northbound.commons.RestMessages;
35 import org.opendaylight.controller.northbound.commons.exception.InternalServerErrorException;
36 import org.opendaylight.controller.northbound.commons.exception.NotAcceptableException;
37 import org.opendaylight.controller.northbound.commons.exception.ResourceConflictException;
38 import org.opendaylight.controller.northbound.commons.exception.ResourceNotFoundException;
39 import org.opendaylight.controller.northbound.commons.exception.UnauthorizedException;
40 import org.opendaylight.controller.northbound.commons.utils.NorthboundUtils;
41 import org.opendaylight.controller.sal.authorization.Privilege;
42 import org.opendaylight.controller.sal.utils.GlobalConstants;
43 import org.opendaylight.controller.sal.utils.ServiceHelper;
44 import org.opendaylight.controller.sal.utils.Status;
45
46 /**
47  * Static Routing Northbound APIs
48  *
49  * <br><br>
50  * Authentication scheme : <b>HTTP Basic</b><br>
51  * Authentication realm : <b>opendaylight</b><br>
52  * Transport : <b>HTTP and HTTPS</b><br>
53  * <br>
54  * HTTPS Authentication is disabled by default. Administrator can enable it in tomcat-server.xml after adding
55  * a proper keystore / SSL certificate from a trusted authority.<br>
56  * More info : http://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html#Configuration
57  */
58 @Path("/")
59 public class StaticRoutingNorthbound {
60
61     private String username;
62
63     @Context
64     public void setSecurityContext(SecurityContext context) {
65         username = context.getUserPrincipal().getName();
66     }
67     protected String getUserName() {
68         return username;
69     }
70
71
72
73     private List<StaticRoute> getStaticRoutesInternal(String containerName) {
74
75         IForwardingStaticRouting staticRouting = (IForwardingStaticRouting) ServiceHelper
76                 .getInstance(IForwardingStaticRouting.class, containerName,
77                         this);
78
79         if (staticRouting == null) {
80             throw new ResourceNotFoundException(RestMessages.NOCONTAINER
81                     .toString());
82         }
83
84         List<StaticRoute> routes = new ArrayList<StaticRoute>();
85
86         for (StaticRouteConfig conf : staticRouting.getStaticRouteConfigs()
87                 .values()) {
88             StaticRoute route = new StaticRoute(conf.getName(), conf
89                     .getStaticRoute(), conf.getNextHop());
90             routes.add(route);
91         }
92         return routes;
93     }
94
95     /**
96      * Returns a list of static routes present on the given container
97      *
98      * @param containerName Name of the Container. The Container name for the base controller is "default".
99      * @return List of configured static routes on the given container
100      */
101     @Path("/{containerName}")
102     @GET
103     @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
104     @TypeHint(StaticRoutes.class)
105     @StatusCodes( {
106             @ResponseCode(code = 200, condition = "Operation successful"),
107             @ResponseCode(code = 404, condition = "The containerName passed was not found") })
108     public StaticRoutes getStaticRoutes(
109             @PathParam("containerName") String containerName) {
110
111         if(!NorthboundUtils.isAuthorized(getUserName(), containerName,
112                 Privilege.WRITE, this)){
113             throw new
114                 UnauthorizedException("User is not authorized to perform this operation on container "
115                             + containerName);
116         }
117         return new StaticRoutes(getStaticRoutesInternal(containerName));
118     }
119
120     /**
121      * Returns the static route for the provided configuration name on a given container
122      *
123      * @param containerName Name of the Container. The Container name for the base controller is "default".
124      * @param name Name of the Static Route configuration
125      * @return Static route configured with the supplied Name.
126      */
127     @Path("/{containerName}/{name}")
128     @GET
129     @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
130     @TypeHint(StaticRoute.class)
131     @StatusCodes( {
132             @ResponseCode(code = 200, condition = "Operation successful"),
133             @ResponseCode(code = 404, condition = "The Container Name or Static Route Configuration name passed was not found") })
134     public StaticRoute getStaticRoute(
135             @PathParam("containerName") String containerName,
136             @PathParam("name") String name) {
137
138         if(!NorthboundUtils.isAuthorized(getUserName(), containerName,
139                 Privilege.WRITE, this)){
140             throw new
141                 UnauthorizedException("User is not authorized to perform this operation on container "
142                             + containerName);
143         }
144         List<StaticRoute> routes = this.getStaticRoutesInternal(containerName);
145         for (StaticRoute route : routes) {
146             if (route.getName().equalsIgnoreCase(name)) {
147                 return route;
148             }
149         }
150
151         throw new ResourceNotFoundException(RestMessages.NOSTATICROUTE
152                 .toString());
153     }
154
155     /**
156      *
157      * Add a new Static Route
158      *
159      * @param containerName Name of the Container. The Container name for the base controller is "default".
160      * @param name Name of the Static Route configuration
161      * @return Response as dictated by the HTTP Response code
162      */
163     @Path("/{containerName}/{name}")
164     @POST
165     @Consumes( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
166     @StatusCodes( {
167             @ResponseCode(code = 201, condition = "Created Static Route successfully"),
168             @ResponseCode(code = 404, condition = "The Container Name passed is not found"),
169             @ResponseCode(code = 406, condition = "Cannot operate on Default Container when other Containers are active"),
170             @ResponseCode(code = 409, condition = "Failed to create Static Route entry due to Conflicting Name or Prefix."), })
171     public Response addStaticRoute(
172             @PathParam(value = "containerName") String containerName,
173             @PathParam(value = "name") String name,
174             @TypeHint(StaticRoute.class) JAXBElement<StaticRoute> staticRouteData) {
175
176
177         if(!NorthboundUtils.isAuthorized(getUserName(), containerName,
178                 Privilege.WRITE, this)){
179             throw new
180                 UnauthorizedException("User is not authorized to perform this operation on container "
181                             + containerName);
182         }
183         handleDefaultDisabled(containerName);
184
185         IForwardingStaticRouting staticRouting = (IForwardingStaticRouting) ServiceHelper
186                 .getInstance(IForwardingStaticRouting.class, containerName,
187                         this);
188
189         if (staticRouting == null) {
190             throw new ResourceNotFoundException(RestMessages.NOCONTAINER
191                     .toString());
192         }
193
194         StaticRoute sRoute = staticRouteData.getValue();
195         StaticRouteConfig cfgObject = new StaticRouteConfig(sRoute.getName(),
196                 sRoute.getPrefix(), sRoute.getNextHop());
197         Status response = staticRouting.addStaticRoute(cfgObject);
198         if (response.isSuccess()) {
199             NorthboundUtils.auditlog("Static Route", username, "added", name, containerName);
200             return Response.status(Response.Status.CREATED).build();
201         }
202         throw new ResourceConflictException(response.getDescription());
203     }
204
205     /**
206      *
207      * Delete a Static Route
208      *
209      * @param containerName Name of the Container. The Container name for the base controller is "default".
210      * @param name Name of the Static Route configuration to be removed
211      *
212      * @return Response as dictated by the HTTP Response code
213      */
214
215     @Path("/{containerName}/{name}")
216     @DELETE
217     @StatusCodes( {
218             @ResponseCode(code = 200, condition = "Operation successful"),
219             @ResponseCode(code = 404, condition = "Container Name or Configuration Name not found"),
220             @ResponseCode(code = 406, condition = "Cannot operate on Default Container when other Containers are active") })
221     public Response removeStaticRoute(
222             @PathParam(value = "containerName") String containerName,
223             @PathParam(value = "name") String name) {
224
225         if(!NorthboundUtils.isAuthorized(getUserName(), containerName,
226                 Privilege.WRITE, this)){
227             throw new
228                 UnauthorizedException("User is not authorized to perform this operation on container "
229                             + containerName);
230         }
231         handleDefaultDisabled(containerName);
232
233         IForwardingStaticRouting staticRouting = (IForwardingStaticRouting) ServiceHelper
234                 .getInstance(IForwardingStaticRouting.class, containerName,
235                         this);
236
237         if (staticRouting == null) {
238             throw new ResourceNotFoundException(RestMessages.NOCONTAINER
239                     .toString());
240         }
241
242         Status status = staticRouting.removeStaticRoute(name);
243         if (status.isSuccess()) {
244             NorthboundUtils.auditlog("Static Route", username, "removed", name, containerName);
245             return Response.ok().build();
246         }
247         throw new ResourceNotFoundException(status.getDescription());
248     }
249
250     private void handleDefaultDisabled(String containerName) {
251         IContainerManager containerManager = (IContainerManager) ServiceHelper
252                 .getGlobalInstance(IContainerManager.class, this);
253         if (containerManager == null) {
254             throw new InternalServerErrorException(RestMessages.INTERNALERROR
255                     .toString());
256         }
257         if (containerName.equals(GlobalConstants.DEFAULT.toString())
258                 && containerManager.hasNonDefaultContainer()) {
259             throw new NotAcceptableException(RestMessages.DEFAULTDISABLED
260                     .toString());
261         }
262     }
263 }