Skip to content

Commit

Permalink
fix(dp): Unregister CBSD on SAS response 105 (#12360)
Browse files Browse the repository at this point in the history
Fixed an issue where a CBSD state remained "registered" when SAS
returned a response with responseCode 105.
Response code 105 means the CBSD got unregistered in SAS and should
consider itself "Unregistered".
This PR changes the CBSD state to "unregistered" when any SAS response
is a 105 code.

Signed-off-by: Artur Dębski <artur.debski@freedomfi.com>

Co-authored-by: Artur Dębski <artur.debski@freedomfi.com>
  • Loading branch information
2 people authored and berezovskyi-oleksandr committed Mar 31, 2022
1 parent 4bcfd82 commit afbca3d
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 12 deletions.
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,9 @@ def process_grant_response(obj: ResponseDBProcessor, response: DBResponse, sessi
Returns:
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:
Expand Down Expand Up @@ -174,6 +185,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 @@ -213,6 +228,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 @@ -242,11 +261,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 _reset_last_used_max_eirp(grant: DBGrant) -> None:
Expand Down Expand Up @@ -323,3 +339,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 @@ -364,6 +364,36 @@ def test_assign_channel_to_grant_on_grant_response(self):
).count()
self.assertEqual(1, count)

@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

0 comments on commit afbca3d

Please sign in to comment.