2019-10-21 20:51:38 +01:00
|
|
|
import argparse
|
|
|
|
import logging
|
|
|
|
|
2021-05-04 21:29:22 +01:00
|
|
|
from core.api.grpc import client
|
2021-05-06 18:56:51 +01:00
|
|
|
from core.api.grpc.wrappers import NodeType, Position, Server
|
2019-10-21 20:51:38 +01:00
|
|
|
|
|
|
|
|
|
|
|
def log_event(event):
|
|
|
|
logging.info("event: %s", event)
|
|
|
|
|
|
|
|
|
|
|
|
def main(args):
|
2021-05-03 23:25:18 +01:00
|
|
|
# helper to create interfaces
|
2021-05-04 21:29:22 +01:00
|
|
|
interface_helper = client.InterfaceHelper(ip4_prefix="10.83.0.0/16")
|
2021-05-03 23:25:18 +01:00
|
|
|
|
|
|
|
# create grpc client and connect
|
2021-05-04 21:29:22 +01:00
|
|
|
core = client.CoreGrpcClient()
|
2021-05-03 23:25:18 +01:00
|
|
|
core.connect()
|
|
|
|
|
|
|
|
# create session
|
2021-05-06 23:06:16 +01:00
|
|
|
session = core.create_session()
|
2021-05-03 23:25:18 +01:00
|
|
|
|
|
|
|
# add distributed server
|
2021-05-06 18:56:51 +01:00
|
|
|
server = Server(name="core2", host=args.server)
|
|
|
|
session.servers.append(server)
|
2021-05-03 23:25:18 +01:00
|
|
|
|
|
|
|
# handle events session may broadcast
|
|
|
|
core.events(session.id, log_event)
|
|
|
|
|
|
|
|
# create switch node
|
|
|
|
position = Position(x=150, y=100)
|
|
|
|
switch = session.add_node(1, _type=NodeType.SWITCH, position=position)
|
|
|
|
position = Position(x=100, y=50)
|
|
|
|
node1 = session.add_node(2, position=position)
|
|
|
|
position = Position(x=200, y=50)
|
2021-05-06 18:56:51 +01:00
|
|
|
node2 = session.add_node(3, position=position, server=server.name)
|
2021-05-03 23:25:18 +01:00
|
|
|
|
|
|
|
# create links
|
|
|
|
iface1 = interface_helper.create_iface(node1.id, 0)
|
|
|
|
session.add_link(node1=node1, node2=switch, iface1=iface1)
|
|
|
|
iface1 = interface_helper.create_iface(node2.id, 0)
|
|
|
|
session.add_link(node1=node2, node2=switch, iface1=iface1)
|
|
|
|
|
|
|
|
# start session
|
|
|
|
core.start_session(session)
|
2019-10-21 20:51:38 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
logging.basicConfig(level=logging.DEBUG)
|
|
|
|
parser = argparse.ArgumentParser(description="Run distributed_switch example")
|
|
|
|
parser.add_argument(
|
|
|
|
"-a",
|
|
|
|
"--address",
|
2021-01-05 23:28:50 +00:00
|
|
|
required=True,
|
2019-10-21 20:51:38 +01:00
|
|
|
help="local address that distributed servers will use for gre tunneling",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
2021-01-05 23:28:50 +00:00
|
|
|
"-s",
|
|
|
|
"--server",
|
|
|
|
required=True,
|
|
|
|
help="distributed server to use for creating nodes",
|
2019-10-21 20:51:38 +01:00
|
|
|
)
|
|
|
|
args = parser.parse_args()
|
|
|
|
main(args)
|