OpenDaylight Controller functional modules.
[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.MediaType;
23 import javax.ws.rs.core.Response;
24 import javax.xml.bind.JAXBElement;
25
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;
40
41 /**
42  * Static Routing Northbound APIs
43  *
44  * <br><br>
45  * Authentication scheme : <b>HTTP Basic</b><br>
46  * Authentication realm : <b>opendaylight</b><br>
47  * Transport : <b>HTTP and HTTPS</b><br>
48  * <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
52  */
53 @Path("/")
54 public class StaticRoutingNorthbound {
55
56     private List<StaticRoute> getStaticRoutesInternal(String containerName) {
57
58         IForwardingStaticRouting staticRouting = (IForwardingStaticRouting) ServiceHelper
59                 .getInstance(IForwardingStaticRouting.class, containerName,
60                         this);
61
62         if (staticRouting == null) {
63             throw new ResourceNotFoundException(RestMessages.NOCONTAINER
64                     .toString());
65         }
66
67         List<StaticRoute> routes = new ArrayList<StaticRoute>();
68
69         for (StaticRouteConfig conf : staticRouting.getStaticRouteConfigs()
70                 .values()) {
71             StaticRoute route = new StaticRoute(conf.getName(), conf
72                     .getStaticRoute(), conf.getNextHop());
73             routes.add(route);
74         }
75         return routes;
76     }
77
78     /**
79      * Returns a list of static routes present on the given container
80      *
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
83      */
84     @Path("/{containerName}")
85     @GET
86     @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
87     @TypeHint(StaticRoutes.class)
88     @StatusCodes( {
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));
94     }
95
96     /**
97      * Returns the static route for the provided configuration name on a given container
98      *
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.
102      */
103     @Path("/{containerName}/{name}")
104     @GET
105     @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
106     @TypeHint(StaticRoute.class)
107     @StatusCodes( {
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)) {
116                 return route;
117             }
118         }
119
120         throw new ResourceNotFoundException(RestMessages.NOSTATICROUTE
121                 .toString());
122     }
123
124     /**
125      *
126      * Add a new Static Route
127      *
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
131      */
132     @Path("/{containerName}/{name}")
133     @POST
134     @Consumes( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
135     @StatusCodes( {
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) {
144
145         handleDefaultDisabled(containerName);
146
147         IForwardingStaticRouting staticRouting = (IForwardingStaticRouting) ServiceHelper
148                 .getInstance(IForwardingStaticRouting.class, containerName,
149                         this);
150
151         if (staticRouting == null) {
152             throw new ResourceNotFoundException(RestMessages.NOCONTAINER
153                     .toString());
154         }
155
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();
162         }
163         throw new ResourceConflictException(response.getDescription());
164     }
165
166     /**
167      *
168      * Delete a Static Route
169      *
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
172      *
173      * @return Response as dictated by the HTTP Response code
174      */
175
176     @Path("/{containerName}/{name}")
177     @DELETE
178     @StatusCodes( {
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) {
185
186         handleDefaultDisabled(containerName);
187
188         IForwardingStaticRouting staticRouting = (IForwardingStaticRouting) ServiceHelper
189                 .getInstance(IForwardingStaticRouting.class, containerName,
190                         this);
191
192         if (staticRouting == null) {
193             throw new ResourceNotFoundException(RestMessages.NOCONTAINER
194                     .toString());
195         }
196
197         Status status = staticRouting.removeStaticRoute(name);
198         if (status.isSuccess()) {
199             return Response.ok().build();
200         }
201         throw new ResourceNotFoundException(status.getDescription());
202     }
203
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
209                     .toString());
210         }
211         if (containerName.equals(GlobalConstants.DEFAULT.toString())
212                 && containerManager.hasNonDefaultContainer()) {
213             throw new NotAcceptableException(RestMessages.DEFAULTDISABLED
214                     .toString());
215         }
216     }
217 }