~niedbalski/ubuntu/vivid/neutron/fixes-1447803

« back to all changes in this revision

Viewing changes to neutron/plugins/cisco/db/nexus_db_v2.py

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2014-10-03 18:45:23 UTC
  • mfrom: (1.1.15)
  • Revision ID: package-import@ubuntu.com-20141003184523-4mt6dy1q3j8n30c9
Tags: 1:2014.2~rc1-0ubuntu1
* New upstream release candidate:
  - d/p/*: Refreshed.
  - d/control: Add python-requests-mock to BD's.
  - d/control: Align versioned requirements with upstream.
* Transition linuxbridge and openvswitch plugin users to modular
  layer 2 plugin (LP: #1323729):
  - d/control: Mark removed plugin packages as transitional, depend
    on neutron-plugin-ml2, mark oldlibs/extra.
  - d/neutron-plugin-{linuxbridge,openvswitch}.install: Drop.
  - d/control: Depend on neutron-plugin-ml2 for linuxbridge
    agent package.
  - d/neutron-plugin-linuxbridge-agent.upstart: Use ml2 plugin
    configuration files.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright 2012, Cisco Systems, Inc.
2
 
#
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
6
 
#
7
 
#         http://www.apache.org/licenses/LICENSE-2.0
8
 
#
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
13
 
#    under the License.
14
 
#
15
 
# @author: Rohit Agarwalla, Cisco Systems, Inc.
16
 
# @author: Arvind Somya, Cisco Systems, Inc. (asomya@cisco.com)
17
 
#
18
 
 
19
 
import sqlalchemy.orm.exc as sa_exc
20
 
 
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
25
 
 
26
 
 
27
 
LOG = logging.getLogger(__name__)
28
 
 
29
 
 
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,
34
 
                                      vlan_id=vlan_id,
35
 
                                      switch_ip=switch_ip,
36
 
                                      instance_id=instance_id)
37
 
 
38
 
 
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)
43
 
 
44
 
 
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,
50
 
                                               vlan_id=vlan_id,
51
 
                                               switch_ip=switch_ip,
52
 
                                               instance_id=instance_id)
53
 
    session.add(binding)
54
 
    session.flush()
55
 
    return binding
56
 
 
57
 
 
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,
63
 
                                         vlan_id=vlan_id,
64
 
                                         switch_ip=switch_ip,
65
 
                                         port_id=port_id,
66
 
                                         instance_id=instance_id)
67
 
    for bind in binding:
68
 
        session.delete(bind)
69
 
    session.flush()
70
 
    return binding
71
 
 
72
 
 
73
 
def update_nexusport_binding(port_id, new_vlan_id):
74
 
    """Updates nexusport binding."""
75
 
    if not new_vlan_id:
76
 
        LOG.warning(_("update_nexusport_binding called with no vlan"))
77
 
        return
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)
83
 
    session.flush()
84
 
    return binding
85
 
 
86
 
 
87
 
def get_nexusvm_bindings(vlan_id, instance_id):
88
 
    """Lists nexusvm bindings."""
89
 
    LOG.debug(_("get_nexusvm_binding() called"))
90
 
 
91
 
    return _lookup_all_nexus_bindings(vlan_id=vlan_id,
92
 
                                      instance_id=instance_id)
93
 
 
94
 
 
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,
99
 
                                      switch_ip=switch_ip,
100
 
                                      vlan_id=vlan_id)
101
 
 
102
 
 
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})
108
 
    try:
109
 
        return _lookup_all_nexus_bindings(port_id=port_id,
110
 
                                          switch_ip=switch_ip)
111
 
    except c_exc.NexusPortBindingNotFound:
112
 
        pass
113
 
 
114
 
 
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')
119
 
 
120
 
 
121
 
def _lookup_nexus_bindings(query_type, session=None, **bfilter):
122
 
    """Look up 'query_type' Nexus bindings matching the filter.
123
 
 
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.
129
 
    """
130
 
    if session is None:
131
 
        session = db.get_session()
132
 
    query_method = getattr(session.query(
133
 
        nexus_models_v2.NexusPortBinding).filter_by(**bfilter), query_type)
134
 
    try:
135
 
        bindings = query_method()
136
 
        if bindings:
137
 
            return bindings
138
 
    except sa_exc.NoResultFound:
139
 
        pass
140
 
    raise c_exc.NexusPortBindingNotFound(**bfilter)
141
 
 
142
 
 
143
 
def _lookup_all_nexus_bindings(session=None, **bfilter):
144
 
    return _lookup_nexus_bindings('all', session, **bfilter)
145
 
 
146
 
 
147
 
def _lookup_one_nexus_binding(session=None, **bfilter):
148
 
    return _lookup_nexus_bindings('one', session, **bfilter)
149
 
 
150
 
 
151
 
def _lookup_first_nexus_binding(session=None, **bfilter):
152
 
    return _lookup_nexus_bindings('first', session, **bfilter)