def show(self, request, id, **kwargs):
        context = request.context
        switch = db.get_bnp_phys_switch(context, id)
        if not switch:
            raise webob.exc.HTTPNotFound(
                _("Switch %s does not exist") % id)
        try:
            snmp_drv = discovery_driver.SNMPDiscoveryDriver(switch)
            ports_list = snmp_drv.get_ports_status()
        except Exception as e:
            LOG.error(_LE("BNP SNMP getbulk failed with exception: %s."), e)
            is_getbulk_success = False
        else:
            is_getbulk_success = True
            sw_ports = {}
            for port_dict in ports_list:
                sw_ports[port_dict['ifindex']] = port_dict['port_status']

        port_status_dict = {}
        switch_list = self._switch_to_show(switch)
        switch_dict = switch_list[0]
        bounded_ports = db.get_bnp_switch_port_map_by_switchid(
            context, id)
        if bounded_ports:
            for port in bounded_ports:
                switch_port = db.get_bnp_phys_switch_port_by_id(
                    context, port['switch_port_id'])
                if is_getbulk_success:
                    port_status_dict[switch_port['interface_name']] = (
                        const.PORT_STATUS.get(
                            str(sw_ports[switch_port['ifindex']])))
                else:
                    port_status_dict[switch_port['interface_name']] = 'UNKNOWN'
        switch_dict['ports'] = port_status_dict
        return {const.BNP_SWITCH_RESOURCE_NAME: switch_dict}
 def test_get_bnp_phys_switch_port_by_id(self):
     """Test get_bnp_phys_switch_port_by_id method."""
     swport = self._get_bnp_phys_switchport_dict()
     db.add_bnp_phys_switch_port(self.ctx, swport)
     port = db.get_bnp_phys_switch_ports_by_switch_id(self.ctx,
                                                      swport['switch_id'])
     new_swport = db.get_bnp_phys_switch_port_by_id(self.ctx,
                                                    port[0]['id'])
     self.assertEqual(port[0]['id'], new_swport['id'])
 def delete_port(self, port_id):
     """delete_port ."""
     db_context = neutron_context.get_admin_context()
     try:
         port_map = db.get_bnp_neutron_port(db_context, port_id)
     except Exception:
         LOG.error(_LE("No neutron port is associated with the phys port"))
         return
     is_last_port_in_vlan = False
     seg_id = port_map.segmentation_id
     bnp_sw_map = db.get_bnp_switch_port_mappings(db_context, port_id)
     switch_port_id = bnp_sw_map[0].switch_port_id
     bnp_switch = db.get_bnp_phys_switch(db_context,
                                         bnp_sw_map[0].switch_id)
     cred_dict = self._get_credentials_dict(bnp_switch, 'delete_port')
     phys_port = db.get_bnp_phys_switch_port_by_id(db_context,
                                                   switch_port_id)
     result = db.get_bnp_neutron_port_by_seg_id(db_context, seg_id)
     if not result:
         LOG.error(_LE("No neutron port is associated with the phys port"))
         self._raise_ml2_error(wexc.HTTPNotFound, 'delete_port')
     if len(result) == 1:
         # to prevent snmp set from the same VLAN
         is_last_port_in_vlan = True
     port_dict = {'port':
                  {'id': port_id,
                   'segmentation_id': seg_id,
                   'ifindex': phys_port.ifindex,
                   'is_last_port_vlan': is_last_port_in_vlan
                   }
                  }
     credentials_dict = port_dict.get('port')
     credentials_dict['credentials'] = cred_dict
     try:
         prov_protocol = bnp_switch.prov_proto
         vendor = bnp_switch.vendor
         family = bnp_switch.family
         prov_driver = self._provisioning_driver(prov_protocol, vendor,
                                                 family)
         if not prov_driver:
             LOG.error(_LE("No suitable provisioning driver found"
                           ))
             self._raise_ml2_error(wexc.HTTPNotFound, 'create_port')
         prov_driver.obj.delete_isolation(port_dict)
         db.delete_bnp_neutron_port(db_context, port_id)
         db.delete_bnp_switch_port_mappings(db_context, port_id)
     except Exception as e:
         LOG.error(_LE("Error in deleting the port '%s' "), e)
         self._raise_ml2_error(wexc.HTTPNotFound, 'delete_port')
 def show(self, request, id):
     context = request.context
     switch = db.get_bnp_phys_switch(context, id)
     port_status_dict = {}
     if not switch:
         raise webob.exc.HTTPNotFound(
             _("Switch %s does not exist") % id)
     switch_list = self._switch_to_show(switch)
     switch_dict = switch_list[0]
     bounded_ports = db.get_bnp_switch_port_map_by_switchid(
         context, id)
     if bounded_ports:
         for port in bounded_ports:
             switch_port = db.get_bnp_phys_switch_port_by_id(
                 context, port['switch_port_id'])
             port_status_dict[switch_port[
                 'interface_name']] = switch_port['port_status']
     switch_dict['ports'] = port_status_dict
     return switch_dict
 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)
 def delete_port(self, port_id):
     """delete_port ."""
     try:
         port_map = db.get_bnp_neutron_port(self.context, port_id)
     except Exception:
         LOG.error(_LE("No neutron port is associated with the phys port"))
         return
     is_last_port_in_vlan = False
     seg_id = port_map.segmentation_id
     bnp_sw_map = db.get_bnp_switch_port_mappings(self.context, port_id)
     switch_port_id = bnp_sw_map[0].switch_port_id
     bnp_switch = db.get_bnp_phys_switch(self.context,
                                         bnp_sw_map[0].switch_id)
     cred_dict = self._get_credentials_dict(bnp_switch, 'delete_port')
     phys_port = db.get_bnp_phys_switch_port_by_id(self.context,
                                                   switch_port_id)
     result = db.get_bnp_neutron_port_by_seg_id(self.context, seg_id)
     if not result:
         LOG.error(_LE("No neutron port is associated with the phys port"))
         self._raise_ml2_error(wexc.HTTPNotFound, 'delete_port')
     if len(result) == 1:
         # to prevent snmp set from the same VLAN
         is_last_port_in_vlan = True
     port_dict = {'port':
                  {'id': port_id,
                   'segmentation_id': seg_id,
                   'ifindex': phys_port.ifindex,
                   'is_last_port_vlan': is_last_port_in_vlan
                   }
                  }
     credentials_dict = port_dict.get('port')
     credentials_dict['credentials'] = cred_dict
     try:
         self.protocol_driver.delete_isolation(port_dict)
         db.delete_bnp_neutron_port(self.context, port_id)
         db.delete_bnp_switch_port_mappings(self.context, port_id)
     except Exception as e:
         LOG.error(_LE("Error in deleting the port '%s' "), e)
         self._raise_ml2_error(wexc.HTTPNotFound, 'delete_port')