~ubuntu-branches/ubuntu/saucy/quantum/saucy

« back to all changes in this revision

Viewing changes to quantum/plugins/nec/db/api.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short, Adam Gandelman, Yolanda Robla, James Page, Chuck Short
  • Date: 2013-02-22 09:54:36 UTC
  • mfrom: (2.1.18)
  • Revision ID: package-import@ubuntu.com-20130222095436-oala8yqn8hy2069z
Tags: 2013.1.g3-0ubuntu1
[ Adam Gandelman ]
* debian/patches/fix-quantum-configuration.patch: Refreshed and update to
  reflect new global root_wrap configuration defined only in
  /etc/quantum/quantum.conf.
* debian/patches/fix-ubuntu-tests.patch: Refreshed, added skipTests for
  linuxbridge tests that attempt to setup udev monitors.
* debian/{control, quantum-server.install}: Drop quantum-ns-metadata-proxy,
  quantum-metadata-agent, quantum-debug from quantum-server package and set a
  Break/Replaces on the last quantum-server package that shipped them.
  (LP: #1112203)
* debian/{control, quantum-plugin-nec.install}:  Drop rootwrap filters,
  only install /w the agent.
* debian/control: Add Breaks/Replaces between quantum-plugin-nec and new
  quantum-plugin-nec-agent.

[ Yolanda Robla ]
* debian/patches: refreshed patches

[ James Page ]
* d/p/fix-quantum-configuration.patch: Add root_wrap helper to AGENT
  section of /etc/quantum/quantum.conf to support new global configuration.
* d/control,quantum-plugin-{bigswitch,brocade,plumgrid}.install: Add new
  plugin packages for BigSwitch, Brocade, Hyper-V and PLUMgrid.
* d/p/fix-quantum-configuration.patch: Set sqlite location for new plugins.
* d/control,quantum-plugin-nec-agent.*: Add new plugin agent package for NEC.
* d/quantum-common.install: Added new binaries and common rootwrap.d config.
* d/control: Removed VCS fields, updated maintainer.
* Wrapped and sorted debian/*.
* d/control: Add BD on python-oslo-config.
* d/control: Add BD on python-netifaces to fix RYU unit tests.

[ Chuck Short ]
* New usptream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
 
20
20
from quantum.db import api as db
21
21
from quantum.db import model_base
 
22
from quantum.db import models_v2
 
23
from quantum.db import securitygroups_db as sg_db
 
24
from quantum.extensions import securitygroup as ext_sg
 
25
from quantum import manager
22
26
from quantum.openstack.common import log as logging
 
27
# NOTE (e0ne): this import is needed for config init
23
28
from quantum.plugins.nec.common import config
24
29
from quantum.plugins.nec.common import exceptions as nexc
25
30
from quantum.plugins.nec.db import models as nmodels
29
34
OFP_VLAN_NONE = 0xffff
30
35
 
31
36
 
 
37
resource_map = {'ofc_tenant': nmodels.OFCTenantMapping,
 
38
                'ofc_network': nmodels.OFCNetworkMapping,
 
39
                'ofc_port': nmodels.OFCPortMapping,
 
40
                'ofc_packet_filter': nmodels.OFCFilterMapping}
 
41
 
 
42
old_resource_map = {'ofc_tenant': nmodels.OFCTenant,
 
43
                    'ofc_network': nmodels.OFCNetwork,
 
44
                    'ofc_port': nmodels.OFCPort,
 
45
                    'ofc_packet_filter': nmodels.OFCFilter}
 
46
 
 
47
 
 
48
# utitlity methods
 
49
 
 
50
def _get_resource_model(resource, old_style):
 
51
    if old_style:
 
52
        return old_resource_map[resource]
 
53
    else:
 
54
        return resource_map[resource]
 
55
 
 
56
 
32
57
def initialize():
33
58
    db.configure_db()
34
59
 
37
62
    db.clear_db(base)
38
63
 
39
64
 
40
 
def get_ofc_item(model, id):
41
 
    session = db.get_session()
42
 
    try:
43
 
        return (session.query(model).
44
 
                filter_by(id=id).
45
 
                one())
46
 
    except sa.orm.exc.NoResultFound:
47
 
        return None
48
 
 
49
 
 
50
 
def find_ofc_item(model, quantum_id):
51
 
    session = db.get_session()
52
 
    try:
53
 
        return (session.query(model).
54
 
                filter_by(quantum_id=quantum_id).
55
 
                one())
56
 
    except sa.orm.exc.NoResultFound:
57
 
        return None
58
 
 
59
 
 
60
 
def add_ofc_item(model, id, quantum_id):
61
 
    session = db.get_session()
62
 
    try:
63
 
        item = model(id=id, quantum_id=quantum_id)
 
65
def get_ofc_item(session, resource, quantum_id, old_style=False):
 
66
    try:
 
67
        model = _get_resource_model(resource, old_style)
 
68
        return session.query(model).filter_by(quantum_id=quantum_id).one()
 
69
    except sa.orm.exc.NoResultFound:
 
70
        return None
 
71
 
 
72
 
 
73
def get_ofc_id(session, resource, quantum_id, old_style=False):
 
74
    ofc_item = get_ofc_item(session, resource, quantum_id, old_style)
 
75
    if ofc_item:
 
76
        if old_style:
 
77
            return ofc_item.id
 
78
        else:
 
79
            return ofc_item.ofc_id
 
80
    else:
 
81
        return None
 
82
 
 
83
 
 
84
def exists_ofc_item(session, resource, quantum_id, old_style=False):
 
85
    if get_ofc_item(session, resource, quantum_id, old_style):
 
86
        return True
 
87
    else:
 
88
        return False
 
89
 
 
90
 
 
91
def find_ofc_item(session, resource, ofc_id, old_style=False):
 
92
    try:
 
93
        model = _get_resource_model(resource, old_style)
 
94
        if old_style:
 
95
            params = dict(id=ofc_id)
 
96
        else:
 
97
            params = dict(ofc_id=ofc_id)
 
98
        return (session.query(model).filter_by(**params).one())
 
99
    except sa.orm.exc.NoResultFound:
 
100
        return None
 
101
 
 
102
 
 
103
def add_ofc_item(session, resource, quantum_id, ofc_id, old_style=False):
 
104
    try:
 
105
        model = _get_resource_model(resource, old_style)
 
106
        if old_style:
 
107
            params = dict(quantum_id=quantum_id, id=ofc_id)
 
108
        else:
 
109
            params = dict(quantum_id=quantum_id, ofc_id=ofc_id)
 
110
        item = model(**params)
64
111
        session.add(item)
65
112
        session.flush()
66
113
    except Exception as exc:
69
116
    return item
70
117
 
71
118
 
72
 
def del_ofc_item(model, id):
73
 
    session = db.get_session()
 
119
def del_ofc_item(session, resource, quantum_id, old_style=False,
 
120
                 warning=True):
74
121
    try:
75
 
        item = (session.query(model).
76
 
                filter_by(id=id).
77
 
                one())
 
122
        model = _get_resource_model(resource, old_style)
 
123
        item = session.query(model).filter_by(quantum_id=quantum_id).one()
78
124
        session.delete(item)
79
125
        session.flush()
 
126
        return True
80
127
    except sa.orm.exc.NoResultFound:
81
 
        LOG.warning(_("_del_ofc_item(): NotFound item "
82
 
                      "(model=%(model)s, id=%(id)s) "), locals())
83
 
 
84
 
 
85
 
def get_portinfo(id):
86
 
    session = db.get_session()
 
128
        if warning:
 
129
            LOG.warning(_("_del_ofc_item(): NotFound item "
 
130
                          "(model=%(model)s, id=%(id)s) "),
 
131
                        {'model': model, 'id': quantum_id})
 
132
        return False
 
133
 
 
134
 
 
135
def get_ofc_id_lookup_both(session, resource, quantum_id):
 
136
    ofc_id = get_ofc_id(session, resource, quantum_id)
 
137
    # Lookup old style of OFC mapping table
 
138
    if not ofc_id:
 
139
        ofc_id = get_ofc_id(session, resource, quantum_id,
 
140
                            old_style=True)
 
141
    if not ofc_id:
 
142
        reason = (_("NotFound %(resource)s for quantum_id=%(id)s.")
 
143
                  % {'resource': resource, 'id': quantum_id})
 
144
        raise nexc.OFCConsistencyBroken(reason=reason)
 
145
    return ofc_id
 
146
 
 
147
 
 
148
def exists_ofc_item_lookup_both(session, resource, quantum_id):
 
149
    if exists_ofc_item(session, resource, quantum_id):
 
150
        return True
 
151
    # Check old style of OFC mapping table
 
152
    if exists_ofc_item(session, resource, quantum_id,
 
153
                       old_style=True):
 
154
        return True
 
155
    return False
 
156
 
 
157
 
 
158
def del_ofc_item_lookup_both(session, resource, quantum_id):
 
159
    # Delete the mapping from new style of OFC mapping table
 
160
    if del_ofc_item(session, resource, quantum_id,
 
161
                    old_style=False, warning=False):
 
162
        return
 
163
    # Delete old style of OFC mapping table
 
164
    if del_ofc_item(session, resource, quantum_id,
 
165
                    old_style=True, warning=False):
 
166
        return
 
167
    # The specified resource not found
 
168
    LOG.warning(_("_del_ofc_item(): NotFound item "
 
169
                  "(resource=%(resource)s, id=%(id)s) "),
 
170
                {'resource': resource, 'id': quantum_id})
 
171
 
 
172
 
 
173
def get_portinfo(session, id):
87
174
    try:
88
175
        return (session.query(nmodels.PortInfo).
89
176
                filter_by(id=id).
92
179
        return None
93
180
 
94
181
 
95
 
def add_portinfo(id, datapath_id='', port_no=0, vlan_id=OFP_VLAN_NONE, mac=''):
96
 
    session = db.get_session()
 
182
def add_portinfo(session, id, datapath_id='', port_no=0,
 
183
                 vlan_id=OFP_VLAN_NONE, mac=''):
97
184
    try:
98
185
        portinfo = nmodels.PortInfo(id=id, datapath_id=datapath_id,
99
186
                                    port_no=port_no, vlan_id=vlan_id, mac=mac)
105
192
    return portinfo
106
193
 
107
194
 
108
 
def del_portinfo(id):
109
 
    session = db.get_session()
 
195
def del_portinfo(session, id):
110
196
    try:
111
 
        portinfo = (session.query(nmodels.PortInfo).
112
 
                    filter_by(id=id).
113
 
                    one())
 
197
        portinfo = session.query(nmodels.PortInfo).filter_by(id=id).one()
114
198
        session.delete(portinfo)
115
199
        session.flush()
116
200
    except sa.orm.exc.NoResultFound:
117
201
        LOG.warning(_("del_portinfo(): NotFound portinfo for "
118
202
                      "port_id: %s"), id)
 
203
 
 
204
 
 
205
def get_port_from_device(port_id):
 
206
    """Get port from database"""
 
207
    LOG.debug(_("get_port_with_securitygroups() called:port_id=%s"), port_id)
 
208
    session = db.get_session()
 
209
    sg_binding_port = sg_db.SecurityGroupPortBinding.port_id
 
210
 
 
211
    query = session.query(models_v2.Port,
 
212
                          sg_db.SecurityGroupPortBinding.security_group_id)
 
213
    query = query.outerjoin(sg_db.SecurityGroupPortBinding,
 
214
                            models_v2.Port.id == sg_binding_port)
 
215
    query = query.filter(models_v2.Port.id == port_id)
 
216
    port_and_sgs = query.all()
 
217
    if not port_and_sgs:
 
218
        return None
 
219
    port = port_and_sgs[0][0]
 
220
    plugin = manager.QuantumManager.get_plugin()
 
221
    port_dict = plugin._make_port_dict(port)
 
222
    port_dict[ext_sg.SECURITYGROUPS] = [
 
223
        sg_id for port, sg_id in port_and_sgs if sg_id]
 
224
    port_dict['security_group_rules'] = []
 
225
    port_dict['security_group_source_groups'] = []
 
226
    port_dict['fixed_ips'] = [ip['ip_address']
 
227
                              for ip in port['fixed_ips']]
 
228
    return port_dict