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 update(self, request, id, **kwargs): context = request.context self._check_admin(context) body = validators.validate_request(request) key_list = ['access_protocol', 'access_parameters', 'enable', 'rediscover'] validators.validate_attributes(body.keys(), key_list) validate_snmp_creds = False phys_switch = db.get_bnp_phys_switch(context, id) if not phys_switch: raise webob.exc.HTTPNotFound( _("Switch %s does not exist") % id) if body.get('access_parameters'): validate_snmp_creds = True access_parameters = body.pop("access_parameters") for key, value in access_parameters.iteritems(): body[key] = value else: access_parameters = { 'write_community': phys_switch['write_community'], 'security_name': phys_switch['security_name'], 'auth_protocol': phys_switch['auth_protocol'], 'priv_protocol': phys_switch['priv_protocol'], 'auth_key': phys_switch['auth_key'], 'priv_key': phys_switch['priv_key'], 'security_level': phys_switch['security_level']} if body.get('access_protocol'): validate_snmp_creds = True protocol = body['access_protocol'] if protocol.lower() not in const.SUPPORTED_PROTOCOLS: raise webob.exc.HTTPBadRequest( _("access protocol %s is not supported") % body[ 'access_protocol']) else: protocol = phys_switch['access_protocol'] switch_dict = self._update_dict(body, dict(phys_switch)) switch_to_show = self._switch_to_show(switch_dict) switch = switch_to_show[0] if validate_snmp_creds: if protocol.lower() == const.SNMP_V3: validators.validate_snmpv3_parameters(access_parameters) else: validators.validate_snmp_parameters(access_parameters) try: snmp_driver = discovery_driver.SNMPDiscoveryDriver(switch_dict) snmp_driver.get_sys_name() db.update_bnp_phys_switch_access_params(context, id, switch_dict) except Exception as e: LOG.error(_LE("Exception in validating credentials '%s' "), e) raise webob.exc.HTTPBadRequest( _("Validation of credentials failed")) if body.get('enable'): enable = attributes.convert_to_boolean(body['enable']) if not enable: switch_status = const.SWITCH_STATUS['disable'] db.update_bnp_phys_switch_status(context, id, switch_status) else: switch_status = const.SWITCH_STATUS['enable'] db.update_bnp_phys_switch_status(context, id, switch_status) switch['status'] = switch_status if body.get('rediscover'): bnp_switch = self._discover_switch(switch_dict) db_switch_ports = db.get_bnp_phys_switch_ports_by_switch_id( context, id) self._update_switch_ports(context, id, bnp_switch.get('ports'), db_switch_ports) return switch