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.
14 import xml.sax.handler
16 class IpfixEntityHandler(xml.sax.handler.ContentHandler):
18 RECORD_FIELDS = ['name', 'dataType', 'elementId', 'status']
20 # Cf. RFC 5101, Section 6.
32 'boolean': 1, # Not clear.
34 'octetArray': 0, # Not clear.
35 'string': 0, # Not clear.
37 'dateTimeMilliseconds': 8,
38 'dateTimeMicroseconds': 8,
39 'dateTimeNanoseconds': 8,
45 self.current_field_name = None
46 self.current_field_value = []
47 self.current_record = dict()
49 def startDocument(self):
53 #define IPFIX_ENTITY(ENUM, ID, SIZE, NAME)
57 def endDocument(self):
59 #undef IPFIX_ENTITY"""
61 def startElement(self, name, attrs):
62 if name in self.RECORD_FIELDS:
63 self.current_field_name = name
65 self.current_field_name = None
66 self.current_field_value = []
69 def camelcase_to_uppercase(s):
70 return re.sub('(.)([A-Z]+)', r'\1_\2', s).upper()
72 def endElement(self, name):
73 if self.current_field_name is not None:
74 self.current_record[self.current_field_name] = ''.join(
75 self.current_field_value).strip()
76 elif (name == 'record'
77 and self.current_record.get('status') == 'current'
78 and 'dataType' in self.current_record):
80 self.current_record['enumName'] = self.camelcase_to_uppercase(
81 self.current_record['name'])
82 self.current_record['dataTypeSize'] = self.DATA_TYPE_SIZE.get(
83 self.current_record['dataType'], 0)
85 print 'IPFIX_ENTITY(%(enumName)s, %(elementId)s, ' \
86 '%(dataTypeSize)i, %(name)s)' % self.current_record
87 self.current_record.clear()
89 def characters(self, content):
90 if self.current_field_name is not None:
91 self.current_field_value.append(content)
93 def print_ipfix_entity_macros(xml_file):
94 xml.sax.parse(xml_file, IpfixEntityHandler())
98 %(name)s: IPFIX entity definition generator
99 Prints C macros defining IPFIX entities from the standard IANA file at
100 <http://www.iana.org/assignments/ipfix/ipfix.xml>
101 usage: %(name)s [OPTIONS] XML
102 where XML is the standard IANA XML file defining IPFIX entities
104 The following options are also available:
105 -h, --help display this help message
106 -V, --version display version information\
110 if __name__ == '__main__':
113 options, args = getopt.gnu_getopt(sys.argv[1:], 'hV',
115 except getopt.GetoptError, geo:
116 sys.stderr.write('%s: %s\n' % (sys.argv[0], geo.msg))
119 for key, value in options:
120 if key in ['-h', '--help']:
122 elif key in ['-V', '--version']:
123 print 'ipfix-gen-entities (Open vSwitch)'
128 sys.stderr.write('%s: exactly 1 non-option arguments required '
129 '(use --help for help)\n' % sys.argv[0])
132 print_ipfix_entity_macros(args[0])
134 # except Exception, e:
135 # sys.stderr.write('%s: %s\n' % (sys.argv[0], e))