vmware-nsx/vmware_nsx/db/vnic_index_db.py
Boden R e86347e3a0 use core resource attribute constants from neutron-lib
neutron-lib now contains the API definitions for neutron's core
resources. This patch switches neutron core resource variables to
neutron-lib.

Change-Id: Id178e46f5fb220860de09316abff7684eff272ef
2017-05-23 13:08:46 -06:00

105 lines
4.2 KiB
Python

# Copyright 2014 VMware, 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.
from sqlalchemy.orm import exc
from neutron.db import _resource_extend as resource_extend
from neutron_lib.api.definitions import port as port_def
from oslo_db import exception as db_exc
from oslo_log import log as logging
from vmware_nsx.db import nsxv_models
from vmware_nsx.extensions import vnicindex as vnicidx
LOG = logging.getLogger(__name__)
@resource_extend.has_resource_extenders
class VnicIndexDbMixin(object):
@staticmethod
@resource_extend.extends([port_def.COLLECTION_NAME])
def _extend_port_vnic_index_binding(port_res, port_db):
state = port_db.vnic_index
port_res[vnicidx.VNIC_INDEX] = state.index if state else None
def _get_port_vnic_index(self, context, port_id):
"""Returns the vnic index for the given port.
If the port is not associated with any vnic then return None
"""
session = context.session
try:
mapping = (session.query(nsxv_models.NsxvPortIndexMapping).
filter_by(port_id=port_id).one())
return mapping['index']
except exc.NoResultFound:
LOG.debug("No record in DB for vnic-index of port %s", port_id)
def _get_mappings_for_device_id(self, context, device_id):
session = context.session
mappings = (session.query(nsxv_models.NsxvPortIndexMapping).
filter_by(device_id=device_id))
return mappings
def _create_port_vnic_index_mapping(self, context, port_id,
device_id, index):
"""Save the port vnic-index to DB."""
session = context.session
with session.begin(subtransactions=True):
index_mapping_model = nsxv_models.NsxvPortIndexMapping(
port_id=port_id, device_id=device_id, index=index)
session.add(index_mapping_model)
def _update_port_vnic_index_mapping(self, context, port_id,
device_id, index):
session = context.session
# delete original entry
query = (session.query(nsxv_models.NsxvPortIndexMapping).
filter_by(device_id=device_id, index=index))
query.delete()
# create a new one
self._create_port_vnic_index_mapping(context, port_id, device_id,
index)
def _set_port_vnic_index_mapping(self, context, port_id, device_id, index):
"""Save the port vnic-index to DB."""
try:
self._create_port_vnic_index_mapping(context, port_id,
device_id, index)
except db_exc.DBDuplicateEntry:
# A retry for the nova scheduling could result in this error.
LOG.debug("Entry already exists for %s %s %s", port_id,
device_id, index)
mappings = self._get_mappings_for_device_id(context, device_id)
for mapping in mappings:
if (mapping['port_id'] != port_id and
mapping['index'] == index):
# a new port is using this device - update!
self._update_port_vnic_index_mapping(context, port_id,
device_id, index)
return
if (mapping['port_id'] == port_id and
mapping['index'] != index):
raise
def _delete_port_vnic_index_mapping(self, context, port_id):
"""Delete the port vnic-index association."""
session = context.session
query = (session.query(nsxv_models.NsxvPortIndexMapping).
filter_by(port_id=port_id))
query.delete()