complete dependency inject CRUD implementations
[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 package org.opendaylight.neutron.northbound.api;
9
10 import java.net.HttpURLConnection;
11 import java.util.ArrayList;
12 import java.util.List;
13 import javax.inject.Inject;
14 import javax.inject.Singleton;
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.NeutronFloatingIp;
30 import org.ops4j.pax.cdi.api.OsgiService;
31
32 /**
33  * Neutron Northbound REST APIs for managing Neutron Floating IPs.
34  */
35 @Singleton
36 @Path("/floatingips")
37 public final class NeutronFloatingIpsNorthbound
38         extends AbstractNeutronNorthbound<NeutronFloatingIp, NeutronFloatingIpRequest, INeutronFloatingIpCRUD> {
39
40     private static final String RESOURCE_NAME = "Floating IP";
41
42     @Inject
43     public NeutronFloatingIpsNorthbound(@OsgiService INeutronFloatingIpCRUD neutronCRUD) {
44         super(neutronCRUD);
45     }
46
47     @Override
48     protected String getResourceName() {
49         return RESOURCE_NAME;
50     }
51
52     /**
53      * Returns a list of all FloatingIps.
54      */
55     @GET
56     @Produces({ MediaType.APPLICATION_JSON })
57     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
58             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
59             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
60             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
61     public Response listFloatingIps(
62             // return fields
63             @QueryParam("fields") List<String> fields,
64             // note: openstack isn't clear about filtering on lists, so we aren't handling them
65             @QueryParam("id") String queryID,
66             @QueryParam("floating_network_id") String queryFloatingNetworkId,
67             @QueryParam("port_id") String queryPortId,
68             @QueryParam("fixed_ip_address") String queryFixedIpAddress,
69             @QueryParam("floating_ip_address") String queryFloatingIpAddress,
70             @QueryParam("tenant_id") String queryTenantID,
71             @QueryParam("router_id") String queryRouterID,
72             @QueryParam("status") String queryStatus,
73             // pagination
74             @QueryParam("limit") String limit,
75             @QueryParam("marker") String marker,
76             @QueryParam("page_reverse") String pageReverse
77     // sorting not supported
78     ) {
79         INeutronFloatingIpCRUD floatingIpInterface = getNeutronCRUD();
80         List<NeutronFloatingIp> allFloatingIps = floatingIpInterface.getAll();
81         List<NeutronFloatingIp> ans = new ArrayList<>();
82         for (NeutronFloatingIp floatingIp : allFloatingIps) {
83             //match filters: TODO provider extension and router extension
84             if ((queryID == null || queryID.equals(floatingIp.getID()))
85                     && (queryFloatingNetworkId == null
86                         || queryFloatingNetworkId.equals(floatingIp.getFloatingNetworkUUID()))
87                     && (queryPortId == null || queryPortId.equals(floatingIp.getPortUUID()))
88                     && (queryFixedIpAddress == null || queryFixedIpAddress.equals(floatingIp.getFixedIpAddress()))
89                     && (queryFloatingIpAddress == null
90                         || queryFloatingIpAddress.equals(floatingIp.getFloatingIpAddress()))
91                     && (queryStatus == null || queryStatus.equals(floatingIp.getStatus()))
92                     && (queryRouterID == null || queryRouterID.equals(floatingIp.getRouterUUID()))
93                     && (queryTenantID == null || queryTenantID.equals(floatingIp.getTenantID()))) {
94                 if (fields.size() > 0) {
95                     ans.add(floatingIp.extractFields(fields));
96                 } else {
97                     ans.add(floatingIp);
98                 }
99             }
100         }
101         //TODO: apply pagination to results
102         return Response.status(HttpURLConnection.HTTP_OK).entity(new NeutronFloatingIpRequest(ans)).build();
103     }
104
105     /**
106      * Returns a specific FloatingIp.
107      */
108     @Path("{floatingipUUID}")
109     @GET
110     @Produces({ MediaType.APPLICATION_JSON })
111     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
112             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
113             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
114             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
115             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
116     public Response showFloatingIp(@PathParam("floatingipUUID") String floatingipUUID,
117             // return fields
118             @QueryParam("fields") List<String> fields) {
119         return show(floatingipUUID, fields);
120     }
121
122     /**
123      * Creates new FloatingIps.
124      */
125     @POST
126     @Produces({ MediaType.APPLICATION_JSON })
127     @Consumes({ MediaType.APPLICATION_JSON })
128     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_CREATED, condition = "Created"),
129             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
130     public Response createFloatingIps(final NeutronFloatingIpRequest input) {
131         return create(input);
132     }
133
134     /**
135      * Updates a FloatingIp.
136      */
137     @Path("{floatingipUUID}")
138     @PUT
139     @Produces({ MediaType.APPLICATION_JSON })
140     @Consumes({ MediaType.APPLICATION_JSON })
141     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
142             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
143             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
144     public Response updateFloatingIp(@PathParam("floatingipUUID") String floatingipUUID,
145             NeutronFloatingIpRequest input) {
146         return update(floatingipUUID, input);
147     }
148
149     /**
150      * Deletes a FloatingIp.
151      */
152     @Path("{floatingipUUID}")
153     @DELETE
154     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_NO_CONTENT, condition = "No Content"),
155             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
156             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
157     public Response deleteFloatingIp(@PathParam("floatingipUUID") String floatingipUUID) {
158         return delete(floatingipUUID);
159     }
160 }