[CORD-1502] [CORD-1516] Authenticating the user that is sending the
request and caching it's orm classes

Change-Id: I4754c1395d085ed0cf313c7fffebbd2d0b8c7fd4
diff --git a/src/grpc_client/main.py b/src/grpc_client/main.py
index 5612d76..b3e29b1 100644
--- a/src/grpc_client/main.py
+++ b/src/grpc_client/main.py
@@ -1,8 +1,9 @@
 import functools
 from xosapi.xos_grpc_client import SecureClient, InsecureClient
-from twisted.internet import reactor, defer
+from twisted.internet import defer
 from resources import RESOURCES
 from xosconfig import Config
+from twisted.internet import reactor
 
 LOCAL_CERT = '/Users/teone/Sites/opencord/orchestration/xos-tosca/local_certs.crt'
 
@@ -12,13 +13,14 @@
 
         self.grpc_secure_endpoint = Config.get('grpc.secure_endpoint')
         self.grpc_insecure_endpoint = Config.get('grpc.insecure_endpoint')
-        self.username = Config.get('grpc.admin_username')
-        self.password = Config.get('grpc.admin_password')
 
-    def setup_resources(self, client):
+    def setup_resources(self, client, key, deferred, recipe):
         print "[XOS-TOSCA] Loading resources"
+        if key not in RESOURCES:
+            RESOURCES[key] = {}
         for k in client.xos_orm.all_model_names:
-            RESOURCES[k] = getattr(client.xos_orm, k)
+            RESOURCES[key][k] = getattr(client.xos_orm, k)
+        reactor.callLater(0, deferred.callback, recipe)
 
     def start(self):
         print "[XOS-TOSCA] Connecting to xos-core"
@@ -29,15 +31,23 @@
             self.client.stop()
             self.client.session_change = True
 
-        if self.username and self.password:
-            # NOTE if we authenticate users given the parameters in the rest request, do we need this?
-            self.client = SecureClient(endpoint=self.grpc_secure_endpoint, username=self.username, password=self.password, cacert=LOCAL_CERT)
-        else:
-            self.client = InsecureClient(endpoint=self.grpc_insecure_endpoint)
+        self.client = InsecureClient(endpoint=self.grpc_insecure_endpoint)
 
-        self.client.set_reconnect_callback(functools.partial(self.setup_resources, self.client))
+        self.client.set_reconnect_callback(functools.partial(deferred.callback, self.client))
         self.client.start()
 
-        # TODO can we call this once the client is setted up?
-        reactor.callLater(12, deferred.callback, self.client)
-        return deferred
\ No newline at end of file
+        return deferred
+
+    def create_secure_client(self, username, password, recipe):
+        """
+        This method will check if this combination of username/password already has stored orm classes in RESOURCES, otherwise create them
+        """
+        deferred = defer.Deferred()
+        key = "%s~%s" % (username, password)
+        if key in RESOURCES:
+            reactor.callLater(0, deferred.callback, recipe)
+        else:
+            client = SecureClient(endpoint=self.grpc_secure_endpoint, username=username, password=password, cacert=LOCAL_CERT)
+            client.set_reconnect_callback(functools.partial(self.setup_resources, client, key, deferred, recipe))
+            client.start()
+        return deferred