1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
3
# Copyright (c) 2011 Citrix Systems, Inc.
4
# Copyright 2011 OpenStack LLC.
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
10
# http://www.apache.org/licenses/LICENSE-2.0
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
18
"""Implements vlans for vmwareapi."""
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.vmwareapi_conn import VMWareAPISession
26
from nova.virt.vmwareapi import network_utils
29
LOG = logging.getLogger("nova.network.vmwareapi_net")
33
FLAGS['vlan_interface'].SetDefault('vmnic0')
36
def ensure_vlan_bridge(vlan_num, bridge, bridge_interface, net_attrs=None):
37
"""Create a vlan and bridge unless they already exist."""
38
# Open vmwareapi session
39
host_ip = FLAGS.vmwareapi_host_ip
40
host_username = FLAGS.vmwareapi_host_username
41
host_password = FLAGS.vmwareapi_host_password
42
if not host_ip or host_username is None or host_password is None:
43
raise Exception(_('Must specify vmwareapi_host_ip, '
44
'vmwareapi_host_username '
45
'and vmwareapi_host_password to use '
46
'connection_type=vmwareapi'))
47
session = VMWareAPISession(host_ip, host_username, host_password,
48
FLAGS.vmwareapi_api_retry_count)
49
vlan_interface = bridge_interface
50
# Check if the vlan_interface physical network adapter exists on the host
51
if not network_utils.check_if_vlan_interface_exists(session,
53
raise exception.NetworkAdapterNotFound(adapter=vlan_interface)
55
# Get the vSwitch associated with the Physical Adapter
56
vswitch_associated = network_utils.get_vswitch_for_vlan_interface(
57
session, vlan_interface)
58
if vswitch_associated is None:
59
raise exception.SwicthNotFoundForNetworkAdapter(adapter=vlan_interface)
60
# Check whether bridge already exists and retrieve the the ref of the
61
# network whose name_label is "bridge"
62
network_ref = network_utils.get_network_with_the_name(session, bridge)
63
if network_ref is None:
64
# Create a port group on the vSwitch associated with the vlan_interface
65
# corresponding physical network adapter on the ESX host
66
network_utils.create_port_group(session, bridge, vswitch_associated,
69
# Get the vlan id and vswitch corresponding to the port group
70
pg_vlanid, pg_vswitch = \
71
network_utils.get_vlanid_and_vswitch_for_portgroup(session, bridge)
73
# Check if the vswitch associated is proper
74
if pg_vswitch != vswitch_associated:
75
raise exception.InvalidVLANPortGroup(bridge=bridge,
76
expected=vswitch_associated,
79
# Check if the vlan id is proper for the port group
80
if pg_vlanid != vlan_num:
81
raise exception.InvalidVLANTag(bridge=bridge, tag=vlan_num,