Matteo Scandolo | 920e8fd | 2017-08-08 13:05:24 -0700 | [diff] [blame] | 1 | |
| 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 Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 17 | import functools |
| 18 | from xosapi.xos_grpc_client import SecureClient, InsecureClient |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 19 | from twisted.internet import defer |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 20 | from resources import RESOURCES |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 21 | from xosconfig import Config |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 22 | from twisted.internet import reactor |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 23 | |
Matteo Scandolo | 5a07a2c | 2018-06-05 18:04:00 -0700 | [diff] [blame] | 24 | from xosconfig import Config |
| 25 | from multistructlog import create_logger |
| 26 | log = create_logger(Config().get('logging')) |
| 27 | |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 28 | class GRPC_Client: |
| 29 | def __init__(self): |
| 30 | self.client = None |
| 31 | |
Matteo Scandolo | 2c1a001 | 2017-09-12 17:08:04 -0700 | [diff] [blame] | 32 | 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 Bavier | 571489f | 2019-02-05 19:23:51 -0700 | [diff] [blame^] | 38 | def setup_resources(self, client, key, deferred, arg): |
Matteo Scandolo | 5a07a2c | 2018-06-05 18:04:00 -0700 | [diff] [blame] | 39 | log.info("[XOS-TOSCA] Loading resources for authenticated user") |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 40 | if key not in RESOURCES: |
| 41 | RESOURCES[key] = {} |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 42 | for k in client.xos_orm.all_model_names: |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 43 | RESOURCES[key][k] = getattr(client.xos_orm, k) |
Andy Bavier | 571489f | 2019-02-05 19:23:51 -0700 | [diff] [blame^] | 44 | reactor.callLater(0, deferred.callback, arg) |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 45 | |
| 46 | def start(self): |
Matteo Scandolo | 5a07a2c | 2018-06-05 18:04:00 -0700 | [diff] [blame] | 47 | log.info("[XOS-TOSCA] Connecting to xos-core") |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 48 | |
| 49 | deferred = defer.Deferred() |
| 50 | |
| 51 | if self.client: |
| 52 | self.client.stop() |
| 53 | self.client.session_change = True |
| 54 | |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 55 | self.client = InsecureClient(endpoint=self.grpc_insecure_endpoint) |
Scott Baker | 3f7739c | 2018-02-20 20:17:34 -0800 | [diff] [blame] | 56 | self.client.restart_on_disconnect = True |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 57 | |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 58 | self.client.set_reconnect_callback(functools.partial(deferred.callback, self.client)) |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 59 | self.client.start() |
| 60 | |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 61 | return deferred |
| 62 | |
Andy Bavier | 571489f | 2019-02-05 19:23:51 -0700 | [diff] [blame^] | 63 | def create_secure_client(self, username, password, arg): |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 64 | """ |
| 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 Bavier | 571489f | 2019-02-05 19:23:51 -0700 | [diff] [blame^] | 70 | reactor.callLater(0, deferred.callback, arg) |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 71 | else: |
Matteo Scandolo | 2c1a001 | 2017-09-12 17:08:04 -0700 | [diff] [blame] | 72 | local_cert = Config.get('local_cert') |
| 73 | client = SecureClient(endpoint=self.grpc_secure_endpoint, username=username, password=password, cacert=local_cert) |
Scott Baker | 3f7739c | 2018-02-20 20:17:34 -0800 | [diff] [blame] | 74 | client.restart_on_disconnect = True |
Scott Baker | 503ad84 | 2018-06-13 10:08:25 -0700 | [diff] [blame] | 75 | # 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 Bavier | 571489f | 2019-02-05 19:23:51 -0700 | [diff] [blame^] | 78 | client.set_reconnect_callback(functools.partial(self.setup_resources, client, key, deferred, arg)) |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 79 | client.start() |
| 80 | return deferred |