Merge Master in geni-v3 conflict resolution
[sfa.git] / sfa / importer / __init__.py
1 #!/usr/bin/python
2
3 import sys
4 from datetime import datetime
5
6 from sfa.util.xrn import get_authority, hrn_to_urn
7 from sfa.generic import Generic
8 from sfa.util.config import Config
9 from sfa.util.sfalogging import _SfaLogger
10 from sfa.trust.hierarchy import Hierarchy
11 #from sfa.trust.trustedroots import TrustedRoots
12 from sfa.trust.gid import create_uuid
13 from sfa.storage.alchemy import dbsession
14 from sfa.storage.model import RegRecord, RegAuthority, RegUser
15 from sfa.trust.certificate import convert_public_key, Keypair
16
17
18 class Importer:
19
20     def __init__(self,auth_hierarchy=None,logger=None):
21         self.config = Config()
22         if auth_hierarchy is not None:
23             self.auth_hierarchy=auth_hierarchy
24         else:
25             self.auth_hierarchy = Hierarchy ()
26         if logger is not None:
27             self.logger=logger
28         else:
29             self.logger = _SfaLogger(logfile='/var/log/sfa_import.log', loggername='importlog')
30             self.logger.setLevelFromOptVerbose(self.config.SFA_API_LOGLEVEL)
31 # ugly side effect so that other modules get it right
32         import sfa.util.sfalogging
33         sfa.util.sfalogging.logger=logger
34 #        self.TrustedRoots = TrustedRoots(self.config.get_trustedroots_dir())    
35    
36     # check before creating a RegRecord entry as we run this over and over
37     def record_exists (self, type, hrn):
38        return dbsession.query(RegRecord).filter_by(hrn=hrn,type=type).count()!=0 
39
40     def create_top_level_auth_records(self, hrn):
41         """
42         Create top level db records (includes root and sub authorities (local/remote)
43         """
44         # make sure parent exists
45         parent_hrn = get_authority(hrn)
46         if not parent_hrn:
47             parent_hrn = hrn
48         if not parent_hrn == hrn:
49             self.create_top_level_auth_records(parent_hrn)
50
51         # ensure key and cert exists:
52         self.auth_hierarchy.create_top_level_auth(hrn)
53         # create the db record if it doesnt already exist
54         if not self.record_exists ('authority',hrn):
55             auth_info = self.auth_hierarchy.get_auth_info(hrn)
56             auth_record = RegAuthority(hrn=hrn, gid=auth_info.get_gid_object(),
57                                        authority=get_authority(hrn))
58             auth_record.just_created()
59             dbsession.add (auth_record)
60             dbsession.commit()
61             self.logger.info("SfaImporter: imported authority (parent) %s " % auth_record)     
62    
63
64     def create_sm_client_record(self):
65         """
66         Create a user record for the Slicemanager service.
67         """
68         hrn = self.interface_hrn + '.slicemanager'
69         urn = hrn_to_urn(hrn, 'user')
70         if not self.auth_hierarchy.auth_exists(urn):
71             self.logger.info("SfaImporter: creating Slice Manager user")
72             self.auth_hierarchy.create_auth(urn)
73
74         if self.record_exists ('user',hrn): return
75         auth_info = self.auth_hierarchy.get_auth_info(hrn)
76         user_record = RegUser(hrn=hrn, gid=auth_info.get_gid_object(),
77                               authority=get_authority(hrn))
78         user_record.just_created()
79         dbsession.add (user_record)
80         dbsession.commit()
81         self.logger.info("SfaImporter: importing user (slicemanager) %s " % user_record)
82
83
84     def create_interface_records(self):
85         """
86         Create a record for each SFA interface
87         """
88         # just create certs for all sfa interfaces even if they
89         # aren't enabled
90         auth_info = self.auth_hierarchy.get_auth_info(self.config.SFA_INTERFACE_HRN)
91         pkey = auth_info.get_pkey_object()
92         hrn=self.config.SFA_INTERFACE_HRN
93         for type in  [ 'authority+sa', 'authority+am', 'authority+sm', ]:
94             urn = hrn_to_urn(hrn, type)
95             gid = self.auth_hierarchy.create_gid(urn, create_uuid(), pkey)
96             # for now we have to preserve the authority+<> stuff
97             if self.record_exists (type,hrn): continue
98             interface_record = RegAuthority(type=type, hrn=hrn, gid=gid,
99                                             authority=get_authority(hrn))
100             interface_record.just_created()
101             dbsession.add (interface_record)
102             dbsession.commit()
103             self.logger.info("SfaImporter: imported authority (%s) %s " % (type,interface_record))
104  
105     def run(self, options=None):
106         if not self.config.SFA_REGISTRY_ENABLED:
107             self.logger.critical("Importer: need SFA_REGISTRY_ENABLED to run import")
108
109         # testbed-neutral : create local certificates and the like
110         auth_hierarchy = Hierarchy ()
111         self.create_top_level_auth_records(self.config.SFA_INTERFACE_HRN)
112         self.create_interface_records()
113  
114         # testbed-specific
115         testbed_importer = None
116         generic=Generic.the_flavour()
117         importer_class = generic.importer_class()
118         if importer_class:
119             begin_time=datetime.now()
120             self.logger.info ("Starting import on %s, using class %s from flavour %s"%\
121                          (begin_time,importer_class.__name__,generic.flavour))
122             testbed_importer = importer_class (auth_hierarchy, self.logger)
123             if testbed_importer:
124                 testbed_importer.add_options(options)
125                 testbed_importer.run (options)
126             end_time=datetime.now()
127             duration=end_time-begin_time
128             self.logger.info("Import took %s"%duration)