def test_redis_unavailable(self): self._servicer._redis_dict = MockUnavailableRedis("localhost", 6380) req = UpdateRecordRequest() req.id = "IMSI557" req.fields["mac_addr"] = "aa:bb:aa:bb:aa:bb" with self.assertRaises(grpc.RpcError) as err: self._stub.UpdateRecord(req) self.assertEqual(err.exception.code(), grpc.StatusCode.UNAVAILABLE) get_req = GetDirectoryFieldRequest() get_req.id = "IMSI557" get_req.field_key = "mac_addr" with self.assertRaises(grpc.RpcError) as err: self._stub.GetDirectoryField(get_req) self.assertEqual(err.exception.code(), grpc.StatusCode.UNAVAILABLE) del_req = DeleteRecordRequest() del_req.id = "IMSI557" with self.assertRaises(grpc.RpcError) as err: self._stub.DeleteRecord(del_req) self.assertEqual(err.exception.code(), grpc.StatusCode.UNAVAILABLE) void_req = Void() with self.assertRaises(grpc.RpcError) as err: self._stub.GetAllDirectoryRecords(void_req) self.assertEqual(err.exception.code(), grpc.StatusCode.UNAVAILABLE)
def get_record_field_handler(client, args): get_request = GetDirectoryFieldRequest() get_request.id = args.id get_request.field_key = args.field_key try: res = client.GetDirectoryField(get_request) print("Successfully got field: (%s, %s) for ID: %s" % (res.key, res.value, args.id)) except grpc.RpcError as e: print("gRPC failed with %s: %s" % (e.code(), e.details()))
def get_record(imsi: str, field: str) -> str: """ Make RPC call to 'GetDirectoryField' method of local directoryD service """ try: chan = ServiceRegistry.get_rpc_channel(DIRECTORYD_SERVICE_NAME, ServiceRegistry.LOCAL) except ValueError: logging.error('Cant get RPC channel to %s', DIRECTORYD_SERVICE_NAME) return client = GatewayDirectoryServiceStub(chan) if not imsi.startswith("IMSI"): imsi = "IMSI" + imsi try: # Location will be filled in by directory service req = GetDirectoryFieldRequest(id=imsi, field_key=field) res = client.GetDirectoryField(req, DEFAULT_GRPC_TIMEOUT) if res.value is not None: return res.value except grpc.RpcError as err: logging.error( "GetDirectoryFieldRequest error for id: %s! [%s] %s", imsi, err.code(), err.details()) return None
def test_get_field(self): self._servicer._redis_dict.clear() req = UpdateRecordRequest() req.id = "IMSI557" req.fields["mac_addr"] = "aa:bb:aa:bb:aa:bb" self._stub.UpdateRecord(req) self.assertTrue(req.id in self._servicer._redis_dict) get_req = GetDirectoryFieldRequest() get_req.id = "IMSI557" get_req.field_key = "mac_addr" ret = self._stub.GetDirectoryField(get_req) self.assertEqual("aa:bb:aa:bb:aa:bb", ret.value) with self.assertRaises(grpc.RpcError) as err: get_req.field_key = "ipv4_addr" self._stub.GetDirectoryField(get_req) self.assertEqual(err.exception.code(), grpc.StatusCode.NOT_FOUND)
def _build_get_record_data(record_list: list, input_file: str): get_record_reqs = [] for index, record in enumerate(record_list): request = GetDirectoryFieldRequest( id=record.id, field_key="mac-addr", ) request_dict = json_format.MessageToDict(request) get_record_reqs.append(request_dict) with open(input_file, 'w') as file: json.dump(get_record_reqs, file, separators=(',', ':'))
def create_AbortSessionRequest(self, imsi: str) -> AbortSessionResult: # Get SessionID req = GetDirectoryFieldRequest(id=imsi, field_key="session_id") try: res = self._directorydstub.GetDirectoryField( req, DEFAULT_GRPC_TIMEOUT) except grpc.RpcError as err: print("Error: GetDirectoryFieldRequest error for id: %s! [%s] %s" % (imsi, err.code(), err.details())) self._print_directoryd_content() return self._abort_session_stub.AbortSession( AbortSessionRequest( session_id=res.value, user_name=imsi, ))
def create_ReAuthRequest(self, imsi, policy_id, flow_list, qos): """ Sends Policy RAR message to session manager """ print("Sending Policy RAR message to session manager") flow_match_list = [] res = None self.get_flow_match(flow_list, flow_match_list) policy_qos = FlowQos( qci=qos["qci"], max_req_bw_ul=qos["max_req_bw_ul"], max_req_bw_dl=qos["max_req_bw_dl"], gbr_ul=qos["gbr_ul"], gbr_dl=qos["gbr_dl"], arp=QosArp( priority_level=qos["arp_prio"], pre_capability=qos["pre_cap"], pre_vulnerability=qos["pre_vul"], ), ) policy_rule = PolicyRule( id=policy_id, priority=qos["priority"], flow_list=flow_match_list, tracking_type=PolicyRule.NO_TRACKING, rating_group=1, monitoring_key=None, qos=policy_qos, ) qos = QoSInformation(qci=qos["qci"]) # Get sessionid req = GetDirectoryFieldRequest(id=imsi, field_key="session_id") try: res = self._directorydstub.GetDirectoryField( req, DEFAULT_GRPC_TIMEOUT) except grpc.RpcError as err: logging.error( "GetDirectoryFieldRequest error for id: %s! [%s] %s", imsi, err.code(), err.details(), ) self._session_stub.PolicyReAuth( PolicyReAuthRequest( session_id=res.value, imsi=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, ))
def create_ReAuthRequest(self, imsi, policy_id, flow_list, qos): """ Sends Policy RAR message to session manager """ print("Sending Policy RAR message to session manager") flow_match_list = [] res = None self.get_flow_match(flow_list, flow_match_list) policy_qos = FlowQos( qci=qos["qci"], max_req_bw_ul=qos["max_req_bw_ul"], max_req_bw_dl=qos["max_req_bw_dl"], gbr_ul=qos["gbr_ul"], gbr_dl=qos["gbr_dl"], arp=QosArp( priority_level=qos["arp_prio"], pre_capability=qos["pre_cap"], pre_vulnerability=qos["pre_vul"], ), ) policy_rule = PolicyRule( id=policy_id, priority=qos["priority"], flow_list=flow_match_list, tracking_type=PolicyRule.NO_TRACKING, rating_group=1, monitoring_key=None, qos=policy_qos, ) qos = QoSInformation(qci=qos["qci"]) # Get sessionid #TODO: remove retries i = 0 MAX = 3 res = None while i < MAX: req = GetDirectoryFieldRequest(id=imsi, field_key="session_id") try: res = self._directorydstub.GetDirectoryField( req, DEFAULT_GRPC_TIMEOUT) except grpc.RpcError as err: print( "error: GetDirectoryFieldRequest error for id: %s! [%s] %s", imsi, err.code(), err.details(), ) if req != None: i = MAX else: i += 1 print( "warning: directoryd failed to return sessionId for %s. Retrying", imsi) time.sleep(3) if res == None: print("error: Couldnt find sessionid. Directoryd content:") allRecordsResponse = self._directorydstub.GetAllDirectoryRecords( Void(), DEFAULT_GRPC_TIMEOUT) for record in allRecordsResponse.recordsResponse: print("%s", str(record)) self._session_stub.PolicyReAuth( PolicyReAuthRequest( session_id=res.value, imsi=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, ))