Only one transaction can be opened at time, code refactor

parent 0f9592fe
......@@ -12,7 +12,7 @@ from yangson.enumerations import ContentType, ValidationScope
from yangson.exceptions import YangsonException
from yangson.schemanode import SchemaError, SemanticError
from . import usr_state_data_handlers, usr_conf_data_handlers, usr_op_handlers
from . import op_internal, usr_state_data_handlers, usr_conf_data_handlers, usr_op_handlers
from .usr_datastore import UserDatastore
from .rest_server import RestServer
from .config import CONFIG_GLOBAL, CONFIG_NACM, load_config, print_config
......@@ -158,7 +158,8 @@ def main():
usr_state_data_handlers.register_state_handlers(datastore)
# Register handlers for operations
usr_op_handlers.register_op_handlers()
op_internal.register_op_handlers(datastore)
usr_op_handlers.register_op_handlers(datastore)
# Create HTTP server
rest_srv = RestServer()
......
This diff is collapsed.
from .config import CONFIG
from .helpers import JsonNodeT
from .handler_list import OP_HANDLERS
from .data import BaseDatastore, RpcInfo
class OpHandlersContainer:
def __init__(self, ds: BaseDatastore):
self.ds = ds
def jetconf_conf_start(self, rpc: RpcInfo) -> JsonNodeT:
transaction_opts = rpc.op_input_args.get("options")
self.ds.make_user_journal(rpc.username, transaction_opts)
ret_data = {"status": "OK"}
return ret_data
def jetconf_conf_status(self, rpc: RpcInfo) -> JsonNodeT:
usr_journal = self.ds.get_user_journal(rpc.username)
transaction_opened = True if (usr_journal is not None) else False
ret_data = {
"status": "OK",
"transaction-opened": transaction_opened
}
return ret_data
def jetconf_conf_drop(self, rpc: RpcInfo) -> JsonNodeT:
self.ds.drop_user_journal(rpc.username)
ret_data = {"status": "OK"}
return ret_data
def jetconf_conf_commit(self, rpc: RpcInfo) -> JsonNodeT:
usr_journal = self.ds.get_user_journal(rpc.username)
try:
self.ds.lock_data(rpc.username)
commit_res = usr_journal.commit(self.ds)
if CONFIG["GLOBAL"]["PERSISTENT_CHANGES"] is True:
self.ds.save()
finally:
self.ds.unlock_data()
self.ds.drop_user_journal(rpc.username)
ret_data = {
"status": "OK",
"conf-changed": commit_res
}
return ret_data
def jetconf_get_schema_digest(self, rpc: RpcInfo) -> JsonNodeT:
ret_data = self.ds.get_dm().schema_digest()
return ret_data
def jetconf_get_list_length(self, rpc: RpcInfo) -> JsonNodeT:
try:
list_url = rpc.op_input_args["url"] # type: str
except (TypeError, KeyError):
raise ValueError("This operation expects \"url\" input parameter")
try:
staging = rpc.op_input_args["staging"] # type: str
except (TypeError, KeyError):
staging = False
rpc_gll = RpcInfo()
rpc_gll.username = rpc.username
rpc_gll.skip_nacm_check = rpc.skip_nacm_check
rpc_gll.path = list_url.rstrip("/")
rpc_gll.qs = {}
ln_val = self.ds.get_node_rpc(rpc_gll, staging).value
if isinstance(ln_val, list):
ret_data = {"jetconf:list-length": len(ln_val)}
else:
raise ValueError("Passed URI does not point to List")
return ret_data
def register_op_handlers(ds: BaseDatastore):
op_handlers_obj = OpHandlersContainer(ds)
OP_HANDLERS.register(op_handlers_obj.jetconf_conf_start, "jetconf:conf-start")
OP_HANDLERS.register(op_handlers_obj.jetconf_conf_status, "jetconf:conf-status")
OP_HANDLERS.register(op_handlers_obj.jetconf_conf_drop, "jetconf:conf-drop")
OP_HANDLERS.register(op_handlers_obj.jetconf_conf_commit, "jetconf:conf-commit")
OP_HANDLERS.register(op_handlers_obj.jetconf_get_schema_digest, "jetconf:get-schema-digest")
OP_HANDLERS.register(op_handlers_obj.jetconf_get_list_length, "jetconf:get-list-length")
......@@ -2,13 +2,14 @@ from colorlog import info
from .helpers import JsonNodeT
from .handler_list import OP_HANDLERS
from .data import BaseDatastore
# ---------- User-defined handlers follow ----------
class OpHandlersContainer:
def __init__(self):
pass
def __init__(self, ds: BaseDatastore):
self.ds = ds
def jukebox_play_op(self, input_args: JsonNodeT, username: str) -> JsonNodeT:
# Structure of RPC's input and output arguments is defined in YANG data model
......@@ -18,8 +19,6 @@ class OpHandlersContainer:
info("Song number: {}".format(input_args["example-jukebox:song-number"]))
OP_HANDLERS_IMPL = OpHandlersContainer()
def register_op_handlers():
OP_HANDLERS.register(OP_HANDLERS_IMPL.jukebox_play_op, "example-jukebox:play")
def register_op_handlers(ds: BaseDatastore):
op_handlers_obj = OpHandlersContainer(ds)
OP_HANDLERS.register(op_handlers_obj.jukebox_play_op, "example-jukebox:play")
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment