1e686f13a68a27f848a65d84052c7a8ed7c25cc2
[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.List;
14 import javax.ws.rs.Consumes;
15 import javax.ws.rs.DELETE;
16 import javax.ws.rs.GET;
17 import javax.ws.rs.POST;
18 import javax.ws.rs.PUT;
19 import javax.ws.rs.Path;
20 import javax.ws.rs.PathParam;
21 import javax.ws.rs.Produces;
22 import javax.ws.rs.QueryParam;
23 import javax.ws.rs.core.MediaType;
24 import javax.ws.rs.core.Response;
25 import org.codehaus.enunciate.jaxrs.ResponseCode;
26 import org.codehaus.enunciate.jaxrs.StatusCodes;
27 import org.opendaylight.neutron.spi.INeutronFloatingIpCRUD;
28 import org.opendaylight.neutron.spi.NeutronFloatingIp;
29
30 /**
31  * Neutron Northbound REST APIs.<br>
32  * This class provides REST APIs for managing Neutron Floating IPs
33  *
34  * <br>
35  * <br>
36  * Authentication scheme : <b>HTTP Basic</b><br>
37  * Authentication realm : <b>opendaylight</b><br>
38  * Transport : <b>HTTP and HTTPS</b><br>
39  * <br>
40  * HTTPS Authentication is disabled by default. Administrator can enable it in
41  * tomcat-server.xml after adding a proper keystore / SSL certificate from a
42  * trusted authority.<br>
43  * More info :
44  * http://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html#Configuration
45  *
46  */
47
48 @Path("/floatingips")
49 public final class NeutronFloatingIpsNorthbound
50         extends AbstractNeutronNorthbound<NeutronFloatingIp, NeutronFloatingIpRequest, INeutronFloatingIpCRUD> {
51     private static final String RESOURCE_NAME = "Floating IP";
52
53     @Override
54     protected String getResourceName() {
55         return RESOURCE_NAME;
56     }
57
58     /**
59      * Returns a list of all FloatingIps.
60      */
61
62     @GET
63     @Produces({ MediaType.APPLICATION_JSON })
64     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
65             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
66             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
67             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
68     public Response listFloatingIps(
69             // return fields
70             @QueryParam("fields") List<String> fields,
71             // note: openstack isn't clear about filtering on lists, so we aren't handling them
72             @QueryParam("id") String queryID,
73             @QueryParam("floating_network_id") String queryFloatingNetworkId,
74             @QueryParam("port_id") String queryPortId,
75             @QueryParam("fixed_ip_address") String queryFixedIpAddress,
76             @QueryParam("floating_ip_address") String queryFloatingIpAddress,
77             @QueryParam("tenant_id") String queryTenantID,
78             @QueryParam("router_id") String queryRouterID,
79             @QueryParam("status") String queryStatus,
80             // pagination
81             @QueryParam("limit") String limit,
82             @QueryParam("marker") String marker,
83             @QueryParam("page_reverse") String pageReverse
84     // sorting not supported
85     ) {
86         INeutronFloatingIpCRUD floatingIpInterface = getNeutronCRUD();
87         List<NeutronFloatingIp> allFloatingIps = floatingIpInterface.getAll();
88         List<NeutronFloatingIp> ans = new ArrayList<>();
89         for (NeutronFloatingIp floatingIp : allFloatingIps) {
90             //match filters: TODO provider extension and router extension
91             if ((queryID == null || queryID.equals(floatingIp.getID()))
92                     && (queryFloatingNetworkId == null
93                         || queryFloatingNetworkId.equals(floatingIp.getFloatingNetworkUUID()))
94                     && (queryPortId == null || queryPortId.equals(floatingIp.getPortUUID()))
95                     && (queryFixedIpAddress == null || queryFixedIpAddress.equals(floatingIp.getFixedIpAddress()))
96                     && (queryFloatingIpAddress == null
97                         || queryFloatingIpAddress.equals(floatingIp.getFloatingIpAddress()))
98                     && (queryStatus == null || queryStatus.equals(floatingIp.getStatus()))
99                     && (queryRouterID == null || queryRouterID.equals(floatingIp.getRouterUUID()))
100                     && (queryTenantID == null || queryTenantID.equals(floatingIp.getTenantID()))) {
101                 if (fields.size() > 0) {
102                     ans.add(floatingIp.extractFields(fields));
103                 } else {
104                     ans.add(floatingIp);
105                 }
106             }
107         }
108         //TODO: apply pagination to results
109         return Response.status(HttpURLConnection.HTTP_OK).entity(new NeutronFloatingIpRequest(ans)).build();
110     }
111
112     /**
113      * Returns a specific FloatingIp.
114      */
115
116     @Path("{floatingipUUID}")
117     @GET
118     @Produces({ MediaType.APPLICATION_JSON })
119     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
120             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
121             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
122             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
123             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
124     public Response showFloatingIp(@PathParam("floatingipUUID") String floatingipUUID,
125             // return fields
126             @QueryParam("fields") List<String> fields) {
127         return show(floatingipUUID, fields);
128     }
129
130     /**
131      * Creates new FloatingIps.
132      */
133
134     @POST
135     @Produces({ MediaType.APPLICATION_JSON })
136     @Consumes({ MediaType.APPLICATION_JSON })
137     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_CREATED, condition = "Created"),
138             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
139     public Response createFloatingIps(final NeutronFloatingIpRequest input) {
140         return create(input);
141     }
142
143     /**
144      * Updates a FloatingIp.
145      */
146
147     @Path("{floatingipUUID}")
148     @PUT
149     @Produces({ MediaType.APPLICATION_JSON })
150     @Consumes({ MediaType.APPLICATION_JSON })
151     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
152             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
153             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
154     public Response updateFloatingIp(@PathParam("floatingipUUID") String floatingipUUID,
155             NeutronFloatingIpRequest input) {
156         return update(floatingipUUID, input);
157     }
158
159     /**
160      * Deletes a FloatingIp.
161      */
162
163     @Path("{floatingipUUID}")
164     @DELETE
165     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_NO_CONTENT, condition = "No Content"),
166             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
167             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
168     public Response deleteFloatingIp(@PathParam("floatingipUUID") String floatingipUUID) {
169         return delete(floatingipUUID);
170     }
171 }