def __init__(self): """ Initialize sessionManager util. """ self._session_stub = SessionProxyResponderStub( get_rpc_channel("sessiond")) self._directorydstub = GatewayDirectoryServiceStub( get_rpc_channel("directoryd"))
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 send_policy_rar(client, args): sessiond_chan = ServiceRegistry.get_rpc_channel("sessiond", ServiceRegistry.LOCAL) sessiond_client = SessionProxyResponderStub(sessiond_chan) flow_list_str = args.flow_rules.split(";") flow_match_list = [] for i, flow_str in enumerate(flow_list_str): print("%d: %s" % (i, flow_str)) flow_fields = flow_str.split(",") if flow_fields[0] == "UL": flow_direction = FlowMatch.UPLINK elif flow_fields[0] == "DL": flow_direction = FlowMatch.DOWNLINK else: print("%s is not valid" % flow_fields[0]) raise ValueError( "UL or DL are the only valid" " values for first parameter of flow match" ) ip_protocol = int(flow_fields[1]) if flow_fields[1] == FlowMatch.IPPROTO_TCP: udp_src_port = 0 udp_dst_port = 0 if flow_fields[3]: tcp_src_port = int(flow_fields[3]) else: tcp_src_port = 0 if flow_fields[5]: tcp_dst_port = int(flow_fields[5]) else: tcp_dst_port = 0 elif flow_fields[1] == FlowMatch.IPPROTO_UDP: tcp_src_port = 0 tcp_dst_port = 0 if flow_fields[3]: udp_src_port = int(flow_fields[3]) else: udp_src_port = 0 if flow_fields[5]: udp_dst_port = int(flow_fields[5]) else: udp_dst_port = 0 else: udp_src_port = 0 udp_dst_port = 0 tcp_src_port = 0 tcp_dst_port = 0 flow_match_list.append( FlowDescription( match=FlowMatch( direction=flow_direction, ip_proto=ip_protocol, ipv4_src=flow_fields[2], ipv4_dst=flow_fields[4], tcp_src=tcp_src_port, tcp_dst=tcp_dst_port, udp_src=udp_src_port, udp_dst=udp_dst_port, ), action=FlowDescription.PERMIT, ) ) qos_parameter_list = args.qos.split(",") if len(qos_parameter_list) == 7: # utilize user passed arguments policy_qos = FlowQos( qci=int(args.qci), max_req_bw_ul=int(qos_parameter_list[0]), max_req_bw_dl=int(qos_parameter_list[1]), gbr_ul=int(qos_parameter_list[2]), gbr_dl=int(qos_parameter_list[3]), arp=QosArp( priority_level=int(qos_parameter_list[4]), pre_capability=int(qos_parameter_list[5]), pre_vulnerability=int(qos_parameter_list[6]), ), ) else: # parameter missing, use default values policy_qos = FlowQos( qci=int(args.qci), max_req_bw_ul=100000, max_req_bw_dl=100000, arp=QosArp(priority_level=1, pre_capability=1, pre_vulnerability=0), ) policy_rule = PolicyRule( id=args.policy_id, priority=int(args.priority), flow_list=flow_match_list, tracking_type=PolicyRule.NO_TRACKING, rating_group=1, monitoring_key=None, qos=policy_qos, ) qos = QoSInformation(qci=int(args.qci)) reauth_result = sessiond_client.PolicyReAuth( PolicyReAuthRequest( session_id=args.session_id, imsi=args.imsi, rules_to_remove=[], rules_to_install=[], dynamic_rules_to_install=[DynamicRuleInstall(policy_rule=policy_rule)], event_triggers=[], revalidation_time=None, usage_monitoring_credits=[], qos_info=qos, ) ) print(reauth_result)