grpc: added session options to session wrapped object, updated get_session and start_session to utilize this change, updated pygui to leverage as well

This commit is contained in:
Blake Harnden 2021-04-22 16:12:33 -07:00
parent 597834a993
commit 7938379e6d
7 changed files with 42 additions and 21 deletions

View file

@ -314,6 +314,7 @@ class CoreGrpcClient:
config=service_config.config,
)
config_service_configs.append(config_service_config)
options = {k: v.value for k, v in session.options.items()}
request = core_pb2.StartSessionRequest(
session_id=session.id,
nodes=nodes,
@ -328,6 +329,7 @@ class CoreGrpcClient:
service_file_configs=service_file_configs,
asymmetric_links=asymmetric_links,
config_service_configs=config_service_configs,
options=options,
)
response = self.stub.StartSession(request)
return response.result, list(response.exceptions)

View file

@ -226,6 +226,11 @@ class CoreGrpcServer(core_pb2_grpc.CoreApiServicer):
session.directory.mkdir(exist_ok=True)
session.set_state(EventTypes.CONFIGURATION_STATE)
# session options
session.options.config_reset()
for key, value in request.options.items():
session.options.set_config(key, value)
# location
if request.HasField("location"):
grpcutils.session_location(session, request.location)
@ -571,6 +576,7 @@ class CoreGrpcServer(core_pb2_grpc.CoreApiServicer):
service_configs = grpcutils.get_node_service_configs(session)
config_service_configs = grpcutils.get_node_config_service_configs(session)
session_file = str(session.file_path) if session.file_path else None
options = get_config_options(session.options.get_configs(), session.options)
session_proto = core_pb2.Session(
id=session.id,
state=session.state.value,
@ -590,6 +596,7 @@ class CoreGrpcServer(core_pb2_grpc.CoreApiServicer):
mobility_configs=mobility_configs,
metadata=session.metadata,
file=session_file,
options=options,
)
return core_pb2.GetSessionResponse(session=session_proto)

View file

@ -753,6 +753,7 @@ class Session:
emane_config: Dict[str, ConfigOption]
metadata: Dict[str, str]
file: Path
options: Dict[str, ConfigOption]
def set_node(self, node: Node) -> None:
self.nodes[node.id] = node
@ -792,6 +793,7 @@ class Session:
node = nodes[node_id]
node.mobility_config = ConfigOption.from_dict(mapped_config.config)
file_path = Path(proto.file) if proto.file else None
options = ConfigOption.from_dict(proto.options)
return Session(
id=proto.id,
state=SessionState(proto.state),
@ -806,6 +808,7 @@ class Session:
emane_config=ConfigOption.from_dict(proto.emane_config),
metadata=dict(proto.metadata),
file=file_path,
options=options,
)

View file

@ -112,3 +112,13 @@ class SessionConfig(ConfigurableManager, ConfigurableOptions):
if value is not None:
value = int(value)
return value
def config_reset(self, node_id: int = None) -> None:
"""
Clear prior configuration files and reset to default values.
:param node_id: node id to store configuration for
:return: nothing
"""
super().config_reset(node_id)
self.set_configs(self.default_values())

View file

@ -1,11 +1,8 @@
import logging
import tkinter as tk
from tkinter import ttk
from typing import TYPE_CHECKING, Dict, Optional
from typing import TYPE_CHECKING, Optional
import grpc
from core.api.grpc.wrappers import ConfigOption
from core.gui.dialogs.dialog import Dialog
from core.gui.themes import PADX, PADY
from core.gui.widgets import ConfigFrame
@ -21,24 +18,15 @@ class SessionOptionsDialog(Dialog):
super().__init__(app, "Session Options")
self.config_frame: Optional[ConfigFrame] = None
self.has_error: bool = False
self.config: Dict[str, ConfigOption] = self.get_config()
self.enabled: bool = not self.app.core.is_runtime()
if not self.has_error:
self.draw()
def get_config(self) -> Dict[str, ConfigOption]:
try:
session_id = self.app.core.session.id
return self.app.core.client.get_session_options(session_id)
except grpc.RpcError as e:
self.app.show_grpc_exception("Get Session Options Error", e)
self.has_error = True
self.destroy()
def draw(self) -> None:
self.top.columnconfigure(0, weight=1)
self.top.rowconfigure(0, weight=1)
self.config_frame = ConfigFrame(self.top, self.app, self.config, self.enabled)
options = self.app.core.session.options
self.config_frame = ConfigFrame(self.top, self.app, options, self.enabled)
self.config_frame.draw_config()
self.config_frame.grid(sticky=tk.NSEW, pady=PADY)
@ -54,10 +42,6 @@ class SessionOptionsDialog(Dialog):
def save(self) -> None:
config = self.config_frame.parse_config()
try:
session_id = self.app.core.session.id
result = self.app.core.client.set_session_options(session_id, config)
logger.info("saved session config: %s", result)
except grpc.RpcError as e:
self.app.show_grpc_exception("Set Session Options Error", e)
for key, value in config.items():
self.app.core.session.options[key].value = value
self.destroy()

View file

@ -188,6 +188,7 @@ message StartSessionRequest {
repeated services.ServiceFileConfig service_file_configs = 11;
repeated Link asymmetric_links = 12;
repeated configservices.ConfigServiceConfig config_service_configs = 13;
map<string, string> options = 14;
}
message StartSessionResponse {
@ -727,6 +728,7 @@ message Session {
map<int32, common.MappedConfig> mobility_configs = 16;
map<string, string> metadata = 17;
string file = 18;
map<string, common.ConfigOption> options = 19;
}
message SessionSummary {

View file

@ -149,6 +149,18 @@ class TestGrpcw:
service_file_data = "echo hello"
node1.service_file_configs[service_name] = {service_file: service_file_data}
# setup session option
option_key = "controlnet"
option_value = "172.16.0.0/24"
option = ConfigOption(
label=option_key,
name=option_key,
value=option_value,
type=ConfigOptionType.STRING,
group="Default",
)
session.options[option_key] = option
# when
with patch.object(CoreXmlWriter, "write"):
with client.context_connect():
@ -189,6 +201,7 @@ class TestGrpcw:
real_node1, service_name, service_file
)
assert service_file.data == service_file_data
assert option_value == real_session.options.get_config(option_key)
@pytest.mark.parametrize("session_id", [None, 6013])
def test_create_session(