From 640100604bb013dc765146fd4c8aba2622ce1a9a Mon Sep 17 00:00:00 2001 From: Brad Hall Date: Wed, 21 Dec 2011 14:47:28 -0800 Subject: [PATCH] blueprint ovs-portstats This adds an extension to support fetching port statistics from any plugin that supports it. Change-Id: Id14ee3c9604878b8a06d668126452cd5a5071b10 --- quantum/extensions/_portstats_view.py | 51 +++++++++++++ quantum/extensions/portstats.py | 101 ++++++++++++++++++++++++++ 2 files changed, 152 insertions(+) create mode 100644 quantum/extensions/_portstats_view.py create mode 100644 quantum/extensions/portstats.py diff --git a/quantum/extensions/_portstats_view.py b/quantum/extensions/_portstats_view.py new file mode 100644 index 0000000000..700d42f2ed --- /dev/null +++ b/quantum/extensions/_portstats_view.py @@ -0,0 +1,51 @@ +""" +# vim: tabstop=4 shiftwidth=4 softtabstop=4 +# +# Copyright 2011 Nicira Networks, Inc. All rights reserved. +# +# 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 +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# +# @author: Brad Hall, Nicira Networks, Inc +# +""" + + +def get_view_builder(req): + """get view builder""" + base_url = req.application_url + return ViewBuilder(base_url) + + +class ViewBuilder(object): + """ + ViewBuilder for Port statistics. + + Port stats coming back from the plugin will look like this: + { + "rx_packets": 0, + "rx_bytes": 0, + "tx_errors": 0, + "rx_errors": 0, + "tx_bytes": 0, + "tx_packets": 0 + } + """ + def __init__(self, base_url): + self.base_url = base_url + + def build(self, portstat_data, is_detail=True): + # We just ignore is_detail -- it doesn't make sense in this context. + return self._build(portstat_data) + + def _build(self, portstat_data): + return portstat_data diff --git a/quantum/extensions/portstats.py b/quantum/extensions/portstats.py new file mode 100644 index 0000000000..59a706d345 --- /dev/null +++ b/quantum/extensions/portstats.py @@ -0,0 +1,101 @@ +""" +# vim: tabstop=4 shiftwidth=4 softtabstop=4 +# +# Copyright 2011 Nicira Networks, Inc. All rights reserved. +# +# 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 +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# +# @author: Brad Hall, Nicira Networks, Inc +# +""" + +import logging + +from quantum import wsgi +from quantum.extensions import _portstats_view as portstats_view +from quantum.api import faults +from quantum.common import exceptions as qexception +from quantum.common import extensions +from quantum.manager import QuantumManager + + +LOG = logging.getLogger("quantum.api.portstats") + + +class Portstats(object): + def __init__(self): + pass + + @classmethod + def get_name(cls): + return "Port Statistics" + + @classmethod + def get_alias(cls): + return "portstats" + + @classmethod + def get_description(cls): + return "Port Statistics" + + @classmethod + def get_namespace(cls): + return "http://docs.openstack.org/ext/portstats/api/v1.0" + + @classmethod + def get_updated(cls): + return "2011-12-20T10:00:00-00:00" + + @classmethod + def get_resources(cls): + """ Returns all defined resources """ + controller = StatsController(QuantumManager.get_plugin()) + parent_resource = dict(member_name="port", + collection_name="extensions/ovs/tenants/" + \ + ":(tenant_id)/networks/:(network_id)/ports") + return [extensions.ResourceExtension('stats', controller, + parent=parent_resource)] + + +class StatsController(wsgi.Controller): + _serialization_metadata = { + "application/xml": { + "attributes": { + "stats": ["rx_bytes", "rx_packets", "rx_errors", + "tx_bytes", "tx_packets", "tx_errors"], + }, + }, + } + + def __init__(self, plugin): + self._resource_name = 'stats' + self._plugin = plugin + + def _show(self, request, tenant_id, network_id, port_id): + """Returns port statistics for a given port""" + if not hasattr(self._plugin, "get_port_stats"): + return \ + faults.QuantumHTTPError( + qexception.NotImplementedError("get_port_stats")) + + stats = self._plugin.get_port_stats(tenant_id, network_id, + port_id) + builder = portstats_view.get_view_builder(request) + result = builder.build(stats, True) + return dict(stats=result) + + def index(self, request, tenant_id, network_id, port_id): + return self._show(request, tenant_id, network_id, port_id) + + def show(self, request, tenant_id, network_id, port_id, id): + return self._show(request, tenant_id, network_id, port_id)