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