-
Notifications
You must be signed in to change notification settings - Fork 390
/
physical_router_config.py
871 lines (776 loc) · 35.4 KB
/
physical_router_config.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
#
# Copyright (c) 2014 Juniper Networks, Inc. All rights reserved.
#
"""
This file contains implementation of inetconf interface for physical router
configuration manager
"""
from ncclient import manager
import copy
import time
import datetime
from cStringIO import StringIO
from dm_utils import DMUtils
from device_api.juniper_common_xsd import *
class PushConfigState(object):
PUSH_STATE_INIT = 0
PUSH_STATE_SUCCESS = 1
PUSH_STATE_RETRY = 2
REPUSH_INTERVAL = 15
REPUSH_MAX_INTERVAL = 300
PUSH_DELAY_PER_KB = 0.01
PUSH_DELAY_MAX = 100
PUSH_DELAY_ENABLE = True
@classmethod
def set_repush_interval(cls, value):
cls.REPUSH_INTERVAL = value
# end set_repush_interval
@classmethod
def set_repush_max_interval(cls, value):
cls.REPUSH_MAX_INTERVAL = value
# end set_repush_max_interval
@classmethod
def set_push_delay_per_kb(cls, value):
cls.PUSH_DELAY_PER_KB = value
# end set_push_delay_per_kb
@classmethod
def set_push_delay_max(cls, value):
cls.PUSH_DELAY_MAX = value
# end set_push_delay_max
@classmethod
def set_push_delay_enable(cls, value):
cls.PUSH_DELAY_ENABLE = value
# end set_push_delay_enable
@classmethod
def get_repush_interval(cls):
return cls.REPUSH_INTERVAL
# end set_repush_interval
@classmethod
def get_repush_max_interval(cls):
return cls.REPUSH_MAX_INTERVAL
# end get_repush_max_interval
@classmethod
def get_push_delay_per_kb(cls):
return cls.PUSH_DELAY_PER_KB
# end get_push_delay_per_kb
@classmethod
def get_push_delay_max(cls):
return cls.PUSH_DELAY_MAX
# end get_push_delay_max
@classmethod
def get_push_delay_enable(cls):
return cls.PUSH_DELAY_ENABLE
# end get_push_delay_enable
# end PushConfigState
class PhysicalRouterConfig(object):
# mapping from contrail family names to junos
_FAMILY_MAP = {
'route-target': '',
'inet-vpn': FamilyInetVpn(unicast=''),
'inet6-vpn': FamilyInet6Vpn(unicast=''),
'e-vpn': FamilyEvpn(signaling='')
}
def __init__(self, management_ip, user_creds,
vendor, product, logger=None):
self.management_ip = management_ip
self.user_creds = user_creds
self.vendor = vendor
self.product = product
self.reset_bgp_config()
self._logger = logger
self.push_config_state = PushConfigState.PUSH_STATE_INIT
self.commit_stats = {
'last_commit_time': '',
'last_commit_duration': '',
'commit_status_message': '',
'total_commits_sent_since_up': 0,
}
# end __init__
def update(self, management_ip, user_creds, vendor, product):
self.management_ip = management_ip
self.user_creds = user_creds
self.vendor = vendor
self.product = product
# end update
def get_commit_stats(self):
return self.commit_stats
# end get_commit_stats
def retry(self):
if self.push_config_state == PushConfigState.PUSH_STATE_RETRY:
return True
return False
# end retry
def get_xml_data(self, config):
xml_data = StringIO()
config.export(xml_data, 1)
return xml_data.getvalue()
# end get_xml_data
def build_netconf_config(self, groups, operation='replace'):
groups.set_name("__contrail__")
configuraion = Configuration(groups=groups)
groups.set_operation(operation)
apply_groups = ApplyGroups(name="__contrail__")
configuraion.set_apply_groups(apply_groups)
if operation == "delete":
apply_groups.set_operation(operation)
conf = config(configuration=configuraion)
return conf
def send_netconf(self, new_config, default_operation="merge",
operation="replace"):
self.push_config_state = PushConfigState.PUSH_STATE_INIT
start_time = None
config_size = 0
try:
with manager.connect(host=self.management_ip, port=22,
username=self.user_creds['username'],
password=self.user_creds['password'],
unknown_host_cb=lambda x, y: True) as m:
new_config = self.build_netconf_config(new_config, operation)
config_str = self.get_xml_data(new_config)
self._logger.info("\nsend netconf message: %s\n" % config_str)
config_size = len(config_str)
m.edit_config(
target='candidate', config=config_str,
test_option='test-then-set',
default_operation=default_operation)
self.commit_stats['total_commits_sent_since_up'] += 1
start_time = time.time()
m.commit()
end_time = time.time()
self.commit_stats['commit_status_message'] = 'success'
self.commit_stats['last_commit_time'] = \
datetime.datetime.fromtimestamp(
end_time).strftime('%Y-%m-%d %H:%M:%S')
self.commit_stats['last_commit_duration'] = str(
end_time - start_time)
self.push_config_state = PushConfigState.PUSH_STATE_SUCCESS
except Exception as e:
if self._logger:
self._logger.error("Router %s: %s" % (self.management_ip,
e.message))
self.commit_stats[
'commit_status_message'] = 'failed to apply config,\
router response: ' + e.message
if start_time is not None:
self.commit_stats['last_commit_time'] = \
datetime.datetime.fromtimestamp(
start_time).strftime('%Y-%m-%d %H:%M:%S')
self.commit_stats['last_commit_duration'] = str(
time.time() - start_time)
self.push_config_state = PushConfigState.PUSH_STATE_RETRY
return config_size
# end send_config
def add_pnf_logical_interface(self, junos_interface):
if not self.interfaces_config:
self.interfaces_config = Interfaces()
family = Family(inet=FamilyInet([Address(name=junos_interface.ip)]))
unit = Unit(name=junos_interface.unit, vlan_id=junos_interface.vlan_tag, family=family)
interface = Interface(name=junos_interface.ifd_name, unit=unit)
self.interfaces_config.add_interface(interface)
# end add_pnf_logical_interface
def add_static_routes(self, parent, static_routes):
static_config = parent.get_static()
if not static_config:
static_config = Static()
parent.set_static(static_config)
for dest, next_hops in static_routes.items():
route_config = Route(name=dest)
for next_hop in next_hops:
next_hop_str = next_hop.get("next-hop")
preference = next_hop.get("preference")
if not next_hop_str:
continue
if preference:
route_config.set_qualified_next_hop(QualifiedNextHop(
name=next_hop_str, preference=str(preference)))
else:
route_config.set_next_hop(next_hop_str)
static_config.add_route(route_config)
# end add_static_routes
def add_dynamic_tunnels(self, tunnel_source_ip,
ip_fabric_nets, bgp_router_ips):
dynamic_tunnel = DynamicTunnel(name="__contrail__",
source_address=tunnel_source_ip, gre='')
if ip_fabric_nets is not None:
for subnet in ip_fabric_nets.get("subnet", []):
dest_net = subnet['ip_prefix'] + '/' + str(subnet['ip_prefix_len'])
dynamic_tunnel.add_destination_networks(
DestinationNetworks(name=dest_net))
for bgp_router_ip in bgp_router_ips or []:
dynamic_tunnel.add_destination_networks(
DestinationNetworks(name=bgp_router_ip + '/32'))
dynamic_tunnels = DynamicTunnels()
dynamic_tunnels.add_dynamic_tunnel(dynamic_tunnel)
if self.global_routing_options_config is None:
self.global_routing_options_config = RoutingOptions()
self.global_routing_options_config.set_dynamic_tunnels(dynamic_tunnels)
# end add_dynamic_tunnels
def add_inet_public_vrf_filter(self, forwarding_options_config,
firewall_config, inet_type):
fo = Family()
inet_filter = InetFilter(input=DMUtils.make_public_vrf_filter_name(inet_type))
if inet_type == 'inet6':
fo.set_inet6(FamilyInet6(filter=inet_filter))
else:
fo.set_inet(FamilyInet(filter=inet_filter))
forwarding_options_config.add_family(fo)
f = FirewallFilter(name=DMUtils.make_public_vrf_filter_name(inet_type))
ff = firewall_config.get_family()
if not ff:
ff = FirewallFamily()
firewall_config.set_family(ff)
if inet_type == 'inet6':
inet6 = ff.get_inet6()
if not inet6:
inet6 = FirewallInet()
ff.set_inet6(inet6)
inet6.add_filter(f)
else:
inet = ff.get_inet()
if not inet:
inet = FirewallInet()
ff.set_inet(inet)
inet.add_filter(f)
term = Term(name="default-term", then=Then(accept=''))
f.add_term(term)
return f
# end add_inet_public_vrf_filter
def add_inet_filter_term(self, ri_name, prefixes, inet_type):
if inet_type == 'inet6':
prefixes = DMUtils.get_ipv6_prefixes(prefixes)
else:
prefixes = DMUtils.get_ipv4_prefixes(prefixes)
from_ = From()
for prefix in prefixes:
from_.add_destination_address(prefix)
then_ = Then()
then_.add_routing_instance(ri_name)
return Term(name=DMUtils.make_vrf_term_name(ri_name),
fromxx=from_, then=then_)
# end add_inet_filter_term
'''
ri_name: routing instance name to be configured on mx
is_l2: a flag used to indicate routing instance type, i.e : l2 or l3
is_l2_l3: VN forwarding mode is of type 'l2_l3' or not
import/export targets: routing instance import, export targets
prefixes: for l3 vrf static routes and for public vrf filter terms
gateways: for l2 evpn, bug#1395944
router_external: this indicates the routing instance configured is for
the public network
interfaces: logical interfaces to be part of vrf
fip_map: contrail instance ip to floating-ip map, used for snat & floating ip support
network_id : this is used for configuraing irb interfaces
static_routes: this is used for add PNF vrf static routes
no_vrf_table_label: if this is set to True will not generate vrf table label knob
restrict_proxy_arp: proxy-arp restriction config is generated for irb interfaces
only if vn is external and has fip map
'''
def add_routing_instance(self, ri_conf):
ri_name = ri_conf.get("ri_name")
is_l2 = ri_conf.get("is_l2", False)
is_l2_l3 = ri_conf.get("is_l2_l3", False)
import_targets = ri_conf.get("import_targets", set())
export_targets = ri_conf.get("export_targets", set())
prefixes = ri_conf.get("prefixes", [])
gateways = ri_conf.get("gateways", [])
router_external = ri_conf.get("router_external", False)
interfaces = ri_conf.get("interfaces", [])
vni = ri_conf.get("vni", None)
fip_map = ri_conf.get("fip_map", None)
network_id = ri_conf.get("network_id", None)
static_routes = ri_conf.get("static_routes", {})
no_vrf_table_label = ri_conf.get("no_vrf_table_label", False)
restrict_proxy_arp = ri_conf.get("restrict_proxy_arp", False)
self.routing_instances[ri_name] = ri_conf
ri_config = self.ri_config or RoutingInstances()
policy_config = self.policy_config or PolicyOptions()
ri = Instance(name=ri_name)
ri_config.add_instance(ri)
ri_opt = None
if router_external and is_l2 == False:
ri_opt = RoutingInstanceRoutingOptions(
static=Static(route=[Route(name="0.0.0.0/0", next_table="inet.0")]))
ri.set_routing_options(ri_opt)
# for both l2 and l3
ri.set_vrf_import(DMUtils.make_import_name(ri_name))
ri.set_vrf_export(DMUtils.make_export_name(ri_name))
has_ipv6_prefixes = DMUtils.has_ipv6_prefixes(prefixes)
has_ipv4_prefixes = DMUtils.has_ipv4_prefixes(prefixes)
if not is_l2:
if ri_opt is None:
ri_opt = RoutingInstanceRoutingOptions()
ri.set_routing_options(ri_opt)
if prefixes and fip_map is None:
static_config = ri_opt.get_static()
if not static_config:
static_config = Static()
ri_opt.set_static(static_config)
rib_config_v6 = None
static_config_v6 = None
for prefix in prefixes:
if ':' in prefix and not rib_config_v6:
static_config_v6 = Static()
rib_config_v6 = RIB(name=ri_name + ".inet6.0")
rib_config_v6.set_static(static_config_v6)
ri_opt.set_rib(rib_config_v6)
if ':' in prefix:
static_config_v6.add_route(Route(name=prefix, discard=''))
else:
static_config.add_route(Route(name=prefix, discard=''))
if router_external:
self.add_to_global_ri_opts(prefix)
ri.set_instance_type("vrf")
if not no_vrf_table_label:
ri.set_vrf_table_label('') # only for l3
if fip_map is None:
for interface in interfaces:
ri.add_interface(Interface(name=interface.name))
if static_routes:
self.add_static_routes(ri_opt, static_routes)
if has_ipv4_prefixes:
ri_opt.set_auto_export(AutoExport(family=Family(inet=FamilyInet(unicast=''))))
if has_ipv6_prefixes:
ri_opt.set_auto_export(AutoExport(family=Family(inet6=FamilyInet6(unicast=''))))
else:
ri.set_instance_type("virtual-switch")
if fip_map is not None:
if ri_opt is None:
ri_opt = RoutingInstanceRoutingOptions()
ri.set_routing_options(ri_opt)
static_config = ri_opt.get_static()
if not static_config:
static_config = Static()
ri_opt.set_static(static_config)
static_config.add_route(Route(name="0.0.0.0/0", next_hop=interfaces[0].name))
ri.add_interface(Interface(name=interfaces[0].name))
public_vrf_ips = {}
for pip in fip_map.values():
if pip["vrf_name"] not in public_vrf_ips:
public_vrf_ips[pip["vrf_name"]] = set()
public_vrf_ips[pip["vrf_name"]].add(pip["floating_ip"])
for public_vrf, fips in public_vrf_ips.items():
ri_public = Instance(name=public_vrf)
ri_config.add_instance(ri_public)
ri_public.add_interface(Interface(name=interfaces[1].name))
ri_opt = RoutingInstanceRoutingOptions()
ri_public.set_routing_options(ri_opt)
static_config = Static()
ri_opt.set_static(static_config)
for fip in fips:
static_config.add_route(Route(name=fip + "/32",
next_hop=interfaces[1].name))
# add policies for export route targets
ps = PolicyStatement(name=DMUtils.make_export_name(ri_name))
then = Then()
ps.set_term(Term(name="t1", then=then))
for route_target in export_targets:
comm = Community(add='',
community_name=DMUtils.make_community_name(route_target))
then.add_community(comm)
if fip_map is not None:
# for nat instance
then.set_reject('')
else:
then.set_accept('')
policy_config.add_policy_statement(ps)
# add policies for import route targets
ps = PolicyStatement(name=DMUtils.make_import_name(ri_name))
from_ = From()
term = Term(name="t1", fromxx=from_)
ps.set_term(term)
for route_target in import_targets:
from_.add_community(DMUtils.make_community_name(route_target))
term.set_then(Then(accept=''))
ps.set_then(Then(reject=''))
policy_config.add_policy_statement(ps)
# add firewall config for public VRF
forwarding_options_config = self.forwarding_options_config
firewall_config = self.firewall_config
if router_external and is_l2 == False:
forwarding_options_config = (self.forwarding_options_config or
ForwardingOptions())
firewall_config = self.firewall_config or Firewall()
if has_ipv4_prefixes and not self.inet4_forwarding_filter:
#create single instance inet4 filter
self.inet4_forwarding_filter = self.add_inet_public_vrf_filter(
forwarding_options_config,
firewall_config, "inet")
if has_ipv6_prefixes and not self.inet6_forwarding_filter:
#create single instance inet6 filter
self.inet6_forwarding_filter = self.add_inet_public_vrf_filter(
forwarding_options_config,
firewall_config, "inet6")
if has_ipv4_prefixes:
#add terms to inet4 filter
term = self.add_inet_filter_term(ri_name, prefixes, "inet4")
# insert before the last term
terms = self.inet4_forwarding_filter.get_term()
terms = [term] + (terms or [])
self.inet4_forwarding_filter.set_term(terms)
if has_ipv6_prefixes:
#add terms to inet6 filter
term = self.add_inet_filter_term(ri_name, prefixes, "inet6")
# insert before the last term
terms = self.inet6_forwarding_filter.get_term()
terms = [term] + (terms or [])
self.inet6_forwarding_filter.set_term(terms)
if fip_map is not None:
firewall_config = firewall_config or Firewall()
f = FirewallFilter(name=DMUtils.make_private_vrf_filter_name(ri_name))
ff = firewall_config.get_family()
if not ff:
ff = FirewallFamily()
firewall_config.set_family(ff)
inet = ff.get_inet()
if not inet:
inet = FirewallInet()
ff.set_inet(inet)
inet.add_filter(f)
term = Term(name=DMUtils.make_vrf_term_name(ri_name))
from_ = From()
for fip_user_ip in fip_map.keys():
from_.add_source_address(fip_user_ip)
term.set_from(from_)
term.set_then(Then(routing_instance=[ri_name]))
f.add_term(term)
term = Term(name="default-term", then=Then(accept=''))
f.add_term(term)
interfaces_config = self.interfaces_config or Interfaces()
irb_intf = Interface(name="irb")
interfaces_config.add_interface(irb_intf)
intf_unit = Unit(name=str(network_id))
if restrict_proxy_arp:
intf_unit.set_proxy_arp(ProxyArp(restricted=''))
inet = FamilyInet()
inet.set_filter(InetFilter(input=DMUtils.make_private_vrf_filter_name(ri_name)))
intf_unit.set_family(Family(inet=inet))
irb_intf.add_unit(intf_unit)
# add L2 EVPN and BD config
bd_config = None
interfaces_config = self.interfaces_config
proto_config = self.proto_config
if (is_l2 and vni is not None and
self.is_family_configured(self.bgp_params, "e-vpn")):
ri.set_vtep_source_interface("lo0.0")
bd_config = BridgeDomains()
ri.set_bridge_domains(bd_config)
bd = Domain(name=DMUtils.make_bridge_name(vni), vlan_id='none', vxlan=VXLan(vni=vni))
bd_config.add_domain(bd)
for interface in interfaces:
bd.add_interface(Interface(name=interface.name))
if is_l2_l3:
# network_id is unique, hence irb
bd.set_routing_interface("irb." + str(network_id))
ri.set_protocols(RoutingInstanceProtocols(
evpn=Evpn(encapsulation='vxlan', extended_vni_list='all')))
interfaces_config = self.interfaces_config or Interfaces()
if is_l2_l3:
irb_intf = Interface(name='irb', gratuitous_arp_reply='')
interfaces_config.add_interface(irb_intf)
if gateways is not None:
intf_unit = Unit(name=str(network_id))
irb_intf.add_unit(intf_unit)
family = Family()
intf_unit.set_family(family)
inet = None
inet6 = None
for (irb_ip, gateway) in gateways:
if ':' in irb_ip:
if not inet6:
inet6 = FamilyInet6()
family.set_inet6(inet6)
addr = Address()
inet6.add_address(addr)
else:
if not inet:
inet = FamilyInet()
family.set_inet(inet)
addr = Address()
inet.add_address(addr)
addr.set_name(irb_ip)
if len(gateway) and gateway != '0.0.0.0':
addr.set_virtual_gateway_address(gateway)
lo_intf = Interface(name="lo0")
interfaces_config.add_interface(lo_intf)
fam_inet = FamilyInet(address=[Address(name=self.bgp_params['address'] + "/32",
primary='', preferred='')])
intf_unit = Unit(name="0", family=Family(inet=fam_inet))
lo_intf.add_unit(intf_unit)
self.build_l2_evpn_interface_config(interfaces_config, interfaces)
# fip services config
services_config = self.services_config
if fip_map is not None:
services_config = self.services_config or Services()
service_name = DMUtils.make_services_set_name(ri_name)
service_set = ServiceSet(name=service_name)
services_config.add_service_set(service_set)
nat_rule = NATRules(name=service_name + "-sn-rule")
service_set.add_nat_rules(NATRules(name=DMUtils.make_snat_rule_name(ri_name)))
service_set.add_nat_rules(NATRules(name=DMUtils.make_dnat_rule_name(ri_name)))
next_hop_service = NextHopService(inside_service_interface = interfaces[0].name,
outside_service_interface = interfaces[1].name)
service_set.set_next_hop_service(next_hop_service)
nat = NAT(allow_overlapping_nat_pools='')
services_config.add_nat(nat)
snat_rule = Rule(name=DMUtils.make_snat_rule_name(ri_name),
match_direction="input")
nat.add_rule(snat_rule)
dnat_rule = Rule(name=DMUtils.make_dnat_rule_name(ri_name),
match_direction="output")
nat.add_rule(dnat_rule)
for pip, fip_vn in fip_map.items():
fip = fip_vn["floating_ip"]
term = Term(name=DMUtils.make_ip_term_name(pip))
snat_rule.set_term(term)
# private ip
from_ = From(source_address=[pip + "/32"])
term.set_from(from_)
# public ip
then_ = Then()
term.set_then(then_)
translated = Translated(source_prefix=fip + "/32",
translation_type=TranslationType(basic_nat44=''))
then_.set_translated(translated)
term = Term(name=DMUtils.make_ip_term_name(fip))
dnat_rule.set_term(term)
# public ip
from_ = From(destination_address=[fip + "/32"])
term.set_from(from_)
# private ip
then_ = Then()
term.set_then(then_)
translated = Translated(destination_prefix=pip + "/32",
translation_type=TranslationType(dnat_44=''))
then_.set_translated(translated)
interfaces_config = self.interfaces_config or Interfaces()
si_intf = Interface(name=interfaces[0].ifd_name)
interfaces_config.add_interface(si_intf)
intf_unit = Unit(name=interfaces[0].unit)
si_intf.add_unit(intf_unit)
family = Family(inet=FamilyInet())
intf_unit.set_family(family)
intf_unit.set_service_domain("inside")
intf_unit = Unit(name=interfaces[1].unit)
si_intf.add_unit(intf_unit)
family = Family(inet=FamilyInet())
intf_unit.set_family(family)
intf_unit.set_service_domain("outside")
self.forwarding_options_config = forwarding_options_config
self.firewall_config = firewall_config
self.policy_config = policy_config
self.proto_config = proto_config
self.interfaces_config = interfaces_config
self.services_config = services_config
self.route_targets |= import_targets | export_targets
self.ri_config = ri_config
# end add_routing_instance
def build_l2_evpn_interface_config(self, interfaces_config, interfaces):
ifd_map = {}
for interface in interfaces:
ifd_map.setdefault(interface.ifd_name, []).append(interface)
for ifd_name, interface_list in ifd_map.items():
intf = Interface(name=ifd_name)
interfaces_config.add_interface(intf)
if interface_list[0].is_untagged():
if (len(interface_list) > 1):
self._logger.error(
"invalid logical interfaces config for ifd %s" % (
ifd_name))
continue
intf.set_encapsulation("ethernet-bridge")
intf.add_unit(Unit(name=interface_list[0].unit, family=Family(bridge='')))
else:
intf.set_flexible_vlan_tagging('')
intf.set_encapsulation("flexible-ethernet-services")
for interface in interface_list:
intf.add_unit(Unit(name=interface.unit, encapsulation='vlan-bridge',
vlan_id=str(interface.vlan_tag)))
# end build_l2_evpn_interface_config
def set_global_routing_options(self, bgp_params):
if bgp_params['address'] is not None:
if not self.global_routing_options_config:
self.global_routing_options_config = RoutingOptions()
self.global_routing_options_config.set_router_id(bgp_params['address'])
# end set_global_routing_options
def add_to_global_ri_opts(self, prefix):
if not prefix:
return
if self.global_routing_options_config is None:
self.global_routing_options_config = RoutingOptions()
static_config = Static()
if ':' in prefix:
rib_config_v6 = RIB(name='inet6.0')
rib_config_v6.set_static(static_config)
self.global_routing_options_config.add_rib(rib_config_v6)
else:
self.global_routing_options_config.add_static(static_config)
static_config.add_route(Route(name=prefix, discard=''))
# end add_to_global_ri_opts
def is_family_configured(self, params, family_name):
if params is None or params.get('address_families') is None:
return False
families = params['address_families'].get('family', [])
if family_name in families:
return True
return False
def add_families(self, parent, params):
if params.get('address_families') is None:
return
families = params['address_families'].get('family', [])
if not families:
return
family_etree = Family()
parent.set_family(family_etree)
for family in families:
fam = family.replace('-', '_')
if family in ['e-vpn', 'e_vpn']:
fam = 'evpn'
if family in self._FAMILY_MAP:
getattr(family_etree, "set_" + fam)(self._FAMILY_MAP[family])
else:
getattr(family_etree, "set_" + fam)('')
# end add_families
def add_bgp_auth_config(self, bgp_config, bgp_params):
if bgp_params.get('auth_data') is None:
return
keys = bgp_params['auth_data'].get('key_items', [])
if len(keys) > 0:
bgp_config.set_authentication_key(keys[0].get('key'))
def add_bgp_hold_time_config(self, bgp_config, bgp_params):
if bgp_params.get('hold_time') is None:
return
bgp_config.set_hold_time(bgp_params.get('hold_time'))
def set_bgp_config(self, params):
self.bgp_params = params
# end set_bgp_config
def _get_bgp_config_xml(self, external=False):
if self.bgp_params is None:
return None
bgp_group = BgpGroup()
if external:
bgp_group.set_name(DMUtils.make_bgp_group_name(True))
bgp_group.set_type('external')
bgp_group.set_multihop('')
else:
bgp_group.set_name(DMUtils.make_bgp_group_name(False))
bgp_group.set_type('internal')
bgp_group.set_local_address(self.bgp_params['address'])
self.add_families(bgp_group, self.bgp_params)
self.add_bgp_auth_config(bgp_group, self.bgp_params)
self.add_bgp_hold_time_config(bgp_group, self.bgp_params)
return bgp_group
# end _get_bgp_config_xml
def reset_bgp_config(self):
self.routing_instances = {}
self.bgp_params = None
self.ri_config = None
self.interfaces_config = None
self.services_config = None
self.policy_config = None
self.firewall_config = None
self.inet4_forwarding_filter = None
self.inet6_forwarding_filter = None
self.forwarding_options_config = None
self.global_routing_options_config = None
self.proto_config = None
self.route_targets = set()
self.bgp_peers = {}
self.external_peers = {}
# ene reset_bgp_config
def delete_bgp_config(self):
self.reset_bgp_config()
self.send_netconf(Groups(), default_operation="none", operation="delete")
# end delete_config
def add_bgp_peer(self, router, params, attr, external):
peer_data = {}
peer_data['params'] = params
peer_data['attr'] = attr
if external:
self.external_peers[router] = peer_data
else:
self.bgp_peers[router] = peer_data
# end add_peer
def _get_neighbor_config_xml(self, bgp_config, peers):
for peer, peer_data in peers.items():
params = peer_data.get('params', {})
attr = peer_data.get('attr', {})
nbr = BgpGroup(name=peer)
bgp_config.add_neighbor(nbr)
bgp_sessions = attr.get('session')
if bgp_sessions:
# for now assume only one session
session_attrs = bgp_sessions[0].get('attributes', [])
for session_attr in session_attrs:
# For not, only consider the attribute if bgp-router is
# not specified
if session_attr.get('bgp_router') is None:
self.add_families(nbr, session_attr)
self.add_bgp_auth_config(nbr, session_attr)
break
peer_as = params.get('local_autonomous_system') or params.get('autonomous_system')
nbr.set_peer_as(peer_as)
# end _get_neighbor_config_xml
def set_as_config(self):
if self.global_routing_options_config is None:
self.global_routing_options_config = RoutingOptions()
self.global_routing_options_config.set_route_distinguisher_id(self.bgp_params['identifier'])
local_as = self.bgp_params.get('local_autonomous_system') or self.bgp_params.get('autonomous_system')
self.global_routing_options_config.set_autonomous_system(str(local_as))
# end set_as_config
def set_route_targets_config(self):
if self.policy_config is None:
self.policy_config = PolicyOptions()
for route_target in self.route_targets:
comm = CommunityType(name=DMUtils.make_community_name(route_target),
members=route_target)
self.policy_config.add_community(comm)
# end set_route_targets_config
def set_bgp_group_config(self):
bgp_config = self._get_bgp_config_xml()
if bgp_config is None:
return False
if self.proto_config is None:
self.proto_config = Protocols()
bgp = Bgp()
self.proto_config.set_bgp(bgp)
bgp.add_group(bgp_config)
self._get_neighbor_config_xml(bgp_config, self.bgp_peers)
if self.external_peers is not None:
ext_grp_config = self._get_bgp_config_xml(True)
bgp.add_group(ext_grp_config)
self._get_neighbor_config_xml(ext_grp_config, self.external_peers)
return True
# end set_bgp_group_config
def send_bgp_config(self):
if not self.set_bgp_group_config():
return 0
self.set_as_config()
self.set_route_targets_config()
groups = Groups()
groups.set_routing_instances(self.ri_config)
groups.set_interfaces(self.interfaces_config)
groups.set_services(self.services_config)
groups.set_policy_options(self.policy_config)
groups.set_firewall(self.firewall_config)
groups.set_forwarding_options(self.forwarding_options_config)
groups.set_routing_options(self.global_routing_options_config)
groups.set_protocols(self.proto_config)
return self.send_netconf(groups)
# end send_bgp_config
# end PhycalRouterConfig
class JunosInterface(object):
def __init__(self, if_name, if_type, if_vlan_tag=0, if_ip=None):
self.name = if_name
self.if_type = if_type
self.vlan_tag = if_vlan_tag
ifparts = if_name.split('.')
self.ifd_name = ifparts[0]
self.unit = ifparts[1]
self.ip = if_ip
# end __init__
def is_untagged(self):
if not self.vlan_tag:
return True
return False
# end is_untagged
# end JunosInterface