def _update_grant_from_request(response: DBResponse, grant: DBGrant) -> None: payload = response.request.payload operation_param = payload.get(OPERATION_PARAM, {}) frequency_range = operation_param.get("operationFrequencyRange", {}) grant.max_eirp = operation_param.get("maxEirp", 0) grant.low_frequency = frequency_range.get("lowFrequency", 0) grant.high_frequency = frequency_range.get("highFrequency", 0)
def test_unset_frequency(self, orig_avail_freqs, low_freq, high_freq, expected_avail_freq): # Given cbsd = DBCbsd( cbsd_id="some_cbsd_id", fcc_id="some_fcc_id", cbsd_serial_number="some_serial_number", user_id="some_user_id", state_id=1, desired_state_id=1, available_frequencies=orig_avail_freqs, ) grant = DBGrant( cbsd=cbsd, state_id=1, grant_id="some_grant_id", low_frequency=low_freq, high_frequency=high_freq, max_eirp=15, ) self.session.add_all([cbsd, grant]) self.session.commit() # When unset_frequency(grant) cbsd = self.session.query(DBCbsd).filter(DBCbsd.id == cbsd.id).scalar() # Then self.assertEqual(expected_avail_freq, cbsd.available_frequencies)
def _update_grant_from_response(response: DBResponse, grant: DBGrant) -> None: if not grant: return grant_expire_time = response.payload.get(GRANT_EXPIRE_TIME, None) heartbeat_interval = response.payload.get(HEARTBEAT_INTERVAL, None) transmit_expire_time = response.payload.get(TRANSMIT_EXPIRE_TIME, None) channel_type = response.payload.get(CHANNEL_TYPE, None) if grant_expire_time: grant.grant_expire_time = grant_expire_time if heartbeat_interval: grant.heartbeat_interval = int(heartbeat_interval) if transmit_expire_time: grant.transmit_expire_time = transmit_expire_time if channel_type: grant.channel_type = channel_type logger.info(f'Updated grant: {grant}')
def _get_or_create_grant_from_response( obj: ResponseDBProcessor, response: DBResponse, session: Session, ) -> Optional[DBGrant]: cbsd_id = response.payload.get( CBSD_ID, ) or response.request.payload.get(CBSD_ID) grant_id = response.payload.get( GRANT_ID, ) or response.request.payload.get(GRANT_ID) cbsd = session.query(DBCbsd).filter(DBCbsd.cbsd_id == cbsd_id).scalar() grant = None if grant_id: logger.info(f'Getting grant by: {cbsd_id=} {grant_id=}') grant = session.query(DBGrant).filter( DBGrant.cbsd_id == cbsd.id, DBGrant.grant_id == grant_id, ).scalar() if grant_id and not grant: grant_idle_state = obj.grant_states_map[GrantStates.IDLE.value] grant = DBGrant(cbsd=cbsd, grant_id=grant_id, state=grant_idle_state) _update_grant_from_request(response, grant) session.add(grant) logger.info(f'Created new grant: {grant}') return grant
def _build_grant(cbsd: DBCbsd, state_id: str, channel: DBChannel = None) -> DBGrant: return DBGrant( cbsd=cbsd, channel=channel, grant_id="some_grant_id", state_id=state_id, )
def _build_grant(cbsd: DBCbsd, state_id: str) -> DBGrant: return DBGrant( cbsd=cbsd, grant_id="some_grant_id", state_id=state_id, low_frequency=3550_000_000, high_frequency=3570_000_000, max_eirp=20, )
def _create_grant(self, grant_id, channel, cbsd, state): grant = DBGrant( channel=channel, cbsd=cbsd, state=state, grant_id=grant_id, ) self.session.add(grant) self.session.commit() return grant
def test_preexisting_grant_state_after_response( self, request_type_name, response_code, response_data, expected_grants_states, ): # Given requests_fixtures = [_fake_requests_map[request_type_name][0]] db_requests = self._create_db_requests(request_type_name, requests_fixtures) grant_id = db_requests[0].payload.get( 'grantId') or "test_grant_id_for_1" grant = DBGrant( cbsd=db_requests[0].cbsd, state_id=1, grant_id=grant_id, low_frequency=3560000000, high_frequency=3580000000, max_eirp=15, ) self.session.add(grant) self.session.commit() response = self._prepare_response_from_db_requests( db_requests=db_requests, response_code=response_code, response_data=response_data, ) # When self._process_response( request_type_name=request_type_name, response=response, db_requests=db_requests, ) # Then self.assertListEqual( expected_grants_states, [ g.state.name for g in self.session.query(DBGrant).order_by( DBGrant.id).all() ], ) self.assertEqual(self.session.query(DBCbsd).count(), 1)
def with_grant( self, grant_id: str, state_id: int, hb_interval_sec: int, last_hb_timestamp: int = None, ) -> DBCbsdBuilder: last_hb_time = datetime.fromtimestamp( last_hb_timestamp, ) if last_hb_timestamp else None grant = DBGrant( grant_id=grant_id, state_id=state_id, heartbeat_interval=hb_interval_sec, last_heartbeat_request_time=last_hb_time, ) self.cbsd.grants.append(grant) return self
def _create_grant_from_response( response: DBResponse, state: DBGrantState, session: Session, grant_id: str = None, ) -> Optional[DBGrant]: grant_id = grant_id or response.grant_id if not grant_id: return None cbsd_id = session.query( DBCbsd.id).filter(DBCbsd.cbsd_id == response.cbsd_id) grant = DBGrant(cbsd_id=cbsd_id.subquery(), grant_id=grant_id, state=state) _update_grant_from_request(response, grant) session.add(grant) logger.info(f'Created new grant {grant}') return grant