Esempio n. 1
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)
Esempio n. 2
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)
Esempio n. 3
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()
Esempio n. 4
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)
Esempio n. 5
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)
Esempio n. 6
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)
Esempio n. 7
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)
Esempio n. 8
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()