Ejemplo n.º 1
0
def devices(local_server, project):
    """
    Create two VPCS for test
    """
    device1 = VPCSNode(VPCS(), local_server, project)
    device1._vpcs_id = str(uuid.uuid4())
    device1._settings = {"name": "VPCS 1", "script_file": "", "console": None, "startup_script": None}
    device1.setInitialized(True)

    port = EthernetPort("E1")
    port.setAdapterNumber(0)
    port.setPortNumber(0)
    device1._ports.append(port)

    device2 = VPCSNode(VPCS(), local_server, project)
    device2._vpcs_id = str(uuid.uuid4())
    device2._settings = {"name": "VPCS 2", "script_file": "", "console": None, "startup_script": None}
    device2.setInitialized(True)

    port = EthernetPort("E2")
    port.setAdapterNumber(0)
    port.setPortNumber(0)
    device2._ports.append(port)

    return (device1, device2)
Ejemplo n.º 2
0
    def _addAdapters(self, adapters):
        """
        Adds adapters.

        :param adapters: number of adapters
        """

        interface_number = segment_number = 0
        for adapter_number in range(0, adapters):
            if self._first_port_name and adapter_number == 0:
                port_name = self._first_port_name
            else:
                port_name = self._port_name_format.format(
                    interface_number,
                    segment_number,
                    port0=interface_number,
                    port1=1 + interface_number,
                    segment0=segment_number,
                    segment1=1 + segment_number)
                interface_number += 1
                if self._port_segment_size and interface_number % self._port_segment_size == 0:
                    segment_number += 1
                    interface_number = 0
            if self._settings["use_ubridge"]:
                new_port = EthernetPort(port_name)
            else:
                new_port = EthernetPort(port_name, nio=NIOVMNET)
            new_port.setAdapterNumber(adapter_number)
            new_port.setPortNumber(0)
            new_port.setPacketCaptureSupported(True)
            self._ports.append(new_port)
            log.debug("Adapter {} with port {} has been added".format(
                adapter_number, port_name))
Ejemplo n.º 3
0
    def __init__(self, module, server, project):
        VM.__init__(self, module, server, project)

        log.info("VPCS instance is being created")
        self._vm_id = None
        self._defaults = {}
        self._inital_settings = None
        self._export_directory = None
        self._loading = False
        self._ports = []
        self._settings = {
            "name": "",
            "script_file": "",
            "startup_script": None,
            "startup_script_path": None,
            "console": None
        }

        port_name = EthernetPort.longNameType() + str(0)
        short_name = EthernetPort.shortNameType() + str(0)

        # VPCS devices have only one Ethernet port
        port = EthernetPort(port_name)
        port.setShortName(short_name)
        port.setAdapterNumber(0)
        port.setPortNumber(0)
        self._ports.append(port)
        log.debug("port {} has been added".format(port_name))

        # save the default settings
        self._defaults = self._settings.copy()
Ejemplo n.º 4
0
    def load(self, node_info):
        """
        Loads an Ethernet switch representation
        (from a topology file).

        :param node_info: representation of the node (dictionary)
        """

        settings = node_info["properties"]
        name = settings.pop("name")

        # create the ports with the correct port numbers, IDs and settings
        if "ports" in node_info:
            ports = node_info["ports"]
            for topology_port in ports:
                port = EthernetPort(topology_port["name"])
                port.setPortNumber(topology_port["port_number"])
                port.setId(topology_port["id"])
                port.setStatus(EthernetPort.started)
                self._ports.append(port)
                self._settings["ports"][port.portNumber()] = {
                    "type": topology_port["type"],
                    "vlan": topology_port["vlan"]
                }

        log.info("Ethernet switch {} is loading".format(name))
        self.setup(name)
Ejemplo n.º 5
0
    def update(self, new_settings):
        """
        Updates the settings for this Ethernet switch.

        :param new_settings: settings dictionary
        """

        updated = False
        params = {}
        if "ports" in new_settings:
            ports_to_update = {}
            ports = new_settings["ports"]

            for port_number in ports.keys():
                if port_number in self._settings["ports"]:
                    if self._settings["ports"][port_number] != ports[port_number]:
                        for port in self._ports:
                            if port.portNumber() == port_number and not port.isFree():
                                ports_to_update[port_number] = ports[port_number]
                                break
                    continue
                port = EthernetPort(str(port_number))
                port.setPortNumber(port_number)
                port.setStatus(EthernetPort.started)
                port.setPacketCaptureSupported(True)
                self._ports.append(port)
                updated = True
                log.debug("port {} has been added".format(port_number))

            if ports_to_update:
                params["ports"] = []
                for port_number, info in ports_to_update.items():
                    info["port"] = port_number
                    params["ports"].append(info)
                updated = True

            # delete ports that are not configured
            for port_number in self._settings["ports"].keys():
                if port_number not in new_settings["ports"]:
                    for port in self._ports.copy():
                        if port.portNumber() == port_number:
                            self._ports.remove(port)
                            log.debug("port {} has been removed".format(port.name()))
                            break

            self._settings["ports"] = new_settings["ports"].copy()

        if "name" in new_settings and new_settings["name"] != self.name():
            if self.hasAllocatedName(new_settings["name"]):
                self.error_signal.emit(self.id(), 'Name "{}" is already used by another node'.format(new_settings["name"]))
                return
            params["name"] = new_settings["name"]
            updated = True

        if updated:
            log.debug("{} is being updated: {}".format(self.name(), params))
            self.httpPut("/dynamips/devices/{device_id}".format(device_id=self._device_id), self._updateCallback, body=params)
        else:
            log.info("{} has been updated".format(self.name()))
            self.updated_signal.emit()
Ejemplo n.º 6
0
    def __init__(self, module, server, project):
        VM.__init__(self, module, server, project)

        log.info("VPCS instance is being created")
        self._vm_id = None
        self._defaults = {}
        self._export_directory = None
        self._loading = False
        self._ports = []
        self._settings = {"name": "",
                          "script_file": "",
                          "startup_script": None,
                          "startup_script_path": None,
                          "console": None}

        port_name = EthernetPort.longNameType() + str(0)
        short_name = EthernetPort.shortNameType() + str(0)

        # VPCS devices have only one Ethernet port
        port = EthernetPort(port_name)
        port.setShortName(short_name)
        port.setAdapterNumber(0)
        port.setPortNumber(0)
        self._ports.append(port)
        log.debug("port {} has been added".format(port_name))

        # save the default settings
        self._defaults = self._settings.copy()
Ejemplo n.º 7
0
    def _addAdapters(self, adapters):
        """
        Adds adapters.

        :param adapters: number of adapters
        """

        for port_number in range(0, adapters):
            port_name = EthernetPort.longNameType() + str(port_number)
            new_port = EthernetPort(port_name)
            new_port.setPortNumber(port_number)
            self._ports.append(new_port)
            log.debug("port {} has been added".format(port_name))
Ejemplo n.º 8
0
    def _addAdapters(self, adapters):
        """Adds adapters.

        :param adapters: number of adapters
        """
        for adapter_number in range(0, adapters):
            adapter_name = EthernetPort.longNameType() + str(adapter_number)
            short_name = EthernetPort.shortNameType() + str(adapter_number)
            new_port = EthernetPort(adapter_name)
            new_port.setShortName(short_name)
            new_port.setAdapterNumber(adapter_number)
            new_port.setPortNumber(0)
            self._ports.append(new_port)
            log.debug("Adapter {} has been added".format(adapter_name))
Ejemplo n.º 9
0
    def _addAdapters(self, adapters):
        """
        Adds adapters.

        :param adapters: number of adapters
        """

        interface_number = segment_number = 0
        for adapter_number in range(0, adapters):
            if self._first_port_name and adapter_number == 0:
                port_name = self._first_port_name
            else:
                port_name = self._port_name_format.format(
                    interface_number,
                    segment_number,
                    port0=interface_number,
                    port1=1 + interface_number,
                    segment0=segment_number,
                    segment1=1 + segment_number)
                interface_number += 1
                if self._port_segment_size and interface_number % self._port_segment_size == 0:
                    segment_number += 1
                    interface_number = 0
            new_port = EthernetPort(port_name)
            new_port.setAdapterNumber(adapter_number)
            new_port.setPortNumber(0)
            new_port.setHotPluggable(False)
            self._ports.append(new_port)
            log.debug("Adapter {} with port {} has been added".format(
                adapter_number, port_name))
Ejemplo n.º 10
0
    def load(self, node_info):
        """
        Loads an Ethernet switch representation
        (from a topology file).

        :param node_info: representation of the node (dictionary)
        """

        settings = node_info["properties"]
        name = settings.pop("name")

        # create the ports with the correct port numbers, IDs and settings
        if "ports" in node_info:
            ports = node_info["ports"]
            for topology_port in ports:
                port = EthernetPort(topology_port["name"])
                port.setPortNumber(topology_port["port_number"])
                port.setId(topology_port["id"])
                port.setStatus(EthernetPort.started)
                self._ports.append(port)
                self._settings["ports"][port.portNumber()] = {"type": topology_port["type"],
                                                          "vlan": topology_port["vlan"]}

        log.info("Ethernet switch {} is loading".format(name))
        self.setup(name)
Ejemplo n.º 11
0
    def _addAdapters(self, adapters):
        """
        Adds adapters.

        :param adapters: number of adapters
        """

        interface_number = segment_number = 0
        for adapter_number in range(0, adapters):
            if self._first_port_name and adapter_number == 0:
                port_name = self._first_port_name
            else:
                port_name = self._port_name_format.format(
                    interface_number,
                    segment_number,
                    port0 = interface_number,
                    port1 = 1 + interface_number,
                    segment0 = segment_number,
                    segment1 = 1 + segment_number
                )
                interface_number += 1
                if self._port_segment_size and interface_number % self._port_segment_size == 0:
                    segment_number += 1
                    interface_number = 0
            if self._settings["use_ubridge"]:
                new_port = EthernetPort(port_name)
            else:
                new_port = EthernetPort(port_name, nio=NIOVMNET)
            new_port.setAdapterNumber(adapter_number)
            new_port.setPortNumber(0)
            new_port.setPacketCaptureSupported(True)
            self._ports.append(new_port)
            log.debug("Adapter {} with port {} has been added".format(adapter_number, port_name))
Ejemplo n.º 12
0
    def update(self, new_settings):
        """
        Updates the settings for this Ethernet hub.

        :param new_settings: settings dictionary
        """

        ports_to_create = []
        ports = new_settings["ports"]

        updated = False
        for port_number in ports:
            if port_number not in ports_to_create:
                ports_to_create.append(port_number)

        for port in self._ports.copy():
            if port.isFree():
                self._ports.remove(port)
                updated = True
                log.debug("port {} has been removed".format(port.name()))
            else:
                ports_to_create.remove(port.name())

        for port_name in ports_to_create:
            port = EthernetPort(port_name)
            port.setPortNumber(int(port_name))
            port.setStatus(EthernetPort.started)
            self._ports.append(port)
            updated = True
            log.debug("port {} has been added".format(port_name))

        params = {}
        if "name" in new_settings and new_settings["name"] != self.name():
            if self.hasAllocatedName(new_settings["name"]):
                self.error_signal.emit(self.id(), 'Name "{}" is already used by another node'.format(new_settings["name"]))
                return
            params = {"id": self._ethhub_id,
                      "name": new_settings["name"]}
            updated = True

        self._settings["ports"] = new_settings["ports"].copy()
        if updated:
            if params:
                log.debug("{} is being updated: {}".format(self.name(), params))
                self._server.send_message("dynamips.ethhub.update", params, self._updateCallback)
            else:
                log.info("{} has been updated".format(self.name()))
                self.updated_signal.emit()
Ejemplo n.º 13
0
    def update(self, new_settings):
        """
        Updates the settings for this Ethernet switch.

        :param new_settings: settings dictionary
        """

        ports_to_update = {}
        ports = new_settings["ports"]
        updated = False
        for port_number in ports.keys():
            if port_number in self._settings["ports"]:
                if self._settings["ports"][port_number] != ports[port_number]:
                    for port in self._ports:
                        if port.portNumber() == port_number and not port.isFree():
                            ports_to_update[port_number] = ports[port_number]
                            break
                continue
            port = EthernetPort(str(port_number))
            port.setPortNumber(port_number)
            port.setStatus(EthernetPort.started)
            port.setPacketCaptureSupported(True)
            self._ports.append(port)
            updated = True
            log.debug("port {} has been added".format(port_number))

        params = {"id": self._ethsw_id}
        if ports_to_update:
            params["ports"] = {}
            for port_number, info in ports_to_update.items():
                params["ports"][port_number] = info
            updated = True

        if "name" in new_settings and new_settings["name"] != self.name():
            if self.hasAllocatedName(new_settings["name"]):
                self.error_signal.emit(self.id(), 'Name "{}" is already used by another node'.format(new_settings["name"]))
                return
            params["name"] = new_settings["name"]
            updated = True

        # delete ports that are not configured
        for port_number in self._settings["ports"].keys():
            if port_number not in new_settings["ports"]:
                for port in self._ports.copy():
                    if port.portNumber() == port_number:
                        self._ports.remove(port)
                        log.debug("port {} has been removed".format(port.name()))
                        break

        self._settings["ports"] = new_settings["ports"].copy()
        if updated:
            log.debug("{} is being updated: {}".format(self.name(), params))
            self._server.send_message("dynamips.ethsw.update", params, self._updateCallback)
Ejemplo n.º 14
0
    def _updatePorts(self, ports):
        self._settings["ports"] = ports
        old_ports = self._ports.copy()
        self._ports = []
        for port in ports:
            new_port = None

            # Update port if already exist
            for old_port in old_ports:
                if old_port.adapterNumber(
                ) == port["adapter_number"] and old_port.portNumber(
                ) == port["port_number"] and old_port.name() == port["name"]:
                    new_port = old_port
                    old_ports.remove(old_port)
                    break

            if new_port is None:
                if port["link_type"] == "serial":
                    new_port = SerialPort(port["name"])
                else:
                    new_port = EthernetPort(port["name"])
            new_port.setShortName(port["short_name"])
            new_port.setAdapterNumber(port["adapter_number"])
            new_port.setPortNumber(port["port_number"])
            new_port.setDataLinkTypes(port["data_link_types"])
            new_port.setStatus(self.status())
            self._ports.append(new_port)
Ejemplo n.º 15
0
    def _updatePorts(self, ports):
        """
        Update the ports on this node.

        :param ports: array of Port objects
        """

        self._settings["ports"] = ports
        old_ports = self._ports.copy()
        self._ports = []
        for port in ports:
            new_port = None

            # Update port if it already exists
            for old_port in old_ports:
                if old_port.adapterNumber() == port["adapter_number"] and old_port.portNumber() == port["port_number"]:
                    new_port = old_port
                    old_port.setName(port["name"])
                    old_ports.remove(old_port)
                    break

            if new_port is None:
                if port["link_type"] == "serial":
                    new_port = SerialPort(port["name"])
                else:
                    new_port = EthernetPort(port["name"])
            new_port.setShortName(port["short_name"])
            new_port.setAdapterNumber(port["adapter_number"])
            new_port.setPortNumber(port["port_number"])
            new_port.setDataLinkTypes(port["data_link_types"])
            new_port.setStatus(self.status())
            new_port.setAdapterType(port.get("adapter_type"))
            new_port.setMacAddress(port.get("mac_address"))
            self._ports.append(new_port)
Ejemplo n.º 16
0
    def update(self, new_settings):
        """
        Updates the settings for this Ethernet hub.

        :param new_settings: settings dictionary
        """

        updated = False
        if "ports" in new_settings:
            ports_to_create = []
            ports = new_settings["ports"]
            for port_number in ports:
                if port_number not in ports_to_create:
                    ports_to_create.append(str(port_number))

            for port in self._ports.copy():
                if port.isFree():
                    self._ports.remove(port)
                    updated = True
                    log.debug("port {} has been removed".format(port.name()))
                elif port.name() in ports_to_create:
                    ports_to_create.remove(port.name())

            for port_name in ports_to_create:
                port = EthernetPort(port_name)
                port.setPortNumber(int(port_name))
                port.setStatus(EthernetPort.started)
                port.setPacketCaptureSupported(True)
                self._ports.append(port)
                updated = True
                log.debug("port {} has been added".format(port_name))

            self._settings["ports"] = new_settings["ports"].copy()

        params = {}
        if "name" in new_settings and new_settings["name"] != self.name():
            if self.hasAllocatedName(new_settings["name"]):
                self.error_signal.emit(
                    self.id(),
                    'Name "{}" is already used by another node'.format(
                        new_settings["name"]))
                return
            params["name"] = new_settings["name"]
            updated = True

        if updated:
            if params:
                log.debug("{} is being updated: {}".format(
                    self.name(), params))
                self.httpPut("/dynamips/devices/{device_id}".format(
                    device_id=self._device_id),
                             self._updateCallback,
                             body=params)
            else:
                log.info("{} has been updated".format(self.name()))
                self.updated_signal.emit()
Ejemplo n.º 17
0
    def _addAdapters(self, adapters):
        """
        Adds adapters.

        :param adapters: number of adapters
        """

        interface_number = segment_number = 0
        for adapter_number in range(0, adapters):
            if self._first_port_name and adapter_number == 0:
                port_name = self._first_port_name
            else:
                port_name = self._port_name_format.format(
                    interface_number,
                    segment_number,
                    port0=interface_number,
                    port1=1 + interface_number,
                    segment0=segment_number,
                    segment1=1 + segment_number
                )
                interface_number += 1
                if self._port_segment_size and interface_number % self._port_segment_size == 0:
                    segment_number += 1
                    interface_number = 0
            new_port = EthernetPort(port_name)
            new_port.setAdapterNumber(adapter_number)
            new_port.setPortNumber(0)
            new_port.setHotPluggable(False)
            self._ports.append(new_port)
            log.debug("Adapter {} with port {} has been added".format(adapter_number, port_name))
Ejemplo n.º 18
0
    def setup(self, name=None, device_id=None, initial_ports=[]):
        """
        Setups this Ethernet switch.

        :param name: optional name for this switch
        :param device_id: device identifier on the server
        :param initial_ports: ports to be automatically added when creating this switch
        """

        # let's create a unique name if none has been chosen
        if not name:
            name = self.allocateName("SW")

        if not name:
            self.error_signal.emit(self.id(), "could not allocate a name for this Ethernet switch")
            return

        self._settings["name"] = name
        if not initial_ports:
            # default configuration if no initial ports
            for port_number in range(1, 9):
                # add 8 ports
                initial_ports.append({"name": str(port_number),
                                      "port_number": port_number,
                                      "type": "access",
                                      "vlan": 1})

        # add initial ports
        for initial_port in initial_ports:
            port = EthernetPort(initial_port["name"])
            port.setPortNumber(initial_port["port_number"])
            if "id" in initial_port:
                port.setId(initial_port["id"])
            port.setStatus(EthernetPort.started)
            port.setPacketCaptureSupported(True)
            self._ports.append(port)
            self._settings["ports"][port.portNumber()] = {"type": initial_port["type"],
                                                          "vlan": initial_port["vlan"],
                                                          "ethertype": initial_port.get("ethertype", "")}

        params = {"name": name,
                  "device_type": "ethernet_switch"}
        if device_id:
            params["device_id"] = device_id
        self.httpPost("/dynamips/devices", self._setupCallback, body=params)
Ejemplo n.º 19
0
    def __init__(self, module, server):
        Node.__init__(self, server)

        log.info("VPCS instance is being created")
        self._vpcs_id = None
        self._defaults = {}
        self._inital_settings = None
        self._loading = False
        self._module = module
        self._ports = []
        self._settings = {"name": "", "script_file": "", "console": None}

        port_name = EthernetPort.longNameType() + str(0)
        port = EthernetPort(port_name)
        port.setPortNumber(0)
        self._ports.append(port)
        log.debug("port {} has been added".format(port_name))

        # save the default settings
        self._defaults = self._settings.copy()
Ejemplo n.º 20
0
    def update(self, new_settings):
        """
        Updates the settings for this Ethernet switch.

        :param new_settings: settings dictionary
        """

        ports_to_update = {}
        ports = new_settings["ports"]
        updated = False
        for port_number in ports.keys():
            if port_number in self._settings["ports"]:
                if self._settings["ports"][port_number] != ports[port_number]:
                    for port in self._ports:
                        if port.portNumber() == port_number and not port.isFree():
                            ports_to_update[port_number] = ports[port_number]
                            break
                continue
            port = EthernetPort(str(port_number))
            port.setPortNumber(port_number)
            port.setStatus(EthernetPort.started)
            self._ports.append(port)
            updated = True
            log.debug("port {} has been added".format(port_number))

        params = {"id": self._ethsw_id}
        if ports_to_update:
            params["ports"] = {}
            for port_number, info in ports_to_update.items():
                params["ports"][port_number] = info
            updated = True

        if "name" in new_settings and new_settings["name"] != self.name():
            params["name"] = new_settings["name"]
            updated = True

        self._settings["ports"] = new_settings["ports"].copy()
        if updated:
            log.debug("{} is being updated: {}".format(self.name(), params))
            self._server.send_message("dynamips.ethsw.update", params, self._updateCallback)
Ejemplo n.º 21
0
    def __init__(self, module, server, project):
        super().__init__(module, server, project)

        log.info("VPCS instance is being created")
        self._vm_id = None
        self._settings = {
            "name": "",
            "startup_script": None,
            "startup_script_path": None,
            "console": None
        }

        port_name = EthernetPort.longNameType() + str(0)
        short_name = EthernetPort.shortNameType() + str(0)

        # VPCS devices have only one fixed Ethernet port
        port = EthernetPort(port_name)
        port.setShortName(short_name)
        port.setAdapterNumber(0)
        port.setPortNumber(0)
        self._ports.append(port)
        log.debug("port {} has been added".format(port_name))
Ejemplo n.º 22
0
    def setup(self, name=None, initial_ports=[]):
        """
        Setups this Ethernet switch.

        :param name: optional name for this switch
        :param initial_ports: ports to be automatically added when creating this switch
        """

        # let's create a unique name if none has been chosen
        if not name:
            name = self.allocateName("SW")

        if not name:
            self.error_signal.emit(self.id(), "could not allocate a name for this Ethernet switch")
            return

        if not initial_ports:
            # default configuration if no initial ports
            for port_number in range(1, 9):
                # add 8 ports
                initial_ports.append({"name": str(port_number),
                                      "port_number": port_number,
                                      "type": "access",
                                      "vlan": 1})

        # add initial ports
        for initial_port in initial_ports:
            port = EthernetPort(initial_port["name"])
            port.setPortNumber(initial_port["port_number"])
            if "id" in initial_port:
                port.setId(initial_port["id"])
            port.setStatus(EthernetPort.started)
            port.setPacketCaptureSupported(True)
            self._ports.append(port)
            self._settings["ports"][port.portNumber()] = {"type": initial_port["type"],
                                                          "vlan": initial_port["vlan"]}

        params = {"name": name}
        self._server.send_message("dynamips.ethsw.create", params, self._setupCallback)
Ejemplo n.º 23
0
    def __init__(self, module, server):
        Node.__init__(self, server)

        log.info("VPCS instance is being created")
        self._vpcs_id = None
        self._defaults = {}
        self._inital_settings = None
        self._loading = False
        self._module = module
        self._ports = []
        self._settings = {"name": "",
                          "script_file": "",
                          "console": None}

        port_name = EthernetPort.longNameType() + str(0)
        port = EthernetPort(port_name)
        port.setPortNumber(0)
        self._ports.append(port)
        log.debug("port {} has been added".format(port_name))

        # save the default settings
        self._defaults = self._settings.copy()
Ejemplo n.º 24
0
    def __init__(self, module, server, project):
        super().__init__(module, server, project)

        log.info("VPCS instance is being created")
        self._vm_id = None
        self._settings = {"name": "",
                          "startup_script": None,
                          "startup_script_path": None,
                          "console": None}

        port_name = EthernetPort.longNameType() + str(0)
        short_name = EthernetPort.shortNameType() + str(0)

        # VPCS devices have only one fixed Ethernet port
        port = EthernetPort(port_name)
        port.setShortName(short_name)
        port.setAdapterNumber(0)
        port.setPortNumber(0)
        self._ports.append(port)
        log.debug("port {} has been added".format(port_name))
Ejemplo n.º 25
0
    def update(self, new_settings):
        """
        Updates the settings for this Ethernet hub.

        :param new_settings: settings dictionary
        """

        updated = False
        if "ports" in new_settings:
            ports_to_create = []
            ports = new_settings["ports"]
            for port_number in ports:
                if port_number not in ports_to_create:
                    ports_to_create.append(port_number)

            for port in self._ports.copy():
                if port.isFree():
                    self._ports.remove(port)
                    updated = True
                    log.debug("port {} has been removed".format(port.name()))
                elif port.name() in ports_to_create:
                    ports_to_create.remove(port.name())

            for port_name in ports_to_create:
                port = EthernetPort(port_name)
                port.setPortNumber(int(port_name))
                port.setStatus(EthernetPort.started)
                port.setPacketCaptureSupported(True)
                self._ports.append(port)
                updated = True
                log.debug("port {} has been added".format(port_name))

            self._settings["ports"] = new_settings["ports"].copy()

        params = {}
        if "name" in new_settings and new_settings["name"] != self.name():
            if self.hasAllocatedName(new_settings["name"]):
                self.error_signal.emit(self.id(), 'Name "{}" is already used by another node'.format(new_settings["name"]))
                return
            params["name"] = new_settings["name"]
            updated = True

        if updated:
            if params:
                log.debug("{} is being updated: {}".format(self.name(), params))
                self.httpPut("/dynamips/devices/{device_id}".format(device_id=self._device_id), self._updateCallback, body=params)
            else:
                log.info("{} has been updated".format(self.name()))
                self.updated_signal.emit()
Ejemplo n.º 26
0
    def update(self, new_settings):
        """
        Updates the settings for this Ethernet hub.

        :param new_settings: settings dictionary
        """

        ports_to_create = []
        ports = new_settings["ports"]

        updated = False
        for port_number in ports:
            if port_number not in ports_to_create:
                ports_to_create.append(port_number)

        for port in self._ports.copy():
            if port.isFree():
                self._ports.remove(port)
                updated = True
                log.debug("port {} has been removed".format(port.name()))
            else:
                ports_to_create.remove(port.name())

        for port_name in ports_to_create:
            port = EthernetPort(port_name)
            port.setPortNumber(int(port_name))
            port.setStatus(EthernetPort.started)
            self._ports.append(port)
            updated = True
            log.debug("port {} has been added".format(port_name))

        params = {}
        if "name" in new_settings and new_settings["name"] != self.name():
            if self.hasAllocatedName(new_settings["name"]):
                self.error_signal.emit(
                    self.id(),
                    'Name "{}" is already used by another node'.format(
                        new_settings["name"]))
                return
            params = {"id": self._ethhub_id, "name": new_settings["name"]}
            updated = True

        self._settings["ports"] = new_settings["ports"].copy()
        if updated:
            if params:
                log.debug("{} is being updated: {}".format(
                    self.name(), params))
                self._server.send_message("dynamips.ethhub.update", params,
                                          self._updateCallback)
            else:
                log.info("{} has been updated".format(self.name()))
                self.updated_signal.emit()
Ejemplo n.º 27
0
    def update(self, new_settings):
        """
        Updates the settings for this Ethernet switch.

        :param new_settings: settings dictionary
        """

        ports_to_update = {}
        ports = new_settings["ports"]
        updated = False
        for port_number in ports.keys():
            if port_number in self._settings["ports"]:
                if self._settings["ports"][port_number] != ports[port_number]:
                    for port in self._ports:
                        if port.portNumber(
                        ) == port_number and not port.isFree():
                            ports_to_update[port_number] = ports[port_number]
                            break
                continue
            port = EthernetPort(str(port_number))
            port.setPortNumber(port_number)
            port.setStatus(EthernetPort.started)
            self._ports.append(port)
            updated = True
            log.debug("port {} has been added".format(port_number))

        params = {"id": self._ethsw_id}
        if ports_to_update:
            params["ports"] = {}
            for port_number, info in ports_to_update.items():
                params["ports"][port_number] = info
            updated = True

        if "name" in new_settings and new_settings["name"] != self.name():
            if self.hasAllocatedName(new_settings["name"]):
                self.error_signal.emit(
                    self.id(),
                    'Name "{}" is already used by another node'.format(
                        new_settings["name"]))
                return
            params["name"] = new_settings["name"]
            updated = True

        self._settings["ports"] = new_settings["ports"].copy()
        if updated:
            log.debug("{} is being updated: {}".format(self.name(), params))
            self._server.send_message("dynamips.ethsw.update", params,
                                      self._updateCallback)
Ejemplo n.º 28
0
    def _addAdapters(self, adapters):
        """
        Adds adapters.

        :param adapters: number of adapters
        """

        for port_number in range(0, self._settings["adapter_start_index"] + adapters):
            if port_number < self._settings["adapter_start_index"]:
                continue
            port_name = EthernetPort.longNameType() + str(port_number)
            new_port = EthernetPort(port_name)
            new_port.setPortNumber(port_number)
            new_port.setPacketCaptureSupported(True)
            self._ports.append(new_port)
            log.debug("port {} has been added".format(port_name))
Ejemplo n.º 29
0
    def _addAdapters(self, adapters):
        """Adds adapters.

        :param adapters: number of adapters
        """
        for adapter_number in range(0, adapters):
            adapter_name = "eth" + str(adapter_number)
            short_name = EthernetPort.shortNameType() + str(adapter_number)
            new_port = EthernetPort(adapter_name)
            new_port.setShortName(short_name)
            new_port.setAdapterNumber(adapter_number)
            new_port.setPortNumber(0)
            new_port.setHotPluggable(False)
            new_port.setPacketCaptureSupported(True)
            self._ports.append(new_port)
            log.debug("Adapter {} has been added".format(adapter_name))
Ejemplo n.º 30
0
def devices(local_server, project):
    """
    Create two VPCS for test
    """
    device1 = VPCSNode(VPCS(), local_server, project)
    device1._vpcs_id = str(uuid.uuid4())
    device1._settings = {
        "name": "VPCS 1",
        "script_file": "",
        "console": None,
        "startup_script": None
    }
    device1.setInitialized(True)

    port = EthernetPort("E1")
    port.setAdapterNumber(0)
    port.setPortNumber(0)
    device1._ports.append(port)

    device2 = VPCSNode(VPCS(), local_server, project)
    device2._vpcs_id = str(uuid.uuid4())
    device2._settings = {
        "name": "VPCS 2",
        "script_file": "",
        "console": None,
        "startup_script": None
    }
    device2.setInitialized(True)

    port = EthernetPort("E2")
    port.setAdapterNumber(0)
    port.setPortNumber(0)
    device2._ports.append(port)

    return (device1, device2)
Ejemplo n.º 31
0
    def update(self, new_settings):
        """
        Updates the settings for this Ethernet switch.

        :param new_settings: settings dictionary
        """

        updated = False
        params = {}
        if "ports" in new_settings:
            ports_to_update = {}
            ports = new_settings["ports"]

            for port_number in ports.keys():
                if port_number in self._settings["ports"]:
                    if self._settings["ports"][port_number] != ports[
                            port_number]:
                        for port in self._ports:
                            if port.portNumber(
                            ) == port_number and not port.isFree():
                                ports_to_update[port_number] = ports[
                                    port_number]
                                break
                    continue
                port = EthernetPort(str(port_number))
                port.setPortNumber(port_number)
                port.setStatus(EthernetPort.started)
                port.setPacketCaptureSupported(True)
                self._ports.append(port)
                updated = True
                log.debug("port {} has been added".format(port_number))

            if ports_to_update:
                params["ports"] = []
                for port_number, info in ports_to_update.items():
                    info["port"] = port_number
                    params["ports"].append(info)
                updated = True

            # delete ports that are not configured
            for port_number in self._settings["ports"].keys():
                if port_number not in new_settings["ports"]:
                    for port in self._ports.copy():
                        if port.portNumber() == port_number:
                            self._ports.remove(port)
                            log.debug("port {} has been removed".format(
                                port.name()))
                            break

            self._settings["ports"] = new_settings["ports"].copy()

        if "name" in new_settings and new_settings["name"] != self.name():
            if self.hasAllocatedName(new_settings["name"]):
                self.error_signal.emit(
                    self.id(),
                    'Name "{}" is already used by another node'.format(
                        new_settings["name"]))
                return
            params["name"] = new_settings["name"]
            updated = True

        if updated:
            log.debug("{} is being updated: {}".format(self.name(), params))
            self.httpPut("/dynamips/devices/{device_id}".format(
                device_id=self._device_id),
                         self._updateCallback,
                         body=params)
        else:
            log.info("{} has been updated".format(self.name()))
            self.updated_signal.emit()
Ejemplo n.º 32
0
    def setup(self, name=None, device_id=None, initial_ports=[]):
        """
        Setups this Ethernet switch.

        :param name: optional name for this switch
        :param device_id: device identifier on the server
        :param initial_ports: ports to be automatically added when creating this switch
        """

        # let's create a unique name if none has been chosen
        if not name:
            name = self.allocateName("SW")

        if not name:
            self.error_signal.emit(
                self.id(),
                "could not allocate a name for this Ethernet switch")
            return

        self._settings["name"] = name
        if not initial_ports:
            # default configuration if no initial ports
            for port_number in range(1, 9):
                # add 8 ports
                initial_ports.append({
                    "name": str(port_number),
                    "port_number": port_number,
                    "type": "access",
                    "vlan": 1
                })

        # add initial ports
        for initial_port in initial_ports:
            port = EthernetPort(initial_port["name"])
            port.setPortNumber(initial_port["port_number"])
            if "id" in initial_port:
                port.setId(initial_port["id"])
            port.setStatus(EthernetPort.started)
            port.setPacketCaptureSupported(True)
            self._ports.append(port)
            self._settings["ports"][port.portNumber()] = {
                "type": initial_port["type"],
                "vlan": initial_port["vlan"]
            }

        params = {"name": name, "device_type": "ethernet_switch"}
        if device_id:
            params["device_id"] = device_id
        self.httpPost("/dynamips/devices", self._setupCallback, body=params)
Ejemplo n.º 33
0
    def _addAdapters(self, adapters):
        """
        Adds adapters.

        :param adapters: number of adapters
        """

        for adapter_number in range(0, adapters):
            adapter_name = EthernetPort.longNameType() + str(adapter_number)
            short_name = EthernetPort.shortNameType() + str(adapter_number)
            new_port = EthernetPort(adapter_name)
            new_port.setShortName(short_name)
            new_port.setAdapterNumber(adapter_number)
            new_port.setPortNumber(0)
            new_port.setPacketCaptureSupported(True)
            self._ports.append(new_port)
            log.debug("Adapter {} has been added".format(adapter_name))