def delete(self, request, id, **kwargs):
     context = request.context
     self._check_admin(context)
     port_prov = None
     is_uuid = False
     if not uuidutils.is_uuid_like(id):
         switch = db.get_bnp_phys_switch_by_name(context, id)
     else:
         is_uuid = True
         switch = db.get_bnp_phys_switch(context, id)
     if not switch:
         raise webob.exc.HTTPNotFound(
             _("Switch %s does not exist") % id)
     if isinstance(switch, list) and len(switch) > 1:
         raise webob.exc.HTTPConflict(
             _("Multiple switches matches found "
               "for name %s, use an ID to be more specific.") % id)
     if isinstance(switch, list) and len(switch) == 1:
         portmap = db.get_bnp_switch_port_map_by_switchid(context,
                                                          switch[0].id)
         port_prov = switch[0].port_prov
     else:
         portmap = db.get_bnp_switch_port_map_by_switchid(context, id)
         port_prov = switch['port_prov']
     if portmap:
         raise webob.exc.HTTPConflict(
             _("Switch id %s has active port mappings") % id)
     if port_prov == const.SWITCH_STATUS['enable']:
         raise webob.exc.HTTPBadRequest(
             _("Disable the switch %s to delete") % id)
     if is_uuid:
         db.delete_bnp_phys_switch(context, id)
     else:
         db.delete_bnp_phys_switch_by_name(context, id)
    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 delete(self, request, id, **kwargs):
     context = request.context
     self._check_admin(context)
     switch = db.get_bnp_phys_switch(context, id)
     if not switch:
         raise webob.exc.HTTPNotFound(_("Switch %s does not exist") % id)
     portmap = db.get_bnp_switch_port_map_by_switchid(context, id)
     if portmap:
         raise webob.exc.HTTPConflict(
             _("Switch id %s has active port mappings") % id)
     if (switch['port_provisioning'] ==
             const.PORT_PROVISIONING_STATUS['enable']):
         raise webob.exc.HTTPBadRequest(
             _("Disable the switch %s to delete") % id)
     db.delete_bnp_phys_switch(context, id)
 def delete(self, request, id, **kwargs):
     context = request.context
     self._check_admin(context)
     switch = db.get_bnp_phys_switch(context, id)
     portmap = db.get_bnp_switch_port_map_by_switchid(context, id)
     if portmap:
         raise webob.exc.HTTPConflict(
             _("Switch id %s has active port mappings") % id)
     if not switch:
         raise webob.exc.HTTPNotFound(
             _("Switch %s does not exist") % id)
     if switch['status'] == const.SWITCH_STATUS['enable']:
         raise webob.exc.HTTPBadRequest(
             _("Disable the switch %s to delete") % id)
     db.delete_bnp_phys_switch(context, id)
 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