2 * Copyright IBM Corporation, 2013. 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 org.codehaus.enunciate.jaxrs.ResponseCode;
12 import org.codehaus.enunciate.jaxrs.StatusCodes;
13 import org.opendaylight.neutron.neutron.spi.INeutronNetworkCRUD;
14 import org.opendaylight.neutron.neutron.spi.INeutronPortAware;
15 import org.opendaylight.neutron.neutron.spi.INeutronPortCRUD;
16 import org.opendaylight.neutron.neutron.spi.INeutronSubnetCRUD;
17 import org.opendaylight.neutron.neutron.spi.NeutronCRUDInterfaces;
18 import org.opendaylight.neutron.neutron.spi.NeutronPort;
19 import org.opendaylight.neutron.neutron.spi.NeutronSubnet;
20 import org.opendaylight.neutron.neutron.spi.Neutron_IPs;
22 import javax.ws.rs.Consumes;
23 import javax.ws.rs.DELETE;
24 import javax.ws.rs.DefaultValue;
25 import javax.ws.rs.GET;
26 import javax.ws.rs.POST;
27 import javax.ws.rs.PUT;
28 import javax.ws.rs.Path;
29 import javax.ws.rs.PathParam;
30 import javax.ws.rs.Produces;
31 import javax.ws.rs.QueryParam;
32 import javax.ws.rs.core.Context;
33 import javax.ws.rs.core.MediaType;
34 import javax.ws.rs.core.Response;
35 import javax.ws.rs.core.UriInfo;
36 import java.util.ArrayList;
37 import java.util.HashMap;
38 import java.util.Iterator;
39 import java.util.List;
42 * Neutron Northbound REST APIs.<br>
43 * This class provides REST APIs for managing neutron port objects
47 * Authentication scheme : <b>HTTP Basic</b><br>
48 * Authentication realm : <b>opendaylight</b><br>
49 * Transport : <b>HTTP and HTTPS</b><br>
51 * HTTPS Authentication is disabled by default. Administrator can enable it in
52 * tomcat-server.xml after adding a proper keystore / SSL certificate from a
53 * trusted authority.<br>
55 * http://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html#Configuration
60 public class NeutronPortsNorthbound {
62 final String mac_regex="^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$";
64 private NeutronPort extractFields(NeutronPort o, List<String> fields) {
65 return o.extractFields(fields);
72 * Returns a list of all Ports */
75 @Produces({ MediaType.APPLICATION_JSON })
76 //@TypeHint(OpenStackPorts.class)
78 @ResponseCode(code = 200, condition = "Operation successful"),
79 @ResponseCode(code = 401, condition = "Unauthorized"),
80 @ResponseCode(code = 501, condition = "Not Implemented"),
81 @ResponseCode(code = 503, condition = "No providers available") })
82 public Response listPorts(
84 @QueryParam("fields") List<String> fields,
85 // note: openstack isn't clear about filtering on lists, so we aren't handling them
86 @QueryParam("id") String queryID,
87 @QueryParam("network_id") String queryNetworkID,
88 @QueryParam("name") String queryName,
89 @QueryParam("admin_state_up") String queryAdminStateUp,
90 @QueryParam("status") String queryStatus,
91 @QueryParam("mac_address") String queryMACAddress,
92 @QueryParam("device_id") String queryDeviceID,
93 @QueryParam("device_owner") String queryDeviceOwner,
94 @QueryParam("tenant_id") String queryTenantID,
96 @QueryParam("limit") Integer limit,
97 @QueryParam("marker") String marker,
98 @DefaultValue("false") @QueryParam("page_reverse") Boolean pageReverse
99 // sorting not supported
101 INeutronPortCRUD portInterface = NeutronCRUDInterfaces.getINeutronPortCRUD(this);
102 if (portInterface == null) {
103 throw new ServiceUnavailableException("Port CRUD Interface "
104 + RestMessages.SERVICEUNAVAILABLE.toString());
106 List<NeutronPort> allPorts = portInterface.getAllPorts();
107 List<NeutronPort> ans = new ArrayList<NeutronPort>();
108 Iterator<NeutronPort> i = allPorts.iterator();
109 while (i.hasNext()) {
110 NeutronPort oSS = i.next();
111 if ((queryID == null || queryID.equals(oSS.getID())) &&
112 (queryNetworkID == null || queryNetworkID.equals(oSS.getNetworkUUID())) &&
113 (queryName == null || queryName.equals(oSS.getName())) &&
114 (queryAdminStateUp == null || queryAdminStateUp.equals(oSS.getAdminStateUp())) &&
115 (queryStatus == null || queryStatus.equals(oSS.getStatus())) &&
116 (queryMACAddress == null || queryMACAddress.equals(oSS.getMacAddress())) &&
117 (queryDeviceID == null || queryDeviceID.equals(oSS.getDeviceID())) &&
118 (queryDeviceOwner == null || queryDeviceOwner.equals(oSS.getDeviceOwner())) &&
119 (queryTenantID == null || queryTenantID.equals(oSS.getTenantID()))) {
120 if (fields.size() > 0) {
121 ans.add(extractFields(oSS,fields));
128 if (limit != null && ans.size() > 1) {
129 // Return a paginated request
130 NeutronPortRequest request = (NeutronPortRequest) PaginatedRequestFactory.createRequest(limit,
131 marker, pageReverse, uriInfo, ans, NeutronPort.class);
132 return Response.status(200).entity(request).build();
135 return Response.status(200).entity(
136 new NeutronPortRequest(ans)).build();
140 * Returns a specific Port */
144 @Produces({ MediaType.APPLICATION_JSON })
145 //@TypeHint(OpenStackPorts.class)
147 @ResponseCode(code = 200, condition = "Operation successful"),
148 @ResponseCode(code = 401, condition = "Unauthorized"),
149 @ResponseCode(code = 404, condition = "Not Found"),
150 @ResponseCode(code = 501, condition = "Not Implemented"),
151 @ResponseCode(code = 503, condition = "No providers available") })
152 public Response showPort(
153 @PathParam("portUUID") String portUUID,
155 @QueryParam("fields") List<String> fields ) {
156 INeutronPortCRUD portInterface = NeutronCRUDInterfaces.getINeutronPortCRUD(this);
157 if (portInterface == null) {
158 throw new ServiceUnavailableException("Port CRUD Interface "
159 + RestMessages.SERVICEUNAVAILABLE.toString());
161 if (!portInterface.portExists(portUUID)) {
162 throw new ResourceNotFoundException("port UUID does not exist.");
164 if (fields.size() > 0) {
165 NeutronPort ans = portInterface.getPort(portUUID);
166 return Response.status(200).entity(
167 new NeutronPortRequest(extractFields(ans, fields))).build();
169 return Response.status(200).entity(
170 new NeutronPortRequest(portInterface.getPort(portUUID))).build();
175 * Creates new Ports */
178 @Produces({ MediaType.APPLICATION_JSON })
179 @Consumes({ MediaType.APPLICATION_JSON })
180 //@TypeHint(OpenStackPorts.class)
182 @ResponseCode(code = 201, condition = "Created"),
183 @ResponseCode(code = 400, condition = "Bad Request"),
184 @ResponseCode(code = 401, condition = "Unauthorized"),
185 @ResponseCode(code = 403, condition = "Forbidden"),
186 @ResponseCode(code = 404, condition = "Not Found"),
187 @ResponseCode(code = 409, condition = "Conflict"),
188 @ResponseCode(code = 501, condition = "Not Implemented"),
189 @ResponseCode(code = 503, condition = "MAC generation failure"),
190 @ResponseCode(code = 503, condition = "No providers available") })
191 public Response createPorts(final NeutronPortRequest input) {
192 INeutronPortCRUD portInterface = NeutronCRUDInterfaces.getINeutronPortCRUD(this);
193 if (portInterface == null) {
194 throw new ServiceUnavailableException("Port CRUD Interface "
195 + RestMessages.SERVICEUNAVAILABLE.toString());
197 INeutronNetworkCRUD networkInterface = NeutronCRUDInterfaces.getINeutronNetworkCRUD( this);
198 if (networkInterface == null) {
199 throw new ServiceUnavailableException("Network CRUD Interface "
200 + RestMessages.SERVICEUNAVAILABLE.toString());
202 INeutronSubnetCRUD subnetInterface = NeutronCRUDInterfaces.getINeutronSubnetCRUD( this);
203 if (subnetInterface == null) {
204 throw new ServiceUnavailableException("Subnet CRUD Interface "
205 + RestMessages.SERVICEUNAVAILABLE.toString());
207 if (input.isSingleton()) {
208 NeutronPort singleton = input.getSingleton();
211 * the port must be part of an existing network, must not already exist,
212 * have a valid MAC and the MAC not be in use
214 if (singleton.getNetworkUUID() == null) {
215 throw new BadRequestException("network UUID musy be specified");
217 if (portInterface.portExists(singleton.getID())) {
218 throw new BadRequestException("port UUID already exists");
220 if (!networkInterface.networkExists(singleton.getNetworkUUID())) {
221 throw new ResourceNotFoundException("network UUID does not exist.");
223 if (singleton.getMacAddress() == null ||
224 !singleton.getMacAddress().matches(mac_regex)) {
225 throw new BadRequestException("MAC address not properly formatted");
227 if (portInterface.macInUse(singleton.getMacAddress())) {
228 throw new ResourceConflictException("MAC Address is in use.");
231 * if fixed IPs are specified, each one has to have an existing subnet ID
232 * that is in the same scoping network as the port. In addition, if an IP
233 * address is specified it has to be a valid address for the subnet and not
236 List<Neutron_IPs> fixedIPs = singleton.getFixedIPs();
237 if (fixedIPs != null && fixedIPs.size() > 0) {
238 Iterator<Neutron_IPs> fixedIPIterator = fixedIPs.iterator();
239 while (fixedIPIterator.hasNext()) {
240 Neutron_IPs ip = fixedIPIterator.next();
241 if (ip.getSubnetUUID() == null) {
242 throw new BadRequestException("subnet UUID not specified");
244 if (!subnetInterface.subnetExists(ip.getSubnetUUID())) {
245 throw new BadRequestException("subnet UUID must exists");
247 NeutronSubnet subnet = subnetInterface.getSubnet(ip.getSubnetUUID());
248 if (!singleton.getNetworkUUID().equalsIgnoreCase(subnet.getNetworkUUID())) {
249 throw new BadRequestException("network UUID must match that of subnet");
251 if (ip.getIpAddress() != null) {
252 if (!subnet.isValidIP(ip.getIpAddress())) {
253 throw new BadRequestException("IP address is not valid");
255 if (subnet.isIPInUse(ip.getIpAddress())) {
256 throw new ResourceConflictException("IP address is in use.");
262 Object[] instances = NeutronUtil.getInstances(INeutronPortAware.class, this);
263 if (instances != null) {
264 if (instances.length > 0) {
265 for (Object instance : instances) {
266 INeutronPortAware service = (INeutronPortAware) instance;
267 int status = service.canCreatePort(singleton);
268 if (status < 200 || status > 299) {
269 return Response.status(status).build();
273 throw new ServiceUnavailableException("No providers registered. Please try again later");
276 throw new ServiceUnavailableException("Couldn't get providers list. Please try again later");
279 // add the port to the cache
280 portInterface.addPort(singleton);
281 if (instances != null) {
282 for (Object instance : instances) {
283 INeutronPortAware service = (INeutronPortAware) instance;
284 service.neutronPortCreated(singleton);
288 List<NeutronPort> bulk = input.getBulk();
289 Iterator<NeutronPort> i = bulk.iterator();
290 HashMap<String, NeutronPort> testMap = new HashMap<String, NeutronPort>();
291 Object[] instances = NeutronUtil.getInstances(INeutronPortAware.class, this);
292 while (i.hasNext()) {
293 NeutronPort test = i.next();
296 * the port must be part of an existing network, must not already exist,
297 * have a valid MAC and the MAC not be in use. Further the bulk request
298 * can't already contain a new port with the same UUID
300 if (portInterface.portExists(test.getID())) {
301 throw new BadRequestException("port UUID already exists");
303 if (testMap.containsKey(test.getID())) {
304 throw new BadRequestException("port UUID already exists");
306 for (NeutronPort check : testMap.values()) {
307 if (test.getMacAddress().equalsIgnoreCase(check.getMacAddress())) {
308 throw new ResourceConflictException("MAC address already allocated");
310 for (Neutron_IPs test_fixedIP : test.getFixedIPs()) {
311 for (Neutron_IPs check_fixedIP : check.getFixedIPs()) {
312 if (test_fixedIP.getSubnetUUID().equals(check_fixedIP.getSubnetUUID())) {
313 if (test_fixedIP.getIpAddress().equals(check_fixedIP.getIpAddress())) {
314 throw new ResourceConflictException("IP address already allocated");
320 testMap.put(test.getID(), test);
321 if (!networkInterface.networkExists(test.getNetworkUUID())) {
322 throw new ResourceNotFoundException("network UUID does not exist.");
324 if (!test.getMacAddress().matches(mac_regex)) {
325 throw new BadRequestException("MAC address not properly formatted");
327 if (portInterface.macInUse(test.getMacAddress())) {
328 throw new ResourceConflictException("MAC address in use");
332 * if fixed IPs are specified, each one has to have an existing subnet ID
333 * that is in the same scoping network as the port. In addition, if an IP
334 * address is specified it has to be a valid address for the subnet and not
335 * already in use (or be the gateway IP address of the subnet)
337 List<Neutron_IPs> fixedIPs = test.getFixedIPs();
338 if (fixedIPs != null && fixedIPs.size() > 0) {
339 Iterator<Neutron_IPs> fixedIPIterator = fixedIPs.iterator();
340 while (fixedIPIterator.hasNext()) {
341 Neutron_IPs ip = fixedIPIterator.next();
342 if (ip.getSubnetUUID() == null) {
343 throw new BadRequestException("subnet UUID must be specified");
345 if (!subnetInterface.subnetExists(ip.getSubnetUUID())) {
346 throw new BadRequestException("subnet UUID doesn't exists");
348 NeutronSubnet subnet = subnetInterface.getSubnet(ip.getSubnetUUID());
349 if (!test.getNetworkUUID().equalsIgnoreCase(subnet.getNetworkUUID())) {
350 throw new BadRequestException("network UUID must match that of subnet");
352 if (ip.getIpAddress() != null) {
353 if (!subnet.isValidIP(ip.getIpAddress())) {
354 throw new BadRequestException("ip address not valid");
356 //TODO: need to add consideration for a fixed IP being assigned the same address as a allocated IP in the
358 if (subnet.isIPInUse(ip.getIpAddress())) {
359 throw new ResourceConflictException("IP address in use");
364 if (instances != null) {
365 if (instances.length > 0) {
366 for (Object instance : instances) {
367 INeutronPortAware service = (INeutronPortAware) instance;
368 int status = service.canCreatePort(test);
369 if (status < 200 || status > 299) {
370 return Response.status(status).build();
374 throw new ServiceUnavailableException("No providers registered. Please try again later");
377 throw new ServiceUnavailableException("Couldn't get providers list. Please try again later");
381 //once everything has passed, then we can add to the cache
383 while (i.hasNext()) {
384 NeutronPort test = i.next();
385 portInterface.addPort(test);
386 if (instances != null) {
387 for (Object instance : instances) {
388 INeutronPortAware service = (INeutronPortAware) instance;
389 service.neutronPortCreated(test);
394 return Response.status(201).entity(input).build();
402 @Produces({ MediaType.APPLICATION_JSON })
403 @Consumes({ MediaType.APPLICATION_JSON })
404 //@TypeHint(OpenStackPorts.class)
406 @ResponseCode(code = 200, condition = "Operation successful"),
407 @ResponseCode(code = 400, condition = "Bad Request"),
408 @ResponseCode(code = 401, condition = "Unauthorized"),
409 @ResponseCode(code = 403, condition = "Forbidden"),
410 @ResponseCode(code = 404, condition = "Not Found"),
411 @ResponseCode(code = 409, condition = "Conflict"),
412 @ResponseCode(code = 501, condition = "Not Implemented"),
413 @ResponseCode(code = 503, condition = "No providers available") })
414 public Response updatePort(
415 @PathParam("portUUID") String portUUID,
416 NeutronPortRequest input
418 INeutronPortCRUD portInterface = NeutronCRUDInterfaces.getINeutronPortCRUD(this);
419 if (portInterface == null) {
420 throw new ServiceUnavailableException("Port CRUD Interface "
421 + RestMessages.SERVICEUNAVAILABLE.toString());
423 INeutronSubnetCRUD subnetInterface = NeutronCRUDInterfaces.getINeutronSubnetCRUD( this);
424 if (subnetInterface == null) {
425 throw new ServiceUnavailableException("Subnet CRUD Interface "
426 + RestMessages.SERVICEUNAVAILABLE.toString());
429 // port has to exist and only a single delta is supported
430 if (!portInterface.portExists(portUUID)) {
431 throw new ResourceNotFoundException("port UUID does not exist.");
433 NeutronPort target = portInterface.getPort(portUUID);
434 if (!input.isSingleton()) {
435 throw new BadRequestException("only singleton edit suported");
437 NeutronPort singleton = input.getSingleton();
438 NeutronPort original = portInterface.getPort(portUUID);
440 // deltas restricted by Neutron
441 if (singleton.getID() != null || singleton.getTenantID() != null ||
442 singleton.getStatus() != null) {
443 throw new BadRequestException("attribute change blocked by Neutron");
446 Object[] instances = NeutronUtil.getInstances(INeutronPortAware.class, this);
447 if (instances != null) {
448 if (instances.length > 0) {
449 for (Object instance : instances) {
450 INeutronPortAware service = (INeutronPortAware) instance;
451 int status = service.canUpdatePort(singleton, original);
452 if (status < 200 || status > 299) {
453 return Response.status(status).build();
457 throw new ServiceUnavailableException("No providers registered. Please try again later");
460 throw new ServiceUnavailableException("Couldn't get providers list. Please try again later");
463 // Verify the new fixed ips are valid
464 List<Neutron_IPs> fixedIPs = singleton.getFixedIPs();
465 if (fixedIPs != null && fixedIPs.size() > 0) {
466 Iterator<Neutron_IPs> fixedIPIterator = fixedIPs.iterator();
467 while (fixedIPIterator.hasNext()) {
468 Neutron_IPs ip = fixedIPIterator.next();
469 if (ip.getSubnetUUID() == null) {
470 throw new BadRequestException("subnet UUID must be specified");
472 if (!subnetInterface.subnetExists(ip.getSubnetUUID())) {
473 throw new BadRequestException("subnet UUID doesn't exist.");
475 NeutronSubnet subnet = subnetInterface.getSubnet(ip.getSubnetUUID());
476 if (!target.getNetworkUUID().equalsIgnoreCase(subnet.getNetworkUUID())) {
477 throw new BadRequestException("network UUID must match that of subnet");
479 if (ip.getIpAddress() != null) {
480 if (!subnet.isValidIP(ip.getIpAddress())) {
481 throw new BadRequestException("invalid IP address");
483 if (subnet.isIPInUse(ip.getIpAddress())) {
484 throw new ResourceConflictException("IP address in use");
490 // TODO: Support change of security groups
491 // update the port and return the modified object
492 portInterface.updatePort(portUUID, singleton);
493 NeutronPort updatedPort = portInterface.getPort(portUUID);
494 if (instances != null) {
495 for (Object instance : instances) {
496 INeutronPortAware service = (INeutronPortAware) instance;
497 service.neutronPortUpdated(updatedPort);
500 return Response.status(200).entity(
501 new NeutronPortRequest(updatedPort)).build();
511 @ResponseCode(code = 204, condition = "No Content"),
512 @ResponseCode(code = 401, condition = "Unauthorized"),
513 @ResponseCode(code = 403, condition = "Forbidden"),
514 @ResponseCode(code = 404, condition = "Not Found"),
515 @ResponseCode(code = 501, condition = "Not Implemented"),
516 @ResponseCode(code = 503, condition = "No providers available") })
517 public Response deletePort(
518 @PathParam("portUUID") String portUUID) {
519 INeutronPortCRUD portInterface = NeutronCRUDInterfaces.getINeutronPortCRUD(this);
520 if (portInterface == null) {
521 throw new ServiceUnavailableException("Port CRUD Interface "
522 + RestMessages.SERVICEUNAVAILABLE.toString());
525 // port has to exist and not be owned by anyone. then it can be removed from the cache
526 if (!portInterface.portExists(portUUID)) {
527 throw new ResourceNotFoundException("port UUID does not exist.");
529 NeutronPort port = portInterface.getPort(portUUID);
530 if (port.getDeviceID() != null ||
531 port.getDeviceOwner() != null) {
532 Response.status(403).build();
534 NeutronPort singleton = portInterface.getPort(portUUID);
535 Object[] instances = NeutronUtil.getInstances(INeutronPortAware.class, this);
536 if (instances != null) {
537 if (instances.length > 0) {
538 for (Object instance : instances) {
539 INeutronPortAware service = (INeutronPortAware) instance;
540 int status = service.canDeletePort(singleton);
541 if (status < 200 || status > 299) {
542 return Response.status(status).build();
546 throw new ServiceUnavailableException("No providers registered. Please try again later");
549 throw new ServiceUnavailableException("Couldn't get providers list. Please try again later");
551 portInterface.removePort(portUUID);
552 if (instances != null) {
553 for (Object instance : instances) {
554 INeutronPortAware service = (INeutronPortAware) instance;
555 service.neutronPortDeleted(singleton);
558 return Response.status(204).build();