def _update_switch_ports(self, context, switch_id, ports, db_switch_ports):
     port_ifname_map = {}
     db_swport_ifname_map = {}
     for port in ports:
         port_ifname_map[port['interface_name']] = const.PORT_STATUS[
             port['port_status']]
     for sw_port in db_switch_ports:
         db_swport_ifname_map[
             sw_port['interface_name']] = sw_port['port_status']
     for port_ifname in port_ifname_map.keys():
         if port_ifname in db_swport_ifname_map.keys():
             if port_ifname_map[port_ifname] != db_swport_ifname_map[
                port_ifname]:
                 db.update_bnp_phys_swport_status(
                     context, switch_id,
                     port_ifname, port_ifname_map[port_ifname])
             port_ifname_map.pop(port_ifname)
             db_swport_ifname_map.pop(port_ifname)
         elif port_ifname not in db_swport_ifname_map.keys():
             for port in ports:
                 if port['interface_name'] == port_ifname:
                     ifindex = port['ifindex']
                     break
             phys_port = {'switch_id': switch_id,
                          'port_status': port_ifname_map[port_ifname],
                          'interface_name': port_ifname,
                          'ifindex': ifindex}
             db.add_bnp_phys_switch_port(context, phys_port)
             port_ifname_map.pop(port_ifname)
     if db_swport_ifname_map:
         for swport_ifname in db_swport_ifname_map:
             db.delete_bnp_phys_switch_ports_by_name(context, switch_id,
                                                     swport_ifname)
 def test_update_bnp_phys_swport_status(self):
     """Test update_bnp_phys_swport_status method."""
     port_dict = self._get_bnp_phys_switchport_dict()
     db.add_bnp_phys_switch_port(self.ctx, port_dict)
     db.update_bnp_phys_swport_status(self.ctx,
                                      port_dict['switch_id'],
                                      port_dict['interface_name'],
                                      "DOWN")
     port_updt = self.ctx.session.query(models.BNPPhysicalSwitchPort).all()
     self.assertEqual(port_updt[0]['port_status'], "DOWN")
 def monitor_port_status(self):
     """Sync switch database periodically."""
     self.context = neutron_context.get_admin_context()
     portmaps = db.get_all_bnp_swport_mappings(self.context)
     for portmap in portmaps:
         swport = db.get_bnp_phys_switch_port_by_id(
             self.context, portmap['switch_port_id'])
         old_status = swport['port_status']
         switch = db.get_bnp_phys_switch(self.context,
                                         portmap['switch_id'])
         try:
             snmp_drv = discovery_driver.SNMPDiscoveryDriver(switch)
             port_status = snmp_drv.get_port_status(swport['ifindex'])
         except Exception as e:
             LOG.error(_LE("BNP SNMP polling exception: %s."), e)
             if old_status != 'UNKNOWN':
                 LOG.info(_LI("BNP SNMP polling: Update port status to "
                              "UNKNOWN."))
                 db.update_bnp_phys_swport_status(
                     self.context, swport['switch_id'],
                     swport['interface_name'], 'UNKNOWN')
                 db.set_port_status(self.context,
                                    portmap['neutron_port_id'],
                                    n_const.PORT_STATUS_ERROR)
         else:
             new_status = constants.PORT_STATUS.get(str(port_status))
             LOG.debug("BNP SNMP polling: new port status %s", new_status)
             if new_status != old_status:
                 LOG.info(_LI('BNP SNMP polling: Update port status to %s'),
                          new_status)
                 db.update_bnp_phys_swport_status(
                     self.context, swport['switch_id'],
                     swport['interface_name'], new_status)
                 if new_status == 'UP':
                     db.set_port_status(self.context,
                                        portmap['neutron_port_id'],
                                        n_const.PORT_STATUS_ACTIVE)
                 else:
                     db.set_port_status(self.context,
                                        portmap['neutron_port_id'],
                                        n_const.PORT_STATUS_DOWN)