~salvatore-orlando/neutron/quantum-api

« back to all changes in this revision

Viewing changes to quantum/plugins/openvswitch/ovs_db.py

  • Committer: Salvatore Orlando
  • Date: 2011-06-24 13:52:17 UTC
  • mfrom: (6.1.14 quantum-trunk)
  • Revision ID: salvatore.orlando@eu.citrix.com-20110624135217-h6uz1zu3fxxpf3wt
Merge trunk
Resolving conflicts

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
 
2
# Copyright 2011 Nicira Networks, Inc.
 
3
# All Rights Reserved.
 
4
#
 
5
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
 
6
#    not use this file except in compliance with the License. You may obtain
 
7
#    a copy of the License at
 
8
#
 
9
#         http://www.apache.org/licenses/LICENSE-2.0
 
10
#
 
11
#    Unless required by applicable law or agreed to in writing, software
 
12
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 
13
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 
14
#    License for the specific language governing permissions and limitations
 
15
#    under the License.
 
16
# @author: Somik Behera, Nicira Networks, Inc.
 
17
# @author: Brad Hall, Nicira Networks, Inc.
 
18
# @author: Dan Wendlandt, Nicira Networks, Inc.
 
19
 
 
20
 
 
21
from sqlalchemy.orm import exc
 
22
 
 
23
import quantum.db.api as db
 
24
import quantum.db.models as models
 
25
import ovs_models
 
26
 
 
27
 
 
28
def get_vlans():
 
29
    session = db.get_session()
 
30
    try:
 
31
        bindings = session.query(ovs_models.VlanBinding).\
 
32
          all()
 
33
    except exc.NoResultFound:
 
34
        return []
 
35
    res = []
 
36
    for x in bindings:
 
37
        res.append((x.vlan_id, x.network_id))
 
38
    return res
 
39
 
 
40
 
 
41
def add_vlan_binding(vlanid, netid):
 
42
    session = db.get_session()
 
43
    binding = ovs_models.VlanBinding(vlanid, netid)
 
44
    session.add(binding)
 
45
    session.flush()
 
46
    return binding.vlan_id
 
47
 
 
48
 
 
49
def remove_vlan_binding(netid):
 
50
    session = db.get_session()
 
51
    try:
 
52
        binding = session.query(ovs_models.VlanBinding).\
 
53
          filter_by(network_id=netid).\
 
54
          one()
 
55
        session.delete(binding)
 
56
    except exc.NoResultFound:
 
57
            pass
 
58
    session.flush()
 
59
 
 
60
 
 
61
def update_network_binding(netid, ifaceid):
 
62
    session = db.get_session()
 
63
    # Add to or delete from the bindings table
 
64
    if ifaceid == None:
 
65
        try:
 
66
            binding = session.query(ovs_models.NetworkBinding).\
 
67
              filter_by(network_id=netid).\
 
68
              one()
 
69
            session.delete(binding)
 
70
        except exc.NoResultFound:
 
71
            raise Exception("No binding found with network_id = %s" % netid)
 
72
    else:
 
73
        binding = ovs_models.NetworkBinding(netid, ifaceid)
 
74
        session.add(binding)
 
75
 
 
76
    session.flush()