blob: fc64ef35c578ec9f8c00d7349f0e0ce66fce559d [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)
Scott Baker3f7739c2018-02-20 20:17:34 -080052 self.client.restart_on_disconnect = True
Matteo Scandolo9ce18252017-06-22 10:48:25 -070053
Matteo Scandolo21dde412017-07-11 18:54:12 -070054 self.client.set_reconnect_callback(functools.partial(deferred.callback, self.client))
Matteo Scandolo9ce18252017-06-22 10:48:25 -070055 self.client.start()
56
Matteo Scandolo21dde412017-07-11 18:54:12 -070057 return deferred
58
59 def create_secure_client(self, username, password, recipe):
60 """
61 This method will check if this combination of username/password already has stored orm classes in RESOURCES, otherwise create them
62 """
63 deferred = defer.Deferred()
64 key = "%s~%s" % (username, password)
65 if key in RESOURCES:
66 reactor.callLater(0, deferred.callback, recipe)
67 else:
Matteo Scandolo2c1a0012017-09-12 17:08:04 -070068 local_cert = Config.get('local_cert')
69 client = SecureClient(endpoint=self.grpc_secure_endpoint, username=username, password=password, cacert=local_cert)
Scott Baker3f7739c2018-02-20 20:17:34 -080070 client.restart_on_disconnect = True
Matteo Scandolo21dde412017-07-11 18:54:12 -070071 client.set_reconnect_callback(functools.partial(self.setup_resources, client, key, deferred, recipe))
72 client.start()
73 return deferred