Skip to content

Commit

Permalink
Merge "Add vrouter to vm link when host_id is added by nova"
Browse files Browse the repository at this point in the history
  • Loading branch information
Zuul authored and opencontrail-ci-admin committed Jan 25, 2016
2 parents 73211b7 + 8a0b6f3 commit 7334e08
Showing 1 changed file with 68 additions and 60 deletions.
128 changes: 68 additions & 60 deletions src/config/api-server/vnc_cfg_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,27 @@ class VirtualMachineInterfaceServer(Resource, VirtualMachineInterface):
portbindings['VNIC_TYPE_DIRECT'] = 'direct'
portbindings['PORT_FILTER'] = True

@staticmethod
def _kvp_to_dict(kvps):
return dict((kvp['key'], kvp['value']) for kvp in kvps)
# end _kvp_to_dict

@classmethod
def _check_vrouter_link(cls, obj_dict, kvp_dict, db_conn):
host_id = kvp_dict.get('host_id')
vm_refs = obj_dict.get('virtual_machine_refs')

if (not host_id or not vm_refs or
kvp_dict.get('vif_type') != cls.portbindings['VIF_TYPE_HW_VEB']):
return
vrouter_fq_name = ['default-global-system-config', host_id]
vrouter_id = db_conn.fq_name_to_uuid('virtual-router', vrouter_fq_name)
cls.server.internal_request_ref_update(
'virtual-router', vrouter_id, 'ADD', 'virtual_machine',
vm_refs[0]['uuid'])
return
# end _check_vrouter_link

@classmethod
def pre_dbe_create(cls, tenant_name, obj_dict, db_conn):
vn_dict = obj_dict['virtual_network_refs'][0]
Expand All @@ -399,8 +420,8 @@ def pre_dbe_create(cls, tenant_name, obj_dict, db_conn):
proj_uuid = vn_dict['parent_uuid']
user_visibility = obj_dict['id_perms'].get('user_visible', True)
verify_quota_kwargs = {'db_conn': db_conn,
'fq_name': obj_dict['fq_name'],
'resource': 'virtual_machine_interfaces',
'fq_name': obj_dict['fq_name'],
'resource': 'virtual_machine_interfaces',
'obj_type': 'virtual-machine-interface',
'user_visibility': user_visibility,
'proj_uuid': proj_uuid}
Expand All @@ -415,9 +436,9 @@ def pre_dbe_create(cls, tenant_name, obj_dict, db_conn):
if 'virtual_machine_interface_mac_addresses' in obj_dict:
mc = obj_dict['virtual_machine_interface_mac_addresses']
if 'mac_address' in mc:
if len(mc['mac_address'])==1:
if len(mc['mac_address']) == 1:
inmac = [m.replace("-",":") for m in mc['mac_address']]
if inmac!=None:
if inmac != None:
mac_addrs_obj = MacAddressesType(inmac)
else:
mac_addr = cls.addr_mgmt.mac_alloc(obj_dict)
Expand All @@ -429,44 +450,42 @@ def pre_dbe_create(cls, tenant_name, obj_dict, db_conn):
mac_addrs_dict = json.loads(mac_addrs_json)
obj_dict['virtual_machine_interface_mac_addresses'] = mac_addrs_dict

if 'virtual_machine_interface_allowed_address_pairs' in obj_dict:
aap_config = obj_dict['virtual_machine_interface_allowed_address_pairs']
if 'allowed_address_pair' in aap_config:
aaps = aap_config['allowed_address_pair']
for aap in aaps or []:
if aap['mac'] == "":
aap['mac'] = obj_dict['virtual_machine_interface_mac_addresses']['mac_address']
aap_config = obj_dict.get(
'virtual_machine_interface_allowed_address_pairs', {})
for aap in aap_config.get('allowed_address_pair', []):
if aap['mac'] == "":
aap['mac'] = mac_addrs_dict['mac_address']

if 'virtual_machine_interface_bindings' in obj_dict:
bindings = obj_dict['virtual_machine_interface_bindings']
kvps = bindings['key_value_pair']

vif_type_presence = False
vnic_type_presence = False
for kvp in kvps:
if kvp['key'] == 'vif_type':
vif_type_presence = True
if kvp['key'] == 'vnic_type':
vnic_type_presence = True
if kvp['value'] == cls.portbindings['VNIC_TYPE_DIRECT']:
if not 'provider_properties' in vn_dict:
msg = 'No provider details in direct port'
return (False, (400, msg))
vif_type = {'key' : 'vif_type', 'value' : cls.portbindings['VIF_TYPE_HW_VEB']}
vif_type_presence = True
kvps.append(vif_type)
vif_params = {'port_filter': cls.portbindings['PORT_FILTER'], 'vlan' : str(vn_dict['provider_properties']['segmentation_id'])}
vif_details = {'key' : 'vif_details', 'value' : vif_params}
kvps.append(vif_details)

if not vif_type_presence:
vif_type = {'key' : 'vif_type', 'value' : cls.portbindings['VIF_TYPE_VROUTER']}
kvp_dict = cls._kvp_to_dict(kvps)

if kvp_dict.get('vnic_type') == cls.portbinding('VNIC_TYPE_DIRECT'):
if not 'provider_properties' in vn_dict:
msg = 'No provider details in direct port'
return (False, (400, msg))
kvp_dict['vif_type'] = cls.portbindings['VIF_TYPE_HW_VEB']
vif_type = {'key': 'vif_type',
'value': cls.portbindings['VIF_TYPE_HW_VEB']}
kvps.append(vif_type)
vlan = vn_dict['provider_properties']['segmentation_id']
vif_params = {'port_filter': cls.portbindings['PORT_FILTER'],
'vlan': str(vlan)}
vif_details = {'key': 'vif_details', 'value': vif_params}
kvps.append(vif_details)

if 'vif_type' not in kvp_dict:
vif_type = {'key': 'vif_type',
'value': cls.portbindings['VIF_TYPE_VROUTER']}
kvps.append(vif_type)

if not vnic_type_presence:
vnic_type = {'key' : 'vnic_type', 'value' : cls.portbindings['VNIC_TYPE_NORMAL']}
if 'vnic_type' not in kvp_dict:
vnic_type = {'key': 'vnic_type',
'value': cls.portbindings['VNIC_TYPE_NORMAL']}
kvps.append(vnic_type)

cls._check_vrouter_link(kvp_dict, obj_dict, db_conn)
return True, ""
# end pre_dbe_create

Expand Down Expand Up @@ -509,40 +528,29 @@ def pre_dbe_update(cls, id, fq_name, obj_dict, db_conn, **kwargs):
if not read_ok:
return (False, (500, read_result))

if 'virtual_machine_interface_allowed_address_pairs' in obj_dict:
aap_config = obj_dict['virtual_machine_interface_allowed_address_pairs']
if 'allowed_address_pair' in aap_config:
aaps = aap_config['allowed_address_pair']
for aap in aaps or []:
if aap['mac'] == "":
aap['mac'] = read_result['virtual_machine_interface_mac_addresses']['mac_address']
aap_config = obj_dict.get(
'virtual_machine_interface_allowed_address_pairs', {})
for aap in aap_config.get('allowed_address_pair', []):
if aap['mac'] == "":
aap['mac'] = read_result[
'virtual_machine_interface_mac_addresses']['mac_address']

bindings = read_result.get('virtual_machine_interface_bindings', {})
kvps = bindings.get('key_value_pair', [])
kvp_dict = cls._kvp_to_dict(kvps)
old_vnic_type = kvp_dict.get('vnic_type', 'normal')

old_vnic_type = 'normal'
if 'virtual_machine_interface_bindings' in read_result:
bindings = read_result['virtual_machine_interface_bindings']
if 'key_value_pair' in bindings:
kvps = bindings['key_value_pair']
for kvp in kvps:
if kvp['key'] == 'vnic_type':
old_vnic_type = kvp['value']
break

new_vnic_type = old_vnic_type
if 'virtual_machine_interface_bindings' in obj_dict:
bindings = obj_dict['virtual_machine_interface_bindings']
if 'key_value_pair' in bindings:
kvps = bindings['key_value_pair']
for kvp in kvps:
if kvp['key'] == 'vnic_type':
new_vnic_type = kvp['value']
break

if (old_vnic_type != new_vnic_type):
return (False, (409, "Vnic_type can not be modified"))
kvp_dict = cls._kvp_to_dict(kvps)
new_vnic_type = kvp_dict.get('vnic_type', old_vnic_type)
if (old_vnic_type != new_vnic_type):
return (False, (409, "Vnic_type can not be modified"))
cls._check_vrouter_link(kvp_dict, obj_dict, db_conn)

return True, ""

# end pre_dbe_update
# end class VirtualMachineInterfaceServer

Expand Down

0 comments on commit 7334e08

Please sign in to comment.