clean-up to remove un-used three WebApplicationException subclasses
[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 package org.opendaylight.neutron.northbound.api;
9
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;
32
33 /**
34  * Neutron Northbound REST APIs for managing neutron routers.
35  */
36 @Singleton
37 @Path("/routers")
38 public final class NeutronRoutersNorthbound
39         extends AbstractNeutronNorthbound<NeutronRouter, NeutronRouterRequest, INeutronRouterCRUD> {
40
41     private static final String RESOURCE_NAME = "Router";
42
43     @Inject
44     public NeutronRoutersNorthbound(@OsgiService INeutronRouterCRUD neutronCRUD) {
45         super(neutronCRUD);
46     }
47
48     @Override
49     protected String getResourceName() {
50         return RESOURCE_NAME;
51     }
52
53     /**
54      * Returns a list of all Routers.
55      */
56     @GET
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(
64             // return fields
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,
73             // pagination
74             @QueryParam("limit") String limit,
75             @QueryParam("marker") String marker,
76             @QueryParam("page_reverse") String pageReverse
77     // sorting not supported
78     ) {
79         INeutronRouterCRUD routerInterface = getNeutronCRUD();
80         List<NeutronRouter> allRouters = routerInterface.getAll();
81         List<NeutronRouter> ans = new ArrayList<>();
82         for (NeutronRouter router : allRouters) {
83             if ((queryID == null || queryID.equals(router.getID()))
84                     && (queryName == null || queryName.equals(router.getName()))
85                     && (queryAdminStateUp == null || queryAdminStateUp.equals(router.getAdminStateUp()))
86                     && (queryStatus == null || queryStatus.equals(router.getStatus()))
87                     && (queryTenantID == null || queryTenantID.equals(router.getTenantID()))) {
88                 if (fields.size() > 0) {
89                     ans.add(router.extractFields(fields));
90                 } else {
91                     ans.add(router);
92                 }
93             }
94         }
95         //TODO: apply pagination to results
96         return Response.status(HttpURLConnection.HTTP_OK).entity(new NeutronRouterRequest(ans)).build();
97     }
98
99     /**
100      * Returns a specific Router.
101      */
102     @Path("{routerUUID}")
103     @GET
104     @Produces({ MediaType.APPLICATION_JSON })
105     //@TypeHint(OpenStackRouters.class)
106     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
107             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
108             @ResponseCode(code = HttpURLConnection.HTTP_FORBIDDEN, condition = "Forbidden"),
109             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
110             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
111             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
112     public Response showRouter(@PathParam("routerUUID") String routerUUID,
113             // return fields
114             @QueryParam("fields") List<String> fields) {
115         return show(routerUUID, fields);
116     }
117
118     /**
119      * Creates new Routers.
120      */
121     @POST
122     @Produces({ MediaType.APPLICATION_JSON })
123     @Consumes({ MediaType.APPLICATION_JSON })
124     //@TypeHint(OpenStackRouters.class)
125     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_CREATED, condition = "Created"),
126             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
127     public Response createRouters(final NeutronRouterRequest input) {
128         return create(input);
129     }
130
131     @Override
132     protected void updateDelta(String uuid, NeutronRouter delta, NeutronRouter original) {
133         delta.setID(uuid);
134         delta.setTenantID(original.getTenantID());
135     }
136
137     /**
138      * Updates a Router.
139      */
140     @Path("{routerUUID}")
141     @PUT
142     @Produces({ MediaType.APPLICATION_JSON })
143     @Consumes({ MediaType.APPLICATION_JSON })
144     //@TypeHint(OpenStackRouters.class)
145     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
146             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
147             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
148     public Response updateRouter(@PathParam("routerUUID") String routerUUID, NeutronRouterRequest input) {
149         return update(routerUUID, input);
150     }
151
152     /**
153      * Deletes a Router.
154      */
155     @Path("{routerUUID}")
156     @DELETE
157     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_NO_CONTENT, condition = "No Content"),
158             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
159             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
160     public Response deleteRouter(@PathParam("routerUUID") String routerUUID) {
161         return delete(routerUUID);
162     }
163
164     /**
165      * Adds an interface to a router.
166      */
167     @Path("{routerUUID}/add_router_interface")
168     @PUT
169     @Produces({ MediaType.APPLICATION_JSON })
170     @Consumes({ MediaType.APPLICATION_JSON })
171     //@TypeHint(OpenStackRouterInterfaces.class)
172     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful") })
173     public Response addRouterInterface(@PathParam("routerUUID") String routerUUID, NeutronRouterInterface input) {
174         // Do nothing. Keep this interface for compatibility
175         return Response.status(HttpURLConnection.HTTP_OK).entity(input).build();
176     }
177
178     @Path("{routerUUID}/remove_router_interface")
179     @PUT
180     @Produces({ MediaType.APPLICATION_JSON })
181     @Consumes({ MediaType.APPLICATION_JSON })
182     //@TypeHint(OpenStackRouterInterfaces.class)
183     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful") })
184     public Response removeRouterInterface(@PathParam("routerUUID") String routerUUID, NeutronRouterInterface input) {
185         // Do nothing. Keep this interface for compatibility
186         return Response.status(HttpURLConnection.HTTP_OK).entity(input).build();
187     }
188 }