Coverage for nova/virt/vmwareapi/vif.py: 0%

66 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-04-24 11:16 +0000

1# Copyright (c) 2011 Citrix Systems, Inc. 

2# Copyright 2011 OpenStack Foundation 

3# 

4# Licensed under the Apache License, Version 2.0 (the "License"); you may 

5# not use this file except in compliance with the License. You may obtain 

6# a copy of the License at 

7# 

8# http://www.apache.org/licenses/LICENSE-2.0 

9# 

10# Unless required by applicable law or agreed to in writing, software 

11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 

12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 

13# License for the specific language governing permissions and limitations 

14# under the License. 

15 

16"""VIF drivers for VMware.""" 

17 

18from oslo_log import log as logging 

19from oslo_utils import versionutils 

20from oslo_vmware import vim_util 

21 

22import nova.conf 

23from nova import exception 

24from nova.i18n import _ 

25from nova.network import model 

26from nova.virt.vmwareapi import constants 

27from nova.virt.vmwareapi import network_util 

28from nova.virt.vmwareapi import vm_util 

29 

30LOG = logging.getLogger(__name__) 

31CONF = nova.conf.CONF 

32 

33 

34def _check_ovs_supported_version(session): 

35 # The port type 'ovs' is only support by the VC version 5.5 onwards 

36 min_version = versionutils.convert_version_to_int( 

37 constants.MIN_VC_OVS_VERSION) 

38 vc_version = versionutils.convert_version_to_int( 

39 vim_util.get_vc_version(session)) 

40 if vc_version < min_version: 

41 LOG.warning('VMware vCenter version less than %(version)s ' 

42 'does not support the \'ovs\' port type.', 

43 {'version': constants.MIN_VC_OVS_VERSION}) 

44 

45 

46def get_network_ref(session, cluster, vif): 

47 if vif['type'] == model.VIF_TYPE_OVS: 

48 _check_ovs_supported_version(session) 

49 # Check if this is the NSX-MH plugin is used 

50 if CONF.vmware.integration_bridge: 

51 net_id = CONF.vmware.integration_bridge 

52 use_external_id = False 

53 network_type = 'opaque' 

54 else: 

55 # The NSX|V3 plugin will pass the nsx-logical-switch-id as part 

56 # of the port details. This will enable the VC to connect to 

57 # that specific opaque network 

58 net_id = (vif.get('details') and 

59 vif['details'].get('nsx-logical-switch-id')) 

60 if not net_id: 

61 # Make use of the original one, in the event that the 

62 # plugin does not pass the aforementioned id 

63 LOG.info('NSX Logical switch ID is not present. ' 

64 'Using network ID to attach to the ' 

65 'opaque network.') 

66 net_id = vif['network']['id'] 

67 use_external_id = True 

68 network_type = 'nsx.LogicalSwitch' 

69 network_ref = {'type': 'OpaqueNetwork', 

70 'network-id': net_id, 

71 'network-type': network_type, 

72 'use-external-id': use_external_id} 

73 elif vif['type'] == model.VIF_TYPE_DVS: 

74 # Port binding for DVS VIF types may pass the name 

75 # of the port group, so use it if present 

76 vif_details = vif.get('details', {}) 

77 

78 dvs_uuid = vif_details.get('dvs_id') 

79 dvs_port_group_key = vif_details.get('pg_id') 

80 if dvs_uuid and dvs_port_group_key: 

81 network_ref = { 

82 'type': 'DistributedVirtualPortgroup', 

83 'dvpg': dvs_port_group_key, 

84 'dvsw': dvs_uuid 

85 } 

86 else: 

87 network_id = vif_details.get('dvs_port_group_name') 

88 if network_id is None: 

89 # Make use of the original one, in the event that the 

90 # port binding does not provide this key in VIF details 

91 network_id = vif['network']['bridge'] 

92 network_ref = network_util.get_network_with_the_name( 

93 session, network_id, cluster) 

94 if not network_ref: 

95 raise exception.NetworkNotFoundForBridge(bridge=network_id) 

96 if vif_details.get('dvs_port_key'): 

97 network_ref['dvs_port_key'] = vif_details['dvs_port_key'] 

98 else: 

99 reason = _('vif type %s not supported') % vif['type'] 

100 raise exception.InvalidInput(reason=reason) 

101 return network_ref 

102 

103 

104def get_vif_dict(session, cluster, vif_model, vif): 

105 mac = vif['address'] 

106 name = vif['network']['bridge'] or CONF.vmware.integration_bridge 

107 ref = get_network_ref(session, cluster, vif) 

108 return {'network_name': name, 

109 'mac_address': mac, 

110 'network_ref': ref, 

111 'iface_id': vif['id'], 

112 'vif_model': vif_model} 

113 

114 

115def get_vif_info(session, cluster, vif_model, network_info): 

116 vif_infos = [] 

117 if network_info is None: 

118 return vif_infos 

119 for vif in network_info: 

120 vif_infos.append(get_vif_dict(session, cluster, vif_model, vif)) 

121 return vif_infos 

122 

123 

124def get_network_device(hardware_devices, mac_address): 

125 """Return the network device with MAC 'mac_address'.""" 

126 for device in hardware_devices: 

127 if device.__class__.__name__ in vm_util.ALL_SUPPORTED_NETWORK_DEVICES: 

128 if hasattr(device, 'macAddress'): 

129 if device.macAddress == mac_address: 

130 return device