~cisco-openstack/nova/network-refactoring-l2

« back to all changes in this revision

Viewing changes to nova/virt/vmwareapi/vif.py

  • Committer: Ryu Ishimoto
  • Date: 2011-07-20 12:00:20 UTC
  • Revision ID: ryu@midokura.jp-20110720120020-jsij4ku1ivjn63as
Moved ensure_vlan_bridge of vmware to VIF driver

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
 
2
 
 
3
# Copyright (c) 2011 Citrix Systems, Inc.
 
4
# Copyright 2011 OpenStack LLC.
 
5
#
 
6
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
 
7
#    not use this file except in compliance with the License. You may obtain
 
8
#    a copy of the License at
 
9
#
 
10
#         http://www.apache.org/licenses/LICENSE-2.0
 
11
#
 
12
#    Unless required by applicable law or agreed to in writing, software
 
13
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 
14
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 
15
#    License for the specific language governing permissions and limitations
 
16
#    under the License.
 
17
 
 
18
"""VIF drivers for VMWare."""
 
19
 
 
20
from nova import db
 
21
from nova import exception
 
22
from nova import flags
 
23
from nova import log as logging
 
24
from nova import utils
 
25
from nova.virt.vif import VIFDriver
 
26
from nova.virt.vmwareapi_conn import VMWareAPISession
 
27
from nova.virt.vmwareapi import network_utils
 
28
 
 
29
 
 
30
LOG = logging.getLogger("nova.virt.vmwareapi.vif")
 
31
 
 
32
FLAGS = flags.FLAGS
 
33
 
 
34
 
 
35
class VMWareVlanBridgeDriver(VIFDriver):
 
36
    """VIF driver for Linux VLAN bridge."""
 
37
 
 
38
    def plug(self, instance, network, mapping):
 
39
        """Create a vlan and bridge unless they already exist."""
 
40
        vlan_num = network['vlan']
 
41
        bridge = network['bridge']
 
42
        bridge_interface = network['bridge_interface']
 
43
 
 
44
        # Open vmwareapi session
 
45
        host_ip = FLAGS.vmwareapi_host_ip
 
46
        host_username = FLAGS.vmwareapi_host_username
 
47
        host_password = FLAGS.vmwareapi_host_password
 
48
        if not host_ip or host_username is None or host_password is None:
 
49
            raise Exception(_('Must specify vmwareapi_host_ip, '
 
50
                              'vmwareapi_host_username '
 
51
                              'and vmwareapi_host_password to use '
 
52
                              'connection_type=vmwareapi'))
 
53
        session = VMWareAPISession(host_ip, host_username, host_password,
 
54
                                   FLAGS.vmwareapi_api_retry_count)
 
55
        vlan_interface = bridge_interface
 
56
        # Check if the vlan_interface physical network adapter exists on the host
 
57
        if not network_utils.check_if_vlan_interface_exists(session,
 
58
                                                            vlan_interface):
 
59
            raise exception.NetworkAdapterNotFound(adapter=vlan_interface)
 
60
 
 
61
        # Get the vSwitch associated with the Physical Adapter
 
62
        vswitch_associated = network_utils.get_vswitch_for_vlan_interface(
 
63
                                            session, vlan_interface)
 
64
        if vswitch_associated is None:
 
65
            raise exception.SwicthNotFoundForNetworkAdapter(adapter=vlan_interface)
 
66
        # Check whether bridge already exists and retrieve the the ref of the
 
67
        # network whose name_label is "bridge"
 
68
        network_ref = network_utils.get_network_with_the_name(session, bridge)
 
69
        if network_ref is None:
 
70
            # Create a port group on the vSwitch associated with the vlan_interface
 
71
            # corresponding physical network adapter on the ESX host
 
72
            network_utils.create_port_group(session, bridge, vswitch_associated,
 
73
                                    vlan_num)
 
74
        else:
 
75
            # Get the vlan id and vswitch corresponding to the port group
 
76
            pg_vlanid, pg_vswitch = \
 
77
                network_utils.get_vlanid_and_vswitch_for_portgroup(session, bridge)
 
78
 
 
79
            # Check if the vswitch associated is proper
 
80
            if pg_vswitch != vswitch_associated:
 
81
                raise exception.InvalidVLANPortGroup(bridge=bridge,
 
82
                                                     expected=vswitch_associated,
 
83
                                                     actual=pg_vswitch)
 
84
 
 
85
            # Check if the vlan id is proper for the port group
 
86
            if pg_vlanid != vlan_num:
 
87
                raise exception.InvalidVLANTag(bridge=bridge, tag=vlan_num,
 
88
                                               pgroup=pg_vlanid)
 
89
 
 
90
 
 
91
    def unplug(self, instance, network, mapping):
 
92
        pass
 
93