Esempio n. 1
0
def main(args):
    # helper to create interfaces
    interface_helper = client.InterfaceHelper(ip4_prefix="10.83.0.0/16")

    # create grpc client and connect
    core = client.CoreGrpcClient()
    core.connect()

    # create session
    session = core.create_session()

    # add distributed server
    server = Server(name="core2", host=args.server)
    session.servers.append(server)

    # 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)
    node2 = session.add_node(3, position=position, server=server.name)

    # 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)
Esempio n. 2
0
    def move_node(
        self,
        session_id: int,
        node_id: int,
        position: wrappers.Position = None,
        geo: wrappers.Geo = None,
        source: str = None,
    ) -> bool:
        """
        Move node using provided position or geo location.

        :param session_id: session id
        :param node_id: node id
        :param position: x,y position to move to
        :param geo: geospatial position to move to
        :param source: source generating motion
        :return: nothing
        :raises grpc.RpcError: when session or nodes do not exist
        """
        if not position and not geo:
            raise CoreError("must provide position or geo to move node")
        position = position.to_proto() if position else None
        geo = geo.to_proto() if geo else None
        request = core_pb2.MoveNodeRequest(
            session_id=session_id,
            node_id=node_id,
            position=position,
            geo=geo,
            source=source,
        )
        response = self.stub.MoveNode(request)
        return response.result
Esempio n. 3
0
 def create_node(self, x: float, y: float, node_type: NodeType,
                 model: str) -> Optional[Node]:
     """
     Add node, with information filled in, to grpc manager
     """
     node_id = self.next_node_id()
     position = Position(x=x, y=y)
     image = None
     if NodeUtils.is_image_node(node_type):
         image = "ubuntu:latest"
     emane = None
     if node_type == NodeType.EMANE:
         if not self.session.emane_models:
             dialog = EmaneInstallDialog(self.app)
             dialog.show()
             return
         emane = self.session.emane_models[0]
         name = f"emane{node_id}"
     elif node_type == NodeType.WIRELESS_LAN:
         name = f"wlan{node_id}"
     elif node_type in [NodeType.RJ45, NodeType.TUNNEL]:
         name = "unassigned"
     else:
         name = f"n{node_id}"
     node = Node(
         id=node_id,
         type=node_type,
         name=name,
         model=model,
         position=position,
         image=image,
         emane=emane,
     )
     if NodeUtils.is_custom(node_type, model):
         services = NodeUtils.get_custom_node_services(
             self.app.guiconfig, model)
         node.services[:] = services
     # assign default services to CORE node
     else:
         services = self.session.default_services.get(model)
         if services:
             node.services = services.copy()
     logging.info(
         "add node(%s) to session(%s), coordinates(%s, %s)",
         node.name,
         self.session.id,
         x,
         y,
     )
     self.session.nodes[node.id] = node
     return node
Esempio n. 4
0
    def test_move_node_pos(self, grpc_server: CoreGrpcServer):
        # given
        client = CoreGrpcClient()
        session = grpc_server.coreemu.create_session()
        node = session.add_node(CoreNode)
        position = Position(x=100.0, y=50.0)

        # then
        with client.context_connect():
            result = client.move_node(session.id, node.id, position=position)

        # then
        assert result is True
        assert node.position.x == position.x
        assert node.position.y == position.y
Esempio n. 5
0
    def test_add_node(self, grpc_server: CoreGrpcServer):
        # given
        client = CoreGrpcClient()
        session = grpc_server.coreemu.create_session()

        # then
        with client.context_connect():
            position = Position(x=0, y=0)
            node = Node(id=1,
                        name="n1",
                        type=NodeType.DEFAULT,
                        position=position)
            node_id = client.add_node(session.id, node)

        # then
        assert node_id is not None
        assert session.get_node(node_id, CoreNode) is not None
Esempio n. 6
0
from core.api.grpc import client
from core.api.grpc.wrappers import Position

# interface helper
iface_helper = client.InterfaceHelper(ip4_prefix="10.0.0.0/24",
                                      ip6_prefix="2001::/64")

# create grpc client and connect
core = client.CoreGrpcClient()
core.connect()

# add session
session = core.create_session()

# create nodes
position = Position(x=100, y=100)
node1 = session.add_node(1, position=position)
position = Position(x=300, y=100)
node2 = session.add_node(2, position=position)

# create link
iface1 = iface_helper.create_iface(node1.id, 0)
iface2 = iface_helper.create_iface(node2.id, 0)
session.add_link(node1=node1, node2=node2, iface1=iface1, iface2=iface2)

# start session
core.start_session(session)
Esempio n. 7
0
    def test_start_session(self, grpc_server: CoreGrpcServer, definition):
        # given
        client = CoreGrpcClient()
        with client.context_connect():
            session = client.create_session()
        position = Position(x=50, y=100)
        node1 = session.add_node(1, position=position)
        position = Position(x=100, y=100)
        node2 = session.add_node(2, position=position)
        position = Position(x=200, y=200)
        wlan_node = session.add_node(3,
                                     _type=NodeType.WIRELESS_LAN,
                                     position=position)
        iface_helper = InterfaceHelper(ip4_prefix="10.83.0.0/16")
        iface1_id = 0
        iface1 = iface_helper.create_iface(node1.id, iface1_id)
        iface2_id = 0
        iface2 = iface_helper.create_iface(node2.id, iface2_id)
        link = Link(node1_id=node1.id,
                    node2_id=node2.id,
                    iface1=iface1,
                    iface2=iface2)
        session.links = [link]
        hook = Hook(state=SessionState.RUNTIME,
                    file="echo.sh",
                    data="echo hello")
        session.hooks = {hook.file: hook}
        location_x = 5
        location_y = 10
        location_z = 15
        location_lat = 20
        location_lon = 30
        location_alt = 40
        location_scale = 5
        session.location = SessionLocation(
            x=location_x,
            y=location_y,
            z=location_z,
            lat=location_lat,
            lon=location_lon,
            alt=location_alt,
            scale=location_scale,
        )

        # setup wlan config
        wlan_config_key = "range"
        wlan_config_value = "333"
        wlan_node.set_wlan({wlan_config_key: wlan_config_value})

        # setup mobility config
        mobility_config_key = "refresh_ms"
        mobility_config_value = "60"
        wlan_node.set_mobility({mobility_config_key: mobility_config_value})

        # setup service config
        service_name = "DefaultRoute"
        service_validate = ["echo hello"]
        node1.service_configs[service_name] = NodeServiceData(
            executables=[],
            dependencies=[],
            dirs=[],
            configs=[],
            startup=[],
            validate=service_validate,
            validation_mode=ServiceValidationMode.NON_BLOCKING,
            validation_timer=0,
            shutdown=[],
            meta="",
        )

        # setup service file config
        service_file = "defaultroute.sh"
        service_file_data = "echo hello"
        node1.service_file_configs[service_name] = {
            service_file: service_file_data
        }

        # setup session option
        option_key = "controlnet"
        option_value = "172.16.0.0/24"
        session.set_options({option_key: option_value})

        # when
        with patch.object(CoreXmlWriter, "write"):
            with client.context_connect():
                client.start_session(session, definition=definition)

        # then
        real_session = grpc_server.coreemu.sessions[session.id]
        if definition:
            state = EventTypes.DEFINITION_STATE
        else:
            state = EventTypes.RUNTIME_STATE
        assert real_session.state == state
        assert node1.id in real_session.nodes
        assert node2.id in real_session.nodes
        assert wlan_node.id in real_session.nodes
        assert iface1_id in real_session.nodes[node1.id].ifaces
        assert iface2_id in real_session.nodes[node2.id].ifaces
        hook_file, hook_data = real_session.hooks[EventTypes.RUNTIME_STATE][0]
        assert hook_file == hook.file
        assert hook_data == hook.data
        assert real_session.location.refxyz == (location_x, location_y,
                                                location_z)
        assert real_session.location.refgeo == (
            location_lat,
            location_lon,
            location_alt,
        )
        assert real_session.location.refscale == location_scale
        set_wlan_config = real_session.mobility.get_model_config(
            wlan_node.id, BasicRangeModel.name)
        assert set_wlan_config[wlan_config_key] == wlan_config_value
        set_mobility_config = real_session.mobility.get_model_config(
            wlan_node.id, Ns2ScriptedMobility.name)
        assert set_mobility_config[
            mobility_config_key] == mobility_config_value
        service = real_session.services.get_service(node1.id,
                                                    service_name,
                                                    default_service=True)
        assert service.validate == tuple(service_validate)
        real_node1 = real_session.get_node(node1.id, CoreNode)
        service_file = real_session.services.get_service_file(
            real_node1, service_name, service_file)
        assert service_file.data == service_file_data
        assert option_value == real_session.options.get_config(option_key)