37e3cf2463e35bd96df89f6422dbf8df078e8f6a
[neutron.git] / northbound-api / src / main / java / org / opendaylight / neutron / northbound / api / NeutronFloatingIPsNorthbound.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.INeutronFloatingIPCRUD;
32 import org.opendaylight.neutron.spi.NeutronCRUDInterfaces;
33 import org.opendaylight.neutron.spi.NeutronFloatingIP;
34
35 /**
36  * Neutron Northbound REST APIs.<br>
37  * This class provides REST APIs for managing Neutron Floating IPs
38  *
39  * <br>
40  * <br>
41  * Authentication scheme : <b>HTTP Basic</b><br>
42  * Authentication realm : <b>opendaylight</b><br>
43  * Transport : <b>HTTP and HTTPS</b><br>
44  * <br>
45  * HTTPS Authentication is disabled by default. Administrator can enable it in
46  * tomcat-server.xml after adding a proper keystore / SSL certificate from a
47  * trusted authority.<br>
48  * More info :
49  * http://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html#Configuration
50  *
51  */
52
53 @Path("/floatingips")
54 public class NeutronFloatingIPsNorthbound
55     extends AbstractNeutronNorthbound<NeutronFloatingIP, NeutronFloatingIPRequest, INeutronFloatingIPCRUD> {
56     private static final String RESOURCE_NAME = "Floating IP";
57
58     @Override
59     protected String getResourceName() {
60         return RESOURCE_NAME;
61     }
62
63     @Override
64     protected NeutronFloatingIP extractFields(NeutronFloatingIP o, List<String> fields) {
65         return o.extractFields(fields);
66     }
67
68     @Override
69     protected NeutronFloatingIPRequest newNeutronRequest(NeutronFloatingIP o) {
70         return new NeutronFloatingIPRequest(o);
71     }
72
73     private NeutronCRUDInterfaces getNeutronInterfaces(boolean flag) {
74         NeutronCRUDInterfaces answer = new NeutronCRUDInterfaces().fetchINeutronFloatingIPCRUD(this);
75         if (answer.getFloatingIPInterface() == null) {
76             throw new ServiceUnavailableException(serviceUnavailable());
77         }
78         if (flag) {
79             answer = answer.fetchINeutronNetworkCRUD(this).fetchINeutronSubnetCRUD(this).fetchINeutronPortCRUD(this);
80             if (answer.getNetworkInterface() == null) {
81                 throw new ServiceUnavailableException("Network CRUD Interface "
82                         + RestMessages.SERVICEUNAVAILABLE.toString());
83             }
84             if (answer.getSubnetInterface() == null) {
85                 throw new ServiceUnavailableException("Subnet CRUD Interface "
86                         + RestMessages.SERVICEUNAVAILABLE.toString());
87             }
88             if (answer.getPortInterface() == null) {
89                 throw new ServiceUnavailableException("Port CRUD Interface "
90                         + RestMessages.SERVICEUNAVAILABLE.toString());
91             }
92         }
93         return answer;
94     }
95
96     @Override
97     protected INeutronFloatingIPCRUD getNeutronCRUD() {
98         NeutronCRUDInterfaces answer = getNeutronInterfaces(false);
99         return answer.getFloatingIPInterface();
100     }
101
102     /**
103      * Returns a list of all FloatingIPs */
104
105     @GET
106     @Produces({ MediaType.APPLICATION_JSON })
107     @StatusCodes({
108             @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
109             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
110             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
111             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
112     public Response listFloatingIPs(
113             // return fields
114             @QueryParam("fields") List<String> fields,
115             // note: openstack isn't clear about filtering on lists, so we aren't handling them
116             @QueryParam("id") String queryID,
117             @QueryParam("floating_network_id") String queryFloatingNetworkId,
118             @QueryParam("port_id") String queryPortId,
119             @QueryParam("fixed_ip_address") String queryFixedIPAddress,
120             @QueryParam("floating_ip_address") String queryFloatingIPAddress,
121             @QueryParam("tenant_id") String queryTenantID,
122             @QueryParam("router_id") String queryRouterID,
123             @QueryParam("status") String queryStatus,
124             // pagination
125             @QueryParam("limit") String limit,
126             @QueryParam("marker") String marker,
127             @QueryParam("page_reverse") String pageReverse
128             // sorting not supported
129             ) {
130         INeutronFloatingIPCRUD floatingIPInterface = getNeutronInterfaces(false).getFloatingIPInterface();
131         List<NeutronFloatingIP> allFloatingIPs = floatingIPInterface.getAll();
132         List<NeutronFloatingIP> ans = new ArrayList<NeutronFloatingIP>();
133         Iterator<NeutronFloatingIP> i = allFloatingIPs.iterator();
134         while (i.hasNext()) {
135             NeutronFloatingIP oSS = i.next();
136             //match filters: TODO provider extension and router extension
137             if ((queryID == null || queryID.equals(oSS.getID())) &&
138                     (queryFloatingNetworkId == null || queryFloatingNetworkId.equals(oSS.getFloatingNetworkUUID())) &&
139                     (queryPortId == null || queryPortId.equals(oSS.getPortUUID())) &&
140                     (queryFixedIPAddress == null || queryFixedIPAddress.equals(oSS.getFixedIPAddress())) &&
141                     (queryFloatingIPAddress == null || queryFloatingIPAddress.equals(oSS.getFloatingIPAddress())) &&
142                     (queryStatus == null || queryStatus.equals(oSS.getStatus())) &&
143                     (queryRouterID == null || queryRouterID.equals(oSS.getRouterUUID())) &&
144                     (queryTenantID == null || queryTenantID.equals(oSS.getTenantID()))) {
145                 if (fields.size() > 0) {
146                     ans.add(extractFields(oSS,fields));
147                 } else {
148                     ans.add(oSS);
149                 }
150             }
151         }
152         //TODO: apply pagination to results
153         return Response.status(HttpURLConnection.HTTP_OK).entity(
154                 new NeutronFloatingIPRequest(ans)).build();
155     }
156
157     /**
158      * Returns a specific FloatingIP */
159
160     @Path("{floatingipUUID}")
161     @GET
162     @Produces({ MediaType.APPLICATION_JSON })
163     @StatusCodes({
164             @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
165             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
166             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
167             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
168             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
169     public Response showFloatingIP(
170             @PathParam("floatingipUUID") String floatingipUUID,
171             // return fields
172             @QueryParam("fields") List<String> fields ) {
173         return show(floatingipUUID, fields);
174     }
175
176     /**
177      * Creates new FloatingIPs */
178
179     @POST
180     @Produces({ MediaType.APPLICATION_JSON })
181     @Consumes({ MediaType.APPLICATION_JSON })
182     @StatusCodes({
183         @ResponseCode(code = HttpURLConnection.HTTP_CREATED, condition = "Created"),
184         @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
185     public Response createFloatingIPs(final NeutronFloatingIPRequest input) {
186         return create(input);
187     }
188
189     /**
190      * Updates a FloatingIP */
191
192     @Path("{floatingipUUID}")
193     @PUT
194     @Produces({ MediaType.APPLICATION_JSON })
195     @Consumes({ MediaType.APPLICATION_JSON })
196     @StatusCodes({
197             @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
198             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
199             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
200     public Response updateFloatingIP(
201             @PathParam("floatingipUUID") String floatingipUUID,
202             NeutronFloatingIPRequest input
203             ) {
204         return update(floatingipUUID, input);
205     }
206
207     /**
208      * Deletes a FloatingIP */
209
210     @Path("{floatingipUUID}")
211     @DELETE
212     @StatusCodes({
213             @ResponseCode(code = HttpURLConnection.HTTP_NO_CONTENT, condition = "No Content"),
214             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
215             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
216     public Response deleteFloatingIP(
217             @PathParam("floatingipUUID") String floatingipUUID) {
218         return delete(floatingipUUID);
219     }
220 }