def get_enb_s1_connected_states(configured_serial_ids, mconfig) -> List[State]: states = [] enb_s1_connected = get_all_enb_connected() for enb_id in enb_s1_connected: enb = find_enb_by_cell_id(mconfig, enb_id) if enb and enb.serial_num not in configured_serial_ids: status = EnodebStatus(enodeb_configured=False, gps_latitude='N/A', gps_longitude='N/A', enodeb_connected=True, opstate_enabled=False, rf_tx_on=False, rf_tx_desired=False, gps_connected=False, ptp_connected=False, mme_connected=True, fsm_state='N/A') status_dict = status._asdict() status_dict['ip_address'] = enb.config.ip_address serialized = json.dumps(status_dict) state = State(type="single_enodeb", deviceID=enb.serial_num, value=serialized.encode('utf-8')) states.append(state) return states
def get_enb_s1_connected_states(enb_s1_state_map, configured_serial_ids, mconfig) -> List[State]: states = [] for enb_id in enb_s1_state_map: enb = find_enb_by_cell_id(mconfig, enb_id) if enb and enb.serial_num not in configured_serial_ids: status = EnodebStatus(enodeb_configured=False, gps_latitude='N/A', gps_longitude='N/A', enodeb_connected=True, opstate_enabled=False, rf_tx_on=False, rf_tx_desired=False, gps_connected=False, ptp_connected=False, mme_connected=True, fsm_state='N/A', cell_id=enb_id) status_dict = status._asdict() # Add IP address to state status_dict['ip_address'] = enb.config.ip_address # Add num of UEs connected to state, use cellID from mconfig status_dict['ues_connected'] = enb_s1_state_map.get(enb_id, 0) serialized = json.dumps(status_dict) state = State( type="single_enodeb", deviceID=enb.serial_num, value=serialized.encode('utf-8') ) states.append(state) return states
async def _collect_states_to_replicate(self): states_to_report = [] for key in self._redis_client: try: idval, state_type = self._parse_key(key) except ValueError as err: logging.debug(err) continue state_scope = self._serdes[state_type].state_scope device_id = self.make_scoped_device_id(idval, state_scope) in_mem_key = self.make_mem_key(device_id, state_type) redis_version = self._redis_client.get_version(idval, state_type) if in_mem_key in self._state_versions and \ self._state_versions[in_mem_key] == redis_version: continue redis_state = self._redis_client.get(key) if self._serdes[state_type].state_format == PROTO_FORMAT: state_to_serialize = MessageToDict(redis_state) else: state_to_serialize = redis_state serialized_json_state = json.dumps(state_to_serialize) state_proto = State(type=state_type, deviceID=device_id, value=serialized_json_state.encode("utf-8"), version=redis_version) states_to_report.append(state_proto) if len(states_to_report) == 0: logging.debug("Not replicating state. No state has changed!") return None return ReportStatesRequest(states=states_to_report)
def get_operational_states(enb_acs_manager: StateMachineManager, mconfig: mconfigs_pb2.EnodebD) -> List[State]: """ Returns: A list of State with EnodebStatus encoded as JSON """ states = [] configured_serial_ids = [] enb_status_by_serial = get_all_enb_status(enb_acs_manager) for serial_id in enb_status_by_serial: enb_status_dict = enb_status_by_serial[serial_id]._asdict() enb_status_dict['ip_address'] = enb_acs_manager.get_ip_of_serial( serial_id) serialized = json.dumps(enb_status_dict) state = State(type="single_enodeb", deviceID=serial_id, value=serialized.encode('utf-8')) configured_serial_ids.append(serial_id) states.append(state) # Get S1 connected eNBs s1_states = get_enb_s1_connected_states(configured_serial_ids, mconfig) for state in s1_states: states.append(state) return states
async def _collect_states_to_replicate(self): states_to_report = [] for redis_dict in self._redis_dicts: for key in redis_dict: device_id = make_scoped_device_id(key, redis_dict.state_scope) in_mem_key = make_mem_key(device_id, redis_dict.redis_type) redis_version = redis_dict.get_version(key) self._state_keys_from_current_iteration.add(in_mem_key) if in_mem_key in self._state_versions and \ self._state_versions[in_mem_key] == redis_version: continue redis_state = redis_dict.get(key) if redis_dict.state_format == PROTO_FORMAT: state_to_serialize = MessageToDict(redis_state) serialized_json_state = json.dumps(state_to_serialize) else: serialized_json_state = jsonpickle.encode(redis_state) state_proto = State(type=redis_dict.redis_type, deviceID=device_id, value=serialized_json_state.encode("utf-8"), version=redis_version) states_to_report.append(state_proto) if len(states_to_report) == 0: logging.debug("Not replicating state. No state has changed!") return None return ReportStatesRequest(states=states_to_report)
def _get_checkin_request_as_state(self) -> State: request = self._checkin_manager.get_latest_checkin_request() value = MessageToJson(request) state = State(type="checkin_request", deviceID=snowflake.snowflake(), value=value.encode('utf-8')) return state
async def _collect_states_to_replicate(self): states_to_report = [] for redis_dict in self._redis_dicts: for key in redis_dict: redis_state = redis_dict.get(key) device_id = make_scoped_device_id(key, redis_dict.state_scope) in_mem_key = make_mem_key(device_id, redis_dict.redis_type) if redis_state is None: logging.debug( "Content of key %s is empty, skipping", in_mem_key, ) continue redis_version = redis_dict.get_version(key) self._state_keys_from_current_iteration.add(in_mem_key) if in_mem_key in self._state_versions and \ self._state_versions[in_mem_key] == redis_version: logging.debug( "key %s already read on this iteration, skipping", in_mem_key, ) continue try: if redis_dict.state_format == PROTO_FORMAT: state_to_serialize = MessageToDict(redis_state) serialized_json_state = json.dumps(state_to_serialize) else: serialized_json_state = jsonpickle.encode(redis_state) except Exception as e: # pylint: disable=broad-except logging.error( "Found bad state for %s for %s, not " "replicating this state: %s", key, device_id, e, ) continue state_proto = State( type=redis_dict.redis_type, deviceID=device_id, value=serialized_json_state.encode("utf-8", ), version=redis_version, ) logging.debug( "key with version, %s contains: %s", in_mem_key, serialized_json_state, ) states_to_report.append(state_proto) if len(states_to_report) == 0: logging.debug("Not replicating state. No state has changed!") return None return ReportStatesRequest(states=states_to_report)
def _get_gw_state(self) -> Optional[State]: gw_state = self._checkin_manager.get_latest_gw_state() if gw_state is not None: state = State(type="gw_state", deviceID=snowflake.snowflake(), value=gw_state.encode('utf-8')) return state return None
def serialize_subscriber_states( sub_table: Dict[str, ICMPMonitoringResponse]) -> List[State]: states = [] for sub, icmp_resp in sub_table.items(): serialized = json.dumps(icmp_resp._asdict()) state = State(type=ICMP_STATE_TYPE, deviceID=sub, value=serialized.encode('utf-8')) states.append(state) return states
def get_operational_states( state_machine_manager: StateMachineManager) -> List[State]: """ Returns: A list of SingleEnodebStatus encoded as JSON into State """ states = [] for serial_id in state_machine_manager.get_connected_serial_id_list(): enb_state = get_single_enb_status(serial_id, state_machine_manager) state = State(type="enodeb", deviceID=serial_id, value=MessageToJson(enb_state).encode('utf-8')) states.append(state) return states
def get_operational_states( enb_acs_manager: StateMachineManager, ) -> List[State]: """ Returns: A list of State with EnodebStatus encoded as JSON """ states = [] enb_status_by_serial = get_all_enb_status(enb_acs_manager) for serial_id in enb_status_by_serial: serialized = json.dumps(enb_status_by_serial[serial_id]._asdict()) state = State(type="single_enodeb", deviceID=serial_id, value=serialized.encode('utf-8')) states.append(state) return states
def _construct_operational_state_mock(device_id: str) \ -> unittest.mock.Mock: mock = unittest.mock.Mock() future = asyncio.Future() future.set_result( GetOperationalStatesResponse(states=[ State( type="test", deviceID=device_id, value="hello!".encode('utf-8'), ) ])) mock.GetOperationalStates.future.side_effect = [future] return mock
def get_operational_states( enb_acs_manager: StateMachineManager, mconfig: mconfigs_pb2.EnodebD, print_grpc_payload: bool = False, ) -> List[State]: """ Returns: A list of State with EnodebStatus encoded as JSON """ states = [] configured_serial_ids = [] enb_status_by_serial = get_all_enb_status(enb_acs_manager) # Get S1 connected eNBs enb_statuses = get_all_enb_state(print_grpc_payload) for serial_id in enb_status_by_serial: enb_status_dict = enb_status_by_serial[serial_id]._asdict() # Add IP address to state enb_status_dict['ip_address'] = enb_acs_manager.get_ip_of_serial( serial_id, ) # Add num of UEs connected num_ue_connected = enb_statuses.get(enb_status_dict['cell_id'], 0) enb_status_dict['ues_connected'] = num_ue_connected serialized = json.dumps(enb_status_dict) state = State( type="single_enodeb", deviceID=serial_id, value=serialized.encode('utf-8'), ) configured_serial_ids.append(serial_id) states.append(state) # Get state for externally configured enodebs s1_states = get_enb_s1_connected_states( enb_statuses, configured_serial_ids, mconfig, ) states.extend(s1_states) return states
async def test_checkin(proxy_cloud_connections=True): """Send checkin using either proxy or direct to cloud connection""" chan = ServiceRegistry.get_rpc_channel( 'state', ServiceRegistry.CLOUD, proxy_cloud_connections=proxy_cloud_connections, ) client = StateServiceStub(chan) # Construct a simple state to send for test value = json.dumps({"datetime": datetime.datetime.now()}, default=str) states = [ State(type="string_map", deviceID=snowflake.snowflake(), value=value.encode('utf-8')), ] request = ReportStatesRequest(states=states) timeout = 1000 await grpc_async_wrapper(client.ReportStates.future(request, timeout))
async def test(): # force bootstrap to be called on the first error self.state_reporter._error_handler.fail_threshold = 0 # use dummy state servicer get_rpc_mock.return_value = self.channel # mock out get_proxy_config get_proxy_config_mock.return_value = { "cloud_address": 1, "cloud_port": 2, "gateway_cert": 3, "gateway_key": 4, } # force cert_is_invalid to return false future = asyncio.Future() future.set_result(True) cert_is_invalid_mock.return_value = future # make schedule_bootstrap_now awaitable future = asyncio.Future() future.set_result(None) bootstrap_manager.schedule_bootstrap_now.return_value = future request = ReportStatesRequest(states=[State(type="fail")]) await self.state_reporter._send_to_state_service(request)
def _make_state(type_val: str, key: str, value: str) -> State: return State(type=type_val, deviceID=key, value=value.encode('utf-8'))