blob: edda57c2a9323f9148f0a1742b2e40e35e2310ed [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
Matteo Scandolo9ce18252017-06-22 10:48:25 -070024class GRPC_Client:
25 def __init__(self):
26 self.client = None
27
Matteo Scandolo2c1a0012017-09-12 17:08:04 -070028 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 Scandolo21dde412017-07-11 18:54:12 -070034 def setup_resources(self, client, key, deferred, recipe):
Matteo Scandolo9ce18252017-06-22 10:48:25 -070035 print "[XOS-TOSCA] Loading resources"
Matteo Scandolo21dde412017-07-11 18:54:12 -070036 if key not in RESOURCES:
37 RESOURCES[key] = {}
Matteo Scandolo9ce18252017-06-22 10:48:25 -070038 for k in client.xos_orm.all_model_names:
Matteo Scandolo21dde412017-07-11 18:54:12 -070039 RESOURCES[key][k] = getattr(client.xos_orm, k)
40 reactor.callLater(0, deferred.callback, recipe)
Matteo Scandolo9ce18252017-06-22 10:48:25 -070041
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 Scandolo21dde412017-07-11 18:54:12 -070051 self.client = InsecureClient(endpoint=self.grpc_insecure_endpoint)
Matteo Scandolo9ce18252017-06-22 10:48:25 -070052
Matteo Scandolo21dde412017-07-11 18:54:12 -070053 self.client.set_reconnect_callback(functools.partial(deferred.callback, self.client))
Matteo Scandolo9ce18252017-06-22 10:48:25 -070054 self.client.start()
55
Matteo Scandolo21dde412017-07-11 18:54:12 -070056 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 Scandolo2c1a0012017-09-12 17:08:04 -070067 local_cert = Config.get('local_cert')
68 client = SecureClient(endpoint=self.grpc_secure_endpoint, username=username, password=password, cacert=local_cert)
Matteo Scandolo21dde412017-07-11 18:54:12 -070069 client.set_reconnect_callback(functools.partial(self.setup_resources, client, key, deferred, recipe))
70 client.start()
71 return deferred