Add constraints to delete_switch()
Change-Id: I35486ac4958c2876ecfe41e5066181bc46964295
diff --git a/db/db.py b/db/db.py
index 97d4b18..1dc32bc 100644
--- a/db/db.py
+++ b/db/db.py
@@ -138,8 +138,22 @@
self.connection.rollback()
raise
+ # Delete the specified switch
+ #
+ # Constraints:
+ # 1. The switch must exist
+ # 2. The switch may not be referenced by any ports -
+ # delete them first!
def delete_switch(self, switch_id):
+ switch = self.get_switch(switch_id)
+ if switch is None:
+ raise InputError("Cannot delete switch ID %s which does not exist" % switch_id)
+ ports = self.get_ports_by_switch(switch_id)
+ if ports is not None:
+ raise InputError("Cannot delete switch ID %s when it still has %d ports" %
+ (switch_id, len(ports)))
self._delete_row("switch", "switch_id", switch_id)
+ return switch_id
def delete_port(self, port_id):
self._delete_row("port", "port_id", port_id)