Merge "Bug 5278 [BORON] Remove checks that require neutron providers for L3 and others"
[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
13 import java.util.ArrayList;
14 import java.util.Iterator;
15 import java.util.List;
16
17 import javax.ws.rs.Consumes;
18 import javax.ws.rs.DELETE;
19 import javax.ws.rs.GET;
20 import javax.ws.rs.POST;
21 import javax.ws.rs.PUT;
22 import javax.ws.rs.Path;
23 import javax.ws.rs.PathParam;
24 import javax.ws.rs.Produces;
25 import javax.ws.rs.QueryParam;
26 import javax.ws.rs.core.MediaType;
27 import javax.ws.rs.core.Response;
28
29 import org.codehaus.enunciate.jaxrs.ResponseCode;
30 import org.codehaus.enunciate.jaxrs.StatusCodes;
31 import org.opendaylight.neutron.spi.INeutronNetworkCRUD;
32 import org.opendaylight.neutron.spi.INeutronPortCRUD;
33 import org.opendaylight.neutron.spi.INeutronRouterAware;
34 import org.opendaylight.neutron.spi.INeutronRouterCRUD;
35 import org.opendaylight.neutron.spi.INeutronSubnetCRUD;
36 import org.opendaylight.neutron.spi.NeutronCRUDInterfaces;
37 import org.opendaylight.neutron.spi.NeutronRouter;
38 import org.opendaylight.neutron.spi.NeutronRouter_Interface;
39
40
41 /**
42  * Neutron Northbound REST APIs.<br>
43  * This class provides REST APIs for managing neutron routers
44  *
45  * <br>
46  * <br>
47  * Authentication scheme : <b>HTTP Basic</b><br>
48  * Authentication realm : <b>opendaylight</b><br>
49  * Transport : <b>HTTP and HTTPS</b><br>
50  * <br>
51  * HTTPS Authentication is disabled by default. Administrator can enable it in
52  * tomcat-server.xml after adding a proper keystore / SSL certificate from a
53  * trusted authority.<br>
54  * More info :
55  * http://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html#Configuration
56  *
57  */
58
59 @Path("/routers")
60 public class NeutronRoutersNorthbound
61     extends AbstractNeutronNorthboundIAware<NeutronRouter, NeutronRouterRequest, INeutronRouterCRUD, INeutronRouterAware> {
62     static final String ROUTER_INTERFACE_STR = "network:router_interface";
63     static final String ROUTER_GATEWAY_STR = "network:router_gateway";
64     private static final String RESOURCE_NAME = "Router";
65
66     @Override
67     protected String getResourceName() {
68         return RESOURCE_NAME;
69     }
70
71     @Override
72     protected NeutronRouter extractFields(NeutronRouter o, List<String> fields) {
73         return o.extractFields(fields);
74     }
75
76     private NeutronCRUDInterfaces getNeutronInterfaces(boolean flag) {
77         NeutronCRUDInterfaces answer = new NeutronCRUDInterfaces().fetchINeutronRouterCRUD(this);
78         if (answer.getRouterInterface() == null) {
79             throw new ServiceUnavailableException(serviceUnavailable());
80         }
81         if (flag) {
82             answer = answer.fetchINeutronNetworkCRUD(this);
83             if (answer.getNetworkInterface() == null) {
84                 throw new ServiceUnavailableException("Network CRUD Interface "
85                     + RestMessages.SERVICEUNAVAILABLE.toString());
86             }
87         }
88         return answer;
89     }
90
91     @Override
92     protected INeutronRouterCRUD getNeutronCRUD() {
93         return getNeutronInterfaces(false).getRouterInterface();
94     }
95
96     private NeutronCRUDInterfaces getAttachInterfaces() {
97         NeutronCRUDInterfaces answer = new NeutronCRUDInterfaces().fetchINeutronRouterCRUD(this);
98         if (answer.getRouterInterface() == null) {
99             throw new ServiceUnavailableException(serviceUnavailable());
100         }
101         answer = answer.fetchINeutronPortCRUD(this).fetchINeutronSubnetCRUD(this);
102         if (answer.getPortInterface() == null) {
103             throw new ServiceUnavailableException("Port CRUD Interface "
104                     + RestMessages.SERVICEUNAVAILABLE.toString());
105         }
106         if (answer.getSubnetInterface() == null) {
107             throw new ServiceUnavailableException("Subnet CRUD Interface "
108                     + RestMessages.SERVICEUNAVAILABLE.toString());
109         }
110         return answer;
111     }
112
113     @Override
114     protected NeutronRouterRequest newNeutronRequest(NeutronRouter o) {
115         return new NeutronRouterRequest(o);
116     }
117
118     @Override
119     protected Object[] getInstances() {
120         return NeutronUtil.getInstances(INeutronRouterAware.class, this);
121     }
122
123     @Override
124     protected int canCreate(Object instance, NeutronRouter singleton) {
125         INeutronRouterAware service = (INeutronRouterAware) instance;
126         return service.canCreateRouter(singleton);
127     }
128
129     @Override
130     protected void created(Object instance, NeutronRouter singleton) {
131         INeutronRouterAware service = (INeutronRouterAware) instance;
132         service.neutronRouterCreated(singleton);
133     }
134
135     @Override
136     protected int canUpdate(Object instance, NeutronRouter delta, NeutronRouter original) {
137         INeutronRouterAware service = (INeutronRouterAware) instance;
138         return service.canUpdateRouter(delta, original);
139     }
140
141     @Override
142     protected void updated(Object instance, NeutronRouter updated) {
143         INeutronRouterAware service = (INeutronRouterAware) instance;
144         service.neutronRouterUpdated(updated);
145     }
146
147     @Override
148     protected int canDelete(Object instance, NeutronRouter singleton) {
149         INeutronRouterAware service = (INeutronRouterAware) instance;
150         return service.canDeleteRouter(singleton);
151     }
152
153     @Override
154     protected void deleted(Object instance, NeutronRouter singleton) {
155         INeutronRouterAware service = (INeutronRouterAware) instance;
156         service.neutronRouterDeleted(singleton);
157     }
158
159     /**
160      * Returns a list of all Routers */
161
162     @GET
163     @Produces({ MediaType.APPLICATION_JSON })
164     //@TypeHint(OpenStackRouters.class)
165     @StatusCodes({
166             @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
167             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
168             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
169             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
170     public Response listRouters(
171             // return fields
172             @QueryParam("fields") List<String> fields,
173             // note: openstack isn't clear about filtering on lists, so we aren't handling them
174             @QueryParam("id") String queryID,
175             @QueryParam("name") String queryName,
176             @QueryParam("admin_state_up") String queryAdminStateUp,
177             @QueryParam("status") String queryStatus,
178             @QueryParam("tenant_id") String queryTenantID,
179             @QueryParam("external_gateway_info") String queryExternalGatewayInfo,
180             // pagination
181             @QueryParam("limit") String limit,
182             @QueryParam("marker") String marker,
183             @QueryParam("page_reverse") String pageReverse
184             // sorting not supported
185             ) {
186         INeutronRouterCRUD routerInterface = getNeutronInterfaces(false).getRouterInterface();
187         if (routerInterface == null) {
188             throw new ServiceUnavailableException(serviceUnavailable());
189         }
190         List<NeutronRouter> allRouters = routerInterface.getAllRouters();
191         List<NeutronRouter> ans = new ArrayList<NeutronRouter>();
192         Iterator<NeutronRouter> i = allRouters.iterator();
193         while (i.hasNext()) {
194             NeutronRouter oSS = i.next();
195             if ((queryID == null || queryID.equals(oSS.getID())) &&
196                     (queryName == null || queryName.equals(oSS.getName())) &&
197                     (queryAdminStateUp == null || queryAdminStateUp.equals(oSS.getAdminStateUp())) &&
198                     (queryStatus == null || queryStatus.equals(oSS.getStatus())) &&
199                     (queryExternalGatewayInfo == null || queryExternalGatewayInfo.equals(oSS.getExternalGatewayInfo())) &&
200                     (queryTenantID == null || queryTenantID.equals(oSS.getTenantID()))) {
201                 if (fields.size() > 0) {
202                     ans.add(extractFields(oSS,fields));
203                 } else {
204                     ans.add(oSS);
205                 }
206             }
207         }
208         //TODO: apply pagination to results
209         return Response.status(HttpURLConnection.HTTP_OK).entity(
210                 new NeutronRouterRequest(ans)).build();
211     }
212
213     /**
214      * Returns a specific Router */
215
216     @Path("{routerUUID}")
217     @GET
218     @Produces({ MediaType.APPLICATION_JSON })
219     //@TypeHint(OpenStackRouters.class)
220     @StatusCodes({
221             @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
222             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
223             @ResponseCode(code = HttpURLConnection.HTTP_FORBIDDEN, condition = "Forbidden"),
224             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
225             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
226             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
227     public Response showRouter(
228             @PathParam("routerUUID") String routerUUID,
229             // return fields
230             @QueryParam("fields") List<String> fields) {
231         return show(routerUUID, fields);
232     }
233
234     /**
235      * Creates new Routers */
236
237     @POST
238     @Produces({ MediaType.APPLICATION_JSON })
239     @Consumes({ MediaType.APPLICATION_JSON })
240     //@TypeHint(OpenStackRouters.class)
241     @StatusCodes({
242             @ResponseCode(code = HttpURLConnection.HTTP_CREATED, condition = "Created"),
243             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
244     public Response createRouters(final NeutronRouterRequest input) {
245         getNeutronInterfaces(true); // ensure that network service is loaded
246         return create(input);
247     }
248
249     @Override
250     protected void updateDelta(String uuid, NeutronRouter delta, NeutronRouter original) {
251         delta.setID(uuid);
252         delta.setTenantID(original.getTenantID());
253     }
254
255     /**
256      * Updates a Router */
257
258     @Path("{routerUUID}")
259     @PUT
260     @Produces({ MediaType.APPLICATION_JSON })
261     @Consumes({ MediaType.APPLICATION_JSON })
262     //@TypeHint(OpenStackRouters.class)
263     @StatusCodes({
264             @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
265             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
266     public Response updateRouter(
267             @PathParam("routerUUID") String routerUUID,
268             NeutronRouterRequest input
269             ) {
270         getNeutronInterfaces(true); // ensure that network service is loaded
271         return update(routerUUID, input);
272     }
273
274     /**
275      * Deletes a Router */
276
277     @Path("{routerUUID}")
278     @DELETE
279     @StatusCodes({
280             @ResponseCode(code = HttpURLConnection.HTTP_NO_CONTENT, condition = "No Content"),
281             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
282     public Response deleteRouter(
283             @PathParam("routerUUID") String routerUUID) {
284         return delete(routerUUID);
285     }
286
287     /**
288      * Adds an interface to a router */
289
290     @Path("{routerUUID}/add_router_interface")
291     @PUT
292     @Produces({ MediaType.APPLICATION_JSON })
293     @Consumes({ MediaType.APPLICATION_JSON })
294     //@TypeHint(OpenStackRouterInterfaces.class)
295     @StatusCodes({
296             @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
297             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
298     public Response addRouterInterface(
299             @PathParam("routerUUID") String routerUUID,
300             NeutronRouter_Interface input
301             ) {
302         NeutronCRUDInterfaces interfaces = getAttachInterfaces();
303         INeutronRouterCRUD routerInterface = interfaces.getRouterInterface();
304
305         NeutronRouter target = routerInterface.getRouter(routerUUID);
306         Object[] instances = getInstances();
307         if (instances != null) {
308             for (Object instance : instances) {
309                 INeutronRouterAware service = (INeutronRouterAware) instance;
310                 int status = service.canAttachInterface(target, input);
311                 if (status < HTTP_OK_BOTTOM || status > HTTP_OK_TOP) {
312                     return Response.status(status).build();
313                 }
314             }
315         }
316
317         target.addInterface(input.getPortUUID(), input);
318         if (instances != null) {
319             for (Object instance : instances) {
320                 INeutronRouterAware service = (INeutronRouterAware) instance;
321                 service.neutronRouterInterfaceAttached(target, input);
322             }
323         }
324
325         return Response.status(HttpURLConnection.HTTP_OK).entity(input).build();
326     }
327
328
329     private int checkDownstreamDetach(NeutronRouter target, NeutronRouter_Interface input) {
330         Object[] instances = getInstances();
331         if (instances != null) {
332             for (Object instance : instances) {
333                 INeutronRouterAware service = (INeutronRouterAware) instance;
334                 int status = service.canDetachInterface(target, input);
335                 if (status < HTTP_OK_BOTTOM || status > HTTP_OK_TOP) {
336                     return status;
337                 }
338             }
339         }
340         return HTTP_OK_BOTTOM;
341     }
342
343     /**
344      * Removes an interface to a router */
345
346     @Path("{routerUUID}/remove_router_interface")
347     @PUT
348     @Produces({ MediaType.APPLICATION_JSON })
349     @Consumes({ MediaType.APPLICATION_JSON })
350     //@TypeHint(OpenStackRouterInterfaces.class)
351     @StatusCodes({
352             @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
353             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
354     public Response removeRouterInterface(
355             @PathParam("routerUUID") String routerUUID,
356             NeutronRouter_Interface input
357             ) {
358         NeutronCRUDInterfaces interfaces = getAttachInterfaces();
359         INeutronRouterCRUD routerInterface = interfaces.getRouterInterface();
360         Object[] instances = getInstances();
361
362         NeutronRouter target = routerInterface.getRouter(routerUUID);
363         input.setID(target.getID());
364         input.setTenantID(target.getTenantID());
365         int status = checkDownstreamDetach(target, input);
366         if (status != HTTP_OK_BOTTOM) {
367             return Response.status(status).build();
368         }
369         target.removeInterface(input.getPortUUID());
370         if (instances != null) {
371             for (Object instance : instances) {
372                 INeutronRouterAware service = (INeutronRouterAware) instance;
373                 service.neutronRouterInterfaceDetached(target, input);
374             }
375         }
376         return Response.status(HttpURLConnection.HTTP_OK).entity(input).build();
377     }
378 }