def main(): service = MagmaService('policydb', mconfigs_pb2.PolicyDB()) apn_rules_dict = ApnRuleAssignmentsDict() assignments_dict = RuleAssignmentsDict() basenames_dict = BaseNameDict() rating_groups_dict = RatingGroupsDict() sessiond_chan = ServiceRegistry.get_rpc_channel('sessiond', ServiceRegistry.LOCAL) session_mgr_stub = LocalSessionManagerStub(sessiond_chan) sessiond_stub = SessionProxyResponderStub(sessiond_chan) reauth_handler = ReAuthHandler(assignments_dict, sessiond_stub) # Add all servicers to the server session_servicer = SessionRpcServicer(service.mconfig, rating_groups_dict, basenames_dict, apn_rules_dict) session_servicer.add_to_server(service.rpc_server) orc8r_chan = ServiceRegistry.get_rpc_channel('policydb', ServiceRegistry.CLOUD) policy_stub = PolicyAssignmentControllerStub(orc8r_chan) policy_servicer = PolicyRpcServicer(reauth_handler, basenames_dict, policy_stub) policy_servicer.add_to_server(service.rpc_server) # Start a background thread to stream updates from the cloud if service.config['enable_streaming']: stream = StreamerClient( { 'policydb': PolicyDBStreamerCallback(), 'apn_rule_mappings': ApnRuleMappingsStreamerCallback( session_mgr_stub, basenames_dict, apn_rules_dict, ), 'rule_mappings': RuleMappingsStreamerCallback( reauth_handler, basenames_dict, assignments_dict, apn_rules_dict, ), 'rating_groups': RatingGroupsStreamerCallback( rating_groups_dict), }, service.loop, ) stream.start() else: logging.info('enable_streaming set to False. Streamer disabled!') # Run the service loop service.run() # Cleanup the service service.close()
def test_EnableStaticRules(self): """ Check the happy path where everything succeeds. """ rules_by_sid = {} rules_by_basename = { "bn1": ChargingRuleNameSet(RuleNames=["p4", "p5"], ), } reauth_handler = ReAuthHandler( rules_by_sid, MockSessionProxyResponderStub(), ) servicer = PolicyRpcServicer( reauth_handler, rules_by_basename, MockPolicyAssignmentControllerStub(), ) # Bind the rpc server to a free port thread_pool = futures.ThreadPoolExecutor(max_workers=10) rpc_server = grpc.server(thread_pool) port = rpc_server.add_insecure_port('0.0.0.0:0') # Create a mock "mconfig" for the servicer to use mconfig = unittest.mock.Mock() mconfig.ip_block = None # Add the servicer servicer.add_to_server(rpc_server) rpc_server.start() # Create a rpc stub channel = grpc.insecure_channel('0.0.0.0:{}'.format(port)) stub = PolicyDBStub(channel) rules_by_basename["bn1"] = ChargingRuleNameSet(RuleNames=["p4", "p5"], ) req = EnableStaticRuleRequest( imsi="s1", rule_ids=["p1", "p2", "p3"], base_names=["bn1"], ) stub.EnableStaticRules(req) self.assertEqual( len(rules_by_sid["s1"].installed_policies), 5, 'After a successful update, Redis should be tracking ' '5 active rules.', )
def test_FailOrc8r(self): """ Check that nothing is updated if orc8r is unreachable """ rules_by_sid = {} rules_by_basename = { "bn1": ChargingRuleNameSet(RuleNames=["p4", "p5"], ), } reauth_handler = ReAuthHandler( rules_by_sid, MockSessionProxyResponderStub(), ) servicer = PolicyRpcServicer( reauth_handler, rules_by_basename, MockPolicyAssignmentControllerStub2(), ) # Bind the rpc server to a free port thread_pool = futures.ThreadPoolExecutor(max_workers=10) rpc_server = grpc.server(thread_pool) port = rpc_server.add_insecure_port('0.0.0.0:0') # Create a mock "mconfig" for the servicer to use mconfig = unittest.mock.Mock() mconfig.ip_block = None # Add the servicer servicer.add_to_server(rpc_server) rpc_server.start() # Create a rpc stub channel = grpc.insecure_channel('0.0.0.0:{}'.format(port)) stub = PolicyDBStub(channel) req = EnableStaticRuleRequest( imsi="s1", rule_ids=["p1", "p2", "p3"], base_names=["bn1"], ) with self.assertRaises(grpc.RpcError): stub.EnableStaticRules(req) self.assertFalse( "s1" in rules_by_sid, "There should be no installed policies for s1", )