485 return retme |
485 return retme |
486 |
486 |
487 def get_subnets_count(self, context, filters=None): |
487 def get_subnets_count(self, context, filters=None): |
488 return len(self.get_ipnets(context, filters)) |
488 return len(self.get_ipnets(context, filters)) |
489 |
489 |
490 def _release_subnet_dhcp_port(self, context, subnet): |
490 def _release_subnet_dhcp_port(self, context, subnet, delete_network): |
491 """Release any dhcp port associated with the subnet""" |
491 """Release any dhcp port associated with the subnet""" |
492 filters = dict(evs=subnet['network_id']) |
492 filters = dict(evs=subnet['network_id']) |
493 portlist = self.get_ports(context, filters) |
493 portlist = self.get_ports(context, filters) |
494 if len(portlist) == 1: |
494 |
|
495 if delete_network: |
|
496 # One can delete a network if there is only one port that has a |
|
497 # VNIC attached to it and that port happens to be a DHCP port. |
|
498 ports_with_deviceid = [port for port in portlist |
|
499 if port['device_id'] != ''] |
|
500 update_subnet = len(ports_with_deviceid) == 1 |
|
501 else: |
|
502 # One can delete a subnet if there is only one port and that |
|
503 # port happens to be a DHCP port. |
|
504 update_subnet = len(portlist) == 1 |
|
505 if update_subnet: |
495 # the lone port is a dhcp port created by dhcp agent |
506 # the lone port is a dhcp port created by dhcp agent |
496 # it must be released before we can delete the subnet |
507 # it must be released before we can delete the subnet |
497 assert portlist[0]['device_owner'] == 'network:dhcp' |
|
498 subnet_update = {'subnet': {'enable_dhcp': False}, |
508 subnet_update = {'subnet': {'enable_dhcp': False}, |
499 'evs_rpccall_sync': True} |
509 'evs_rpccall_sync': True} |
500 self.update_subnet(context, subnet['id'], subnet_update) |
510 self.update_subnet(context, subnet['id'], subnet_update) |
501 |
511 |
502 def delete_subnet(self, context, id): |
512 def delete_subnet(self, context, id): |
503 try: |
513 try: |
504 subnet = self.get_subnet(context, id) |
514 subnet = self.get_subnet(context, id) |
513 # Since, there is no subnet.delete.start event, we use an another |
523 # Since, there is no subnet.delete.start event, we use an another |
514 # approach of updating the subnet's enable_dhcp attribute to |
524 # approach of updating the subnet's enable_dhcp attribute to |
515 # False that in turn sends a subnet.udpate notification. This |
525 # False that in turn sends a subnet.udpate notification. This |
516 # results in DHCP agent releasing the port. |
526 # results in DHCP agent releasing the port. |
517 if subnet['enable_dhcp']: |
527 if subnet['enable_dhcp']: |
518 self._release_subnet_dhcp_port(context, subnet) |
528 self._release_subnet_dhcp_port(context, subnet, False) |
519 evs.removeIPnet(id) |
529 evs.removeIPnet(id) |
520 except radcli.ObjectError as oe: |
530 except radcli.ObjectError as oe: |
521 raise EVSControllerError(oe.get_payload().errmsg) |
531 raise EVSControllerError(oe.get_payload().errmsg) |
522 |
532 |
523 # notify dhcp agent |
533 # notify dhcp agent |
647 try: |
657 try: |
648 filters = dict(network_id=id) |
658 filters = dict(network_id=id) |
649 subnets = self.get_subnets(context, filters=filters) |
659 subnets = self.get_subnets(context, filters=filters) |
650 dhcp_subnets = [s for s in subnets if s['enable_dhcp']] |
660 dhcp_subnets = [s for s in subnets if s['enable_dhcp']] |
651 for subnet in dhcp_subnets: |
661 for subnet in dhcp_subnets: |
652 self._release_subnet_dhcp_port(context, subnet) |
662 self._release_subnet_dhcp_port(context, subnet, True) |
653 self._evsc.deleteEVS(id, context.tenant_id) |
663 self._evsc.deleteEVS(id, context.tenant_id) |
654 except radcli.ObjectError as oe: |
664 except radcli.ObjectError as oe: |
655 raise EVSControllerError(oe.get_payload().errmsg) |
665 raise EVSControllerError(oe.get_payload().errmsg) |
656 |
666 |
657 # notify dhcp agent of network deletion |
667 # notify dhcp agent of network deletion |