-
Notifications
You must be signed in to change notification settings - Fork 390
/
physical_router_config.py
886 lines (811 loc) · 39.1 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
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
#
# Copyright (c) 2014 Juniper Networks, Inc. All rights reserved.
#
"""
This file contains implementation of inetconf interface for physical router
configuration manager
"""
from lxml import etree
from ncclient import manager
import copy
import time
import datetime
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': '<route-target/>',
'inet-vpn': '<inet-vpn><unicast/></inet-vpn>',
'inet6-vpn': '<inet6-vpn><unicast/></inet6-vpn>',
'e-vpn': '<evpn><signaling/></evpn>'
}
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 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:
add_config = etree.Element(
"config",
nsmap={"xc": "urn:ietf:params:xml:ns:netconf:base:1.0"})
config = etree.SubElement(add_config, "configuration")
config_group = etree.SubElement(
config, "groups", operation=operation)
contrail_group = etree.SubElement(config_group, "name")
contrail_group.text = "__contrail__"
if isinstance(new_config, list):
for nc in new_config:
config_group.append(nc)
else:
config_group.append(new_config)
if operation == "delete":
apply_groups = etree.SubElement(
config, "apply-groups", operation=operation)
else:
apply_groups = etree.SubElement(config, "apply-groups")
apply_groups.text = "__contrail__"
self._logger.info("\nsend netconf message: %s\n" % (
etree.tostring(add_config, pretty_print=True)))
config_str = etree.tostring(add_config)
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.logical_interface_config:
self.logical_interface_config = etree.Element('interfaces')
li_config = etree.fromstring("""
<interface>
<name>{physical_interface_name}</name>
<unit>
<name>{logical_interface_id}</name>
<vlan-id>{vlan_id}</vlan-id>
<family>
<inet>
<address>{ip}</address>
</inet>
</family>
</unit>
</interface>
""".format(
physical_interface_name=junos_interface.ifd_name,
logical_interface_id=junos_interface.unit,
vlan_id=junos_interface.vlan_tag,
ip=junos_interface.ip
)
)
self.logical_interface_config.append(li_config)
def add_static_routes(self, parent, static_routes):
static_config = etree.SubElement(parent, "static")
for dest, next_hops in static_routes.items():
route_config = etree.SubElement(static_config, "route")
etree.SubElement(route_config, "name").text = 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:
qualified = etree.SubElement(
route_config, "qualified-next-hop")
qualified.text = next_hop_str
etree.SubElement(
qualified, "preference").text = str(preference)
else:
etree.SubElement(
route_config, "next-hop").text = next_hop_str
def add_dynamic_tunnels(self, tunnel_source_ip,
ip_fabric_nets, bgp_router_ips):
self.tunnel_config = etree.Element("routing-options")
dynamic_tunnels = etree.SubElement(
self.tunnel_config, "dynamic-tunnels")
dynamic_tunnel = etree.SubElement(dynamic_tunnels, "dynamic-tunnel")
etree.SubElement(dynamic_tunnel, "name").text = "__contrail__"
etree.SubElement(
dynamic_tunnel, "source-address").text = tunnel_source_ip
etree.SubElement(dynamic_tunnel, "gre")
if ip_fabric_nets is not None:
for subnet in ip_fabric_nets.get("subnet", []):
dest_network = etree.SubElement(
dynamic_tunnel, "destination-networks")
etree.SubElement(dest_network, "name").text = subnet[
'ip_prefix'] + '/' + str(subnet['ip_prefix_len'])
for bgp_router_ip in bgp_router_ips:
dest_network = etree.SubElement(
dynamic_tunnel, "destination-networks")
etree.SubElement(dest_network, "name").text = bgp_router_ip + '/32'
# end add_dynamic_tunnels
'''
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
'''
def add_routing_instance(self, ri_name, is_l2, is_l2_l3,
import_targets, export_targets,
prefixes=[], gateways=[],
router_external=False,
interfaces=[], vni=None,
fip_map=None, network_id=None,
static_routes={}, no_vrf_table_label=False):
self.routing_instances[ri_name] = {'import_targets': import_targets,
'export_targets': export_targets,
'prefixes': prefixes,
'gateways': gateways,
'router_external': router_external,
'interfaces': interfaces,
'vni': vni,
'fip_map': fip_map}
ri_config = self.ri_config or etree.Element("routing-instances")
policy_config = self.policy_config or etree.Element("policy-options")
ri = etree.SubElement(ri_config, "instance")
etree.SubElement(ri, "name").text = ri_name
ri_opt = None
if router_external and is_l2 == False:
ri_opt = etree.SubElement(ri, "routing-options")
static_config = etree.SubElement(ri_opt, "static")
route_config = etree.SubElement(static_config, "route")
etree.SubElement(route_config, "name").text = "0.0.0.0/0"
etree.SubElement(route_config, "next-table").text = "inet.0"
# for both l2 and l3
etree.SubElement(ri, "vrf-import").text = ri_name + "-import"
etree.SubElement(ri, "vrf-export").text = ri_name + "-export"
if not is_l2:
if ri_opt is None:
ri_opt = etree.SubElement(ri, "routing-options")
has_ipv6_prefixes = False
has_ipv4_prefixes = False
if prefixes and fip_map is None:
static_config = etree.SubElement(ri_opt, "static")
rib_config_v6 = None
static_config_v6 = None
for prefix in prefixes:
if ':' in prefix and not rib_config_v6:
rib_config_v6 = etree.SubElement(ri_opt, "rib")
etree.SubElement(rib_config_v6, "name").text = ri_name + ".inet6.0"
static_config_v6 = etree.SubElement(rib_config_v6, "static")
has_ipv6_prefixes = True
if ':' in prefix:
route_config = etree.SubElement(static_config_v6, "route")
else:
route_config = etree.SubElement(static_config, "route")
has_ipv4_prefixes = True
etree.SubElement(route_config, "name").text = prefix
etree.SubElement(route_config, "discard")
if router_external:
self.add_to_global_ri_opts(prefix)
etree.SubElement(ri, "instance-type").text = "vrf"
if not no_vrf_table_label:
etree.SubElement(ri, "vrf-table-label") # only for l3
if fip_map is None:
for interface in interfaces:
if_element = etree.SubElement(ri, "interface")
etree.SubElement(if_element, "name").text = interface.name
if ri_opt is None:
ri_opt = etree.SubElement(ri, "routing-options")
if static_routes:
self.add_static_routes(ri_opt, static_routes)
if has_ipv4_prefixes:
auto_export = """<auto-export>
<family><inet><unicast/></inet></family>
</auto-export>"""
ri_opt.append(etree.fromstring(auto_export))
if has_ipv6_prefixes:
auto_export = """<auto-export>
<family><inet6><unicast/></inet6></family>
</auto-export>"""
ri_opt.append(etree.fromstring(auto_export))
else:
etree.SubElement(ri, "instance-type").text = "virtual-switch"
if fip_map is not None:
if ri_opt is None:
ri_opt = etree.SubElement(ri, "routing-options")
static_config = etree.SubElement(ri_opt, "static")
route_config = etree.SubElement(static_config, "route")
etree.SubElement(route_config, "name").text = "0.0.0.0/0"
etree.SubElement(
route_config, "next-hop").text = interfaces[0].name
if_element = etree.SubElement(ri, "interface")
etree.SubElement(if_element, "name").text = 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 = etree.SubElement(ri_config, "instance")
etree.SubElement(ri_public, "name").text = public_vrf
ri_opt = etree.SubElement(ri_public, "routing-options")
static_config = etree.SubElement(ri_opt, "static")
if_element = etree.SubElement(ri_public, "interface")
etree.SubElement(if_element, "name").text = interfaces[1].name
for fip in fips:
route_config = etree.SubElement(static_config, "route")
etree.SubElement(route_config, "name").text = fip + "/32"
etree.SubElement(
route_config, "next-hop").text = interfaces[1].name
# add policies for export route targets
ps = etree.SubElement(policy_config, "policy-statement")
etree.SubElement(ps, "name").text = ri_name + "-export"
term = etree.SubElement(ps, "term")
etree.SubElement(term, "name").text = "t1"
then = etree.SubElement(term, "then")
for route_target in export_targets:
comm = etree.SubElement(then, "community")
etree.SubElement(comm, "add")
etree.SubElement(
comm, "community-name").text = route_target.replace(':', '_')
if fip_map is not None:
# for nat instance
etree.SubElement(then, "reject")
else:
etree.SubElement(then, "accept")
# add policies for import route targets
ps = etree.SubElement(policy_config, "policy-statement")
etree.SubElement(ps, "name").text = ri_name + "-import"
term = etree.SubElement(ps, "term")
etree.SubElement(term, "name").text = "t1"
from_ = etree.SubElement(term, "from")
for route_target in import_targets:
target_name = route_target.replace(':', '_')
etree.SubElement(from_, "community").text = target_name
then = etree.SubElement(term, "then")
etree.SubElement(then, "accept")
then = etree.SubElement(ps, "then")
etree.SubElement(then, "reject")
# 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:
if self.forwarding_options_config is None:
forwarding_options_config = etree.Element("forwarding-options")
fo = etree.SubElement(forwarding_options_config, "family")
inet = etree.SubElement(fo, "inet")
f = etree.SubElement(inet, "filter")
etree.SubElement(
f, "input").text = "redirect_to_public_vrf_filter"
firewall_config = self.firewall_config or etree.Element(
"firewall")
fc = etree.SubElement(firewall_config, "family")
inet = etree.SubElement(fc, "inet")
f = etree.SubElement(inet, "filter")
etree.SubElement(
f, "name").text = "redirect_to_public_vrf_filter"
self.inet_forwarding_filter = f
term = etree.SubElement(f, "term")
etree.SubElement(term, "name").text = "default-term"
then_ = etree.SubElement(term, "then")
etree.SubElement(then_, "accept")
term = etree.Element("term")
etree.SubElement(term, "name").text = "term-" + ri_name[:59]
if prefixes:
from_ = etree.SubElement(term, "from")
for prefix in prefixes:
etree.SubElement(
from_, "destination-address").text = prefix
then_ = etree.SubElement(term, "then")
etree.SubElement(then_, "routing-instance").text = ri_name
# insert after 'name' element but before the last term
self.inet_forwarding_filter.insert(1, term)
if fip_map is not None:
firewall_config = self.firewall_config or etree.Element("firewall")
fc = etree.SubElement(firewall_config, "family")
inet = etree.SubElement(fc, "inet")
f = etree.SubElement(inet, "filter")
etree.SubElement(
f, "name").text = "redirect_to_" + ri_name[:46] + "_vrf"
term = etree.SubElement(f, "term")
etree.SubElement(term, "name").text = "term-" + ri_name[:59]
from_ = etree.SubElement(term, "from")
for fip_user_ip in fip_map.keys():
etree.SubElement(from_, "source-address").text = fip_user_ip
then_ = etree.SubElement(term, "then")
etree.SubElement(then_, "routing-instance").text = ri_name
term = etree.SubElement(f, "term")
etree.SubElement(term, "name").text = "default-term"
then_ = etree.SubElement(term, "then")
etree.SubElement(then_, "accept")
interfaces_config = self.interfaces_config or etree.Element(
"interfaces")
irb_intf = etree.SubElement(interfaces_config, "interface")
etree.SubElement(irb_intf, "name").text = "irb"
intf_unit = etree.SubElement(irb_intf, "unit")
etree.SubElement(intf_unit, "name").text = str(network_id)
family = etree.SubElement(intf_unit, "family")
inet = etree.SubElement(family, "inet")
f = etree.SubElement(inet, "filter")
iput = etree.SubElement(f, "input")
etree.SubElement(
iput,
"filter-name").text = "redirect_to_" + ri_name[:46] + "_vrf"
# 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")):
etree.SubElement(ri, "vtep-source-interface").text = "lo0.0"
bd_config = etree.SubElement(ri, "bridge-domains")
bd = etree.SubElement(bd_config, "domain")
etree.SubElement(bd, "name").text = "bd-" + str(vni)
etree.SubElement(bd, "vlan-id").text = 'none'
vxlan = etree.SubElement(bd, "vxlan")
etree.SubElement(vxlan, "vni").text = str(vni)
for interface in interfaces:
if_element = etree.SubElement(bd, "interface")
etree.SubElement(if_element, "name").text = interface.name
if is_l2_l3:
# network_id is unique, hence irb
etree.SubElement(
bd, "routing-interface").text = "irb." + str(network_id)
evpn_proto_config = etree.SubElement(ri, "protocols")
evpn = etree.SubElement(evpn_proto_config, "evpn")
etree.SubElement(evpn, "encapsulation").text = "vxlan"
etree.SubElement(evpn, "extended-vni-list").text = "all"
interfaces_config = self.interfaces_config or etree.Element(
"interfaces")
if is_l2_l3:
irb_intf = etree.SubElement(interfaces_config, "interface")
etree.SubElement(irb_intf, "name").text = "irb"
etree.SubElement(irb_intf, "gratuitous-arp-reply")
if gateways is not None:
intf_unit = etree.SubElement(irb_intf, "unit")
etree.SubElement(intf_unit, "name").text = str(network_id)
family = etree.SubElement(intf_unit, "family")
inet = None
inet6 = None
for (irb_ip, gateway) in gateways:
if ':' in irb_ip:
if not inet6:
inet6 = etree.SubElement(family, "inet6")
addr = etree.SubElement(inet6, "address")
else:
if not inet:
inet = etree.SubElement(family, "inet")
addr = etree.SubElement(inet, "address")
etree.SubElement(addr, "name").text = irb_ip
if len(gateway) and gateway != '0.0.0.0':
etree.SubElement(
addr, "virtual-gateway-address").text = gateway
lo_intf = etree.SubElement(interfaces_config, "interface")
etree.SubElement(lo_intf, "name").text = "lo0"
intf_unit = etree.SubElement(lo_intf, "unit")
etree.SubElement(intf_unit, "name").text = "0"
family = etree.SubElement(intf_unit, "family")
inet = etree.SubElement(family, "inet")
addr = etree.SubElement(inet, "address")
etree.SubElement(addr, "name").text = self.bgp_params[
'address'] + "/32"
etree.SubElement(addr, "primary")
etree.SubElement(addr, "preferred")
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 etree.Element("services")
service_name = 'sv-' + ri_name
# mx has limitation for service-set and nat-rule name length,
# allowed max 63 chars
service_name = service_name[:23]
service_set = etree.SubElement(services_config, "service-set")
etree.SubElement(service_set, "name").text = service_name
nat_rule = etree.SubElement(service_set, "nat-rules")
etree.SubElement(nat_rule, "name").text = service_name + "-sn-rule"
nat_rule = etree.SubElement(service_set, "nat-rules")
etree.SubElement(nat_rule, "name").text = service_name + "-dn-rule"
next_hop_service = etree.SubElement(
service_set, "next-hop-service")
etree.SubElement(
next_hop_service,
"inside-service-interface").text = interfaces[0].name
etree.SubElement(
next_hop_service,
"outside-service-interface").text = interfaces[1].name
nat = etree.SubElement(services_config, "nat")
snat_rule = etree.SubElement(nat, "rule")
etree.SubElement(
snat_rule, "name").text = service_name + "-sn-rule"
etree.SubElement(snat_rule, "match-direction").text = "input"
dnat_rule = etree.SubElement(nat, "rule")
etree.SubElement(
dnat_rule, "name").text = service_name + "-dn-rule"
etree.SubElement(dnat_rule, "match-direction").text = "output"
for pip, fip_vn in fip_map.items():
fip = fip_vn["floating_ip"]
term = etree.SubElement(snat_rule, "term")
etree.SubElement(
term, "name").text = "term_" + pip.replace('.', '_')
from_ = etree.SubElement(term, "from")
src_addr = etree.SubElement(from_, "source-address")
# private ip
etree.SubElement(src_addr, "name").text = pip + "/32"
then_ = etree.SubElement(term, "then")
translated = etree.SubElement(then_, "translated")
etree.SubElement(
translated,
"source-prefix").text = fip + "/32" # public ip
translation_type = etree.SubElement(
translated, "translation-type")
etree.SubElement(translation_type, "basic-nat44")
term = etree.SubElement(dnat_rule, "term")
etree.SubElement(
term, "name").text = "term_" + fip.replace('.', '_')
from_ = etree.SubElement(term, "from")
src_addr = etree.SubElement(from_, "destination-address")
etree.SubElement(
src_addr, "name").text = fip + "/32" # public ip
then_ = etree.SubElement(term, "then")
translated = etree.SubElement(then_, "translated")
etree.SubElement(
translated,
"destination-prefix").text = pip + "/32" # source ip
translation_type = etree.SubElement(
translated, "translation-type")
etree.SubElement(translation_type, "dnat-44")
interfaces_config = self.interfaces_config or etree.Element(
"interfaces")
si_intf = etree.SubElement(interfaces_config, "interface")
etree.SubElement(si_intf, "name").text = interfaces[0].ifd_name
intf_unit = etree.SubElement(si_intf, "unit")
etree.SubElement(intf_unit, "name").text = interfaces[0].unit
family = etree.SubElement(intf_unit, "family")
etree.SubElement(family, "inet")
etree.SubElement(intf_unit, "service-domain").text = "inside"
intf_unit = etree.SubElement(si_intf, "unit")
etree.SubElement(intf_unit, "name").text = interfaces[1].unit
family = etree.SubElement(intf_unit, "family")
etree.SubElement(family, "inet")
etree.SubElement(intf_unit, "service-domain").text = "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 = etree.SubElement(interfaces_config, "interface")
etree.SubElement(intf, "name").text = ifd_name
if interface_list[0].is_untagged():
if (len(interface_list) > 1):
self._logger.error(
"invalid logical interfaces config for ifd %s" % (
ifd_name))
continue
etree.SubElement(
intf, "encapsulation").text = "ethernet-bridge"
intf_unit = etree.SubElement(intf, "unit")
etree.SubElement(
intf_unit, "name").text = interface_list[0].unit
family = etree.SubElement(intf_unit, "family")
etree.SubElement(family, "bridge")
else:
etree.SubElement(intf, "flexible-vlan-tagging")
etree.SubElement(
intf, "encapsulation").text = "flexible-ethernet-services"
for interface in interface_list:
intf_unit = etree.SubElement(intf, "unit")
etree.SubElement(intf_unit, "name").text = interface.unit
etree.SubElement(
intf_unit, "encapsulation").text = "vlan-bridge"
etree.SubElement(
intf_unit, "vlan-id").text = str(interface.vlan_tag)
# end build_l2_evpn_interface_config
def add_mpls_protocol(self):
proto_config = self.proto_config or etree.Element("protocols")
mpls = etree.SubElement(proto_config, "mpls")
intf = etree.SubElement(mpls, "interface")
etree.SubElement(intf, "name").text = "all"
def set_global_routing_options(self, bgp_params):
if bgp_params['address'] is not None:
self.global_routing_options_config = etree.Element(
"routing-options")
etree.SubElement(
self.global_routing_options_config,
"router-id").text = bgp_params['address']
# end set_global_routing_options
def add_to_global_ri_opts(self, prefix):
if self.global_routing_options_config is None:
self.global_routing_options_config = etree.Element(
"routing-options")
static_config = etree.SubElement(
self.global_routing_options_config, "static")
route_config = etree.SubElement(static_config, "route")
etree.SubElement(route_config, "name").text = prefix
etree.SubElement(route_config, "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_family_etree(self, parent, params):
if params.get('address_families') is None:
return
families = params['address_families'].get('family', [])
if not families:
return
family_etree = etree.SubElement(parent, "family")
for family in families:
if family in self._FAMILY_MAP:
family_subtree = etree.fromstring(self._FAMILY_MAP[family])
family_etree.append(family_subtree)
else:
etree.SubElement(family_etree, family)
# end _add_family_etree
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:
etree.SubElement(
bgp_config, "authentication-key").text = keys[0].get('key')
def add_bgp_hold_time_config(self, bgp_config, bgp_params):
if bgp_params.get('hold_time') is None:
return
etree.SubElement(
bgp_config, "hold-time").text = str(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_config = etree.Element("group", operation="replace")
if external:
etree.SubElement(bgp_config, "name").text = "__contrail_external__"
etree.SubElement(bgp_config, "type").text = "external"
else:
etree.SubElement(bgp_config, "name").text = "__contrail__"
etree.SubElement(bgp_config, "type").text = "internal"
etree.SubElement(bgp_config, "multihop")
local_address = etree.SubElement(bgp_config, "local-address")
local_address.text = self.bgp_params['address']
self._add_family_etree(bgp_config, self.bgp_params)
self.add_bgp_auth_config(bgp_config, self.bgp_params)
self.add_bgp_hold_time_config(bgp_config, self.bgp_params)
etree.SubElement(bgp_config, "keep").text = "all"
return bgp_config
# end _get_bgp_config_xml
def reset_bgp_config(self):
self.routing_instances = {}
self.bgp_params = None
self.ri_config = None
self.tunnel_config = None
self.interfaces_config = None
self.services_config = None
self.policy_config = None
self.firewall_config = None
self.inet_forwarding_filter = None
self.forwarding_options_config = None
self.global_routing_options_config = None
self.proto_config = None
self.logical_interface_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([], 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 = etree.SubElement(bgp_config, "neighbor")
etree.SubElement(nbr, "name").text = peer
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_family_etree(nbr, session_attr)
self.add_bgp_auth_config(nbr, session_attr)
break
peer_as = params.get('local_autonomous_system') or params.get('autonomous_system')
etree.SubElement(nbr, "peer-as").text = str(peer_as)
# end _get_neighbor_config_xml
def send_bgp_config(self):
bgp_config = self._get_bgp_config_xml()
if bgp_config is None:
return 0
proto_config = etree.Element("protocols")
bgp = etree.SubElement(proto_config, "bgp")
bgp.append(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.append(ext_grp_config)
self._get_neighbor_config_xml(ext_grp_config, self.external_peers)
routing_options_config = etree.Element("routing-options")
etree.SubElement(
routing_options_config,
"route-distinguisher-id").text = self.bgp_params['identifier']
local_as = self.bgp_params.get('local_autonomous_system') or self.bgp_params.get('autonomous_system')
etree.SubElement(routing_options_config, "autonomous-system").text = str(local_as)
config_list = [proto_config, routing_options_config]
if self.ri_config is not None:
config_list.append(self.ri_config)
for route_target in self.route_targets:
comm = etree.SubElement(self.policy_config, "community")
etree.SubElement(
comm, 'name').text = route_target.replace(':', '_')
etree.SubElement(comm, 'members').text = route_target
if self.tunnel_config is not None:
config_list.append(self.tunnel_config)
if self.interfaces_config is not None:
config_list.append(self.interfaces_config)
if self.services_config is not None:
config_list.append(self.services_config)
if self.policy_config is not None:
config_list.append(self.policy_config)
if self.firewall_config is not None:
config_list.append(self.firewall_config)
if self.forwarding_options_config is not None:
config_list.append(self.forwarding_options_config)
if self.global_routing_options_config is not None:
config_list.append(self.global_routing_options_config)
if self.proto_config is not None:
config_list.append(self.proto_config)
if self.logical_interface_config is not None:
config_list.append(self.logical_interface_config)
return self.send_netconf(config_list)
# 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