~zulcss/ubuntu/precise/quantum/trunk

« back to all changes in this revision

Viewing changes to quantum/plugins/metaplugin/agent/linuxbridge_quantum_agent.py

  • Committer: Chuck Short
  • Date: 2012-11-26 19:51:11 UTC
  • mfrom: (26.1.1 raring-proposed)
  • Revision ID: zulcss@ubuntu.com-20121126195111-jnz2cr4xi6whemw2
* New upstream release for the Ubuntu Cloud Archive.
* debian/patches/*: Refreshed for opening of Grizzly.
* New upstream release.
* debian/rules: FTFBS if there is missing binaries.
* debian/quantum-server.install: Add quantum-debug.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python
2
 
# vim: tabstop=4 shiftwidth=4 softtabstop=4
3
 
#
4
 
# Copyright 2012 Cisco Systems, Inc.
5
 
# Copyright 2012 NTT MCL, Inc.
6
 
# All Rights Reserved.
7
 
#
8
 
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
9
 
#    not use this file except in compliance with the License. You may obtain
10
 
#    a copy of the License at
11
 
#
12
 
#         http://www.apache.org/licenses/LICENSE-2.0
13
 
#
14
 
#    Unless required by applicable law or agreed to in writing, software
15
 
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16
 
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17
 
#    License for the specific language governing permissions and limitations
18
 
#    under the License.
19
 
#
20
 
#
21
 
# Performs per host Linux Bridge configuration for Quantum.
22
 
# Based on the structure of the OpenVSwitch agent in the
23
 
# Quantum OpenVSwitch Plugin.
24
 
# @author: Sumit Naiksatam, Cisco Systems, Inc.
25
 
 
26
 
import logging
27
 
import sys
28
 
import time
29
 
 
30
 
from sqlalchemy.ext.sqlsoup import SqlSoup
31
 
 
32
 
from quantum.openstack.common import cfg
33
 
from quantum.common import config as logging_config
34
 
from quantum.common import constants
35
 
from quantum.plugins.linuxbridge.common import config
36
 
import quantum.plugins.linuxbridge.agent.linuxbridge_quantum_agent as lb
37
 
 
38
 
from quantum.agent.linux import utils
39
 
 
40
 
logging.basicConfig()
41
 
LOG = logging.getLogger(__name__)
42
 
 
43
 
BRIDGE_NAME_PREFIX = "brq"
44
 
VLAN_BINDINGS = "vlan_bindings"
45
 
PORT_BINDINGS = "port_bindings"
46
 
 
47
 
 
48
 
class MetaLinuxBridgeQuantumAgent(lb.LinuxBridgeQuantumAgent):
49
 
 
50
 
    def manage_networks_on_host(self, db,
51
 
                                old_vlan_bindings,
52
 
                                old_port_bindings):
53
 
        vlan_bindings = {}
54
 
        try:
55
 
            flavor_key = db.flavors.network_id
56
 
            vlan_key = db.vlan_bindings.network_id
57
 
            query = db.session.query(db.vlan_bindings)
58
 
            joined = query.join((db.flavors,
59
 
                                 flavor_key == vlan_key))
60
 
            where = db.flavors.flavor == 'linuxbridge'
61
 
            vlan_binds = joined.filter(where).all()
62
 
        except Exception as e:
63
 
            LOG.info("Unable to get vlan bindings! Exception: %s" % e)
64
 
            self.db_connected = False
65
 
            return {VLAN_BINDINGS: {},
66
 
                    PORT_BINDINGS: []}
67
 
 
68
 
        vlans_string = ""
69
 
        for bind in vlan_binds:
70
 
            entry = {'network_id': bind.network_id, 'vlan_id': bind.vlan_id}
71
 
            vlan_bindings[bind.network_id] = entry
72
 
            vlans_string = "%s %s" % (vlans_string, entry)
73
 
 
74
 
        port_bindings = []
75
 
        try:
76
 
            flavor_key = db.flavors.network_id
77
 
            port_key = db.ports.network_id
78
 
            query = db.session.query(db.ports)
79
 
            joined = query.join((db.flavors,
80
 
                                 flavor_key == port_key))
81
 
            where = db.flavors.flavor == 'linuxbridge'
82
 
            port_binds = joined.filter(where).all()
83
 
        except Exception as e:
84
 
            LOG.info("Unable to get port bindings! Exception: %s" % e)
85
 
            self.db_connected = False
86
 
            return {VLAN_BINDINGS: {},
87
 
                    PORT_BINDINGS: []}
88
 
 
89
 
        all_bindings = {}
90
 
        for bind in port_binds:
91
 
            append_entry = False
92
 
            if self.target_v2_api:
93
 
                all_bindings[bind.id] = bind
94
 
                entry = {'network_id': bind.network_id,
95
 
                         'uuid': bind.id,
96
 
                         'status': bind.status,
97
 
                         'interface_id': bind.id}
98
 
                append_entry = bind.admin_state_up
99
 
            else:
100
 
                all_bindings[bind.uuid] = bind
101
 
                entry = {'network_id': bind.network_id, 'state': bind.state,
102
 
                         'op_status': bind.op_status, 'uuid': bind.uuid,
103
 
                         'interface_id': bind.interface_id}
104
 
                append_entry = bind.state == constants.PORT_STATUS_ACTIVE
105
 
            if append_entry:
106
 
                port_bindings.append(entry)
107
 
 
108
 
        plugged_interfaces = []
109
 
        ports_string = ""
110
 
        for pb in port_bindings:
111
 
            ports_string = "%s %s" % (ports_string, pb)
112
 
            port_id = pb['uuid']
113
 
            interface_id = pb['interface_id']
114
 
 
115
 
            vlan_id = str(vlan_bindings[pb['network_id']]['vlan_id'])
116
 
            if self.process_port_binding(port_id,
117
 
                                         pb['network_id'],
118
 
                                         interface_id,
119
 
                                         vlan_id):
120
 
                if self.target_v2_api:
121
 
                    all_bindings[port_id].status = constants.PORT_STATUS_ACTIVE
122
 
                else:
123
 
                    all_bindings[port_id].op_status = (
124
 
                        constants.PORT_STATUS_ACTIVE)
125
 
 
126
 
            plugged_interfaces.append(interface_id)
127
 
 
128
 
        if old_port_bindings != port_bindings:
129
 
            LOG.debug("Port-bindings: %s" % ports_string)
130
 
 
131
 
        self.process_unplugged_interfaces(plugged_interfaces)
132
 
 
133
 
        if old_vlan_bindings != vlan_bindings:
134
 
            LOG.debug("VLAN-bindings: %s" % vlans_string)
135
 
 
136
 
        self.process_deleted_networks(vlan_bindings)
137
 
 
138
 
        try:
139
 
            db.commit()
140
 
        except Exception as e:
141
 
            LOG.info("Unable to update database! Exception: %s" % e)
142
 
            db.rollback()
143
 
            vlan_bindings = {}
144
 
            port_bindings = []
145
 
 
146
 
        return {VLAN_BINDINGS: vlan_bindings,
147
 
                PORT_BINDINGS: port_bindings}
148
 
 
149
 
 
150
 
def main():
151
 
    cfg.CONF(args=sys.argv, project='quantum')
152
 
 
153
 
    # (TODO)  - swap with common logging
154
 
    logging_config.setup_logging(cfg.CONF)
155
 
 
156
 
    br_name_prefix = BRIDGE_NAME_PREFIX
157
 
    physical_interface = cfg.CONF.LINUX_BRIDGE.physical_interface
158
 
    polling_interval = cfg.CONF.AGENT.polling_interval
159
 
    reconnect_interval = cfg.CONF.DATABASE.reconnect_interval
160
 
    root_helper = cfg.CONF.AGENT.root_helper
161
 
    'Establish database connection and load models'
162
 
    db_connection_url = cfg.CONF.DATABASE.sql_connection
163
 
    plugin = MetaLinuxBridgeQuantumAgent(br_name_prefix, physical_interface,
164
 
                                         polling_interval, reconnect_interval,
165
 
                                         root_helper,
166
 
                                         cfg.CONF.AGENT.target_v2_api)
167
 
    LOG.info("Agent initialized successfully, now running... ")
168
 
    plugin.daemon_loop(db_connection_url)
169
 
 
170
 
    sys.exit(0)
171
 
 
172
 
if __name__ == "__main__":
173
 
    main()