def _cleanup_subs(): client = SubscriberDBStub( ServiceRegistry.get_rpc_channel( SUBSCRIBERDB_SERVICE_NAME, ServiceRegistry.LOCAL, ), ) for sid in client.ListSubscribers(Void()).sids: client.DeleteSubscriber(SIDUtils.to_pb('IMSI%s' % sid.id))
def create_account_in_OCS(imsi): ocs_chan = ServiceRegistry.get_rpc_channel("ocs", ServiceRegistry.CLOUD) ocs_client = MockOCSStub(ocs_chan) print("Clearing accounts in OCS") ocs_client.ClearSubscribers(Void()) print("Creating account in OCS") ocs_client.CreateAccount(SubscriberID(id=imsi))
def _build_list_subs_data(num_subs: int, input_file: str): _load_subs(num_subs) list_subs_reqs = [] for i in range(1, num_subs): list_sub_req_dict = json_format.MessageToDict(Void()) list_subs_reqs.append(list_sub_req_dict) with open(input_file, 'w') as file: json.dump(list_subs_reqs, file, separators=(',', ':'))
def _get_subscribers(self): chan = ServiceRegistry.get_rpc_channel( 'mobilityd', ServiceRegistry.LOCAL, ) client = MobilityServiceStub(chan) table = client.GetSubscriberIPTable(Void()) return table.entries
def create_account_in_PCRF(imsi): pcrf_chan = ServiceRegistry.get_rpc_channel("pcrf", ServiceRegistry.CLOUD) pcrf_client = MockPCRFStub(pcrf_chan) print("Clearing accounts in PCRF") pcrf_client.ClearSubscribers(Void()) print("Creating account in PCRF") pcrf_client.CreateAccount(SubscriberID(id=imsi))
def _setup_ip_block(client): ip_blocks_rsp = client.ListAddedIPv4Blocks(Void()) remove_blocks_req = RemoveIPBlockRequest(force=True) for block in ip_blocks_rsp.ip_block_list: remove_blocks_req.ip_blocks.append(block) client.RemoveIPBlock(remove_blocks_req) ip_block = ipaddress.ip_network('192.168.128.0/20') client.AddIPBlock(IPBlock(version=IPBlock.IPV4, net_address=ip_block.network_address.packed, prefix_len=ip_block.prefixlen))
def _reload_service_config(client, service): response = client.ReloadServiceConfig(Void()) if response.result == ReloadConfigResponse.RELOAD_SUCCESS: print('Config successfully reloaded. ' 'Some config may require a service restart to take effect') else: print('Config cannot be reloaded. Service restart required.') should_restart = input('Restart %s? (y/N) ' % service).lower() == 'y' if should_restart: _restart_service(service)
def list_suciprofile(client, args): """ list_suciprofile """ response = client.ListSuciProfile(Void()) if not response.suci_profiles: print("SuciProfileList is empty") else: for x in response.suci_profiles: print_suciprofile(x)
def _cleanup_subs(): """Clear directory records""" client = GatewayDirectoryServiceStub( ServiceRegistry.get_rpc_channel( DIRECTORYD_SERVICE_NAME, ServiceRegistry.LOCAL, ), ) for record in client.GetAllDirectoryRecords(Void()).records: sid = DeleteRecordRequest(id=record.id, ) client.DeleteRecord(sid)
def RebootAll(self, _=None, context=None) -> None: """ Reboot all connected eNodeB devices """ print_grpc( Void(), self._print_grpc_payload, "RebootAll Request:", ) serial_list = self.state_machine_manager.get_connected_serial_id_list() for enb_serial in serial_list: handler = self._get_handler(enb_serial) handler.reboot_asap()
async def _get_attached_imsis(self) -> List[str]: try: smsd_resp = await grpc_async_wrapper( self._directoryd.GetAllDirectoryRecords.future( Void(), TIMEOUT_SECS, ), ) return [r.id for r in smsd_resp.records] except grpc.RpcError as err: logging.error("Error fetching IMSIs from directoryd: %s", err) return []
def get_gateway_hw_id(): """ Get the hardware ID of the gateway. Is blocking. Returns: hw_id (str): hardware ID of the gateway specified by env variable GATEWAY_IP """ magmad_stub = MagmadStub(get_rpc_channel('magmad')) stub_response = magmad_stub.GetGatewayId(Void()) gateway_hw_id = stub_response.gateway_id return gateway_hw_id
def test_remove_no_assigned_blocks(self): """ remove should return nothing """ remove_request0 = RemoveIPBlockRequest(ip_blocks=[self._block_msg], force=False) resp = self._stub.RemoveIPBlock(remove_request0) expect = RemoveIPBlockResponse() self.assertEqual(expect, resp) resp = self._stub.ListAddedIPv4Blocks(Void()) expect = ListAddedIPBlocksResponse() self.assertEqual(expect, resp)
async def test(): # Mock out GetServiceInfo.future mock = unittest.mock.Mock() service_info_future = asyncio.Future() service_info_future.set_result(ServiceInfo()) mock.GetServiceInfo.future.side_effect = [service_info_future] service303_mock.side_effect = [mock] await self._service_poller._get_service_info() mock.GetServiceInfo.future.assert_called_once_with( Void(), self._service_poller.GET_STATUS_TIMEOUT)
def EnableStaticRules( self, request: EnableStaticRuleRequest, context, ) -> Void: """ Associate the static rules with the specified subscriber. Also send a RAR to sessiond to install the specified rules for the subscriber. """ try: self._subscriberdb_stub.EnableStaticRules(request) except grpc.RpcError: logging.error( 'Unable to enable rules for subscriber %s. ', request.imsi, ) context.set_code(grpc.StatusCode.NOT_FOUND) context.set_details('Failed to update rule assignments in orc8r') return Void() rules_to_install = self._get_rules( request.rule_ids, request.base_names, ) rar = PolicyReAuthRequest( # Leave session id empty, re-auth for all sessions imsi=request.imsi, rules_to_install=[ StaticRuleInstall(rule_id=rule_id) for rule_id in rules_to_install ], ) success = self._reauth_handler.handle_policy_re_auth(rar) if not success: context.set_code(grpc.StatusCode.UNKNOWN) context.set_details( 'Failed to enable all static rules for ' 'subscriber. Partial update may have succeeded', ) return Void()
def test_service_run(self, mock_get_proxy_config): """ Test if the service starts and stops gracefully. """ self.assertEqual(self._service.state, ServiceInfo.STARTING) mock_get_proxy_config.return_value = { 'cloud_address': '127.0.0.1', 'proxy_cloud_connections': True, } # Start the service and pause the loop self._service.loop.stop() self._service.run() asyncio.set_event_loop(self._service.loop) self._service.log_counter._periodic_task.cancel() self.assertEqual(self._service.state, ServiceInfo.ALIVE) # Create a rpc stub and query the Service303 interface ServiceRegistry.add_service('test', '0.0.0.0', self._service.port) channel = ServiceRegistry.get_rpc_channel( 'test', ServiceRegistry.LOCAL, ) self._stub = Service303Stub(channel) info = ServiceInfo( name='test', version='0.0.0', state=ServiceInfo.ALIVE, health=ServiceInfo.APP_HEALTHY, start_time_secs=12345, ) self.assertEqual(self._stub.GetServiceInfo(Void()), info) # Stop the service self._stub.StopService(Void()) self._service.loop.run_forever() self.assertEqual(self._service.state, ServiceInfo.STOPPED)
async def test(): # Mock out GetServiceInfo.future mock = unittest.mock.Mock() service_info_future = asyncio.Future() # Force an exception to happen service_info_future.add_done_callback = fake_add_done mock.GetServiceInfo.future.side_effect = [service_info_future] service303_mock.side_effect = [mock] await self._service_poller._get_service_info() mock.GetServiceInfo.future.assert_called_once_with( Void(), self._service_poller.GET_STATUS_TIMEOUT)
def collect(self, service_name): """ Calls into Service303 to get service metrics samples and rescheudle collection. """ chan = ServiceRegistry.get_rpc_channel(service_name, ServiceRegistry.LOCAL) client = Service303Stub(chan) future = client.GetMetrics.future(Void(), self.grpc_timeout) future.add_done_callback(lambda future: self._loop.call_soon_threadsafe( self.collect_done, service_name, future)) self._loop.call_later(self.collect_interval, self.collect, service_name)
def test_set_gw_info(self): mac1 = "22:22:c6:d0:02:3c" ipaddr1 = ipaddress.ip_address("10.1.1.11") gwinfo_msg = GWInfo() gwinfo_msg.ip.version = IPBlock.IPV4 gwinfo_msg.ip.address = ipaddr1.packed gwinfo_msg.mac = mac1 self._stub.SetGatewayInfo(gwinfo_msg) gw_info = self._stub.GetGatewayInfo(Void()) gw_ip_get = ipaddress.ip_address(gw_info.ip.address) self.assertEqual(ipaddr1, gw_ip_get) self.assertEqual(mac1, gw_info.mac)
def get_suciprofile(home_net_public_key_id: int): """ get_suciprofile """ channel = grpc.insecure_channel('localhost:50051') stub = SuciProfileDBStub(channel) response = stub.ListSuciProfile(Void()) if not response.suci_profiles: print("SuciProfileList is empty") else: for x in response.suci_profiles: if home_net_public_key_id == x.home_net_public_key_id: return x.home_net_public_key return None
def test_add_delete_subscriber(self): """ Test if AddSubscriber and DeleteSubscriber rpc call works """ sid = SIDUtils.to_pb('IMSI1') data = SubscriberData(sid=sid) # Add subscriber self._stub.AddSubscriber(data) # Add subscriber again with self.assertRaises(grpc.RpcError) as err: self._stub.AddSubscriber(data) self.assertEqual(err.exception.code(), grpc.StatusCode.ALREADY_EXISTS) # See if we can get the data for the subscriber self.assertEqual(self._stub.GetSubscriberData(sid).sid, data.sid) self.assertEqual(len(self._stub.ListSubscribers(Void()).sids), 1) self.assertEqual(self._stub.ListSubscribers(Void()).sids[0], sid) # Delete the subscriber self._stub.DeleteSubscriber(sid) self.assertEqual(len(self._stub.ListSubscribers(Void()).sids), 0)
def _print_directoryd_content(self): try: allRecordsResponse = self._directorydstub.GetAllDirectoryRecords( Void(), DEFAULT_GRPC_TIMEOUT) except grpc.RpcError as e: print( "error: couldnt print directoryd content. gRPC failed with %s: %s" % (e.code(), e.details())) return if allRecordsResponse is None: print("No records were found at directoryd") else: for record in allRecordsResponse.records: print("%s" % str(record))
def test_poll_exception(self, _service_configs_mock, service303_mock): """ Test if the query to Service303 fails and handled gracefully. """ # Mock out GetServiceInfo.future mock = unittest.mock.Mock() mock.GetServiceInfo.future.side_effect = [unittest.mock.Mock()] service303_mock.side_effect = [mock] self._service_poller.start() mock.GetServiceInfo.future.assert_called_once_with( Void(), self._service_poller.GET_STATUS_TIMEOUT) # pylint: disable=protected-access self._service_poller._get_service_info_done('test1', MockFuture(True))
def _build_release_ip_data(client): release_ip_reqs = [] table = client.GetSubscriberIPTable(Void()) if not table.entries: print('No IPs allocated to be freed, please run allocate test first') exit(1) for entry in table.entries: release_ip_req = ReleaseIPRequest(sid=entry.sid, ip=entry.ip, apn=entry.apn) release_ip_dict = json_format.MessageToDict(release_ip_req) # Dumping ReleaseIP request into json release_ip_reqs.append(release_ip_dict) with open('release_data.json', 'w') as file: json.dump(release_ip_reqs, file)
def test_get_subscriber_ip_table(self): """ test GetSubscriberIPTable """ self._stub.AddIPBlock(self._block_msg) resp = self._stub.GetSubscriberIPTable(Void()) self.assertEqual(len(resp.entries), 0) alloc_request0 = AllocateIPRequest(sid=self._sid0, version=AllocateIPRequest.IPV4, apn=self._apn0) ip_msg0 = self._stub.AllocateIPAddress(alloc_request0) entry0 = SubscriberIPTableEntry(sid=self._sid0, ip=ip_msg0.ip_list[0], apn=self._apn0) resp = self._stub.GetSubscriberIPTable(Void()) self.assertTrue(entry0 in resp.entries) alloc_request1 = AllocateIPRequest(sid=self._sid1, version=AllocateIPRequest.IPV4, apn=self._apn1) ip_msg1 = self._stub.AllocateIPAddress(alloc_request1) entry1 = SubscriberIPTableEntry(sid=self._sid1, ip=ip_msg1.ip_list[0], apn=self._apn1) resp = self._stub.GetSubscriberIPTable(Void()) self.assertTrue(entry0 in resp.entries) self.assertTrue(entry1 in resp.entries) # keep in table after in release release_request0 = ReleaseIPRequest(sid=self._sid0, ip=ip_msg0.ip_list[0], apn=self._apn0) resp = self._stub.ReleaseIPAddress(release_request0) resp = self._stub.GetSubscriberIPTable(Void()) self.assertTrue(entry0 in resp.entries) self.assertTrue(entry1 in resp.entries)
def test_get_gw_info(self): gw_info = self._stub.GetGatewayInfo(Void()) gw_ip_get = str(ipaddress.ip_address(gw_info.ip.address)) def_gw_cmd = shlex.split("ip route show") p = subprocess.Popen(def_gw_cmd, stdout=subprocess.PIPE) output = p.stdout.read().decode("utf-8") def_ip = None for line in output.splitlines(): if 'default ' in line: tokens = line.split() def_ip = tokens[2] break self.assertEqual(def_ip, gw_ip_get)
def get_gateway_service_mconfigs(services: List[str]) -> Dict[str, Any]: """ Get the managed configurations of some gateway services. Args: services: List of service names to fetch configs for Returns: service mconfigs keyed by name """ ret = {} magmad_stub = MagmadStub(get_rpc_channel('magmad')) stub_response = magmad_stub.GetConfigs(Void()) for srv in services: ret[srv] = unpack_mconfig_any(stub_response.configs_by_key[srv]) return ret
def get_table_assignment(client, args): response = client.GetAllTableAssignments(Void()) table_assignments = response.table_assignments if args.apps: app_filter = args.apps.split(',') table_assignments = [table_assignment for table_assignment in table_assignments if table_assignment.app_name in app_filter] table_template = '{:<25}{:<20}{:<25}' print(table_template.format('App', 'Main Table', 'Scratch Tables')) print('-' * 70) for table_assignment in table_assignments: print(table_template.format( table_assignment.app_name, table_assignment.main_table, str([table for table in table_assignment.scratch_tables])))
def gateway_health_status(): config = load_service_mconfig_as_json('mme') # eNB status for #eNBs connected chan = ServiceRegistry.get_rpc_channel('enodebd', ServiceRegistry.LOCAL) client = EnodebdStub(chan) status = client.GetStatus(Void()) mme_log_path = '/var/log/mme.log' health_summary = AGWHealthSummary( relay_enabled=config['relayEnabled'], nb_enbs_connected=status.meta['n_enodeb_connected'], allocated_ips=get_allocated_ips(), subscriber_table=get_subscriber_table(), registration_success_rate=get_registration_success_rate(mme_log_path), ) return str(health_summary)
def list_allocated_ips_handler(client, args): list_blocks_resp = client.ListAddedIPv4Blocks(Void()) for block_msg in list_blocks_resp.ip_block_list: ip = ipaddress.ip_address(block_msg.net_address) block = ipaddress.ip_network("%s/%d" % (ip, block_msg.prefix_len)) print("IPs allocated from block %s:" % block) list_ips_resp = client.ListAllocatedIPs(block_msg) for ip_msg in list_ips_resp.ip_list: if ip_msg.version == IPAddress.IPV4: ip = ipaddress.IPv4Address(ip_msg.address) elif ip_msg.address == IPAddress.IPV6: ip = ipaddress.IPv6Address(ip_msg.address) else: print("Unsupported IP Version") continue print("\t%s" % ip)