3 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
10 package org.opendaylight.controller.forwarding.staticrouting.northbound;
12 import java.util.ArrayList;
13 import java.util.List;
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;
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;
47 * Static Routing Northbound APIs
50 * Authentication scheme : <b>HTTP Basic</b><br>
51 * Authentication realm : <b>opendaylight</b><br>
52 * Transport : <b>HTTP and HTTPS</b><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
59 public class StaticRoutingNorthbound {
61 private String username;
64 public void setSecurityContext(SecurityContext context) {
65 username = context.getUserPrincipal().getName();
67 protected String getUserName() {
73 private List<StaticRoute> getStaticRoutesInternal(String containerName) {
75 IForwardingStaticRouting staticRouting = (IForwardingStaticRouting) ServiceHelper
76 .getInstance(IForwardingStaticRouting.class, containerName,
79 if (staticRouting == null) {
80 throw new ResourceNotFoundException(RestMessages.NOCONTAINER
84 List<StaticRoute> routes = new ArrayList<StaticRoute>();
86 for (StaticRouteConfig conf : staticRouting.getStaticRouteConfigs()
88 StaticRoute route = new StaticRoute(conf.getName(), conf
89 .getStaticRoute(), conf.getNextHop());
96 * Returns a list of static routes present on the given container
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
101 @Path("/{containerName}")
103 @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
104 @TypeHint(StaticRoutes.class)
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) {
111 if(!NorthboundUtils.isAuthorized(getUserName(), containerName,
112 Privilege.WRITE, this)){
114 UnauthorizedException("User is not authorized to perform this operation on container "
117 return new StaticRoutes(getStaticRoutesInternal(containerName));
121 * Returns the static route for the provided configuration name on a given container
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.
127 @Path("/{containerName}/{name}")
129 @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
130 @TypeHint(StaticRoute.class)
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) {
138 if(!NorthboundUtils.isAuthorized(getUserName(), containerName,
139 Privilege.WRITE, this)){
141 UnauthorizedException("User is not authorized to perform this operation on container "
144 List<StaticRoute> routes = this.getStaticRoutesInternal(containerName);
145 for (StaticRoute route : routes) {
146 if (route.getName().equalsIgnoreCase(name)) {
151 throw new ResourceNotFoundException(RestMessages.NOSTATICROUTE
157 * Add a new Static Route
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
163 @Path("/{containerName}/{name}")
165 @Consumes( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
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) {
177 if(!NorthboundUtils.isAuthorized(getUserName(), containerName,
178 Privilege.WRITE, this)){
180 UnauthorizedException("User is not authorized to perform this operation on container "
183 handleDefaultDisabled(containerName);
185 IForwardingStaticRouting staticRouting = (IForwardingStaticRouting) ServiceHelper
186 .getInstance(IForwardingStaticRouting.class, containerName,
189 if (staticRouting == null) {
190 throw new ResourceNotFoundException(RestMessages.NOCONTAINER
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();
202 throw new ResourceConflictException(response.getDescription());
207 * Delete a Static Route
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
212 * @return Response as dictated by the HTTP Response code
215 @Path("/{containerName}/{name}")
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) {
225 if(!NorthboundUtils.isAuthorized(getUserName(), containerName,
226 Privilege.WRITE, this)){
228 UnauthorizedException("User is not authorized to perform this operation on container "
231 handleDefaultDisabled(containerName);
233 IForwardingStaticRouting staticRouting = (IForwardingStaticRouting) ServiceHelper
234 .getInstance(IForwardingStaticRouting.class, containerName,
237 if (staticRouting == null) {
238 throw new ResourceNotFoundException(RestMessages.NOCONTAINER
242 Status status = staticRouting.removeStaticRoute(name);
243 if (status.isSuccess()) {
244 NorthboundUtils.auditlog("Static Route", username, "removed", name, containerName);
245 return Response.ok().build();
247 throw new ResourceNotFoundException(status.getDescription());
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
257 if (containerName.equals(GlobalConstants.DEFAULT.toString())
258 && containerManager.hasNonDefaultContainer()) {
259 throw new NotAcceptableException(RestMessages.DEFAULTDISABLED