-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
xlate/openconfig: add cache datastore option
This change adds a runtime option for the OpenConfig translator to use a cache datastore. It may improve the response speed to retrieve operational states of OpenConfig models. However, it also introduces differences between the operational states and actual states of the device as a side effect.
- Loading branch information
1 parent
2fe8417
commit 3666d39
Showing
14 changed files
with
1,802 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
"""Cache datastore for OpenConfig translators.""" | ||
|
||
|
||
from abc import abstractmethod | ||
import logging | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class CacheDataNotExistError(Exception): | ||
pass | ||
|
||
|
||
class Cache: | ||
"""Base for cache datastore. | ||
It is an abstract class. You should implement a subclass for each datastore type. | ||
""" | ||
|
||
@abstractmethod | ||
def get(self, module): | ||
"""Get operational state data of a module from the cache datastore. | ||
Args: | ||
module (str): Module name. | ||
Returns: | ||
dict: Operational state data of a module. | ||
Raises: | ||
CacheDataNotExistError: Data for the module does not exist in the cache datastore. | ||
""" | ||
pass | ||
|
||
@abstractmethod | ||
def set(self, module, data): | ||
"""Set operational state data of an OpenConfig model to the cache datastore. | ||
Args: | ||
module (str): Module name. | ||
data (dict): Operational state data of a model. | ||
""" | ||
pass | ||
|
||
|
||
class InMemoryCache(Cache): | ||
"""In-memory cache datastore. | ||
Attribute: | ||
_data (dict): Operational state data for modules. A key is a module name. | ||
""" | ||
|
||
def __init__(self): | ||
self._data = {} | ||
|
||
def get(self, module): | ||
try: | ||
return self._data[module] | ||
except KeyError as e: | ||
logger.error("%s is not cached.", module) | ||
raise CacheDataNotExistError( | ||
f"Cache data for {module} does not exist in the cache datastore" | ||
) from e | ||
|
||
def set(self, module, data): | ||
self._data[module] = data |
88 changes: 88 additions & 0 deletions
88
src/xlate/openconfig/goldstone/xlate/openconfig/cache_updater.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
"""Cache data updater for OpenConfig translators.""" | ||
|
||
import asyncio | ||
import logging | ||
from .interfaces import InterfacesObjectTree | ||
from .platform import PlatformObjectTree | ||
from .terminal_device import TerminalDeviceObjectTree | ||
from goldstone.lib.errors import Error as ConnectorError | ||
from goldstone.lib.connector.sysrepo import Connector | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
DEFAULT_UPDATE_INTERVAL = 5 | ||
|
||
|
||
class CacheUpdater: | ||
"""Cache data updater for OpenConfig translators. | ||
Args: | ||
cache (Cache): Cache datastore instance to use. | ||
operational_modes (dict): Supported operational-modes. | ||
update_interval (int): Interval seconds between executions of the update task. | ||
""" | ||
|
||
def __init__( | ||
self, cache, operational_modes, update_interval=DEFAULT_UPDATE_INTERVAL | ||
): | ||
self._cache = cache | ||
self._operational_modes = operational_modes | ||
self._update_interval = update_interval | ||
self._connector = Connector() | ||
self._required_data = [] | ||
self._object_trees = { | ||
"openconfig-interfaces": InterfacesObjectTree(), | ||
"openconfig-platform": PlatformObjectTree(self._operational_modes), | ||
"openconfig-terminal-device": TerminalDeviceObjectTree( | ||
self._operational_modes | ||
), | ||
} | ||
for _, object_tree in self._object_trees.items(): | ||
for data in object_tree.required_data(): | ||
if not data in self._required_data: | ||
self._required_data.append(data) | ||
|
||
def _get_gs(self): | ||
"""Get operational state data of Goldstone primitive models from the central datastore. | ||
Returns: | ||
dict: Operational state data of Goldstone primitive models. | ||
""" | ||
gs = {} | ||
for d in self._required_data: | ||
try: | ||
gs[d["name"]] = self._connector.get_operational( | ||
d["xpath"], d["default"] | ||
) | ||
except ConnectorError as e: | ||
logger.error("Failed to get source data from %s. %s", d["name"], e) | ||
return gs | ||
|
||
async def _update(self): | ||
"""Update cache datastore.""" | ||
gs = self._get_gs() | ||
for module, object_tree in self._object_trees.items(): | ||
try: | ||
self._cache.set(module, object_tree.create(gs)) | ||
except Exception as e: | ||
logger.error("Failed to update cache data for %s. %s", module, e) | ||
|
||
async def _update_loop(self): | ||
"""Update task coroutine.""" | ||
while True: | ||
await asyncio.sleep(self._update_interval) | ||
await self._update() | ||
|
||
async def start(self): | ||
"""Start a service. | ||
Returns: | ||
list: List of coroutine objects. | ||
""" | ||
return [self._update_loop()] | ||
|
||
async def stop(self): | ||
"""Stop a service.""" | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.