Matteo Scandolo | 920e8fd | 2017-08-08 13:05:24 -0700 | [diff] [blame] | 1 | # Copyright 2017-present Open Networking Foundation |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | |
Zack Williams | 6bb2cfe | 2019-03-27 15:01:45 -0700 | [diff] [blame] | 16 | from __future__ import absolute_import |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 17 | |
Zack Williams | 6bb2cfe | 2019-03-27 15:01:45 -0700 | [diff] [blame] | 18 | import functools |
| 19 | |
| 20 | from xosapi.xos_grpc_client import InsecureClient, SecureClient |
| 21 | |
Matteo Scandolo | 5a07a2c | 2018-06-05 18:04:00 -0700 | [diff] [blame] | 22 | from multistructlog import create_logger |
Zack Williams | 6bb2cfe | 2019-03-27 15:01:45 -0700 | [diff] [blame] | 23 | from twisted.internet import defer, reactor |
| 24 | from xosconfig import Config |
| 25 | |
| 26 | from .resources import RESOURCES |
| 27 | |
| 28 | log = create_logger(Config().get("logging")) |
| 29 | |
Matteo Scandolo | 5a07a2c | 2018-06-05 18:04:00 -0700 | [diff] [blame] | 30 | |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 31 | class GRPC_Client: |
| 32 | def __init__(self): |
| 33 | self.client = None |
| 34 | |
Zack Williams | 6bb2cfe | 2019-03-27 15:01:45 -0700 | [diff] [blame] | 35 | insecure = Config.get("gprc_endpoint") |
| 36 | secure = Config.get("gprc_endpoint") |
Matteo Scandolo | 2c1a001 | 2017-09-12 17:08:04 -0700 | [diff] [blame] | 37 | |
| 38 | self.grpc_secure_endpoint = secure + ":50051" |
| 39 | self.grpc_insecure_endpoint = insecure + ":50055" |
| 40 | |
Andy Bavier | 571489f | 2019-02-05 19:23:51 -0700 | [diff] [blame] | 41 | def setup_resources(self, client, key, deferred, arg): |
Matteo Scandolo | 5a07a2c | 2018-06-05 18:04:00 -0700 | [diff] [blame] | 42 | log.info("[XOS-TOSCA] Loading resources for authenticated user") |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 43 | if key not in RESOURCES: |
| 44 | RESOURCES[key] = {} |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 45 | for k in client.xos_orm.all_model_names: |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 46 | RESOURCES[key][k] = getattr(client.xos_orm, k) |
Andy Bavier | 571489f | 2019-02-05 19:23:51 -0700 | [diff] [blame] | 47 | reactor.callLater(0, deferred.callback, arg) |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 48 | |
| 49 | def start(self): |
Matteo Scandolo | 5a07a2c | 2018-06-05 18:04:00 -0700 | [diff] [blame] | 50 | log.info("[XOS-TOSCA] Connecting to xos-core") |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 51 | |
| 52 | deferred = defer.Deferred() |
| 53 | |
| 54 | if self.client: |
| 55 | self.client.stop() |
| 56 | self.client.session_change = True |
| 57 | |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 58 | self.client = InsecureClient(endpoint=self.grpc_insecure_endpoint) |
Scott Baker | 3f7739c | 2018-02-20 20:17:34 -0800 | [diff] [blame] | 59 | self.client.restart_on_disconnect = True |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 60 | |
Zack Williams | 6bb2cfe | 2019-03-27 15:01:45 -0700 | [diff] [blame] | 61 | self.client.set_reconnect_callback( |
| 62 | functools.partial(deferred.callback, self.client) |
| 63 | ) |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 64 | self.client.start() |
| 65 | |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 66 | return deferred |
| 67 | |
Andy Bavier | 571489f | 2019-02-05 19:23:51 -0700 | [diff] [blame] | 68 | def create_secure_client(self, username, password, arg): |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 69 | """ |
Zack Williams | 6bb2cfe | 2019-03-27 15:01:45 -0700 | [diff] [blame] | 70 | This method will check if this combination of username/password already |
| 71 | has stored orm classes in RESOURCES, otherwise create them |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 72 | """ |
| 73 | deferred = defer.Deferred() |
| 74 | key = "%s~%s" % (username, password) |
| 75 | if key in RESOURCES: |
Andy Bavier | 571489f | 2019-02-05 19:23:51 -0700 | [diff] [blame] | 76 | reactor.callLater(0, deferred.callback, arg) |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 77 | else: |
Zack Williams | 6bb2cfe | 2019-03-27 15:01:45 -0700 | [diff] [blame] | 78 | local_cert = Config.get("local_cert") |
| 79 | client = SecureClient( |
| 80 | endpoint=self.grpc_secure_endpoint, |
| 81 | username=username, |
| 82 | password=password, |
| 83 | cacert=local_cert, |
| 84 | ) |
Scott Baker | 3f7739c | 2018-02-20 20:17:34 -0800 | [diff] [blame] | 85 | client.restart_on_disconnect = True |
Scott Baker | 503ad84 | 2018-06-13 10:08:25 -0700 | [diff] [blame] | 86 | # SecureClient is preceeded by an insecure client, so treat all secure clients as previously connected |
| 87 | # See CORD-3152 |
| 88 | client.was_connected = True |
Zack Williams | 6bb2cfe | 2019-03-27 15:01:45 -0700 | [diff] [blame] | 89 | client.set_reconnect_callback( |
| 90 | functools.partial(self.setup_resources, client, key, deferred, arg) |
| 91 | ) |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 92 | client.start() |
| 93 | return deferred |