blob: 90ecc0b2a4c15277c404606025cc7d0867ef9b13 [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
24LOCAL_CERT = '/Users/teone/Sites/opencord/orchestration/xos-tosca/local_certs.crt'
25
26class GRPC_Client:
27 def __init__(self):
28 self.client = None
29
30 self.grpc_secure_endpoint = Config.get('grpc.secure_endpoint')
31 self.grpc_insecure_endpoint = Config.get('grpc.insecure_endpoint')
Matteo Scandolo9ce18252017-06-22 10:48:25 -070032
Matteo Scandolo21dde412017-07-11 18:54:12 -070033 def setup_resources(self, client, key, deferred, recipe):
Matteo Scandolo9ce18252017-06-22 10:48:25 -070034 print "[XOS-TOSCA] Loading resources"
Matteo Scandolo21dde412017-07-11 18:54:12 -070035 if key not in RESOURCES:
36 RESOURCES[key] = {}
Matteo Scandolo9ce18252017-06-22 10:48:25 -070037 for k in client.xos_orm.all_model_names:
Matteo Scandolo21dde412017-07-11 18:54:12 -070038 RESOURCES[key][k] = getattr(client.xos_orm, k)
39 reactor.callLater(0, deferred.callback, recipe)
Matteo Scandolo9ce18252017-06-22 10:48:25 -070040
41 def start(self):
42 print "[XOS-TOSCA] Connecting to xos-core"
43
44 deferred = defer.Deferred()
45
46 if self.client:
47 self.client.stop()
48 self.client.session_change = True
49
Matteo Scandolo21dde412017-07-11 18:54:12 -070050 self.client = InsecureClient(endpoint=self.grpc_insecure_endpoint)
Matteo Scandolo9ce18252017-06-22 10:48:25 -070051
Matteo Scandolo21dde412017-07-11 18:54:12 -070052 self.client.set_reconnect_callback(functools.partial(deferred.callback, self.client))
Matteo Scandolo9ce18252017-06-22 10:48:25 -070053 self.client.start()
54
Matteo Scandolo21dde412017-07-11 18:54:12 -070055 return deferred
56
57 def create_secure_client(self, username, password, recipe):
58 """
59 This method will check if this combination of username/password already has stored orm classes in RESOURCES, otherwise create them
60 """
61 deferred = defer.Deferred()
62 key = "%s~%s" % (username, password)
63 if key in RESOURCES:
64 reactor.callLater(0, deferred.callback, recipe)
65 else:
66 client = SecureClient(endpoint=self.grpc_secure_endpoint, username=username, password=password, cacert=LOCAL_CERT)
67 client.set_reconnect_callback(functools.partial(self.setup_resources, client, key, deferred, recipe))
68 client.start()
69 return deferred