blob: 314081f1891b0ee69ec5b9868b280076dc56be52 [file] [log] [blame]
Matteo Scandolo920e8fd2017-08-08 13:05:24 -07001# Copyright 2017-present Open Networking Foundation
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15
Zack Williams6bb2cfe2019-03-27 15:01:45 -070016from __future__ import absolute_import
Matteo Scandolo9ce18252017-06-22 10:48:25 -070017
Zack Williams6bb2cfe2019-03-27 15:01:45 -070018import functools
19
20from xosapi.xos_grpc_client import InsecureClient, SecureClient
21
Matteo Scandolo5a07a2c2018-06-05 18:04:00 -070022from multistructlog import create_logger
Zack Williams6bb2cfe2019-03-27 15:01:45 -070023from twisted.internet import defer, reactor
24from xosconfig import Config
25
26from .resources import RESOURCES
27
28log = create_logger(Config().get("logging"))
29
Matteo Scandolo5a07a2c2018-06-05 18:04:00 -070030
Matteo Scandolo9ce18252017-06-22 10:48:25 -070031class GRPC_Client:
32 def __init__(self):
33 self.client = None
34
Zack Williams6bb2cfe2019-03-27 15:01:45 -070035 insecure = Config.get("gprc_endpoint")
36 secure = Config.get("gprc_endpoint")
Matteo Scandolo2c1a0012017-09-12 17:08:04 -070037
38 self.grpc_secure_endpoint = secure + ":50051"
39 self.grpc_insecure_endpoint = insecure + ":50055"
40
Andy Bavier571489f2019-02-05 19:23:51 -070041 def setup_resources(self, client, key, deferred, arg):
Matteo Scandolo5a07a2c2018-06-05 18:04:00 -070042 log.info("[XOS-TOSCA] Loading resources for authenticated user")
Matteo Scandolo21dde412017-07-11 18:54:12 -070043 if key not in RESOURCES:
44 RESOURCES[key] = {}
Matteo Scandolo9ce18252017-06-22 10:48:25 -070045 for k in client.xos_orm.all_model_names:
Matteo Scandolo21dde412017-07-11 18:54:12 -070046 RESOURCES[key][k] = getattr(client.xos_orm, k)
Andy Bavier571489f2019-02-05 19:23:51 -070047 reactor.callLater(0, deferred.callback, arg)
Matteo Scandolo9ce18252017-06-22 10:48:25 -070048
49 def start(self):
Matteo Scandolo5a07a2c2018-06-05 18:04:00 -070050 log.info("[XOS-TOSCA] Connecting to xos-core")
Matteo Scandolo9ce18252017-06-22 10:48:25 -070051
52 deferred = defer.Deferred()
53
54 if self.client:
55 self.client.stop()
56 self.client.session_change = True
57
Matteo Scandolo21dde412017-07-11 18:54:12 -070058 self.client = InsecureClient(endpoint=self.grpc_insecure_endpoint)
Scott Baker3f7739c2018-02-20 20:17:34 -080059 self.client.restart_on_disconnect = True
Matteo Scandolo9ce18252017-06-22 10:48:25 -070060
Zack Williams6bb2cfe2019-03-27 15:01:45 -070061 self.client.set_reconnect_callback(
62 functools.partial(deferred.callback, self.client)
63 )
Matteo Scandolo9ce18252017-06-22 10:48:25 -070064 self.client.start()
65
Matteo Scandolo21dde412017-07-11 18:54:12 -070066 return deferred
67
Andy Bavier571489f2019-02-05 19:23:51 -070068 def create_secure_client(self, username, password, arg):
Matteo Scandolo21dde412017-07-11 18:54:12 -070069 """
Zack Williams6bb2cfe2019-03-27 15:01:45 -070070 This method will check if this combination of username/password already
71 has stored orm classes in RESOURCES, otherwise create them
Matteo Scandolo21dde412017-07-11 18:54:12 -070072 """
73 deferred = defer.Deferred()
74 key = "%s~%s" % (username, password)
75 if key in RESOURCES:
Andy Bavier571489f2019-02-05 19:23:51 -070076 reactor.callLater(0, deferred.callback, arg)
Matteo Scandolo21dde412017-07-11 18:54:12 -070077 else:
Zack Williams6bb2cfe2019-03-27 15:01:45 -070078 local_cert = Config.get("local_cert")
79 client = SecureClient(
80 endpoint=self.grpc_secure_endpoint,
81 username=username,
82 password=password,
83 cacert=local_cert,
84 )
Scott Baker3f7739c2018-02-20 20:17:34 -080085 client.restart_on_disconnect = True
Scott Baker503ad842018-06-13 10:08:25 -070086 # SecureClient is preceeded by an insecure client, so treat all secure clients as previously connected
87 # See CORD-3152
88 client.was_connected = True
Zack Williams6bb2cfe2019-03-27 15:01:45 -070089 client.set_reconnect_callback(
90 functools.partial(self.setup_resources, client, key, deferred, arg)
91 )
Matteo Scandolo21dde412017-07-11 18:54:12 -070092 client.start()
93 return deferred