Add better distinction of error cases

Add a new NotFoundError exception for use internally, so we can track
specific failure cases.
Also add a new Error class as a central place to store our error
numbers consistently.
In the API protocol, add a "NOTFOUND" error string alongside "ERROR"
to help the admin interface and other callers distinguish error cases
better.
In the admin interface, actually return distinct non-zero errors in
failure cases. Previously, almost all failures would have returned
suceesfully to the calling shell.

Change-Id: Ie382b737a80b7cd41c551e3a4a2a7e0827260bdc
diff --git a/db/db.py b/db/db.py
index 21ead71..010fb84 100644
--- a/db/db.py
+++ b/db/db.py
@@ -31,7 +31,7 @@
     sys.path.insert(0, vlandpath)
     sys.path.insert(0, "%s/.." % vlandpath)
 
-from errors import CriticalError, InputError
+from errors import CriticalError, InputError, NotFoundError
 
 class VlanDB:
     def __init__(self, db_name="vland", username="vland", readonly=True):
@@ -104,12 +104,12 @@
 
         switch = self.get_switch_by_id(switch_id)
         if switch is None:
-            raise InputError("Switch ID %d does not exist" % int(switch_id))
+            raise NotFoundError("Switch ID %d does not exist" % int(switch_id))
 
         for vlan_id in (current_vlan_id, base_vlan_id):
             vlan = self.get_vlan_by_id(vlan_id)
             if vlan is None:
-                raise InputError("VLAN ID %d does not exist" % int(vlan_id))
+                raise NotFoundError("VLAN ID %d does not exist" % int(vlan_id))
 
         port_id = self.get_port_by_switch_and_name(switch_id, name)
         if port_id is not None:
@@ -186,7 +186,7 @@
         for port_id in (port_id1, port_id2):
             port = self.get_port_by_id(int(port_id))
             if port is None:
-                raise InputError("Port ID %d does not exist" % int(port_id))
+                raise NotFoundError("Port ID %d does not exist" % int(port_id))
             if not port.is_trunk:
                 raise InputError("Port ID %d is not in trunk mode" % int(port_id))
             if port.is_locked:
@@ -233,7 +233,7 @@
     def delete_switch(self, switch_id):
         switch = self.get_switch_by_id(switch_id)
         if switch is None:
-            raise InputError("Switch ID %d does not exist" % int(switch_id))
+            raise NotFoundError("Switch ID %d does not exist" % int(switch_id))
         ports = self.get_ports_by_switch(switch_id)
         if ports is not None:
             raise InputError("Cannot delete switch ID %d when it still has %d ports" %
@@ -249,7 +249,7 @@
     def delete_port(self, port_id):
         port = self.get_port_by_id(port_id)
         if port is None:
-            raise InputError("Port ID %d does not exist" % int(port_id))
+            raise NotFoundError("Port ID %d does not exist" % int(port_id))
         if port.is_locked:
             raise InputError("Cannot delete port ID %d as it is locked" % int(port_id))
         self._delete_row("port", "port_id", port_id)
@@ -263,7 +263,7 @@
     def delete_vlan(self, vlan_id):
         vlan = self.get_vlan_by_id(vlan_id)
         if vlan is None:
-            raise InputError("VLAN ID %d does not exist" % int(vlan_id))
+            raise NotFoundError("VLAN ID %d does not exist" % int(vlan_id))
         ports = self.get_ports_by_current_vlan(vlan_id)
         if ports is not None:
             raise InputError("Cannot delete VLAN ID %d when it still has %d ports" %
@@ -284,7 +284,7 @@
     def delete_trunk(self, trunk_id):
         trunk = self.get_trunk_by_id(trunk_id)
         if trunk is None:
-            raise InputError("Trunk ID %d does not exist" % int(trunk_id))
+            raise NotFoundError("Trunk ID %d does not exist" % int(trunk_id))
         ports = self.get_ports_by_trunk(trunk_id)
         for port_id in ports:
             self._set_port_trunk(port_id, TRUNK_ID_NONE)
@@ -581,7 +581,7 @@
     def set_port_is_locked(self, port_id, is_locked):
         port = self.get_port_by_id(port_id)
         if port is None:
-            raise InputError("Port ID %d does not exist" % int(port_id))
+            raise NotFoundError("Port ID %d does not exist" % int(port_id))
         try:
             sql = "UPDATE port SET is_locked=%s WHERE port_id=%s RETURNING port_id"
             data = (is_locked, port_id)
@@ -599,7 +599,7 @@
     def set_port_mode(self, port_id, mode):
         port = self.get_port_by_id(port_id)
         if port is None:
-            raise InputError("Port ID %d does not exist" % int(port_id))
+            raise NotFoundError("Port ID %d does not exist" % int(port_id))
         if mode == "access":
             is_trunk = False
         elif mode == "trunk":
@@ -629,14 +629,14 @@
     def set_current_vlan(self, port_id, vlan_id):
         port = self.get_port_by_id(port_id)
         if port is None:
-            raise InputError("Port ID %d does not exist" % int(port_id))
+            raise NotFoundError("Port ID %d does not exist" % int(port_id))
 
         if port.is_trunk or port.is_locked:
             raise CriticalError("The port is locked")
 
         vlan = self.get_vlan_by_id(vlan_id)
         if vlan is None:
-            raise InputError("VLAN ID %d does not exist" % int(vlan_id))
+            raise NotFoundError("VLAN ID %d does not exist" % int(vlan_id))
 
         try:
             sql = "UPDATE port SET current_vlan_id=%s WHERE port_id=%s RETURNING port_id"
@@ -661,14 +661,14 @@
     def set_base_vlan(self, port_id, vlan_id):
         port = self.get_port_by_id(port_id)
         if port is None:
-            raise InputError("Port ID %d does not exist" % int(port_id))
+            raise NotFoundError("Port ID %d does not exist" % int(port_id))
 
         if port.is_trunk or port.is_locked:
             raise CriticalError("The port is locked")
 
         vlan = self.get_vlan_by_id(vlan_id)
         if vlan is None:
-            raise InputError("VLAN ID %d does not exist" % int(vlan_id))
+            raise NotFoundError("VLAN ID %d does not exist" % int(vlan_id))
         if not vlan.is_base_vlan:
             raise InputError("VLAN ID %d is not a base VLAN" % int(vlan_id))
 
@@ -692,7 +692,7 @@
     def _set_port_trunk(self, port_id, trunk_id):
         port = self.get_port_by_id(port_id)
         if port is None:
-            raise InputError("Port ID %d does not exist" % int(port_id))
+            raise NotFoundError("Port ID %d does not exist" % int(port_id))
         if port.is_locked:
             raise CriticalError("The port is locked")
         try: