Revert "Checkstyle enforcer"
[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
62         private String username;
63         
64     @Context
65     public void setSecurityContext(SecurityContext context) {
66         username = context.getUserPrincipal().getName();
67     }
68     protected String getUserName() {
69         return username;
70     }
71         
72
73         
74     private List<StaticRoute> getStaticRoutesInternal(String containerName) {
75
76         IForwardingStaticRouting staticRouting = (IForwardingStaticRouting) ServiceHelper
77                 .getInstance(IForwardingStaticRouting.class, containerName,
78                         this);
79
80         if (staticRouting == null) {
81             throw new ResourceNotFoundException(RestMessages.NOCONTAINER
82                     .toString());
83         }
84
85         List<StaticRoute> routes = new ArrayList<StaticRoute>();
86
87         for (StaticRouteConfig conf : staticRouting.getStaticRouteConfigs()
88                 .values()) {
89             StaticRoute route = new StaticRoute(conf.getName(), conf
90                     .getStaticRoute(), conf.getNextHop());
91             routes.add(route);
92         }
93         return routes;
94     }
95
96     /**
97      * Returns a list of static routes present on the given container
98      *
99      * @param containerName Name of the Container. The Container name for the base controller is "default".
100      * @return List of configured static routes on the given container
101      */
102     @Path("/{containerName}")
103     @GET
104     @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
105     @TypeHint(StaticRoutes.class)
106     @StatusCodes( {
107             @ResponseCode(code = 200, condition = "Operation successful"),
108             @ResponseCode(code = 404, condition = "The containerName passed was not found") })
109     public StaticRoutes getStaticRoutes(
110             @PathParam("containerName") String containerName) {
111
112         if(!NorthboundUtils.isAuthorized(getUserName(), containerName, 
113                 Privilege.WRITE, this)){
114             throw new 
115                 UnauthorizedException("User is not authorized to perform this operation on container "
116                             + containerName);
117         }
118         return new StaticRoutes(getStaticRoutesInternal(containerName));
119     }
120
121     /**
122      * Returns the static route for the provided configuration name on a given container
123      *
124      * @param containerName Name of the Container. The Container name for the base controller is "default".
125      * @param name Name of the Static Route configuration
126      * @return Static route configured with the supplied Name.
127      */
128     @Path("/{containerName}/{name}")
129     @GET
130     @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
131     @TypeHint(StaticRoute.class)
132     @StatusCodes( {
133             @ResponseCode(code = 200, condition = "Operation successful"),
134             @ResponseCode(code = 404, condition = "The Container Name or Static Route Configuration name passed was not found") })
135     public StaticRoute getStaticRoute(
136             @PathParam("containerName") String containerName,
137             @PathParam("name") String name) {
138
139         if(!NorthboundUtils.isAuthorized(getUserName(), containerName, 
140                 Privilege.WRITE, this)){
141             throw new 
142                 UnauthorizedException("User is not authorized to perform this operation on container "
143                             + containerName);
144         }
145         List<StaticRoute> routes = this.getStaticRoutesInternal(containerName);
146         for (StaticRoute route : routes) {
147             if (route.getName().equalsIgnoreCase(name)) {
148                 return route;
149             }
150         }
151
152         throw new ResourceNotFoundException(RestMessages.NOSTATICROUTE
153                 .toString());
154     }
155
156     /**
157      *
158      * Add a new Static Route
159      *
160      * @param containerName Name of the Container. The Container name for the base controller is "default".
161      * @param name Name of the Static Route configuration
162      * @return Response as dictated by the HTTP Response code
163      */
164     @Path("/{containerName}/{name}")
165     @POST
166     @Consumes( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
167     @StatusCodes( {
168             @ResponseCode(code = 201, condition = "Created Static Route successfully"),
169             @ResponseCode(code = 404, condition = "The Container Name passed is not found"),
170             @ResponseCode(code = 406, condition = "Cannot operate on Default Container when other Containers are active"),
171             @ResponseCode(code = 409, condition = "Failed to create Static Route entry due to Conflicting Name or Prefix."), })
172     public Response addStaticRoute(
173             @PathParam(value = "containerName") String containerName,
174             @PathParam(value = "name") String name,
175             @TypeHint(StaticRoute.class) JAXBElement<StaticRoute> staticRouteData) {
176
177    
178         if(!NorthboundUtils.isAuthorized(getUserName(), containerName, 
179                 Privilege.WRITE, this)){
180             throw new 
181                 UnauthorizedException("User is not authorized to perform this operation on container "
182                             + containerName);
183         }
184         handleDefaultDisabled(containerName);
185
186         IForwardingStaticRouting staticRouting = (IForwardingStaticRouting) ServiceHelper
187                 .getInstance(IForwardingStaticRouting.class, containerName,
188                         this);
189
190         if (staticRouting == null) {
191             throw new ResourceNotFoundException(RestMessages.NOCONTAINER
192                     .toString());
193         }
194
195         StaticRoute sRoute = staticRouteData.getValue();
196         StaticRouteConfig cfgObject = new StaticRouteConfig(sRoute.getName(),
197                 sRoute.getPrefix(), sRoute.getNextHop());
198         Status response = staticRouting.addStaticRoute(cfgObject);
199         if (response.isSuccess()) {
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             return Response.ok().build();
245         }
246         throw new ResourceNotFoundException(status.getDescription());
247     }
248
249     private void handleDefaultDisabled(String containerName) {
250         IContainerManager containerManager = (IContainerManager) ServiceHelper
251                 .getGlobalInstance(IContainerManager.class, this);
252         if (containerManager == null) {
253             throw new InternalServerErrorException(RestMessages.INTERNALERROR
254                     .toString());
255         }
256         if (containerName.equals(GlobalConstants.DEFAULT.toString())
257                 && containerManager.hasNonDefaultContainer()) {
258             throw new NotAcceptableException(RestMessages.DEFAULTDISABLED
259                     .toString());
260         }
261     }
262 }