| 1 |
# Copyright 2015 OpenStack Foundation |
| 2 |
# Copyright (c) 2015 Hewlett-Packard Development Company, L.P. |
| 3 |
# |
| 4 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 |
# not use this file except in compliance with the License. You may obtain |
| 6 |
# a copy of the License at |
| 7 |
# |
| 8 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
# |
| 10 |
# Unless required by applicable law or agreed to in writing, software |
| 11 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 |
# License for the specific language governing permissions and limitations |
| 14 |
# under the License. |
| 15 |
|
| 16 |
from neutron.db import models_v2 |
| 17 |
|
| 18 |
from neutron_lib.db import model_query |
| 19 |
from neutron_lib import exceptions |
| 20 |
from neutron_lib.plugins.ml2 import api |
| 21 |
import sqlalchemy as sa |
| 22 |
from sqlalchemy.orm import exc |
| 23 |
|
| 24 |
|
| 25 |
class L2GatewayCommonDbMixin: |
| 26 |
|
| 27 |
def _apply_filters_to_query(self, query, model, filters): |
| 28 |
"""Apply filters to query for the models.""" |
| 29 |
if filters: |
| 30 |
for key, value in filters.items(): |
| 31 |
column = getattr(model, key, None) |
| 32 |
if column: |
| 33 |
query = query.filter(column.in_(value)) |
| 34 |
return query |
| 35 |
|
| 36 |
def _model_query(self, context, model): |
| 37 |
"""Query model based on filter.""" |
| 38 |
query = context.session.query(model) |
| 39 |
query_filter = None |
| 40 |
if not context.is_admin and hasattr(model, 'tenant_id'): |
| 41 |
if hasattr(model, 'shared'): |
| 42 |
query_filter = ((model.tenant_id == context.tenant_id) | |
| 43 |
(model.shared == sa.true())) |
| 44 |
else: |
| 45 |
query_filter = (model.tenant_id == context.tenant_id) |
| 46 |
if query_filter is not None: |
| 47 |
query = query.filter(query_filter) |
| 48 |
return query |
| 49 |
|
| 50 |
def _get_collection_query(self, context, model, filters=None, |
| 51 |
sorts=None, limit=None, marker_obj=None, |
| 52 |
page_reverse=False): |
| 53 |
"""Get collection query for the models.""" |
| 54 |
collection = self._model_query(context, model) |
| 55 |
collection = model_query.apply_filters(collection, model, filters) |
| 56 |
return collection |
| 57 |
|
| 58 |
def _get_marker_obj(self, context, resource, limit, marker): |
| 59 |
"""Get marker object for the resource.""" |
| 60 |
if limit and marker: |
| 61 |
return getattr(self, '_get_%s' % resource)(context, marker) |
| 62 |
return None |
| 63 |
|
| 64 |
def _fields(self, resource, fields): |
| 65 |
"""Get fields for the resource for get query.""" |
| 66 |
if fields: |
| 67 |
return {key: item for key, item in resource.items() |
| 68 |
if key in fields} |
| 69 |
return resource |
| 70 |
|
| 71 |
def _get_tenant_id_for_create(self, context, resource): |
| 72 |
"""Get tenant id for creation of resources.""" |
| 73 |
if context.is_admin and 'tenant_id' in resource: |
| 74 |
tenant_id = resource['tenant_id'] |
| 75 |
elif ('tenant_id' in resource and |
| 76 |
resource['tenant_id'] != context.tenant_id): |
| 77 |
reason = _('Cannot create resource for another tenant') |
| 78 |
raise exceptions.AdminRequired(reason=reason) |
| 79 |
else: |
| 80 |
tenant_id = context.tenant_id |
| 81 |
return tenant_id |
| 82 |
|
| 83 |
def _get_collection(self, context, model, dict_func, filters=None, |
| 84 |
fields=None, sorts=None, limit=None, marker_obj=None, |
| 85 |
page_reverse=False): |
| 86 |
"""Get collection object based on query for resources.""" |
| 87 |
query = self._get_collection_query(context, model, filters=filters, |
| 88 |
sorts=sorts, |
| 89 |
limit=limit, |
| 90 |
marker_obj=marker_obj, |
| 91 |
page_reverse=page_reverse) |
| 92 |
items = [dict_func(c, fields) for c in query] |
| 93 |
if limit and page_reverse: |
| 94 |
items.reverse() |
| 95 |
return items |
| 96 |
|
| 97 |
def _make_segment_dict(self, record): |
| 98 |
"""Make a segment dictionary out of a DB record.""" |
| 99 |
return {api.ID: record.id, |
| 100 |
api.NETWORK_TYPE: record.network_type, |
| 101 |
api.PHYSICAL_NETWORK: record.physical_network, |
| 102 |
api.SEGMENTATION_ID: record.segmentation_id} |
| 103 |
|
| 104 |
def _get_network(self, context, id): |
| 105 |
try: |
| 106 |
network = self._get_by_id(context, models_v2.Network, id) |
| 107 |
except exc.NoResultFound: |
| 108 |
raise exceptions.NetworkNotFound(net_id=id) |
| 109 |
return network |
| 110 |
|
| 111 |
def _get_by_id(self, context, model, id): |
| 112 |
query = self._model_query(context, model) |
| 113 |
return query.filter(model.id == id).one() |