blob: b3e29b147e36f34e6e80173fcb250351353d869c [file] [log] [blame]
Matteo Scandolo9ce18252017-06-22 10:48:25 -07001import functools
2from xosapi.xos_grpc_client import SecureClient, InsecureClient
Matteo Scandolo21dde412017-07-11 18:54:12 -07003from twisted.internet import defer
Matteo Scandolo485b7132017-06-30 11:46:47 -07004from resources import RESOURCES
Matteo Scandolo9ce18252017-06-22 10:48:25 -07005from xosconfig import Config
Matteo Scandolo21dde412017-07-11 18:54:12 -07006from twisted.internet import reactor
Matteo Scandolo9ce18252017-06-22 10:48:25 -07007
8LOCAL_CERT = '/Users/teone/Sites/opencord/orchestration/xos-tosca/local_certs.crt'
9
10class 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 Scandolo9ce18252017-06-22 10:48:25 -070016
Matteo Scandolo21dde412017-07-11 18:54:12 -070017 def setup_resources(self, client, key, deferred, recipe):
Matteo Scandolo9ce18252017-06-22 10:48:25 -070018 print "[XOS-TOSCA] Loading resources"
Matteo Scandolo21dde412017-07-11 18:54:12 -070019 if key not in RESOURCES:
20 RESOURCES[key] = {}
Matteo Scandolo9ce18252017-06-22 10:48:25 -070021 for k in client.xos_orm.all_model_names:
Matteo Scandolo21dde412017-07-11 18:54:12 -070022 RESOURCES[key][k] = getattr(client.xos_orm, k)
23 reactor.callLater(0, deferred.callback, recipe)
Matteo Scandolo9ce18252017-06-22 10:48:25 -070024
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 Scandolo21dde412017-07-11 18:54:12 -070034 self.client = InsecureClient(endpoint=self.grpc_insecure_endpoint)
Matteo Scandolo9ce18252017-06-22 10:48:25 -070035
Matteo Scandolo21dde412017-07-11 18:54:12 -070036 self.client.set_reconnect_callback(functools.partial(deferred.callback, self.client))
Matteo Scandolo9ce18252017-06-22 10:48:25 -070037 self.client.start()
38
Matteo Scandolo21dde412017-07-11 18:54:12 -070039 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