Skip to content

Commit

Permalink
Do not duplicate the code to construct resource keys.
Browse files Browse the repository at this point in the history
Fixes #275

Signed-off-by: Gil Bregman <[email protected]>
  • Loading branch information
gbregman committed Oct 31, 2023
1 parent 58dfde5 commit 90db78f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 18 deletions.
7 changes: 4 additions & 3 deletions control/grpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from .proto import gateway_pb2_grpc as pb2_grpc
from .config import GatewayConfig
from .discovery import DiscoveryService
from .state import GatewayState

MAX_ANA_GROUPS = 4

Expand Down Expand Up @@ -426,7 +427,7 @@ def remove_namespace(self, request, context=None):
def matching_host_exists(self, context, subsys_nqn, host_nqn) -> bool:
if not context:
return False
host_key = "_".join([self.gateway_state.local.HOST_PREFIX + subsys_nqn, host_nqn])
host_key = GatewayState.build_host_key(subsys_nqn, host_nqn)
state = self.gateway_state.local.get_state()
if state.get(host_key):
return True
Expand Down Expand Up @@ -559,7 +560,7 @@ def remove_host(self, request, context=None):
def matching_listener_exists(self, context, nqn, gw_name, trtype, traddr, trsvcid) -> bool:
if not context:
return False
listener_key = "_".join([self.gateway_state.local.LISTENER_PREFIX + nqn, gw_name, trtype, traddr, trsvcid])
listener_key = GatewayState.build_listener_key(nqn, gw_name, trtype, traddr, trsvcid)
state = self.gateway_state.local.get_state()
if state.get(listener_key):
return True
Expand Down Expand Up @@ -614,7 +615,7 @@ def create_listener_safe(self, request, context=None):

state = self.gateway_state.local.get_state()
req = None
subsys = state.get(self.gateway_state.local.SUBSYSTEM_PREFIX + request.nqn)
subsys = state.get(GatewayState.build_subsystem_key(request.nqn))
if subsys:
self.logger.debug(f"value of sub-system: {subsys}")
try:
Expand Down
52 changes: 37 additions & 15 deletions control/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,30 @@ class GatewayState(ABC):
HOST_PREFIX = "host_"
LISTENER_PREFIX = "listener_"

def build_bdev_key(bdev_name: str) -> str:
return GatewayState.BDEV_PREFIX + bdev_name

def build_namespace_key(subsystem_nqn: str, nsid) -> str:
key = GatewayState.NAMESPACE_PREFIX + subsystem_nqn
if nsid is not None:
key = key + "_" + nsid
return key

def build_subsystem_key(subsystem_nqn: str) -> str:
return GatewayState.SUBSYSTEM_PREFIX + subsystem_nqn

def build_host_key(subsystem_nqn: str, host_nqn) -> str:
key = GatewayState.HOST_PREFIX + subsystem_nqn
if host_nqn is not None:
key = key + "_" + host_nqn
return key

def build_partial_listener_key(subsystem_nqn: str) -> str:
return GatewayState.LISTENER_PREFIX + subsystem_nqn

def build_listener_key(subsystem_nqn: str, gateway: str, trtype: str, traddr: str, trsvcid: str) -> str:
return GatewayState.build_partial_listener_key(subsystem_nqn) + "_" + gateway + "_" + trtype + "_" + traddr + "_" + trsvcid

@abstractmethod
def get_state(self) -> Dict[str, str]:
"""Returns the state dictionary."""
Expand All @@ -46,64 +70,62 @@ def _remove_key(self, key: str):

def add_bdev(self, bdev_name: str, val: str):
"""Adds a bdev to the state data store."""
key = self.BDEV_PREFIX + bdev_name
key = GatewayState.build_bdev_key(bdev_name)
self._add_key(key, val)

def remove_bdev(self, bdev_name: str):
"""Removes a bdev from the state data store."""
key = self.BDEV_PREFIX + bdev_name
key = GatewayState.build_bdev_key(bdev_name)
self._remove_key(key)

def add_namespace(self, subsystem_nqn: str, nsid: str, val: str):
"""Adds a namespace to the state data store."""
key = self.NAMESPACE_PREFIX + subsystem_nqn + "_" + nsid
key = GatewayState.build_namespace_key(subsystem_nqn, nsid)
self._add_key(key, val)

def remove_namespace(self, subsystem_nqn: str, nsid: str):
"""Removes a namespace from the state data store."""
key = self.NAMESPACE_PREFIX + subsystem_nqn + "_" + nsid
key = GatewayState.build_namespace_key(subsystem_nqn, nsid)
self._remove_key(key)

def add_subsystem(self, subsystem_nqn: str, val: str):
"""Adds a subsystem to the state data store."""
key = self.SUBSYSTEM_PREFIX + subsystem_nqn
key = GatewayState.build_subsystem_key(subsystem_nqn)
self._add_key(key, val)

def remove_subsystem(self, subsystem_nqn: str):
"""Removes a subsystem from the state data store."""
key = self.SUBSYSTEM_PREFIX + subsystem_nqn
key = GatewayState.build_subsystem_key(subsystem_nqn)
self._remove_key(key)

# Delete all keys related to subsystem
state = self.get_state()
for key in state.keys():
if (key.startswith(self.NAMESPACE_PREFIX + subsystem_nqn) or
key.startswith(self.HOST_PREFIX + subsystem_nqn) or
key.startswith(self.LISTENER_PREFIX + subsystem_nqn)):
if (key.startswith(GatewayState.build_namespace_key(subsystem_nqn, None)) or
key.startswith(GatewayState.build_host_key(subsystem_nqn, None)) or
key.startswith(GatewayState.build_partial_listener_key(subsystem_nqn))):
self._remove_key(key)

def add_host(self, subsystem_nqn: str, host_nqn: str, val: str):
"""Adds a host to the state data store."""
key = "{}{}_{}".format(self.HOST_PREFIX, subsystem_nqn, host_nqn)
key = GatewayState.build_host_key(subsystem_nqn, host_nqn)
self._add_key(key, val)

def remove_host(self, subsystem_nqn: str, host_nqn: str):
"""Removes a host from the state data store."""
key = "{}{}_{}".format(self.HOST_PREFIX, subsystem_nqn, host_nqn)
key = GatewayState.build_host_key(subsystem_nqn, host_nqn)
self._remove_key(key)

def add_listener(self, subsystem_nqn: str, gateway: str, trtype: str,
traddr: str, trsvcid: str, val: str):
"""Adds a listener to the state data store."""
key = "{}{}_{}_{}_{}_{}".format(self.LISTENER_PREFIX, subsystem_nqn,
gateway, trtype, traddr, trsvcid)
key = GatewayState.build_listener_key(subsystem_nqn, gateway, trtype, traddr, trsvcid)
self._add_key(key, val)

def remove_listener(self, subsystem_nqn: str, gateway: str, trtype: str,
traddr: str, trsvcid: str):
"""Removes a listener from the state data store."""
key = "{}{}_{}_{}_{}_{}".format(self.LISTENER_PREFIX, subsystem_nqn,
gateway, trtype, traddr, trsvcid)
key = GatewayState.build_listener_key(subsystem_nqn, gateway, trtype, traddr, trsvcid)
self._remove_key(key)

@abstractmethod
Expand Down

0 comments on commit 90db78f

Please sign in to comment.