Checkstyle formatting issues fix (Northbound API)
[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 NeutronFloatingIP extractFields(NeutronFloatingIP o, List<String> fields) {
62         return o.extractFields(fields);
63     }
64
65     @Override
66     protected NeutronFloatingIPRequest newNeutronRequest(NeutronFloatingIP o) {
67         return new NeutronFloatingIPRequest(o);
68     }
69
70     private NeutronCRUDInterfaces getNeutronInterfaces(boolean flag) {
71         NeutronCRUDInterfaces answer = new NeutronCRUDInterfaces().fetchINeutronFloatingIPCRUD(this);
72         if (answer.getFloatingIPInterface() == null) {
73             throw new ServiceUnavailableException(serviceUnavailable());
74         }
75         if (flag) {
76             answer = answer.fetchINeutronNetworkCRUD(this).fetchINeutronSubnetCRUD(this).fetchINeutronPortCRUD(this);
77             if (answer.getNetworkInterface() == null) {
78                 throw new ServiceUnavailableException(
79                         "Network CRUD Interface " + RestMessages.SERVICEUNAVAILABLE.toString());
80             }
81             if (answer.getSubnetInterface() == null) {
82                 throw new ServiceUnavailableException(
83                         "Subnet CRUD Interface " + RestMessages.SERVICEUNAVAILABLE.toString());
84             }
85             if (answer.getPortInterface() == null) {
86                 throw new ServiceUnavailableException(
87                         "Port CRUD Interface " + RestMessages.SERVICEUNAVAILABLE.toString());
88             }
89         }
90         return answer;
91     }
92
93     @Override
94     protected INeutronFloatingIPCRUD getNeutronCRUD() {
95         NeutronCRUDInterfaces answer = getNeutronInterfaces(false);
96         return answer.getFloatingIPInterface();
97     }
98
99     /**
100      * Returns a list of all FloatingIPs */
101
102     @GET
103     @Produces({ MediaType.APPLICATION_JSON })
104     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
105             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
106             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
107             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
108     public Response listFloatingIPs(
109             // return fields
110             @QueryParam("fields") List<String> fields,
111             // note: openstack isn't clear about filtering on lists, so we aren't handling them
112             @QueryParam("id") String queryID,
113             @QueryParam("floating_network_id") String queryFloatingNetworkId,
114             @QueryParam("port_id") String queryPortId,
115             @QueryParam("fixed_ip_address") String queryFixedIPAddress,
116             @QueryParam("floating_ip_address") String queryFloatingIPAddress,
117             @QueryParam("tenant_id") String queryTenantID,
118             @QueryParam("router_id") String queryRouterID,
119             @QueryParam("status") String queryStatus,
120             // pagination
121             @QueryParam("limit") String limit,
122             @QueryParam("marker") String marker,
123             @QueryParam("page_reverse") String pageReverse
124     // sorting not supported
125     ) {
126         INeutronFloatingIPCRUD floatingIPInterface = getNeutronInterfaces(false).getFloatingIPInterface();
127         List<NeutronFloatingIP> allFloatingIPs = floatingIPInterface.getAll();
128         List<NeutronFloatingIP> ans = new ArrayList<>();
129         Iterator<NeutronFloatingIP> i = allFloatingIPs.iterator();
130         while (i.hasNext()) {
131             NeutronFloatingIP oSS = i.next();
132             //match filters: TODO provider extension and router extension
133             if ((queryID == null || queryID.equals(oSS.getID()))
134                     && (queryFloatingNetworkId == null || queryFloatingNetworkId.equals(oSS.getFloatingNetworkUUID()))
135                     && (queryPortId == null || queryPortId.equals(oSS.getPortUUID()))
136                     && (queryFixedIPAddress == null || queryFixedIPAddress.equals(oSS.getFixedIPAddress()))
137                     && (queryFloatingIPAddress == null || queryFloatingIPAddress.equals(oSS.getFloatingIPAddress()))
138                     && (queryStatus == null || queryStatus.equals(oSS.getStatus()))
139                     && (queryRouterID == null || queryRouterID.equals(oSS.getRouterUUID()))
140                     && (queryTenantID == null || queryTenantID.equals(oSS.getTenantID()))) {
141                 if (fields.size() > 0) {
142                     ans.add(extractFields(oSS, fields));
143                 } else {
144                     ans.add(oSS);
145                 }
146             }
147         }
148         //TODO: apply pagination to results
149         return Response.status(HttpURLConnection.HTTP_OK).entity(new NeutronFloatingIPRequest(ans)).build();
150     }
151
152     /**
153      * Returns a specific FloatingIP */
154
155     @Path("{floatingipUUID}")
156     @GET
157     @Produces({ MediaType.APPLICATION_JSON })
158     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
159             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
160             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
161             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
162             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
163     public Response showFloatingIP(@PathParam("floatingipUUID") String floatingipUUID,
164             // return fields
165             @QueryParam("fields") List<String> fields) {
166         return show(floatingipUUID, fields);
167     }
168
169     /**
170      * Creates new FloatingIPs */
171
172     @POST
173     @Produces({ MediaType.APPLICATION_JSON })
174     @Consumes({ MediaType.APPLICATION_JSON })
175     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_CREATED, condition = "Created"),
176             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
177     public Response createFloatingIPs(final NeutronFloatingIPRequest input) {
178         return create(input);
179     }
180
181     /**
182      * Updates a FloatingIP */
183
184     @Path("{floatingipUUID}")
185     @PUT
186     @Produces({ MediaType.APPLICATION_JSON })
187     @Consumes({ MediaType.APPLICATION_JSON })
188     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
189             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
190             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
191     public Response updateFloatingIP(@PathParam("floatingipUUID") String floatingipUUID,
192             NeutronFloatingIPRequest input) {
193         return update(floatingipUUID, input);
194     }
195
196     /**
197      * Deletes a FloatingIP */
198
199     @Path("{floatingipUUID}")
200     @DELETE
201     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_NO_CONTENT, condition = "No Content"),
202             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
203             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
204     public Response deleteFloatingIP(@PathParam("floatingipUUID") String floatingipUUID) {
205         return delete(floatingipUUID);
206     }
207 }