2 * Copyright (c) 2013, 2015 IBM Corporation and others. All rights reserved.
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
9 package org.opendaylight.neutron.northbound.api;
11 import java.net.HttpURLConnection;
13 import java.util.ArrayList;
14 import java.util.Iterator;
15 import java.util.List;
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;
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;
36 * Neutron Northbound REST APIs.<br>
37 * This class provides REST APIs for managing Neutron Floating IPs
41 * Authentication scheme : <b>HTTP Basic</b><br>
42 * Authentication realm : <b>opendaylight</b><br>
43 * Transport : <b>HTTP and HTTPS</b><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>
49 * http://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html#Configuration
54 public class NeutronFloatingIPsNorthbound
55 extends AbstractNeutronNorthbound<NeutronFloatingIP, NeutronFloatingIPRequest, INeutronFloatingIPCRUD> {
56 private static final String RESOURCE_NAME = "Floating IP";
59 protected String getResourceName() {
64 protected NeutronFloatingIP extractFields(NeutronFloatingIP o, List<String> fields) {
65 return o.extractFields(fields);
69 protected NeutronFloatingIPRequest newNeutronRequest(NeutronFloatingIP o) {
70 return new NeutronFloatingIPRequest(o);
73 private NeutronCRUDInterfaces getNeutronInterfaces(boolean flag) {
74 NeutronCRUDInterfaces answer = new NeutronCRUDInterfaces().fetchINeutronFloatingIPCRUD(this);
75 if (answer.getFloatingIPInterface() == null) {
76 throw new ServiceUnavailableException(serviceUnavailable());
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());
84 if (answer.getSubnetInterface() == null) {
85 throw new ServiceUnavailableException("Subnet CRUD Interface "
86 + RestMessages.SERVICEUNAVAILABLE.toString());
88 if (answer.getPortInterface() == null) {
89 throw new ServiceUnavailableException("Port CRUD Interface "
90 + RestMessages.SERVICEUNAVAILABLE.toString());
97 protected INeutronFloatingIPCRUD getNeutronCRUD() {
98 NeutronCRUDInterfaces answer = getNeutronInterfaces(false);
99 return answer.getFloatingIPInterface();
103 * Returns a list of all FloatingIPs */
106 @Produces({ MediaType.APPLICATION_JSON })
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(
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,
125 @QueryParam("limit") String limit,
126 @QueryParam("marker") String marker,
127 @QueryParam("page_reverse") String pageReverse
128 // sorting not supported
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));
152 //TODO: apply pagination to results
153 return Response.status(HttpURLConnection.HTTP_OK).entity(
154 new NeutronFloatingIPRequest(ans)).build();
158 * Returns a specific FloatingIP */
160 @Path("{floatingipUUID}")
162 @Produces({ MediaType.APPLICATION_JSON })
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,
172 @QueryParam("fields") List<String> fields ) {
173 return show(floatingipUUID, fields);
177 * Creates new FloatingIPs */
180 @Produces({ MediaType.APPLICATION_JSON })
181 @Consumes({ MediaType.APPLICATION_JSON })
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);
190 * Updates a FloatingIP */
192 @Path("{floatingipUUID}")
194 @Produces({ MediaType.APPLICATION_JSON })
195 @Consumes({ MediaType.APPLICATION_JSON })
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
204 return update(floatingipUUID, input);
208 * Deletes a FloatingIP */
210 @Path("{floatingipUUID}")
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);