def _update_neighbors_backend(configmanager, force): global _neighdata global _neighbypeerid vintage = _neighdata.get('!!vintage', 0) now = util.monotonic_time() if vintage > (now - 60) and not force: return _neighdata = {'!!vintage': now} _neighbypeerid = {'!!vintage': now} switches = netutil.list_switches(configmanager) switchcreds = netutil.get_switchcreds(configmanager, switches) switchcreds = [ x + (force,) for x in switchcreds] pool = GreenPool(64) for ans in pool.imap(_extract_neighbor_data, switchcreds): yield ans
def handle_read_api_request(pathcomponents, configmanager): # TODO(jjohnson2): discovery core.py api handler design, apply it here # to make this a less tangled mess as it gets extended if len(pathcomponents) == 1: return [ msg.ChildCollection('macs/'), msg.ChildCollection('neighbors/') ] elif pathcomponents[1] == 'neighbors': if len(pathcomponents) == 3 and pathcomponents[-1] == 'by-switch': return [ msg.ChildCollection(x + '/') for x in list_switches(configmanager) ] else: return _handle_neighbor_query(pathcomponents[2:], configmanager) elif len(pathcomponents) == 2: if pathcomponents[-1] == 'macs': return [ msg.ChildCollection(x) for x in ( # 'by-node/', 'by-mac/', 'by-switch/', 'rescan') ] elif pathcomponents[-1] == 'neighbors': return [msg.ChildCollection('by-switch/')] else: raise exc.NotFoundException( 'Unknown networking resource {0}'.format(pathcomponents[-1])) if False and pathcomponents[2] == 'by-node': # TODO: should be list of node names, and then under that 'by-mac' if len(pathcomponents) == 3: return [ msg.ChildCollection(x.replace(':', '-')) for x in util.natural_sort(list(_nodesbymac)) ] elif len(pathcomponents) == 4: macaddr = pathcomponents[-1].replace('-', ':') return dump_macinfo(macaddr) elif pathcomponents[2] == 'by-mac': if len(pathcomponents) == 3: return [ msg.ChildCollection(x.replace(':', '-')) for x in sorted(list(_macmap)) ] elif len(pathcomponents) == 4: return dump_macinfo(pathcomponents[-1]) elif pathcomponents[2] == 'by-switch': if len(pathcomponents) == 3: return [ msg.ChildCollection(x + '/') for x in list_switches(configmanager) ] if len(pathcomponents) == 4: return [msg.ChildCollection('by-port/')] if len(pathcomponents) == 5: switchname = pathcomponents[-2] if switchname not in _macsbyswitch: raise exc.NotFoundException( 'No known macs for switch {0}'.format(switchname)) return [ msg.ChildCollection(x.replace('/', '-') + '/') for x in util.natural_sort(list(_macsbyswitch[switchname])) ] if len(pathcomponents) == 6: return [msg.ChildCollection('by-mac/')] if len(pathcomponents) == 7: switchname = pathcomponents[-4] portname = pathcomponents[-2] try: if portname not in _macsbyswitch[switchname]: portname = portname.replace('-', '/') maclist = _macsbyswitch[switchname][portname] except KeyError: raise exc.NotFoundException('No known macs for switch {0} ' 'port {1}'.format( switchname, portname)) return [ msg.ChildCollection(x.replace(':', '-')) for x in sorted(maclist) ] if len(pathcomponents) == 8: return dump_macinfo(pathcomponents[-1]) raise exc.NotFoundException('Unrecognized path {0}'.format( '/'.join(pathcomponents)))
def handle_read_api_request(pathcomponents, configmanager): # TODO(jjohnson2): discovery core.py api handler design, apply it here # to make this a less tangled mess as it gets extended if len(pathcomponents) == 1: return [ msg.ChildCollection('macs/'), msg.ChildCollection('neighbors/') ] elif pathcomponents[1] == 'neighbors': if len(pathcomponents) == 3 and pathcomponents[-1] == 'by-switch': return [ msg.ChildCollection(x + '/') for x in list_switches(configmanager) ] else: return _handle_neighbor_query(pathcomponents[2:], configmanager) elif len(pathcomponents) == 2: if pathcomponents[-1] == 'macs': return [ msg.ChildCollection(x) for x in ( # 'by-node/', 'by-mac/', 'by-switch/', 'rescan') ] elif pathcomponents[-1] == 'neighbors': return [msg.ChildCollection('by-switch/')] else: raise exc.NotFoundException( 'Unknown networking resource {0}'.format(pathcomponents[-1])) if False and pathcomponents[2] == 'by-node': # TODO: should be list of node names, and then under that 'by-mac' if len(pathcomponents) == 3: return [ msg.ChildCollection(x.replace(':', '-')) for x in util.natural_sort(list(_nodesbymac)) ] elif len(pathcomponents) == 4: macaddr = pathcomponents[-1].replace('-', ':') return dump_macinfo(macaddr) elif pathcomponents[2] == 'by-mac': if len(pathcomponents) == 3: return [ msg.ChildCollection(x.replace(':', '-')) for x in sorted(list(_apimacmap)) ] elif len(pathcomponents) == 4: return dump_macinfo(pathcomponents[-1]) elif pathcomponents[2] == 'by-switch': if len(pathcomponents) == 3: return [ msg.ChildCollection(x + '/') for x in list_switches(configmanager) ] if len(pathcomponents) == 4: return [msg.ChildCollection('by-port/')] if len(pathcomponents) == 5: switchname = pathcomponents[-2] if switchname not in _macsbyswitch: raise exc.NotFoundException( 'No known macs for switch {0}'.format(switchname)) return [ msg.ChildCollection(x.replace('/', '-') + '/') for x in util.natural_sort(list(_macsbyswitch[switchname])) ] if len(pathcomponents) == 6: return [msg.ChildCollection('by-mac/')] if len(pathcomponents) == 7: switchname = pathcomponents[-4] portname = pathcomponents[-2] try: if portname not in _macsbyswitch[switchname]: portname = portname.replace('-', '/') maclist = _macsbyswitch[switchname][portname] except KeyError: foundsomemacs = False if switchname in _macsbyswitch: try: matcher = re.compile(portname) except Exception: raise exc.InvalidArgumentException( 'Invalid regular expression specified') maclist = [] for actualport in _macsbyswitch[switchname]: if bool(matcher.match(actualport)): foundsomemacs = True maclist = maclist + _macsbyswitch[switchname][ actualport] if not foundsomemacs: raise exc.NotFoundException('No known macs for switch {0} ' 'port {1}'.format( switchname, portname)) return [ msg.ChildCollection(x.replace(':', '-')) for x in sorted(maclist) ] if len(pathcomponents) == 8: return dump_macinfo(pathcomponents[-1]) elif pathcomponents[2] == 'rescan': return [msg.KeyValueData({'scanning': mapupdating.locked()})] raise exc.NotFoundException('Unrecognized path {0}'.format( '/'.join(pathcomponents)))