Exemple #1
0
class SwitchPort(mf.ModelBase, mixins.BasicEvents, mixins.Name):
    #定义表结构
    table_name = 'switch_port'

    #port编号
    port_num = fields.IntField()
    #admin状态
    admin_state = df_fields.EnumField(('up', 'down'))
    lport = df_fields.ReferenceField(l2.LogicalPort)
    #接口类型
    type = df_fields.EnumField((
        constants.SWITCH_BRIDGE_INTERFACE,
        constants.SWITCH_PATCH_INTERFACE,
        constants.SWITCH_COMPUTE_INTERFACE,
        constants.SWITCH_TUNNEL_INTERFACE,
        constants.SWITCH_UNKNOWN_INTERFACE,
    ), )
    #对端信息
    peer = fields.StringField()
    #接口mac地址
    mac_in_use = df_fields.MacAddressField()
    #
    attached_mac = df_fields.MacAddressField()
    #隧道类型
    tunnel_type = fields.StringField()
Exemple #2
0
class OvsPort(mf.ModelBase, mixins.BasicEvents, mixins.Name):
    table_name = 'ovs_port'

    ofport = fields.IntField()
    admin_state = df_fields.EnumField(('up', 'down'))
    lport = df_fields.ReferenceField(l2.LogicalPort)
    type = df_fields.EnumField((
        constants.OVS_BRIDGE_INTERFACE,
        constants.OVS_PATCH_INTERFACE,
        constants.OVS_VM_INTERFACE,
        constants.OVS_TUNNEL_INTERFACE,
        constants.OVS_UNKNOWN_INTERFACE,
    ), )
    peer = fields.StringField()
    peer_bridge = fields.StringField()
    mac_in_use = df_fields.MacAddressField()
    attached_mac = df_fields.MacAddressField()
    tunnel_type = fields.StringField()

    @classmethod
    def from_idl_row(cls, row):
        res = cls(
            id=str(row.uuid),
            name=row.name,
            type=_get_interface_type(row),
        )
        if row.ofport:
            res.ofport = int(row.ofport[0])

        if row.mac_in_use:
            res.mac_in_use = row.mac_in_use[0]

        if row.admin_state:
            res.admin_state = row.admin_state[0]

        if res.type == constants.OVS_PATCH_INTERFACE:
            res.peer = row.options['peer']
            res.peer_bridge = row.external_ids['peer_bridge']

        if res.type == constants.OVS_TUNNEL_INTERFACE:
            res.tunnel_type = row.type

        external_ids = row.external_ids
        lport_id = external_ids.get('iface-id')
        if lport_id is not None:
            res.lport = lport_id

        attached_mac = external_ids.get('attached-mac')
        if attached_mac is not None:
            res.attached_mac = attached_mac

        return res
Exemple #3
0
class AllowedAddressPairsActivePort(mf.ModelBase, mixins.Topic,
                                    mixins.BasicEvents):
    table_name = "activeport"
    ip = df_fields.IpAddressField()
    network = df_fields.ReferenceField(l2.LogicalSwitch)
    detected_mac = df_fields.MacAddressField()
    detected_lport = df_fields.ReferenceField(l2.LogicalPort)
Exemple #4
0
class LogicalPort(mf.ModelBase, mixins.Name, mixins.Version, mixins.Topic,
                  mixins.UniqueKey, mixins.BasicEvents):
    table_name = "lport"
    ips = df_fields.ListOfField(df_fields.IpAddressField())
    subnets = df_fields.ReferenceListField(Subnet)
    macs = df_fields.ListOfField(df_fields.MacAddressField())
    enabled = fields.BoolField()
    binding = fields.EmbeddedField(PortBinding)
    lswitch = df_fields.ReferenceField(LogicalSwitch)
    security_groups = df_fields.ReferenceListField(secgroups.SecurityGroup)
    allowed_address_pairs = fields.ListField(AddressPair)
    port_security_enabled = fields.BoolField()
    device_owner = fields.StringField()
    device_id = fields.StringField()
    qos_policy = df_fields.ReferenceField(qos.QosPolicy)
    dhcp_params = fields.EmbeddedField(DhcpParams)
    binding_vnic_type = df_fields.EnumField(portbindings.VNIC_TYPES)

    @property
    def ip(self):
        try:
            return self.ips[0]
        except IndexError:
            return None

    @property
    def mac(self):
        try:
            return self.macs[0]
        except IndexError:
            return None

    @property
    def is_local(self):
        return port_locator.is_port_local(self)

    @property
    def is_remote(self):
        return port_locator.is_port_remote(self)

    @property
    def all_ips(self):
        ips = set(self.ips)
        ips.update(pair.ip_address for pair in self.allowed_address_pairs)
        return ips

    def __str__(self):
        data = {}
        for name in dir(self):
            if name.startswith('_'):
                continue
            cls_definition = getattr(self.__class__, name, None)
            if isinstance(cls_definition, fields.BaseField):
                if name in self._set_fields:
                    data[name] = getattr(self, name)
            elif not cls_definition:  # Display only instnaces, not classes
                data[name] = getattr(self, name)
        return str(data)
Exemple #5
0
class LogicalSimplePort(mf.ModelBase, mixins.Name, mixins.BasicEvents):
    table_name = "lport"
    port_num = fields.IntField(False)
    ips = df_fields.ListOfField(df_fields.IpAddressField())
    subnets = df_fields.ReferenceListField(Subnet)
    macs = df_fields.ListOfField(df_fields.MacAddressField())
    enabled = fields.BoolField()
    lswitch = df_fields.ReferenceField(LogicalSwitch)
    qos_policy = df_fields.ReferenceField(qos.QosPolicy)

    @property
    def ip(self):
        try:
            return self.ips[0]
        except IndexError:
            return None

    @property
    def mac(self):
        try:
            return self.macs[0]
        except IndexError:
            return None

    def __str__(self):
        data = {}
        for name in dir(self):
            if name.startswith('_'):
                continue
            cls_definition = getattr(self.__class__, name, None)
            if isinstance(cls_definition, fields.BaseField):
                if name in self._set_fields:
                    data[name] = getattr(self, name)
            elif not cls_definition:  # Display only instnaces, not classes
                data[name] = getattr(self, name)
        return str(data)
Exemple #6
0
class AddressPair(models.Base):
    ip_address = df_fields.IpAddressField(required=True)
    mac_address = df_fields.MacAddressField(required=True)
Exemple #7
0
class LogicalPort(mf.ModelBase, mixins.Name, mixins.Version, mixins.Topic,
                  mixins.UniqueKey):
    table_name = "lport"
    ips = df_fields.ListOfField(df_fields.IpAddressField())
    subnets = df_fields.ReferenceListField(Subnet)
    macs = df_fields.ListOfField(df_fields.MacAddressField())
    enabled = fields.BoolField()
    chassis = df_fields.ReferenceField(core.Chassis)
    lswitch = df_fields.ReferenceField(LogicalSwitch)
    security_groups = df_fields.ReferenceListField(secgroups.SecurityGroup)
    allowed_address_pairs = fields.ListField(AddressPair)
    port_security_enabled = fields.BoolField()
    device_owner = fields.StringField()
    device_id = fields.StringField()
    qos_policy = df_fields.ReferenceField(qos.QosPolicy)
    remote_vtep = fields.BoolField()
    extra_dhcp_options = df_fields.IntStringDictField()
    binding_vnic_type = df_fields.EnumField(portbindings.VNIC_TYPES)

    def __init__(self,
                 ofport=None,
                 is_local=None,
                 peer_vtep_address=None,
                 **kwargs):
        super(LogicalPort, self).__init__(**kwargs)
        self.ofport = ofport
        self.is_local = is_local
        self.peer_vtep_address = peer_vtep_address

    @property
    def ip(self):
        try:
            return self.ips[0]
        except IndexError:
            return None

    @property
    def mac(self):
        try:
            return self.macs[0]
        except IndexError:
            return None

    def is_vm_port(self):
        """
        Return True if the device owner starts with 'compute:' (or is None)
        """
        owner = self.device_owner
        if not owner or owner.startswith(n_const.DEVICE_OWNER_COMPUTE_PREFIX):
            return True
        return False

    def __str__(self):
        data = {}
        for name in dir(self):
            if name.startswith('_'):
                continue
            cls_definition = getattr(self.__class__, name, None)
            if isinstance(cls_definition, fields.BaseField):
                if name in self._set_fields:
                    data[name] = getattr(self, name)
            elif not cls_definition:  # Display only instnaces, not classes
                data[name] = getattr(self, name)
        return str(data)

    def emit_created(self):
        ofport = getattr(self, 'ofport', None)
        if not ofport:
            return
        is_local = getattr(self, 'is_local', None)
        LOG.info("Adding new logical port = %s", self)
        if is_local:
            self.emit_local_created()
        else:
            self.emit_remote_created()

    def emit_updated(self, original_lport):
        ofport = getattr(self, 'ofport', None)
        if not ofport:
            return
        is_local = getattr(self, 'is_local', None)
        LOG.info(
            "Updating %(location)s logical port = %(port)s, "
            "original port = %(original_port)s", {
                'port': self,
                'original_port': original_lport,
                'location': 'local' if is_local else 'remote'
            })
        if is_local:
            self.emit_local_updated(original_lport)
        else:
            self.emit_remote_updated(original_lport)

    def emit_deleted(self):
        is_local = getattr(self, 'is_local', None)
        ofport = getattr(self, 'ofport', None)
        if not ofport:
            return
        if is_local:
            self.emit_local_deleted()
        else:
            self.emit_remote_deleted()