core-extra/daemon/examples/python/distributed_switch.py

52 lines
1.6 KiB
Python

import logging
import distributed_parser
from core.emulator.coreemu import CoreEmu
from core.emulator.emudata import IpPrefixes, NodeOptions
from core.emulator.enumerations import EventTypes, NodeTypes
def main(args):
# ip generator for example
prefixes = IpPrefixes(ip4_prefix="10.83.0.0/16")
# create emulator instance for creating sessions and utility methods
coreemu = CoreEmu(
{"controlnet": "172.16.0.0/24", "distributed_address": args.address}
)
session = coreemu.create_session()
# initialize distributed
server_name = "core2"
session.distributed.add_server(server_name, args.server)
# must be in configuration state for nodes to start, when using "node_add" below
session.set_state(EventTypes.CONFIGURATION_STATE)
# create local node, switch, and remote nodes
node_one = session.add_node()
switch = session.add_node(_type=NodeTypes.SWITCH)
options = NodeOptions()
options.server = server_name
node_two = session.add_node(options=options)
# create node interfaces and link
interface_one = prefixes.create_interface(node_one)
interface_two = prefixes.create_interface(node_two)
session.add_link(node_one.id, switch.id, interface_one=interface_one)
session.add_link(node_two.id, switch.id, interface_one=interface_two)
# instantiate session
session.instantiate()
# pause script for verification
input("press enter for shutdown")
# shutdown session
coreemu.shutdown()
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
args = distributed_parser.parse(__file__)
main(args)