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.GET;
19 import javax.ws.rs.POST;
20 import javax.ws.rs.PUT;
21 import javax.ws.rs.Path;
22 import javax.ws.rs.PathParam;
23 import javax.ws.rs.Produces;
24 import javax.ws.rs.QueryParam;
25 import javax.ws.rs.core.MediaType;
26 import javax.ws.rs.core.Response;
28 import org.codehaus.enunciate.jaxrs.ResponseCode;
29 import org.codehaus.enunciate.jaxrs.StatusCodes;
30 import org.opendaylight.controller.networkconfig.neutron.INeutronNetworkCRUD;
31 import org.opendaylight.controller.networkconfig.neutron.INeutronPortAware;
32 import org.opendaylight.controller.networkconfig.neutron.INeutronPortCRUD;
33 import org.opendaylight.controller.networkconfig.neutron.INeutronSubnetCRUD;
34 import org.opendaylight.controller.networkconfig.neutron.NeutronCRUDInterfaces;
35 import org.opendaylight.controller.networkconfig.neutron.NeutronPort;
36 import org.opendaylight.controller.networkconfig.neutron.NeutronSubnet;
37 import org.opendaylight.controller.networkconfig.neutron.Neutron_IPs;
38 import org.opendaylight.controller.northbound.commons.RestMessages;
39 import org.opendaylight.controller.northbound.commons.exception.BadRequestException;
40 import org.opendaylight.controller.northbound.commons.exception.ResourceConflictException;
41 import org.opendaylight.controller.northbound.commons.exception.ResourceNotFoundException;
42 import org.opendaylight.controller.northbound.commons.exception.ServiceUnavailableException;
43 import org.opendaylight.controller.sal.utils.ServiceHelper;
46 * Neutron Northbound REST APIs.<br>
47 * This class provides REST APIs for managing neutron port objects
51 * Authentication scheme : <b>HTTP Basic</b><br>
52 * Authentication realm : <b>opendaylight</b><br>
53 * Transport : <b>HTTP and HTTPS</b><br>
55 * HTTPS Authentication is disabled by default. Administrator can enable it in
56 * tomcat-server.xml after adding a proper keystore / SSL certificate from a
57 * trusted authority.<br>
59 * http://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html#Configuration
64 public class NeutronPortsNorthbound {
66 final String mac_regex="^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$";
68 private NeutronPort extractFields(NeutronPort o, List<String> fields) {
69 return o.extractFields(fields);
73 * Returns a list of all Ports */
76 @Produces({ MediaType.APPLICATION_JSON })
77 //@TypeHint(OpenStackPorts.class)
79 @ResponseCode(code = 200, condition = "Operation successful"),
80 @ResponseCode(code = 401, condition = "Unauthorized"),
81 @ResponseCode(code = 501, condition = "Not Implemented") })
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") String limit,
97 @QueryParam("marker") String marker,
98 @QueryParam("page_reverse") String 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));
127 //TODO: apply pagination to results
128 return Response.status(200).entity(
129 new NeutronPortRequest(ans)).build();
133 * Returns a specific Port */
137 @Produces({ MediaType.APPLICATION_JSON })
138 //@TypeHint(OpenStackPorts.class)
140 @ResponseCode(code = 200, condition = "Operation successful"),
141 @ResponseCode(code = 401, condition = "Unauthorized"),
142 @ResponseCode(code = 404, condition = "Not Found"),
143 @ResponseCode(code = 501, condition = "Not Implemented") })
144 public Response showPort(
145 @PathParam("portUUID") String portUUID,
147 @QueryParam("fields") List<String> fields ) {
148 INeutronPortCRUD portInterface = NeutronCRUDInterfaces.getINeutronPortCRUD(this);
149 if (portInterface == null) {
150 throw new ServiceUnavailableException("Port CRUD Interface "
151 + RestMessages.SERVICEUNAVAILABLE.toString());
153 if (!portInterface.portExists(portUUID)) {
154 throw new ResourceNotFoundException("port UUID does not exist.");
156 if (fields.size() > 0) {
157 NeutronPort ans = portInterface.getPort(portUUID);
158 return Response.status(200).entity(
159 new NeutronPortRequest(extractFields(ans, fields))).build();
161 return Response.status(200).entity(
162 new NeutronPortRequest(portInterface.getPort(portUUID))).build();
167 * Creates new Ports */
170 @Produces({ MediaType.APPLICATION_JSON })
171 @Consumes({ MediaType.APPLICATION_JSON })
172 //@TypeHint(OpenStackPorts.class)
174 @ResponseCode(code = 201, condition = "Created"),
175 @ResponseCode(code = 400, condition = "Bad Request"),
176 @ResponseCode(code = 401, condition = "Unauthorized"),
177 @ResponseCode(code = 403, condition = "Forbidden"),
178 @ResponseCode(code = 404, condition = "Not Found"),
179 @ResponseCode(code = 409, condition = "Conflict"),
180 @ResponseCode(code = 501, condition = "Not Implemented"),
181 @ResponseCode(code = 503, condition = "MAC generation failure") })
182 public Response createPorts(final NeutronPortRequest input) {
183 INeutronPortCRUD portInterface = NeutronCRUDInterfaces.getINeutronPortCRUD(this);
184 if (portInterface == null) {
185 throw new ServiceUnavailableException("Port CRUD Interface "
186 + RestMessages.SERVICEUNAVAILABLE.toString());
188 INeutronNetworkCRUD networkInterface = NeutronCRUDInterfaces.getINeutronNetworkCRUD( this);
189 if (networkInterface == null) {
190 throw new ServiceUnavailableException("Network CRUD Interface "
191 + RestMessages.SERVICEUNAVAILABLE.toString());
193 INeutronSubnetCRUD subnetInterface = NeutronCRUDInterfaces.getINeutronSubnetCRUD( this);
194 if (subnetInterface == null) {
195 throw new ServiceUnavailableException("Subnet CRUD Interface "
196 + RestMessages.SERVICEUNAVAILABLE.toString());
198 if (input.isSingleton()) {
199 NeutronPort singleton = input.getSingleton();
202 * the port must be part of an existing network, must not already exist,
203 * have a valid MAC and the MAC not be in use
205 if (singleton.getNetworkUUID() == null) {
206 throw new BadRequestException("network UUID musy be specified");
208 if (portInterface.portExists(singleton.getID())) {
209 throw new BadRequestException("port UUID already exists");
211 if (!networkInterface.networkExists(singleton.getNetworkUUID())) {
212 throw new ResourceNotFoundException("network UUID does not exist.");
214 if (singleton.getMacAddress() == null ||
215 !singleton.getMacAddress().matches(mac_regex)) {
216 throw new BadRequestException("MAC address not properly formatted");
218 if (portInterface.macInUse(singleton.getMacAddress())) {
219 throw new ResourceConflictException("MAC Address is in use.");
221 Object[] instances = ServiceHelper.getGlobalInstances(INeutronPortAware.class, this, null);
222 if (instances != null) {
223 for (Object instance : instances) {
224 INeutronPortAware service = (INeutronPortAware) instance;
225 int status = service.canCreatePort(singleton);
226 if (status < 200 || status > 299) {
227 return Response.status(status).build();
232 * if fixed IPs are specified, each one has to have an existing subnet ID
233 * that is in the same scoping network as the port. In addition, if an IP
234 * address is specified it has to be a valid address for the subnet and not
237 List<Neutron_IPs> fixedIPs = singleton.getFixedIPs();
238 if (fixedIPs != null && fixedIPs.size() > 0) {
239 Iterator<Neutron_IPs> fixedIPIterator = fixedIPs.iterator();
240 while (fixedIPIterator.hasNext()) {
241 Neutron_IPs ip = fixedIPIterator.next();
242 if (ip.getSubnetUUID() == null) {
243 throw new BadRequestException("subnet UUID not specified");
245 if (!subnetInterface.subnetExists(ip.getSubnetUUID())) {
246 throw new BadRequestException("subnet UUID must exists");
248 NeutronSubnet subnet = subnetInterface.getSubnet(ip.getSubnetUUID());
249 if (!singleton.getNetworkUUID().equalsIgnoreCase(subnet.getNetworkUUID())) {
250 throw new BadRequestException("network UUID must match that of subnet");
252 if (ip.getIpAddress() != null) {
253 if (!subnet.isValidIP(ip.getIpAddress())) {
254 throw new BadRequestException("IP address is not valid");
256 if (subnet.isIPInUse(ip.getIpAddress())) {
257 throw new ResourceConflictException("IP address is in use.");
263 // add the port to the cache
264 portInterface.addPort(singleton);
265 if (instances != null) {
266 for (Object instance : instances) {
267 INeutronPortAware service = (INeutronPortAware) instance;
268 service.neutronPortCreated(singleton);
272 List<NeutronPort> bulk = input.getBulk();
273 Iterator<NeutronPort> i = bulk.iterator();
274 HashMap<String, NeutronPort> testMap = new HashMap<String, NeutronPort>();
275 Object[] instances = ServiceHelper.getGlobalInstances(INeutronPortAware.class, this, null);
276 while (i.hasNext()) {
277 NeutronPort test = i.next();
280 * the port must be part of an existing network, must not already exist,
281 * have a valid MAC and the MAC not be in use. Further the bulk request
282 * can't already contain a new port with the same UUID
284 if (portInterface.portExists(test.getID())) {
285 throw new BadRequestException("port UUID already exists");
287 if (testMap.containsKey(test.getID())) {
288 throw new BadRequestException("port UUID already exists");
290 for (NeutronPort check : testMap.values()) {
291 if (test.getMacAddress().equalsIgnoreCase(check.getMacAddress())) {
292 throw new ResourceConflictException("MAC address already allocated");
294 for (Neutron_IPs test_fixedIP : test.getFixedIPs()) {
295 for (Neutron_IPs check_fixedIP : check.getFixedIPs()) {
296 if (test_fixedIP.getIpAddress().equals(check_fixedIP.getIpAddress())) {
297 throw new ResourceConflictException("IP address already allocated");
302 testMap.put(test.getID(), test);
303 if (!networkInterface.networkExists(test.getNetworkUUID())) {
304 throw new ResourceNotFoundException("network UUID does not exist.");
306 if (!test.getMacAddress().matches(mac_regex)) {
307 throw new BadRequestException("MAC address not properly formatted");
309 if (portInterface.macInUse(test.getMacAddress())) {
310 throw new ResourceConflictException("MAC address in use");
312 if (instances != null) {
313 for (Object instance : instances) {
314 INeutronPortAware service = (INeutronPortAware) instance;
315 int status = service.canCreatePort(test);
316 if (status < 200 || status > 299) {
317 return Response.status(status).build();
322 * if fixed IPs are specified, each one has to have an existing subnet ID
323 * that is in the same scoping network as the port. In addition, if an IP
324 * address is specified it has to be a valid address for the subnet and not
325 * already in use (or be the gateway IP address of the subnet)
327 List<Neutron_IPs> fixedIPs = test.getFixedIPs();
328 if (fixedIPs != null && fixedIPs.size() > 0) {
329 Iterator<Neutron_IPs> fixedIPIterator = fixedIPs.iterator();
330 while (fixedIPIterator.hasNext()) {
331 Neutron_IPs ip = fixedIPIterator.next();
332 if (ip.getSubnetUUID() == null) {
333 throw new BadRequestException("subnet UUID must be specified");
335 if (!subnetInterface.subnetExists(ip.getSubnetUUID())) {
336 throw new BadRequestException("subnet UUID doesn't exists");
338 NeutronSubnet subnet = subnetInterface.getSubnet(ip.getSubnetUUID());
339 if (!test.getNetworkUUID().equalsIgnoreCase(subnet.getNetworkUUID())) {
340 throw new BadRequestException("network UUID must match that of subnet");
342 if (ip.getIpAddress() != null) {
343 if (!subnet.isValidIP(ip.getIpAddress())) {
344 throw new BadRequestException("ip address not valid");
346 //TODO: need to add consideration for a fixed IP being assigned the same address as a allocated IP in the
348 if (subnet.isIPInUse(ip.getIpAddress())) {
349 throw new ResourceConflictException("IP address in use");
356 //once everything has passed, then we can add to the cache
358 while (i.hasNext()) {
359 NeutronPort test = i.next();
360 portInterface.addPort(test);
361 if (instances != null) {
362 for (Object instance : instances) {
363 INeutronPortAware service = (INeutronPortAware) instance;
364 service.neutronPortCreated(test);
369 return Response.status(201).entity(input).build();
377 @Produces({ MediaType.APPLICATION_JSON })
378 @Consumes({ MediaType.APPLICATION_JSON })
379 //@TypeHint(OpenStackPorts.class)
381 @ResponseCode(code = 200, condition = "Operation successful"),
382 @ResponseCode(code = 400, condition = "Bad Request"),
383 @ResponseCode(code = 401, condition = "Unauthorized"),
384 @ResponseCode(code = 403, condition = "Forbidden"),
385 @ResponseCode(code = 404, condition = "Not Found"),
386 @ResponseCode(code = 409, condition = "Conflict"),
387 @ResponseCode(code = 501, condition = "Not Implemented") })
388 public Response updatePort(
389 @PathParam("portUUID") String portUUID,
390 NeutronPortRequest input
392 INeutronPortCRUD portInterface = NeutronCRUDInterfaces.getINeutronPortCRUD(this);
393 if (portInterface == null) {
394 throw new ServiceUnavailableException("Port CRUD Interface "
395 + RestMessages.SERVICEUNAVAILABLE.toString());
397 INeutronSubnetCRUD subnetInterface = NeutronCRUDInterfaces.getINeutronSubnetCRUD( this);
398 if (subnetInterface == null) {
399 throw new ServiceUnavailableException("Subnet CRUD Interface "
400 + RestMessages.SERVICEUNAVAILABLE.toString());
403 // port has to exist and only a single delta is supported
404 if (!portInterface.portExists(portUUID)) {
405 throw new ResourceNotFoundException("port UUID does not exist.");
407 NeutronPort target = portInterface.getPort(portUUID);
408 if (!input.isSingleton()) {
409 throw new BadRequestException("only singleton edit suported");
411 NeutronPort singleton = input.getSingleton();
412 NeutronPort original = portInterface.getPort(portUUID);
414 // deltas restricted by Neutron
415 if (singleton.getID() != null || singleton.getTenantID() != null ||
416 singleton.getStatus() != null) {
417 throw new BadRequestException("attribute change blocked by Neutron");
420 Object[] instances = ServiceHelper.getGlobalInstances(INeutronPortAware.class, this, null);
421 if (instances != null) {
422 for (Object instance : instances) {
423 INeutronPortAware service = (INeutronPortAware) instance;
424 int status = service.canUpdatePort(singleton, original);
425 if (status < 200 || status > 299) {
426 return Response.status(status).build();
431 // Verify the new fixed ips are valid
432 List<Neutron_IPs> fixedIPs = singleton.getFixedIPs();
433 if (fixedIPs != null && fixedIPs.size() > 0) {
434 Iterator<Neutron_IPs> fixedIPIterator = fixedIPs.iterator();
435 while (fixedIPIterator.hasNext()) {
436 Neutron_IPs ip = fixedIPIterator.next();
437 if (ip.getSubnetUUID() == null) {
438 throw new BadRequestException("subnet UUID must be specified");
440 if (!subnetInterface.subnetExists(ip.getSubnetUUID())) {
441 throw new BadRequestException("subnet UUID doesn't exist.");
443 NeutronSubnet subnet = subnetInterface.getSubnet(ip.getSubnetUUID());
444 if (!target.getNetworkUUID().equalsIgnoreCase(subnet.getNetworkUUID())) {
445 throw new BadRequestException("network UUID must match that of subnet");
447 if (ip.getIpAddress() != null) {
448 if (!subnet.isValidIP(ip.getIpAddress())) {
449 throw new BadRequestException("invalid IP address");
451 if (subnet.isIPInUse(ip.getIpAddress())) {
452 throw new ResourceConflictException("IP address in use");
458 // TODO: Support change of security groups
459 // update the port and return the modified object
460 portInterface.updatePort(portUUID, singleton);
461 NeutronPort updatedPort = portInterface.getPort(portUUID);
462 if (instances != null) {
463 for (Object instance : instances) {
464 INeutronPortAware service = (INeutronPortAware) instance;
465 service.neutronPortUpdated(updatedPort);
468 return Response.status(200).entity(
469 new NeutronPortRequest(updatedPort)).build();
479 @ResponseCode(code = 204, condition = "No Content"),
480 @ResponseCode(code = 401, condition = "Unauthorized"),
481 @ResponseCode(code = 403, condition = "Forbidden"),
482 @ResponseCode(code = 404, condition = "Not Found"),
483 @ResponseCode(code = 501, condition = "Not Implemented") })
484 public Response deletePort(
485 @PathParam("portUUID") String portUUID) {
486 INeutronPortCRUD portInterface = NeutronCRUDInterfaces.getINeutronPortCRUD(this);
487 if (portInterface == null) {
488 throw new ServiceUnavailableException("Port CRUD Interface "
489 + RestMessages.SERVICEUNAVAILABLE.toString());
492 // port has to exist and not be owned by anyone. then it can be removed from the cache
493 if (!portInterface.portExists(portUUID)) {
494 throw new ResourceNotFoundException("port UUID does not exist.");
496 NeutronPort port = portInterface.getPort(portUUID);
497 if (port.getDeviceID() != null ||
498 port.getDeviceOwner() != null) {
499 Response.status(403).build();
501 NeutronPort singleton = portInterface.getPort(portUUID);
502 Object[] instances = ServiceHelper.getGlobalInstances(INeutronPortAware.class, this, null);
503 if (instances != null) {
504 for (Object instance : instances) {
505 INeutronPortAware service = (INeutronPortAware) instance;
506 int status = service.canDeletePort(singleton);
507 if (status < 200 || status > 299) {
508 return Response.status(status).build();
512 portInterface.removePort(portUUID);
513 if (instances != null) {
514 for (Object instance : instances) {
515 INeutronPortAware service = (INeutronPortAware) instance;
516 service.neutronPortDeleted(singleton);
519 return Response.status(204).build();