Wire in flow commands and parser
[netvirt.git] / resources / tools / odltools / odltools / netvirt / flows.py
1 import collections
2 import logging
3 from odltools.mdsal.models import ietf_interfaces
4 from odltools.mdsal.models import odl_fib
5 from odltools.mdsal.models import odl_interface_meta
6 from odltools.mdsal.models import odl_l3vpn
7 from odltools.mdsal.models import opendaylight_inventory
8 from odltools.mdsal.models.model import Model
9 from odltools.mdsal.models.models import Models
10 from odltools.mdsal.models.opendaylight_inventory import Nodes
11 from odltools.netvirt import utils
12 import config
13 import flow_parser
14
15
16 logger = logging.getLogger("netvirt.flows")
17
18
19 TABLE_MAP = {
20     'ifm': [0, 17, 220],
21     'l3vpn': [19, 20, 21, 22, 36, 81],
22     'elan': [50, 51, 52, 55],
23     'acl': [211, 212, 213, 214, 215, 241, 242, 243, 244, 245]
24 }
25
26 OPTIONALS = ['ifname', 'lport', 'elan-tag', 'mpls', 'vpnid', 'reason',
27              'dst-mac', 'src-mac', 'ofport', 'vlanid']
28
29
30 def filter_flow(flow_dict, filter_list):
31     if not filter_list:
32         return True
33     for flow_filter in filter_list:
34         if flow_dict.get(flow_filter):
35             return True
36     return False
37
38
39 def get_all_flows(args, modules=None, filter_by=None):
40     modules = modules if modules else ['ifm']
41     filter_by = filter_by if filter_by else []
42     if not modules:
43         return 'No modules specified'
44     ifaces = {}
45     ifstates = {}
46     ifindexes = {}
47     bindings = {}
48     einsts = {}
49     eifaces = {}
50     fibentries = {}
51     vpnids = {}
52     vpninterfaces = {}
53     groups = {}
54     if 'all' in modules:
55         table_list = list(range(0, 255))
56     else:
57         table_list = list(set([table for mod in modules for table in TABLE_MAP[mod]]))
58     # models = Models(args)
59     # odl_inventory_nodes_config = opendaylight_inventory.nodes(Model.CONFIG, args)
60     of_nodes = config.gmodels.odl_inventory_nodes_config.get_nodes_by_key()
61     # ietf_interfaces_interfaces = ietf_interfaces.interfaces(Model.CONFIG, args)
62     # ietf_interfaces_interfaces_state = ietf_interfaces.interfaces_state(Model.OPERATIONAL, args)
63     # odl_interface_meta_if_index_interface_map = odl_interface_meta.if_indexes_interface_map(Model.OPERATIONAL, args)
64     # odl_fib_fib_entries = odl_fib.fib_entries(Model.CONFIG, args)
65     # odl_l3vpn_vpn_instance_to_vpn_id = odl_l3vpn.vpn_instance_to_vpn_id(Model.CONFIG, args)
66     if 'ifm' in modules:
67         ifaces = config.gmodels.ietf_interfaces_interfaces.get_interfaces_by_key()
68         ifstates = config.gmodels.ietf_interfaces_interfaces_state.get_interfaces_by_key()
69     if 'l3vpn' in modules:
70         ifaces = ifaces or config.gmodels.ietf_interfaces_interfaces.get_interfaces_by_key()
71         ifindexes = ifindexes or config.gmodels.odl_interface_meta_if_index_interface_map.get_if_index_interfaces_by_key()
72         fibentries = fibentries or config.gmodels.odl_fib_fib_entries.get_vrf_entries_by_key()
73         vpnids = vpnids or config.gmodels.odl_l3vpn_vpn_instance_to_vpn_id.get_vpn_ids_by_key()
74         vpninterfaces = vpninterfaces or config.gmodels.l3vpn_vpn_interfaces.get_vpn_ids_by_key()
75         groups = groups or config.gmodels.odl_inventory_nodes_config.get_groups(of_nodes)
76     if 'acl' in modules:
77         ifaces = ifaces or config.gmodels.ietf_interfaces_interfaces.get_interfaces_by_key()
78         ifindexes = ifindexes or config.gmodels.odl_interface_meta_if_index_interface_map.get_if_index_interfaces_by_key()
79         einsts = einsts or config.gmodels.elan_elan_instances.get_elan_instances_by_key()
80         eifaces = eifaces or config.gmodels.elan_elan_interfaces.get_elan_interfaces()
81     if 'elan' in modules:
82         ifaces = ifaces or config.gmodels.ietf_interfaces_interfaces.get_interfaces_by_key()
83         einsts = einsts or config.gmodels.elan_elan_instances.get_elan_instances_by_key()
84         eifaces = eifaces or config.gmodels.elan_elan_interfaces.get_elan_interfaces()
85         ifindexes = ifindexes or config.gmodels.odl_interface_meta_if_index_interface_map.get_if_index_interfaces_by_key()
86     if 'all' in modules:
87         groups = groups or config.gmodels.odl_inventory_nodes_config.get_groups(of_nodes)
88         ifaces = ifaces or config.gmodels.ietf_interfaces_interfaces.get_interfaces_by_key()
89         ifstates = ifstates or config.gmodels.ietf_interfaces_interfaces_state.get_interfaces_by_key()
90         ifindexes = ifindexes or config.gmodels.odl_interface_meta_if_index_interface_map.get_if_index_interfaces_by_key()
91         fibentries = fibentries or config.gmodels.odl_fib_fib_entries.get_vrf_entries_by_key()
92         vpnids = vpnids or config.gmodels.odl_l3vpn_vpn_instance_to_vpn_id.get_vpn_ids_by_key()
93         vpninterfaces = vpninterfaces or config.gmodels.l3vpn_vpn_interfaces.get_vpn_ids_by_key()
94         einsts = einsts or config.gmodels.elan_elan_instances.get_elan_instances_by_key()
95         eifaces = eifaces or config.gmodels.elan_elan_interfaces.get_elan_interfaces()
96     flows = []
97     for node in of_nodes.itervalues():
98         tables = [x for x in node[Nodes.NODE_TABLE] if x['id'] in table_list]
99         for table in tables:
100             for flow in table.get('flow', []):
101                 flow_info = {'dpnid': Model.get_dpn_from_ofnodeid(node['id'])}
102                 flow_dict = get_any_flow(flow, flow_info, groups,
103                                          ifaces, ifstates, ifindexes,
104                                          fibentries, vpnids, vpninterfaces,
105                                          einsts, eifaces)
106                 if flow_dict is not None and filter_flow(flow_dict, filter_by):
107                     flows.append(flow_dict)
108     return flows
109
110
111 def create_flow_dict(flow_info, flow):
112     flow_dict = {'table': flow['table_id'], 'id': flow['id'],
113                  'name': flow.get('flow-name'), 'flow': flow,
114                  'dpnid': flow_info['dpnid']}
115     for opt in OPTIONALS:
116         if flow_info.get(opt):
117             flow_dict[opt] = flow_info.get(opt)
118     return flow_dict
119
120
121 def get_any_flow(flow, flow_info, groups, ifaces, ifstates, ifindexes,
122                  fibentries, vpnids, vpninterfaces, einsts, eifaces):
123     table = flow['table_id']
124     if table in TABLE_MAP['ifm']:
125         stale_ifm = stale_ifm_flow(flow, flow_info, ifaces, ifstates)
126         flow_info = stale_ifm if stale_ifm else flow_parser.get_flow_info_from_ifm_table(flow_info, flow)
127     elif table in TABLE_MAP['acl']:
128         stale_acl = stale_acl_flow(flow, flow_info, ifaces, ifindexes, einsts, eifaces)
129         flow_info = stale_acl if stale_acl else flow_parser.get_flow_info_from_acl_table(flow_info, flow)
130     elif table in TABLE_MAP['elan']:
131         stale_elan = stale_elan_flow(flow, flow_info, ifaces, ifindexes, einsts, eifaces)
132         flow_info = stale_elan if stale_elan else flow_parser.get_flow_info_from_elan_table(flow_info, flow)
133     elif table in TABLE_MAP['l3vpn']:
134         stale_l3vpn = stale_l3vpn_flow(flow, flow_info, groups, ifaces, ifindexes, vpnids, vpninterfaces, fibentries)
135         flow_info = stale_l3vpn if stale_l3vpn else flow_parser.get_flow_info_from_l3vpn_table(flow_info, flow)
136     else:
137         flow_info = flow_parser.get_flow_info_from_any(flow_info, flow)
138         iface = (get_iface_for_lport(ifaces, ifindexes, flow_info.get('lport'))
139                  if flow_info.get('lport') else None)
140         if iface and iface.get('name'):
141             flow_info['ifname'] = iface['name']
142     return create_flow_dict(flow_info, flow)
143
144
145 def stale_l3vpn_flow(flow, flow_info, groups, ifaces, ifindexes,
146                      vpnids, vpninterfaces, fibentries):
147     flow_parser.get_flow_info_from_l3vpn_table(flow_info, flow)
148     lport = flow_info.get('lport')
149     iface = get_iface_for_lport(ifaces, ifindexes, lport)
150     if lport and not iface:
151         flow_info['reason'] = 'Interface for lport not found'
152         return create_flow_dict(flow_info, flow)
153     if iface:
154         flow_info['ifname'] = iface['name']
155     vpninterface = vpninterfaces.get(iface.get('name')) if iface else None
156     if not vpninterfaces:
157         flow_info['reason'] = 'VpnInterface for Lport not found'
158         return create_flow_dict(flow_info, flow)
159     vpnid = flow_info.get('vpnid')
160     if vpnid and not vpnids.get(vpnid):
161         flow_info['reason'] = 'VpnInstance for VpnId not found'
162         return create_flow_dict(flow_info, flow)
163     if vpnid and vpninterface and vpnids.get(vpnid):
164         if (vpninterface.get('vpn-instance-name') !=
165                 vpnids[vpnid]['vpn-instance-name']):
166             flow_info['reason'] = 'Lport VpnId mismatch'
167             return create_flow_dict(flow_info, flow)
168     label = flow_info.get('label')
169     fibentry = fibentries.get(label) if label else None
170     if label and not fibentry:
171         flow_info['reason'] = 'Fibentry for MplsLabel not found'
172         return create_flow_dict(flow_info, flow)
173     # Label check for group
174     prefix = fibentry.get('destPrefix') if fibentry else None
175     if prefix and flow_info.get('group-id'):
176         gid = flow_info.get('group-id')
177         if groups.get(gid) and (
178                     groups.get(gid).get('group-name', '') != prefix):
179             flow_info['reason'] = 'DestPrefix mismatch for label and group'
180             return create_flow_dict(flow_info, flow)
181     return None
182
183
184 def stale_elan_flow(flow, flow_info, ifaces, ifindexes, einsts, eifaces):
185     # hex(int(mask, 16) & int(hexa, 16))
186     flow_parser.get_flow_info_from_elan_table(flow_info, flow)
187     lport = flow_info.get('lport')
188     eltag = flow_info.get('elan-tag')
189     iface = get_iface_for_lport(ifaces, ifindexes, lport)
190     if lport and not iface:
191         flow_info['reason'] = 'Interface for lport not found'
192         return create_flow_dict(flow_info, flow)
193     if iface:
194         flow_info['ifname'] = iface['name']
195     if not is_elantag_valid(eltag, eifaces, einsts, iface):
196         flow_info['reason'] = 'Lport Elantag mismatch'
197         return create_flow_dict(flow_info, flow)
198     return None
199
200
201 def stale_acl_flow(flow, flow_info, ifaces, ifindexes, einsts, eifaces):
202     flow_parser.get_flow_info_from_acl_table(flow_info, flow)
203     lport = flow_info.get('lport')
204     eltag = flow_info.get('elan-tag')
205     iface = get_iface_for_lport(ifaces, ifindexes, lport)
206     if lport and not iface:
207         flow_info['reason'] = 'Interface for lport not found'
208         return create_flow_dict(flow_info, flow)
209     if iface:
210         flow_info['ifname'] = iface['name']
211     if not is_elantag_valid(eltag, eifaces, einsts, iface):
212         flow_info['reason'] = 'Lport Elantag mismatch'
213         return create_flow_dict(flow_info, flow)
214     #return create_flow_dict(flow_info, flow)
215     return None
216
217
218 def is_elantag_valid(eltag, eifaces, einsts, iface):
219     if iface and eltag and eltag != get_eltag_for_iface(eifaces, einsts, iface):
220         return False
221     return True
222
223
224 def is_correct_elan_flow(flow_info, mmac, einsts, flow_host):
225     flow = flow_info.get('flow')
226     flow_etag = flow_info.get('elan-tag')
227     for k, v in mmac.iteritems():
228         mac_host = v.get('compute')
229         if einsts.get(k):
230             einst_tag = einsts.get(k).get('elan-tag')
231             # print einst_tag, flow_etag, mac_host
232             if flow_etag and einst_tag and flow_etag == einst_tag:
233                 if mac_host.startswith(flow_host):
234                     act_resubmit = get_act_resubmit(flow)
235                     if (act_resubmit and act_resubmit.get('table') == 220):
236                         return 'Correct'
237                 else:
238                     act_tunnel = get_act_set_tunnel(flow)
239                     if act_tunnel:
240                         return 'Correct'
241                 return 'Wrong'
242     return 'Wrong'
243
244
245 def get_iface_for_lport(ifaces, ifindexes, lport):
246     if lport:
247         if ifindexes.get(lport):
248             ifname = ifindexes.get(lport).get('interface-name')
249             if ifname and ifaces.get(ifname):
250                 return ifaces.get(ifname)
251     return None
252
253
254 def get_eltag_for_iface(eifaces, einsts, iface):
255     ifname = iface.get('name') if iface else None
256     eiface = eifaces.get(ifname) if ifname else None
257     einst_name = eiface.get('elan-instance-name') if eiface else None
258     einst = einsts.get(einst_name) if einst_name else None
259     return einst.get('elan-tag') if einst else None
260
261
262 def get_stale_flows(modules=['ifm']):
263     if not modules:
264         return 'No modules specified'
265     ifaces = {}
266     ifstates = {}
267     ifindexes = {}
268     bindings = {}
269     einsts = {}
270     eifaces = {}
271     fibentries = {}
272     vpnids = {}
273     vpninterfaces = {}
274     groups = {}
275     table_list = list(set([table for module in modules for table in TABLE_MAP[module]]))
276     ##table_list = [214, 244]
277
278     of_nodes = config.gmodels.odl_inventory_nodes_config.get_nodes_by_key()
279     if 'ifm' in modules:
280         ifaces = config.gmodels.ietf_interfaces_interfaces.get_interfaces_by_key()
281         ifstates = config.gmodels.ietf_interfaces_interfaces_state.get_interfaces_by_key()
282     if 'l3vpn' in modules:
283         ifaces = ifaces or config.gmodels.ietf_interfaces_interfaces.get_interfaces_by_key()
284         # ifindexes = ifindexes or config.gmodels.odl_interface_meta_if_index_interface_map.get_if_index_interfaces_by_key()
285         fibentries = fibentries or config.gmodels.odl_fib_fib_entries.get_vrf_entries_by_key()
286         vpnids = vpnids or config.gmodels.odl_l3vpn_vpn_instance_to_vpn_id.get_vpn_instances_by_key()
287         vpninterfaces = vpninterfaces or config.gmodels.l3vpn_vpn_interfaces.get_vpn_ids_by_key()
288         groups = groups or config.gmodels.odl_inventory_nodes_config.get_groups(of_nodes)
289     if 'acl' in modules:
290         ifaces = ifaces or config.gmodels.ietf_interfaces_interfaces.get_interfaces_by_key()
291         # ifindexes = ifindexes or config.gmodels.odl_interface_meta_if_index_interface_map.get_if_index_interfaces_by_key()
292         einsts = einsts or config.gmodels.elan_elan_instances.get_elan_instances_by_key()
293         eifaces = eifaces or config.gmodels.elan_elan_interfaces.get_elan_interfaces_by_key()
294     if 'elan' in modules:
295         ifaces = ifaces or config.gmodels.ietf_interfaces_interfaces.get_interfaces_by_key()
296         einsts = einsts or config.gmodels.elan_elan_instances.get_elan_instances_by_key()
297         eifaces = eifaces or config.gmodels.elan_elan_interfaces.get_elan_interfaces_by_key()
298         # ifindexes = ifindexes or config.gmodels.odl_interface_meta_if_index_interface_map.get_if_index_interfaces_by_key()
299     stale_flows = []
300     for node in of_nodes.itervalues():
301         tables = [x for x in node[Nodes.NODE_TABLE] if x['id'] in table_list]
302         for table in tables:
303             for flow in table.get('flow', []):
304                 flow_dict = None
305                 flow_info = {'dpnid': Model.get_dpn_from_ofnodeid(node['id'])}
306                 if 'ifm' in modules and table['id'] in TABLE_MAP['ifm']:
307                     flow_dict = stale_ifm_flow(flow, flow_info, ifaces, ifstates)
308                 if 'l3vpn' in modules and table['id'] in TABLE_MAP['l3vpn']:
309                     flow_dict = stale_l3vpn_flow(flow, flow_info, groups, ifaces, ifindexes, vpnids, vpninterfaces, fibentries)
310                 if 'elan' in modules and table['id'] in TABLE_MAP['elan']:
311                     flow_dict = stale_elan_flow(flow, flow_info, ifaces, ifindexes, einsts, eifaces)
312                 if 'acl' in modules and table['id'] in TABLE_MAP['acl']:
313                     flow_dict = stale_acl_flow(flow, flow_info, ifaces, ifindexes, einsts, eifaces)
314                 if flow_dict is not None:
315                     stale_flows.append(flow_dict)
316
317     return stale_flows
318
319
320 def show_link_flow_binding(args):
321     stale_ids, bindings = get_stale_bindings(args)
322     flows = get_stale_flows()
323     print len(stale_ids), len(flows)
324     for flow in flows:
325         if flow['ifname'] in stale_ids and 'bound-services' in bindings[flow['ifname']]:
326             print 'Flow with binding: ', flow['ifname']
327         else:
328             print 'Flow without binding: ', flow['ifname']
329
330
331 def show_stale_flows(args, sort_by='table'):
332     config.get_models(args, {
333         "elan_elan_instances",
334         "elan_elan_interfaces",
335         "ietf_interfaces_interfaces",
336         "ietf_interfaces_interfaces_state",
337         "interface_service_bindings_service_bindings",
338         "l3vpn_vpn_interfaces",
339         # "mip_mac",
340         "neutron_neutron",
341         "odl_fib_fib_entries",
342         # "odl_interface_meta_if_index_interface_map",
343         "odl_l3vpn_vpn_instance_to_vpn_id",
344         "odl_inventory_nodes_config"})
345     compute_map = config.gmodels.odl_inventory_nodes_config.get_dpn_host_mapping()
346     nports = config.gmodels.neutron_neutron.get_ports_by_key()
347
348     for flow in utils.sort(get_stale_flows(['ifm', 'acl', 'elan', 'l3vpn']), sort_by):
349         host = compute_map.get(flow.get('dpnid'), flow.get('dpnid'))
350         ip_list = get_ips_for_iface(nports, flow.get('ifname'))
351         if ip_list:
352             flow['iface-ips'] = ip_list
353         result = 'Table:{}, Host:{}, FlowId:{}{}'.format(
354             flow['table'], host, flow['id'],
355             utils.show_optionals(flow))
356         print result
357         ##path = get_data_path('flows', flow)
358         #print('http://192.168.2.32:8383/restconf/config/{}'.format(path))
359         #print 'Flow:', utils.format_json(args, flow_parser.parse_flow(flow['flow']))
360
361
362 def show_elan_flows(args):
363     compute_map = config.gmodels.odl_inventory_nodes_config.get_dpn_host_mapping()
364     for flow in utils.sort(get_all_flows(['elan']), 'id'):
365         host = compute_map.get(flow.get('dpnid'), flow.get('dpnid'))
366         result = 'MacHost:{}{}, Table:{}, FlowId:{}, {}, Flow:{}'.format(
367             flow['id'][-17:], host, flow['table'], flow['id'], utils.show_optionals(flow),
368            utils.format_json(args, flow_parser.parse_flow(flow['flow'])))
369         print result
370         #print 'Flow:', utils.format_json(args, flow_parser.parse_flow(flow['flow']))
371
372
373 def get_matchstr(args, flow):
374     if flow and flow.get('flow') and flow.get('flow').get('match'):
375         return utils.format_json(args, flow.get('flow').get('match', None))
376
377
378 def get_key_for_dup_detect(args, flow):
379     result = '{}:{}:{}'.format(flow.get('dpnid'), flow.get('table'), get_matchstr(args, flow))
380     return result
381
382
383 def show_dup_flows(args):
384     config.get_models(args, {
385         "elan_elan_instances",
386         "elan_elan_interfaces",
387         "ietf_interfaces_interfaces",
388         "ietf_interfaces_interfaces_state",
389         "interface_service_bindings_service_bindings",
390         "l3vpn_vpn_interfaces",
391         # "mip_mac",
392         "odl_fib_fib_entries",
393         # "odl_interface_meta_if_index_interface_map",
394         "odl_l3vpn_vpn_instance_to_vpn_id",
395         "odl_inventory_nodes_config"})
396     mmac = {}  # config.gmodels.mip_mac.get_entries_by_key()
397     einsts = config.gmodels.elan_elan_instances.get_elan_instances_by_key()
398     compute_map = config.gmodels.odl_inventory_nodes_config.get_dpn_host_mapping()
399
400     flows = utils.sort(get_all_flows(['elan']), 'table')
401     matches = collections.defaultdict(list)
402     for flow in flows:
403         dup_key = get_key_for_dup_detect(args, flow)
404         if dup_key:
405             if matches and matches.get(dup_key):
406                 matches[dup_key].append(flow)
407             else:
408                 matches[dup_key].append(flow)
409     for k, v in matches.iteritems():
410         if len(v) > 1:
411             dpnid = k.split(':')[0]
412             host = compute_map.get(dpnid, dpnid)
413             result = 'Host:{}, FlowCount:{}, MatchKey:{}, ElanTag:{}'.format(host, len(v), k, v[0].get('elan-tag'))
414             print result
415             for idx, flow in enumerate(v):
416                 result = "Duplicate"
417                 mac_addr = flow.get('dst-mac')
418                 if mac_addr and mmac.get(mac_addr):
419                     result = is_correct_elan_flow(flow, mmac.get(mac_addr), einsts, host)
420                 print '    {} Flow-{}:{}'.format(result, idx,
421                                                  utils.format_json(args, flow_parser.parse_flow(flow.get('flow'))))
422
423
424 def show_learned_mac_flows(args):
425     config.get_models(args, {
426         "elan_elan_instances",
427         "elan_elan_interfaces",
428         "ietf_interfaces_interfaces",
429         "ietf_interfaces_interfaces_state",
430         "interface_service_bindings_service_bindings",
431         "l3vpn_vpn_interfaces",
432         # "mip_mac",
433         "neutron_neutron",
434         "odl_fib_fib_entries",
435         # "odl_interface_meta_if_index_interface_map",
436         "odl_l3vpn_vpn_instance_to_vpn_id",
437         "odl_inventory_nodes_config"})
438     nports = config.gmodels.neutron_neutron.get_ports_by_key(key='mac-address')
439     compute_map = config.gmodels.odl_inventory_nodes_config.get_dpn_host_mapping()
440
441     flows = utils.sort(get_all_flows(['elan']), 'table')
442     for flow_info in flows:
443         flow = flow_info.get('flow')
444         dpnid = flow_info.get('dpnid')
445         host = compute_map.get(dpnid, dpnid)
446         if ((flow_info.get('table') == 50 and
447                      flow.get('idle-timeout') == 300 and not
448         nports.get(flow_info.get('src-mac'))) or
449                 (flow_info.get('table') == 51 and
450                      not nports.get(flow_info.get('dst-mac')))):
451             result = 'Table:{}, Host:{}, FlowId:{}{}'.format(
452                 flow_info.get('table'), host, flow.get('id'),
453                 utils.show_optionals(flow_info))
454             print result
455             print 'Flow:{}'.format(utils.format_json(args, flow_parser.parse_flow(flow)))
456
457
458 def get_stale_bindings(args):
459     # ietf_interfaces_interfaces = ietf_interfaces.interfaces(Model.CONFIG, args)
460     # interface_service_bindings_service_bindings = interface_service_bindings.service_bindings(Model.CONFIG, args)
461     ifaces = config.gmodels.ietf_interfaces_interfaces.get_interfaces_by_key()
462     bindings, orphans = config.gmodels.interface_service_bindings_service_bindings.get_service_bindings()
463     return set(bindings.keys()) - set(ifaces.keys()), bindings
464
465
466 def dump_flows(args, modules=None, sort_by='table', filter_by=None):
467     modules = modules if modules else ['ifm']
468     filter_by = filter_by if filter_by else []
469     # odl_inventory_nodes_config = opendaylight_inventory.nodes(Model.CONFIG, args)
470     compute_map = config.gmodels.odl_inventory_nodes_config.get_dpn_host_mapping()
471     # neutron_neutron = neutron.neutron(Model.CONFIG, args)
472     nports = config.gmodels.neutron_neutron.get_ports_by_key()
473     logger.info("dump_flows: %s", args)
474     for flow in utils.sort(get_all_flows(modules, filter_by), sort_by):
475         host = compute_map.get(flow.get('dpnid'), flow.get('dpnid'))
476         ip_list = get_ips_for_iface(nports, flow.get('ifname'))
477         if ip_list:
478             flow['iface-ips'] = ip_list
479         result = 'Table:{}, Host:{}, FlowId:{}{}'.format(
480             flow['table'], host, flow['id'],
481             utils.show_optionals(flow))
482         print result
483         print 'Flow:', utils.format_json(args, flow_parser.parse_flow(flow['flow']))
484
485
486 def show_all_flows(args):
487     config.get_models(args, {
488         "elan_elan_instances",
489         "elan_elan_interfaces",
490         "ietf_interfaces_interfaces",
491         "ietf_interfaces_interfaces_state",
492         "interface_service_bindings_service_bindings",
493         "l3vpn_vpn_interfaces",
494         "neutron_neutron",
495         "odl_fib_fib_entries",
496         # "odl_interface_meta_if_index_interface_map",
497         "odl_l3vpn_vpn_instance_to_vpn_id",
498         "odl_inventory_nodes_config"})
499     dump_flows(args, modules=['all'])
500
501
502 def get_ips_for_iface(nports, ifname):
503     ips = []
504     port = nports.get(ifname) if ifname else None
505     fixed_ips = port.get('fixed-ips', []) if port else []
506     for fixed_ip in fixed_ips:
507         ips.append(fixed_ip['ip-address'])
508     return ips
509
510
511 def stale_ifm_flow(flow, flow_info, ifaces, ifstates):
512     flow_parser.get_flow_info_from_ifm_table(flow_info, flow)
513     flow_ifname = flow_info['ifname']
514     iface = ifaces.get(flow_ifname)
515     if flow_ifname is not None and not iface:
516         flow_info['reason'] = 'Interface doesnt exist'
517         return create_flow_dict(flow_info, flow)
518     elif flow_ifname and ifstates.get(flow_ifname):
519         ifstate = ifstates.get(flow_ifname)
520         ncid_list = ifstate.get('lower-layer-if')
521         ncid = ncid_list[0] if ncid_list else None
522         dpn = Model.get_dpn_from_ofnodeid(ncid)
523         if dpn and dpn != flow_info['dpnid']:
524             flow_info['reason'] = 'DpnId mismatch for flow and Interface'
525             return create_flow_dict(flow_info, flow)
526         if (flow_info.get('lport') and ifstate.get('if-index')
527             and flow_info['lport'] != ifstate['if-index']):
528             flow_info['reason'] = 'Lport and IfIndex mismatch'
529             return create_flow_dict(flow_info, flow)
530         if (flow_info.get('ofport') and ifstate.get('lower-layer-if')
531             and flow_info['ofport'] != Model.get_ofport_from_ncid(ifstate.get('lower-layer-if')[0])):
532             flow_info['reason'] = 'OfPort mismatch'
533         if (flow_info.get('vlanid') and iface.get('odl-interface:vlan-id')
534             and flow_info['vlanid'] != iface.get('odl-interface:vlan-id')):
535             flow_info['reason'] = 'VlanId mismatch'
536     return None
537     # return create_flow_dict(flow_info, flow)