def test_get_bnp_phys_switch(self): """Test get_bnp_phys_switch method.""" sw_dict = self._get_bnp_phys_switch_dict() db.add_bnp_phys_switch(self.ctx, sw_dict) sw_mac = db.get_bnp_phys_switch_by_mac(self.ctx, sw_dict['mac_address']) sw_ip = db.get_bnp_phys_switch_by_ip(self.ctx, sw_dict['ip_address']) sw = db.get_bnp_phys_switch(self.ctx, sw_mac['id']) self.assertEqual(sw['id'], sw_mac['id']) self.assertEqual(sw['id'], sw_ip['id'])
def create(self, request, **kwargs): context = request.context self._check_admin(context) body = validators.validate_request(request) key_list = ['name', 'ip_address', 'vendor', 'disc_proto', 'disc_creds', 'prov_proto', 'prov_creds'] keys = body.keys() for key in key_list: if key not in keys: raise webob.exc.HTTPBadRequest( _("Key %s not found in request body") % key) validators.validate_switch_attributes(keys, key_list) if body['vendor'] not in const.SUPPORTED_VENDORS: raise webob.exc.HTTPBadRequest( _("Switch with vendor %s is not supported") % body['vendor']) ip_address = body['ip_address'] bnp_switch = db.get_bnp_phys_switch_by_ip(context, ip_address) if bnp_switch: raise webob.exc.HTTPConflict( _("Switch with ip_address %s is already present") % ip_address) access_parameters = self._get_access_param(context, body['disc_proto'], body['disc_creds']) for key, value in access_parameters.iteritems(): body[key] = value bnp_switch = self._discover_switch(body) if bnp_switch.get('mac_address'): body['mac_address'] = bnp_switch.get('mac_address') body['port_prov'] = const.SWITCH_STATUS['enable'] else: body['port_prov'] = const.SWITCH_STATUS['create'] if 'family' not in body: body['family'] = None db_switch = db.add_bnp_phys_switch(context, body) if bnp_switch.get('ports'): self._add_physical_port(context, db_switch.get('id'), bnp_switch.get('ports')) return {const.BNP_SWITCH_RESOURCE_NAME: dict(db_switch)}
def create(self, request): context = request.context self._check_admin(context) body = validators.validate_request(request) key_list = ['ip_address', 'vendor', 'access_protocol', 'access_parameters'] keys = body.keys() for key in key_list: if key not in keys: raise webob.exc.HTTPBadRequest( _("Key %s not found in request body") % key) if body['vendor'] not in const.SUPPORTED_VENDORS: raise webob.exc.HTTPBadRequest( _("Switch with vendor %s is not supported") % body['vendor']) ip_address = body['ip_address'] bnp_switch = db.get_bnp_phys_switch_by_ip(context, ip_address) if bnp_switch: raise webob.exc.HTTPConflict( _("Switch with ip_address %s is already present") % ip_address) validators.validate_access_parameters(body) access_parameters = body.pop("access_parameters") switch_dict = self._create_switch_dict() for key, value in access_parameters.iteritems(): body[key] = value switch = self._update_dict(body, switch_dict) bnp_switch = self._discover_switch(switch) if bnp_switch.get('mac_address'): switch['mac_address'] = bnp_switch.get('mac_address') switch['status'] = const.SWITCH_STATUS['enable'] else: switch['status'] = const.SWITCH_STATUS['create'] db_switch = db.add_bnp_phys_switch(context, switch) if bnp_switch.get('ports'): self._add_physical_port(context, db_switch.get('id'), bnp_switch.get('ports')) return dict(db_switch)
def create(self, request, **kwargs): context = request.context self._check_admin(context) body = validators.validate_request(request) key_list = ['name', 'ip_address', 'vendor', 'management_protocol', 'credentials', 'mac_address'] keys = body.keys() for key in key_list: if key not in keys: raise webob.exc.HTTPBadRequest( _("Key %s not found in request body") % key) key_list.append('family') validators.validate_attributes(keys, key_list) ip_address = body['ip_address'] if const.FAMILY not in body: body['family'] = '' bnp_switch = db.get_bnp_phys_switch_by_ip(context, ip_address) if bnp_switch: raise webob.exc.HTTPConflict( _("Switch with ip_address %s is already present") % ip_address) filters = {'mac_address': body['mac_address']} switch_exists = db.get_if_bnp_phy_switch_exists(context, **filters) if switch_exists: raise webob.exc.HTTPConflict( _("Switch with mac_address %s is already present") % body['mac_address']) access_parameters = self._get_access_param(context, body['management_protocol'], body['credentials']) credentials = body['credentials'] body['port_provisioning'] = const.PORT_PROVISIONING_STATUS['enable'] result = self.validate_protocol(access_parameters, credentials, body) body['validation_result'] = result db_switch = db.add_bnp_phys_switch(context, body) return {const.BNP_SWITCH_RESOURCE_NAME: dict(db_switch)}
def create(self, request, **kwargs): context = request.context self._check_admin(context) body = validators.validate_request(request) key_list = ['name', 'ip_address', 'vendor', 'management_protocol', 'credentials', 'mac_address'] keys = body.keys() for key in key_list: if key not in keys: raise webob.exc.HTTPBadRequest( _("Key %s not found in request body") % key) key_list.append('family') validators.validate_attributes(keys, key_list) ip_address = body['ip_address'] if const.FAMILY not in body: body['family'] = '' bnp_switch = db.get_bnp_phys_switch_by_ip(context, ip_address) if bnp_switch: raise webob.exc.HTTPConflict( _("Switch with ip_address %s is already present") % ip_address) filters = {'mac_address': body['mac_address']} switch_exists = db.get_if_bnp_phy_switch_exists(context, **filters) if switch_exists: raise webob.exc.HTTPConflict( _("Switch with mac_address %s is already present") % body['mac_address']) access_parameters = self._get_access_param(context, body['management_protocol'], body['credentials']) credentials = body['credentials'] body['port_provisioning'] = const.SWITCH_STATUS['enable'] result = self.validate_protocol(access_parameters, credentials, body) body['validation_result'] = result db_switch = db.add_bnp_phys_switch(context, body) return {const.BNP_SWITCH_RESOURCE_NAME: dict(db_switch)}
def update(self, request, id, **kwargs): context = request.context self._check_admin(context) body = validators.validate_request(request) key_list = [ 'name', 'ip_address', 'vendor', 'management_protocol', 'credentials', 'mac_address', 'port_provisioning', 'validate' ] validators.validate_attributes(body.keys(), key_list) switch = db.get_bnp_phys_switch(context, id) if not switch: raise webob.exc.HTTPNotFound(_("Switch %s does not exist") % id) if body.get('ip_address'): if (body['ip_address'] != switch['ip_address']): ip = body['ip_address'] bnp_switch = db.get_bnp_phys_switch_by_ip(context, ip) if bnp_switch: raise webob.exc.HTTPConflict( _("Switch with ip_address %s is already present") % ip) else: switch['ip_address'] = ip if body.get('port_provisioning'): port_prov = body['port_provisioning'] if (port_prov.upper() not in const.PORT_PROVISIONING_STATUS.values()): raise webob.exc.HTTPBadRequest( _("Invalid port-provisioning option %s ") % port_prov) switch['port_provisioning'] = port_prov.upper() if body.get('name'): switch['name'] = body['name'] if body.get('vendor'): switch['vendor'] = body['vendor'] if body.get('management_protocol') and body.get('credentials'): proto = body['management_protocol'] cred = body['credentials'] self._get_access_param(context, proto, cred) switch['management_protocol'] = proto switch['credentials'] = cred elif (body.get('management_protocol') and not body.get('credentials')): proto = body['management_protocol'] if (body['management_protocol'] != switch['management_protocol']): raise webob.exc.HTTPBadRequest( _("Invalid management_protocol : %s ") % proto) switch['management_protocol'] = proto elif (body.get('credentials') and not body.get('management_protocol')): cred = body['credentials'] self._get_access_param(context, switch['management_protocol'], cred) switch['credentials'] = cred if body.get('mac_address') or body.get('validate'): body['vendor'] = switch['vendor'] body['management_protocol'] = switch['management_protocol'] body['family'] = switch['family'] sw_proto = switch['management_protocol'] sw_cred = switch['credentials'] body['ip_address'] = switch['ip_address'] access_parameters = self._get_access_param(context, sw_proto, sw_cred) if body.get('mac_address'): filters = {'mac_address': body['mac_address']} switch_exists = db.get_if_bnp_phy_switch_exists( context, **filters) if switch_exists: raise webob.exc.HTTPConflict( _("Switch with mac_address %s is already present") % body['mac_address']) result = self.validate_protocol(access_parameters, switch['credentials'], body) switch['validation_result'] = result switch['mac_address'] = body['mac_address'] elif body.get('validate') and not body.get('mac_address'): body['mac_address'] = switch['mac_address'] result = self.validate_protocol(access_parameters, switch['credentials'], body) switch['validation_result'] = result db.update_bnp_phy_switch(context, id, switch) return switch
def update(self, request, id, **kwargs): context = request.context self._check_admin(context) body = validators.validate_request(request) key_list = ['name', 'ip_address', 'vendor', 'management_protocol', 'credentials', 'mac_address', 'port_provisioning', 'validate'] validators.validate_attributes(body.keys(), key_list) switch = db.get_bnp_phys_switch(context, id) if not switch: raise webob.exc.HTTPNotFound( _("Switch %s does not exist") % id) if body.get('ip_address'): if (body['ip_address'] != switch['ip_address']): ip = body['ip_address'] bnp_switch = db.get_bnp_phys_switch_by_ip(context, ip) if bnp_switch: raise webob.exc.HTTPConflict( _("Switch with ip_address %s is already present") % ip) else: switch['ip_address'] = ip if body.get('port_provisioning'): port_prov = body['port_provisioning'] if (port_prov.upper() not in const.PORT_PROVISIONING_STATUS.values()): raise webob.exc.HTTPBadRequest( _("Invalid port-provisioning option %s ") % port_prov) switch['port_provisioning'] = port_prov.upper() if body.get('name'): switch['name'] = body['name'] if body.get('vendor'): switch['vendor'] = body['vendor'] if body.get('management_protocol') and body.get('credentials'): proto = body['management_protocol'] cred = body['credentials'] self._get_access_param(context, proto, cred) switch['management_protocol'] = proto switch['credentials'] = cred elif (body.get('management_protocol') and not body.get('credentials')): proto = body['management_protocol'] if (body['management_protocol'] != switch['management_protocol']): raise webob.exc.HTTPBadRequest( _("Invalid management_protocol : %s ") % proto) switch['management_protocol'] = proto elif (body.get('credentials') and not body.get('management_protocol')): cred = body['credentials'] self._get_access_param(context, switch['management_protocol'], cred) switch['credentials'] = cred if body.get('mac_address') or body.get('validate'): body['vendor'] = switch['vendor'] body['management_protocol'] = switch['management_protocol'] body['family'] = switch['family'] sw_proto = switch['management_protocol'] sw_cred = switch['credentials'] body['ip_address'] = switch['ip_address'] access_parameters = self._get_access_param(context, sw_proto, sw_cred) if body.get('mac_address'): filters = {'mac_address': body['mac_address']} switch_exists = db.get_if_bnp_phy_switch_exists( context, **filters) if switch_exists: raise webob.exc.HTTPConflict( _("Switch with mac_address %s is already present") % body['mac_address']) result = self.validate_protocol(access_parameters, switch['credentials'], body) switch['validation_result'] = result switch['mac_address'] = body['mac_address'] elif body.get('validate') and not body.get('mac_address'): body['mac_address'] = switch['mac_address'] result = self.validate_protocol(access_parameters, switch['credentials'], body) switch['validation_result'] = result db.update_bnp_phy_switch(context, id, switch) return switch