Checkstyle Import issues fix (SPI tests,Northbound API)
[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.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;
32
33
34 /**
35  * Neutron Northbound REST APIs.<br>
36  * This class provides REST APIs for managing neutron routers
37  *
38  * <br>
39  * <br>
40  * Authentication scheme : <b>HTTP Basic</b><br>
41  * Authentication realm : <b>opendaylight</b><br>
42  * Transport : <b>HTTP and HTTPS</b><br>
43  * <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>
47  * More info :
48  * http://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html#Configuration
49  *
50  */
51
52 @Path("/routers")
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";
58
59     @Override
60     protected String getResourceName() {
61         return RESOURCE_NAME;
62     }
63
64     @Override
65     protected NeutronRouter extractFields(NeutronRouter o, List<String> fields) {
66         return o.extractFields(fields);
67     }
68
69     private NeutronCRUDInterfaces getNeutronInterfaces(boolean flag) {
70         NeutronCRUDInterfaces answer = new NeutronCRUDInterfaces().fetchINeutronRouterCRUD(this);
71         if (answer.getRouterInterface() == null) {
72             throw new ServiceUnavailableException(serviceUnavailable());
73         }
74         if (flag) {
75             answer = answer.fetchINeutronNetworkCRUD(this);
76             if (answer.getNetworkInterface() == null) {
77                 throw new ServiceUnavailableException("Network CRUD Interface "
78                     + RestMessages.SERVICEUNAVAILABLE.toString());
79             }
80         }
81         return answer;
82     }
83
84     @Override
85     protected INeutronRouterCRUD getNeutronCRUD() {
86         return getNeutronInterfaces(false).getRouterInterface();
87     }
88
89     private NeutronCRUDInterfaces getAttachInterfaces() {
90         NeutronCRUDInterfaces answer = new NeutronCRUDInterfaces().fetchINeutronRouterCRUD(this);
91         if (answer.getRouterInterface() == null) {
92             throw new ServiceUnavailableException(serviceUnavailable());
93         }
94         answer = answer.fetchINeutronPortCRUD(this).fetchINeutronSubnetCRUD(this);
95         if (answer.getPortInterface() == null) {
96             throw new ServiceUnavailableException("Port CRUD Interface "
97                     + RestMessages.SERVICEUNAVAILABLE.toString());
98         }
99         if (answer.getSubnetInterface() == null) {
100             throw new ServiceUnavailableException("Subnet CRUD Interface "
101                     + RestMessages.SERVICEUNAVAILABLE.toString());
102         }
103         return answer;
104     }
105
106     @Override
107     protected NeutronRouterRequest newNeutronRequest(NeutronRouter o) {
108         return new NeutronRouterRequest(o);
109     }
110
111     /**
112      * Returns a list of all Routers */
113
114     @GET
115     @Produces({ MediaType.APPLICATION_JSON })
116     //@TypeHint(OpenStackRouters.class)
117     @StatusCodes({
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(
123             // return fields
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,
132             // pagination
133             @QueryParam("limit") String limit,
134             @QueryParam("marker") String marker,
135             @QueryParam("page_reverse") String pageReverse
136             // sorting not supported
137             ) {
138         INeutronRouterCRUD routerInterface = getNeutronInterfaces(false).getRouterInterface();
139         if (routerInterface == null) {
140             throw new ServiceUnavailableException(serviceUnavailable());
141         }
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));
155                 } else {
156                     ans.add(oSS);
157                 }
158             }
159         }
160         //TODO: apply pagination to results
161         return Response.status(HttpURLConnection.HTTP_OK).entity(
162                 new NeutronRouterRequest(ans)).build();
163     }
164
165     /**
166      * Returns a specific Router */
167
168     @Path("{routerUUID}")
169     @GET
170     @Produces({ MediaType.APPLICATION_JSON })
171     //@TypeHint(OpenStackRouters.class)
172     @StatusCodes({
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,
181             // return fields
182             @QueryParam("fields") List<String> fields) {
183         return show(routerUUID, fields);
184     }
185
186     /**
187      * Creates new Routers */
188
189     @POST
190     @Produces({ MediaType.APPLICATION_JSON })
191     @Consumes({ MediaType.APPLICATION_JSON })
192     //@TypeHint(OpenStackRouters.class)
193     @StatusCodes({
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);
199     }
200
201     @Override
202     protected void updateDelta(String uuid, NeutronRouter delta, NeutronRouter original) {
203         delta.setID(uuid);
204         delta.setTenantID(original.getTenantID());
205     }
206
207     /**
208      * Updates a Router */
209
210     @Path("{routerUUID}")
211     @PUT
212     @Produces({ MediaType.APPLICATION_JSON })
213     @Consumes({ MediaType.APPLICATION_JSON })
214     //@TypeHint(OpenStackRouters.class)
215     @StatusCodes({
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
222             ) {
223         getNeutronInterfaces(true); // ensure that network service is loaded
224         return update(routerUUID, input);
225     }
226
227     /**
228      * Deletes a Router */
229
230     @Path("{routerUUID}")
231     @DELETE
232     @StatusCodes({
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);
239     }
240
241     /**
242      * Adds an interface to a router */
243
244     @Path("{routerUUID}/add_router_interface")
245     @PUT
246     @Produces({ MediaType.APPLICATION_JSON })
247     @Consumes({ MediaType.APPLICATION_JSON })
248     //@TypeHint(OpenStackRouterInterfaces.class)
249     @StatusCodes({
250             @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful") })
251     public Response addRouterInterface(
252             @PathParam("routerUUID") String routerUUID,
253             NeutronRouter_Interface input
254             ) {
255         // Do nothing. Keep this interface for compatibility
256         return Response.status(HttpURLConnection.HTTP_OK).entity(input).build();
257     }
258
259     @Path("{routerUUID}/remove_router_interface")
260     @PUT
261     @Produces({ MediaType.APPLICATION_JSON })
262     @Consumes({ MediaType.APPLICATION_JSON })
263     //@TypeHint(OpenStackRouterInterfaces.class)
264     @StatusCodes({
265             @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful") })
266     public Response removeRouterInterface(
267             @PathParam("routerUUID") String routerUUID,
268             NeutronRouter_Interface input
269             ) {
270         // Do nothing. Keep this interface for compatibility
271         return Response.status(HttpURLConnection.HTTP_OK).entity(input).build();
272     }
273 }