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.ServiceUnavailableException;
40 import org.opendaylight.controller.sal.utils.ServiceHelper;
43 * Open DOVE Northbound REST APIs.<br>
44 * This class provides REST APIs for managing the open DOVE
48 * Authentication scheme : <b>HTTP Basic</b><br>
49 * Authentication realm : <b>opendaylight</b><br>
50 * Transport : <b>HTTP and HTTPS</b><br>
52 * HTTPS Authentication is disabled by default. Administrator can enable it in
53 * tomcat-server.xml after adding a proper keystore / SSL certificate from a
54 * trusted authority.<br>
56 * http://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html#Configuration
61 public class NeutronPortsNorthbound {
63 final String mac_regex="^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$";
65 private NeutronPort extractFields(NeutronPort o, List<String> fields) {
66 return o.extractFields(fields);
70 * Returns a list of all Ports */
73 @Produces({ MediaType.APPLICATION_JSON })
74 //@TypeHint(OpenStackPorts.class)
76 @ResponseCode(code = 200, condition = "Operation successful"),
77 @ResponseCode(code = 401, condition = "Unauthorized"),
78 @ResponseCode(code = 501, condition = "Not Implemented") })
79 public Response listPorts(
81 @QueryParam("fields") List<String> fields,
82 // note: openstack isn't clear about filtering on lists, so we aren't handling them
83 @QueryParam("id") String queryID,
84 @QueryParam("network_id") String queryNetworkID,
85 @QueryParam("name") String queryName,
86 @QueryParam("admin_state_up") String queryAdminStateUp,
87 @QueryParam("status") String queryStatus,
88 @QueryParam("mac_address") String queryMACAddress,
89 @QueryParam("device_id") String queryDeviceID,
90 @QueryParam("device_owner") String queryDeviceOwner,
91 @QueryParam("tenant_id") String queryTenantID,
93 @QueryParam("limit") String limit,
94 @QueryParam("marker") String marker,
95 @QueryParam("page_reverse") String pageReverse
96 // sorting not supported
98 INeutronPortCRUD portInterface = NeutronCRUDInterfaces.getINeutronPortCRUD(this);
99 if (portInterface == null) {
100 throw new ServiceUnavailableException("Port CRUD Interface "
101 + RestMessages.SERVICEUNAVAILABLE.toString());
103 List<NeutronPort> allPorts = portInterface.getAllPorts();
104 List<NeutronPort> ans = new ArrayList<NeutronPort>();
105 Iterator<NeutronPort> i = allPorts.iterator();
106 while (i.hasNext()) {
107 NeutronPort oSS = i.next();
108 if ((queryID == null || queryID.equals(oSS.getID())) &&
109 (queryNetworkID == null || queryNetworkID.equals(oSS.getNetworkUUID())) &&
110 (queryName == null || queryName.equals(oSS.getName())) &&
111 (queryAdminStateUp == null || queryAdminStateUp.equals(oSS.getAdminStateUp())) &&
112 (queryStatus == null || queryStatus.equals(oSS.getStatus())) &&
113 (queryMACAddress == null || queryMACAddress.equals(oSS.getMacAddress())) &&
114 (queryDeviceID == null || queryDeviceID.equals(oSS.getDeviceID())) &&
115 (queryDeviceOwner == null || queryDeviceOwner.equals(oSS.getDeviceOwner())) &&
116 (queryTenantID == null || queryTenantID.equals(oSS.getTenantID()))) {
117 if (fields.size() > 0) {
118 ans.add(extractFields(oSS,fields));
124 //TODO: apply pagination to results
125 return Response.status(200).entity(
126 new NeutronPortRequest(ans)).build();
130 * Returns a specific Port */
134 @Produces({ MediaType.APPLICATION_JSON })
135 //@TypeHint(OpenStackPorts.class)
137 @ResponseCode(code = 200, condition = "Operation successful"),
138 @ResponseCode(code = 401, condition = "Unauthorized"),
139 @ResponseCode(code = 404, condition = "Not Found"),
140 @ResponseCode(code = 501, condition = "Not Implemented") })
141 public Response showPort(
142 @PathParam("portUUID") String portUUID,
144 @QueryParam("fields") List<String> fields ) {
145 INeutronPortCRUD portInterface = NeutronCRUDInterfaces.getINeutronPortCRUD(this);
146 if (portInterface == null) {
147 throw new ServiceUnavailableException("Port CRUD Interface "
148 + RestMessages.SERVICEUNAVAILABLE.toString());
150 if (!portInterface.portExists(portUUID)) {
151 return Response.status(404).build();
153 if (fields.size() > 0) {
154 NeutronPort ans = portInterface.getPort(portUUID);
155 return Response.status(200).entity(
156 new NeutronPortRequest(extractFields(ans, fields))).build();
158 return Response.status(200).entity(
159 new NeutronPortRequest(portInterface.getPort(portUUID))).build();
164 * Creates new Ports */
167 @Produces({ MediaType.APPLICATION_JSON })
168 @Consumes({ MediaType.APPLICATION_JSON })
169 //@TypeHint(OpenStackPorts.class)
171 @ResponseCode(code = 201, condition = "Created"),
172 @ResponseCode(code = 400, condition = "Bad Request"),
173 @ResponseCode(code = 401, condition = "Unauthorized"),
174 @ResponseCode(code = 403, condition = "Forbidden"),
175 @ResponseCode(code = 404, condition = "Not Found"),
176 @ResponseCode(code = 409, condition = "Conflict"),
177 @ResponseCode(code = 501, condition = "Not Implemented"),
178 @ResponseCode(code = 503, condition = "MAC generation failure") })
179 public Response createPorts(final NeutronPortRequest input) {
180 INeutronPortCRUD portInterface = NeutronCRUDInterfaces.getINeutronPortCRUD(this);
181 if (portInterface == null) {
182 throw new ServiceUnavailableException("Port CRUD Interface "
183 + RestMessages.SERVICEUNAVAILABLE.toString());
185 INeutronNetworkCRUD networkInterface = NeutronCRUDInterfaces.getINeutronNetworkCRUD( this);
186 if (networkInterface == null) {
187 throw new ServiceUnavailableException("Network CRUD Interface "
188 + RestMessages.SERVICEUNAVAILABLE.toString());
190 INeutronSubnetCRUD subnetInterface = NeutronCRUDInterfaces.getINeutronSubnetCRUD( this);
191 if (subnetInterface == null) {
192 throw new ServiceUnavailableException("Subnet CRUD Interface "
193 + RestMessages.SERVICEUNAVAILABLE.toString());
195 if (input.isSingleton()) {
196 NeutronPort singleton = input.getSingleton();
199 * the port must be part of an existing network, must not already exist,
200 * have a valid MAC and the MAC not be in use
202 if (singleton.getNetworkUUID() == null) {
203 return Response.status(400).build();
205 if (portInterface.portExists(singleton.getID())) {
206 return Response.status(400).build();
208 if (!networkInterface.networkExists(singleton.getNetworkUUID())) {
209 return Response.status(404).build();
211 if (singleton.getMacAddress() == null ||
212 !singleton.getMacAddress().matches(mac_regex)) {
213 return Response.status(400).build();
215 if (portInterface.macInUse(singleton.getMacAddress())) {
216 return Response.status(409).build();
218 Object[] instances = ServiceHelper.getGlobalInstances(INeutronPortAware.class, this, null);
219 if (instances != null) {
220 for (Object instance : instances) {
221 INeutronPortAware service = (INeutronPortAware) instance;
222 int status = service.canCreatePort(singleton);
223 if (status < 200 || status > 299) {
224 return Response.status(status).build();
229 * if fixed IPs are specified, each one has to have an existing subnet ID
230 * that is in the same scoping network as the port. In addition, if an IP
231 * address is specified it has to be a valid address for the subnet and not
234 List<Neutron_IPs> fixedIPs = singleton.getFixedIPs();
235 if (fixedIPs != null && fixedIPs.size() > 0) {
236 Iterator<Neutron_IPs> fixedIPIterator = fixedIPs.iterator();
237 while (fixedIPIterator.hasNext()) {
238 Neutron_IPs ip = fixedIPIterator.next();
239 if (ip.getSubnetUUID() == null) {
240 return Response.status(400).build();
242 if (!subnetInterface.subnetExists(ip.getSubnetUUID())) {
243 return Response.status(400).build();
245 NeutronSubnet subnet = subnetInterface.getSubnet(ip.getSubnetUUID());
246 if (!singleton.getNetworkUUID().equalsIgnoreCase(subnet.getNetworkUUID())) {
247 return Response.status(400).build();
249 if (ip.getIpAddress() != null) {
250 if (!subnet.isValidIP(ip.getIpAddress())) {
251 return Response.status(400).build();
253 if (subnet.isIPInUse(ip.getIpAddress())) {
254 return Response.status(409).build();
260 // add the port to the cache
261 portInterface.addPort(singleton);
262 if (instances != null) {
263 for (Object instance : instances) {
264 INeutronPortAware service = (INeutronPortAware) instance;
265 service.neutronPortCreated(singleton);
269 List<NeutronPort> bulk = input.getBulk();
270 Iterator<NeutronPort> i = bulk.iterator();
271 HashMap<String, NeutronPort> testMap = new HashMap<String, NeutronPort>();
272 Object[] instances = ServiceHelper.getGlobalInstances(INeutronPortAware.class, this, null);
273 while (i.hasNext()) {
274 NeutronPort test = i.next();
277 * the port must be part of an existing network, must not already exist,
278 * have a valid MAC and the MAC not be in use. Further the bulk request
279 * can't already contain a new port with the same UUID
281 if (portInterface.portExists(test.getID())) {
282 return Response.status(400).build();
284 if (testMap.containsKey(test.getID())) {
285 return Response.status(400).build();
287 for (NeutronPort check : testMap.values()) {
288 if (test.getMacAddress().equalsIgnoreCase(check.getMacAddress())) {
289 return Response.status(409).build();
291 for (Neutron_IPs test_fixedIP : test.getFixedIPs()) {
292 for (Neutron_IPs check_fixedIP : check.getFixedIPs()) {
293 if (test_fixedIP.getIpAddress().equals(check_fixedIP.getIpAddress())) {
294 return Response.status(409).build();
299 testMap.put(test.getID(), test);
300 if (!networkInterface.networkExists(test.getNetworkUUID())) {
301 return Response.status(404).build();
303 if (!test.getMacAddress().matches(mac_regex)) {
304 return Response.status(400).build();
306 if (portInterface.macInUse(test.getMacAddress())) {
307 return Response.status(409).build();
309 if (instances != null) {
310 for (Object instance : instances) {
311 INeutronPortAware service = (INeutronPortAware) instance;
312 int status = service.canCreatePort(test);
313 if (status < 200 || status > 299) {
314 return Response.status(status).build();
319 * if fixed IPs are specified, each one has to have an existing subnet ID
320 * that is in the same scoping network as the port. In addition, if an IP
321 * address is specified it has to be a valid address for the subnet and not
322 * already in use (or be the gateway IP address of the subnet)
324 List<Neutron_IPs> fixedIPs = test.getFixedIPs();
325 if (fixedIPs != null && fixedIPs.size() > 0) {
326 Iterator<Neutron_IPs> fixedIPIterator = fixedIPs.iterator();
327 while (fixedIPIterator.hasNext()) {
328 Neutron_IPs ip = fixedIPIterator.next();
329 if (ip.getSubnetUUID() == null) {
330 return Response.status(400).build();
332 if (!subnetInterface.subnetExists(ip.getSubnetUUID())) {
333 return Response.status(400).build();
335 NeutronSubnet subnet = subnetInterface.getSubnet(ip.getSubnetUUID());
336 if (!test.getNetworkUUID().equalsIgnoreCase(subnet.getNetworkUUID())) {
337 return Response.status(400).build();
339 if (ip.getIpAddress() != null) {
340 if (!subnet.isValidIP(ip.getIpAddress())) {
341 return Response.status(400).build();
343 //TODO: need to add consideration for a fixed IP being assigned the same address as a allocated IP in the
345 if (subnet.isIPInUse(ip.getIpAddress())) {
346 return Response.status(409).build();
353 //once everything has passed, then we can add to the cache
355 while (i.hasNext()) {
356 NeutronPort test = i.next();
357 portInterface.addPort(test);
358 if (instances != null) {
359 for (Object instance : instances) {
360 INeutronPortAware service = (INeutronPortAware) instance;
361 service.neutronPortCreated(test);
366 return Response.status(201).entity(input).build();
374 @Produces({ MediaType.APPLICATION_JSON })
375 @Consumes({ MediaType.APPLICATION_JSON })
376 //@TypeHint(OpenStackPorts.class)
378 @ResponseCode(code = 200, condition = "Operation successful"),
379 @ResponseCode(code = 400, condition = "Bad Request"),
380 @ResponseCode(code = 401, condition = "Unauthorized"),
381 @ResponseCode(code = 403, condition = "Forbidden"),
382 @ResponseCode(code = 404, condition = "Not Found"),
383 @ResponseCode(code = 409, condition = "Conflict"),
384 @ResponseCode(code = 501, condition = "Not Implemented") })
385 public Response updatePort(
386 @PathParam("portUUID") String portUUID,
387 NeutronPortRequest input
389 INeutronPortCRUD portInterface = NeutronCRUDInterfaces.getINeutronPortCRUD(this);
390 if (portInterface == null) {
391 throw new ServiceUnavailableException("Port CRUD Interface "
392 + RestMessages.SERVICEUNAVAILABLE.toString());
394 INeutronSubnetCRUD subnetInterface = NeutronCRUDInterfaces.getINeutronSubnetCRUD( this);
395 if (subnetInterface == null) {
396 throw new ServiceUnavailableException("Subnet CRUD Interface "
397 + RestMessages.SERVICEUNAVAILABLE.toString());
400 // port has to exist and only a single delta is supported
401 if (!portInterface.portExists(portUUID)) {
402 return Response.status(404).build();
404 NeutronPort target = portInterface.getPort(portUUID);
405 if (!input.isSingleton()) {
406 return Response.status(400).build();
408 NeutronPort singleton = input.getSingleton();
409 NeutronPort original = portInterface.getPort(portUUID);
411 // deltas restricted by Neutron
412 if (singleton.getID() != null || singleton.getTenantID() != null ||
413 singleton.getStatus() != null) {
414 return Response.status(400).build();
417 Object[] instances = ServiceHelper.getGlobalInstances(INeutronPortAware.class, this, null);
418 if (instances != null) {
419 for (Object instance : instances) {
420 INeutronPortAware service = (INeutronPortAware) instance;
421 int status = service.canUpdatePort(singleton, original);
422 if (status < 200 || status > 299) {
423 return Response.status(status).build();
428 // Verify the new fixed ips are valid
429 List<Neutron_IPs> fixedIPs = singleton.getFixedIPs();
430 if (fixedIPs != null && fixedIPs.size() > 0) {
431 Iterator<Neutron_IPs> fixedIPIterator = fixedIPs.iterator();
432 while (fixedIPIterator.hasNext()) {
433 Neutron_IPs ip = fixedIPIterator.next();
434 if (ip.getSubnetUUID() == null) {
435 return Response.status(400).build();
437 if (!subnetInterface.subnetExists(ip.getSubnetUUID())) {
438 return Response.status(400).build();
440 NeutronSubnet subnet = subnetInterface.getSubnet(ip.getSubnetUUID());
441 if (!target.getNetworkUUID().equalsIgnoreCase(subnet.getNetworkUUID())) {
442 return Response.status(400).build();
444 if (ip.getIpAddress() != null) {
445 if (!subnet.isValidIP(ip.getIpAddress())) {
446 return Response.status(400).build();
448 if (subnet.isIPInUse(ip.getIpAddress())) {
449 return Response.status(409).build();
455 // TODO: Support change of security groups
456 // update the port and return the modified object
457 portInterface.updatePort(portUUID, singleton);
458 NeutronPort updatedPort = portInterface.getPort(portUUID);
459 if (instances != null) {
460 for (Object instance : instances) {
461 INeutronPortAware service = (INeutronPortAware) instance;
462 service.neutronPortUpdated(updatedPort);
465 return Response.status(200).entity(
466 new NeutronPortRequest(updatedPort)).build();
476 @ResponseCode(code = 204, condition = "No Content"),
477 @ResponseCode(code = 401, condition = "Unauthorized"),
478 @ResponseCode(code = 403, condition = "Forbidden"),
479 @ResponseCode(code = 404, condition = "Not Found"),
480 @ResponseCode(code = 501, condition = "Not Implemented") })
481 public Response deletePort(
482 @PathParam("portUUID") String portUUID) {
483 INeutronPortCRUD portInterface = NeutronCRUDInterfaces.getINeutronPortCRUD(this);
484 if (portInterface == null) {
485 throw new ServiceUnavailableException("Port CRUD Interface "
486 + RestMessages.SERVICEUNAVAILABLE.toString());
489 // port has to exist and not be owned by anyone. then it can be removed from the cache
490 if (!portInterface.portExists(portUUID)) {
491 return Response.status(404).build();
493 NeutronPort port = portInterface.getPort(portUUID);
494 if (port.getDeviceID() != null ||
495 port.getDeviceOwner() != null) {
496 Response.status(403).build();
498 NeutronPort singleton = portInterface.getPort(portUUID);
499 Object[] instances = ServiceHelper.getGlobalInstances(INeutronPortAware.class, this, null);
500 if (instances != null) {
501 for (Object instance : instances) {
502 INeutronPortAware service = (INeutronPortAware) instance;
503 int status = service.canDeletePort(singleton);
504 if (status < 200 || status > 299) {
505 return Response.status(status).build();
509 portInterface.removePort(portUUID);
510 if (instances != null) {
511 for (Object instance : instances) {
512 INeutronPortAware service = (INeutronPortAware) instance;
513 service.neutronPortDeleted(singleton);
516 return Response.status(204).build();