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
8 package org.opendaylight.neutron.northbound.api;
10 import java.net.HttpURLConnection;
11 import java.util.ArrayList;
12 import java.util.List;
13 import javax.inject.Inject;
14 import javax.inject.Singleton;
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.NeutronRouter;
30 import org.opendaylight.neutron.spi.NeutronRouterInterface;
31 import org.ops4j.pax.cdi.api.OsgiService;
34 * Neutron Northbound REST APIs for managing neutron routers.
38 public final class NeutronRoutersNorthbound
39 extends AbstractNeutronNorthbound<NeutronRouter, NeutronRouterRequest, INeutronRouterCRUD> {
41 private static final String RESOURCE_NAME = "Router";
44 public NeutronRoutersNorthbound(@OsgiService INeutronRouterCRUD neutronCRUD) {
49 protected String getResourceName() {
54 * Returns a list of all Routers.
57 @Produces({ MediaType.APPLICATION_JSON })
58 //@TypeHint(OpenStackRouters.class)
59 @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
60 @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
61 @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
62 @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
63 public Response listRouters(
65 @QueryParam("fields") List<String> fields,
66 // note: openstack isn't clear about filtering on lists, so we aren't handling them
67 @QueryParam("id") String queryID,
68 @QueryParam("name") String queryName,
69 @QueryParam("admin_state_up") Boolean queryAdminStateUp,
70 @QueryParam("status") String queryStatus,
71 @QueryParam("tenant_id") String queryTenantID,
72 @QueryParam("external_gateway_info") String queryExternalGatewayInfo,
74 @QueryParam("limit") String limit,
75 @QueryParam("marker") String marker,
76 @QueryParam("page_reverse") String pageReverse
77 // sorting not supported
79 INeutronRouterCRUD routerInterface = getNeutronCRUD();
80 if (routerInterface == null) {
81 throw new ServiceUnavailableException(serviceUnavailable());
83 List<NeutronRouter> allRouters = routerInterface.getAll();
84 List<NeutronRouter> ans = new ArrayList<>();
85 for (NeutronRouter router : allRouters) {
86 if ((queryID == null || queryID.equals(router.getID()))
87 && (queryName == null || queryName.equals(router.getName()))
88 && (queryAdminStateUp == null || queryAdminStateUp.equals(router.getAdminStateUp()))
89 && (queryStatus == null || queryStatus.equals(router.getStatus()))
90 && (queryTenantID == null || queryTenantID.equals(router.getTenantID()))) {
91 if (fields.size() > 0) {
92 ans.add(router.extractFields(fields));
98 //TODO: apply pagination to results
99 return Response.status(HttpURLConnection.HTTP_OK).entity(new NeutronRouterRequest(ans)).build();
103 * Returns a specific Router.
105 @Path("{routerUUID}")
107 @Produces({ MediaType.APPLICATION_JSON })
108 //@TypeHint(OpenStackRouters.class)
109 @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
110 @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
111 @ResponseCode(code = HttpURLConnection.HTTP_FORBIDDEN, condition = "Forbidden"),
112 @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
113 @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
114 @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
115 public Response showRouter(@PathParam("routerUUID") String routerUUID,
117 @QueryParam("fields") List<String> fields) {
118 return show(routerUUID, fields);
122 * Creates new Routers.
125 @Produces({ MediaType.APPLICATION_JSON })
126 @Consumes({ MediaType.APPLICATION_JSON })
127 //@TypeHint(OpenStackRouters.class)
128 @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_CREATED, condition = "Created"),
129 @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
130 public Response createRouters(final NeutronRouterRequest input) {
131 return create(input);
135 protected void updateDelta(String uuid, NeutronRouter delta, NeutronRouter original) {
137 delta.setTenantID(original.getTenantID());
143 @Path("{routerUUID}")
145 @Produces({ MediaType.APPLICATION_JSON })
146 @Consumes({ MediaType.APPLICATION_JSON })
147 //@TypeHint(OpenStackRouters.class)
148 @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
149 @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
150 @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
151 public Response updateRouter(@PathParam("routerUUID") String routerUUID, NeutronRouterRequest input) {
152 return update(routerUUID, input);
158 @Path("{routerUUID}")
160 @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_NO_CONTENT, condition = "No Content"),
161 @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
162 @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
163 public Response deleteRouter(@PathParam("routerUUID") String routerUUID) {
164 return delete(routerUUID);
168 * Adds an interface to a router.
170 @Path("{routerUUID}/add_router_interface")
172 @Produces({ MediaType.APPLICATION_JSON })
173 @Consumes({ MediaType.APPLICATION_JSON })
174 //@TypeHint(OpenStackRouterInterfaces.class)
175 @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful") })
176 public Response addRouterInterface(@PathParam("routerUUID") String routerUUID, NeutronRouterInterface input) {
177 // Do nothing. Keep this interface for compatibility
178 return Response.status(HttpURLConnection.HTTP_OK).entity(input).build();
181 @Path("{routerUUID}/remove_router_interface")
183 @Produces({ MediaType.APPLICATION_JSON })
184 @Consumes({ MediaType.APPLICATION_JSON })
185 //@TypeHint(OpenStackRouterInterfaces.class)
186 @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful") })
187 public Response removeRouterInterface(@PathParam("routerUUID") String routerUUID, NeutronRouterInterface input) {
188 // Do nothing. Keep this interface for compatibility
189 return Response.status(HttpURLConnection.HTTP_OK).entity(input).build();