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