create initial example client script for grpc and separated from within the client library

This commit is contained in:
bharnden 2019-03-30 19:01:11 -07:00
parent 2b14865473
commit 27ea317a57
3 changed files with 107 additions and 100 deletions

View file

@ -5,7 +5,6 @@ gRpc client for interfacing with CORE, when gRPC mode is enabled.
from __future__ import print_function
import logging
import os
import threading
from contextlib import contextmanager
@ -830,102 +829,3 @@ class CoreGrpcClient(object):
yield
finally:
self.close()
def main():
xml_file_name = "/tmp/core.xml"
client = CoreGrpcClient()
with client.context_connect():
if os.path.exists(xml_file_name):
response = client.open_xml(xml_file_name)
print("open xml: {}".format(response))
print("services: {}".format(client.get_services()))
# create session
session_data = client.create_session()
client.exception_events(session_data.id, lambda x: print(x))
client.node_events(session_data.id, lambda x: print(x))
client.session_events(session_data.id, lambda x: print(x))
client.link_events(session_data.id, lambda x: print(x))
client.file_events(session_data.id, lambda x: print(x))
client.config_events(session_data.id, lambda x: print(x))
print("created session: {}".format(session_data))
print("default services: {}".format(client.get_service_defaults(session_data.id)))
print("emane models: {}".format(client.get_emane_models(session_data.id)))
print("add hook: {}".format(client.add_hook(session_data.id, core_pb2.STATE_RUNTIME, "test", "echo hello")))
print("hooks: {}".format(client.get_hooks(session_data.id)))
response = client.get_sessions()
print("core client received: {}".format(response))
print("set emane config: {}".format(client.set_emane_config(session_data.id, {"otamanagerttl": "2"})))
print("emane config: {}".format(client.get_emane_config(session_data.id)))
# set session location
response = client.set_session_location(
session_data.id,
x=0, y=0,
lat=47.57917, lon=-122.13232, alt=3.0,
scale=150000.0
)
print("set location response: {}".format(response))
# get options
print("get options: {}".format(client.get_session_options(session_data.id)))
# get location
print("get location: {}".format(client.get_session_location(session_data.id)))
# change session state
print("set session state: {}".format(client.set_session_state(session_data.id, core_pb2.STATE_CONFIGURATION)))
# create switch node
switch = core_pb2.Node(type=core_pb2.NODE_SWITCH)
response = client.add_node(session_data.id, switch)
print("created switch: {}".format(response))
switch_id = response.id
# ip generator for example
interface_helper = InterfaceHelper(ip4_prefix="10.83.0.0/16")
for _ in xrange(2):
node = core_pb2.Node()
response = client.add_node(session_data.id, node)
print("created node: {}".format(response))
node_id = response.id
position = core_pb2.Position(x=5, y=5)
print("edit node: {}".format(client.edit_node(session_data.id, node_id, position)))
print("get node: {}".format(client.get_node(session_data.id, node_id)))
print("emane model config: {}".format(
client.get_emane_model_config(session_data.id, node_id, "emane_tdma")))
print("node service: {}".format(client.get_node_service(session_data.id, node_id, "zebra")))
# create link
interface_one = interface_helper.create_interface(node_id, 0)
print("created link: {}".format(client.add_link(session_data.id, node_id, switch_id, interface_one)))
link_options = core_pb2.LinkOptions(per=50)
print("edit link: {}".format(client.edit_link(
session_data.id, node_id, switch_id, link_options, interface_one=0)))
print("get node links: {}".format(client.get_node_links(session_data.id, node_id)))
# change session state
print("set session state: {}".format(client.set_session_state(session_data.id, core_pb2.STATE_INSTANTIATION)))
# import pdb; pdb.set_trace()
# get session
print("get session: {}".format(client.get_session(session_data.id)))
# save xml
client.save_xml(session_data.id, xml_file_name)
# delete session
print("delete session: {}".format(client.delete_session(session_data.id)))
if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG)
main()

View file

View file

@ -0,0 +1,107 @@
import logging
import os
from core.grpc import client
from core.grpc import core_pb2
def log_event(event):
logging.info("event: %s", event)
def main():
xml_file_name = "/tmp/core.xml"
core = client.CoreGrpcClient()
with core.context_connect():
if os.path.exists(xml_file_name):
response = core.open_xml(xml_file_name)
print("open xml: {}".format(response))
print("services: {}".format(core.get_services()))
# create session
session = core.create_session()
core.exception_events(session.id, log_event)
core.node_events(session.id, log_event)
core.session_events(session.id, log_event)
core.link_events(session.id, log_event)
core.file_events(session.id, log_event)
core.config_events(session.id, log_event)
print("created session: {}".format(session))
print("default services: {}".format(core.get_service_defaults(session.id)))
print("emane models: {}".format(core.get_emane_models(session.id)))
print("add hook: {}".format(core.add_hook(session.id, core_pb2.STATE_RUNTIME, "test", "echo hello")))
print("hooks: {}".format(core.get_hooks(session.id)))
response = core.get_sessions()
print("core client received: {}".format(response))
print("set emane config: {}".format(core.set_emane_config(session.id, {"otamanagerttl": "2"})))
print("emane config: {}".format(core.get_emane_config(session.id)))
# set session location
response = core.set_session_location(
session.id, x=0, y=0,
lat=47.57917, lon=-122.13232, alt=3.0,
scale=150000.0
)
print("set location response: {}".format(response))
# get options
print("get options: {}".format(core.get_session_options(session.id)))
# get location
print("get location: {}".format(core.get_session_location(session.id)))
# change session state
print("set session state: {}".format(core.set_session_state(session.id, core_pb2.STATE_CONFIGURATION)))
# create switch node
switch = core_pb2.Node(type=core_pb2.NODE_SWITCH)
response = core.add_node(session.id, switch)
print("created switch: {}".format(response))
switch_id = response.id
# ip generator for example
interface_helper = client.InterfaceHelper(ip4_prefix="10.83.0.0/16")
# create node nodes and link them to switch
for _ in xrange(2):
node = core_pb2.Node()
response = core.add_node(session.id, node)
print("created node: {}".format(response))
node_id = response.id
position = core_pb2.Position(x=5, y=5)
print("edit node: {}".format(core.edit_node(session.id, node_id, position)))
print("get node: {}".format(core.get_node(session.id, node_id)))
print("emane model config: {}".format(
core.get_emane_model_config(session.id, node_id, "emane_tdma")))
print("node service: {}".format(core.get_node_service(session.id, node_id, "zebra")))
# create link
interface_one = interface_helper.create_interface(node_id, 0)
print("created link: {}".format(core.add_link(session.id, node_id, switch_id, interface_one)))
link_options = core_pb2.LinkOptions(per=50)
print("edit link: {}".format(core.edit_link(
session.id, node_id, switch_id, link_options, interface_one=0)))
print("get node links: {}".format(core.get_node_links(session.id, node_id)))
# change session state
print("set session state: {}".format(core.set_session_state(session.id, core_pb2.STATE_INSTANTIATION)))
# get session
print("get session: {}".format(core.get_session(session.id)))
# save xml
core.save_xml(session.id, xml_file_name)
# delete session
print("delete session: {}".format(core.delete_session(session.id)))
if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG)
main()