96cee57e0112b0cde893858e85e568e17325e6dc
[neutron.git] / northbound-api / src / main / java / org / opendaylight / neutron / northbound / api / NeutronRoutersNorthbound.java
1 /*
2  * Copyright (c) 2013, 2015 IBM Corporation and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.neutron.northbound.api;
10
11 import java.net.HttpURLConnection;
12 import java.util.ArrayList;
13 import java.util.List;
14 import javax.ws.rs.Consumes;
15 import javax.ws.rs.DELETE;
16 import javax.ws.rs.GET;
17 import javax.ws.rs.POST;
18 import javax.ws.rs.PUT;
19 import javax.ws.rs.Path;
20 import javax.ws.rs.PathParam;
21 import javax.ws.rs.Produces;
22 import javax.ws.rs.QueryParam;
23 import javax.ws.rs.core.MediaType;
24 import javax.ws.rs.core.Response;
25 import org.codehaus.enunciate.jaxrs.ResponseCode;
26 import org.codehaus.enunciate.jaxrs.StatusCodes;
27 import org.opendaylight.neutron.spi.INeutronRouterCRUD;
28 import org.opendaylight.neutron.spi.NeutronRouter;
29 import org.opendaylight.neutron.spi.NeutronRouterInterface;
30
31 /**
32  * Neutron Northbound REST APIs.<br>
33  * This class provides REST APIs for managing neutron routers
34  *
35  * <br>
36  * <br>
37  * Authentication scheme : <b>HTTP Basic</b><br>
38  * Authentication realm : <b>opendaylight</b><br>
39  * Transport : <b>HTTP and HTTPS</b><br>
40  * <br>
41  * HTTPS Authentication is disabled by default. Administrator can enable it in
42  * tomcat-server.xml after adding a proper keystore / SSL certificate from a
43  * trusted authority.<br>
44  * More info :
45  * http://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html#Configuration
46  *
47  */
48
49 @Path("/routers")
50 public final class NeutronRoutersNorthbound
51         extends AbstractNeutronNorthbound<NeutronRouter, NeutronRouterRequest, INeutronRouterCRUD> {
52     static final String ROUTER_INTERFACE_STR = "network:router_interface";
53     static final String ROUTER_GATEWAY_STR = "network:router_gateway";
54     private static final String RESOURCE_NAME = "Router";
55
56     @Override
57     protected String getResourceName() {
58         return RESOURCE_NAME;
59     }
60
61     /**
62      * Returns a list of all Routers.
63      */
64
65     @GET
66     @Produces({ MediaType.APPLICATION_JSON })
67     //@TypeHint(OpenStackRouters.class)
68     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
69             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
70             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
71             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
72     public Response listRouters(
73             // return fields
74             @QueryParam("fields") List<String> fields,
75             // note: openstack isn't clear about filtering on lists, so we aren't handling them
76             @QueryParam("id") String queryID,
77             @QueryParam("name") String queryName,
78             @QueryParam("admin_state_up") Boolean queryAdminStateUp,
79             @QueryParam("status") String queryStatus,
80             @QueryParam("tenant_id") String queryTenantID,
81             @QueryParam("external_gateway_info") String queryExternalGatewayInfo,
82             // pagination
83             @QueryParam("limit") String limit,
84             @QueryParam("marker") String marker,
85             @QueryParam("page_reverse") String pageReverse
86     // sorting not supported
87     ) {
88         INeutronRouterCRUD routerInterface = getNeutronCRUD();
89         if (routerInterface == null) {
90             throw new ServiceUnavailableException(serviceUnavailable());
91         }
92         List<NeutronRouter> allRouters = routerInterface.getAll();
93         List<NeutronRouter> ans = new ArrayList<>();
94         for (NeutronRouter router : allRouters) {
95             if ((queryID == null || queryID.equals(router.getID()))
96                     && (queryName == null || queryName.equals(router.getName()))
97                     && (queryAdminStateUp == null || queryAdminStateUp.equals(router.getAdminStateUp()))
98                     && (queryStatus == null || queryStatus.equals(router.getStatus()))
99                     && (queryTenantID == null || queryTenantID.equals(router.getTenantID()))) {
100                 if (fields.size() > 0) {
101                     ans.add(router.extractFields(fields));
102                 } else {
103                     ans.add(router);
104                 }
105             }
106         }
107         //TODO: apply pagination to results
108         return Response.status(HttpURLConnection.HTTP_OK).entity(new NeutronRouterRequest(ans)).build();
109     }
110
111     /**
112      * Returns a specific Router.
113      */
114
115     @Path("{routerUUID}")
116     @GET
117     @Produces({ MediaType.APPLICATION_JSON })
118     //@TypeHint(OpenStackRouters.class)
119     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
120             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
121             @ResponseCode(code = HttpURLConnection.HTTP_FORBIDDEN, condition = "Forbidden"),
122             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
123             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
124             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
125     public Response showRouter(@PathParam("routerUUID") String routerUUID,
126             // return fields
127             @QueryParam("fields") List<String> fields) {
128         return show(routerUUID, fields);
129     }
130
131     /**
132      * Creates new Routers.
133      */
134
135     @POST
136     @Produces({ MediaType.APPLICATION_JSON })
137     @Consumes({ MediaType.APPLICATION_JSON })
138     //@TypeHint(OpenStackRouters.class)
139     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_CREATED, condition = "Created"),
140             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
141     public Response createRouters(final NeutronRouterRequest input) {
142         return create(input);
143     }
144
145     @Override
146     protected void updateDelta(String uuid, NeutronRouter delta, NeutronRouter original) {
147         delta.setID(uuid);
148         delta.setTenantID(original.getTenantID());
149     }
150
151     /**
152      * Updates a Router.
153      */
154
155     @Path("{routerUUID}")
156     @PUT
157     @Produces({ MediaType.APPLICATION_JSON })
158     @Consumes({ MediaType.APPLICATION_JSON })
159     //@TypeHint(OpenStackRouters.class)
160     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
161             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
162             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
163     public Response updateRouter(@PathParam("routerUUID") String routerUUID, NeutronRouterRequest input) {
164         return update(routerUUID, input);
165     }
166
167     /**
168      * Deletes a Router.
169      */
170
171     @Path("{routerUUID}")
172     @DELETE
173     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_NO_CONTENT, condition = "No Content"),
174             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
175             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
176     public Response deleteRouter(@PathParam("routerUUID") String routerUUID) {
177         return delete(routerUUID);
178     }
179
180     /**
181      * Adds an interface to a router.
182      */
183
184     @Path("{routerUUID}/add_router_interface")
185     @PUT
186     @Produces({ MediaType.APPLICATION_JSON })
187     @Consumes({ MediaType.APPLICATION_JSON })
188     //@TypeHint(OpenStackRouterInterfaces.class)
189     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful") })
190     public Response addRouterInterface(@PathParam("routerUUID") String routerUUID, NeutronRouterInterface input) {
191         // Do nothing. Keep this interface for compatibility
192         return Response.status(HttpURLConnection.HTTP_OK).entity(input).build();
193     }
194
195     @Path("{routerUUID}/remove_router_interface")
196     @PUT
197     @Produces({ MediaType.APPLICATION_JSON })
198     @Consumes({ MediaType.APPLICATION_JSON })
199     //@TypeHint(OpenStackRouterInterfaces.class)
200     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful") })
201     public Response removeRouterInterface(@PathParam("routerUUID") String routerUUID, NeutronRouterInterface input) {
202         // Do nothing. Keep this interface for compatibility
203         return Response.status(HttpURLConnection.HTTP_OK).entity(input).build();
204     }
205 }