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.");
222 * if fixed IPs are specified, each one has to have an existing subnet ID
223 * that is in the same scoping network as the port. In addition, if an IP
224 * address is specified it has to be a valid address for the subnet and not
227 List<Neutron_IPs> fixedIPs = singleton.getFixedIPs();
228 if (fixedIPs != null && fixedIPs.size() > 0) {
229 Iterator<Neutron_IPs> fixedIPIterator = fixedIPs.iterator();
230 while (fixedIPIterator.hasNext()) {
231 Neutron_IPs ip = fixedIPIterator.next();
232 if (ip.getSubnetUUID() == null) {
233 throw new BadRequestException("subnet UUID not specified");
235 if (!subnetInterface.subnetExists(ip.getSubnetUUID())) {
236 throw new BadRequestException("subnet UUID must exists");
238 NeutronSubnet subnet = subnetInterface.getSubnet(ip.getSubnetUUID());
239 if (!singleton.getNetworkUUID().equalsIgnoreCase(subnet.getNetworkUUID())) {
240 throw new BadRequestException("network UUID must match that of subnet");
242 if (ip.getIpAddress() != null) {
243 if (!subnet.isValidIP(ip.getIpAddress())) {
244 throw new BadRequestException("IP address is not valid");
246 if (subnet.isIPInUse(ip.getIpAddress())) {
247 throw new ResourceConflictException("IP address is in use.");
253 Object[] instances = ServiceHelper.getGlobalInstances(INeutronPortAware.class, this, null);
254 if (instances != null) {
255 for (Object instance : instances) {
256 INeutronPortAware service = (INeutronPortAware) instance;
257 int status = service.canCreatePort(singleton);
258 if (status < 200 || status > 299) {
259 return Response.status(status).build();
265 // add the port to the cache
266 portInterface.addPort(singleton);
267 if (instances != null) {
268 for (Object instance : instances) {
269 INeutronPortAware service = (INeutronPortAware) instance;
270 service.neutronPortCreated(singleton);
274 List<NeutronPort> bulk = input.getBulk();
275 Iterator<NeutronPort> i = bulk.iterator();
276 HashMap<String, NeutronPort> testMap = new HashMap<String, NeutronPort>();
277 Object[] instances = ServiceHelper.getGlobalInstances(INeutronPortAware.class, this, null);
278 while (i.hasNext()) {
279 NeutronPort test = i.next();
282 * the port must be part of an existing network, must not already exist,
283 * have a valid MAC and the MAC not be in use. Further the bulk request
284 * can't already contain a new port with the same UUID
286 if (portInterface.portExists(test.getID())) {
287 throw new BadRequestException("port UUID already exists");
289 if (testMap.containsKey(test.getID())) {
290 throw new BadRequestException("port UUID already exists");
292 for (NeutronPort check : testMap.values()) {
293 if (test.getMacAddress().equalsIgnoreCase(check.getMacAddress())) {
294 throw new ResourceConflictException("MAC address already allocated");
296 for (Neutron_IPs test_fixedIP : test.getFixedIPs()) {
297 for (Neutron_IPs check_fixedIP : check.getFixedIPs()) {
298 if (test_fixedIP.getIpAddress().equals(check_fixedIP.getIpAddress())) {
299 throw new ResourceConflictException("IP address already allocated");
304 testMap.put(test.getID(), test);
305 if (!networkInterface.networkExists(test.getNetworkUUID())) {
306 throw new ResourceNotFoundException("network UUID does not exist.");
308 if (!test.getMacAddress().matches(mac_regex)) {
309 throw new BadRequestException("MAC address not properly formatted");
311 if (portInterface.macInUse(test.getMacAddress())) {
312 throw new ResourceConflictException("MAC address in use");
316 * if fixed IPs are specified, each one has to have an existing subnet ID
317 * that is in the same scoping network as the port. In addition, if an IP
318 * address is specified it has to be a valid address for the subnet and not
319 * already in use (or be the gateway IP address of the subnet)
321 List<Neutron_IPs> fixedIPs = test.getFixedIPs();
322 if (fixedIPs != null && fixedIPs.size() > 0) {
323 Iterator<Neutron_IPs> fixedIPIterator = fixedIPs.iterator();
324 while (fixedIPIterator.hasNext()) {
325 Neutron_IPs ip = fixedIPIterator.next();
326 if (ip.getSubnetUUID() == null) {
327 throw new BadRequestException("subnet UUID must be specified");
329 if (!subnetInterface.subnetExists(ip.getSubnetUUID())) {
330 throw new BadRequestException("subnet UUID doesn't exists");
332 NeutronSubnet subnet = subnetInterface.getSubnet(ip.getSubnetUUID());
333 if (!test.getNetworkUUID().equalsIgnoreCase(subnet.getNetworkUUID())) {
334 throw new BadRequestException("network UUID must match that of subnet");
336 if (ip.getIpAddress() != null) {
337 if (!subnet.isValidIP(ip.getIpAddress())) {
338 throw new BadRequestException("ip address not valid");
340 //TODO: need to add consideration for a fixed IP being assigned the same address as a allocated IP in the
342 if (subnet.isIPInUse(ip.getIpAddress())) {
343 throw new ResourceConflictException("IP address in use");
348 if (instances != null) {
349 for (Object instance : instances) {
350 INeutronPortAware service = (INeutronPortAware) instance;
351 int status = service.canCreatePort(test);
352 if (status < 200 || status > 299) {
353 return Response.status(status).build();
359 //once everything has passed, then we can add to the cache
361 while (i.hasNext()) {
362 NeutronPort test = i.next();
363 portInterface.addPort(test);
364 if (instances != null) {
365 for (Object instance : instances) {
366 INeutronPortAware service = (INeutronPortAware) instance;
367 service.neutronPortCreated(test);
372 return Response.status(201).entity(input).build();
380 @Produces({ MediaType.APPLICATION_JSON })
381 @Consumes({ MediaType.APPLICATION_JSON })
382 //@TypeHint(OpenStackPorts.class)
384 @ResponseCode(code = 200, condition = "Operation successful"),
385 @ResponseCode(code = 400, condition = "Bad Request"),
386 @ResponseCode(code = 401, condition = "Unauthorized"),
387 @ResponseCode(code = 403, condition = "Forbidden"),
388 @ResponseCode(code = 404, condition = "Not Found"),
389 @ResponseCode(code = 409, condition = "Conflict"),
390 @ResponseCode(code = 501, condition = "Not Implemented") })
391 public Response updatePort(
392 @PathParam("portUUID") String portUUID,
393 NeutronPortRequest input
395 INeutronPortCRUD portInterface = NeutronCRUDInterfaces.getINeutronPortCRUD(this);
396 if (portInterface == null) {
397 throw new ServiceUnavailableException("Port CRUD Interface "
398 + RestMessages.SERVICEUNAVAILABLE.toString());
400 INeutronSubnetCRUD subnetInterface = NeutronCRUDInterfaces.getINeutronSubnetCRUD( this);
401 if (subnetInterface == null) {
402 throw new ServiceUnavailableException("Subnet CRUD Interface "
403 + RestMessages.SERVICEUNAVAILABLE.toString());
406 // port has to exist and only a single delta is supported
407 if (!portInterface.portExists(portUUID)) {
408 throw new ResourceNotFoundException("port UUID does not exist.");
410 NeutronPort target = portInterface.getPort(portUUID);
411 if (!input.isSingleton()) {
412 throw new BadRequestException("only singleton edit suported");
414 NeutronPort singleton = input.getSingleton();
415 NeutronPort original = portInterface.getPort(portUUID);
417 // deltas restricted by Neutron
418 if (singleton.getID() != null || singleton.getTenantID() != null ||
419 singleton.getStatus() != null) {
420 throw new BadRequestException("attribute change blocked by Neutron");
423 Object[] instances = ServiceHelper.getGlobalInstances(INeutronPortAware.class, this, null);
424 if (instances != null) {
425 for (Object instance : instances) {
426 INeutronPortAware service = (INeutronPortAware) instance;
427 int status = service.canUpdatePort(singleton, original);
428 if (status < 200 || status > 299) {
429 return Response.status(status).build();
434 // Verify the new fixed ips are valid
435 List<Neutron_IPs> fixedIPs = singleton.getFixedIPs();
436 if (fixedIPs != null && fixedIPs.size() > 0) {
437 Iterator<Neutron_IPs> fixedIPIterator = fixedIPs.iterator();
438 while (fixedIPIterator.hasNext()) {
439 Neutron_IPs ip = fixedIPIterator.next();
440 if (ip.getSubnetUUID() == null) {
441 throw new BadRequestException("subnet UUID must be specified");
443 if (!subnetInterface.subnetExists(ip.getSubnetUUID())) {
444 throw new BadRequestException("subnet UUID doesn't exist.");
446 NeutronSubnet subnet = subnetInterface.getSubnet(ip.getSubnetUUID());
447 if (!target.getNetworkUUID().equalsIgnoreCase(subnet.getNetworkUUID())) {
448 throw new BadRequestException("network UUID must match that of subnet");
450 if (ip.getIpAddress() != null) {
451 if (!subnet.isValidIP(ip.getIpAddress())) {
452 throw new BadRequestException("invalid IP address");
454 if (subnet.isIPInUse(ip.getIpAddress())) {
455 throw new ResourceConflictException("IP address in use");
461 // TODO: Support change of security groups
462 // update the port and return the modified object
463 portInterface.updatePort(portUUID, singleton);
464 NeutronPort updatedPort = portInterface.getPort(portUUID);
465 if (instances != null) {
466 for (Object instance : instances) {
467 INeutronPortAware service = (INeutronPortAware) instance;
468 service.neutronPortUpdated(updatedPort);
471 return Response.status(200).entity(
472 new NeutronPortRequest(updatedPort)).build();
482 @ResponseCode(code = 204, condition = "No Content"),
483 @ResponseCode(code = 401, condition = "Unauthorized"),
484 @ResponseCode(code = 403, condition = "Forbidden"),
485 @ResponseCode(code = 404, condition = "Not Found"),
486 @ResponseCode(code = 501, condition = "Not Implemented") })
487 public Response deletePort(
488 @PathParam("portUUID") String portUUID) {
489 INeutronPortCRUD portInterface = NeutronCRUDInterfaces.getINeutronPortCRUD(this);
490 if (portInterface == null) {
491 throw new ServiceUnavailableException("Port CRUD Interface "
492 + RestMessages.SERVICEUNAVAILABLE.toString());
495 // port has to exist and not be owned by anyone. then it can be removed from the cache
496 if (!portInterface.portExists(portUUID)) {
497 throw new ResourceNotFoundException("port UUID does not exist.");
499 NeutronPort port = portInterface.getPort(portUUID);
500 if (port.getDeviceID() != null ||
501 port.getDeviceOwner() != null) {
502 Response.status(403).build();
504 NeutronPort singleton = portInterface.getPort(portUUID);
505 Object[] instances = ServiceHelper.getGlobalInstances(INeutronPortAware.class, this, null);
506 if (instances != null) {
507 for (Object instance : instances) {
508 INeutronPortAware service = (INeutronPortAware) instance;
509 int status = service.canDeletePort(singleton);
510 if (status < 200 || status > 299) {
511 return Response.status(status).build();
515 portInterface.removePort(portUUID);
516 if (instances != null) {
517 for (Object instance : instances) {
518 INeutronPortAware service = (INeutronPortAware) instance;
519 service.neutronPortDeleted(singleton);
522 return Response.status(204).build();