sort out signature of extraceField method
[neutron.git] / northbound-api / src / main / java / org / opendaylight / neutron / northbound / api / NeutronSubnetsNorthbound.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.DefaultValue;
18 import javax.ws.rs.GET;
19 import javax.ws.rs.POST;
20 import javax.ws.rs.PUT;
21 import javax.ws.rs.Path;
22 import javax.ws.rs.PathParam;
23 import javax.ws.rs.Produces;
24 import javax.ws.rs.QueryParam;
25 import javax.ws.rs.core.Context;
26 import javax.ws.rs.core.MediaType;
27 import javax.ws.rs.core.Response;
28 import javax.ws.rs.core.UriInfo;
29 import org.codehaus.enunciate.jaxrs.ResponseCode;
30 import org.codehaus.enunciate.jaxrs.StatusCodes;
31 import org.opendaylight.neutron.spi.INeutronSubnetCRUD;
32 import org.opendaylight.neutron.spi.NeutronCRUDInterfaces;
33 import org.opendaylight.neutron.spi.NeutronSubnet;
34
35 /**
36  * Neutron Northbound REST APIs for Subnets.<br>
37  * This class provides REST APIs for managing neutron Subnets
38  *
39  * <br>
40  * <br>
41  * Authentication scheme : <b>HTTP Basic</b><br>
42  * Authentication realm : <b>opendaylight</b><br>
43  * Transport : <b>HTTP and HTTPS</b><br>
44  * <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>
48  * More info :
49  * http://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html#Configuration
50  *
51  */
52
53 @Path("/subnets")
54 public class NeutronSubnetsNorthbound
55         extends AbstractNeutronNorthbound<NeutronSubnet, NeutronSubnetRequest, INeutronSubnetCRUD> {
56     private static final String RESOURCE_NAME = "Subnet";
57
58     @Override
59     protected String getResourceName() {
60         return RESOURCE_NAME;
61     }
62
63     private NeutronCRUDInterfaces getNeutronInterfaces(boolean needNetwork) {
64         NeutronCRUDInterfaces answer = new NeutronCRUDInterfaces().fetchINeutronSubnetCRUD(this);
65         if (answer.getSubnetInterface() == null) {
66             throw new ServiceUnavailableException(serviceUnavailable());
67         }
68         if (needNetwork) {
69             answer = answer.fetchINeutronNetworkCRUD(this);
70             if (answer.getNetworkInterface() == null) {
71                 throw new ServiceUnavailableException(
72                         "Network CRUD Interface " + RestMessages.SERVICEUNAVAILABLE.toString());
73             }
74         }
75         return answer;
76     }
77
78     @Override
79     protected INeutronSubnetCRUD getNeutronCRUD() {
80         return getNeutronInterfaces(false).getSubnetInterface();
81     }
82
83     @Override
84     protected NeutronSubnetRequest newNeutronRequest(NeutronSubnet o) {
85         return new NeutronSubnetRequest(o);
86     }
87
88     @Context
89     UriInfo uriInfo;
90
91     /**
92      * Returns a list of all Subnets */
93     @GET
94     @Produces({ MediaType.APPLICATION_JSON })
95     //@TypeHint(OpenStackSubnets.class)
96     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
97             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
98             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
99             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
100     public Response listSubnets(
101             // return fields
102             @QueryParam("fields") List<String> fields,
103             // note: openstack isn't clear about filtering on lists, so we aren't handling them
104             @QueryParam("id") String queryID,
105             @QueryParam("network_id") String queryNetworkID,
106             @QueryParam("name") String queryName,
107             @QueryParam("ip_version") Integer queryIPVersion,
108             @QueryParam("cidr") String queryCIDR,
109             @QueryParam("gateway_ip") String queryGatewayIP,
110             @QueryParam("enable_dhcp") Boolean queryEnableDHCP,
111             @QueryParam("tenant_id") String queryTenantID,
112             @QueryParam("ipv6_address_mode") String queryIpV6AddressMode,
113             @QueryParam("ipv6_ra_mode") String queryIpV6RaMode,
114             // linkTitle
115             @QueryParam("limit") Integer limit,
116             @QueryParam("marker") String marker,
117             @DefaultValue("false") @QueryParam("page_reverse") Boolean pageReverse
118     // sorting not supported
119     ) {
120         INeutronSubnetCRUD subnetInterface = getNeutronInterfaces(false).getSubnetInterface();
121         List<NeutronSubnet> allNetworks = subnetInterface.getAll();
122         List<NeutronSubnet> ans = new ArrayList<>();
123         Iterator<NeutronSubnet> i = allNetworks.iterator();
124         while (i.hasNext()) {
125             NeutronSubnet oSS = i.next();
126             if ((queryID == null || queryID.equals(oSS.getID()))
127                     && (queryNetworkID == null || queryNetworkID.equals(oSS.getNetworkUUID()))
128                     && (queryName == null || queryName.equals(oSS.getName()))
129                     && (queryIPVersion == null || queryIPVersion.equals(oSS.getIpVersion()))
130                     && (queryCIDR == null || queryCIDR.equals(oSS.getCidr()))
131                     && (queryGatewayIP == null || queryGatewayIP.equals(oSS.getGatewayIP()))
132                     && (queryEnableDHCP == null || queryEnableDHCP.equals(oSS.getEnableDHCP()))
133                     && (queryTenantID == null || queryTenantID.equals(oSS.getTenantID()))
134                     && (queryIpV6AddressMode == null || queryIpV6AddressMode.equals(oSS.getIpV6AddressMode()))
135                     && (queryIpV6RaMode == null || queryIpV6RaMode.equals(oSS.getIpV6RaMode()))) {
136                 if (fields.size() > 0) {
137                     ans.add(oSS.extractFields(fields));
138                 } else {
139                     ans.add(oSS);
140                 }
141             }
142         }
143
144         if (limit != null && ans.size() > 1) {
145             // Return a paginated request
146             NeutronSubnetRequest request = (NeutronSubnetRequest) PaginatedRequestFactory.createRequest(limit, marker,
147                     pageReverse, uriInfo, ans, NeutronSubnet.class);
148             return Response.status(HttpURLConnection.HTTP_OK).entity(request).build();
149         }
150
151         return Response.status(HttpURLConnection.HTTP_OK).entity(new NeutronSubnetRequest(ans)).build();
152     }
153
154     /**
155      * Returns a specific Subnet */
156
157     @Path("{subnetUUID}")
158     @GET
159     @Produces({ MediaType.APPLICATION_JSON })
160     //@TypeHint(OpenStackSubnets.class)
161     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
162             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
163             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
164             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
165             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
166     public Response showSubnet(@PathParam("subnetUUID") String subnetUUID,
167             // return fields
168             @QueryParam("fields") List<String> fields) {
169         return show(subnetUUID, fields);
170     }
171
172     /**
173      * Creates new Subnets */
174
175     @POST
176     @Produces({ MediaType.APPLICATION_JSON })
177     @Consumes({ MediaType.APPLICATION_JSON })
178     //@TypeHint(OpenStackSubnets.class)
179     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_CREATED, condition = "Created"),
180             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
181     public Response createSubnets(final NeutronSubnetRequest input) {
182         getNeutronInterfaces(true); // Ensure that network service is loaded
183         return create(input);
184     }
185
186     @Override
187     protected void updateDelta(String uuid, NeutronSubnet delta, NeutronSubnet original) {
188         /*
189          * note: what we get appears to not be a delta, but rather a
190          * complete updated object.  So, that needs to be sent down to
191          * folks to check
192          */
193
194         delta.setID(uuid);
195         delta.setNetworkUUID(original.getNetworkUUID());
196         delta.setTenantID(original.getTenantID());
197         delta.setIpVersion(original.getIpVersion());
198         delta.setCidr(original.getCidr());
199     }
200
201     /**
202      * Updates a Subnet */
203
204     @Path("{subnetUUID}")
205     @PUT
206     @Produces({ MediaType.APPLICATION_JSON })
207     @Consumes({ MediaType.APPLICATION_JSON })
208     //@TypeHint(OpenStackSubnets.class)
209     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
210             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
211             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
212     public Response updateSubnet(@PathParam("subnetUUID") String subnetUUID, final NeutronSubnetRequest input) {
213         return update(subnetUUID, input);
214     }
215
216     /**
217      * Deletes a Subnet */
218
219     @Path("{subnetUUID}")
220     @DELETE
221     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_NO_CONTENT, condition = "No Content"),
222             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
223             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
224     public Response deleteSubnet(@PathParam("subnetUUID") String subnetUUID) {
225         return delete(subnetUUID);
226     }
227 }