2 * Copyright (c) 2013, 2015 IBM Corporation and others. All rights reserved.
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
9 package org.opendaylight.neutron.northbound.api;
11 import java.net.HttpURLConnection;
12 import java.util.ArrayList;
13 import java.util.Iterator;
14 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.PUT;
20 import javax.ws.rs.Path;
21 import javax.ws.rs.PathParam;
22 import javax.ws.rs.Produces;
23 import javax.ws.rs.QueryParam;
24 import javax.ws.rs.core.MediaType;
25 import javax.ws.rs.core.Response;
26 import org.codehaus.enunciate.jaxrs.ResponseCode;
27 import org.codehaus.enunciate.jaxrs.StatusCodes;
28 import org.opendaylight.neutron.spi.INeutronRouterCRUD;
29 import org.opendaylight.neutron.spi.NeutronCRUDInterfaces;
30 import org.opendaylight.neutron.spi.NeutronRouter;
31 import org.opendaylight.neutron.spi.NeutronRouter_Interface;
35 * Neutron Northbound REST APIs.<br>
36 * This class provides REST APIs for managing neutron routers
40 * Authentication scheme : <b>HTTP Basic</b><br>
41 * Authentication realm : <b>opendaylight</b><br>
42 * Transport : <b>HTTP and HTTPS</b><br>
44 * HTTPS Authentication is disabled by default. Administrator can enable it in
45 * tomcat-server.xml after adding a proper keystore / SSL certificate from a
46 * trusted authority.<br>
48 * http://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html#Configuration
53 public class NeutronRoutersNorthbound
54 extends AbstractNeutronNorthbound<NeutronRouter, NeutronRouterRequest, INeutronRouterCRUD> {
55 static final String ROUTER_INTERFACE_STR = "network:router_interface";
56 static final String ROUTER_GATEWAY_STR = "network:router_gateway";
57 private static final String RESOURCE_NAME = "Router";
60 protected String getResourceName() {
65 protected NeutronRouter extractFields(NeutronRouter o, List<String> fields) {
66 return o.extractFields(fields);
69 private NeutronCRUDInterfaces getNeutronInterfaces(boolean flag) {
70 NeutronCRUDInterfaces answer = new NeutronCRUDInterfaces().fetchINeutronRouterCRUD(this);
71 if (answer.getRouterInterface() == null) {
72 throw new ServiceUnavailableException(serviceUnavailable());
75 answer = answer.fetchINeutronNetworkCRUD(this);
76 if (answer.getNetworkInterface() == null) {
77 throw new ServiceUnavailableException("Network CRUD Interface "
78 + RestMessages.SERVICEUNAVAILABLE.toString());
85 protected INeutronRouterCRUD getNeutronCRUD() {
86 return getNeutronInterfaces(false).getRouterInterface();
89 private NeutronCRUDInterfaces getAttachInterfaces() {
90 NeutronCRUDInterfaces answer = new NeutronCRUDInterfaces().fetchINeutronRouterCRUD(this);
91 if (answer.getRouterInterface() == null) {
92 throw new ServiceUnavailableException(serviceUnavailable());
94 answer = answer.fetchINeutronPortCRUD(this).fetchINeutronSubnetCRUD(this);
95 if (answer.getPortInterface() == null) {
96 throw new ServiceUnavailableException("Port CRUD Interface "
97 + RestMessages.SERVICEUNAVAILABLE.toString());
99 if (answer.getSubnetInterface() == null) {
100 throw new ServiceUnavailableException("Subnet CRUD Interface "
101 + RestMessages.SERVICEUNAVAILABLE.toString());
107 protected NeutronRouterRequest newNeutronRequest(NeutronRouter o) {
108 return new NeutronRouterRequest(o);
112 * Returns a list of all Routers */
115 @Produces({ MediaType.APPLICATION_JSON })
116 //@TypeHint(OpenStackRouters.class)
118 @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
119 @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
120 @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
121 @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
122 public Response listRouters(
124 @QueryParam("fields") List<String> fields,
125 // note: openstack isn't clear about filtering on lists, so we aren't handling them
126 @QueryParam("id") String queryID,
127 @QueryParam("name") String queryName,
128 @QueryParam("admin_state_up") Boolean queryAdminStateUp,
129 @QueryParam("status") String queryStatus,
130 @QueryParam("tenant_id") String queryTenantID,
131 @QueryParam("external_gateway_info") String queryExternalGatewayInfo,
133 @QueryParam("limit") String limit,
134 @QueryParam("marker") String marker,
135 @QueryParam("page_reverse") String pageReverse
136 // sorting not supported
138 INeutronRouterCRUD routerInterface = getNeutronInterfaces(false).getRouterInterface();
139 if (routerInterface == null) {
140 throw new ServiceUnavailableException(serviceUnavailable());
142 List<NeutronRouter> allRouters = routerInterface.getAll();
143 List<NeutronRouter> ans = new ArrayList<NeutronRouter>();
144 Iterator<NeutronRouter> i = allRouters.iterator();
145 while (i.hasNext()) {
146 NeutronRouter oSS = i.next();
147 if ((queryID == null || queryID.equals(oSS.getID())) &&
148 (queryName == null || queryName.equals(oSS.getName())) &&
149 (queryAdminStateUp == null || queryAdminStateUp.equals(oSS.getAdminStateUp())) &&
150 (queryStatus == null || queryStatus.equals(oSS.getStatus())) &&
151 (queryExternalGatewayInfo == null || queryExternalGatewayInfo.equals(oSS.getExternalGatewayInfo())) &&
152 (queryTenantID == null || queryTenantID.equals(oSS.getTenantID()))) {
153 if (fields.size() > 0) {
154 ans.add(extractFields(oSS,fields));
160 //TODO: apply pagination to results
161 return Response.status(HttpURLConnection.HTTP_OK).entity(
162 new NeutronRouterRequest(ans)).build();
166 * Returns a specific Router */
168 @Path("{routerUUID}")
170 @Produces({ MediaType.APPLICATION_JSON })
171 //@TypeHint(OpenStackRouters.class)
173 @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
174 @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
175 @ResponseCode(code = HttpURLConnection.HTTP_FORBIDDEN, condition = "Forbidden"),
176 @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
177 @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
178 @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
179 public Response showRouter(
180 @PathParam("routerUUID") String routerUUID,
182 @QueryParam("fields") List<String> fields) {
183 return show(routerUUID, fields);
187 * Creates new Routers */
190 @Produces({ MediaType.APPLICATION_JSON })
191 @Consumes({ MediaType.APPLICATION_JSON })
192 //@TypeHint(OpenStackRouters.class)
194 @ResponseCode(code = HttpURLConnection.HTTP_CREATED, condition = "Created"),
195 @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
196 public Response createRouters(final NeutronRouterRequest input) {
197 getNeutronInterfaces(true); // ensure that network service is loaded
198 return create(input);
202 protected void updateDelta(String uuid, NeutronRouter delta, NeutronRouter original) {
204 delta.setTenantID(original.getTenantID());
208 * Updates a Router */
210 @Path("{routerUUID}")
212 @Produces({ MediaType.APPLICATION_JSON })
213 @Consumes({ MediaType.APPLICATION_JSON })
214 //@TypeHint(OpenStackRouters.class)
216 @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
217 @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
218 @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
219 public Response updateRouter(
220 @PathParam("routerUUID") String routerUUID,
221 NeutronRouterRequest input
223 getNeutronInterfaces(true); // ensure that network service is loaded
224 return update(routerUUID, input);
228 * Deletes a Router */
230 @Path("{routerUUID}")
233 @ResponseCode(code = HttpURLConnection.HTTP_NO_CONTENT, condition = "No Content"),
234 @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
235 @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
236 public Response deleteRouter(
237 @PathParam("routerUUID") String routerUUID) {
238 return delete(routerUUID);
242 * Adds an interface to a router */
244 @Path("{routerUUID}/add_router_interface")
246 @Produces({ MediaType.APPLICATION_JSON })
247 @Consumes({ MediaType.APPLICATION_JSON })
248 //@TypeHint(OpenStackRouterInterfaces.class)
250 @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful") })
251 public Response addRouterInterface(
252 @PathParam("routerUUID") String routerUUID,
253 NeutronRouter_Interface input
255 // Do nothing. Keep this interface for compatibility
256 return Response.status(HttpURLConnection.HTTP_OK).entity(input).build();
259 @Path("{routerUUID}/remove_router_interface")
261 @Produces({ MediaType.APPLICATION_JSON })
262 @Consumes({ MediaType.APPLICATION_JSON })
263 //@TypeHint(OpenStackRouterInterfaces.class)
265 @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful") })
266 public Response removeRouterInterface(
267 @PathParam("routerUUID") String routerUUID,
268 NeutronRouter_Interface input
270 // Do nothing. Keep this interface for compatibility
271 return Response.status(HttpURLConnection.HTTP_OK).entity(input).build();