ptp-notification-armada-app/locationservice-base/docker/locationservice/locationservicesdk/client/base.py
Andre Mauricio Zelak e17699d3d1 Use new get_rpc_client API from oslo.messaging
Use the new get_rpc_client API to handle the RCPClient
instantiation.

This new API was added into oslo.messaging here [1].

[1] https://review.opendev.org/c/openstack/oslo.messaging/+/862419

Closes-bug: 2011620

Signed-off-by: Andre Mauricio Zelak <andre.zelak@windriver.com>
Change-Id: I2322d0bb49b342f8eec49f599257f9dac83b3d8a
2023-03-20 17:47:02 -03:00

114 lines
4.3 KiB
Python

#
# Copyright (c) 2021 Wind River Systems, Inc.
#
# SPDX-License-Identifier: Apache-2.0
#
import os
import json
import time
import oslo_messaging
from oslo_config import cfg
from locationservicesdk.common.helpers import rpc_helper
from locationservicesdk.model.dto.rpc_endpoint import RpcEndpointInfo
import logging
LOG = logging.getLogger(__name__)
from locationservicesdk.common.helpers import log_helper
log_helper.config_logger(LOG)
class BrokerClientBase(object):
def __init__(self, broker_name, broker_transport_endpoint):
self.broker_name = broker_name
self.listeners = {}
self.broker_endpoint = RpcEndpointInfo(broker_transport_endpoint)
self.transport = rpc_helper.get_transport(self.broker_endpoint)
LOG.debug("Created Broker client:{0}".format(broker_name))
def __del__(self):
self.transport.cleanup()
del self.transport
return
def __create_listener(self, context):
target = oslo_messaging.Target(
topic=context['topic'],
server=context['server'])
endpoints = context['endpoints']
server = oslo_messaging.get_rpc_server(
self.transport, target, endpoints, executor=None)
return server
def _refresh(self):
for topic, servers in self.listeners.items():
for servername, context in servers.items():
try:
rpcserver = context.get('rpcserver', None)
isactive = context.get('active', False)
if isactive and not rpcserver:
rpcserver = self.__create_listener(context)
rpcserver.start()
context['rpcserver'] = rpcserver
LOG.debug("Started rpcserver@{0}@{1}".format(context['topic'], context['server']))
elif not isactive and rpcserver:
rpcserver.stop()
rpcserver.wait()
context.pop('rpcserver')
LOG.debug("Stopped rpcserver@{0}@{1}".format(context['topic'], context['server']))
except:
LOG.error("Failed to update listener for topic/server:{0}/{1}"
.format(topic, servername))
continue
def add_listener(self, topic, server, listener_endpoints=None):
context = self.listeners.get(topic,{}).get(server, {})
if not context:
context = {
'endpoints': listener_endpoints,
'topic': topic,
'server': server,
'active': True
}
if not self.listeners.get(topic, None):
self.listeners[topic] = {}
self.listeners[topic][server] = context
else:
context['endpoints'] = listener_endpoints
context['active'] = True
self._refresh()
def remove_listener(self, topic, server):
context = self.listeners.get(topic,{}).get(server, {})
if context:
context['active'] = False
self._refresh()
def is_listening(self, topic, server):
context = self.listeners.get(topic,{}).get(server, {})
return context.get('active', False)
def any_listener(self):
for topic, servers in self.listeners.items():
for servername, context in servers.items():
isactive = context.get('active', False)
if isactive:
return True
return False
def call(self, topic, server, api_name, **api_kwargs):
target = oslo_messaging.Target(
topic=topic, server=server, version=self.broker_endpoint.Version,
namespace=self.broker_endpoint.Namespace)
queryclient = oslo_messaging.get_rpc_client(self.transport, target, timeout = 2, retry = 0)
return queryclient.call({}, api_name, **api_kwargs)
def cast(self, topic, api_name, **api_kwargs):
target = oslo_messaging.Target(
topic=topic, fanout=True, version=self.broker_endpoint.Version,
namespace=self.broker_endpoint.Namespace)
queryclient = oslo_messaging.get_rpc_client(self.transport, target)
queryclient.cast({}, api_name, **api_kwargs)