-
Notifications
You must be signed in to change notification settings - Fork 53
/
interface.py
1593 lines (1266 loc) · 63 KB
/
interface.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'''
Interface classes for nxos OS.
'''
__all__ = (
'Interface',
'PhysicalInterface',
'VirtualInterface',
'PseudoInterface',
'LoopbackInterface',
'EthernetInterface',
'SubInterface',
'VlanInterface',
'PortchannelInterface',
'NveInterface',
)
import re
import abc
from abc import ABC
from enum import Enum
from genie.conf.base.config import CliConfig
from genie.decorator import managedattribute
from genie.conf.base import ConfigurableBase
from genie.conf.base.exceptions import UnknownInterfaceTypeError
from genie.conf.base.attributes import SubAttributes, SubAttributesDict,\
AttributesHelper, UnsupportedAttributeWarning
from genie.conf.base.cli import CliConfigBuilder
from genie.libs.conf.base import \
MAC, \
IPv4Address, IPv4Interface, \
IPv6Address, IPv6Interface
import genie.libs.conf.interface
from genie.libs.conf.interface import Layer, L2_type
class ConfigurableInterfaceNamespace(ConfigurableBase):
def __init__(self, interface=None):
assert interface
self._interface = interface
_interface = None
@property
def interface(self):
return self._interface
@property
def testbed(self):
return self.interface.testbed
@property
def device(self):
return self.interface.device
class Option(Enum):
def __str__(self):
return str(self.value)
class Duplex(Option):
# Create the same for Half, Full or Auto
# This way it will remove the confusion of which name to use
half = 'half'
full = 'full'
auto = 'auto'
class Interface(genie.libs.conf.interface.Interface):
'''Base class for NX-OS interfaces.
Args:
name (str): Interface name.
This is a factory class that instantiates proper subclasses chosen
based on `name`.
'''
def __new__(cls, *args, **kwargs):
factory_cls = cls
if cls is Interface:
try:
name = kwargs['name']
except KeyError:
raise TypeError('\'name\' argument missing')
d_parsed = genie.libs.conf.interface.ParsedInterfaceName(
name, kwargs.get('device', None))
if d_parsed.subintf:
factory_cls = SubInterface
else:
try:
factory_cls = cls._name_to_class_map[d_parsed.type]
except KeyError:
pass
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
layer = Layer.L3
priority_flow_control_mode = None
switchport_mode = None
sw_acc_vlan = None
sw_trunk_encap = None
sw_trunk_allowed_vlan = None
sw_trunk_native_vlan = None
sw_trunk_prun_vlan = None
switchport_enable = None
rip = None # XXXJST TODO
def build_config(self, apply=True, attributes=None, unconfig=False,
**kwargs):
"""method to build the configuration of an interface object
Api to build the configuration of an interface object. This
configuration depends of the configurable attributes of this object.
Args:
None
Return:
`str`
Examples:
>>> from genie.libs.conf.interface.nxos import Interface
Create a Interface obj
>>> eth = Interface(name='ethernet3/1')
assign configurable attributes to the interface obj
>>> eth.description = 'ethernet3/1'
>>> eth.shutdown = False
Build configuration
>>> configuration = eth.build_config()
"""
assert not kwargs, kwargs
attributes = AttributesHelper(self, attributes)
configurations = CliConfigBuilder(unconfig=unconfig)
with self._build_config_create_interface_submode_context(\
configurations):
self._build_config_interface_submode(\
configurations=configurations, \
attributes=attributes, unconfig=unconfig)
if apply:
if configurations:
self.device.configure(configurations, fail_invalid=True)
else:
return CliConfig(device=self.device, unconfig=unconfig,
cli_config=configurations)
def build_unconfig(self, apply=True, attributes=None, **kwargs):
return self.build_config(apply=apply,
attributes=attributes,
unconfig=True, **kwargs)
def _build_config_create_interface_submode_context(self, configurations):
# -- ETHER, PC, VLAN, LO, MGMT
# nxos: interface <intf> (config-if)
# -- SUB
# nxos: interface <intf> (config-subif)
# -- NVE
# nxos: interface <intf> (config-if-nve)
return configurations.submode_context('interface {}'.format(self.name))
def _build_config_interface_submode(self, configurations, \
attributes, unconfig):
# -- L3, SUB, PC, VLAN
# nxos: interface <intf> / medium broadcast
# nxos: interface <intf> / medium p2p
configurations.append_line(
attributes.format('medium {medium}'),
unconfig_cmd='no medium')
if attributes.value('cdp'):
configurations.append_line(
attributes.format('cdp enable'),
unconfig_cmd='no cdp')
# bandwidth
configurations.append_line(
attributes.format('bandwidth {bandwidth}'),
unconfig_cmd='no bandwidth')
configurations.append_line(
attributes.format('delay {delay}'),
unconfig_cmd='no delay')
v = attributes.value('description')
if v:
if v is True:
pass # TODO Create a usefull default description
configurations.append_line('description {}'.format(v),
unconfig_cmd='no description')
# mtu
configurations.append_line(
attributes.format('mtu {mtu}'),
unconfig_cmd='no mtu')
# enabled
enabled = attributes.value('enabled')
if enabled is not None:
if enabled:
config_cmd = 'no shutdown'
unconfig_cmd = 'shutdown'
else:
config_cmd = 'shutdown'
unconfig_cmd = 'no shutdown'
configurations.append_line(
attributes.format(config_cmd),
unconfig_cmd=unconfig_cmd)
# Compatibility
else:
shutdown = attributes.value('shutdown')
if shutdown is not None:
if unconfig:
# Special case: unconfiguring always applies shutdown
configurations.append_line('shutdown', raw=True)
elif shutdown:
configurations.append_line('shutdown', raw=True)
else:
configurations.append_line('no shutdown', raw=True)
# link_up_down_trap_enable
if attributes.value('link_up_down_trap_enable'):
configurations.append_line('snmp trap link-status')
# vrf
configurations.append_line(
attributes.format('vrf member {vrf.name}'))
# link_status
if attributes.value('link_status'):
configurations.append_line('logging event port link-status')
# ip address <ipv4> <prefix-length> [secondary] | [tag <route-tag>]
# ip address dhcp
# need to put 'if' with ipv4 address
if attributes.value('dhcp'):
configurations.append_line('ip address dhcp')
# ip unnumbered <unnumbered_intf_ref>
configurations.append_line(
attributes.format(
attributes.format('ip unnumbered {unnumbered_intf_ref}'),
unconfig_cmd='no ip unnumbered'))
# N/A : ipv6_unnumbered_intf_ref
# N/A : ipv6_enabled
# ipv6 address autoconfig [default]
ipv6_autoconf = attributes.value('ipv6_autoconf')
ipv6_autoconf_default = attributes.value('ipv6_autoconf_default')
if ipv6_autoconf is not None:
cmd = 'ipv6 address autoconfig'
if ipv6_autoconf:
if ipv6_autoconf_default:
cmd += ' default'
configurations.append_line(cmd)
# enable switchport configuration when attribute is True
if not re.match('[V|v]lan', self.name):
if attributes.value('switchport_enable'):
configurations.append_line(
attributes.format('switchport'))
if attributes.value('switchport_enable') == False:
configurations.append_line(
attributes.format('no switchport'),
unconfig_cmd='switchport')
# configure switchport realted configurations
configurations.append_line(
attributes.format('switchport mode {switchport_mode}'))
if self.switchport_mode == L2_type.ACCESS:
configurations.append_line(
attributes.format('switchport access vlan {sw_acc_vlan}'))
elif self.switchport_mode == L2_type.TRUNK:
configurations.append_line(
attributes.format('switchport trunk encapsulation {sw_trunk_encap}'))
configurations.append_line(
attributes.format('switchport trunk allowed vlan {sw_trunk_allowed_vlan}'))
configurations.append_line(
attributes.format('switchport trunk native vlan {sw_trunk_native_vlan}'))
configurations.append_line(
attributes.format('switchport trunk pruning vlan {sw_trunk_prun_vlan}'))
# configure layer3 related configurations
configurations.append_line(
attributes.format('ip address {ipv4.ip} {ipv4.netmask}'),
unconfig_cmd='no ip address')
# XXXJST TODO move to Rip
if self.ipv4:
configurations.append_line(
attributes.format('ip router rip {rip.instance_id}'))
configurations.append_line(
attributes.format('ipv6 address {ipv6.with_prefixlen}'),
unconfig_cmd='no ipv6 address')
# XXXJST TODO move to Rip
if self.ipv6:
configurations.append_line(
attributes.format('ipv6 router rip {rip.instance_id}'))
# load-interval
configurations.append_line(
attributes.format('load-interval {load_interval}'),
unconfig_cmd='no load-interval')
configurations.append_line(
attributes.format('load-interval counter {load_interval_counter}'),
unconfig_cmd='no load-interval counter')
configurations.append_line(
attributes.format('flowcontrol receive {flowcontrol_receive}'),
unconfig_cmd='no flowcontrol receive')
configurations.append_line(
attributes.format('flowcontrol send {flowcontrol_send}'),
unconfig_cmd='no flowcontrol send')
configurations.append_line(
attributes.format('priority-flow-control mode \
{priority_flow_control_mode}'))
# evpn_multisite_dci_tracking
if attributes.value('evpn_multisite_dci_tracking'):
configurations.append_line(
attributes.format('evpn multisite dci-tracking'),
unconfig_cmd='no evpn multisite dci-tracking')
# evpn_multisite_fabric_tracking
if attributes.value('evpn_multisite_fabric_tracking'):
configurations.append_line(
attributes.format('evpn multisite fabric-tracking'),
unconfig_cmd='no evpn multisite fabric-tracking')
# ip forward
if attributes.value('ip_forward'):
configurations.append_line(
attributes.format('ip forward'),
unconfig_cmd='no ip forward')
# IPv4Addr attributes
for ipv4addr, attributes2 in attributes.sequence_values(
'ipv4addr', sort=True):
if unconfig:
configurations.append_block(ipv4addr.build_unconfig(
apply=False, attributes=attributes2))
else:
configurations.append_block(ipv4addr.build_config(
apply=False, attributes=attributes2))
# IPv6Addr attributes
for ipv6addr, attributes2 in attributes.sequence_values(
'ipv6addr', sort=True):
if unconfig:
configurations.append_block(ipv6addr.build_unconfig(
apply=False, attributes=attributes2))
else:
configurations.append_block(ipv6addr.build_config(
apply=False, attributes=attributes2))
# -- ETHER L2, L3, PC
# nxos: interface <intf> / buffer-boost
def restore_default(self, apply=True):
"""method to reset the interface configuration to defaults
Api to reset the configutaion of an interface object.
Args:
None
Return:
`str`
Examples:
>>> from genie.libs.conf.interface.nxos import Interface
Create a Interface obj
>>> eth = Interface(name='ethernet3/1')
Reset configuration
>>> reset = eth.restore_default()
"""
configurations = CliConfigBuilder()
# name is a mandatory arguments
configurations.append_line('default interface {}'.format(self.name))
if apply:
if configurations:
self.device.configure(str(configurations))
else:
# Return configuration
return str(configurations)
@abc.abstractmethod
def __init__(self, *args, **kwargs):
'''Abstract constructor.'''
super().__init__(*args, **kwargs)
class PhysicalInterface(Interface, genie.libs.conf.interface.PhysicalInterface):
""" Physical interface class, presenting physical type of `Interface`
objects
`PhysicalInterface` class inherits from the `Interface` class.
It is the super class of and `EthernetInterface`
Args:
All the parameters/attributes inherits from its supper class
'Interface'
"""
@abc.abstractmethod
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def build_unconfig(self, apply=True, attributes=None, **kwargs):
# physical interfaces unconfigured
attributes_unconfig = AttributesHelper(self, attributes)
if attributes_unconfig.iswildcard:
configurations = CliConfigBuilder(unconfig=True)
configurations.append_line('default interface {}'.format(self.name),
raw=True)
if apply:
if configurations:
self.device.configure(str(configurations))
else:
# Return configuration
return configurations
else:
return self.build_config(apply=apply,
attributes=attributes,
unconfig=True, **kwargs)
def _build_config_interface_submode(self, configurations, attributes, unconfig):
super()._build_config_interface_submode(configurations, attributes, unconfig)
class VirtualInterface(Interface, genie.libs.conf.interface.VirtualInterface):
""" VirtualInterface class, presenting logical/virtual type of `Interface`
objects
`VirtualInterface` class inherits from the `Interface` class. It is the
super class of `LoopbackInterface`, 'PortchannelInterface`,
`VlanInterface` and `SubInterface`
Args:
All the parameters/attributes inherits from its super class
'Interface'
"""
def _build_config_interface_submode(self, configurations, attributes, unconfig):
# Virtual interfaces can be fully unconfigured
if unconfig and attributes.iswildcard:
configurations.submode_unconfig()
super()._build_config_interface_submode(configurations, attributes, unconfig)
@abc.abstractmethod
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
class PseudoInterface(VirtualInterface, genie.libs.conf.interface.PseudoInterface):
@abc.abstractmethod
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
class LoopbackInterface(VirtualInterface, genie.libs.conf.interface.LoopbackInterface):
""" LoopbackInterface class, presenting loopback type of `Interface`
objects
`LoopbackInterface` class inherits from the `VirtualInterface` class.
Args:
All the parameters/attrinutes inherits from its supper class
'Interface'
"""
_interface_name_types = (
'loopback',
'Loopback',
)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
class EthernetInterface(PhysicalInterface, genie.libs.conf.interface.EthernetInterface):
""" EthernetInterface class, presenting ethernet type of `Interface`
objects
`EthernetInterface` class inherits from the `PhysicalInterface` class.
Args:
All the parameters/attributes inherits from its supper class
'Interface'
Class variables:
type: interface type - ethernet
Examples:
>>> from genie.libs.conf.interface.nxos import Interface
Create a Interface obj
>>> eth = Interface(name='ethernet1/1')
assign configurable attributes to the interface obj
>>> eth.ipv4 = '10.1.1.1 255.255.255.0'
Build configuration
>>> configuration = eth.build_config()
"""
_interface_name_types = (
'ethernet',
'Ethernet',
'gigabitethernet',
'GigabitEthernet',
)
class SwitchportAttributes(ConfigurableInterfaceNamespace):
# nxos: interface <intf> / switchport
enabled = False
class Mode(Enum):
access = 'access'
dot1q_tunnel = 'dot1q-tunnel'
fex_fabric = 'fex-fabric'
trunk = 'trunk'
_mode = None
mode = managedattribute(name='mode', type=(None, Mode))
access_vlan = None # int
trunk_allowed_vlan = None # int
trunk_native_vlan = None # int
def build_config(self, apply=True, attributes=None, unconfig=False,
**kwargs):
assert not apply
assert not kwargs, kwargs
attributes = AttributesHelper(self, attributes)
configurations = CliConfigBuilder(unconfig=unconfig)
switchport_enabled = attributes.value('enabled')
if switchport_enabled is True:
configurations.append_block('switchport')
elif switchport_enabled is False:
if not unconfig:
configurations.append_block('no switchport')
if self.enabled:
# nxos: interface <intf> / switchport mode access
# nxos: interface <intf> / switchport mode dot1q-tunnel
# nxos: interface <intf> / switchport mode fex-fabric
# nxos: interface <intf> / switchport mode trunk
configurations.append_line('switchport mode {mode.value}')
if self.mode is None:
pass
elif self.mode == self.Mode.access:
# nxos: interface <intf> / switchport access vlan 1
configurations.append_line('switchport access vlan {access_vlan}')
elif self.mode == self.Mode.dot1q_tunnel:
pass
elif self.mode == self.Mode.fex_fabric:
pass
elif self.mode == self.Mode.trunk:
# nxos: interface <intf> / switchport trunk allowed vlan 1
configurations.append_line('switchport trunk allowed vlan {trunk_allowed_vlan}')
# nxos: interface <intf> / switchport trunk allowed vlan add 1
# nxos: interface <intf> / switchport trunk allowed vlan all
# nxos: interface <intf> / switchport trunk allowed vlan except 1
# nxos: interface <intf> / switchport trunk allowed vlan none
# nxos: interface <intf> / switchport trunk allowed vlan remove 1
# nxos: interface <intf> / switchport trunk native vlan 1
configurations.append_line('switchport trunk native vlan {trunk_native_vlan}')
# nxos: interface <intf> / switchport autostate exclude
# nxos: interface <intf> / switchport autostate exclude vlan 1
# nxos: interface <intf> / switchport autostate exclude vlan add 1
# nxos: interface <intf> / switchport autostate exclude vlan all
# nxos: interface <intf> / switchport autostate exclude vlan except 1
# nxos: interface <intf> / switchport autostate exclude vlan remove 1
# nxos: interface <intf> / switchport block multicast
# nxos: interface <intf> / switchport block unicast
# nxos: interface <intf> / switchport dot1q ethertype 0x8100
# nxos: interface <intf> / switchport dot1q ethertype 0x88A8
# nxos: interface <intf> / switchport dot1q ethertype 0x9100
# nxos: interface <intf> / switchport dot1q ethertype 0x600
# nxos: interface <intf> / switchport host
# nxos: interface <intf> / switchport isolated
# nxos: interface <intf> / switchport monitor
# nxos: interface <intf> / switchport vlan mapping 1 1
# nxos: interface <intf> / switchport vlan mapping 1 inner 1 1
# nxos: interface <intf> / switchport vlan mapping enable
return str(configurations)
def build_unconfig(self, apply=True, attributes=None, **kwargs):
return self.build_config(apply=apply, attributes=attributes, unconfig=True, **kwargs)
switchport = managedattribute(
name='switchport',
read_only=True,
doc=SwitchportAttributes.__doc__)
@switchport.initter
def switchport(self):
return EthernetInterface.SwitchportAttributes(interface=self)
mac_aging = None
auto_negotiate = None
def _build_config_interface_submode(self, configurations, attributes, unconfig):
"""internal method to build the configuration of a ethernet interface object
Please use build_config and build_unconfig.
Api to build the configuration of a ethernet interface object. This
configuration depends of the configurable attributes of this object.
Args:
None
Return:
`str`
Examples:
>>> from genie.libs.conf.interface.nxos import Interface
Create a Interface obj
>>> eth = Interface(name='ethernet3/1')
assign configurable attributes to the interface obj
>>> eth.description = 'ethernet3/1'
>>> eth.shutdown = False
>>> eth.vrf = <vrf object>
>>> eth.rip = <rip object>
>>> eth.speed = 100000
>>> eth.duplex = 'auto'
>>> eth.ipv4 = '10.0.0.1'
Build configuration
>>> configuration = eth.build_config()
"""
super()._build_config_interface_submode(configurations=configurations,
attributes=attributes,
unconfig=unconfig)
configurations.append_line(
attributes.format('duplex {duplex}'),
unconfig_cmd='no duplex')
configurations.append_line(
attributes.format('speed {speed}'),
unconfig_cmd='no speed')
configurations.append_line(
attributes.format('mac-address {mac_address}'),
unconfig_cmd='no mac-address')
# TODO auto_negotiation
# mac_address
configurations.append_line(
attributes.format('mac address-table aging-time {mac_aging}'),
unconfig_cmd='no mac address-table aging')
# speed <port_speed>
configurations.append_line(
attributes.format('speed {port_speed.value}'),
unconfig_cmd='no speed')
# access port
if self.switchport_mode == L2_type.ACCESS.name.lower():
# switchport access vlan <access_vlan>
configurations.append_line(
attributes.format('switchport access vlan {access_vlan}'),
unconfig_cmd='no switchport access vlan')
# trunk port
if self.switchport_mode == L2_type.TRUNK.name.lower():
# switchport trunk allowed vlan <trunk_vlans>
configurations.append_line(
attributes.format('switchport trunk allowed vlan {trunk_vlans}'),
unconfig_cmd='no switchport trunk allowed vlan')
# switchport trunk allowed vlan add <trunk_add_vlans>
cmd = 'switchport trunk allowed vlan add {trunk_add_vlans}'
uncmd = 'switchport trunk allowed vlan remove {trunk_add_vlans}'
configurations.append_line(
attributes.format(cmd),
unconfig_cmd=attributes.format(uncmd))
# switchport trunk allowed vlan remove <trunk_remove_vlans>
cmd = 'switchport trunk allowed vlan remove {trunk_remove_vlans}'
uncmd = 'switchport trunk allowed vlan add {trunk_remove_vlans}'
configurations.append_line(
attributes.format(cmd),
unconfig_cmd=attributes.format(uncmd))
# switchport trunk native vlan <native_vlan>
cmd = 'switchport trunk native vlan {native_vlan}'
uncmd = 'no switchport trunk native vlan'
configurations.append_line(
attributes.format(cmd),
unconfig_cmd=attributes.format(uncmd))
# speed auto
# duplex auto
auto_negotiate = attributes.value('auto_negotiate')
if auto_negotiate is not None:
if auto_negotiate:
configurations.append_line('speed auto')
configurations.append_line('duplex auto')
else:
configurations.append_line('no speed', raw=True)
configurations.append_line('no duplex', raw=True)
else:
# duplex_mode
duplex_mode = attributes.value('duplex_mode')
if duplex_mode:
if duplex_mode.name == 'full':
configurations.append_line('duplex full',
unconfig_cmd='no duplex')
elif duplex_mode.name == 'half':
configurations.append_line('duplex half',
unconfig_cmd='no duplex')
# flow_control_receive
flow_control_receive = attributes.value('flow_control_receive')
if flow_control_receive is not None:
if flow_control_receive:
configurations.append_line('flowcontrol receive on')
else:
configurations.append_line('no flowcontrol receive')
# flow_control_send
flow_control_send = attributes.value('flow_control send')
if flow_control_send is not None:
if flow_control_send:
configurations.append_line('flowcontrol send on')
else:
configurations.append_line('no flowcontrol send')
# -- ETHER L2, L3
# nxos: interface <intf> / beacon
# nxos: interface <intf> / channel-group 1
# nxos: interface <intf> / channel-group 1 force
# nxos: interface <intf> / channel-group 1 force mode active
# nxos: interface <intf> / channel-group 1 force mode on
# nxos: interface <intf> / channel-group 1 force mode passive
# nxos: interface <intf> / channel-group 1 mode active
# nxos: interface <intf> / channel-group 1 mode on
# nxos: interface <intf> / channel-group 1 mode passive
# nxos: interface <intf> / errdisable port detect cause acl-exception
# nxos: interface <intf> / fec cl74
# nxos: interface <intf> / fec cl91
# nxos: interface <intf> / fec off
# nxos: interface <intf> / lacp port-priority 1
# nxos: interface <intf> / lacp rate fast
# nxos: interface <intf> / lacp rate normal
# nxos: interface <intf> / mac packet-classify
# nxos: interface <intf> / mdix auto
# nxos: interface <intf> / power efficient-ethernet auto
# nxos: interface <intf> / power efficient-ethernet sleep threshold aggressive
# nxos: interface <intf> / speed-group 10000
# nxos: interface <intf> / speed-group 40000
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
class SubInterface(VirtualInterface, genie.libs.conf.interface.SubInterface):
""" SubInterface class, presenting subinterface type of `Interface`
object
`SubInterface` class inherits from the `VirtualInterface` class.
class
Args:
All the parameters/attributes inherits from its super class
`Interface`
Class variables:
type: interface type - subinterface
"""
# -- SUB
# nxos: interface <intf> / encapsulation dot1Q 1
# nxos: interface <intf> / ip unnumbered loopback0
first_dot1q = None
def _build_config_interface_submode(self, configurations, attributes, unconfig):
"""internal method to build the configuration of a subinterface interface object
Please use build_config and build_unconfig.
Api to build the configuration of a subinterface interface object. This
configuration depends of the configurable attributes of this object.
Examples:
>>> from genie.libs.conf.interface.nxos import SubInterface
Create a Interface obj
>>> subif = SubInterface(name='vlan11')
assign configurable attributes to the interface obj
>>> subif.mtu = '500'
Build configuration
>>> configuration = vlan.build_config()
"""
super()._build_config_interface_submode(configurations, attributes, unconfig)
# encapsulation <encapsulation> <first_dot1q>
if attributes.value('encapsulation'):
encapsulation = attributes.value('encapsulation').name
else:
encapsulation = None
if attributes.value('first_dot1q'):
first_dot1q = attributes.value('first_dot1q')
else:
first_dot1q = None
if encapsulation and encapsulation == 'dot1q':
if first_dot1q:
cmd = 'encapsulation {} {}'.format(encapsulation, first_dot1q)
uncmd = 'no encapsulation {}'.format(encapsulation)
configurations.append_line(cmd, unconfig_cmd=uncmd)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
class MgmtInterface(EthernetInterface, genie.libs.conf.interface.ManagementInterface):
# -- MGMT
# nxos: interface <intf> / media-type auto
# nxos: interface <intf> / media-type rj45
# nxos: interface <intf> / media-type sfp
_interface_name_types = (
'mgmt',
'Mgmt',
)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
class VlanInterface(VirtualInterface, genie.libs.conf.interface.VlanInterface):
""" VlanInterface class, presenting vlan type of `Interface`
objects
`VlanInterface` class inherits from the `VirtualInterface` class.
Args:
All the parameters/attrinutes inherits from its supper class
'Interface'
Class variables:
type: interface type - vlan
"""
_interface_name_types = (
'vlan',
'Vlan',
)
mac_aging = None
def _build_config_interface_submode(self, configurations, attributes, unconfig):
"""internal method to build the configuration of a vlan interface object
Please use build_config and build_unconfig.
Api to build the configuration of a vlan interface object. This
configuration depends of the configurable attributes of this object.
Examples:
>>> from genie.libs.conf.interface.nxos import Interface
Create a Interface obj
>>> port = Interface(name='vlan11')
assign configurable attributes to the interface obj
>>> vlan.mtu = '500'
Build configuration
>>> configuration = vlan.build_config()
"""
super()._build_config_interface_submode(configurations, attributes, unconfig)
configurations.append_line(
attributes.format('mac address-table aging-time {mac_aging}'),
unconfig_cmd='no mac address-table aging')
# -- VLAN
# nxos: interface <intf> / autostate
# nxos: interface <intf> / carrier-delay <0-60>
# nxos: interface <intf> / carrier-delay msec <0-1000>
# nxos: interface <intf> / fabric forwarding mode anycast-gateway
# nxos: interface <intf> / ip arp test 1.2.3.4 aaaa.bbbb.cccc 1
# nxos: interface <intf> / ip drop-glean
# nxos: interface <intf> / ipv6 forward
# nxos: interface <intf> / management
def __init__(self, **kwargs):
super().__init__(**kwargs)
class PortchannelInterface(VirtualInterface, genie.libs.conf.interface.AggregatedInterface):
""" PortchannelInterface class, presenting port-channel type of `Interface`
objects
`PortchannelInterface` class inherits from the `VirtualInterface` class.
Args:
All the parameters/attrinutes inherits from its supper class
'Interface'
Class variables:
type: interface type - portchannel
"""
_interface_name_types = (
'port-channel',
'Port-channel',
'Port-Channel',
)
members = managedattribute(
name='members',