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.MediaType;
23 import javax.ws.rs.core.Response;
24 import javax.xml.bind.JAXBElement;
26 import org.codehaus.enunciate.jaxrs.ResponseCode;
27 import org.codehaus.enunciate.jaxrs.StatusCodes;
28 import org.codehaus.enunciate.jaxrs.TypeHint;
29 import org.opendaylight.controller.containermanager.IContainerManager;
30 import org.opendaylight.controller.forwarding.staticrouting.IForwardingStaticRouting;
31 import org.opendaylight.controller.forwarding.staticrouting.StaticRouteConfig;
32 import org.opendaylight.controller.northbound.commons.RestMessages;
33 import org.opendaylight.controller.northbound.commons.exception.InternalServerErrorException;
34 import org.opendaylight.controller.northbound.commons.exception.NotAcceptableException;
35 import org.opendaylight.controller.northbound.commons.exception.ResourceConflictException;
36 import org.opendaylight.controller.northbound.commons.exception.ResourceNotFoundException;
37 import org.opendaylight.controller.sal.utils.GlobalConstants;
38 import org.opendaylight.controller.sal.utils.ServiceHelper;
39 import org.opendaylight.controller.sal.utils.Status;
42 * Static Routing Northbound APIs
45 * Authentication scheme : <b>HTTP Basic</b><br>
46 * Authentication realm : <b>opendaylight</b><br>
47 * Transport : <b>HTTP and HTTPS</b><br>
49 * HTTPS Authentication is disabled by default. Administrator can enable it in tomcat-server.xml after adding
50 * a proper keystore / SSL certificate from a trusted authority.<br>
51 * More info : http://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html#Configuration
54 public class StaticRoutingNorthbound {
56 private List<StaticRoute> getStaticRoutesInternal(String containerName) {
58 IForwardingStaticRouting staticRouting = (IForwardingStaticRouting) ServiceHelper
59 .getInstance(IForwardingStaticRouting.class, containerName,
62 if (staticRouting == null) {
63 throw new ResourceNotFoundException(RestMessages.NOCONTAINER
67 List<StaticRoute> routes = new ArrayList<StaticRoute>();
69 for (StaticRouteConfig conf : staticRouting.getStaticRouteConfigs()
71 StaticRoute route = new StaticRoute(conf.getName(), conf
72 .getStaticRoute(), conf.getNextHop());
79 * Returns a list of static routes present on the given container
81 * @param containerName Name of the Container. The Container name for the base controller is "default".
82 * @return List of configured static routes on the given container
84 @Path("/{containerName}")
86 @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
87 @TypeHint(StaticRoutes.class)
89 @ResponseCode(code = 200, condition = "Operation successful"),
90 @ResponseCode(code = 404, condition = "The containerName passed was not found") })
91 public StaticRoutes getStaticRoutes(
92 @PathParam("containerName") String containerName) {
93 return new StaticRoutes(getStaticRoutesInternal(containerName));
97 * Returns the static route for the provided configuration name on a given container
99 * @param containerName Name of the Container. The Container name for the base controller is "default".
100 * @param name Name of the Static Route configuration
101 * @return Static route configured with the supplied Name.
103 @Path("/{containerName}/{name}")
105 @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
106 @TypeHint(StaticRoute.class)
108 @ResponseCode(code = 200, condition = "Operation successful"),
109 @ResponseCode(code = 404, condition = "The Container Name or Static Route Configuration name passed was not found") })
110 public StaticRoute getStaticRoute(
111 @PathParam("containerName") String containerName,
112 @PathParam("name") String name) {
113 List<StaticRoute> routes = this.getStaticRoutesInternal(containerName);
114 for (StaticRoute route : routes) {
115 if (route.getName().equalsIgnoreCase(name)) {
120 throw new ResourceNotFoundException(RestMessages.NOSTATICROUTE
126 * Add a new Static Route
128 * @param containerName Name of the Container. The Container name for the base controller is "default".
129 * @param name Name of the Static Route configuration
130 * @return Response as dictated by the HTTP Response code
132 @Path("/{containerName}/{name}")
134 @Consumes( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
136 @ResponseCode(code = 201, condition = "Created Static Route successfully"),
137 @ResponseCode(code = 404, condition = "The Container Name passed is not found"),
138 @ResponseCode(code = 406, condition = "Cannot operate on Default Container when other Containers are active"),
139 @ResponseCode(code = 409, condition = "Failed to create Static Route entry due to Conflicting Name or Prefix."), })
140 public Response addStaticRoute(
141 @PathParam(value = "containerName") String containerName,
142 @PathParam(value = "name") String name,
143 @TypeHint(StaticRoute.class) JAXBElement<StaticRoute> staticRouteData) {
145 handleDefaultDisabled(containerName);
147 IForwardingStaticRouting staticRouting = (IForwardingStaticRouting) ServiceHelper
148 .getInstance(IForwardingStaticRouting.class, containerName,
151 if (staticRouting == null) {
152 throw new ResourceNotFoundException(RestMessages.NOCONTAINER
156 StaticRoute sRoute = staticRouteData.getValue();
157 StaticRouteConfig cfgObject = new StaticRouteConfig(sRoute.getName(),
158 sRoute.getPrefix(), sRoute.getNextHop());
159 Status response = staticRouting.addStaticRoute(cfgObject);
160 if (response.isSuccess()) {
161 return Response.status(Response.Status.CREATED).build();
163 throw new ResourceConflictException(response.getDescription());
168 * Delete a Static Route
170 * @param containerName Name of the Container. The Container name for the base controller is "default".
171 * @param name Name of the Static Route configuration to be removed
173 * @return Response as dictated by the HTTP Response code
176 @Path("/{containerName}/{name}")
179 @ResponseCode(code = 200, condition = "Operation successful"),
180 @ResponseCode(code = 404, condition = "Container Name or Configuration Name not found"),
181 @ResponseCode(code = 406, condition = "Cannot operate on Default Container when other Containers are active") })
182 public Response removeStaticRoute(
183 @PathParam(value = "containerName") String containerName,
184 @PathParam(value = "name") String name) {
186 handleDefaultDisabled(containerName);
188 IForwardingStaticRouting staticRouting = (IForwardingStaticRouting) ServiceHelper
189 .getInstance(IForwardingStaticRouting.class, containerName,
192 if (staticRouting == null) {
193 throw new ResourceNotFoundException(RestMessages.NOCONTAINER
197 Status status = staticRouting.removeStaticRoute(name);
198 if (status.isSuccess()) {
199 return Response.ok().build();
201 throw new ResourceNotFoundException(status.getDescription());
204 private void handleDefaultDisabled(String containerName) {
205 IContainerManager containerManager = (IContainerManager) ServiceHelper
206 .getGlobalInstance(IContainerManager.class, this);
207 if (containerManager == null) {
208 throw new InternalServerErrorException(RestMessages.INTERNALERROR
211 if (containerName.equals(GlobalConstants.DEFAULT.toString())
212 && containerManager.hasNonDefaultContainer()) {
213 throw new NotAcceptableException(RestMessages.DEFAULTDISABLED