def get_trunks(self, context, filters=None, fields=None, sorts=None, limit=None, marker=None, page_reverse=False): ret = [] bottom_top_map = {} top_bottom_map = {} t_ctx = t_context.get_context_from_neutron_context(context) route_filters = [{'key': 'resource_type', 'comparator': 'eq', 'value': t_constants.RT_TRUNK}] routes = db_api.list_resource_routings(t_ctx, route_filters) for route in routes: bottom_top_map[route['bottom_id']] = route['top_id'] top_bottom_map[route['top_id']] = route['bottom_id'] if limit: if marker: mappings = db_api.get_bottom_mappings_by_top_id( t_ctx, marker, t_constants.RT_TRUNK) # if mapping exists, we retrieve trunk information # from bottom, otherwise from top if mappings: pod_id = mappings[0][0]['pod_id'] current_pod = db_api.get_pod(t_ctx, pod_id) ret = self._get_trunks_from_pod_with_limit( context, current_pod, bottom_top_map, top_bottom_map, filters, limit, marker) else: ret = self._get_trunks_from_top_with_limit( context, top_bottom_map, filters, limit, marker) else: current_pod = db_api.get_next_bottom_pod(t_ctx) # if current_pod exists, we retrieve trunk information # from bottom, otherwise from top if current_pod: ret = self._get_trunks_from_pod_with_limit( context, current_pod, bottom_top_map, top_bottom_map, filters, limit, None) else: ret = self._get_trunks_from_top_with_limit( context, top_bottom_map, filters, limit, None) else: pods = db_api.list_pods(t_ctx) _filters = self._transform_trunk_filters(filters, top_bottom_map) for pod in pods: if not pod['az_name']: continue client = self._get_client(pod['region_name']) pod_trunks = client.list_trunks(t_ctx, filters=_filters) ret.extend(pod_trunks) ret = self._map_trunks_from_bottom_to_top(ret, bottom_top_map) top_trunks = self._get_trunks_from_top(context, top_bottom_map, filters) ret.extend(top_trunks) return [super(TricircleTrunkPlugin, self)._fields(trunk, fields) for trunk in ret]
def test_get_next_bottom_pod(self): next_pod = api.get_next_bottom_pod(self.context) self.assertIsNone(next_pod) pods = [] for i in xrange(5): pod = { "pod_id": "test_pod_uuid_%d" % i, "pod_name": "test_pod_%d" % i, "pod_az_name": "test_pod_az_name_%d" % i, "dc_name": "test_dc_name_%d" % i, "az_name": "test_az_uuid_%d" % i, } api.create_pod(self.context, pod) pods.append(pod) next_pod = api.get_next_bottom_pod(self.context) self.assertEqual(next_pod, pods[0]) next_pod = api.get_next_bottom_pod(self.context, current_pod_id="test_pod_uuid_2") self.assertEqual(next_pod, pods[3]) next_pod = api.get_next_bottom_pod(self.context, current_pod_id="test_pod_uuid_4") self.assertIsNone(next_pod)
def test_get_next_bottom_pod(self): next_pod = api.get_next_bottom_pod(self.context) self.assertIsNone(next_pod) pods = [] for i in xrange(5): pod = {'pod_id': 'test_pod_uuid_%d' % i, 'region_name': 'test_pod_%d' % i, 'pod_az_name': 'test_pod_az_name_%d' % i, 'dc_name': 'test_dc_name_%d' % i, 'az_name': 'test_az_uuid_%d' % i, } api.create_pod(self.context, pod) pods.append(pod) next_pod = api.get_next_bottom_pod(self.context) self.assertEqual(next_pod, pods[0]) next_pod = api.get_next_bottom_pod( self.context, current_pod_id='test_pod_uuid_2') self.assertEqual(next_pod, pods[3]) next_pod = api.get_next_bottom_pod( self.context, current_pod_id='test_pod_uuid_4') self.assertIsNone(next_pod)
def _get_trunks_from_pod_with_limit(self, context, current_pod, bottom_top_map, top_bottom_map, filters, limit, marker): ret = [] t_ctx = t_context.get_context_from_neutron_context(context) q_client = self._get_client( current_pod['region_name']).get_native_client('trunk', t_ctx) params = {'limit': 0 if not limit else limit} if marker: b_marker = top_bottom_map[marker] params.update({'marker': b_marker}) if filters: if 'id' in filters: map_ids = self._get_map_trunk_ids(filters['id'], top_bottom_map) filters['id'] = map_ids params.update(filters) bottom_trunks = q_client.get(q_client.trunks_path, params=params)['trunks'] for bottom_trunk in bottom_trunks: top_id = bottom_top_map.get(bottom_trunk['id']) # TODO(xiulin): handle unmapped trunk if top_id: bottom_trunk['id'] = top_id ret.append(bottom_trunk) if len(ret) == limit: return ret remainder = limit - len(ret) next_pod = db_api.get_next_bottom_pod( t_ctx, current_pod_id=current_pod['pod_id']) if next_pod: # get from next bottom pod next_ret = self._get_trunks_from_pod_with_limit( context, next_pod, bottom_top_map, top_bottom_map, filters, remainder, None) ret.extend(next_ret) return ret else: # get from top pod top_ret = self._get_trunks_from_top_with_limit( context, top_bottom_map, filters, remainder, None) ret.extend(top_ret) return ret