-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathjncdevice.py
974 lines (887 loc) · 38.3 KB
/
jncdevice.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
#!/usr/bin/env python
# Copyright 2011 Leonidas Poulopoulos (GRNET S.A - NOC)
# Copyright 2019 Tomas Cejka (CESNET): fixed namespace
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import re as re_
import os
from lxml import etree as etree_
import flowspec.logging_utils
logger = flowspec.logging_utils.logger_init_default(__name__, "celery_netconf_xml.log", False)
new_ele = lambda tag, attrs={}, **extra: etree_.Element(tag, attrs, **extra)
sub_ele = lambda parent, tag, attrs={}, **extra: etree_.SubElement(parent, tag, attrs, **extra)
NETCONFOPERATION = '{urn:ietf:params:xml:ns:netconf:base:1.0}operation'
# Globals
Tag_pattern_ = re_.compile(r'({.*})?(.*)')
STRING_CLEANUP_PAT = re_.compile(r"[\n\r\s]+")
class Device(object):
def __init__(self):
self.name = ''
self.domain_name = ''
self.interfaces = []
self.vlans = []
self.routing_options = []
self.protocols = {}
def export(self, netconf_config=False):
config = new_ele("configuration", nsmap={None: "http://xml.juniper.net/xnm/1.1/xnm"})
device = new_ele('system')
if self.name:
sub_ele(device, "host-name").text = self.name
if self.domain_name:
sub_ele(device, "domain-name").text = self.domain_name
if len(device.getchildren()):
config.append(device)
interfaces = new_ele('interfaces')
if len(self.interfaces):
for interface in self.interfaces:
if (interface):
interfaces.append(interface.export())
config.append(interfaces)
vlans = new_ele('vlans')
if len(self.vlans):
for vlan in self.vlans:
if (vlan):
vlans.append(vlan.export())
config.append(vlans)
routing_options = new_ele('routing-options')
if len(self.routing_options):
for ro in self.routing_options:
if (ro):
routing_options.append(ro.export())
config.append(routing_options)
protocols = new_ele('protocols')
if len(self.protocols.keys()):
for pro in self.protocols.keys():
protocols.append(self.protocols[pro].export())
config.append(protocols)
if netconf_config:
conf = new_ele("config", nsmap={None: "urn:ietf:params:xml:ns:netconf:base:1.0"})
conf.append(config)
config = conf
if len(config.getchildren()):
return config
else:
return False
def build(self, node):
for child in node:
nodeName_ = Tag_pattern_.match(child.tag).groups()[-1]
self.buildChildren(child, nodeName_)
def buildChildren(self, child_, nodeName_, from_subclass=False):
if nodeName_ == 'interfaces':
for node in child_:
obj_ = Interface()
obj_.build(node)
self.interfaces.append(obj_)
if nodeName_ == 'vlans':
for node in child_:
obj_ = Vlan()
obj_.build(node)
self.vlans.append(obj_)
if nodeName_ == 'routing-options':
for node in child_:
childName_ = Tag_pattern_.match(node.tag).groups()[-1]
logger.info("jncdevice::Device::build(): routing-options childName_="+str(childName_))
flow_node = None
if childName_ == 'flow':
flow_node = node
elif childName_ == 'rib': # IPv6 rule support
for node2 in node:
childName_ = Tag_pattern_.match(node2.tag).groups()[-1]
if childName_ == 'flow':
flow_node = node2
break
# *************** FLOW ****************
if flow_node != None:
obj_ = Flow()
obj_.build(flow_node)
logger.info("jncdevice::Device::build(): => flow_node="+str(flow_node)+" => obj_="+str(obj_))
self.routing_options.append(obj_)
if nodeName_ == 'protocols':
for node in child_:
childName_ = Tag_pattern_.match(node.tag).groups()[-1]
if childName_ == 'l2circuit':
obj_ = L2Circuit()
obj_.build(node)
self.protocols['l2circuit'] = obj_
if childName_ == 'oam':
obj_ = OAM()
obj_.build(node)
self.protocols['oam']=obj_
class DeviceDiff(Device):
_instance = None
def __new__(cls, *args, **kwargs):
if not cls._instance:
cls._instance = super(DeviceDiff, cls).__new__(
cls, *args, **kwargs)
return cls._instance
devdiff = DeviceDiff()
class Interface(object):
def __repr__(self):
return "Name %s, Description: %s" % (self.name, self.description)
def __init__(self,name=None,description=None):
self.name = name
self.bundle = ''
self.description = description
self.vlantagging = ''
self.tunneldict = []
# Unit dict is a list of dictionaries containing units to
# interfaces, should be index like {'unit': 'name',
# 'description': 'foo', 'vlanid': 'bar', 'addresses': ['IPv4addresses', 'IPv6addresses']}
self.unitdict = []
def get_descr(self): return self.description
def set_descr(self,x):
global devdiff
self.description = x
intdiff = Interface(name=self.name, description=self.description)
if len(devdiff.interfaces) > 0:
deviffIntNames = [x.name for x in devdiff.interfaces]
if self.name in deviffIntNames:
for interface in devdiff.interfaces:
if interface.name == self.name:
devdiff.interfaces.remove(interface)
devdiff.interfaces.append(intdiff)
# The new_description attribute initiates the DeviceDiff class
new_description = property(get_descr,set_descr)
def export(self):
ifce = new_ele('interface')
if self.name:
sub_ele(ifce, "name").text = self.name
if self.description:
sub_ele(ifce, "description").text = self.description
if len(self.unitdict):
for unit in self.unitdict:
if unit:
ifce.append(unit.export())
if len(ifce.getchildren()):
return ifce
else:
return False
def build(self, node):
for child in node:
nodeName_ = Tag_pattern_.match(child.tag).groups()[-1]
self.buildChildren(child, nodeName_)
def buildChildren(self, child_, nodeName_, from_subclass=False):
if nodeName_ == 'name':
name_ = child_.text
name_ = re_.sub(STRING_CLEANUP_PAT, " ", name_).strip()
self.name = name_
elif nodeName_ == 'description':
description_ = child_.text
description_ = re_.sub(STRING_CLEANUP_PAT, " ", description_).strip()
self.description = description_
elif nodeName_ == 'unit':
obj_ = Unit()
obj_.build(child_)
self.unitdict.append(obj_)
class Vlan:
def __repr__(self):
return "Name %s, Vlan-Id: %s" % (self.name, self.vlan_id)
def __init__(self):
self.name = ''
self.vlan_id = ''
self.operation = None
def export(self):
if self.operation:
vlan = new_ele('vlan', {NETCONFOPERATION: self.operation})
else:
vlan = new_ele('vlan')
if self.name:
sub_ele(vlan, "name").text = self.name
if self.vlan_id:
sub_ele(vlan, "vlan-id").text = self.vlan_id
if self.name and self.vlan_id:
return vlan
else:
return False
def build(self, node):
for child in node:
nodeName_ = Tag_pattern_.match(child.tag).groups()[-1]
self.buildChildren(child, nodeName_)
def buildChildren(self, child_, nodeName_, from_subclass=False):
if nodeName_ == 'name':
name_ = child_.text
name_ = re_.sub(STRING_CLEANUP_PAT, " ", name_).strip()
self.name = name_
elif nodeName_ == 'vlan-id':
vlanid_ = child_.text
vlanid_ = re_.sub(STRING_CLEANUP_PAT, " ", vlanid_).strip()
self.vlan_id = vlanid_
class Unit:
def __repr__(self):
return "Name %s, Description: %s" % (self.name, self.description)
def __init__(self):
self.name = ''
self.description = ''
self.vlan_id = ''
self.encapsulation = ''
self.apply_groups = ''
self.input_vlan_map = {'swap':False, 'vlan_id':''}
self.output_vlan_map = {'swap':False, 'vlan_id':''}
#family: {'name':(one of inet, inet6, mpls, iso...), 'addresses':[], 'mtu':'', 'accounting': {}, 'vlan_members':['',''], 'vlan_members_operation':'delete' or 'replace' or 'merge'(this is the default so omit)}
self.family = []
def export(self):
unit = new_ele('unit')
if self.name:
sub_ele(unit, "name").text = self.name
if self.description:
sub_ele(unit, "description").text = self.description
if self.apply_groups:
sub_ele(unit, "apply-groups").text = self.apply_groups
if self.encapsulation:
sub_ele(unit, "encapsulation").text = self.encapsulation
if self.vlan_id:
sub_ele(unit, "vlan-id").text = self.vlan_id
if self.input_vlan_map['swap'] or self.input_vlan_map['vlan_id']:
ivm = new_ele('input-vlan-map')
sub_ele(ivm,"swap")
if self.input_vlan_map['vlan_id']:
sub_ele(ivm,"vlan-id").text = self.input_vlan_map['vlan_id']
unit.append(ivm)
if self.output_vlan_map['swap'] or self.output_vlan_map['vlan_id']:
ovm = new_ele('output-vlan-map')
sub_ele(ovm,"swap")
if self.output_vlan_map['vlan_id']:
sub_ele(ovm,"vlan-id").text = self.output_vlan_map['vlan_id']
unit.append(ovm)
if len(self.family):
family = new_ele("family")
for member in self.family:
try:
if member['name']:
mem_name = new_ele(member['name'])
except:
pass
try:
if len(member['addresses']):
for address in member['addresses']:
addr = new_ele('address')
sub_ele(addr,"name").text = address
mem_name.append(addr)
except:
pass
try:
if member['mtu']:
sub_ele(mem_name, "mtu").text = member['mtu']
family.append(mem_name)
except:
pass
try:
if member['vlan_members']:
try:
if member['vlan_members_operation']:
operation = member['vlan_members_operation']
except:
operation = None
ethernet_switching = sub_ele(family,'ethernet-switching')
vlan = sub_ele(ethernet_switching, 'vlan')
for vlan_item in member['vlan_members']:
if operation:
vmem = sub_ele(vlan,'members', {NETCONFOPERATION: operation})
else:
vmem = sub_ele(vlan,'members')
vmem.text = vlan_item
except:
pass
unit.append(family)
if len(unit.getchildren()):
return unit
else:
return False
def build(self, node):
for child in node:
nodeName_ = Tag_pattern_.match(child.tag).groups()[-1]
self.buildChildren(child, nodeName_)
def buildChildren(self, child_, nodeName_, from_subclass=False):
if nodeName_ == 'encapsulation':
encapsulation_ = child_.text
encapsulation_ = re_.sub(STRING_CLEANUP_PAT, " ", encapsulation_).strip()
self.encapsulation = encapsulation_
if nodeName_ == 'apply-groups':
apply_groups_ = child_.text
apply_groups_ = re_.sub(STRING_CLEANUP_PAT, " ", apply_groups_).strip()
self.apply_groups = apply_groups_
if nodeName_ == 'name':
name_ = child_.text
name_ = re_.sub(STRING_CLEANUP_PAT, " ", name_).strip()
self.name = name_
elif nodeName_ == 'vlan-id':
vlanid_ = child_.text
vlanid_ = re_.sub(STRING_CLEANUP_PAT, " ", vlanid_).strip()
self.vlan_id = vlanid_
elif nodeName_ == 'description':
description_ = child_.text
description_ = re_.sub(STRING_CLEANUP_PAT, " ", description_).strip()
self.description = description_
elif nodeName_ == 'input-vlan-map':
for node in child_:
childName_ = Tag_pattern_.match(node.tag).groups()[-1]
if childName_ == 'swap':
self.input_vlan_map['swap'] = True
if childName_ == 'vlan-id':
vlan_id = node.text
vlan_id = re_.sub(STRING_CLEANUP_PAT, " ", vlan_id).strip()
self.input_vlan_map['vlan_id'] = vlan_id
elif nodeName_ == 'output-vlan-map':
for node in child_:
childName_ = Tag_pattern_.match(node.tag).groups()[-1]
if childName_ == 'swap':
self.output_vlan_map['swap'] = True
if childName_ == 'vlan-id':
vlan_id = node.text
vlan_id = re_.sub(STRING_CLEANUP_PAT, " ", vlan_id).strip()
self.output_vlan_map['vlan_id'] = vlan_id
elif nodeName_ == 'family':
vlan_unit_list = []
family_dict = {}
for node in child_:
childName_ = Tag_pattern_.match(node.tag).groups()[-1]
# *************** ETHERNET-SWITCHING ****************
if childName_ == 'ethernet-switching':
for grandChild_ in node:
grandchildName_ = Tag_pattern_.match(grandChild_.tag).groups()[-1]
if grandchildName_ == 'port-mode':
pmode = grandChild_.text
pmode = re_.sub(STRING_CLEANUP_PAT, " ", pmode).strip()
family_dict['port-mode'] = pmode
elif grandchildName_ == 'vlan':
for vlan_member in grandChild_:
vlanmem = vlan_member.text
vlanmem = re_.sub(STRING_CLEANUP_PAT, " ", vlanmem).strip()
vlan_unit_list.append(vlanmem)
family_dict['vlan_members'] = vlan_unit_list
self.family.append(family_dict)
class Flow(object):
def __init__(self, is_ipv4 = True):
self.routes = []
self.is_ipv4 = is_ipv4
logger.info("jncdevice::Flow::Flow(): is_ipv4="+str(is_ipv4))
def export(self):
flow = new_ele('flow')
if self.is_ipv4:
flow0 = flow
else:
flow0 = new_ele('rib')
sub_ele(flow0, 'name').text = 'inet6.0'
flow0.append(flow)
if len(self.routes):
for route in self.routes:
flow.append(route.export())
return flow0
else:
return False
def build(self, node):
for child in node:
nodeName_ = Tag_pattern_.match(child.tag).groups()[-1]
self.buildChildren(child, nodeName_)
def buildChildren(self, child_, nodeName_, from_subclass=False):
if nodeName_ == 'route':
obj_ = Route()
obj_.build(child_)
self.routes.append(obj_)
class Route(object):
def __init__(self):
self.name = ''
self.operation = None
self.match = {
"destination": [],
"destination-v6": [],
"source": [],
"source-v6": [],
"protocol": [],
"port": [],
"destination-port": [],
"source-port": [],
"icmp-code": [],
"icmp-type": [],
"tcp-flags": [],
"packet-length": [],
"dscp": [],
"fragment": []
}
''' Match is a dict with list values
example: self. match = {
"destination": [<ip-prefix(es)>],
"source": [<ip-prefix(es)>],
"protocol": [<numeric-expression(s)>],
"port": [<numeric-expression(s)>],
"destination-port": [<numeric-expression(s)>]
"source-port": [<numeric-expression(s)>],
"icmp-code": [<numeric-expression(s)>],
"icmp-type": [<numeric-expression(s)>],
"tcp-flags": [<bitwise-expression(s)>],
"packet-length": [<numeric-expression(s)>],
"dscp": [<numeric-expression(s)>],
"fragment": [
"dont-fragment"
"not-a-fragment"
"is-fragment"
"first-fragment"
"last-fragment"
]
'''
self.then = {
"accept": False,
"discard": False,
"community": False,
"next-term": False,
"rate-limit": False,
"sample": False,
"routing-instance": False
}
'''Then is a dict (have to see about this in the future:
self.then = {
"accept": True/False,
"discard": True/False,
"community": "<name>"/False,
"next-term": True/False,
"rate-limit": <rate>/False,
"sample": True/False,
"routing-instance": "<RouteTarget extended community>"
}
'''
def export(self):
if self.operation:
ro = new_ele('route', {NETCONFOPERATION: self.operation})
else:
ro = new_ele('route')
if self.name:
sub_ele(ro, "name").text = self.name
match = new_ele("match")
for key in self.match:
if self.match[key]:
if key=="destination-v6":
for value in self.match[key]:
newsub = sub_ele(match, "destination")
sub_ele(newsub, "prefix").text = value
elif key=="source-v6":
for value in self.match[key]:
newsub = sub_ele(match, "source")
sub_ele(newsub, "prefix").text = value
else:
for value in self.match[key]:
sub_ele(match,key).text = value
if match.getchildren():
ro.append(match)
then = new_ele("then")
for key in self.then:
if self.then[key]:
if self.then[key] != True and self.then[key] != False:
sub_ele(then,key).text = self.then[key]
else:
sub_ele(then,key)
if then.getchildren():
ro.append(then)
if ro.getchildren():
return ro
else:
return False
def build(self, node):
for child in node:
nodeName_ = Tag_pattern_.match(child.tag).groups()[-1]
self.buildChildren(child, nodeName_)
def buildChildren(self, child_, nodeName_, from_subclass=False):
if nodeName_ == 'name':
name_ = child_.text
name_ = re_.sub(STRING_CLEANUP_PAT, " ", name_).strip()
self.name = name_
elif nodeName_ == 'match':
for grandChild_ in child_:
grandChildName_ = Tag_pattern_.match(grandChild_.tag).groups()[-1]
grandChildText = grandChild_.text
grandChildText = re_.sub(STRING_CLEANUP_PAT, " ", grandChildText).strip()
self.match[grandChildName_].append(grandChildText)
elif nodeName_ == 'then':
for grandChild_ in child_:
grandChildName_ = Tag_pattern_.match(grandChild_.tag).groups()[-1]
self.then[grandChildName_] = True
class Parser(object):
def __init__(self, confile=None):
self.confile = confile
def export(self):
if self.confile:
confile = self.confile
if os.path.isfile(confile):
# probably it's a file...
configuration = self.parse()
else:
configuration = self.parseString()
return configuration
else:
return None
def parsexml_(self, *args, **kwargs):
if 'parser' not in kwargs:
kwargs['parser'] = etree_.ETCompatXMLParser()
doc = etree_.parse(*args, **kwargs)
return doc
def parse(self):
'''Normally this would be an rpc_reply in case of netconf invoking or
a configuration element in case of normal parsing'''
doc = self.parsexml_(self.confile)
rootNode = doc.getroot()
#NetCONF invoked
rootNodeTag = Tag_pattern_.match(rootNode.tag).groups()[-1]
if rootNodeTag == 'rpc-reply':
rootNode = rootNode.xpath("//*[local-name()='configuration']")[0]
if rootNodeTag == 'data':
rootNode = rootNode.xpath("//*[local-name()='configuration']")[0]
rootObj = Device()
rootObj.build(rootNode)
return rootObj
def parseString(self):
'''Normally this would be an rpc_reply in case of netconf invoking or
a configuration element in case of normal parsing'''
#from StringIO import StringIO
from io import StringIO
from io import BytesIO
doc = self.parsexml_(BytesIO(self.confile.encode()))
rootNode = doc.getroot()
rootNodeTag = Tag_pattern_.match(rootNode.tag).groups()[-1]
if rootNodeTag == 'rpc-reply':
rootNode = rootNode.xpath("//*[local-name()='configuration']")[0]
if rootNodeTag == 'data':
rootNode = rootNode.xpath("//*[local-name()='configuration']")[0]
rootObj = Device()
rootObj.build(rootNode)
return rootObj
class L2Circuit(object):
def __init__(self):
self.neighbors = []
def export(self):
l2circuit = new_ele('l2circuit')
if len(self.neighbors):
for neighbor in self.neighbors:
try:
l2circuit.append(neighbor.export())
except TypeError:
pass
return l2circuit
else:
return False
def build(self, node):
for child in node:
nodeName_ = Tag_pattern_.match(child.tag).groups()[-1]
self.buildChildren(child, nodeName_)
def buildChildren(self, child_, nodeName_, from_subclass=False):
if nodeName_ == 'neighbor':
obj_ = L2CNeighbor()
obj_.build(child_)
self.neighbors.append(obj_)
class OAM(object):
def __init__(self):
self.ethernet = ''
def export(self):
oam = new_ele('oam')
if (self.ethernet):
try:
oam.append(self.ethernet.export())
except TypeError:
pass
return oam
else:
return False
def build(self, node):
for child in node:
nodeName_ = Tag_pattern_.match(child.tag).groups()[-1]
self.buildChildren(child, nodeName_)
def buildChildren(self, child_, nodeName_, from_subclass=False):
if nodeName_ == 'ethernet':
obj_ = EthernetOAM()
obj_.build(child_)
self.ethernet = obj_
class EthernetOAM(object):
def __init__(self):
self.connectivity_fault_management = ''
def export(self):
ethoam = new_ele('ethernet')
if (self.connectivity_fault_management):
try:
ethoam.append(self.connectivity_fault_management.export())
except TypeError:
pass
return ethoam
else:
return False
def build(self, node):
for child in node:
nodeName_ = Tag_pattern_.match(child.tag).groups()[-1]
self.buildChildren(child, nodeName_)
def buildChildren(self, child_, nodeName_, from_subclass=False):
if nodeName_ == 'connectivity-fault-management':
obj_ = EthernetOAMCFM()
obj_.build(child_)
self.connectivity_fault_management = obj_
class EthernetOAMCFM(object):
def __init__(self):
self.maintenance_domains = []
def export(self):
ethoamcfm = new_ele('connectivity-fault-management')
if len(self.maintenance_domains):
for md in self.maintenance_domains:
try:
ethoamcfm.append(md.export())
except TypeError:
pass
return ethoamcfm
else:
return False
def build(self, node):
for child in node:
nodeName_ = Tag_pattern_.match(child.tag).groups()[-1]
self.buildChildren(child, nodeName_)
def buildChildren(self, child_, nodeName_, from_subclass=False):
if nodeName_ == 'maintenance-domain':
obj_ = CFMMD()
obj_.build(child_)
self.maintenance_domains.append(obj_)
class CFMMD(object):
def __repr__(self):
return "MD: %s Lvl: %s" % (self.name, self.level)
def __init__(self):
self.name = ''
self.level = ''
self.operation = None
self.maintenance_association = ''
def export(self):
if self.operation:
md = new_ele('maintenance-domain', {NETCONFOPERATION:self.operation})
else:
md = new_ele('maintenance-domain')
if self.name:
sub_ele(md, "name").text = self.name
if self.level:
sub_ele(md, "level").text = str(self.level)
if self.maintenance_association:
md.append(self.maintenance_association.export())
return md
def build(self, node):
for child in node:
nodeName_ = Tag_pattern_.match(child.tag).groups()[-1]
self.buildChildren(child, nodeName_)
def buildChildren(self, child_, nodeName_, from_subclass=False):
if nodeName_ == 'name':
name_ = child_.text
name_ = re_.sub(STRING_CLEANUP_PAT, " ", name_).strip()
self.name = name_
if nodeName_ == 'level':
level_ = child_.text
level_ = re_.sub(STRING_CLEANUP_PAT, " ", level_).strip()
self.level = level_
if nodeName_ == 'maintenance-association':
obj_ = MaintenanceAssoc()
obj_.build(child_)
self.maintenance_association = obj_
#TODO: Implement MA
class MaintenanceAssoc(object):
def __repr__(self):
return "MA: %s MEP: %s, Ifce: %s, Dir: %s" % (self.name, self.mep['name'], self.mep['ifce'], self.mep['direction'])
def __init__(self, name=None, cc_interval="1s", cc_lt="3", cc_hi="7", cc_ifce_tlv=True, cc_port_tlv=False, mip_hf=None, mep_name=None, mep_ifce=None, mep_ifce_vlan=None, mep_direction=None, mep_auto_disco=True, mep_rem_name=None, sla_iter_profiles=[]):
self.name = name
self.operation = None
self.continuity_check = {"interval":cc_interval,
"loss_threshold": cc_lt,
"hold_interval": cc_hi,
"interface_status_tlv": cc_ifce_tlv,
"port_status_tlv": cc_port_tlv
}
self.mip_half_function = mip_hf
self.mep = {"name": mep_name,
"ifce": mep_ifce,
"ifce_vlan": mep_ifce_vlan,
"direction": mep_direction,
"auto_discovery": mep_auto_disco,
"remote_mep":{"name":mep_rem_name, "sla_iterator_profiles": sla_iter_profiles}
}
def export(self):
if self.operation:
ma = new_ele("maintenance-association", {NETCONFOPERATION: self.operation})
else:
ma = new_ele("maintenance-association")
if self.name:
sub_ele(ma, "name").text = self.name
if self.mip_half_function:
sub_ele(ma, "mip-half-function").text = self.mip_half_function
cc = sub_ele(ma, "continuity-check")
if self.continuity_check['interval']:
sub_ele(cc, "interval").text = self.continuity_check['interval']
if self.continuity_check['loss_threshold']:
sub_ele(cc, "loss-threshold").text = self.continuity_check['loss_threshold']
if self.continuity_check['hold_interval']:
sub_ele(cc, "hold-interval").text = self.continuity_check['hold_interval']
if self.continuity_check['interface_status_tlv']:
sub_ele(cc, "interface-status-tlv")
if self.continuity_check['port_status_tlv']:
sub_ele(cc, "port-status-tlv")
mep = sub_ele(ma, "mep")
if self.mep['name']:
sub_ele(mep, 'name').text = self.mep['name']
if self.mep['direction']:
sub_ele(mep, 'direction').text = self.mep['direction']
if self.mep['auto_discovery']:
sub_ele(mep, 'auto-discovery')
if self.mep['ifce']:
ifce = sub_ele(mep, 'interface')
sub_ele(ifce, "interface-name").text = self.mep['ifce']
if self.mep['ifce_vlan']:
sub_ele(ifce, "vlan-id").text = self.mep['ifce_vlan']
if self.mep['remote_mep']['name'] and self.mep['remote_mep']['sla_iterator_profiles']:
rmep = sub_ele(mep, "remote-mep")
sub_ele(rmep, 'name').text = self.mep['remote_mep']['name']
for sip in self.mep['remote_mep']['sla_iterator_profiles']:
slaip = sub_ele(rmep, 'sla-iterator-profile')
sub_ele(slaip, 'name').text = sip
return ma
def build(self, node):
for child in node:
nodeName_ = Tag_pattern_.match(child.tag).groups()[-1]
self.buildChildren(child, nodeName_)
def buildChildren(self, child_, nodeName_, from_subclass=False):
if nodeName_ == 'name':
name_ = child_.text
name_ = re_.sub(STRING_CLEANUP_PAT, " ", name_).strip()
self.name = name_
if nodeName_ == 'mip-half-function':
mhf_ = child_.text
mhf_ = re_.sub(STRING_CLEANUP_PAT, " ", mhf_).strip()
self.mip_half_function = mhf_
if nodeName_ == 'continuity-check':
for grandChild_ in child_:
grandChildName_ = Tag_pattern_.match(grandChild_.tag).groups()[-1]
if grandChildName_ == 'interval':
grandChildText = grandChild_.text
grandChildText = re_.sub(STRING_CLEANUP_PAT, " ", grandChildText).strip()
self.continuity_check['interval'] = grandChildText
if grandChildName_ == 'loss-threshold':
grandChildText = grandChild_.text
grandChildText = re_.sub(STRING_CLEANUP_PAT, " ", grandChildText).strip()
self.continuity_check['loss_threshold'] = grandChildText
if grandChildName_ == 'hold-interval':
grandChildText = grandChild_.text
grandChildText = re_.sub(STRING_CLEANUP_PAT, " ", grandChildText).strip()
self.continuity_check['hold_interval'] = grandChildText
if grandChildName_ == 'interface-status-tlv':
self.continuity_check['interface_status_tlv'] = True
if grandChildName_ == 'port-status-tlv':
self.continuity_check['port_status_tlv'] = True
if nodeName_ == 'mep':
for grandChild_ in child_:
grandChildName_ = Tag_pattern_.match(grandChild_.tag).groups()[-1]
if grandChildName_ == 'name':
grandChildText = grandChild_.text
grandChildText = re_.sub(STRING_CLEANUP_PAT, " ", grandChildText).strip()
self.mep['name'] = grandChildText
if grandChildName_ == 'interface':
for grandgrandChild_ in grandChild_:
grandgrandChildName_ = Tag_pattern_.match(grandgrandChild_.tag).groups()[-1]
if grandgrandChildName_ == 'interface-name':
grandgrandChildText = grandgrandChild_.text
grandgrandChildText = re_.sub(STRING_CLEANUP_PAT, " ", grandgrandChildText).strip()
self.mep['ifce'] = grandgrandChildText
if grandgrandChildName_ == 'vlan-id':
grandgrandChildText = grandgrandChild_.text
grandgrandChildText = re_.sub(STRING_CLEANUP_PAT, " ", grandgrandChildText).strip()
self.mep['ifce_vlan'] = grandgrandChildText
if grandChildName_ == 'direction':
grandChildText = grandChild_.text
grandChildText = re_.sub(STRING_CLEANUP_PAT, " ", grandChildText).strip()
self.mep['direction'] = grandChildText
if grandChildName_ == 'auto-discovery':
self.mep['auto_discovery'] = True
if grandChildName_ == 'remote-mep':
self.mep['remote_mep']['sla_iterator_profiles'] = []
for grandgrandChild_ in grandChild_:
grandgrandChildName_ = Tag_pattern_.match(grandgrandChild_.tag).groups()[-1]
if grandgrandChildName_ == 'name':
grandgrandChildText = grandgrandChild_.text
grandgrandChildText = re_.sub(STRING_CLEANUP_PAT, " ", grandgrandChildText).strip()
self.mep['remote_mep']['name'] = grandgrandChildText
if grandgrandChildName_ == 'sla-iterator-profile':
for grand3Child_ in grandgrandChild_:
grand3ChildName_ = Tag_pattern_.match(grand3Child_.tag).groups()[-1]
if grand3ChildName_ == 'name':
grand3ChildText = grand3Child_.text
grand3ChildText = re_.sub(STRING_CLEANUP_PAT, " ", grand3ChildText).strip()
self.mep['remote_mep']['sla_iterator_profiles'].append(grand3ChildText)
class L2CNeighbor(object):
def __repr__(self):
return "Name %s" % (self.name)
def __init__(self):
self.name = ''
self.interfaces = []
def export(self):
l2cneighbor = new_ele('neighbor')
if self.name:
sub_ele(l2cneighbor, "name").text = self.name
if len(self.interfaces):
for ifce in self.interfaces:
l2cneighbor.append(ifce.export())
return l2cneighbor
else:
return False
def build(self, node):
for child in node:
nodeName_ = Tag_pattern_.match(child.tag).groups()[-1]
self.buildChildren(child, nodeName_)
def buildChildren(self, child_, nodeName_, from_subclass=False):
if nodeName_ == 'name':
name_ = child_.text
name_ = re_.sub(STRING_CLEANUP_PAT, " ", name_).strip()
self.name = name_
if nodeName_ == 'interface':
obj_ = L2CIfce()
obj_.build(child_)
self.interfaces.append(obj_)
class L2CIfce(object):
def __repr__(self):
return "Name %s, VCID: %s, MTU: %s" % (self.name, self.virtual_circuit_id, self.mtu)
def __init__(self):
self.name = ''
self.virtual_circuit_id = ''
self.description = ''
self.mtu = ''
self.no_control_word = False
def export(self):
ifce = new_ele('interface')
if self.name:
sub_ele(ifce, "name").text = self.name
if self.virtual_circuit_id:
sub_ele(ifce, "virtual-circuit-id").text = str(self.virtual_circuit_id)
if self.description:
sub_ele(ifce, "description").text = self.description
if self.mtu:
sub_ele(ifce, "mtu").text = str(self.mtu)
if self.no_control_word:
sub_ele(ifce, "no-control-word")
return ifce
def build(self, node):
for child in node:
nodeName_ = Tag_pattern_.match(child.tag).groups()[-1]
self.buildChildren(child, nodeName_)
def buildChildren(self, child_, nodeName_, from_subclass=False):
if nodeName_ == 'name':
name_ = child_.text
name_ = re_.sub(STRING_CLEANUP_PAT, " ", name_).strip()
self.name = name_
if nodeName_ == 'virtual-circuit-id':
virtual_circuit_id_ = child_.text
virtual_circuit_id_ = re_.sub(STRING_CLEANUP_PAT, " ", virtual_circuit_id_).strip()
self.virtual_circuit_id = virtual_circuit_id_
if nodeName_ == 'description':
description_ = child_.text
description_ = re_.sub(STRING_CLEANUP_PAT, " ", description_).strip()
self.description = description_
if nodeName_ == 'mtu':
mtu_ = child_.text
mtu_ = re_.sub(STRING_CLEANUP_PAT, " ", mtu_).strip()
self.mtu = mtu_
if nodeName_ == 'no-control-word':
self.no_control_word = True