Skip to content

Commit

Permalink
[WIP] Use oslo_db for database context
Browse files Browse the repository at this point in the history
  • Loading branch information
sc68cal committed Nov 10, 2015
1 parent f52d5db commit 661d60e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 19 deletions.
2 changes: 1 addition & 1 deletion neutron_classifier/common/constants.py
Expand Up @@ -7,4 +7,4 @@

ENCAPSULATION_TYPES = ['vxlan', 'gre']

NEUTRON_SERVICES = ['neutron-fwaas', 'networking-sfc']
NEUTRON_SERVICES = ['neutron-fwaas', 'networking-sfc', 'security-group']
31 changes: 24 additions & 7 deletions neutron_classifier/db/api.py
Expand Up @@ -13,21 +13,30 @@
# under the License.

from neutron_classifier.db import models
from oslo_db.sqlalchemy import enginefacade


@enginefacade.transaction_context_provider
class ClassifierContext(object):
"Classifier Database Context."


def get_classifier_chain():
pass


def create_classifier_chain(classifier_group, classifier):
@enginefacade.writer
def create_classifier_chain(context, classifier_group, classifier):
chain = models.ClassifierChainEntry()
chain.sequence = 1
chain.classifier = classifier
chain.classifier_group = classifier_group
return chain
context.session.add(chain)


def convert_security_group_rule_to_classifier(security_group_rule):
@enginefacade.writer
def convert_security_group_rule_to_classifier(context, security_group_rule):
# TODO(sc68cal) Pass in the classifier group
group = models.ClassifierGroup()
group.service = 'security-group'

Expand All @@ -48,17 +57,25 @@ def convert_security_group_rule_to_classifier(security_group_rule):
chain2 = models.ClassifierChainEntry()
chain2.classifier_group = group
chain2.classifier = cl2
# Security Group calssifiers might not need to be nested or have sequences
# Security Group classifiers might not need to be nested or have sequences
chain2.sequence = 1
context.session.add(group)
context.session.add(cl1)
context.session.add(cl2)
context.session.add(chain1)
context.session.add(chain2)


def convert_firewall_rule_to_classifier(firewall_rule):
@enginefacade.writer
def convert_firewall_rule_to_classifier(context, firewall_rule):
pass


def convert_classifier_chain_to_security_group(chain_id):
@enginefacade.reader
def convert_classifier_chain_to_security_group(context, chain_id):
pass


def convert_classifier_to_firewall_policy(chain_id):
@enginefacade.reader
def convert_classifier_to_firewall_policy(context, chain_id):
pass
19 changes: 8 additions & 11 deletions neutron_classifier/tests/test_db_api.py
Expand Up @@ -13,21 +13,22 @@
# under the License.
from neutron_classifier.db import api
from neutron_classifier.db import models
import sqlalchemy as sa
from sqlalchemy.orm import sessionmaker

from oslotest import base
from oslo_utils import uuidutils
from oslo_db.sqlalchemy import enginefacade

enginefacade.configure(
sql_connection='sqlite:///:memory:',
sqlite_fk=True,
max_retries=5
)


class DbApiTestCase(base.BaseTestCase):

def setUp(self):
super(DbApiTestCase, self).setUp()
engine = sa.create_engine('sqlite:///:memory:', echo=True)
Session = sessionmaker(bind=engine)
self.session = Session()
models.Base.metadata.create_all(engine)

def test_create_classifier_chain(self):
# TODO(sc68cal) Make this not hacky, and make it pass a session
Expand All @@ -42,10 +43,6 @@ def test_create_classifier_chain(self):
b.destination_ip_prefix = 'fd70:fbb6:449e::/48'
b.source_ip_prefix = 'fddf:cb3b:bc4::/48'
result = api.create_classifier_chain(a, b)
self.session.add(a)
self.session.add(b)
self.session.add(result)
self.session.commit()

def test_convert_security_group_rule_to_classifier(self):
sg_rule = {'direction': 'INGRESS',
Expand All @@ -56,7 +53,7 @@ def test_convert_security_group_rule_to_classifier(self):
'port_range_max': 80,
'remote_ip_prefix': 'fddf:cb3b:bc4::/48',
}
api.convert_security_group_rule_to_classifier(sg_rule)
result = api.convert_security_group_rule_to_classifier(sg_rule)

def test_convert_firewall_rule_to_classifier(self):
firewall_rule = {'protocol': 'foo',
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Expand Up @@ -6,3 +6,4 @@ pbr>=1.6
Babel>=1.3
sqlalchemy
oslo.utils>=2.0.0 # Apache-2.0
oslo.db>=3.0.0 # Apache-2.0

0 comments on commit 661d60e

Please sign in to comment.