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) 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) switch_list = self._switch_to_show(switch) switch_dict = switch_list[0] return {const.BNP_SWITCH_RESOURCE_NAME: switch_dict}
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) switch_list = self._switch_to_show(switch) switch_dict = switch_list[0] return {const.BNP_SWITCH_RESOURCE_NAME: switch_dict}
def update(self, request, id): context = request.context self._check_admin(context) body = validators.validate_request(request) 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', None): if body.get('access_protocol', None): 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.__dict__.get('access_protocol') if protocol.lower() == const.SNMP_V3: validators.validate_snmpv3_parameters( body['access_parameters']) else: validators.validate_snmp_parameters( body['access_parameters']) access_parameters = body.pop("access_parameters") for key, value in access_parameters.iteritems(): body[key] = value switch_dict = self._update_dict(body, dict(phys_switch)) switch_to_show = self._switch_to_show(switch_dict) switch = switch_to_show[0] if 'enable' in body.keys(): if body['enable'] is False: if body.get('rediscover', None): raise webob.exc.HTTPBadRequest( _("Rediscovery of Switch %s is not supported" "when Enable=False") % id) switch_status = const.SWITCH_STATUS['disable'] switch['status'] = switch_status db.update_bnp_phys_switch_status(context, id, switch_status) db.update_bnp_phys_switch_access_params(context, id, switch_dict) return switch elif phys_switch.__dict__['status'] == const.SWITCH_STATUS[ 'enable'] and body['enable'] is True: raise webob.exc.HTTPBadRequest( _("Disable the switch %s to update") % id) if phys_switch.__dict__['status'] == const.SWITCH_STATUS[ 'enable'] and body.get('rediscover', None): raise webob.exc.HTTPBadRequest( _("Disable the switch %d to update") % id) self._discover_switch(switch_dict) switch_status = const.SWITCH_STATUS['enable'] switch['status'] = switch_status db.update_bnp_phys_switch_status(context, id, switch_status) db.update_bnp_phys_switch_access_params(context, id, switch_dict) return switch
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 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) 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 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 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 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_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) 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') 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': bnp_sw_map[0].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.management_protocol 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')
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
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