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