diff --git a/control/grpc.py b/control/grpc.py index f4da2460..a4cd29d7 100644 --- a/control/grpc.py +++ b/control/grpc.py @@ -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 @@ -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 @@ -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 @@ -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: diff --git a/control/state.py b/control/state.py index 3f5acc6f..f4e575b4 100644 --- a/control/state.py +++ b/control/state.py @@ -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.""" @@ -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