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.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;
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;
39 * Neutron Northbound REST APIs for Subnets.<br>
40 * This class provides REST APIs for managing neutron Subnets
44 * Authentication scheme : <b>HTTP Basic</b><br>
45 * Authentication realm : <b>opendaylight</b><br>
46 * Transport : <b>HTTP and HTTPS</b><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>
52 * http://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html#Configuration
57 public class NeutronSubnetsNorthbound
58 extends AbstractNeutronNorthbound<NeutronSubnet, NeutronSubnetRequest, INeutronSubnetCRUD> {
59 private static final String RESOURCE_NAME = "Subnet";
62 protected String getResourceName() {
67 protected NeutronSubnet extractFields(NeutronSubnet o, List<String> fields) {
68 return o.extractFields(fields);
71 private NeutronCRUDInterfaces getNeutronInterfaces(boolean needNetwork) {
72 NeutronCRUDInterfaces answer = new NeutronCRUDInterfaces().fetchINeutronSubnetCRUD(this);
73 if (answer.getSubnetInterface() == null) {
74 throw new ServiceUnavailableException(serviceUnavailable());
77 answer = answer.fetchINeutronNetworkCRUD(this);
78 if (answer.getNetworkInterface() == null) {
79 throw new ServiceUnavailableException("Network CRUD Interface "
80 + RestMessages.SERVICEUNAVAILABLE.toString());
87 protected INeutronSubnetCRUD getNeutronCRUD() {
88 return getNeutronInterfaces(false).getSubnetInterface();
92 protected NeutronSubnetRequest newNeutronRequest(NeutronSubnet o) {
93 return new NeutronSubnetRequest(o);
100 * Returns a list of all Subnets */
102 @Produces({ MediaType.APPLICATION_JSON })
103 //@TypeHint(OpenStackSubnets.class)
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(
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,
124 @QueryParam("limit") Integer limit,
125 @QueryParam("marker") String marker,
126 @DefaultValue("false") @QueryParam("page_reverse") Boolean pageReverse
127 // sorting not supported
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));
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();
160 return Response.status(HttpURLConnection.HTTP_OK).entity(
161 new NeutronSubnetRequest(ans)).build();
165 * Returns a specific Subnet */
167 @Path("{subnetUUID}")
169 @Produces({ MediaType.APPLICATION_JSON })
170 //@TypeHint(OpenStackSubnets.class)
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,
180 @QueryParam("fields") List<String> fields) {
181 return show(subnetUUID, fields);
185 * Creates new Subnets */
188 @Produces({ MediaType.APPLICATION_JSON })
189 @Consumes({ MediaType.APPLICATION_JSON })
190 //@TypeHint(OpenStackSubnets.class)
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);
200 protected void updateDelta(String uuid, NeutronSubnet delta, NeutronSubnet original) {
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
208 delta.setNetworkUUID(original.getNetworkUUID());
209 delta.setTenantID(original.getTenantID());
210 delta.setIpVersion(original.getIpVersion());
211 delta.setCidr(original.getCidr());
215 * Updates a Subnet */
217 @Path("{subnetUUID}")
219 @Produces({ MediaType.APPLICATION_JSON })
220 @Consumes({ MediaType.APPLICATION_JSON })
221 //@TypeHint(OpenStackSubnets.class)
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
229 return update(subnetUUID, input);
233 * Deletes a Subnet */
235 @Path("{subnetUUID}")
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);