1
# Copyright 2014 VMware, Inc.
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
9
# http://www.apache.org/licenses/LICENSE-2.0
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
18
from oslo.db import exception as d_exc
19
from sqlalchemy import orm
21
from neutron.openstack.common import log as logging
22
from neutron.plugins.vmware.common import exceptions as p_exc
23
from neutron.plugins.vmware.dbexts import nsx_models
26
LOG = logging.getLogger(__name__)
29
def lsn_add(context, network_id, lsn_id):
30
"""Add Logical Service Node information to persistent datastore."""
31
with context.session.begin(subtransactions=True):
32
lsn = nsx_models.Lsn(network_id, lsn_id)
33
context.session.add(lsn)
36
def lsn_remove(context, lsn_id):
37
"""Remove Logical Service Node information from datastore given its id."""
38
with context.session.begin(subtransactions=True):
39
context.session.query(nsx_models.Lsn).filter_by(lsn_id=lsn_id).delete()
42
def lsn_remove_for_network(context, network_id):
43
"""Remove information about the Logical Service Node given its network."""
44
with context.session.begin(subtransactions=True):
45
context.session.query(nsx_models.Lsn).filter_by(
46
net_id=network_id).delete()
49
def lsn_get_for_network(context, network_id, raise_on_err=True):
50
"""Retrieve LSN information given its network id."""
51
query = context.session.query(nsx_models.Lsn)
53
return query.filter_by(net_id=network_id).one()
54
except (orm.exc.NoResultFound, d_exc.DBError):
55
msg = _('Unable to find Logical Service Node for network %s')
57
LOG.error(msg, network_id)
58
raise p_exc.LsnNotFound(entity='network',
61
LOG.warn(msg, network_id)
64
def lsn_port_add_for_lsn(context, lsn_port_id, subnet_id, mac, lsn_id):
65
"""Add Logical Service Node Port information to persistent datastore."""
66
with context.session.begin(subtransactions=True):
67
lsn_port = nsx_models.LsnPort(lsn_port_id, subnet_id, mac, lsn_id)
68
context.session.add(lsn_port)
71
def lsn_port_get_for_subnet(context, subnet_id, raise_on_err=True):
72
"""Return Logical Service Node Port information given its subnet id."""
73
with context.session.begin(subtransactions=True):
75
return (context.session.query(nsx_models.LsnPort).
76
filter_by(sub_id=subnet_id).one())
77
except (orm.exc.NoResultFound, d_exc.DBError):
79
raise p_exc.LsnPortNotFound(lsn_id=None,
84
def lsn_port_get_for_mac(context, mac_address, raise_on_err=True):
85
"""Return Logical Service Node Port information given its mac address."""
86
with context.session.begin(subtransactions=True):
88
return (context.session.query(nsx_models.LsnPort).
89
filter_by(mac_addr=mac_address).one())
90
except (orm.exc.NoResultFound, d_exc.DBError):
92
raise p_exc.LsnPortNotFound(lsn_id=None,
94
entity_id=mac_address)
97
def lsn_port_remove(context, lsn_port_id):
98
"""Remove Logical Service Node port from the given Logical Service Node."""
99
with context.session.begin(subtransactions=True):
100
(context.session.query(nsx_models.LsnPort).
101
filter_by(lsn_port_id=lsn_port_id).delete())