class RouteAttributes(KeyedSubAttributes): def __init__(self, parent, key): self.route = key super().__init__(parent) # InterfaceAttributes class InterfaceAttributes(KeyedSubAttributes): def __init__(self, parent, key): self.interface = key super().__init__(parent) interface_attr = managedattribute( name='interface_attr', read_only=True, doc=InterfaceAttributes.__doc__) @interface_attr.initter def interface_attr(self): return SubAttributesDict(self.InterfaceAttributes, parent=self) # NextHopAttributes class NextHopAttributes(KeyedSubAttributes): def __init__(self, parent, key): self.nexthop = key super().__init__(parent) next_hop_attr = managedattribute( name='next_hop_attr', read_only=True, doc=NextHopAttributes.__doc__) @next_hop_attr.initter def next_hop_attr(self): return SubAttributesDict(self.NextHopAttributes, parent=self)
class InterfaceAttributes(InterfaceSubAttributes): class InstanceAttributes(KeyedSubAttributes): def __init__(self, parent, key): self.mag_id = key super().__init__(parent) instance_attr = managedattribute( name='instance_attr', read_only=True, doc=InstanceAttributes.__doc__) @instance_attr.initter def instance_attr(self): return SubAttributesDict(self.InstanceAttributes, parent=self)
class CeAttributes(KeyedSubAttributes): @classmethod def _sanitize_key(cls, key): return int(key) ce_id = managedattribute(name='ce_id', read_only=True) # read-only key interfaces = managedattribute( name='interfaces', finit=typedset( managedattribute.test_isinstance(Interface)).copy, type=typedset( managedattribute.test_isinstance( Interface))._from_iterable) def add_interface(self, intf): # TODO DEPRECATE self.interfaces.add(intf) def remove_interface(self, intf): # TODO DEPRECATE self.interfaces.remove(intf) class InterfaceAttributes(InterfaceSubAttributes): remote_ce_id = None #Always only one per interface # interface GigabitEthernet0/0/1/0 remote-ce-id 2000 # !!% Invalid argument: AC already used by existing xconnect interface_attr = None # InterfaceAttributes def __init__(self, parent, key): self._ce_id = key super().__init__(parent=parent) self.interface_attr = SubAttributesDict( self.InterfaceAttributes, parent=self)
class EFPInterface(SubInterface): service_instance = managedattribute(name='service_instance', read_only=True) # mandatory rewrite_ingress = managedattribute(name='rewrite_ingress', type=str) rewrite_egress = managedattribute(name='rewrite_egress', type=str) def _build_config_create_interface_submode_context(self, configurations): @contextlib.contextmanager def multiple_submode_context(): with configurations.submode_context('interface {}'.format( self.parent_interface.name), cancel_empty=True): with configurations.submode_context( 'service instance {} ethernet'.format( self.service_instance)): yield return multiple_submode_context() def __init__(self, *args, service_instance, **kwargs): self._service_instance = int(service_instance) super().__init__(*args, **kwargs) def _build_config_interface_submode(self, configurations, attributes, unconfig): super()._build_config_interface_submode(configurations, attributes, unconfig) configurations.append_line( attributes.format('rewrite ingress tag {rewrite_ingress}')) configurations.append_line( attributes.format('rewrite egress tag {rewrite_egress}'))
class PrefixSidMapEntry(Base): prefix = managedattribute( name='prefix', default=None, type=(None, managedattribute.test_istype(IPv4Network))) index = managedattribute( name='index', default=None, type=(None, managedattribute.test_istype(int))) range = managedattribute( name='range', default=None, type=(None, managedattribute.test_istype(int))) attach = managedattribute( name='attach', default=None, type=(None, managedattribute.test_istype(bool))) def __hash__(self): return hash((self.prefix,self.index,self.range,self.attach))
class DefaultInterfaceEthernetSegmentAttributes( DeviceNamespace, SubAttributes): class BgpAttributes(DeviceNamespace, SubAttributes): def __init__(self, base): super().__init__( base=base, # Evpn.ethernet_segment.bgp parent=base.parent.ethernet_segment.bgp) bgp = managedattribute(name='bgp', read_only=True, doc=BgpAttributes.__doc__) @bgp.initter def bgp(self): return self.BgpAttributes(base=self.base) esi = managedattribute(name='esi', default=None, type=(None, ESI)) def __init__(self, base): super().__init__( base=base, # Evpn.ethernet_segment parent=base.parent.ethernet_segment)
class DeviceAttributes(genie.conf.base.attributes.DeviceSubAttributes): class InterfaceAttributes( genie.conf.base.attributes.InterfaceSubAttributes): def __init__(self, **kwargs): super().__init__(**kwargs) interface_attr = managedattribute(name='interface_attr', read_only=True, doc=InterfaceAttributes.__doc__) @interface_attr.initter def interface_attr(self): return SubAttributesDict(self.InterfaceAttributes, parent=self) def __init__(self, **kwargs): super().__init__(**kwargs)
class DeviceAttributes(DeviceSubAttributes): class DomainAttributes(KeyedSubAttributes): def __init__(self, parent, key): self.domain_id = key super().__init__(parent=parent) domain_attr = managedattribute( name='domain_attr', read_only=True, doc=DomainAttributes.__doc__) @domain_attr.initter def domain_attr(self): return SubAttributesDict( self.DomainAttributes, parent=self)
class Hltapi(object): '''HLTAPI abstraction object. HLTAPI Device subclasses are encouraged to subclass Hltapi as well to customize HLTAPI calls to allow Vendor-specific requirements. ''' device = managedattribute( name='device', type=managedattribute.auto_ref, # TODO Device is not finished declaring yet gettype=managedattribute.auto_unref, doc='''The HLTAPI-based Genie Device object''') @property def pyats_connection(self): '''The pyATS connection used for HLTAPI access''' connectionmgr = self.device.connectionmgr try: return connectionmgr.connections['hltapi'] except KeyError: # TODO This might not be a HltApiConnection!? return connectionmgr.connections[connectionmgr.default_alias] @property def tcl(self): '''The Tcl interpreter instance.''' return self.pyats_connection._tcl @property def tcl_namespace(self): '''The Tcl namespace where HLTAPI vendor code is loaded.''' return self.pyats_connection._ns def __getattr__(self, name): '''Redirect to undefined attributes to the pyATS connection.''' if not name.startswith('_') and name != 'device': return getattr(self.pyats_connection, name) f = getattr(super(), '__getattr__', None) if f is not None: return f(name) else: raise AttributeError(name) def __init__(self, device): self.device = device super().__init__()
class PhysicalInterface(Interface, genie.libs.conf.interface.PhysicalInterface): '''Class for physical TGEN interfaces/ports''' tgen_port_configured = managedattribute( name='tgen_port_configured', default=False, type=managedattribute.test_istype(bool)) @property def tgen_interface(self): '''Return the physical TGEN interface (self)''' return self @abc.abstractmethod def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs)
class LocatorSetAttributes(KeyedSubAttributes): def __init__(self, parent, key): self.locator_set_name = key super().__init__(parent) # +- DeviceAttributes # +- RouterInstanceAttributes # +- LocatorSetAttributes # +- InterfaceAttributes class InterfaceAttributes(KeyedSubAttributes): def __init__(self, parent, key): self.ls_interface = key super().__init__(parent) # +- DeviceAttributes # +- RouterInstanceAttributes # +- LocatorSetAttributes # +- InterfaceAttributes # +- InterfaceTypeAttributes class InterfaceTypeAttributes(KeyedSubAttributes): def __init__(self, parent, key): assert key in ['ipv4', 'ipv6', 'ethernet'],\ "'{key}' is not supported for locator_set_intf_type_attr, only 'ipv4' and 'ipv6' are supported".format(key=key) self.ls_interface_type = key super().__init__(parent) locator_set_intf_type_attr = managedattribute( name='locator_set_intf_type_attr', read_only=True, doc=InterfaceTypeAttributes.__doc__) @locator_set_intf_type_attr.initter def locator_set_intf_type_attr(self): return SubAttributesDict(self.InterfaceTypeAttributes, parent=self) locator_set_intf_attr = managedattribute( name='InterfaceAttributes', read_only=True, doc=InterfaceAttributes.__doc__) @locator_set_intf_attr.initter def locator_set_intf_attr(self): return SubAttributesDict(self.InterfaceAttributes, parent=self)
class KeyChainMacSecAttributes(KeyedSubAttributes): def __init__(self, parent, key): self.ms_key_chain = key super().__init__(parent) # KeyIdAttributes class KeyIdAttributes(KeyedSubAttributes): def __init__(self, parent, key): self.key_id = key super().__init__(parent) key_id_attr = managedattribute(name='key_id_attr', read_only=True, doc=KeyIdAttributes.__doc__) @key_id_attr.initter def key_id_attr(self): return SubAttributesDict(self.KeyIdAttributes, parent=self)
class InterfaceAttributes(KeyedSubAttributes): def __init__(self, parent, key): self.interface = key super().__init__(parent) # StaticArpAttributes class StaticArpAttributes(KeyedSubAttributes): def __init__(self, parent, key): self.if_static_ip_address = key super().__init__(parent) static_arp_attr = managedattribute(name='static_arp_attr', read_only=True, doc=StaticArpAttributes.__doc__) @static_arp_attr.initter def static_arp_attr(self): return SubAttributesDict(self.StaticArpAttributes, parent=self)
class VrfAttributes(KeyedSubAttributes): def __init__(self, key, *args, **kwargs): self.vrf_name = key super().__init__(*args, **kwargs) # PeerAttribute class PeerAttributes(KeyedSubAttributes): def __init__(self, key, *args, **kwargs): self.address = key super().__init__(*args, **kwargs) peer_attr = managedattribute(name='peer_attr', read_only=True, doc=PeerAttributes.__doc__) @peer_attr.initter def peer_attr(self): return SubAttributesDict(self.PeerAttributes, parent=self)
class InterfaceAttributes(InterfaceSubAttributes): # Interface Static Neighbor multi-line configs intf_staticnbr_keys = managedattribute( name='intf_staticnbr_keys', finit=typedset(managedattribute.test_isinstance(InterfaceStaticNeighbor)).copy, type=typedset(managedattribute.test_isinstance(InterfaceStaticNeighbor))._from_iterable, doc='A `set` of InterfaceStaticNeighbor keys objects') def add_staticnbr_key(self, intf_staticnbr_key): self.intf_staticnbr_keys.add(intf_staticnbr_key) def remove_staticnbr_key(self, intf_staticnbr_key): intf_staticnbr_key._device = None try: self.intf_staticnbr_keys.remove(intf_staticnbr_key) except: pass
class VniAttributes(KeyedSubAttributes): def __init__(self, parent,key): self.evpn_vni = key super().__init__(parent) # RouteAttributes class RouteTargetAttributes(KeyedSubAttributes): def __init__(self, key, *args, **kwargs): self.evpn_vni_rt = key super().__init__(*args, **kwargs) route_target_attr = managedattribute( name='route_target_attr', read_only=True, doc=RouteTargetAttributes.__doc__) @route_target_attr.initter def route_target_attr(self): return SubAttributesDict(self.RouteTargetAttributes, parent=self)
class SiteAttributes(KeyedSubAttributes): def __init__(self, parent, key): self.ms_site_id = key super().__init__(parent) # +- DeviceAttributes # +- RouterInstanceAttributes # +- SiteAttributes # +- InstanceIdAttributes class InstanceIdAttributes(KeyedSubAttributes): def __init__(self, parent, key): self.site_inst_id = key super().__init__(parent) # +- DeviceAttributes # +- RouterInstanceAttributes # +- SiteAttributes # +- InstanceIdAttributes # +- EidRecordAttributes class EidRecordAttributes(KeyedSubAttributes): def __init__(self, parent, key): self.ms_eid_id = key super().__init__(parent) eid_record_attr = managedattribute( name='eid_record_attr', read_only=True, doc=EidRecordAttributes.__doc__) @eid_record_attr.initter def eid_record_attr(self): return SubAttributesDict(self.EidRecordAttributes, parent=self) site_inst_id_attr = managedattribute( name='site_inst_id_attr', read_only=True, doc=InstanceIdAttributes.__doc__) @site_inst_id_attr.initter def site_inst_id_attr(self): return SubAttributesDict(self.InstanceIdAttributes, parent=self)
class InterfaceAttributes(InterfaceSubAttributes): # +- DeviceAttributes # +- InterfaceAttributes # +- MobilityDynamicEidAttributes class MobilityDynamicEidAttributes(KeyedSubAttributes): def __init__(self, parent, key): self.if_mobility_dynamic_eid_name = key super().__init__(parent) mobility_dynamic_eid_attr = managedattribute( name='mobility_dynamic_eid_attr', read_only=True, doc=MobilityDynamicEidAttributes.__doc__) @mobility_dynamic_eid_attr.initter def mobility_dynamic_eid_attr(self): return SubAttributesDict(self.MobilityDynamicEidAttributes, parent=self)
class BaseNamespace(Base): '''A simple namespace that inherits some attributes from a Base-like object. Attributes inherited: - testbed ''' base = managedattribute( name='base', read_only=True, doc='''Object that is either a Base or inherits from it.''') def __init__(self, base, **kwargs): self._base = base super().__init__(**kwargs) @property def testbed(self): return self.base.testbed
class InterfaceAttributes(KeyedSubAttributes): def __init__(self, parent, key): self.interface = key super().__init__(parent=parent) # NeighborAttribute class NeighborAttributes(KeyedSubAttributes): def __init__(self, parent, key): self.ip = key super().__init__(parent) neighbor_attr = managedattribute( name='neighbor_attr', read_only=True, doc=NeighborAttributes.__doc__) @neighbor_attr.initter def neighbor_attr(self): return SubAttributesDict(self.NeighborAttributes, parent=self)
class PrefixAttributes(KeyedSubAttributes): def __init__(self, parent, key): self.prefix = key super().__init__(parent) class MaxLengthRangeAttributes(KeyedSubAttributes): def __init__(self, parent, key): self.maxlength_range = key super().__init__(parent) maxlength_range_attr = managedattribute( name='maxlength_range_attr', read_only=True, doc=MaxLengthRangeAttributes.__doc__) @maxlength_range_attr.initter def maxlength_range_attr(self): return SubAttributesDict(self.MaxLengthRangeAttributes, parent=self)
class InterfaceAttributes(InterfaceSubAttributes): groups = managedattribute( name='groups', finit=typedset( managedattribute.test_isinstance(MldGroup)).copy, type=typedset(managedattribute.test_isinstance( MldGroup))._from_iterable, doc='A `set` of MldGroup associated objects') def add_groups(self, groups): self.groups.add(groups) def remove_groups(self, groups): groups._device = None try: self.groups.remove(groups) except: pass
class VlanAttributes(KeyedSubAttributes): def __init__(self, parent, key): self.vlan_id = key super().__init__(parent) class MacAddressAttributes(KeyedSubAttributes): def __init__(self, parent, key): self.mac_address = key super().__init__(parent) mac_address_attr = managedattribute( name='mac_address_attr', read_only=True, doc=MacAddressAttributes.__doc__) @mac_address_attr.initter def mac_address_attr(self): return SubAttributesDict(self.MacAddressAttributes, parent=self)
class RouteTargetAttributes(KeyedSubAttributes): def __init__(self, parent, key): self.rt = key super().__init__(parent) # ProtocolAttribute class ProtocolAttributes(KeyedSubAttributes): def __init__(self, key, *args, **kwargs): self.protocol = key super().__init__(*args, **kwargs) protocol_attr = managedattribute( name='protocol_attr', read_only=True, doc=ProtocolAttributes.__doc__) @protocol_attr.initter def protocol_attr(self): return SubAttributesDict(self.ProtocolAttributes, parent=self)
class PseudowireIPNeighbor(PseudowireNeighbor, IPNeighbor): pw_id = managedattribute(name='pw_id', default=None, type=(None, managedattribute.test_istype(int))) def __new__(cls, *args, **kwargs): factory_cls = cls if cls is PseudowireIPNeighbor: try: ip = kwargs['ip'] except KeyError: raise TypeError('\'ip\' argument missing') ip = ipaddress.ip_address(ip) if isinstance(ip, ipaddress.IPv4Address): factory_cls = PseudowireIPv4Neighbor elif isinstance(ip, ipaddress.IPv6Address): factory_cls = PseudowireIPv6Neighbor else: raise ValueError(ip) if factory_cls is not cls: self = factory_cls.__new__(factory_cls, *args, **kwargs) elif super().__new__ is object.__new__: self = super().__new__(factory_cls) else: self = super().__new__(factory_cls, *args, **kwargs) return self def __repr__(self): s = '<{}'.format(self.__class__.__name__, ) s += ' {}:{}'.format( self.ip, self.pw_id, ) s += '>' return s def __hash__(self): return hash((self.device, self.container, repr(self)))
class DeviceAttributes(DeviceSubAttributes): class AclAttributes(KeyedSubAttributes): def __init__(self, parent, key): self.acl_name = key super().__init__(parent) class InterfaceAttributes(InterfaceSubAttributes): def __init__(self, parent, key): self.interface_id = key super().__init__(parent, key) interface_attr = managedattribute(name='interface_attr', read_only=True, doc=InterfaceAttributes.__doc__) @interface_attr.initter def interface_attr(self): return SubAttributesDict(self.InterfaceAttributes, parent=self) class AceAttributes(KeyedSubAttributes): def __init__(self, parent, key): self.seq = key super().__init__(parent) ace_attr = managedattribute(name='ace_attr', read_only=True, doc=AceAttributes.__doc__) @ace_attr.initter def ace_attr(self): return SubAttributesDict(self.AceAttributes, parent=self) acl_attr = managedattribute(name='acl_attr', read_only=True, doc=AclAttributes.__doc__) @acl_attr.initter def acl_attr(self): return SubAttributesDict(self.AclAttributes, parent=self)
class InterfaceAttributes(InterfaceSubAttributes): def __init__(self, parent, key): self.intf = key super().__init__(parent, key) groups = managedattribute( name='groups', finit=typedset( managedattribute.test_isinstance(IgmpGroup)).copy, type=typedset(managedattribute.test_isinstance( IgmpGroup))._from_iterable, doc='A `set` of IgmpGroup associated objects') def add_groups(self, groups): self.groups.add(groups) def remove_groups(self, groups): groups._device = None try: self.groups.remove(groups) except: pass
class RouteTargetImportExport(object): route_target = managedattribute( name='route_target', read_only=True) # (read-only hash key) stitching = False def __init__(self, route_target, *, stitching=None): if stitching is None and isinstance(route_target, RouteTargetImportExport): # copy constructor route_target, stitching = \ route_target.route_target, \ route_target.stitching self._route_target = RouteTarget(route_target) self.stitching = bool(stitching) def __eq__(self, other): if not isinstance(other, RouteTargetImportExport): return NotImplemented return (self.route_target, self.stitching) \ == (other.route_target, other.stitching) def __lt__(self, other): if not isinstance(other, RouteTargetImportExport): return NotImplemented return (self.route_target, self.stitching) \ < (other.route_target, other.stitching) def __hash__(self): # return hash((self.route_target, self.stitching)) return hash(self.route_target) def __repr__(self): return '%s(%r%s)' % ( self.__class__.__name__, self.route_target, ', stitching=%r' % (self.stitching,) if self.stitching else '')
class PseudowireNeighborAttributes(PseudowireNeighborSubAttributes): class EthernetSegmentAttributes(PseudowireNeighborNamespace, SubAttributes): esi = managedattribute(name='esi', default=None, type=(None, ESI)) class BgpAttributes(PseudowireNeighborNamespace, SubAttributes): def __init__(self, base): super().__init__( base=base, # Evpn.device_attr[].ethernet_segment.bgp parent=base.parent.ethernet_segment.bgp) bgp = managedattribute(name='bgp', read_only=True, doc=BgpAttributes.__doc__) @bgp.initter def bgp(self): return self.BgpAttributes(base=self.base) def __init__(self, base): super().__init__( base=base, # Evpn.device_attr[].ethernet_segment parent=base.parent.ethernet_segment) ethernet_segment = managedattribute( name='ethernet_segment', read_only=True, doc=EthernetSegmentAttributes.__doc__) @ethernet_segment.initter def ethernet_segment(self): return self.EthernetSegmentAttributes(base=self) def __init__(self, parent, key): super().__init__(parent, key)
class VniSubAttributes(KeyedSubAttributes): vni_id = managedattribute(name='vni_id', read_only=True) # Key @property def vni(self): for vni in self.parent.vnis: if vni.vni_id == vni_id: return vni raise AttributeError('vni: no Vni found matching vni_id=%r' % (vni_id, )) @classmethod def _sanitize_key(cls, key): if isinstance(key, Vni): return key.vni_id if isinstance(key, (str, int)): return int(key) return key def __init__(self, parent, key): self._vni_id = int(key) super().__init__(parent=parent)