create the data directory if it doesnt already exist
[sfa.git] / sfa / util / config.py
1 ##
2 # Geniwrapper Configuration Info
3 #
4 # This module holds configuration parameters for geniwrapper. There are two
5 # main pieces of information that are used: the database connection and
6 # the PLCAPI connection
7 ##
8
9 ##
10 # Geniwrapper uses a MYSQL database to store records. This database may be
11 # co-located with the PLC database, or it may be a separate database. The
12 # following parameters define the connection to the database.
13 #
14 # Note that Geniwrapper does not access any of the PLC databases directly via
15 # a mysql connection; All PLC databases are accessed via PLCAPI.
16
17 ### $Id$
18 ### $URL$
19
20 import os.path
21 import traceback
22
23 from sfa.util.debug import log
24
25 class Config:
26     """
27     Parse the bash/Python/PHP version of the configuration file. Very
28     fast but no type conversions.
29     """
30
31     def __init__(self, config_file = "/etc/sfa/sfa_config"):
32         self.config_file = None
33         self.config_path = None
34         self.data_path = None
35         self.load(config_file)
36
37     def load(self, config_file):
38         try:
39             execfile(config_file, self.__dict__)
40             self.config_file = config_file
41             # path to configuration data
42             self.config_path = os.path.dirname(config_file)
43             # path to server data
44             if not hasattr(self, 'SFA_DATA_DIR'):
45                 # default to /var/lib/sfa not specified in config
46                 self.SFA_DATA_DIR="/var/lib/sfa"
47             # path to config data
48             if not hasattr(self, 'SFA_CONFIG_DIR'):
49                 # default to /var/lib/sfa not specified in config
50                 self.SFA_CONFIG_DIR="/etc/sfa"
51
52             # create the data directory if it doesnt exist
53             if not os.path.isdir(self.SFA_DATA_DIR):
54                 os.mkdir(self.SFA_DATA_DIR)
55              
56         except IOError, e:
57             raise IOError, "Could not find the configuration file: %s" % config_file
58
59     def get_trustedroots_dir(self):
60         return self.config_path + os.sep + 'trusted_roots'
61
62     def get_openflow_aggrMgr_info(self):
63         aggr_mgr_ip = 'localhost'
64         if (hasattr(self,'OPENFLOW_AGGREGATE_MANAGER_IP')):
65             aggr_mgr_ip = self.OPENFLOW_AGGREGATE_MANAGER_IP
66
67         aggr_mgr_port = 2603
68         if (hasattr(self,'OPENFLOW_AGGREGATE_MANAGER_PORT')):
69             aggr_mgr_port = self.OPENFLOW_AGGREGATE_MANAGER_PORT
70
71         return (aggr_mgr_ip,aggr_mgr_port)
72
73     def get_aggregate_rspec_type(self):
74         if (hasattr(self,'SFA_AGGREGATE_RSPEC_TYPE')):
75             return self.SFA_AGGREGATE_RSPEC_TYPE
76         else:
77             return "pl"
78
79     def get_plc_dbinfo(self):
80         return {
81             'dbname' : self.SFA_PLC_DB_NAME,
82             'address' : self.SFA_PLC_DB_HOST,
83             'port' : self.SFA_PLC_DB_PORT,
84             'user' : self.SFA_PLC_DB_USER,
85             'password' : self.SFA_PLC_DB_PASSWORD
86             }
87
88     ##
89     # Geniwrapper uses a PLCAPI connection to perform operations on the registry,
90     # such as creating and deleting slices. This connection requires an account
91     # on the PLC server with full administrator access.
92     #
93     # The Url parameter controls whether the connection uses PLCAPI directly (i.e.
94     # Geniwrapper is located on the same machine as PLC), or uses a XMLRPC connection
95     # to the PLC machine. If you wish to use the API directly, then remove the Url
96     # field from the dictionary. 
97
98     def get_plc_auth(self):
99         return {
100             'AuthMethod': 'capability',
101             'Username': self.SFA_PLC_USER,
102             'AuthString':  self.SFA_PLC_PASSWORD,
103             "Url": self.SFA_PLC_URL
104             }