added hook creation and set location to grpc.StartSession
This commit is contained in:
parent
4e03dc6888
commit
de936ea315
5 changed files with 119 additions and 29 deletions
|
@ -148,17 +148,23 @@ class CoreGrpcClient:
|
|||
self.stub = None
|
||||
self.channel = None
|
||||
|
||||
def start_session(self, session_id, nodes, links):
|
||||
def start_session(self, session_id, nodes, links, location=None, hooks=None):
|
||||
"""
|
||||
Start a session.
|
||||
|
||||
:param int session_id: id of session
|
||||
:param list nodes: list of nodes to create
|
||||
:param list links: list of links to create
|
||||
:param core_pb2.SessionLocation location: location to set
|
||||
:param list[core_pb2.Hook] hooks: session hooks to set
|
||||
:return:
|
||||
"""
|
||||
request = core_pb2.StartSessionRequest(
|
||||
session_id=session_id, nodes=nodes, links=links
|
||||
session_id=session_id,
|
||||
nodes=nodes,
|
||||
links=links,
|
||||
location=location,
|
||||
hooks=hooks,
|
||||
)
|
||||
return self.stub.StartSession(request)
|
||||
|
||||
|
@ -282,9 +288,11 @@ class CoreGrpcClient:
|
|||
:rtype: core_pb2.SetSessionLocationResponse
|
||||
:raises grpc.RpcError: when session doesn't exist
|
||||
"""
|
||||
position = core_pb2.SessionPosition(x=x, y=y, z=z, lat=lat, lon=lon, alt=alt)
|
||||
location = core_pb2.SessionLocation(
|
||||
x=x, y=y, z=z, lat=lat, lon=lon, alt=alt, scale=scale
|
||||
)
|
||||
request = core_pb2.SetSessionLocationRequest(
|
||||
session_id=session_id, position=position, scale=scale
|
||||
session_id=session_id, location=location
|
||||
)
|
||||
return self.stub.SetSessionLocation(request)
|
||||
|
||||
|
|
|
@ -306,3 +306,16 @@ def get_net_stats():
|
|||
stats[line[0]] = {"rx": float(line[1]), "tx": float(line[9])}
|
||||
|
||||
return stats
|
||||
|
||||
|
||||
def session_location(session, location):
|
||||
"""
|
||||
Set session location based on location proto.
|
||||
|
||||
:param core.emulator.session.Session session: session for location
|
||||
:param core_pb2.SessionLocation location: location to set
|
||||
:return: nothing
|
||||
"""
|
||||
session.location.refxyz = (location.x, location.y, location.z)
|
||||
session.location.setrefgeo(location.lat, location.lon, location.alt)
|
||||
session.location.refscale = location.scale
|
||||
|
|
|
@ -122,9 +122,21 @@ class CoreGrpcServer(core_pb2_grpc.CoreApiServicer):
|
|||
if not os.path.exists(session.session_dir):
|
||||
os.mkdir(session.session_dir)
|
||||
|
||||
# location
|
||||
if request.HasField("location"):
|
||||
grpcutils.session_location(session, request.location)
|
||||
|
||||
# add all hooks
|
||||
for hook in request.hooks:
|
||||
session.add_hook(hook.state, hook.file, None, hook.data)
|
||||
|
||||
# create nodes
|
||||
grpcutils.create_nodes(session, request.nodes)
|
||||
|
||||
# emane configs
|
||||
# wlan configs
|
||||
# mobility configs
|
||||
|
||||
# create links
|
||||
grpcutils.create_links(session, request.links)
|
||||
|
||||
|
@ -217,10 +229,11 @@ class CoreGrpcServer(core_pb2_grpc.CoreApiServicer):
|
|||
session = self.get_session(request.session_id, context)
|
||||
x, y, z = session.location.refxyz
|
||||
lat, lon, alt = session.location.refgeo
|
||||
position = core_pb2.SessionPosition(x=x, y=y, z=z, lat=lat, lon=lon, alt=alt)
|
||||
return core_pb2.GetSessionLocationResponse(
|
||||
position=position, scale=session.location.refscale
|
||||
scale = session.location.refscale
|
||||
location = core_pb2.SessionLocation(
|
||||
x=x, y=y, z=z, lat=lat, lon=lon, alt=alt, scale=scale
|
||||
)
|
||||
return core_pb2.GetSessionLocationResponse(location=location)
|
||||
|
||||
def SetSessionLocation(self, request, context):
|
||||
"""
|
||||
|
@ -233,15 +246,7 @@ class CoreGrpcServer(core_pb2_grpc.CoreApiServicer):
|
|||
"""
|
||||
logging.debug("set session location: %s", request)
|
||||
session = self.get_session(request.session_id, context)
|
||||
session.location.refxyz = (
|
||||
request.position.x,
|
||||
request.position.y,
|
||||
request.position.z,
|
||||
)
|
||||
session.location.setrefgeo(
|
||||
request.position.lat, request.position.lon, request.position.alt
|
||||
)
|
||||
session.location.refscale = request.scale
|
||||
grpcutils.session_location(session, request.location)
|
||||
return core_pb2.SetSessionLocationResponse(result=True)
|
||||
|
||||
def SetSessionState(self, request, context):
|
||||
|
|
|
@ -134,6 +134,8 @@ message StartSessionRequest {
|
|||
int32 session_id = 1;
|
||||
repeated Node nodes = 2;
|
||||
repeated Link links = 3;
|
||||
repeated Hook hooks = 4;
|
||||
SessionLocation location = 5;
|
||||
}
|
||||
|
||||
message StartSessionResponse {
|
||||
|
@ -202,14 +204,12 @@ message GetSessionLocationRequest {
|
|||
}
|
||||
|
||||
message GetSessionLocationResponse {
|
||||
SessionPosition position = 1;
|
||||
float scale = 2;
|
||||
SessionLocation location = 1;
|
||||
}
|
||||
|
||||
message SetSessionLocationRequest {
|
||||
int32 session_id = 1;
|
||||
SessionPosition position = 2;
|
||||
float scale = 3;
|
||||
SessionLocation location = 2;
|
||||
}
|
||||
|
||||
message SetSessionLocationResponse {
|
||||
|
@ -872,13 +872,14 @@ message Interface {
|
|||
int32 mtu = 10;
|
||||
}
|
||||
|
||||
message SessionPosition {
|
||||
message SessionLocation {
|
||||
float x = 1;
|
||||
float y = 2;
|
||||
float z = 3;
|
||||
float lat = 4;
|
||||
float lon = 5;
|
||||
float alt = 6;
|
||||
float scale = 7;
|
||||
}
|
||||
|
||||
message Position {
|
||||
|
|
|
@ -3,9 +3,10 @@ from queue import Queue
|
|||
|
||||
import grpc
|
||||
import pytest
|
||||
from mock import patch
|
||||
|
||||
from core.api.grpc import core_pb2
|
||||
from core.api.grpc.client import CoreGrpcClient
|
||||
from core.api.grpc.client import CoreGrpcClient, InterfaceHelper
|
||||
from core.config import ConfigShim
|
||||
from core.emane.ieee80211abg import EmaneIeee80211abgModel
|
||||
from core.emulator.data import EventData
|
||||
|
@ -18,9 +19,71 @@ from core.emulator.enumerations import (
|
|||
)
|
||||
from core.errors import CoreError
|
||||
from core.location.mobility import BasicRangeModel, Ns2ScriptedMobility
|
||||
from core.xml.corexml import CoreXmlWriter
|
||||
|
||||
|
||||
class TestGrpc:
|
||||
def test_start_session(self, grpc_server):
|
||||
# given
|
||||
client = CoreGrpcClient()
|
||||
session = grpc_server.coreemu.create_session()
|
||||
nodes = []
|
||||
position = core_pb2.Position(x=50, y=100)
|
||||
node_one = core_pb2.Node(id=1, position=position, model="PC")
|
||||
position = core_pb2.Position(x=100, y=100)
|
||||
node_two = core_pb2.Node(id=2, position=position, model="PC")
|
||||
nodes.extend([node_one, node_two])
|
||||
links = []
|
||||
interface_helper = InterfaceHelper(ip4_prefix="10.83.0.0/16")
|
||||
interface_one = interface_helper.create_interface(node_one.id, 0)
|
||||
interface_two = interface_helper.create_interface(node_two.id, 0)
|
||||
link = core_pb2.Link(
|
||||
type=core_pb2.LinkType.WIRED,
|
||||
node_one_id=node_one.id,
|
||||
node_two_id=node_two.id,
|
||||
interface_one=interface_one,
|
||||
interface_two=interface_two,
|
||||
)
|
||||
links.append(link)
|
||||
hooks = []
|
||||
hook = core_pb2.Hook(
|
||||
state=core_pb2.SessionState.RUNTIME, file="echo.sh", data="echo hello"
|
||||
)
|
||||
hooks.append(hook)
|
||||
location_x = 5
|
||||
location_y = 10
|
||||
location_z = 15
|
||||
location_lat = 20
|
||||
location_lon = 30
|
||||
location_alt = 40
|
||||
location_scale = 5
|
||||
location = core_pb2.SessionLocation(
|
||||
x=location_x,
|
||||
y=location_y,
|
||||
z=location_z,
|
||||
lat=location_lat,
|
||||
lon=location_lon,
|
||||
alt=location_alt,
|
||||
scale=location_scale,
|
||||
)
|
||||
|
||||
# when
|
||||
with patch.object(CoreXmlWriter, "write"):
|
||||
with client.context_connect():
|
||||
client.start_session(session.id, nodes, links, location, hooks)
|
||||
|
||||
# then
|
||||
assert node_one.id in session.nodes
|
||||
assert node_two.id in session.nodes
|
||||
assert session.nodes[node_one.id].netif(0) is not None
|
||||
assert session.nodes[node_two.id].netif(0) is not None
|
||||
hook_file, hook_data = session._hooks[core_pb2.SessionState.RUNTIME][0]
|
||||
assert hook_file == hook.file
|
||||
assert hook_data == hook.data
|
||||
assert session.location.refxyz == (location_x, location_y, location_z)
|
||||
assert session.location.refgeo == (location_lat, location_lon, location_alt)
|
||||
assert session.location.refscale == location_scale
|
||||
|
||||
@pytest.mark.parametrize("session_id", [None, 6013])
|
||||
def test_create_session(self, grpc_server, session_id):
|
||||
# given
|
||||
|
@ -112,13 +175,13 @@ class TestGrpc:
|
|||
response = client.get_session_location(session.id)
|
||||
|
||||
# then
|
||||
assert response.scale == 1.0
|
||||
assert response.position.x == 0
|
||||
assert response.position.y == 0
|
||||
assert response.position.z == 0
|
||||
assert response.position.lat == 0
|
||||
assert response.position.lon == 0
|
||||
assert response.position.alt == 0
|
||||
assert response.location.scale == 1.0
|
||||
assert response.location.x == 0
|
||||
assert response.location.y == 0
|
||||
assert response.location.z == 0
|
||||
assert response.location.lat == 0
|
||||
assert response.location.lon == 0
|
||||
assert response.location.alt == 0
|
||||
|
||||
def test_set_session_location(self, grpc_server):
|
||||
# given
|
||||
|
|
Loading…
Add table
Reference in a new issue