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 {
62 private String username;
65 public void setSecurityContext(SecurityContext context) {
66 username = context.getUserPrincipal().getName();
68 protected String getUserName() {
74 private List<StaticRoute> getStaticRoutesInternal(String containerName) {
76 IForwardingStaticRouting staticRouting = (IForwardingStaticRouting) ServiceHelper
77 .getInstance(IForwardingStaticRouting.class, containerName,
80 if (staticRouting == null) {
81 throw new ResourceNotFoundException(RestMessages.NOCONTAINER
85 List<StaticRoute> routes = new ArrayList<StaticRoute>();
87 for (StaticRouteConfig conf : staticRouting.getStaticRouteConfigs()
89 StaticRoute route = new StaticRoute(conf.getName(), conf
90 .getStaticRoute(), conf.getNextHop());
97 * Returns a list of static routes present on the given container
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
102 @Path("/{containerName}")
104 @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
105 @TypeHint(StaticRoutes.class)
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) {
112 if(!NorthboundUtils.isAuthorized(getUserName(), containerName,
113 Privilege.WRITE, this)){
115 UnauthorizedException("User is not authorized to perform this operation on container "
118 return new StaticRoutes(getStaticRoutesInternal(containerName));
122 * Returns the static route for the provided configuration name on a given container
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.
128 @Path("/{containerName}/{name}")
130 @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
131 @TypeHint(StaticRoute.class)
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) {
139 if(!NorthboundUtils.isAuthorized(getUserName(), containerName,
140 Privilege.WRITE, this)){
142 UnauthorizedException("User is not authorized to perform this operation on container "
145 List<StaticRoute> routes = this.getStaticRoutesInternal(containerName);
146 for (StaticRoute route : routes) {
147 if (route.getName().equalsIgnoreCase(name)) {
152 throw new ResourceNotFoundException(RestMessages.NOSTATICROUTE
158 * Add a new Static Route
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
164 @Path("/{containerName}/{name}")
166 @Consumes( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
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) {
178 if(!NorthboundUtils.isAuthorized(getUserName(), containerName,
179 Privilege.WRITE, this)){
181 UnauthorizedException("User is not authorized to perform this operation on container "
184 handleDefaultDisabled(containerName);
186 IForwardingStaticRouting staticRouting = (IForwardingStaticRouting) ServiceHelper
187 .getInstance(IForwardingStaticRouting.class, containerName,
190 if (staticRouting == null) {
191 throw new ResourceNotFoundException(RestMessages.NOCONTAINER
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();
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 return Response.ok().build();
246 throw new ResourceNotFoundException(status.getDescription());
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
256 if (containerName.equals(GlobalConstants.DEFAULT.toString())
257 && containerManager.hasNonDefaultContainer()) {
258 throw new NotAcceptableException(RestMessages.DEFAULTDISABLED