added grpc create/edit node calls

This commit is contained in:
bharnden 2019-02-25 22:45:57 -08:00
parent ad7522b5bc
commit 6016152490
3 changed files with 154 additions and 2 deletions

View file

@ -6,6 +6,8 @@ import grpc
import core_pb2
import core_pb2_grpc
from core.emulator.emudata import NodeOptions
from core.enumerations import NodeTypes
def update_proto(obj, **kwargs):
@ -60,6 +62,47 @@ class CoreApiClient(object):
request.state = state
return self.stub.SetSessionState(request)
def create_node(self, session, _type=NodeTypes.DEFAULT, _id=None, node_options=None, emane=None):
if not node_options:
node_options = NodeOptions()
request = core_pb2.CreateNodeRequest()
request.session = session
request.type = _type.value
update_proto(
request,
id=_id,
name=node_options.name,
model=node_options.model,
icon=node_options.icon,
opaque=node_options.opaque,
emane=emane
)
update_proto(
request.position,
x=node_options.x,
y=node_options.y,
lat=node_options.lat,
lon=node_options.lon,
alt=node_options.alt
)
request.services.extend(node_options.services)
return self.stub.CreateNode(request)
def edit_node(self, session, _id, node_options):
request = core_pb2.EditNodeRequest()
request.session = session
request.id = _id
update_proto(
request.position,
x=node_options.x,
y=node_options.y,
lat=node_options.lat,
lon=node_options.lon,
alt=node_options.alt
)
return self.stub.EditNode(request)
@contextmanager
def connect(self):
channel = grpc.insecure_channel(self.address)
@ -101,6 +144,14 @@ def main():
# change session state
print("set session state: %s" % client.set_session_state(session_data.id, core_pb2.INSTANTIATION))
for i in xrange(2):
response = client.create_node(session_data.id)
print("created node: %s" % response)
node_options = NodeOptions()
node_options.x = 5
node_options.y = 5
print("edit node: %s" % client.edit_node(session_data.id, response.id, node_options))
# get session
print("get session: %s" % client.get_session(session_data.id))

View file

@ -1,5 +1,6 @@
import os
from core.emulator.emudata import NodeOptions
from core.enumerations import NodeTypes, EventTypes
from concurrent import futures
@ -198,8 +199,8 @@ class CoreApiServer(core_pb2_grpc.CoreApiServicer):
def GetSession(self, request, context):
session = self.coreemu.sessions.get(request.id)
if not request:
pass
if not session:
raise Exception("no session found")
response = core_pb2.SessionResponse()
response.state = session.state
@ -244,6 +245,58 @@ class CoreApiServer(core_pb2_grpc.CoreApiServicer):
return response
def CreateNode(self, request, context):
session = self.coreemu.sessions.get(request.session)
if not session:
raise Exception("no session found")
node_id = request.id
node_type = request.type
if node_type is None:
node_type = NodeTypes.DEFAULT.value
node_type = NodeTypes(node_type)
logging.info("creating node: %s - %s", node_type.name, request)
node_options = NodeOptions(name=request.name, model=request.model)
node_options.icon = request.icon
node_options.opaque = request.opaque
node_options.services = request.services
position = request.position
node_options.set_position(position.x, position.y)
node_options.set_location(position.lat, position.lon, position.alt)
node = session.add_node(_type=node_type, _id=node_id, node_options=node_options)
# configure emane if provided
emane_model = request.emane
if emane_model:
session.emane.set_model_config(node_id, emane_model)
response = core_pb2.CreateNodeResponse()
response.id = node.objid
return response
def EditNode(self, request, context):
session = self.coreemu.sessions.get(request.session)
if not session:
raise Exception("no session found")
node_id = request.id
node_options = NodeOptions()
x = request.position.x
y = request.position.y
node_options.set_position(x, y)
lat = request.position.lat
lon = request.position.lon
alt = request.position.alt
node_options.set_location(lat, lon, alt)
logging.debug("updating node(%s) - pos(%s, %s) geo(%s, %s, %s)", node_id, x, y, lat, lon, alt)
result = session.update_node(node_id, node_options)
response = core_pb2.EditNodeResponse()
response.result = result
return response
def listen(coreemu, address="[::]:50051"):
logging.info("starting grpc api: %s", address)

View file

@ -19,6 +19,10 @@ service CoreApi {
}
rpc SetSessionState (SetSessionStateRequest) returns (SetSessionStateResponse) {
}
rpc CreateNode (CreateNodeRequest) returns (CreateNodeResponse) {
}
rpc EditNode (EditNodeRequest) returns (EditNodeResponse) {
}
}
// rpc request/response messages
@ -91,6 +95,33 @@ message SetSessionStateResponse {
bool result = 1;
}
message CreateNodeRequest {
int32 session = 1;
int32 id = 2;
NodeType type = 3;
string name = 4;
string model = 5;
string icon = 6;
repeated string services = 7;
Position position = 8;
string emane = 9;
string opaque = 10;
}
message CreateNodeResponse {
int32 id = 1;
}
message EditNodeRequest {
int32 session = 1;
int32 id = 2;
Position position = 3;
}
message EditNodeResponse {
bool result = 1;
}
// data structures for messages below
enum SessionState {
NONE = 0;
@ -102,6 +133,23 @@ enum SessionState {
SHUTDOWN = 6;
}
enum NodeType {
DEFAULT = 0;
PHYSICAL = 1;
TBD = 3;
SWITCH = 4;
HUB = 5;
WIRELESS_LAN = 6;
RJ45 = 7;
TUNNEL = 8;
KTUNNEL = 9;
EMANE = 10;
TAP_BRIDGE = 11;
PEER_TO_PEER = 12;
CONTROL_NET = 13;
EMANE_NET = 14;
}
message ConfigGroup {
string name = 1;
repeated ConfigOption options = 2;