Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(dp): Unregister CBSD on SAS response 105 #12360

Merged
merged 1 commit into from
Mar 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,24 +52,32 @@ def process_registration_response(obj: ResponseDBProcessor, response: DBResponse

cbsd_id = response.payload.get("cbsdId", None)
if response.response_code == ResponseCodes.DEREGISTER.value:
_terminate_all_grants_from_response(response, session)
_unregister_cbsd(response, session)
elif response.response_code == ResponseCodes.SUCCESS.value and cbsd_id:
payload = response.request.payload
cbsd = _find_cbsd_from_registration_request(session, payload)
cbsd = _find_cbsd_from_request(session, payload)
cbsd.cbsd_id = cbsd_id
_change_cbsd_state(cbsd, session, CbsdStates.REGISTERED.value)


def _find_cbsd_from_registration_request(session: Session, payload: Dict) -> DBCbsd:
return session.query(DBCbsd).filter(
DBCbsd.cbsd_serial_number == payload["cbsdSerialNumber"],
).scalar()
def _find_cbsd_from_request(session: Session, payload: Dict) -> DBCbsd:
if "cbsdSerialNumber" in payload:
return session.query(DBCbsd).filter(
DBCbsd.cbsd_serial_number == payload["cbsdSerialNumber"],
).scalar()
if "cbsdId" in payload:
return session.query(DBCbsd).filter(
DBCbsd.cbsd_id == payload["cbsdId"],
).scalar()


def _change_cbsd_state(cbsd: DBCbsd, session: Session, new_state: str) -> None:
if not cbsd:
return
state = session.query(DBCbsdState).filter(
DBCbsdState.name == new_state,
).scalar()
print(f"Changing {cbsd=} {cbsd.state=} to {new_state=}")
cbsd.state = state


Expand All @@ -84,7 +92,7 @@ def process_spectrum_inquiry_response(obj: ResponseDBProcessor, response: DBResp
"""

if response.response_code == ResponseCodes.DEREGISTER.value:
_terminate_all_grants_from_response(response, session)
_unregister_cbsd(response, session)
elif response.response_code == ResponseCodes.SUCCESS.value:
_create_channels(response, session)

Expand Down Expand Up @@ -125,6 +133,11 @@ def process_grant_response(obj: ResponseDBProcessor, response: DBResponse, sessi
Returns:
None
"""

if response.response_code == ResponseCodes.DEREGISTER.value:
_unregister_cbsd(response, session)
return

if response.response_code != ResponseCodes.SUCCESS.value:
cbsd = response.request.cbsd
if cbsd:
Expand Down Expand Up @@ -162,6 +175,10 @@ def process_heartbeat_response(obj: ResponseDBProcessor, response: DBResponse, s
None
"""

if response.response_code == ResponseCodes.DEREGISTER.value:
_unregister_cbsd(response, session)
return

grant = _get_or_create_grant_from_response(obj, response, session)
if not grant:
return
Expand Down Expand Up @@ -200,6 +217,10 @@ def process_relinquishment_response(obj: ResponseDBProcessor, response: DBRespon
None
"""

if response.response_code == ResponseCodes.DEREGISTER.value:
_unregister_cbsd(response, session)
return

grant = _get_or_create_grant_from_response(obj, response, session)
if not grant:
return
Expand Down Expand Up @@ -228,11 +249,8 @@ def process_deregistration_response(obj: ResponseDBProcessor, response: DBRespon
session: Database session
"""

_terminate_all_grants_from_response(response, session)
cbsd_id = response.payload.get("cbsdId", None)
if cbsd_id:
cbsd = session.query(DBCbsd).filter(DBCbsd.cbsd_id == cbsd_id).scalar()
_change_cbsd_state(cbsd, session, CbsdStates.UNREGISTERED.value)
print(f"Processing response {response.payload}")
_unregister_cbsd(response, session)


def _get_or_create_grant_from_response(
Expand Down Expand Up @@ -316,3 +334,10 @@ def _terminate_all_grants_from_response(response: DBResponse, session: Session)
session.query(DBGrant).filter(DBGrant.cbsd == cbsd).delete()
logger.info(f"Deleting all channels for {cbsd_id=}")
session.query(DBChannel).filter(DBChannel.cbsd == cbsd).delete()


def _unregister_cbsd(response: DBResponse, session: Session) -> None:
payload = response.request.payload
cbsd = _find_cbsd_from_request(session, payload)
_terminate_all_grants_from_response(response, session)
_change_cbsd_state(cbsd, session, CbsdStates.UNREGISTERED.value)
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,36 @@ def test_channel_params_set_on_grant_response(self):
self.assertEqual(high_frequency, grant.high_frequency)
self.assertEqual(max_eirp, grant.max_eirp)

@parameterized.expand([
(REGISTRATION_REQ, registration_requests),
(SPECTRUM_INQ_REQ, spectrum_inquiry_requests),
(GRANT_REQ, grant_requests),
(HEARTBEAT_REQ, heartbeat_requests),
(RELINQUISHMENT_REQ, relinquishment_requests),
(DEREGISTRATION_REQ, deregistration_requests),
])
@responses.activate
def test_cbsd_unregistered_after_105_response_code(self, request_type, request_fixture):
# Given
db_requests = self._create_db_requests(
request_type, request_fixture,
)
response = self._prepare_response_from_db_requests(
db_requests, ResponseCodes.DEREGISTER.value,
)

# When
self._process_response(
request_type_name=request_type, response=response, db_requests=db_requests,
)
states = [req.cbsd.state for req in db_requests]

# Then
[
self.assertTrue(state.name == CbsdStates.UNREGISTERED.value)
for state in states
]

def _process_response(self, request_type_name, response, db_requests):
processor = self._get_response_processor(request_type_name)

Expand Down