Isaku Yamahata a5f97ccc3b plugin: introduce ryu plugin
blueprint ovs-driver-extention
This patch implements the blueprint ovs-driver-extention
https://blueprints.launchpad.net/quantum/+spec/ovs-driver-extension

This patch factors out ovs common logic from ovs plugin into ovscommon
and adds Ryu NOS plugin.
This patch enhances ovs plugin for generic OVS controller support and

This patch is to add ofp controller support to OVS.
Store ofp controller address in ovs quantum data base.
- nova firewall_driver
- nova linuxnet_interface_driver

There may be ports unmanaged by nova/quantum. Those ports are used
to connect vm to outside of physical machine. They needs special care.

---
Changes 12 -> 13:
- rebased to 543e150d6dc9144ebcc588b7d2bd66374a107730
  changed files are only MANIFEST.in, setup.py, tools/pip-requres

Changes 11 -> 12:
- ryu agent
  eliminated from quantum.common import exceptions as exc
- ryu.db.api
  eliminated ofp_has_servers
- ryu.nova
  eliminated from quantum.plugins.ryu.nova import ovs_utils
  and eliminate ovs_utils

Chnages 10 -> 11:
- rebased to a945d1a30478c644d307c77a8a85f3a08e5a834e
- more Maru's review
- setup.py: fix setup() argument
  This isn't directly related to ryu plugin though
- improve fake ini file when unit test
  remove fake ini file after unit tests.
  use StringIO when no file is required.
- LOG: don't use %

Chnages 8 -> 9 -> 10:
- minor fixes: forgot to commit some hunks

Chnages 7 -> 8:
- rebased to d6bf2b76162ba806b2ad1f636f6273e47e03a117
- catch up d6bf2b76162ba806b2ad1f636f6273e47e03a117 change
  introduced bin/quantum_ryu_agent
- addressed Maru's review
  - avoid custom patching, use mock for test
    and added mox and mock to pip-requires
  - more pep8
  - avoid \ for line continuation
  - avoid single char variables
  - db.api: first() -> one()
  - utilize implicit conversion
    var is not None -> var
  - and more...

Changes 6 -> 7:
- update comment in ryu/run_tests.py
- make unit tests pass without ryu installed
  i.e.
  PLUGIN_DIR=quantum/plugins/ryu/ ./run_tests.sh
  works now

Chages 5 -> 6:
- remove comment

Change 4 -> 5:
- eliminate relative imports
- copyright
- doc string
- naming (s/CONF_FILE/conf_file/g)
- add " check to ryu/nova/ovs_utils
- ryu/nova/linux_net: comment
- ryu agent: eliminated unused methods
- updated ryu/README: add http://www.osrg.net/ryu/using_with_openstack.html
- added unit tests

Changes 3 -> 4:
- reflected Dan's review
- on-OVS in ryu.ini
- update @author
- some naming

Changes 2 -> 3:
- rebased to 04d144ae0b2ad5618847d1784cea48a08d53a46a
- abandoned to share code and duplicated codes from openvswitch plugin
  for ovs plugin stability.
- dropped setup_ryu.sh and added README
- update nova driver to catch up upstream change (gflags -> cfg)

Changes 1 -> 2:
- unbreak openvswtich unit test
- MANIFEST.in

Changes 3 -> new 1:
- rebased to 1eb3c693b5f6f3f301047100c36c7915434f8be7
- factor out common loginc from openvswitch plugin into ovscommon
- Introduced a new independent ryu plugin
- try new review due to the previous effort was marked abandoned.
  > https://review.openstack.org/#change,3055
  > Change-Id: I17801a7a74d4087838a8a26c1b1f97f28c2dcef3

Changes:
- rebased to 9c5c2caef13fa58234987527ab6caff829a37050
- some clean ups

Signed-off-by: Isaku Yamahata <yamahata@valinux.co.jp>
Change-Id: Ia9fe87525cebccc87b7c18a533f48607272cd97f
2012-02-26 13:30:25 +09:00

260 lines
7.0 KiB
Python

# 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: Somik Behera, Nicira Networks, Inc.
# @author: Brad Hall, Nicira Networks, Inc.
# @author: Dan Wendlandt, Nicira Networks, Inc.
import logging
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, exc
from quantum.api.api_common import OperationalStatus
from quantum.common import exceptions as q_exc
from quantum.db import models
_ENGINE = None
_MAKER = None
BASE = models.BASE
LOG = logging.getLogger('quantum.db.api')
def configure_db(options):
"""
Establish the database, create an engine if needed, and
register the models.
:param options: Mapping of configuration options
"""
global _ENGINE
if not _ENGINE:
_ENGINE = create_engine(options['sql_connection'],
echo=False,
echo_pool=True,
pool_recycle=3600)
register_models()
def clear_db():
global _ENGINE
assert _ENGINE
for table in reversed(BASE.metadata.sorted_tables):
_ENGINE.execute(table.delete())
def get_session(autocommit=True, expire_on_commit=False):
"""Helper method to grab session"""
global _MAKER, _ENGINE
if not _MAKER:
assert _ENGINE
_MAKER = sessionmaker(bind=_ENGINE,
autocommit=autocommit,
expire_on_commit=expire_on_commit)
return _MAKER()
def register_models():
"""Register Models and create properties"""
global _ENGINE
assert _ENGINE
BASE.metadata.create_all(_ENGINE)
def unregister_models():
"""Unregister Models, useful clearing out data before testing"""
global _ENGINE
assert _ENGINE
BASE.metadata.drop_all(_ENGINE)
def network_create(tenant_id, name, op_status=OperationalStatus.UNKNOWN):
session = get_session()
with session.begin():
net = models.Network(tenant_id, name, op_status)
session.add(net)
session.flush()
return net
def network_all_tenant_list():
session = get_session()
return session.query(models.Network).all()
def network_list(tenant_id):
session = get_session()
return session.query(models.Network).\
filter_by(tenant_id=tenant_id).\
all()
def network_get(net_id):
session = get_session()
try:
return session.query(models.Network).\
filter_by(uuid=net_id).\
one()
except exc.NoResultFound, e:
raise q_exc.NetworkNotFound(net_id=net_id)
def network_update(net_id, tenant_id, **kwargs):
session = get_session()
net = network_get(net_id)
for key in kwargs.keys():
net[key] = kwargs[key]
session.merge(net)
session.flush()
return net
def network_destroy(net_id):
session = get_session()
try:
net = session.query(models.Network).\
filter_by(uuid=net_id).\
one()
ports = session.query(models.Port).\
filter_by(network_id=net_id).\
all()
for p in ports:
session.delete(p)
session.delete(net)
session.flush()
return net
except exc.NoResultFound:
raise q_exc.NetworkNotFound(net_id=net_id)
def port_create(net_id, state=None, op_status=OperationalStatus.UNKNOWN):
# confirm network exists
network_get(net_id)
session = get_session()
with session.begin():
port = models.Port(net_id, op_status)
if state is None:
state = 'DOWN'
elif state not in ('ACTIVE', 'DOWN'):
raise q_exc.StateInvalid(port_state=state)
port['state'] = state
session.add(port)
session.flush()
return port
def port_list(net_id):
# confirm network exists
network_get(net_id)
session = get_session()
return session.query(models.Port).\
filter_by(network_id=net_id).\
all()
def port_get(port_id, net_id, session=None):
# confirm network exists
network_get(net_id)
if not session:
session = get_session()
try:
return session.query(models.Port).\
filter_by(uuid=port_id).\
filter_by(network_id=net_id).\
one()
except exc.NoResultFound:
raise q_exc.PortNotFound(net_id=net_id, port_id=port_id)
def port_update(port_id, net_id, **kwargs):
# confirm network exists
network_get(net_id)
port = port_get(port_id, net_id)
session = get_session()
for key in kwargs.keys():
if key == "state":
if kwargs[key] not in ('ACTIVE', 'DOWN'):
raise q_exc.StateInvalid(port_state=kwargs[key])
port[key] = kwargs[key]
session.merge(port)
session.flush()
return port
def port_set_attachment(port_id, net_id, new_interface_id):
# confirm network exists
network_get(net_id)
session = get_session()
port = port_get(port_id, net_id)
if new_interface_id != "":
# We are setting, not clearing, the attachment-id
if port['interface_id']:
raise q_exc.PortInUse(net_id=net_id, port_id=port_id,
att_id=port['interface_id'])
try:
port = session.query(models.Port).\
filter_by(interface_id=new_interface_id).\
one()
raise q_exc.AlreadyAttached(net_id=net_id,
port_id=port_id,
att_id=new_interface_id,
att_port_id=port['uuid'])
except exc.NoResultFound:
# this is what should happen
pass
port.interface_id = new_interface_id
session.merge(port)
session.flush()
return port
def port_unset_attachment(port_id, net_id):
# confirm network exists
network_get(net_id)
session = get_session()
port = port_get(port_id, net_id, session)
port.interface_id = None
session.add(port)
session.flush()
def port_destroy(port_id, net_id):
# confirm network exists
network_get(net_id)
session = get_session()
try:
port = session.query(models.Port).\
filter_by(uuid=port_id).\
filter_by(network_id=net_id).\
one()
if port['interface_id']:
raise q_exc.PortInUse(net_id=net_id, port_id=port_id,
att_id=port['interface_id'])
session.delete(port)
session.flush()
return port
except exc.NoResultFound:
raise q_exc.PortNotFound(port_id=port_id)