6a2f72144455fdbd92ff9aa8f1eb05b758d51c68
[aaa.git] / aaa-shiro / impl / src / main / resources / convert-shiro-ini-to-rest-payload.py
1 #!/bin/env python
2
3 #
4 # Copyright (c) 2017 Inocybe Technologies and others.  All rights reserved.
5 #
6 # This program and the accompanying materials are made available under the
7 # terms of the Eclipse Public License v1.0 which accompanies this distribution,
8 # and is available at http://www.eclipse.org/legal/epl-v10.html
9 #
10
11 #
12 # convert-shiro-ini-to-rest-payload.py
13 #
14 # Used to help ease upgrades.  In ODL Nitrogen, AAA related application config
15 # is now done via the datastore.  This allows a more cohesive experience in
16 # line with the rest of the controller architecture.  More information about
17 # this can be found here:
18 #
19 # https://bugs.opendaylight.org/show_bug.cgi?id=7793
20 #
21 # This program assumes a correctly formatted shiro.ini file.  No extra checks
22 # are done on shiro.ini.
23 #
24
25 import sys, ConfigParser
26 from xml.etree.ElementTree import Element, SubElement, tostring
27 from xml.dom import minidom
28
29
30 SHIRO_CONFIGURATION = "shiro-configuration"
31 NS = "urn:opendaylight:aaa:app:config"
32 MAIN_SECTION = "main"
33 URLS_SECTION = "urls"
34
35 def convert(filename):
36     '''
37     convert shiro.ini to a XML based representation
38     '''
39     config = ConfigParser.ConfigParser()
40     config.optionxform = str
41     config.read(filename)
42     root = Element(SHIRO_CONFIGURATION)
43     root.attrib['xmlns'] = NS
44     for section in config.sections():
45         if MAIN_SECTION in section or URLS_SECTION in section:
46             for item in config.items(section):
47                 child = SubElement(root, section)
48                 k = SubElement(child, "pair-key")
49                 k.text = item[0]
50                 v = SubElement(child, "pair-value")
51                 v.text = item[1]
52     return root
53
54 def usage():
55     print "Usage:"
56     print "> python convert-shiro-ini-to-rest <filename>"
57
58 if __name__ == '__main__':
59     try:
60         filename = sys.argv[1]
61         et = convert(filename)
62         xmlstr = minidom.parseString(tostring(et)).toprettyxml(indent="    ")
63         print xmlstr
64     except(IndexError):
65         usage()