def distributor(request: Request, response: Response): try: pipe = rdbconn.pipeline() KEYPATTERN = 'intcon:out:*:_gateways' next, mainkeys = rdbconn.scan(0, KEYPATTERN, SCAN_COUNT) while next: next, tmpkeys = rdbconn.scan(next, KEYPATTERN, SCAN_COUNT) mainkeys += tmpkeys for mainkey in mainkeys: pipe.hgetall(mainkey) details = pipe.execute() interconnections = dict() for mainkey, detail in zip(mainkeys, details): intconname = getnameid(mainkey) interconnections[intconname] = jsonhash(detail) result = templates.TemplateResponse( "distributor.j2.xml", { "request": request, "interconnections": interconnections }, media_type="application/xml") response.status_code = 200 except Exception as e: response.status_code, result = 500, str() logify( f"module=liberator, space=fsxmlapi, section=distributor, requestid={get_request_uuid()}, exception={e}, traceback={traceback.format_exc()}" ) finally: return result
def acl(request: Request, response: Response): try: pipe = rdbconn.pipeline() # IP LIST OF SIP PROFILE AND REALM # {profilename: realm} KEYPATTERN = 'sipprofile:*' next, mainkeys = rdbconn.scan(0, KEYPATTERN, SCAN_COUNT) while next: next, tmpkeys = rdbconn.scan(next, KEYPATTERN, SCAN_COUNT) mainkeys += tmpkeys for mainkey in mainkeys: pipe.hget(mainkey, 'realm') realms = pipe.execute() sipprofiles = dict() for mainkey, realm in zip(mainkeys, realms): sipprofiles[getnameid(mainkey)] = realm # DEFINED ACL LIST # [{'name': name, 'action': default-action, 'rules': [{'action': allow/deny, 'key': domain/cidr, 'value': ip/domain-value}]}] KEYPATTERN = 'base:acl:*' next, mainkeys = rdbconn.scan(0, KEYPATTERN, SCAN_COUNT) while next: next, tmpkeys = rdbconn.scan(next, KEYPATTERN, SCAN_COUNT) mainkeys += tmpkeys for mainkey in mainkeys: pipe.hgetall(mainkey) defined_acls = list() for detail in pipe.execute(): if detail: name = detail.get('name') action = detail.get('action') rulestrs = fieldjsonify(detail.get('rules')) rules = list( map( lambda rule: { 'action': rule[0], 'key': rule[1], 'value': rule[2] }, map(listify, rulestrs))) defined_acls.append({ 'name': name, 'action': action, 'rules': rules }) result = templates.TemplateResponse("acl.j2.xml", { "request": request, "sipprofiles": sipprofiles, "defined_acls": defined_acls }, media_type="application/xml") response.status_code = 200 except Exception as e: response.status_code, result = 500, str() logify( f"module=liberator, space=fsxmlapi, section=acl, requestid={get_request_uuid()}, exception={e}, traceback={traceback.format_exc()}" ) finally: return result
def sip(request: Request, response: Response): try: pipe = rdbconn.pipeline() # get netalias # {profilename1: profiledata1, profilename2: profiledata2} KEYPATTERN = 'base:netalias:*' next, mainkeys = rdbconn.scan(0, KEYPATTERN, SCAN_COUNT) while next: next, tmpkeys = rdbconn.scan(next, KEYPATTERN, SCAN_COUNT) mainkeys += tmpkeys for mainkey in mainkeys: pipe.hget(mainkey, 'addresses') details = pipe.execute() netaliases = dict() for mainkey, detail in zip(mainkeys, details): aliasname = getnameid(mainkey) addresses = list(map(listify, fieldjsonify(detail))) netaliases[aliasname] = { address[0]: { 'listen': address[1], 'advertise': address[2] } for address in addresses } # get the maping siprofile and data # {profilename1: profiledata1, profilename2: profiledata2} KEYPATTERN = 'sipprofile:*' next, mainkeys = rdbconn.scan(0, KEYPATTERN, SCAN_COUNT) while next: next, tmpkeys = rdbconn.scan(next, KEYPATTERN, SCAN_COUNT) mainkeys += tmpkeys for mainkey in mainkeys: pipe.hgetall(mainkey) details = pipe.execute() sipprofiles = dict() for mainkey, detail in zip(mainkeys, details): sipprofiles[getnameid(mainkey)] = jsonhash(detail) # get the mapping siprofile name and interconnection name # {profilename1: [intconname,...], profilename2: [intconname,...]} KEYPATTERN = 'intcon:out:*' next, mainkeys = rdbconn.scan(0, KEYPATTERN, SCAN_COUNT) while next: next, tmpkeys = rdbconn.scan(next, KEYPATTERN, SCAN_COUNT) mainkeys += tmpkeys for mainkey in mainkeys: if not mainkey.endswith('_gateways'): pipe.hget(mainkey, 'sipprofile') profilenames = pipe.execute() profile_intcons_maps = dict() for mainkey, profilename in zip(mainkeys, profilenames): intconname = getnameid(mainkey) if profilename not in profile_intcons_maps: profile_intcons_maps[profilename] = [intconname] else: if profilename not in profile_intcons_maps[profilename]: profile_intcons_maps[profilename].append(profilename) # get the mapping siprofile name and gateway name # {profilename1: [gateway,...], profilename2: [gateway,...]} profile_gwnames_maps = dict() for profile, intcons in profile_intcons_maps.items(): for intcon in intcons: pipe.hkeys(f'intcon:out:{intcon}:_gateways') profile_gwnames_maps[profile] = list( set([gw for gws in pipe.execute() for gw in gws])) # add gateway data to sip profile data profile_gateways_maps = dict() for profile, gwnames in profile_gwnames_maps.items(): for gwname in gwnames: pipe.hgetall(f'gateway:{gwname}') profile_gateways_maps[profile] = list(map(jsonhash, pipe.execute())) for sipprofile in sipprofiles: if 'gateways' in sipprofiles[sipprofile]: sipprofiles[sipprofile]['gateways'] = profile_gateways_maps[ sipprofile] # template result = templates.TemplateResponse("sip-setting.j2.xml", { "request": request, "sipprofiles": sipprofiles, 'netaliases': netaliases, 'NODEID': NODEID }, media_type="application/xml") response.status_code = 200 except Exception as e: response.status_code, result = 500, str() logify( f"module=liberator, space=fsxmlapi, section=sip-setting, requestid={get_request_uuid()}, exception={e}, traceback={traceback.format_exc()}" ) finally: return result
def directory(request: Request, response: Response): try: pipe = rdbconn.pipeline() # IP LIST OF SIP PROFILE AND REALM # {profilename: realm} KEYPATTERN = 'sipprofile:*' next, mainkeys = rdbconn.scan(0, KEYPATTERN, SCAN_COUNT) while next: next, tmpkeys = rdbconn.scan(next, KEYPATTERN, SCAN_COUNT) mainkeys += tmpkeys for mainkey in mainkeys: pipe.hget(mainkey, 'realm' ) realms = pipe.execute() sipprofiles = dict() for mainkey, realm in zip(mainkeys, realms): sipprofiles[getnameid(mainkey)] = realm # IP LIST OF INBOUND INTERCONNECTION # {profilename: {profilename, sipaddrs, secret}} KEYPATTERN = 'intcon:in:*' next, mainkeys = rdbconn.scan(0, KEYPATTERN, SCAN_COUNT) while next: next, tmpkeys = rdbconn.scan(next, KEYPATTERN, SCAN_COUNT) mainkeys += tmpkeys for mainkey in mainkeys: pipe.hmget(mainkey, 'sipprofile', 'sipaddrs', 'secret', 'authscheme', 'routing') directories = dict() for mainkey, details in zip(mainkeys, pipe.execute()): intconname = getnameid(mainkey) profilename = details[0] sipaddrs = fieldjsonify(details[1]) secret = details[2] authscheme = details[3] routing = details[4] if authscheme=='IP': password = DEFAULT_PASSWORD cidrs = sipaddrs elif authscheme=='DIGEST': password = secret cidrs = list() else: password = secret cidrs = sipaddrs for _profilename, _realm in sipprofiles.items(): if _profilename == profilename: a1hash = hashlib.md5(f'{intconname}:{_realm}:{password}'.encode()).hexdigest() if _realm in directories: directories[_realm].append({'id': intconname, 'cidrs': cidrs, 'a1hash': a1hash, 'routing': routing}) else: directories[_realm] = [{'id': intconname, 'cidrs': cidrs, 'a1hash': a1hash, 'routing': routing}] result = templates.TemplateResponse("directory.j2.xml", {"request": request, "directories": directories}, media_type="application/xml") response.status_code = 200 except Exception as e: response.status_code, result = 500, str() logify(f"module=liberator, space=fsxmlapi, section=directory, requestid={get_request_uuid()}, exception={e}, traceback={traceback.format_exc()}") finally: return result