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 | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 24 | class GRPC_Client: |
| 25 | def __init__(self): |
| 26 | self.client = None |
| 27 | |
Matteo Scandolo | 2c1a001 | 2017-09-12 17:08:04 -0700 | [diff] [blame] | 28 | insecure = Config.get('gprc_endpoint') |
| 29 | secure = Config.get('gprc_endpoint') |
| 30 | |
| 31 | self.grpc_secure_endpoint = secure + ":50051" |
| 32 | self.grpc_insecure_endpoint = insecure + ":50055" |
| 33 | |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 34 | def setup_resources(self, client, key, deferred, recipe): |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 35 | print "[XOS-TOSCA] Loading resources" |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 36 | if key not in RESOURCES: |
| 37 | RESOURCES[key] = {} |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 38 | for k in client.xos_orm.all_model_names: |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 39 | RESOURCES[key][k] = getattr(client.xos_orm, k) |
| 40 | reactor.callLater(0, deferred.callback, recipe) |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 41 | |
| 42 | def start(self): |
| 43 | print "[XOS-TOSCA] Connecting to xos-core" |
| 44 | |
| 45 | deferred = defer.Deferred() |
| 46 | |
| 47 | if self.client: |
| 48 | self.client.stop() |
| 49 | self.client.session_change = True |
| 50 | |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 51 | self.client = InsecureClient(endpoint=self.grpc_insecure_endpoint) |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 52 | |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 53 | self.client.set_reconnect_callback(functools.partial(deferred.callback, self.client)) |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 54 | self.client.start() |
| 55 | |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 56 | return deferred |
| 57 | |
| 58 | def create_secure_client(self, username, password, recipe): |
| 59 | """ |
| 60 | This method will check if this combination of username/password already has stored orm classes in RESOURCES, otherwise create them |
| 61 | """ |
| 62 | deferred = defer.Deferred() |
| 63 | key = "%s~%s" % (username, password) |
| 64 | if key in RESOURCES: |
| 65 | reactor.callLater(0, deferred.callback, recipe) |
| 66 | else: |
Matteo Scandolo | 2c1a001 | 2017-09-12 17:08:04 -0700 | [diff] [blame] | 67 | local_cert = Config.get('local_cert') |
| 68 | client = SecureClient(endpoint=self.grpc_secure_endpoint, username=username, password=password, cacert=local_cert) |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 69 | client.set_reconnect_callback(functools.partial(self.setup_resources, client, key, deferred, recipe)) |
| 70 | client.start() |
| 71 | return deferred |