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.INeutronNetworkCRUD;
35 import org.opendaylight.neutron.spi.INeutronSubnetAware;
36 import org.opendaylight.neutron.spi.INeutronSubnetCRUD;
37 import org.opendaylight.neutron.spi.NeutronCRUDInterfaces;
38 import org.opendaylight.neutron.spi.NeutronSubnet;
41 * Neutron Northbound REST APIs for Subnets.<br>
42 * This class provides REST APIs for managing neutron Subnets
46 * Authentication scheme : <b>HTTP Basic</b><br>
47 * Authentication realm : <b>opendaylight</b><br>
48 * Transport : <b>HTTP and HTTPS</b><br>
50 * HTTPS Authentication is disabled by default. Administrator can enable it in
51 * tomcat-server.xml after adding a proper keystore / SSL certificate from a
52 * trusted authority.<br>
54 * http://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html#Configuration
59 public class NeutronSubnetsNorthbound
60 extends AbstractNeutronNorthbound<NeutronSubnet, NeutronSubnetRequest, INeutronSubnetCRUD, INeutronSubnetAware> {
61 private static final String RESOURCE_NAME = "Subnet";
64 protected String getResourceName() {
69 protected NeutronSubnet extractFields(NeutronSubnet o, List<String> fields) {
70 return o.extractFields(fields);
73 private NeutronCRUDInterfaces getNeutronInterfaces(boolean needNetwork) {
74 NeutronCRUDInterfaces answer = new NeutronCRUDInterfaces().fetchINeutronSubnetCRUD(this);
75 if (answer.getSubnetInterface() == null) {
76 throw new ServiceUnavailableException(serviceUnavailable());
79 answer = answer.fetchINeutronNetworkCRUD(this);
80 if (answer.getNetworkInterface() == null) {
81 throw new ServiceUnavailableException("Network CRUD Interface "
82 + RestMessages.SERVICEUNAVAILABLE.toString());
89 protected INeutronSubnetCRUD getNeutronCRUD() {
90 return getNeutronInterfaces(false).getSubnetInterface();
94 protected NeutronSubnetRequest newNeutronRequest(NeutronSubnet o) {
95 return new NeutronSubnetRequest(o);
99 protected Object[] getInstances() {
100 return NeutronUtil.getInstances(INeutronSubnetAware.class, this);
104 protected int canCreate(Object instance, NeutronSubnet singleton) {
105 INeutronSubnetAware service = (INeutronSubnetAware) instance;
106 return service.canCreateSubnet(singleton);
110 protected void created(Object instance, NeutronSubnet singleton) {
111 INeutronSubnetAware service = (INeutronSubnetAware) instance;
112 service.neutronSubnetCreated(singleton);
116 protected int canUpdate(Object instance, NeutronSubnet delta, NeutronSubnet original) {
117 INeutronSubnetAware service = (INeutronSubnetAware) instance;
118 return service.canUpdateSubnet(delta, original);
122 protected void updated(Object instance, NeutronSubnet updated) {
123 INeutronSubnetAware service = (INeutronSubnetAware) instance;
124 service.neutronSubnetUpdated(updated);
128 protected int canDelete(Object instance, NeutronSubnet singleton) {
129 INeutronSubnetAware service = (INeutronSubnetAware) instance;
130 return service.canDeleteSubnet(singleton);
134 protected void deleted(Object instance, NeutronSubnet singleton) {
135 INeutronSubnetAware service = (INeutronSubnetAware) instance;
136 service.neutronSubnetDeleted(singleton);
143 * Returns a list of all Subnets */
145 @Produces({ MediaType.APPLICATION_JSON })
146 //@TypeHint(OpenStackSubnets.class)
148 @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
149 @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
150 @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
151 @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
152 public Response listSubnets(
154 @QueryParam("fields") List<String> fields,
155 // note: openstack isn't clear about filtering on lists, so we aren't handling them
156 @QueryParam("id") String queryID,
157 @QueryParam("network_id") String queryNetworkID,
158 @QueryParam("name") String queryName,
159 @QueryParam("ip_version") String queryIPVersion,
160 @QueryParam("cidr") String queryCIDR,
161 @QueryParam("gateway_ip") String queryGatewayIP,
162 @QueryParam("enable_dhcp") String queryEnableDHCP,
163 @QueryParam("tenant_id") String queryTenantID,
164 @QueryParam("ipv6_address_mode") String queryIpV6AddressMode,
165 @QueryParam("ipv6_ra_mode") String queryIpV6RaMode,
167 @QueryParam("limit") Integer limit,
168 @QueryParam("marker") String marker,
169 @DefaultValue("false") @QueryParam("page_reverse") Boolean pageReverse
170 // sorting not supported
172 INeutronSubnetCRUD subnetInterface = getNeutronInterfaces(false).getSubnetInterface();
173 List<NeutronSubnet> allNetworks = subnetInterface.getAllSubnets();
174 List<NeutronSubnet> ans = new ArrayList<NeutronSubnet>();
175 Iterator<NeutronSubnet> i = allNetworks.iterator();
176 while (i.hasNext()) {
177 NeutronSubnet oSS = i.next();
178 if ((queryID == null || queryID.equals(oSS.getID())) &&
179 (queryNetworkID == null || queryNetworkID.equals(oSS.getNetworkUUID())) &&
180 (queryName == null || queryName.equals(oSS.getName())) &&
181 (queryIPVersion == null || queryIPVersion.equals(oSS.getIpVersion())) &&
182 (queryCIDR == null || queryCIDR.equals(oSS.getCidr())) &&
183 (queryGatewayIP == null || queryGatewayIP.equals(oSS.getGatewayIP())) &&
184 (queryEnableDHCP == null || queryEnableDHCP.equals(oSS.getEnableDHCP())) &&
185 (queryTenantID == null || queryTenantID.equals(oSS.getTenantID())) &&
186 (queryIpV6AddressMode == null || queryIpV6AddressMode.equals(oSS.getIpV6AddressMode())) &&
187 (queryIpV6RaMode == null || queryIpV6RaMode.equals(oSS.getIpV6RaMode()))){
188 if (fields.size() > 0) {
189 ans.add(extractFields(oSS,fields));
196 if (limit != null && ans.size() > 1) {
197 // Return a paginated request
198 NeutronSubnetRequest request = (NeutronSubnetRequest) PaginatedRequestFactory.createRequest(limit,
199 marker, pageReverse, uriInfo, ans, NeutronSubnet.class);
200 return Response.status(HttpURLConnection.HTTP_OK).entity(request).build();
203 return Response.status(HttpURLConnection.HTTP_OK).entity(
204 new NeutronSubnetRequest(ans)).build();
208 * Returns a specific Subnet */
210 @Path("{subnetUUID}")
212 @Produces({ MediaType.APPLICATION_JSON })
213 //@TypeHint(OpenStackSubnets.class)
215 @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
216 @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
217 @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
218 @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
219 @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
220 public Response showSubnet(
221 @PathParam("subnetUUID") String subnetUUID,
223 @QueryParam("fields") List<String> fields) {
224 return show(subnetUUID, fields);
228 * Creates new Subnets */
231 @Produces({ MediaType.APPLICATION_JSON })
232 @Consumes({ MediaType.APPLICATION_JSON })
233 //@TypeHint(OpenStackSubnets.class)
235 @ResponseCode(code = HttpURLConnection.HTTP_CREATED, condition = "Created"),
236 @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
237 public Response createSubnets(final NeutronSubnetRequest input) {
238 getNeutronInterfaces(true); // Ensure that network service is loaded
239 return create(input);
243 protected void updateDelta(String uuid, NeutronSubnet delta, NeutronSubnet original) {
245 * note: what we get appears to not be a delta, but rather a
246 * complete updated object. So, that needs to be sent down to
251 delta.setNetworkUUID(original.getNetworkUUID());
252 delta.setTenantID(original.getTenantID());
253 delta.setIpVersion(original.getIpVersion());
254 delta.setCidr(original.getCidr());
258 * Updates a Subnet */
260 @Path("{subnetUUID}")
262 @Produces({ MediaType.APPLICATION_JSON })
263 @Consumes({ MediaType.APPLICATION_JSON })
264 //@TypeHint(OpenStackSubnets.class)
266 @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
267 @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
268 public Response updateSubnet(
269 @PathParam("subnetUUID") String subnetUUID, final NeutronSubnetRequest input
271 return update(subnetUUID, input);
275 * Deletes a Subnet */
277 @Path("{subnetUUID}")
280 @ResponseCode(code = HttpURLConnection.HTTP_NO_CONTENT, condition = "No Content"),
281 @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
282 public Response deleteSubnet(
283 @PathParam("subnetUUID") String subnetUUID) {
284 return delete(subnetUUID);