grpc doc improvements, grpc examples additions, small tweak to grpc client for setting emane models not requiring a config when using default configuration
This commit is contained in:
parent
9a5fc94ba2
commit
75d5bced9c
7 changed files with 220 additions and 148 deletions
|
@ -1,6 +1,11 @@
|
|||
"""
|
||||
Example using gRPC API to create a simple switch network.
|
||||
"""
|
||||
|
||||
import logging
|
||||
|
||||
from core.api.grpc import client, core_pb2
|
||||
from core.api.grpc import client
|
||||
from core.api.grpc.core_pb2 import Node, NodeType, Position, SessionState
|
||||
|
||||
|
||||
def log_event(event):
|
||||
|
@ -8,8 +13,11 @@ def log_event(event):
|
|||
|
||||
|
||||
def main():
|
||||
core = client.CoreGrpcClient()
|
||||
# helper to create interface addresses
|
||||
interface_helper = client.InterfaceHelper(ip4_prefix="10.83.0.0/24")
|
||||
|
||||
# create grpc client and start connection context, which auto closes connection
|
||||
core = client.CoreGrpcClient()
|
||||
with core.context_connect():
|
||||
# create session
|
||||
response = core.create_session()
|
||||
|
@ -19,38 +27,41 @@ def main():
|
|||
session_id = response.session_id
|
||||
core.events(session_id, log_event)
|
||||
|
||||
# change session state
|
||||
response = core.set_session_state(
|
||||
session_id, core_pb2.SessionState.CONFIGURATION
|
||||
)
|
||||
# change session state to configuration so that nodes get started when added
|
||||
response = core.set_session_state(session_id, SessionState.CONFIGURATION)
|
||||
logging.info("set session state: %s", response)
|
||||
|
||||
# create switch node
|
||||
switch = core_pb2.Node(type=core_pb2.NodeType.SWITCH)
|
||||
position = Position(x=200, y=200)
|
||||
switch = Node(type=NodeType.SWITCH, position=position)
|
||||
response = core.add_node(session_id, switch)
|
||||
logging.info("created switch: %s", response)
|
||||
switch_id = response.node_id
|
||||
|
||||
# helper to create interfaces
|
||||
interface_helper = client.InterfaceHelper(ip4_prefix="10.83.0.0/16")
|
||||
# create node one
|
||||
position = Position(x=100, y=100)
|
||||
node1 = Node(type=NodeType.DEFAULT, position=position)
|
||||
response = core.add_node(session_id, node1)
|
||||
logging.info("created node: %s", response)
|
||||
node1_id = response.node_id
|
||||
|
||||
for i in range(2):
|
||||
# create node
|
||||
position = core_pb2.Position(x=50 + 50 * i, y=50)
|
||||
node = core_pb2.Node(position=position)
|
||||
response = core.add_node(session_id, node)
|
||||
logging.info("created node: %s", response)
|
||||
node_id = response.node_id
|
||||
# create node two
|
||||
position = Position(x=300, y=100)
|
||||
node2 = Node(type=NodeType.DEFAULT, position=position)
|
||||
response = core.add_node(session_id, node2)
|
||||
logging.info("created node: %s", response)
|
||||
node2_id = response.node_id
|
||||
|
||||
# create link
|
||||
interface_one = interface_helper.create_interface(node_id, 0)
|
||||
response = core.add_link(session_id, node_id, switch_id, interface_one)
|
||||
logging.info("created link: %s", response)
|
||||
# links nodes to switch
|
||||
interface_one = interface_helper.create_interface(node1_id, 0)
|
||||
response = core.add_link(session_id, node1_id, switch_id, interface_one)
|
||||
logging.info("created link: %s", response)
|
||||
interface_one = interface_helper.create_interface(node2_id, 0)
|
||||
response = core.add_link(session_id, node2_id, switch_id, interface_one)
|
||||
logging.info("created link: %s", response)
|
||||
|
||||
# change session state
|
||||
response = core.set_session_state(
|
||||
session_id, core_pb2.SessionState.INSTANTIATION
|
||||
)
|
||||
response = core.set_session_state(session_id, SessionState.INSTANTIATION)
|
||||
logging.info("set session state: %s", response)
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue