]>
git.proxmox.com Git - ovs.git/blob - ofproto/ipfix-gen-entities
3 # Copyright (C) 2012 Nicira, Inc.
5 # Copying and distribution of this file, with or without modification,
6 # are permitted in any medium without royalty provided the copyright
7 # notice and this notice are preserved. This file is offered as-is,
8 # without warranty of any kind.
10 from __future__
import print_function
16 import xml
.sax
.handler
19 class IpfixEntityHandler(xml
.sax
.handler
.ContentHandler
):
21 RECORD_FIELDS
= ['name', 'dataType', 'elementId', 'status']
23 # Cf. RFC 5101, Section 6.
35 'boolean': 1, # Not clear.
37 'octetArray': 0, # Not clear.
38 'string': 0, # Not clear.
40 'dateTimeMilliseconds': 8,
41 'dateTimeMicroseconds': 8,
42 'dateTimeNanoseconds': 8,
48 self
.current_field_name
= None
49 self
.current_field_value
= []
50 self
.current_record
= dict()
52 def startDocument(self
):
56 #define IPFIX_ENTITY(ENUM, ID, SIZE, NAME)
60 def endDocument(self
):
62 #undef IPFIX_ENTITY""")
64 def startElement(self
, name
, attrs
):
65 if name
in self
.RECORD_FIELDS
:
66 self
.current_field_name
= name
68 self
.current_field_name
= None
69 self
.current_field_value
= []
72 def camelcase_to_uppercase(s
):
73 return re
.sub('(.)([A-Z]+)', r
'\1_\2', s
).upper()
75 def endElement(self
, name
):
76 if self
.current_field_name
is not None:
77 self
.current_record
[self
.current_field_name
] = ''.join(
78 self
.current_field_value
).strip()
79 elif (name
== 'record'
80 and self
.current_record
.get('status') == 'current'
81 and 'dataType' in self
.current_record
):
83 self
.current_record
['enumName'] = self
.camelcase_to_uppercase(
84 self
.current_record
['name'])
85 self
.current_record
['dataTypeSize'] = self
.DATA_TYPE_SIZE
.get(
86 self
.current_record
['dataType'], 0)
88 print('IPFIX_ENTITY(%(enumName)s, %(elementId)s, '
89 '%(dataTypeSize)i, %(name)s)' % self
.current_record
)
90 self
.current_record
.clear()
92 def characters(self
, content
):
93 if self
.current_field_name
is not None:
94 self
.current_field_value
.append(content
)
97 def print_ipfix_entity_macros(xml_file
):
98 xml
.sax
.parse(xml_file
, IpfixEntityHandler())
103 %(name)s: IPFIX entity definition generator
104 Prints C macros defining IPFIX entities from the standard IANA file at
105 <http://www.iana.org/assignments/ipfix/ipfix.xml>
106 usage: %(name)s [OPTIONS] XML
107 where XML is the standard IANA XML file defining IPFIX entities
109 The following options are also available:
110 -h, --help display this help message
111 -V, --version display version information\
112 """ % {'name': name
})
115 if __name__
== '__main__':
117 options
, args
= getopt
.gnu_getopt(sys
.argv
[1:], 'hV',
119 except getopt
.GetoptError
as geo
:
120 sys
.stderr
.write('%s: %s\n' % (sys
.argv
[0], geo
.msg
))
123 for key
, value
in options
:
124 if key
in ['-h', '--help']:
126 elif key
in ['-V', '--version']:
127 print('ipfix-gen-entities (Open vSwitch)')
132 sys
.stderr
.write('%s: exactly 1 non-option arguments required '
133 '(use --help for help)\n' % sys
.argv
[0])
136 print_ipfix_entity_macros(args
[0])