blob: 619b43b97e1ddc809cf016fa42a428445b08c1c9 [file] [log] [blame]
Matteo Scandolo920e8fd2017-08-08 13:05:24 -07001
2# Copyright 2017-present Open Networking Foundation
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16
Matteo Scandolo9ce18252017-06-22 10:48:25 -070017import functools
18from xosapi.xos_grpc_client import SecureClient, InsecureClient
Matteo Scandolo21dde412017-07-11 18:54:12 -070019from twisted.internet import defer
Matteo Scandolo485b7132017-06-30 11:46:47 -070020from resources import RESOURCES
Matteo Scandolo9ce18252017-06-22 10:48:25 -070021from xosconfig import Config
Matteo Scandolo21dde412017-07-11 18:54:12 -070022from twisted.internet import reactor
Matteo Scandolo9ce18252017-06-22 10:48:25 -070023
Matteo Scandolo5a07a2c2018-06-05 18:04:00 -070024from xosconfig import Config
25from multistructlog import create_logger
26log = create_logger(Config().get('logging'))
27
Matteo Scandolo9ce18252017-06-22 10:48:25 -070028class GRPC_Client:
29 def __init__(self):
30 self.client = None
31
Matteo Scandolo2c1a0012017-09-12 17:08:04 -070032 insecure = Config.get('gprc_endpoint')
33 secure = Config.get('gprc_endpoint')
34
35 self.grpc_secure_endpoint = secure + ":50051"
36 self.grpc_insecure_endpoint = insecure + ":50055"
37
Andy Bavier571489f2019-02-05 19:23:51 -070038 def setup_resources(self, client, key, deferred, arg):
Matteo Scandolo5a07a2c2018-06-05 18:04:00 -070039 log.info("[XOS-TOSCA] Loading resources for authenticated user")
Matteo Scandolo21dde412017-07-11 18:54:12 -070040 if key not in RESOURCES:
41 RESOURCES[key] = {}
Matteo Scandolo9ce18252017-06-22 10:48:25 -070042 for k in client.xos_orm.all_model_names:
Matteo Scandolo21dde412017-07-11 18:54:12 -070043 RESOURCES[key][k] = getattr(client.xos_orm, k)
Andy Bavier571489f2019-02-05 19:23:51 -070044 reactor.callLater(0, deferred.callback, arg)
Matteo Scandolo9ce18252017-06-22 10:48:25 -070045
46 def start(self):
Matteo Scandolo5a07a2c2018-06-05 18:04:00 -070047 log.info("[XOS-TOSCA] Connecting to xos-core")
Matteo Scandolo9ce18252017-06-22 10:48:25 -070048
49 deferred = defer.Deferred()
50
51 if self.client:
52 self.client.stop()
53 self.client.session_change = True
54
Matteo Scandolo21dde412017-07-11 18:54:12 -070055 self.client = InsecureClient(endpoint=self.grpc_insecure_endpoint)
Scott Baker3f7739c2018-02-20 20:17:34 -080056 self.client.restart_on_disconnect = True
Matteo Scandolo9ce18252017-06-22 10:48:25 -070057
Matteo Scandolo21dde412017-07-11 18:54:12 -070058 self.client.set_reconnect_callback(functools.partial(deferred.callback, self.client))
Matteo Scandolo9ce18252017-06-22 10:48:25 -070059 self.client.start()
60
Matteo Scandolo21dde412017-07-11 18:54:12 -070061 return deferred
62
Andy Bavier571489f2019-02-05 19:23:51 -070063 def create_secure_client(self, username, password, arg):
Matteo Scandolo21dde412017-07-11 18:54:12 -070064 """
65 This method will check if this combination of username/password already has stored orm classes in RESOURCES, otherwise create them
66 """
67 deferred = defer.Deferred()
68 key = "%s~%s" % (username, password)
69 if key in RESOURCES:
Andy Bavier571489f2019-02-05 19:23:51 -070070 reactor.callLater(0, deferred.callback, arg)
Matteo Scandolo21dde412017-07-11 18:54:12 -070071 else:
Matteo Scandolo2c1a0012017-09-12 17:08:04 -070072 local_cert = Config.get('local_cert')
73 client = SecureClient(endpoint=self.grpc_secure_endpoint, username=username, password=password, cacert=local_cert)
Scott Baker3f7739c2018-02-20 20:17:34 -080074 client.restart_on_disconnect = True
Scott Baker503ad842018-06-13 10:08:25 -070075 # SecureClient is preceeded by an insecure client, so treat all secure clients as previously connected
76 # See CORD-3152
77 client.was_connected = True
Andy Bavier571489f2019-02-05 19:23:51 -070078 client.set_reconnect_callback(functools.partial(self.setup_resources, client, key, deferred, arg))
Matteo Scandolo21dde412017-07-11 18:54:12 -070079 client.start()
80 return deferred