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.controller.networkconfig.neutron.northbound;
11 import java.util.ArrayList;
12 import java.util.HashMap;
13 import java.util.Iterator;
14 import java.util.List;
16 import javax.ws.rs.Consumes;
17 import javax.ws.rs.DELETE;
18 import javax.ws.rs.DefaultValue;
19 import javax.ws.rs.GET;
20 import javax.ws.rs.POST;
21 import javax.ws.rs.PUT;
22 import javax.ws.rs.Path;
23 import javax.ws.rs.PathParam;
24 import javax.ws.rs.Produces;
25 import javax.ws.rs.QueryParam;
26 import javax.ws.rs.core.Context;
27 import javax.ws.rs.core.MediaType;
28 import javax.ws.rs.core.Response;
29 import javax.ws.rs.core.UriInfo;
31 import org.codehaus.enunciate.jaxrs.ResponseCode;
32 import org.codehaus.enunciate.jaxrs.StatusCodes;
33 import org.opendaylight.controller.networkconfig.neutron.INeutronNetworkCRUD;
34 import org.opendaylight.controller.networkconfig.neutron.INeutronPortAware;
35 import org.opendaylight.controller.networkconfig.neutron.INeutronPortCRUD;
36 import org.opendaylight.controller.networkconfig.neutron.INeutronSubnetCRUD;
37 import org.opendaylight.controller.networkconfig.neutron.NeutronCRUDInterfaces;
38 import org.opendaylight.controller.networkconfig.neutron.NeutronPort;
39 import org.opendaylight.controller.networkconfig.neutron.NeutronSubnet;
40 import org.opendaylight.controller.networkconfig.neutron.Neutron_IPs;
41 import org.opendaylight.controller.northbound.commons.RestMessages;
42 import org.opendaylight.controller.northbound.commons.exception.BadRequestException;
43 import org.opendaylight.controller.northbound.commons.exception.ResourceConflictException;
44 import org.opendaylight.controller.northbound.commons.exception.ResourceNotFoundException;
45 import org.opendaylight.controller.northbound.commons.exception.ServiceUnavailableException;
46 import org.opendaylight.controller.sal.utils.ServiceHelper;
49 * Neutron Northbound REST APIs.<br>
50 * This class provides REST APIs for managing neutron port objects
54 * Authentication scheme : <b>HTTP Basic</b><br>
55 * Authentication realm : <b>opendaylight</b><br>
56 * Transport : <b>HTTP and HTTPS</b><br>
58 * HTTPS Authentication is disabled by default. Administrator can enable it in
59 * tomcat-server.xml after adding a proper keystore / SSL certificate from a
60 * trusted authority.<br>
62 * http://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html#Configuration
67 public class NeutronPortsNorthbound {
69 final String mac_regex="^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$";
71 private NeutronPort extractFields(NeutronPort o, List<String> fields) {
72 return o.extractFields(fields);
79 * Returns a list of all Ports */
82 @Produces({ MediaType.APPLICATION_JSON })
83 //@TypeHint(OpenStackPorts.class)
85 @ResponseCode(code = 200, condition = "Operation successful"),
86 @ResponseCode(code = 401, condition = "Unauthorized"),
87 @ResponseCode(code = 501, condition = "Not Implemented") })
88 public Response listPorts(
90 @QueryParam("fields") List<String> fields,
91 // note: openstack isn't clear about filtering on lists, so we aren't handling them
92 @QueryParam("id") String queryID,
93 @QueryParam("network_id") String queryNetworkID,
94 @QueryParam("name") String queryName,
95 @QueryParam("admin_state_up") String queryAdminStateUp,
96 @QueryParam("status") String queryStatus,
97 @QueryParam("mac_address") String queryMACAddress,
98 @QueryParam("device_id") String queryDeviceID,
99 @QueryParam("device_owner") String queryDeviceOwner,
100 @QueryParam("tenant_id") String queryTenantID,
102 @QueryParam("limit") Integer limit,
103 @QueryParam("marker") String marker,
104 @DefaultValue("false") @QueryParam("page_reverse") Boolean pageReverse
105 // sorting not supported
107 INeutronPortCRUD portInterface = NeutronCRUDInterfaces.getINeutronPortCRUD(this);
108 if (portInterface == null) {
109 throw new ServiceUnavailableException("Port CRUD Interface "
110 + RestMessages.SERVICEUNAVAILABLE.toString());
112 List<NeutronPort> allPorts = portInterface.getAllPorts();
113 List<NeutronPort> ans = new ArrayList<NeutronPort>();
114 Iterator<NeutronPort> i = allPorts.iterator();
115 while (i.hasNext()) {
116 NeutronPort oSS = i.next();
117 if ((queryID == null || queryID.equals(oSS.getID())) &&
118 (queryNetworkID == null || queryNetworkID.equals(oSS.getNetworkUUID())) &&
119 (queryName == null || queryName.equals(oSS.getName())) &&
120 (queryAdminStateUp == null || queryAdminStateUp.equals(oSS.getAdminStateUp())) &&
121 (queryStatus == null || queryStatus.equals(oSS.getStatus())) &&
122 (queryMACAddress == null || queryMACAddress.equals(oSS.getMacAddress())) &&
123 (queryDeviceID == null || queryDeviceID.equals(oSS.getDeviceID())) &&
124 (queryDeviceOwner == null || queryDeviceOwner.equals(oSS.getDeviceOwner())) &&
125 (queryTenantID == null || queryTenantID.equals(oSS.getTenantID()))) {
126 if (fields.size() > 0) {
127 ans.add(extractFields(oSS,fields));
134 if (limit != null && ans.size() > 1) {
135 // Return a paginated request
136 NeutronPortRequest request = (NeutronPortRequest) PaginatedRequestFactory.createRequest(limit,
137 marker, pageReverse, uriInfo, ans, NeutronPort.class);
138 return Response.status(200).entity(request).build();
141 return Response.status(200).entity(
142 new NeutronPortRequest(ans)).build();
146 * Returns a specific Port */
150 @Produces({ MediaType.APPLICATION_JSON })
151 //@TypeHint(OpenStackPorts.class)
153 @ResponseCode(code = 200, condition = "Operation successful"),
154 @ResponseCode(code = 401, condition = "Unauthorized"),
155 @ResponseCode(code = 404, condition = "Not Found"),
156 @ResponseCode(code = 501, condition = "Not Implemented") })
157 public Response showPort(
158 @PathParam("portUUID") String portUUID,
160 @QueryParam("fields") List<String> fields ) {
161 INeutronPortCRUD portInterface = NeutronCRUDInterfaces.getINeutronPortCRUD(this);
162 if (portInterface == null) {
163 throw new ServiceUnavailableException("Port CRUD Interface "
164 + RestMessages.SERVICEUNAVAILABLE.toString());
166 if (!portInterface.portExists(portUUID)) {
167 throw new ResourceNotFoundException("port UUID does not exist.");
169 if (fields.size() > 0) {
170 NeutronPort ans = portInterface.getPort(portUUID);
171 return Response.status(200).entity(
172 new NeutronPortRequest(extractFields(ans, fields))).build();
174 return Response.status(200).entity(
175 new NeutronPortRequest(portInterface.getPort(portUUID))).build();
180 * Creates new Ports */
183 @Produces({ MediaType.APPLICATION_JSON })
184 @Consumes({ MediaType.APPLICATION_JSON })
185 //@TypeHint(OpenStackPorts.class)
187 @ResponseCode(code = 201, condition = "Created"),
188 @ResponseCode(code = 400, condition = "Bad Request"),
189 @ResponseCode(code = 401, condition = "Unauthorized"),
190 @ResponseCode(code = 403, condition = "Forbidden"),
191 @ResponseCode(code = 404, condition = "Not Found"),
192 @ResponseCode(code = 409, condition = "Conflict"),
193 @ResponseCode(code = 501, condition = "Not Implemented"),
194 @ResponseCode(code = 503, condition = "MAC generation failure") })
195 public Response createPorts(final NeutronPortRequest input) {
196 INeutronPortCRUD portInterface = NeutronCRUDInterfaces.getINeutronPortCRUD(this);
197 if (portInterface == null) {
198 throw new ServiceUnavailableException("Port CRUD Interface "
199 + RestMessages.SERVICEUNAVAILABLE.toString());
201 INeutronNetworkCRUD networkInterface = NeutronCRUDInterfaces.getINeutronNetworkCRUD( this);
202 if (networkInterface == null) {
203 throw new ServiceUnavailableException("Network CRUD Interface "
204 + RestMessages.SERVICEUNAVAILABLE.toString());
206 INeutronSubnetCRUD subnetInterface = NeutronCRUDInterfaces.getINeutronSubnetCRUD( this);
207 if (subnetInterface == null) {
208 throw new ServiceUnavailableException("Subnet CRUD Interface "
209 + RestMessages.SERVICEUNAVAILABLE.toString());
211 if (input.isSingleton()) {
212 NeutronPort singleton = input.getSingleton();
215 * the port must be part of an existing network, must not already exist,
216 * have a valid MAC and the MAC not be in use
218 if (singleton.getNetworkUUID() == null) {
219 throw new BadRequestException("network UUID musy be specified");
221 if (portInterface.portExists(singleton.getID())) {
222 throw new BadRequestException("port UUID already exists");
224 if (!networkInterface.networkExists(singleton.getNetworkUUID())) {
225 throw new ResourceNotFoundException("network UUID does not exist.");
227 if (singleton.getMacAddress() == null ||
228 !singleton.getMacAddress().matches(mac_regex)) {
229 throw new BadRequestException("MAC address not properly formatted");
231 if (portInterface.macInUse(singleton.getMacAddress())) {
232 throw new ResourceConflictException("MAC Address is in use.");
235 * if fixed IPs are specified, each one has to have an existing subnet ID
236 * that is in the same scoping network as the port. In addition, if an IP
237 * address is specified it has to be a valid address for the subnet and not
240 List<Neutron_IPs> fixedIPs = singleton.getFixedIPs();
241 if (fixedIPs != null && fixedIPs.size() > 0) {
242 Iterator<Neutron_IPs> fixedIPIterator = fixedIPs.iterator();
243 while (fixedIPIterator.hasNext()) {
244 Neutron_IPs ip = fixedIPIterator.next();
245 if (ip.getSubnetUUID() == null) {
246 throw new BadRequestException("subnet UUID not specified");
248 if (!subnetInterface.subnetExists(ip.getSubnetUUID())) {
249 throw new BadRequestException("subnet UUID must exists");
251 NeutronSubnet subnet = subnetInterface.getSubnet(ip.getSubnetUUID());
252 if (!singleton.getNetworkUUID().equalsIgnoreCase(subnet.getNetworkUUID())) {
253 throw new BadRequestException("network UUID must match that of subnet");
255 if (ip.getIpAddress() != null) {
256 if (!subnet.isValidIP(ip.getIpAddress())) {
257 throw new BadRequestException("IP address is not valid");
259 if (subnet.isIPInUse(ip.getIpAddress())) {
260 throw new ResourceConflictException("IP address is in use.");
266 Object[] instances = ServiceHelper.getGlobalInstances(INeutronPortAware.class, this, null);
267 if (instances != null) {
268 for (Object instance : instances) {
269 INeutronPortAware service = (INeutronPortAware) instance;
270 int status = service.canCreatePort(singleton);
271 if (status < 200 || status > 299) {
272 return Response.status(status).build();
278 // add the port to the cache
279 portInterface.addPort(singleton);
280 if (instances != null) {
281 for (Object instance : instances) {
282 INeutronPortAware service = (INeutronPortAware) instance;
283 service.neutronPortCreated(singleton);
287 List<NeutronPort> bulk = input.getBulk();
288 Iterator<NeutronPort> i = bulk.iterator();
289 HashMap<String, NeutronPort> testMap = new HashMap<String, NeutronPort>();
290 Object[] instances = ServiceHelper.getGlobalInstances(INeutronPortAware.class, this, null);
291 while (i.hasNext()) {
292 NeutronPort test = i.next();
295 * the port must be part of an existing network, must not already exist,
296 * have a valid MAC and the MAC not be in use. Further the bulk request
297 * can't already contain a new port with the same UUID
299 if (portInterface.portExists(test.getID())) {
300 throw new BadRequestException("port UUID already exists");
302 if (testMap.containsKey(test.getID())) {
303 throw new BadRequestException("port UUID already exists");
305 for (NeutronPort check : testMap.values()) {
306 if (test.getMacAddress().equalsIgnoreCase(check.getMacAddress())) {
307 throw new ResourceConflictException("MAC address already allocated");
309 for (Neutron_IPs test_fixedIP : test.getFixedIPs()) {
310 for (Neutron_IPs check_fixedIP : check.getFixedIPs()) {
311 if (test_fixedIP.getIpAddress().equals(check_fixedIP.getIpAddress())) {
312 throw new ResourceConflictException("IP address already allocated");
317 testMap.put(test.getID(), test);
318 if (!networkInterface.networkExists(test.getNetworkUUID())) {
319 throw new ResourceNotFoundException("network UUID does not exist.");
321 if (!test.getMacAddress().matches(mac_regex)) {
322 throw new BadRequestException("MAC address not properly formatted");
324 if (portInterface.macInUse(test.getMacAddress())) {
325 throw new ResourceConflictException("MAC address in use");
329 * if fixed IPs are specified, each one has to have an existing subnet ID
330 * that is in the same scoping network as the port. In addition, if an IP
331 * address is specified it has to be a valid address for the subnet and not
332 * already in use (or be the gateway IP address of the subnet)
334 List<Neutron_IPs> fixedIPs = test.getFixedIPs();
335 if (fixedIPs != null && fixedIPs.size() > 0) {
336 Iterator<Neutron_IPs> fixedIPIterator = fixedIPs.iterator();
337 while (fixedIPIterator.hasNext()) {
338 Neutron_IPs ip = fixedIPIterator.next();
339 if (ip.getSubnetUUID() == null) {
340 throw new BadRequestException("subnet UUID must be specified");
342 if (!subnetInterface.subnetExists(ip.getSubnetUUID())) {
343 throw new BadRequestException("subnet UUID doesn't exists");
345 NeutronSubnet subnet = subnetInterface.getSubnet(ip.getSubnetUUID());
346 if (!test.getNetworkUUID().equalsIgnoreCase(subnet.getNetworkUUID())) {
347 throw new BadRequestException("network UUID must match that of subnet");
349 if (ip.getIpAddress() != null) {
350 if (!subnet.isValidIP(ip.getIpAddress())) {
351 throw new BadRequestException("ip address not valid");
353 //TODO: need to add consideration for a fixed IP being assigned the same address as a allocated IP in the
355 if (subnet.isIPInUse(ip.getIpAddress())) {
356 throw new ResourceConflictException("IP address in use");
361 if (instances != null) {
362 for (Object instance : instances) {
363 INeutronPortAware service = (INeutronPortAware) instance;
364 int status = service.canCreatePort(test);
365 if (status < 200 || status > 299) {
366 return Response.status(status).build();
372 //once everything has passed, then we can add to the cache
374 while (i.hasNext()) {
375 NeutronPort test = i.next();
376 portInterface.addPort(test);
377 if (instances != null) {
378 for (Object instance : instances) {
379 INeutronPortAware service = (INeutronPortAware) instance;
380 service.neutronPortCreated(test);
385 return Response.status(201).entity(input).build();
393 @Produces({ MediaType.APPLICATION_JSON })
394 @Consumes({ MediaType.APPLICATION_JSON })
395 //@TypeHint(OpenStackPorts.class)
397 @ResponseCode(code = 200, condition = "Operation successful"),
398 @ResponseCode(code = 400, condition = "Bad Request"),
399 @ResponseCode(code = 401, condition = "Unauthorized"),
400 @ResponseCode(code = 403, condition = "Forbidden"),
401 @ResponseCode(code = 404, condition = "Not Found"),
402 @ResponseCode(code = 409, condition = "Conflict"),
403 @ResponseCode(code = 501, condition = "Not Implemented") })
404 public Response updatePort(
405 @PathParam("portUUID") String portUUID,
406 NeutronPortRequest input
408 INeutronPortCRUD portInterface = NeutronCRUDInterfaces.getINeutronPortCRUD(this);
409 if (portInterface == null) {
410 throw new ServiceUnavailableException("Port CRUD Interface "
411 + RestMessages.SERVICEUNAVAILABLE.toString());
413 INeutronSubnetCRUD subnetInterface = NeutronCRUDInterfaces.getINeutronSubnetCRUD( this);
414 if (subnetInterface == null) {
415 throw new ServiceUnavailableException("Subnet CRUD Interface "
416 + RestMessages.SERVICEUNAVAILABLE.toString());
419 // port has to exist and only a single delta is supported
420 if (!portInterface.portExists(portUUID)) {
421 throw new ResourceNotFoundException("port UUID does not exist.");
423 NeutronPort target = portInterface.getPort(portUUID);
424 if (!input.isSingleton()) {
425 throw new BadRequestException("only singleton edit suported");
427 NeutronPort singleton = input.getSingleton();
428 NeutronPort original = portInterface.getPort(portUUID);
430 // deltas restricted by Neutron
431 if (singleton.getID() != null || singleton.getTenantID() != null ||
432 singleton.getStatus() != null) {
433 throw new BadRequestException("attribute change blocked by Neutron");
436 Object[] instances = ServiceHelper.getGlobalInstances(INeutronPortAware.class, this, null);
437 if (instances != null) {
438 for (Object instance : instances) {
439 INeutronPortAware service = (INeutronPortAware) instance;
440 int status = service.canUpdatePort(singleton, original);
441 if (status < 200 || status > 299) {
442 return Response.status(status).build();
447 // Verify the new fixed ips are valid
448 List<Neutron_IPs> fixedIPs = singleton.getFixedIPs();
449 if (fixedIPs != null && fixedIPs.size() > 0) {
450 Iterator<Neutron_IPs> fixedIPIterator = fixedIPs.iterator();
451 while (fixedIPIterator.hasNext()) {
452 Neutron_IPs ip = fixedIPIterator.next();
453 if (ip.getSubnetUUID() == null) {
454 throw new BadRequestException("subnet UUID must be specified");
456 if (!subnetInterface.subnetExists(ip.getSubnetUUID())) {
457 throw new BadRequestException("subnet UUID doesn't exist.");
459 NeutronSubnet subnet = subnetInterface.getSubnet(ip.getSubnetUUID());
460 if (!target.getNetworkUUID().equalsIgnoreCase(subnet.getNetworkUUID())) {
461 throw new BadRequestException("network UUID must match that of subnet");
463 if (ip.getIpAddress() != null) {
464 if (!subnet.isValidIP(ip.getIpAddress())) {
465 throw new BadRequestException("invalid IP address");
467 if (subnet.isIPInUse(ip.getIpAddress())) {
468 throw new ResourceConflictException("IP address in use");
474 // TODO: Support change of security groups
475 // update the port and return the modified object
476 portInterface.updatePort(portUUID, singleton);
477 NeutronPort updatedPort = portInterface.getPort(portUUID);
478 if (instances != null) {
479 for (Object instance : instances) {
480 INeutronPortAware service = (INeutronPortAware) instance;
481 service.neutronPortUpdated(updatedPort);
484 return Response.status(200).entity(
485 new NeutronPortRequest(updatedPort)).build();
495 @ResponseCode(code = 204, condition = "No Content"),
496 @ResponseCode(code = 401, condition = "Unauthorized"),
497 @ResponseCode(code = 403, condition = "Forbidden"),
498 @ResponseCode(code = 404, condition = "Not Found"),
499 @ResponseCode(code = 501, condition = "Not Implemented") })
500 public Response deletePort(
501 @PathParam("portUUID") String portUUID) {
502 INeutronPortCRUD portInterface = NeutronCRUDInterfaces.getINeutronPortCRUD(this);
503 if (portInterface == null) {
504 throw new ServiceUnavailableException("Port CRUD Interface "
505 + RestMessages.SERVICEUNAVAILABLE.toString());
508 // port has to exist and not be owned by anyone. then it can be removed from the cache
509 if (!portInterface.portExists(portUUID)) {
510 throw new ResourceNotFoundException("port UUID does not exist.");
512 NeutronPort port = portInterface.getPort(portUUID);
513 if (port.getDeviceID() != null ||
514 port.getDeviceOwner() != null) {
515 Response.status(403).build();
517 NeutronPort singleton = portInterface.getPort(portUUID);
518 Object[] instances = ServiceHelper.getGlobalInstances(INeutronPortAware.class, this, null);
519 if (instances != null) {
520 for (Object instance : instances) {
521 INeutronPortAware service = (INeutronPortAware) instance;
522 int status = service.canDeletePort(singleton);
523 if (status < 200 || status > 299) {
524 return Response.status(status).build();
528 portInterface.removePort(portUUID);
529 if (instances != null) {
530 for (Object instance : instances) {
531 INeutronPortAware service = (INeutronPortAware) instance;
532 service.neutronPortDeleted(singleton);
535 return Response.status(204).build();