-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyang2cpp.py
More file actions
executable file
·650 lines (556 loc) · 25.2 KB
/
yang2cpp.py
File metadata and controls
executable file
·650 lines (556 loc) · 25.2 KB
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
#!/usr/bin/python
import xml.etree.ElementTree as ET
import argparse
import subprocess
import sys
# Node types strings
NODE_TYPE_MODULE = 'module'
NODE_TYPE_LEAF = 'leaf'
NODE_TYPE_CONTAINER = 'container'
NODE_TYPE_AUGMENT = 'augment'
NODE_TYPE_LIST = 'list'
NODE_TYPE_LEAFLIST = 'leaf-list'
NODE_TYPE_USES = 'uses'
# Conversion from YANG types to C++ types
YangTypeConversion = {
'int8' : 'int8_t',
'uint8' : 'uint8_t',
'int16' : 'int16_t',
'uint16' : 'uint16_t',
'int32' : 'int32_t',
'uint32' : 'uint32_t',
'string' : 'std::string',
}
####################################################################################################
## Convert a YANG node name in a C++ Class name
# @param yangName YANG node name
def yangName2ClassName(yangName):
return yangName.title().replace('-', '').replace('_', '')
####################################################################################################
## Convert a YANG node name in a C++ variable/attribute name
# @param yangName YANG node name
def yangName2VarName(yangName):
return yangName.lower().replace('-', '_') + '_'
####################################################################################################
## Generic node representation
class Node(object):
################################################################################################
## Constructor
# @param self The current object
# @param xmlElem XML Element representing the node to be created
# @param path The path of the node
def __init__(self, xmlElem, path):
if 'name' in xmlElem.attrib:
self.name = xmlElem.attrib['name']
self.path = path
self.children = []
self.valueType = ''
self.key = ''
self.description = ''
################################################################################################
## Retrieve the node name
# @param self The current object
# return Node name
def getName(self):
return self.name
################################################################################################
## Retrieve the node path
# @param self The current object
# return Node path
def getPath(self):
return self.path
################################################################################################
## Set description to node
# @param self The current object
# @param desc Description to be set
def setDescription(self, desc):
self.description = desc
################################################################################################
## Add a child to the current node
# @param self The current object
# @param child Child to be added
def addChildNode(self, child):
self.children.append(child)
################################################################################################
## Retrieve a string containing the line of the leaf C++ instantiation
# @param self The current object
# return String containing the line of the leaf instantiation
def getCppInstantiate(self):
if self.valueType:
return ' CppYangModel::Leaf<' + YangTypeConversion[self.valueType] + '>' + ' '\
+ yangName2VarName(self.name) + ';\n'
if self.key:
return ' std::map<CppYangModel::Leaf<' + YangTypeConversion[self.key.getType()]\
+ '>, ' + yangName2ClassName(self.name) + '>' + ' '\
+ yangName2VarName(self.name) + ';\n'
if self.name:
return ' ' + yangName2ClassName(self.name) + ' ' + yangName2VarName(self.name)\
+ ';\n'
return ''
################################################################################################
## Retrieve a string containing the line of the container C++ object initialization
# @param self The current object
# return Empty string, since the initialization is not needed
def getCppInitializer(self):
return ""
####################################################################################################
## Leaf representation
class Leaf(Node):
################################################################################################
## Constructor
# @param self The current object
# @param xmlElem XML Element representing the leaf to be created
# @param path The path of the leaf
def __init__(self, xmlElem, path):
super(Leaf, self).__init__(xmlElem, path)
# Get type of leaf
valueType = ''
for prop in xmlElem:
propTag = prop.tag.split('}')
propTag = propTag[len(propTag) - 1]
if propTag == 'type':
valueType = prop.attrib['name']
break
self.valueType = valueType
################################################################################################
## Retrieve a string containing the recursive C++ header
# @param self The current object
# return An empty string
def getRecursiveCppHeader(self):
return ''
################################################################################################
## Retrieve a string containing the recursive C++ implementation
# @param self The current object
# return An empty string
def getRecursiveCppImplementation(self):
return ''
################################################################################################
## Retrieve a string containing the line of the leaf C++ object initialization
# @param self The current object
# return String containing the line of the leaf C++ object initialization
def getCppInitializer(self):
return yangName2VarName(self.name) + '("' + self.path + '")'
################################################################################################
## Retrieve the leaf value
# @param self The current object
# return The leaf value
def getType(self):
return self.valueType
################################################################################################
## Retrieve a string containing a representation of the leaf. Used for debug purposes
# @param self The current object
# @param prePrintLine String the must be printed before each line (indentation)
# return String containing the leaf representations
def showRecursive(self, prePrintLine = ''):
print prePrintLine + 'Leaf ' + self.name
print prePrintLine + '| Type: ' + self.valueType
print prePrintLine + '| Path: ' + self.path
print prePrintLine + '\''
####################################################################################################
## Container representation
class Container(Node):
################################################################################################
## Constructor
# @param self The current object
# @param xmlElem XML Element representing the leaf to be created
# @param path The path of the leaf
def __init__(self, xmlElem, path):
super(Container, self).__init__(xmlElem, path)
################################################################################################
## Retrieve a string containing the recursive C++ header, including the headers of its children
# @param self The current object
# return String containing the recursive C++ header
def getRecursiveCppHeader(self):
header = ''
instantiationList = ''
for child in self.children:
header += child.getRecursiveCppHeader()
instantiationList += child.getCppInstantiate()
header += '/******************************************************************************'\
+ '********************/\n'
header += '/**\n'
header += ' * \\brief ' + self.description + '\n'
header += ' */\n'
header += 'class ' + yangName2ClassName(self.name) + ' : public CppYangModel::BasicNode {\n'
header += ' public:\n'
header += ' /**\n'
header += ' * \\brief Constructor\n'
header += ' */\n'
header += ' ' + yangName2ClassName(self.name) + '();\n'
header += '\n'
if instantiationList != '':
header += ' private:\n'
header += instantiationList
header += '};\n\n'
return header
################################################################################################
## Retrieve a string containing the recursive C++ implementation, including its children
# @param self The current object
# return String containing the recursive C++ implementation
def getRecursiveCppImplementation(self):
impl = ''
initializerList = ''
for child in self.children:
impl += child.getRecursiveCppImplementation()
initializer = child.getCppInitializer()
if initializer != '':
initializerList += ',\n ' + child.getCppInitializer()
impl += '/******************************************************************************'\
+ '********************/\n\n'
impl += yangName2ClassName(self.name) + '::' + yangName2ClassName(self.name) + '()\n'
impl += ' : CppYangModel::BasicNode("' + self.path + '")'
## If initializer list is not empty, print it
if initializerList != '':
impl += initializerList + '\n'
impl += '{\n'
impl += '}\n'
impl += '\n'
return impl
################################################################################################
## Retrieve a string containing a representation of the container. Used for debug purposes
# @param self The current object
# @param prePrintLine String the must be printed before each line (indentation)
# return String containing the container representations
def showRecursive(self, prePrintLine = ''):
print prePrintLine + 'Container ' + self.name
print prePrintLine + '| Path: ' + self.path
for child in self.children:
child.showRecursive(prePrintLine + '| ')
print prePrintLine + '\''
####################################################################################################
## List representation
class List(Container):
################################################################################################
## Constructor
# @param self The current object
# @param xmlElem XML Element representing the leaf to be created
# @param path The path of the leaf
def __init__(self, xmlElem, path):
super(List, self).__init__(xmlElem, path)
# Get key
keyName = ''
for prop in xmlElem:
propTag = prop.tag.split('}')
propTag = propTag[len(propTag) - 1]
if propTag == 'key':
keyName = prop.attrib['value']
break
self.keyName = keyName
################################################################################################
## Add a child to the current node instead of the child is the key
# @param self The current object
# @param child Child to be added
def addChildNode(self, child):
# If it's the key of the list, save it as the key, not as a normal child
if child.getName() == self.keyName:
self.key = child
return
self.children.append(child)
################################################################################################
## Retrieve a string containing a representation of the list. Used for debug purposes
# @param self The current object
# @param prePrintLine String the must be printed before each line (indentation)
# return String containing the list representations
def showRecursive(self, prePrintLine = ''):
print prePrintLine + 'List ' + self.name + ' [ ' + self.key.getType() + ' '\
+ self.key.getName() + ' ]'
print prePrintLine + '| Path: ' + self.path
for child in self.children:
child.showRecursive(prePrintLine + '| ')
print prePrintLine + '\''
####################################################################################################
## Augment representation
class Augment(Container):
################################################################################################
## Constructor
# @param self The current object
# @param xmlElem XML Element representing the leaf to be created
# @param path The path of the leaf
def __init__(self, xmlElem, path):
super(Augment, self).__init__(xmlElem, xmlElem.attrib['target-node'] + '/')
self.name = xmlElem.attrib['target-node'][1:].title().replace(":", "_").replace("-", "_")\
.replace("/", "__")
################################################################################################
## Retrieve a string containing a representation of the augment. Used for debug purposes
# @param self The current object
# @param prePrintLine String the must be printed before each line (indentation)
# return String containing the augment representations
def showRecursive(self, prePrintLine = ''):
print prePrintLine + 'Augment ' + self.path
print prePrintLine + '| Path: ' + self.path
for child in self.children:
child.showRecursive(prePrintLine + '| ')
print prePrintLine + '\''
####################################################################################################
## Module representation
class Module(Node):
################################################################################################
## Constructor
# @param self The current object
# @param xmlElem XML Element representing the leaf to be created
# @param path The path of the leaf
def __init__(self, xmlElem, path='/'):
super(Module, self).__init__(xmlElem, path)
self.nodeType = NODE_TYPE_MODULE
################################################################################################
## Retrieve a string containing the recursive C++ header, including the headers of its children
# @param self The current object
# return String containing the recursive C++ header
def getRecursiveCppHeader(self):
header = ''
instantiationList = ''
for child in self.children:
header += child.getRecursiveCppHeader()
instantiationList += child.getCppInstantiate()
header += '/******************************************************************************'\
+ '********************/\n'
header += '/**\n'
header += ' * \\brief ' + self.description + '\n'
header += ' */\n'
header += 'class ' + yangName2ClassName(self.name) + ' {\n'
header += ' public:\n'
header += ' /**\n'
header += ' * \\brief Constructor\n'
header += ' */\n'
header += ' ' + yangName2ClassName(self.name) + '();\n'
header += '\n'
# If instantiation list is not empty, print it
if instantiationList != '':
header += ' private:\n'
header += instantiationList
header += '};'
return header
################################################################################################
## Retrieve a string containing the recursive C++ implementation, including its children
# @param self The current object
# return String containing the recursive C++ implementation
def getRecursiveCppImplementation(self):
impl = ''
initializerList = ''
for child in self.children:
impl += child.getRecursiveCppImplementation()
initializer = child.getCppInitializer()
if initializerList != '' and initializer != '':
initializerList += ',\n '
initializerList += initializer
impl += '/******************************************************************************'\
+ '********************/\n\n'
impl += yangName2ClassName(self.name) + '::' + yangName2ClassName(self.name) + '()\n'
# If initializer list is not empty, print it
if initializerList != '':
impl += ' : ' + initializerList + '\n'
impl += '{\n'
impl += '}\n'
return impl
################################################################################################
## Retrieve a string containing a representation of the module. Used for debug purposes
# @param self The current object
# @param prePrintLine String the must be printed before each line (indentation)
# return String containing the module representations
def showRecursive(self, prePrintLine = ''):
print prePrintLine + 'Module ' + self.name
for child in self.children:
child.showRecursive(prePrintLine + '| ')
print prePrintLine + '\''
####################################################################################################
# Dictionary that maps YANG node type to the related handler class
DataNodeTypes = {
NODE_TYPE_MODULE : Module,
NODE_TYPE_LEAF : Leaf,
NODE_TYPE_CONTAINER : Container,
NODE_TYPE_LIST : List,
NODE_TYPE_AUGMENT : Augment,
}
####################################################################################################
## Handle a description
# @param xmlElem XML node of description
# @param parent Parent node
# return The parent with changes
def handleDescription(xmlElem, parent):
parent.setDescription(xmlElem[0].text);
return parent
####################################################################################################
# Dictionary that maps properties to handler
PropertiesToHandler = {
'description' : handleDescription,
}
####################################################################################################
## Create a node
# @param xmlElem XML element
# @param path Base path
# return The created node
def createNode(xmlElem, path):
tag = xmlElem.tag.split('}')
tag = tag[len(tag) - 1]
if not (tag in DataNodeTypes):
return None
currentPath = ''
if 'name' in xmlElem.attrib:
currentPath = path + xmlElem.attrib['name'] + '/'
node = DataNodeTypes[tag](xmlElem, currentPath)
return node
####################################################################################################
## Iterate over XML element recursively creating nodes
# @param parentNode Parent node
# @param xmlElem XML element
# @param path Base path
def iterateOverNode(parentNode, xmlElem, path = '/'):
for child in xmlElem:
node = createNode(child, path)
if node != None:
parentNode.addChildNode(node)
else:
tag = child.tag.split('}')
tag = tag[len(tag) - 1]
if tag in PropertiesToHandler:
parentNode = PropertiesToHandler[tag](child, parentNode)
continue
iterateOverNode(node, child, node.getPath())
####################################################################################################
# Arguments parsing
parser = argparse.ArgumentParser(description='Convert a given YANG model in a C++ classes model.')
parser.add_argument('-o', '--output', type=str, metavar='PREFIX',
help='Output prefix. Two files (a .h and a .cc) will be created based on this '
'prefix. The default is the YANG module name.')
parser.add_argument('-d', '--output-directory', type=str, metavar='DIR',
help='Path to directory where the output files will be placed in. The default '
'is the current directory.', default='./')
parser.add_argument('-p', '--path', type=str, metavar='PATH1:PATH2', action='append',
help='path is a colon (:) separated list of directories to search for imported '
'modules. This option may be given multiple times.')
parser.add_argument('input', type=str, help='YANG file to be converted.')
args = parser.parse_args()
# Mount pybot command
cmd = ["pyang", args.input, "-f", "yin", "-o", args.input + ".xml"]
if args.path:
for path in args.path:
cmd.append("-p")
cmd.append(path)
if subprocess.call(cmd) != 0:
sys.exit("Error parsing input file: " + args.input)
# Open generated XML
tree = ET.parse(args.input + ".xml")
root = tree.getroot()
# Parse it
rootNode = createNode(root, '')
iterateOverNode(rootNode, root)
header = '/**************************************************************************************'\
+ '************/\n'
header += '/**\n'
header += ' * \\file\n'
header += ' * \\brief ' + rootNode.getName() + ' YANG module representation.\n'
header += ' *\n'
header += ' * WARNING WARNING --> This is an auto generated file <-- WARNING WARNING\n'
header += ' *\n'
header += ' */\n'
header += '/**************************************************************************************'\
+ '************/\n\n'
# Generate header file
headerContent = header
headerContent += '#ifndef __AUTOGEN_' + rootNode.getName().upper() + '_H__\n'
headerContent += '#define __AUTOGEN_' + rootNode.getName().upper() + '_H__\n'
headerContent += '\n'
headerContent += '#include "yang2cpp.h"\n'
headerContent += '\n'
headerContent += rootNode.getRecursiveCppHeader()
headerContent += '\n'
headerContent += '#endif /* __AUTOGEN_' + rootNode.getName().upper() + '_H__ */\n'
outputFile = rootNode.getName()
if args.output:
outputFile = args.output
outputFile += '.h'
f = open(args.output_directory + '/' + outputFile, 'w')
f.write(headerContent)
f.close
# Generate implementation
implementationContent = header
implementationContent += '#include "' + outputFile + '"\n'
implementationContent += '\n'
implementationContent += rootNode.getRecursiveCppImplementation()
outputFile = rootNode.getName()
if args.output:
outputFile = args.output
outputFile += '.cc'
f = open(args.output_directory + '/' + outputFile, 'w')
f.write(implementationContent)
f.close
# Generate basic header
basicHeader = '/*********************************************************************************'\
'*****************/\n'
basicHeader += '/**\n'
basicHeader += ' * \\file\n'
basicHeader += ' * \\brief Basics classes used in YANG generator\n'
basicHeader += ' *\n'
basicHeader += ' * WARNING WARNING --> This is an auto generated file <-- WARNING WARNING\n'
basicHeader += ' *\n'
basicHeader += ' */\n'
basicHeader += '/*********************************************************************************'\
'*****************/\n'
basicHeader += '\n'
basicHeader += '#ifndef __YANG2CPP_H__\n'
basicHeader += '#define __YANG2CPP_H__\n'
basicHeader += '\n'
basicHeader += '#include <string>\n'
basicHeader += '#include <map>\n'
basicHeader += '#include <stdint.h>\n'
basicHeader += '\n'
basicHeader += '/*********************************************************************************'\
'*****************/\n'
basicHeader += '\n'
basicHeader += 'namespace CppYangModel {\n'
basicHeader += '\n'
basicHeader += '/**\n'
basicHeader += ' * \\brief Basic generic node\n'
basicHeader += ' */\n'
basicHeader += 'class BasicNode {\n'
basicHeader += ' public:\n'
basicHeader += ' /**\n'
basicHeader += ' * \\brief Constructor\n'
basicHeader += ' * \param path Path of the node\n'
basicHeader += ' */\n'
basicHeader += ' BasicNode(std::string path) : path_(path) {}\n'
basicHeader += '\n'
basicHeader += ' private:\n'
basicHeader += ' std::string path_;\n'
basicHeader += '};\n'
basicHeader += '\n'
basicHeader += '/*********************************************************************************'\
'*****************/\n'
basicHeader += '/**\n'
basicHeader += ' * \\brief Leaf of the tree\n'
basicHeader += ' */\n'
basicHeader += 'template <class T>\n'
basicHeader += 'class Leaf : public BasicNode {\n'
basicHeader += ' public:\n'
basicHeader += ' /**\n'
basicHeader += ' * \\brief Constructor\n'
basicHeader += ' * \param path Path of the leaf\n'
basicHeader += ' */\n'
basicHeader += ' Leaf(std::string path) : BasicNode(path) {}\n'
basicHeader += '\n'
basicHeader += ' /**\n'
basicHeader += ' * \\brief Set path of the leaf\n'
basicHeader += ' * \param path Path to be set\n'
basicHeader += ' */\n'
basicHeader += ' void setValue(const T& value) {\n'
basicHeader += ' value_ = value;\n'
basicHeader += ' }\n'
basicHeader += '\n'
basicHeader += ' /**\n'
basicHeader += ' * \\brief Get path of the leaf\n'
basicHeader += ' * \\return Path of the list\n'
basicHeader += ' */\n'
basicHeader += ' T getValue() {\n'
basicHeader += ' return value_;\n'
basicHeader += ' }\n'
basicHeader += '\n'
basicHeader += ' private:\n'
basicHeader += ' T value_;\n'
basicHeader += '};\n'
basicHeader += '\n'
basicHeader += '} /* namespace CppYangModel */\n'
basicHeader += '\n'
basicHeader += '#endif /* __YANG2CPP_H__ */\n'
f = open(args.output_directory + '/yang2cpp.h', 'w')
f.write(basicHeader)
f.close