Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 1 | import functools |
| 2 | from xosapi.xos_grpc_client import SecureClient, InsecureClient |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 3 | from twisted.internet import defer |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 4 | from resources import RESOURCES |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 5 | from xosconfig import Config |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 6 | from twisted.internet import reactor |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 7 | |
| 8 | LOCAL_CERT = '/Users/teone/Sites/opencord/orchestration/xos-tosca/local_certs.crt' |
| 9 | |
| 10 | class GRPC_Client: |
| 11 | def __init__(self): |
| 12 | self.client = None |
| 13 | |
| 14 | self.grpc_secure_endpoint = Config.get('grpc.secure_endpoint') |
| 15 | self.grpc_insecure_endpoint = Config.get('grpc.insecure_endpoint') |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 16 | |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 17 | def setup_resources(self, client, key, deferred, recipe): |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 18 | print "[XOS-TOSCA] Loading resources" |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 19 | if key not in RESOURCES: |
| 20 | RESOURCES[key] = {} |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 21 | for k in client.xos_orm.all_model_names: |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 22 | RESOURCES[key][k] = getattr(client.xos_orm, k) |
| 23 | reactor.callLater(0, deferred.callback, recipe) |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 24 | |
| 25 | def start(self): |
| 26 | print "[XOS-TOSCA] Connecting to xos-core" |
| 27 | |
| 28 | deferred = defer.Deferred() |
| 29 | |
| 30 | if self.client: |
| 31 | self.client.stop() |
| 32 | self.client.session_change = True |
| 33 | |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 34 | self.client = InsecureClient(endpoint=self.grpc_insecure_endpoint) |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 35 | |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 36 | self.client.set_reconnect_callback(functools.partial(deferred.callback, self.client)) |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 37 | self.client.start() |
| 38 | |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 39 | return deferred |
| 40 | |
| 41 | def create_secure_client(self, username, password, recipe): |
| 42 | """ |
| 43 | This method will check if this combination of username/password already has stored orm classes in RESOURCES, otherwise create them |
| 44 | """ |
| 45 | deferred = defer.Deferred() |
| 46 | key = "%s~%s" % (username, password) |
| 47 | if key in RESOURCES: |
| 48 | reactor.callLater(0, deferred.callback, recipe) |
| 49 | else: |
| 50 | client = SecureClient(endpoint=self.grpc_secure_endpoint, username=username, password=password, cacert=LOCAL_CERT) |
| 51 | client.set_reconnect_callback(functools.partial(self.setup_resources, client, key, deferred, recipe)) |
| 52 | client.start() |
| 53 | return deferred |