1
# Copyright 2012, Cisco Systems, Inc.
3
# Licensed under the Apache License, Version 2.0 (the "License"); you may
4
# not use this file except in compliance with the License. You may obtain
5
# a copy of the License at
7
# http://www.apache.org/licenses/LICENSE-2.0
9
# Unless required by applicable law or agreed to in writing, software
10
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12
# License for the specific language governing permissions and limitations
15
# @author: Rohit Agarwalla, Cisco Systems, Inc.
16
# @author: Arvind Somya, Cisco Systems, Inc. (asomya@cisco.com)
19
import sqlalchemy.orm.exc as sa_exc
21
import neutron.db.api as db
22
from neutron.openstack.common import log as logging
23
from neutron.plugins.cisco.common import cisco_exceptions as c_exc
24
from neutron.plugins.cisco.db import nexus_models_v2
27
LOG = logging.getLogger(__name__)
30
def get_nexusport_binding(port_id, vlan_id, switch_ip, instance_id):
31
"""Lists a nexusport binding."""
32
LOG.debug(_("get_nexusport_binding() called"))
33
return _lookup_all_nexus_bindings(port_id=port_id,
36
instance_id=instance_id)
39
def get_nexusvlan_binding(vlan_id, switch_ip):
40
"""Lists a vlan and switch binding."""
41
LOG.debug(_("get_nexusvlan_binding() called"))
42
return _lookup_all_nexus_bindings(vlan_id=vlan_id, switch_ip=switch_ip)
45
def add_nexusport_binding(port_id, vlan_id, switch_ip, instance_id):
46
"""Adds a nexusport binding."""
47
LOG.debug(_("add_nexusport_binding() called"))
48
session = db.get_session()
49
binding = nexus_models_v2.NexusPortBinding(port_id=port_id,
52
instance_id=instance_id)
58
def remove_nexusport_binding(port_id, vlan_id, switch_ip, instance_id):
59
"""Removes a nexusport binding."""
60
LOG.debug(_("remove_nexusport_binding() called"))
61
session = db.get_session()
62
binding = _lookup_all_nexus_bindings(session=session,
66
instance_id=instance_id)
73
def update_nexusport_binding(port_id, new_vlan_id):
74
"""Updates nexusport binding."""
76
LOG.warning(_("update_nexusport_binding called with no vlan"))
78
LOG.debug(_("update_nexusport_binding called"))
79
session = db.get_session()
80
binding = _lookup_one_nexus_binding(session=session, port_id=port_id)
81
binding.vlan_id = new_vlan_id
82
session.merge(binding)
87
def get_nexusvm_bindings(vlan_id, instance_id):
88
"""Lists nexusvm bindings."""
89
LOG.debug(_("get_nexusvm_binding() called"))
91
return _lookup_all_nexus_bindings(vlan_id=vlan_id,
92
instance_id=instance_id)
95
def get_port_vlan_switch_binding(port_id, vlan_id, switch_ip):
96
"""Lists nexusvm bindings."""
97
LOG.debug(_("get_port_vlan_switch_binding() called"))
98
return _lookup_all_nexus_bindings(port_id=port_id,
103
def get_port_switch_bindings(port_id, switch_ip):
104
"""List all vm/vlan bindings on a Nexus switch port."""
105
LOG.debug(_("get_port_switch_bindings() called, "
106
"port:'%(port_id)s', switch:'%(switch_ip)s'"),
107
{'port_id': port_id, 'switch_ip': switch_ip})
109
return _lookup_all_nexus_bindings(port_id=port_id,
111
except c_exc.NexusPortBindingNotFound:
115
def get_nexussvi_bindings():
116
"""Lists nexus svi bindings."""
117
LOG.debug(_("get_nexussvi_bindings() called"))
118
return _lookup_all_nexus_bindings(port_id='router')
121
def _lookup_nexus_bindings(query_type, session=None, **bfilter):
122
"""Look up 'query_type' Nexus bindings matching the filter.
124
:param query_type: 'all', 'one' or 'first'
125
:param session: db session
126
:param bfilter: filter for bindings query
127
:return: bindings if query gave a result, else
128
raise NexusPortBindingNotFound.
131
session = db.get_session()
132
query_method = getattr(session.query(
133
nexus_models_v2.NexusPortBinding).filter_by(**bfilter), query_type)
135
bindings = query_method()
138
except sa_exc.NoResultFound:
140
raise c_exc.NexusPortBindingNotFound(**bfilter)
143
def _lookup_all_nexus_bindings(session=None, **bfilter):
144
return _lookup_nexus_bindings('all', session, **bfilter)
147
def _lookup_one_nexus_binding(session=None, **bfilter):
148
return _lookup_nexus_bindings('one', session, **bfilter)
151
def _lookup_first_nexus_binding(session=None, **bfilter):
152
return _lookup_nexus_bindings('first', session, **bfilter)