[ 3195 ] Remove duplicate method add_port

Update to the device operations as follows:
1) Add a few test scenarios to test the device state transitions
2) Check whether there is a callback before removing it from the queue
3) Fix a port reference issue when disabling an ONU
4) Update the CLI to handle exceptions from the grpc server

Change-Id: Ic7f41e80279f41d9a4575da5dd49de11294a22d5
diff --git a/voltha/adapters/ponsim_olt/ponsim_olt.py b/voltha/adapters/ponsim_olt/ponsim_olt.py
index 3fed5c4..aecac9f 100644
--- a/voltha/adapters/ponsim_olt/ponsim_olt.py
+++ b/voltha/adapters/ponsim_olt/ponsim_olt.py
@@ -185,6 +185,7 @@
         self.io_port = None
         self.logical_device_id = None
         self.nni_port = None
+        self.ofp_port_no = None
         self.interface = registry('main').get_args().interface
 
     def __del__(self):
@@ -257,6 +258,7 @@
         )
         ld_initialized = self.adapter_agent.create_logical_device(ld)
         cap = OFPPF_1GB_FD | OFPPF_FIBER
+        self.ofp_port_no = info.nni_port
         self.adapter_agent.add_logical_port(ld_initialized.id, LogicalPort(
             id='nni',
             ofp_port=ofp_port(
@@ -401,12 +403,12 @@
         # Disable all child devices first
         self.adapter_agent.disable_all_child_devices(self.device_id)
 
-        # # Remove all child devices
-        # self.adapter_agent.remove_all_child_devices(self.device_id)
-
         # Remove the peer references from this device
         self.adapter_agent.delete_all_peer_references(self.device_id)
 
+        # Set all ports to disabled
+        self.adapter_agent.disable_all_ports(self.device_id)
+
         # close the frameio port
         registry('frameio').close_port(self.io_port)
 
@@ -427,6 +429,9 @@
         device.connect_status = ConnectStatus.REACHABLE
         self.adapter_agent.update_device(device)
 
+        # Set all ports to enabled
+        self.adapter_agent.enable_all_ports(self.device_id)
+
         ld = LogicalDevice(
             # not setting id and datapth_id will let the adapter agent pick id
             desc=ofp_desc(
@@ -453,11 +458,9 @@
         self.adapter_agent.add_logical_port(ld_initialized.id, LogicalPort(
             id='nni',
             ofp_port=ofp_port(
-                # port_no=info.nni_port,
-                # hw_addr=mac_str_to_tuple('00:00:00:00:00:%02x' % info.nni_port),
-                port_no=self.nni_port.port_no,
+                port_no=self.ofp_port_no,
                 hw_addr=mac_str_to_tuple(
-                    '00:00:00:00:00:%02x' % self.nni_port.port_no),
+                    '00:00:00:00:00:%02x' % self.ofp_port_no),
                 name='nni',
                 config=0,
                 state=OFPPS_LIVE,
@@ -481,7 +484,6 @@
         # Reenable all child devices
         self.adapter_agent.reenable_all_child_devices(device.id)
 
-
         # finally, open the frameio port to receive in-band packet_in messages
         self.log.info('registering-frameio')
         self.io_port = registry('frameio').open_port(
diff --git a/voltha/adapters/ponsim_onu/ponsim_onu.py b/voltha/adapters/ponsim_onu/ponsim_onu.py
index 493145c..a131052 100644
--- a/voltha/adapters/ponsim_onu/ponsim_onu.py
+++ b/voltha/adapters/ponsim_onu/ponsim_onu.py
@@ -45,7 +45,6 @@
 
 @implementer(IAdapterInterface)
 class PonSimOnuAdapter(object):
-
     name = 'ponsim_onu'
 
     supported_device_types = [
@@ -123,7 +122,7 @@
 
     def update_flows_bulk(self, device, flows, groups):
         log.info('bulk-flow-update', device_id=device.id,
-                  flows=flows, groups=groups)
+                 flows=flows, groups=groups)
         assert len(groups.items) == 0
         handler = self.devices_handlers[device.id]
         return handler.update_flow_table(flows.items)
@@ -146,7 +145,6 @@
 
 
 class PonSimOnuHandler(object):
-
     def __init__(self, adapter, device_id):
         self.adapter = adapter
         self.adapter_agent = adapter.adapter_agent
@@ -177,7 +175,7 @@
         # populate device info
         device.root = True
         device.vendor = 'ponsim'
-        device.model ='n/a'
+        device.model = 'n/a'
         device.connect_status = ConnectStatus.REACHABLE
         self.adapter_agent.update_device(device)
 
@@ -250,7 +248,6 @@
 
         yield self.incoming_messages.get()
 
-
     @inlineCallbacks
     def reboot(self):
         self.log.info('rebooting', device_id=self.device_id)
@@ -265,7 +262,7 @@
         self.adapter_agent.update_device(device)
 
         # Sleep 10 secs, simulating a reboot
-        #TODO: send alert and clear alert after the reboot
+        # TODO: send alert and clear alert after the reboot
         yield asleep(10)
 
         # Change the operational status back to its previous state.  With a
@@ -278,7 +275,6 @@
         self.adapter_agent.update_device(device)
         self.log.info('rebooted', device_id=self.device_id)
 
-
     def disable(self):
         self.log.info('disabling', device_id=self.device_id)
 
@@ -301,7 +297,8 @@
         port_no = device.proxy_address.channel_id
         port_id = 'uni-{}'.format(port_no)
         try:
-            port = self.adapter_agent.get_logical_port(logical_device_id, port_id)
+            port = self.adapter_agent.get_logical_port(logical_device_id,
+                                                       port_id)
             self.adapter_agent.delete_logical_port(logical_device_id, port)
         except KeyError:
             self.log.info('logical-port-not-found', device_id=self.device_id,
@@ -316,7 +313,8 @@
         # yield self.adapter_agent.update_logical_port(logical_device_id,
         #                                             port)
         # Unregister for proxied message
-        self.adapter_agent.unregister_for_proxied_messages(device.proxy_address)
+        self.adapter_agent.unregister_for_proxied_messages(
+            device.proxy_address)
 
         # TODO:
         # 1) Remove all flows from the device
@@ -324,7 +322,6 @@
 
         self.log.info('disabled', device_id=device.id)
 
-
     def reenable(self):
         self.log.info('re-enabling', device_id=self.device_id)
 
@@ -342,7 +339,11 @@
         self.adapter_agent.register_for_proxied_messages(device.proxy_address)
 
         # Re-enable the ports on that device
-        self.adapter_agent.reenable_all_ports(self.device_id)
+        self.adapter_agent.enable_all_ports(self.device_id)
+
+        # Add the pon port reference to the parent
+        self.adapter_agent.add_port_reference_to_parent(device.id,
+                                                        self.pon_port)
 
         # Update the connect status to REACHABLE
         device.connect_status = ConnectStatus.REACHABLE
@@ -378,7 +379,6 @@
 
         self.log.info('re-enabled', device_id=device.id)
 
-
     def delete(self):
         self.log.info('deleting', device_id=self.device_id)
 
diff --git a/voltha/core/adapter_agent.py b/voltha/core/adapter_agent.py
index d6e32af..e926d9d 100644
--- a/voltha/core/adapter_agent.py
+++ b/voltha/core/adapter_agent.py
@@ -221,9 +221,7 @@
     def update_adapter_pm_config(self, device, device_pm_config):
         self.adapter.update_pm_config(device, device_pm_config)
 
-    def add_port(self, device_id, port):
-        assert isinstance(port, Port)
-
+    def _add_peer_reference(self, device_id, port):
         # for referential integrity, add/augment references
         port.device_id = device_id
         me_as_peer = Port.PeerPort(device_id=device_id, port_no=port.port_no)
@@ -236,13 +234,30 @@
                 new.CopyFrom(me_as_peer)
             self.root_proxy.update(peer_port_path, peer_port)
 
+    def _del_peer_reference(self, device_id, port):
+        me_as_peer = Port.PeerPort(device_id=device_id, port_no=port.port_no)
+        for peer in port.peers:
+            peer_port_path = '/devices/{}/ports/{}'.format(
+                peer.device_id, peer.port_no)
+            peer_port = self.root_proxy.get(peer_port_path)
+            if me_as_peer in peer_port.peers:
+                peer_port.peers.remove(me_as_peer)
+            self.root_proxy.update(peer_port_path, peer_port)
+
+    def add_port(self, device_id, port):
+        assert isinstance(port, Port)
+
+        # for referential integrity, add/augment references
+        self._add_peer_reference(device_id, port)
+
+        # Add port
         self._make_up_to_date('/devices/{}/ports'.format(device_id),
                               port.port_no, port)
 
     def disable_all_ports(self, device_id):
         """
         Disable all ports on that device, i.e. change the admin status to
-        disable and operational status to UNKNOWN
+        disable and operational status to UNKNOWN.
         :param device_id: device id
         :return: None
         """
@@ -255,7 +270,8 @@
             self._make_up_to_date('/devices/{}/ports'.format(device_id),
                                   port.port_no, port)
 
-    def reenable_all_ports(self, device_id):
+
+    def enable_all_ports(self, device_id):
         """
         Re-enable all ports on that device, i.e. change the admin status to
         enabled and operational status to ACTIVE
@@ -293,16 +309,18 @@
         """
         assert isinstance(port, Port)
         self.log.info('delete-port-reference', device_id=device_id, port=port)
+        self._del_peer_reference(device_id, port)
 
-        # for referential integrity, remove references
-        me_as_peer = Port.PeerPort(device_id=device_id, port_no=port.port_no)
-        for peer in port.peers:
-            peer_port_path = '/devices/{}/ports/{}'.format(
-                peer.device_id, peer.port_no)
-            peer_port = self.root_proxy.get(peer_port_path)
-            if me_as_peer in peer_port.peers:
-                peer_port.peers.remove(me_as_peer)
-            self.root_proxy.update(peer_port_path, peer_port)
+    def add_port_reference_to_parent(self, device_id, port):
+        """
+        Add the port reference to the parent device
+        :param device_id: id of device containing the port
+        :param port: port to add
+        :return: None
+        """
+        assert isinstance(port, Port)
+        self.log.info('add-port-reference', device_id=device_id, port=port)
+        self._add_peer_reference(device_id, port)
 
     def _find_first_available_id(self):
         logical_devices = self.root_proxy.get('/logical_devices')
diff --git a/voltha/core/config/config_node.py b/voltha/core/config/config_node.py
index 56518bb..f1c8fc0 100644
--- a/voltha/core/config/config_node.py
+++ b/voltha/core/config/config_node.py
@@ -309,7 +309,7 @@
                     self._mk_event_bus().advertise,
                     change_type,
                     data,
-                    hash=rev.hash,
+                    hash=rev.hash
                 )
 
     # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ add operation ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
diff --git a/voltha/core/config/config_proxy.py b/voltha/core/config/config_proxy.py
index fa6ec57..57d8150 100644
--- a/voltha/core/config/config_proxy.py
+++ b/voltha/core/config/config_proxy.py
@@ -135,7 +135,8 @@
 
     def unregister_callback(self, callback_type, callback, *args, **kw):
         lst = self._callbacks.setdefault(callback_type, [])
-        lst.remove((callback, args, kw))
+        if (callback, args, kw) in lst:
+            lst.remove((callback, args, kw))
 
     # ~~~~~~~~~~~~~~~~~~~~~ Callback dispatch ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
diff --git a/voltha/core/local_handler.py b/voltha/core/local_handler.py
index 7360ca8..4a0fde8 100644
--- a/voltha/core/local_handler.py
+++ b/voltha/core/local_handler.py
@@ -262,7 +262,7 @@
                 'in admin state \'{}\''.format(device.admin_state)
 
         except AssertionError, e:
-            context.set_details(e.msg)
+            context.set_details(e.message)
             context.set_code(StatusCode.INVALID_ARGUMENT)
             return Device()
 
@@ -287,7 +287,7 @@
             context.set_details(
                 'Malformed device id \'{}\''.format(request.id))
             context.set_code(StatusCode.INVALID_ARGUMENT)
-            return Device()
+            return Empty()
 
         try:
             path = '/devices/{}'.format(request.id)
@@ -300,9 +300,8 @@
             self.root.update(path, device, strict=True)
 
         except AssertionError, e:
-            context.set_details(e.msg)
+            context.set_details(e.message)
             context.set_code(StatusCode.INVALID_ARGUMENT)
-            return Device()
 
         except KeyError:
             context.set_details(
@@ -319,8 +318,7 @@
             context.set_details(
                 'Malformed device id \'{}\''.format(request.id))
             context.set_code(StatusCode.INVALID_ARGUMENT)
-            return Device()
-
+            return Empty()
         try:
             path = '/devices/{}'.format(request.id)
             device = self.root.get(path)
@@ -331,9 +329,8 @@
             self.root.update(path, device, strict=True)
 
         except AssertionError, e:
-            context.set_details(e.msg)
+            context.set_details(e.message)
             context.set_code(StatusCode.INVALID_ARGUMENT)
-            return Device()
 
         except KeyError:
             context.set_details(
@@ -350,7 +347,7 @@
             context.set_details(
                 'Malformed device id \'{}\''.format(request.id))
             context.set_code(StatusCode.INVALID_ARGUMENT)
-            return Device()
+            return Empty()
 
         try:
             path = '/devices/{}'.format(request.id)
@@ -359,11 +356,6 @@
             agent = self.core.get_device_agent(device.id)
             agent.reboot_device(device)
 
-        except AssertionError, e:
-            context.set_details(e.msg)
-            context.set_code(StatusCode.INVALID_ARGUMENT)
-            return Device()
-
         except KeyError:
             context.set_details(
                 'Device \'{}\' not found'.format(request.id))
@@ -379,7 +371,7 @@
             context.set_details(
                 'Malformed device id \'{}\''.format(request.id))
             context.set_code(StatusCode.INVALID_ARGUMENT)
-            return Device()
+            return Empty()
 
         try:
             path = '/devices/{}'.format(request.id)
@@ -391,9 +383,8 @@
             self.root.remove(path)
 
         except AssertionError, e:
-            context.set_details(e.msg)
+            context.set_details(e.message)
             context.set_code(StatusCode.INVALID_ARGUMENT)
-            return Device()
 
         except KeyError:
             context.set_details(
@@ -432,9 +423,10 @@
             return PmConfigs()
 
         try:
-            pm_configs = self.root.get('/devices/{}/pm_configs'.format(request.id))
+            pm_configs = self.root.get(
+                '/devices/{}/pm_configs'.format(request.id))
             pm_configs.id = request.id
-            log.info('device-for-pms',pm_configs=pm_configs)
+            log.info('device-for-pms', pm_configs=pm_configs)
             return pm_configs
         except KeyError:
             context.set_details(
diff --git a/voltha/core/logical_device_agent.py b/voltha/core/logical_device_agent.py
index 1ecd032..dc5d1c0 100644
--- a/voltha/core/logical_device_agent.py
+++ b/voltha/core/logical_device_agent.py
@@ -85,17 +85,21 @@
 
     def stop(self):
         self.log.debug('stopping')
-        self.flows_proxy.unregister_callback(
-            CallbackType.POST_UPDATE, self._flow_table_updated)
-        self.groups_proxy.unregister_callback(
-            CallbackType.POST_UPDATE, self._group_table_updated)
-        self.self_proxy.unregister_callback(
-            CallbackType.POST_ADD, self._port_list_updated)
-        self.self_proxy.unregister_callback(
-            CallbackType.POST_REMOVE, self._port_list_updated)
+        try:
+            self.flows_proxy.unregister_callback(
+                CallbackType.POST_UPDATE, self._flow_table_updated)
+            self.groups_proxy.unregister_callback(
+                CallbackType.POST_UPDATE, self._group_table_updated)
+            self.self_proxy.unregister_callback(
+                CallbackType.POST_ADD, self._port_list_updated)
+            self.self_proxy.unregister_callback(
+                CallbackType.POST_REMOVE, self._port_list_updated)
 
-        # Remove subscription to the event bus
-        self.event_bus.unsubscribe(self.packet_in_subscription)
+            # Remove subscription to the event bus
+            self.event_bus.unsubscribe(self.packet_in_subscription)
+        except Exception, e:
+            self.log.info('stop-exception', e=e)
+
         self.log.info('stopped')
 
     def announce_flows_deleted(self, flows):
@@ -115,15 +119,6 @@
     def signal_group_mod_error(self, code, group_mod):
         pass  # TODO
 
-    def delete_all_flows(self):
-        self.update_flow_table(mk_simple_flow_mod(
-            command=ofp.OFPFC_DELETE,
-            out_port=ofp.OFPP_ANY,
-            out_group=ofp.OFPG_ANY,
-            match_fields=[],
-            actions=[]
-        ))
-
     def update_flow_table(self, flow_mod):
 
         command = flow_mod.command