Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
blueprint portbinding-ex-db related patch in nova: https://review.openstack.org/#/c/21141/ Only OVS and linux bridge plugins now support this feature. Change-Id: I42d9bc59130e2758dd6a221d8953d63ec10e1f3c
- Loading branch information
1 parent
8581676
commit 73900fd
Showing
9 changed files
with
363 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
quantum/db/migration/alembic_migrations/versions/176a85fc7d79_add_portbindings_db.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# vim: tabstop=4 shiftwidth=4 softtabstop=4 | ||
# | ||
# Copyright 2013 OpenStack Foundation | ||
# | ||
# 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. | ||
# | ||
|
||
"""Add portbindings db | ||
Revision ID: 176a85fc7d79 | ||
Revises: grizzly | ||
Create Date: 2013-03-21 14:59:53.052600 | ||
""" | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = '176a85fc7d79' | ||
down_revision = 'grizzly' | ||
|
||
# Change to ['*'] if this migration applies to all plugins | ||
|
||
migration_for_plugins = [ | ||
'quantum.plugins.openvswitch.ovs_quantum_plugin.OVSQuantumPluginV2', | ||
'quantum.plugins.linuxbridge.lb_quantum_plugin.LinuxBridgePluginV2', | ||
] | ||
|
||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
from quantum.db import migration | ||
|
||
|
||
def upgrade(active_plugin=None, options=None): | ||
if not migration.should_run(active_plugin, migration_for_plugins): | ||
return | ||
|
||
op.create_table( | ||
'portbindingports', | ||
sa.Column('port_id', sa.String(length=36), nullable=False), | ||
sa.Column('host', sa.String(length=255), nullable=False), | ||
sa.ForeignKeyConstraint(['port_id'], ['ports.id'], ondelete='CASCADE'), | ||
sa.PrimaryKeyConstraint('port_id') | ||
) | ||
|
||
|
||
def downgrade(active_plugin=None, options=None): | ||
if not migration.should_run(active_plugin, migration_for_plugins): | ||
return | ||
op.drop_table('portbindingports') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
# vim: tabstop=4 shiftwidth=4 softtabstop=4 | ||
|
||
# Copyright 2013 IBM Corp. | ||
# 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: Yong Sheng Gong, IBM, Corp. | ||
|
||
import sqlalchemy as sa | ||
from sqlalchemy import orm | ||
|
||
from quantum.api.v2 import attributes | ||
from quantum.db import db_base_plugin_v2 | ||
from quantum.db import model_base | ||
from quantum.db import models_v2 | ||
from quantum.extensions import portbindings | ||
from quantum.openstack.common import log as logging | ||
from quantum import policy | ||
|
||
|
||
LOG = logging.getLogger(__name__) | ||
|
||
|
||
class PortBindingPort(model_base.BASEV2): | ||
port_id = sa.Column(sa.String(36), | ||
sa.ForeignKey('ports.id', ondelete="CASCADE"), | ||
primary_key=True) | ||
host = sa.Column(sa.String(255), nullable=False) | ||
port = orm.relationship( | ||
models_v2.Port, | ||
backref=orm.backref("portbinding", | ||
lazy='joined', uselist=False, | ||
cascade='delete')) | ||
|
||
|
||
class PortBindingMixin(object): | ||
extra_binding_dict = None | ||
|
||
def _port_model_hook(self, context, original_model, query): | ||
query = query.outerjoin(PortBindingPort, | ||
(original_model.id == | ||
PortBindingPort.port_id)) | ||
return query | ||
|
||
def _port_result_filter_hook(self, query, filters): | ||
values = filters and filters.get(portbindings.HOST_ID, []) | ||
if not values: | ||
return query | ||
if len(values) == 1: | ||
query = query.filter(PortBindingPort.host == values[0]) | ||
else: | ||
query = query.filter(PortBindingPort.host.in_(values)) | ||
return query | ||
|
||
db_base_plugin_v2.QuantumDbPluginV2.register_model_query_hook( | ||
models_v2.Port, | ||
"portbindings_port", | ||
_port_model_hook, | ||
None, | ||
_port_result_filter_hook) | ||
|
||
def _check_portbindings_view_auth(self, context, port): | ||
#TODO(salv-orlando): Remove this as part of bp/make-authz-orthogonal | ||
keys_to_delete = [] | ||
for key in port: | ||
if key.startswith('binding'): | ||
policy_rule = "get_port:%s" % key | ||
if not policy.check(context, policy_rule, port): | ||
keys_to_delete.append(key) | ||
for key in keys_to_delete: | ||
del port[key] | ||
return port | ||
|
||
def _process_portbindings_create_and_update(self, context, port_data, | ||
port): | ||
host = port_data.get(portbindings.HOST_ID) | ||
host_set = attributes.is_attr_set(host) | ||
if not host_set: | ||
_extend_port_dict_binding_host(self, port, None) | ||
return | ||
with context.session.begin(subtransactions=True): | ||
bind_port = context.session.query( | ||
PortBindingPort).filter_by(port_id=port['id']).first() | ||
if not bind_port: | ||
context.session.add(PortBindingPort(port_id=port['id'], | ||
host=host)) | ||
else: | ||
bind_port.host = host | ||
_extend_port_dict_binding_host(self, port, host) | ||
|
||
def get_port_host(self, context, port_id): | ||
with context.session.begin(subtransactions=True): | ||
bind_port = context.session.query( | ||
PortBindingPort).filter_by(port_id=port_id).first() | ||
return bind_port and bind_port.host or None | ||
|
||
|
||
def _extend_port_dict_binding_host(plugin, port_res, host): | ||
port_res[portbindings.HOST_ID] = host | ||
if plugin.extra_binding_dict: | ||
port_res.update(plugin.extra_binding_dict) | ||
return port_res | ||
|
||
|
||
def _extend_port_dict_binding(plugin, port_res, port_db): | ||
if not isinstance(plugin, PortBindingMixin): | ||
return | ||
host = (port_db.portbinding and port_db.portbinding.host or None) | ||
return _extend_port_dict_binding_host( | ||
plugin, port_res, host) | ||
|
||
# Register dict extend functions for ports | ||
db_base_plugin_v2.QuantumDbPluginV2.register_dict_extend_funcs( | ||
attributes.PORTS, [_extend_port_dict_binding]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.