basic rpc api

Change-Id: I89fd51a2fa79e827888564b59a91f4aaf8c2df40
This commit is contained in:
Alexey Weyl 2016-01-20 11:33:36 +02:00
parent df11ca7909
commit 0373cf684d
4 changed files with 66 additions and 14 deletions

View File

@ -7,6 +7,8 @@ Babel>=1.3
python-dateutil>=2.4.2
python-novaclient>=2.26.0
networkx>=1.10
oslo.config>=2.7.0 # Apache-2.0
oslo.messaging!=2.8.0,>2.6.1 # Apache-2.0
oslo.log>=1.12.0 # Apache-2.0
oslo.policy>=0.3.0
oslo.service>=1.0.0 # Apache-2.0

View File

@ -14,6 +14,8 @@ oslo.log>=1.12.0 # Apache-2.0
oslosphinx>=2.5.0 # Apache-2.0
oslotest>=1.10.0 # Apache-2.0
oslo.service>=1.0.0 # Apache-2.0
oslo.config>=2.7.0 # Apache-2.0
oslo.messaging!=2.8.0,>2.6.1 # Apache-2.0
oslo.i18n>=2.1.0
testrepository>=0.0.18
testscenarios>=0.4

View File

@ -1,3 +1,5 @@
# Copyright 2016 - Alcatel-Lucent
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
@ -12,13 +14,17 @@
import json
import pecan
from networkx.readwrite import json_graph
from oslo_config import cfg
from oslo_log import log
import oslo_messaging
import pecan
from pecan.core import abort
from pecan import rest
from vitrage.api.policy import enforce
# noinspection PyProtectedMember
from vitrage.i18n import _LI
@ -26,6 +32,14 @@ LOG = log.getLogger(__name__)
class TopologyController(rest.RestController):
def __init__(self):
transport = oslo_messaging.get_transport(cfg.CONF)
cfg.CONF.set_override('rpc_backend', 'rabbit')
target = oslo_messaging.Target(topic='rpcapiv1')
self.client = oslo_messaging.RPCClient(transport, target)
self.ctxt = {}
@pecan.expose('json')
def post(self, depth, graph_type, query, root):
enforce("get topology", pecan.request.headers,
@ -39,20 +53,19 @@ class TopologyController(rest.RestController):
return self.get_graph(graph_type)
@staticmethod
def get_graph(graph_type):
# TODO(eyal) temporary mock
graph_file = pecan.request.cfg.find_file('graph.sample.json')
def get_graph(self, graph_type):
graph_data = self.client.call(self.ctxt, 'get_topology', arg=None)
LOG.info(graph_data)
# graph_file = pecan.request.cfg.find_file('graph.sample.json')
try:
with open(graph_file) as data_file:
graph = json.load(data_file)
if graph_type == 'graph':
return graph
if graph_type == 'tree':
return json_graph.tree_data(
json_graph.node_link_graph(graph).reverse(),
root=0)
if graph_type == 'graph':
return json.loads(graph_data)
if graph_type == 'tree':
return json_graph.tree_data(
json_graph.node_link_graph(json.loads(graph_data)),
root='RESOURCE:node')
except Exception as e:
LOG.exception("failed to open file ", e)
LOG.exception("failed to open file %s ", e)
abort(404, str(e))

View File

@ -12,7 +12,9 @@
# License for the specific language governing permissions and limitations
# under the License.
from oslo_config import cfg
from oslo_log import log
import oslo_messaging
from oslo_service import service as os_service
@ -30,6 +32,31 @@ class VitrageApiHandlerService(os_service.Service):
super(VitrageApiHandlerService, self).start()
transport = oslo_messaging.get_transport(cfg.CONF)
# TODO(Dany) add real server
target = oslo_messaging.Target(topic='rpcapiv1', server='localhost')
# TODO(Dany) add rabbit configuratipn
# target = om.Target(topic='testme', server='192.168.56.102')
# target = oslo_messaging.Target(
# topic='testme', server='135.248.18.223')
# cfg.CONF.set_override('rabbit_host', '135.248.18.223')
# cfg.CONF.set_override('rabbit_port', 5672)
# cfg.CONF.set_override('rabbit_userid', 'guest')
# cfg.CONF.set_override('rabbit_password', 'cloud')
# cfg.CONF.set_override('rabbit_login_method', 'AMQPLAIN')
# cfg.CONF.set_override('rabbit_virtual_host', '/')
cfg.CONF.set_override('rpc_backend', 'rabbit')
endpoints = [EntityGraphApis(self.entity_graph), ]
# TODO(Dany) use eventlet instead of threading
server = oslo_messaging.get_rpc_server(transport, target,
endpoints, executor='threading')
server.start()
LOG.info("Finish start VitrageApiHandlerService")
def stop(self):
@ -38,3 +65,11 @@ class VitrageApiHandlerService(os_service.Service):
super(VitrageApiHandlerService, self).stop()
LOG.info("Finish stop VitrageApiHandlerService")
class EntityGraphApis(object):
def __init__(self, entity_graph):
self.entity_graph = entity_graph
def get_topology(self, ctx, arg):
return self.entity_graph.output_graph()