Skip to content

Commit

Permalink
[VNC OpenStack] Improve port list
Browse files Browse the repository at this point in the history
When list port with filter device ids through the neutron api, the
device ids can be a contrail virtual machine interface or logical router
id. That patch only lists logical routers if all device ids was not
already founded in virtual machine interfaces.

Backporting note:
The exact commit needed a merge conflict resolution due to conflict with
daaa951.

Change-Id: I022806b9b996fe55f078887929ef3208d66ae36a
Closes-bug: #1607584
Signed-off-by: Martin Millnert <martin@millnert.se>
  • Loading branch information
Édouard Thuleau authored and Millnert committed Oct 26, 2016
1 parent 6bda5c2 commit 4e8d036
Showing 1 changed file with 43 additions and 53 deletions.
96 changes: 43 additions & 53 deletions src/config/vnc_openstack/vnc_openstack/neutron_plugin_db.py
Expand Up @@ -120,12 +120,15 @@ def _relay_request(self, request):
request.body, {'Content-type': request.environ['CONTENT_TYPE']})
#end _relay_request

def _validate_project_ids(self, context=None, project_ids=None):
def _validate_project_ids(self, context=None, filters=None):
if context and not context['is_admin']:
return [context['tenant']]
return [str(uuid.UUID(context['tenant']))]

if not filters.get('tenant_id'):
return None

return_project_ids = []
for project_id in project_ids or []:
for project_id in filters.get('tenant_id'):
try:
return_project_ids.append(str(uuid.UUID(project_id)))
except ValueError:
Expand Down Expand Up @@ -2502,8 +2505,7 @@ def _collect_without_prune(net_ids):
_collect_without_prune(filters['id'])
else:
# read all networks in project, and prune below
proj_ids = self._validate_project_ids(context, filters['tenant_id'])
for p_id in proj_ids:
for p_id in self._validate_project_ids(context, filters) or []:
all_net_objs.extend(self._network_list_project(p_id))
if 'router:external' in filters:
all_net_objs.extend(self._network_list_router_external())
Expand Down Expand Up @@ -2931,9 +2933,7 @@ def ipam_list(self, context=None, filters=None):
# collect phase
all_ipams = [] # all ipams in all projects
if filters and 'tenant_id' in filters:
project_ids = self._validate_project_ids(context,
filters['tenant_id'])
for p_id in project_ids:
for p_id in self._validate_project_ids(context, filters) or []:
project_ipams = self._ipam_list_project(p_id)
all_ipams.append(project_ipams)
else: # no filters
Expand Down Expand Up @@ -3021,9 +3021,7 @@ def policy_list(self, context=None, filters=None):
# collect phase
all_policys = [] # all policys in all projects
if filters and 'tenant_id' in filters:
project_ids = self._validate_project_ids(context,
filters['tenant_id'])
for p_id in project_ids:
for p_id in self._validate_project_ids(context, filters) or []:
project_policys = self._policy_list_project(p_id)
all_policys.append(project_policys)
else: # no filters
Expand Down Expand Up @@ -3181,9 +3179,7 @@ def router_list(self, context=None, filters=None):
pass
else:
# read all routers in project, and prune below
project_ids = self._validate_project_ids(context,
filters['tenant_id'])
for p_id in project_ids:
for p_id in self._validate_project_ids(context, filters) or []:
if 'router:external' in filters:
all_rtrs.append(self._fip_pool_ref_routers(p_id))
else:
Expand Down Expand Up @@ -3461,9 +3457,8 @@ def floatingip_list(self, context, filters=None):
if 'id' in filters:
fip_ids = filters['id']
if 'tenant_id' in filters:
backref_ids = self._validate_project_ids(context,
filters['tenant_id'])
proj_ids = backref_ids
backref_ids = self._validate_project_ids(context, filters)
proj_ids = backref_ids or []
if 'port_id' in filters:
backref_ids = backref_ids or [] + filters['port_id']
port_ids = backref_ids
Expand Down Expand Up @@ -3782,12 +3777,7 @@ def port_list(self, context=None, filters=None):
'network:dhcp' in filters.get('device_owner', [])):
return []

project_ids = self._validate_project_ids(context,
filters.get('tenant_id'))
# normalize to dashed format uuid
project_ids = [str(uuid.UUID(p)) for p in project_ids or []]
if not project_ids:
project_ids = None
project_ids = self._validate_project_ids(context, filters)

port_objs = []
if filters.get('device_id'):
Expand All @@ -3797,30 +3787,38 @@ def port_list(self, context=None, filters=None):
obj_uuids=filters.get('id'),
back_ref_id=filters.get('device_id'))

# If some device ids not yet found look to router interfaces
found_device_ids = set(
vm_ref['uuid']
for vmi_obj in port_objs_filtered_by_device_id
for vm_ref in vmi_obj.get_virtual_machine_refs() or [])
not_found_device_ids = set(filters.get('device_id')) -\
found_device_ids
if not_found_device_ids:
# Port has a back_ref to logical router, so need to read in
# logical routers based on device ids
router_objs = self._logical_router_list(
obj_uuids=list(not_found_device_ids),
parent_id=project_ids,
fields=['virtual_machine_interface_back_refs'])
router_port_ids = [
vmi_ref['uuid']
for router_obj in router_objs
for vmi_ref in
router_obj.get_virtual_machine_interface_refs() or []
]
# Read all logical router ports and add it to the list
if router_port_ids:
port_objs.extend(self._virtual_machine_interface_list(
obj_uuids=router_port_ids,
parent_id=project_ids))

# Filter it with project ids if there are.
if project_ids:
port_objs.extend([p for p in port_objs_filtered_by_device_id
if p.parent_uuid in project_ids])
else:
port_objs.extend(port_objs_filtered_by_device_id)

# Port has a back_ref to logical router, so need to read in
# logical routers based on device ids
router_objs = self._logical_router_list(
obj_uuids=filters.get('device_id'),
parent_id=project_ids,
fields=['virtual_machine_interface_back_refs'])
router_port_ids = [
vmi_ref['uuid']
for router_obj in router_objs
for vmi_ref in
router_obj.get_virtual_machine_interface_refs() or []
]
# Read all logical router ports and add it to the list
if router_port_ids:
port_objs.extend(self._virtual_machine_interface_list(
obj_uuids=router_port_ids,
parent_id=project_ids))
else:
port_objs = self._virtual_machine_interface_list(
obj_uuids=filters.get('id'),
Expand Down Expand Up @@ -3974,9 +3972,7 @@ def security_group_list(self, context, filters=None):
all_sgs.append(project_sgs)
else: # admin context
if filters and 'tenant_id' in filters:
project_ids = self._validate_project_ids(context,
filters['tenant_id'])
for p_id in project_ids:
for p_id in self._validate_project_ids(context, filters) or []:
project_sgs = self._security_group_list_project(p_id, filters)
all_sgs.append(project_sgs)
else: # no tenant_id filter
Expand Down Expand Up @@ -4100,9 +4096,7 @@ def security_group_rule_list(self, context=None, filters=None):
# collect phase
all_sgs = []
if filters and 'tenant_id' in filters:
project_ids = self._validate_project_ids(context,
filters['tenant_id'])
for p_id in project_ids:
for p_id in self._validate_project_ids(context, filters) or []:
project_sgs = self._security_group_list_project(p_id, filters)
all_sgs.append(project_sgs)
else: # no filters
Expand Down Expand Up @@ -4172,9 +4166,7 @@ def route_table_list(self, context, filters=None):
# collect phase
all_rts = [] # all rts in all projects
if filters and 'tenant_id' in filters:
project_ids = self._validate_project_ids(context,
filters['tenant_id'])
for p_id in project_ids:
for p_id in self._validate_project_ids(context, filters) or []:
project_rts = self._route_table_list_project(p_id)
all_rts.append(project_rts)
elif filters and 'name' in filters:
Expand Down Expand Up @@ -4242,9 +4234,7 @@ def svc_instance_list(self, context, filters=None):
# collect phase
all_sis = [] # all sis in all projects
if filters and 'tenant_id' in filters:
project_ids = self._validate_project_ids(context,
filters['tenant_id'])
for p_id in project_ids:
for p_id in self._validate_project_ids(context, filters) or []:
project_sis = self._svc_instance_list_project(p_id)
all_sis.append(project_sis)
elif filters and 'name' in filters:
Expand Down

0 comments on commit 4e8d036

Please sign in to comment.