Move files around to fit pypi better
[netvirt.git] / resources / tools / odltools / odltools / netvirt / ds_analyze.py
1 import json
2 from collections import defaultdict
3
4 import constants as const
5 import flow_parser as fp
6 import netvirt_utils as utils
7 from odltools.mdsal.model import Model
8 from odltools.mdsal.models import opendaylight_inventory, network_topology, itm_state, elan, id_manager, \
9     ietf_interfaces, interface_service_bindings, l3vpn, mip, neutron, odl_fib, odl_interface_meta, odl_l3vpn
10
11 # Required
12 ifaces = None
13 ifstates = None
14
15 #Optional
16 ports = {}
17 tunnels = {}
18 confNodes = {}
19 operNodes = {}
20
21
22 modelpath = "/tmp/robotjob/s1-t1_Create_VLAN_Network_net_1/models"
23 elan_elan_instances = None
24 elan_elan_interfaces = None
25 id_manager_id_pools = None
26 ietf_interfaces_interfaces = None
27 ietf_interfaces_interfaces_state = None
28 interface_service_bindings_service_bindings = None
29 itm_state_tunnels_state = None
30 l3vpn_vpn_interfaces = None
31 mip_mac = None
32 network_topology_network_topology_config = None
33 network_topology_network_topology_operational = None
34 neutron_neutron = None
35 odl_fib_fib_entries = None
36 odl_interface_meta_if_index_interface_map = None
37 odl_inventory_nodes_config = None
38 odl_inventory_nodes_operational = None
39 odl_l3vpn_vpn_instance_to_vpn_id = None
40
41
42 def by_ifname(ifname):
43     ifstate = ifstates.get(ifname)
44     iface = ifaces.get(ifname)
45     port = None
46     tunnel = None
47     tunState = None
48     if iface and iface.get('type') == const.IFTYPE_VLAN:
49         ports = neutron_neutron.get_ports_by_key()
50         port = ports.get(ifname)
51     elif iface and iface.get('type') == const.IFTYPE_TUNNEL:
52         tunnels = itm_state_tunnels_state.get_tunnels_by_key()
53         tunnel = tunnels.get(ifname)
54         tunStates = dsg.get_tunnel_states()
55         tunState = tunStates.get(ifname)
56     else:
57         print "UNSUPPORTED IfType"
58     return iface,ifstate,port,tunnel,tunState
59
60
61 def print_keys():
62     print "InterfaceNames: ", ifaces.keys()
63     print
64     print "IfStateNames: ", ifstates.keys()
65
66
67 def analyze_interface(ifname=None):
68     global ifaces, ifstates
69     if not ifname:
70         print_keys()
71         exit(1)
72     ifname = ifname[0]
73     ifaces = ietf_interfaces_interfaces.get_interfaces_by_key()
74     ifstates = ietf_interfaces_interfaces_state.get_interfaces_by_key()
75     iface,ifstate,port,tunnel,tunState = by_ifname(ifname)
76     print "InterfaceConfig: "
77     json.dumps(iface, indent=2)
78     print "InterfaceState: "
79     json.dumps(ifstate, indent=2)
80     if port:
81         print "NeutronPort: "
82         json.dumps(port, indent=2)
83         analyze_neutron_port(port, iface, ifstate)
84         return
85     if tunnel:
86         print "Tunnel: "
87         json.dumps(tunnel, indent=2)
88     if tunState:
89         print "TunState: "
90         json.dumps(tunState, indent=2)
91     if ifstate:
92         ncId = ifstate.get('lower-layer-if')[0]
93         nodeId = ncId[:ncId.rindex(':')]
94         analyze_inventory(nodeId, True, ncId, ifname)
95         #analyze_inventory(nodeId, False, ncId, ifname)
96
97 def analyze_neutron_port(port, iface, ifstate):
98     for flow in utils.sort(get_all_flows(['all']), 'table'):
99         if ((flow.get('ifname') == port['uuid']) or
100             (flow.get('lport') and ifstate and flow['lport'] == ifstate.get('if-index')) or
101             (iface['name'] == flow.get('ifname'))):
102                 result = 'Table:{},FlowId:{}{}'.format(
103                 flow['table'], flow['id'],
104                 utils.show_optionals(flow))
105                 print result
106                 print 'Flow:', json.dumps(parse_flow(flow.get('flow')))
107
108
109 def analyze_inventory(nodeId, isConfig=True, ncId=None, ifName=None):
110     if isConfig:
111         nodes = odl_inventory_nodes_config.get_nodes_by_key()
112         print "Inventory Config:"
113     else:
114         print "Inventory Operational:"
115         nodes = odl_inventory_nodes_operational.get_nodes_by_key()
116     node = nodes.get(nodeId)
117     tables = node.get(const.NODE_TABLE)
118     groups = node.get(const.NODE_GROUP)
119     flow_list = []
120     print "Flows:"
121     for table in tables:
122         for flow in table.get('flow', []):
123             if not ifName or ifName in utils.nstr(flow.get('flow-name')):
124                 flow_dict = {}
125                 flow_dict['table'] = table['id']
126                 flow_dict['id'] = flow['id']
127                 flow_dict['name'] = flow.get('flow-name')
128                 flow_dict['flow'] = flow
129                 flow_list.append(flow_dict)
130     flows = sorted(flow_list, key=lambda x: x['table'])
131     for flow in flows:
132         print 'Table:', flow['table']
133         print 'FlowId:', flow['id'], 'FlowName:', flow.get('name')
134
135
136 def get_dpn_host_mapping(oper_nodes=None):
137         nodes_dict = {}
138         nodes = oper_nodes or odl_inventory_nodes_operational.get_nodes_by_key()
139         for node in nodes.itervalues():
140             dpnid = utils.get_dpn_from_ofnodeid(node['id'])
141             nodes_dict[dpnid] = node.get('flow-node-inventory:description', '')
142         return nodes_dict
143
144
145 def get_groups(ofnodes=None):
146     of_nodes = ofnodes or odl_inventory_nodes_config.get_nodes_by_key()
147     key ='group-id'
148     group_dict = defaultdict(dict)
149     for node in of_nodes.itervalues():
150         dpnid = utils.get_dpn_from_ofnodeid(node['id'])
151         for group in node.get(const.NODE_GROUP, []):
152             if group_dict.get(dpnid) and group_dict.get(dpnid).get(group[key]):
153                 print 'Duplicate:', dpnid, group[key]
154             group_dict[dpnid][group[key]] = group
155     return dict(group_dict)
156
157
158 def get_stale_flows(modules=['ifm']):
159     if not modules:
160         return 'No modules specified'
161     ifaces = {}
162     ifstates = {}
163     ifindexes = {}
164     bindings = {}
165     einsts = {}
166     eifaces = {}
167     fibentries = {}
168     vpnids = {}
169     vpninterfaces = {}
170     groups = {}
171     table_list = list(set([table for module in modules for table in const.TABLE_MAP[module]]))
172     ##table_list = [214, 244]
173     of_nodes = odl_inventory_nodes_config.get_nodes_by_key()
174     if 'ifm' in modules:
175         ifaces = ietf_interfaces_interfaces.get_interfaces_by_key()
176         ifstates = ietf_interfaces_interfaces_state.get_interfaces_by_key()
177     if 'l3vpn' in modules:
178         ifaces = ifaces or ietf_interfaces_interfaces.get_interfaces_by_key()
179         ifindexes = ifindexes or odl_interface_meta_if_index_interface_map.get_if_index_interfaces_by_key()
180         fibentries = fibentries or odl_fib_fib_entries.get_vrf_entries_by_key()
181         vpnids = vpnids or odl_l3vpn_vpn_instance_to_vpn_id.get_vpn_ids_by_key()
182         vpninterfaces = vpninterfaces or l3vpn_vpn_interfaces.get_vpn_ids_by_key()
183         groups = groups or get_groups(of_nodes)
184     if 'acl' in modules:
185         ifaces = ifaces or ietf_interfaces_interfaces.get_interfaces_by_key()
186         ifindexes = ifindexes or odl_interface_meta_if_index_interface_map.get_if_index_interfaces_by_key()
187         einsts = einsts or elan_elan_instances.get_elan_instances_by_key()
188         eifaces = eifaces or elan_elan_interfaces.get_elan_interfaces()
189     if 'elan' in modules:
190         ifaces = ifaces or ietf_interfaces_interfaces.get_interfaces_by_key()
191         einsts = einsts or elan_elan_instances.get_elan_instances_by_key()
192         eifaces = eifaces or elan_elan_interfaces.get_elan_interfaces()
193         ifindexes = ifindexes or odl_interface_meta_if_index_interface_map.get_if_index_interfaces_by_key()
194     stale_flows = []
195     for node in of_nodes.itervalues():
196         tables = [x for x in node[const.NODE_TABLE] if x['id'] in table_list]
197         for table in tables:
198             for flow in table.get('flow', []):
199                 flow_dict = None
200                 flow_info = {}
201                 flow_info['dpnid'] = utils.get_dpn_from_ofnodeid(node['id'])
202                 if 'ifm' in modules and table['id'] in const.TABLE_MAP['ifm']:
203                     flow_dict = fp.stale_ifm_flow(flow, flow_info, ifaces, ifstates)
204                 if 'l3vpn' in modules and table['id'] in const.TABLE_MAP['l3vpn']:
205                     flow_dict = fp.stale_l3vpn_flow(flow, flow_info, groups, ifaces, ifindexes, vpnids, vpninterfaces, fibentries)
206                 if 'elan' in modules and table['id'] in const.TABLE_MAP['elan']:
207                     flow_dict = fp.stale_elan_flow(flow, flow_info, ifaces, ifindexes, einsts, eifaces)
208                 if 'acl' in modules and table['id'] in const.TABLE_MAP['acl']:
209                     flow_dict = fp.stale_acl_flow(flow, flow_info, ifaces, ifindexes, einsts, eifaces)
210                 if flow_dict is not None:
211                     stale_flows.append(flow_dict)
212
213     return stale_flows
214
215
216 def show_stale_bindings():
217     stale_ids, bindings = get_stale_bindings()
218     for iface_id in sorted(stale_ids):
219         for binding in bindings[iface_id].itervalues():
220             #if binding.get('bound-services'):
221             path = get_data_path('bindings', binding)
222             print json.dumps(bindings[iface_id])
223             print('http://192.168.2.32:8383/restconf/config/{}'.format(path))
224
225
226 def get_stale_bindings():
227     ifaces = ietf_interfaces_interfaces.get_interfaces_by_key()
228     bindings, orphans = interface_service_bindings_service_bindings.get_service_bindings()
229     return set(bindings.keys()) - set(ifaces.keys()), bindings
230
231
232 def get_ips_for_iface(nports, ifname):
233     ips = []
234     port = nports.get(ifname) if ifname else None
235     fixed_ips = port.get('fixed-ips', []) if port else []
236     for fixed_ip in fixed_ips:
237         ips.append(fixed_ip['ip-address'])
238     return ips
239
240
241 def show_link_flow_binding():
242     stale_ids, bindings = get_stale_bindings()
243     flows = get_stale_flows()
244     print len(stale_ids), len(flows)
245     for flow in flows:
246         if flow['ifname'] in stale_ids and 'bound-services' in bindings[flow['ifname']]:
247             print 'Flow with binding: ', flow['ifname']
248         else:
249             print 'Flow without binding: ', flow['ifname']
250
251
252 def show_stale_flows(sort_by='table'):
253     compute_map = get_dpn_host_mapping()
254     nports = neutron_neutron.get_ports_by_key()
255     for flow in utils.sort(get_stale_flows(['ifm', 'acl', 'elan', 'l3vpn']), sort_by):
256         host = compute_map.get(flow.get('dpnid'), flow.get('dpnid'))
257         ip_list = get_ips_for_iface(nports, flow.get('ifname'))
258         if ip_list:
259             flow['iface-ips'] = ip_list
260         result = 'Table:{},Host:{},FlowId:{}{}'.format(
261             flow['table'], host, flow['id'],
262             utils.show_optionals(flow))
263         print result
264         ##path = get_data_path('flows', flow)
265         #print('http://192.168.2.32:8383/restconf/config/{}'.format(path))
266         #print 'Flow:', json.dumps(parse_flow(flow['flow']))
267
268
269 def get_all_flows(modules=['ifm'], filter=[]):
270     if not modules:
271         return 'No modules specified'
272     ifaces = {}
273     ifstates = {}
274     ifindexes = {}
275     bindings = {}
276     einsts = {}
277     eifaces = {}
278     fibentries = {}
279     vpnids = {}
280     vpninterfaces = {}
281     groups = {}
282     if 'all' in modules:
283         table_list = list(range(0, 255))
284     else:
285         table_list = list(set([table for module in modules for table in const.TABLE_MAP[module]]))
286     ##table_list = [214, 244]
287     of_nodes = odl_inventory_nodes_config.get_nodes_by_key()
288     if 'ifm' in modules:
289         ifaces = ietf_interfaces_interfaces.get_interfaces_by_key()
290         ifstates = ietf_interfaces_interfaces_state.get_interfaces_by_key()
291     if 'l3vpn' in modules:
292         ifaces = ifaces or ietf_interfaces_interfaces.get_interfaces_by_key()
293         ifindexes = ifindexes or odl_interface_meta_if_index_interface_map.get_if_index_interfaces_by_key()
294         fibentries = fibentries or dsg.get_fibentries_by_label()
295         vpnids = vpnids or odl_l3vpn_vpn_instance_to_vpn_id.get_vpn_ids_by_key()
296         vpninterfaces = vpninterfaces or l3vpn_vpn_interfaces.get_vpn_ids_by_key()
297         groups = groups or get_groups(of_nodes)
298     if 'acl' in modules:
299         ifaces = ifaces or ietf_interfaces_interfaces.get_interfaces_by_key()
300         ifindexes = ifindexes or odl_interface_meta_if_index_interface_map.get_if_index_interfaces_by_key()
301         einsts = einsts or elan_elan_instances.get_elan_instances_by_key()
302         eifaces = eifaces or elan_elan_interfaces.get_elan_interfaces()
303     if 'elan' in modules:
304         ifaces = ifaces or ietf_interfaces_interfaces.get_interfaces_by_key()
305         einsts = einsts or elan_elan_instances.get_elan_instances_by_key()
306         eifaces = eifaces or elan_elan_interfaces.get_elan_interfaces()
307         ifindexes = ifindexes or odl_interface_meta_if_index_interface_map.get_if_index_interfaces_by_key()
308     if 'all' in modules:
309         groups = groups or get_groups(of_nodes)
310         ifaces = ifaces or ietf_interfaces_interfaces.get_interfaces_by_key()
311         ifstates = ifstates or ietf_interfaces_interfaces_state.get_interfaces_by_key()
312         ifindexes = ifindexes or odl_interface_meta_if_index_interface_map.get_if_index_interfaces_by_key()
313         fibentries = fibentries or dsg.get_fibentries_by_label()
314         vpnids = vpnids or odl_l3vpn_vpn_instance_to_vpn_id.get_vpn_ids_by_key()
315         vpninterfaces = vpninterfaces or l3vpn_vpn_interfaces.get_vpn_ids_by_key()
316         einsts = einsts or elan_elan_instances.get_elan_instances_by_key()
317         eifaces = eifaces or elan_elan_interfaces.get_elan_interfaces()
318     flows = []
319     for node in of_nodes.itervalues():
320         tables = [x for x in node[const.NODE_TABLE] if x['id'] in table_list]
321         for table in tables:
322             for flow in table.get('flow', []):
323                 flow_dict = None
324                 flow_info = {}
325                 flow_info['dpnid'] = utils.get_dpn_from_ofnodeid(node['id'])
326                 flow_dict = fp.get_any_flow(flow, flow_info, groups,
327                                         ifaces, ifstates, ifindexes,
328                                         fibentries, vpnids, vpninterfaces,
329                                         einsts, eifaces)
330                 if (flow_dict is not None and
331                         utils.filter_flow(flow_dict, filter)):
332                     flows.append(flow_dict)
333     return flows
334
335
336 def show_flows(modules=['ifm'], sort_by='table', filter_by=[]):
337     compute_map = get_dpn_host_mapping()
338     nports = neutron_neutron.get_ports_by_key()
339     for flow in utils.sort(get_all_flows(modules, filter_by), sort_by):
340         host = compute_map.get(flow.get('dpnid'), flow.get('dpnid'))
341         ip_list = get_ips_for_iface(nports, flow.get('ifname'))
342         if ip_list:
343             flow['iface-ips'] = ip_list
344         result = 'Table:{},Host:{},FlowId:{}{}'.format(
345             flow['table'], host, flow['id'],
346             utils.show_optionals(flow))
347         print result
348         print 'Flow:', json.dumps(parse_flow(flow['flow']))
349
350
351 def show_all_flows():
352     show_flows(modules=['all'])
353
354
355 def show_elan_flows():
356     compute_map = get_dpn_host_mapping()
357     for flow in utils.sort(get_all_flows(['elan']), 'id'):
358         host = compute_map.get(flow.get('dpnid'), flow.get('dpnid'))
359         result = 'MacHost:{}{},Table:{},FlowId:{},{},Flow:{}'.format(flow['id'][-17:],host,flow['table'],flow['id'],utils.show_optionals(flow),json.dumps(parse_flow(flow['flow'])))
360         print result
361         #print 'Flow:', json.dumps(parse_flow(flow['flow']))
362
363
364 def get_matchstr(flow):
365     if flow and flow.get('flow') and flow.get('flow').get('match'):
366         return json.dumps(flow.get('flow').get('match', None))
367
368
369 def get_key_for_dup_detect(flow):
370     result = '{}:{}:{}'.format(flow.get('dpnid'), flow.get('table'), get_matchstr(flow))
371     return result
372
373
374 def show_dup_flows():
375     mmac = mip_mac.get_entries_by_key()
376     einsts = elan_elan_instances.get_elan_instances_by_key()
377     flows = utils.sort(get_all_flows(['elan']), 'table')
378     matches = defaultdict(list)
379     compute_map = get_dpn_host_mapping()
380     for flow in flows:
381         dup_key = get_key_for_dup_detect(flow)
382         if dup_key:
383             if matches and matches.get(dup_key):
384                 matches[dup_key].append(flow)
385             else:
386                 matches[dup_key].append(flow)
387     for k, v in matches.iteritems():
388         if len(v) > 1:
389             dpnid = k.split(':')[0]
390             host = compute_map.get(dpnid, dpnid)
391             result = 'Host:{},FlowCount:{},MatchKey:{},ElanTag:{}'.format(host, len(v), k,v[0].get('elan-tag'))
392             print result
393             for idx, flow in enumerate(v):
394                 result = "Duplicate"
395                 mac_addr = flow.get('dst-mac')
396                 if mac_addr and mmac.get(mac_addr):
397                     result = fp.is_correct_elan_flow(flow, mmac.get(mac_addr), einsts, host)
398                 print '    {}Flow-{}:{}'.format(result, idx, json.dumps(parse_flow(flow.get('flow'))))
399
400
401 def show_learned_mac_flows():
402     nports = neutron_neutron.get_ports_by_key(key_field='mac-address')
403     flows = utils.sort(get_all_flows(['elan']), 'table')
404     compute_map = get_dpn_host_mapping()
405     for flow_info in flows:
406         flow = flow_info.get('flow')
407         dpnid = flow_info.get('dpnid')
408         host = compute_map.get(dpnid, dpnid)
409         if ((flow_info.get('table') == 50 and
410                 flow.get('idle-timeout') == 300 and not
411                 nports.get(flow_info.get('src-mac'))) or
412                 (flow_info.get('table') == 51 and
413                  not nports.get(flow_info.get('dst-mac')))):
414             result = 'Table:{},Host:{},FlowId:{}{}'.format(
415                 flow_info.get('table'), host, flow.get('id'),
416                 utils.show_optionals(flow_info))
417             print result
418             print 'Flow:{}'.format(json.dumps(parse_flow(flow)))
419
420
421 def show_elan_instances():
422     insts = elan_elan_instances.get_elan_instances_by_key()
423     json.dumps(insts)
424
425
426 def get_duplicate_ids():
427     duplicate_ids= {}
428     for pool in id_manager_id_pools.get_id_pools_by_key().itervalues():
429         id_values = {}
430         for id_entry in pool.get('id-entries', []):
431             id_info = {}
432             id_value = id_entry.get('id-value')[0]
433             id_key = id_entry.get('id-key')
434             if id_values and id_values.get(id_value, None):
435                 key_list = id_values.get(id_value)
436                 key_list.append(id_key)
437                 id_info['id-value'] = id_value
438                 id_info['id-keys'] = key_list
439                 id_info['pool-name'] = pool.get('pool-name')
440                 id_info['parent-pool-name'] = pool.get('parent-pool-name')
441                 duplicate_ids[id_value] = id_info
442             else:
443                 id_values[id_value] = [id_key]
444     return duplicate_ids
445
446
447
448 def show_idpools():
449     ports = neutron_neutron.get_ports_by_key()
450     iface_ids = []
451     for k,v in get_duplicate_ids().iteritems():
452         result = "Id:{},Keys:{}".format(k, json.dumps(v.get('id-keys')))
453         if v.get('pool-name'):
454             result = "{},Pool:{}".format(result, v.get('pool-name'))
455             if v.get('pool-name') == 'interfaces':
456                 iface_ids.extend(v.get('id-keys'))
457         if v.get('parent-pool-name'):
458             result = "{},ParentPool:{}".format(result, v.get('parent-pool-name'))
459         print result
460     print "\nNeutron Ports"
461     print "============="
462     for id in iface_ids:
463         port = ports.get(id, {})
464         print "Iface={}, NeutronPort={}".format(id, json.dumps(port))
465
466
467 def parse_flow(flow):
468     #parse flow fields
469     #hex(int(mask, 16) & int(data, 16))
470     if flow['cookie']:
471         utils.to_hex(flow, 'cookie')
472     # parse instructions
473     for instruction in flow['instructions'].get('instruction', []):
474         if 'write-metadata' in instruction:
475             utils.to_hex(instruction['write-metadata'],'metadata')
476             utils.to_hex(instruction['write-metadata'],'metadata-mask')
477         if 'apply-actions' in instruction:
478             for action in instruction['apply-actions'].get('action', []):
479                 if 'openflowplugin-extension-nicira-action:nx-reg-load' in action:
480                     utils.to_hex(action['openflowplugin-extension-nicira-action:nx-reg-load'], 'value')
481     # parse matches
482     if 'metadata' in flow['match']:
483         metadata = flow['match']['metadata']
484         utils.to_hex(metadata,'metadata')
485         utils.to_hex(metadata,'metadata-mask')
486
487     for ofex in flow['match'].get('openflowplugin-extension-general:extension-list', []):
488         if ofex['extension-key'] == 'openflowplugin-extension-nicira-match:nxm-nx-reg6-key':
489             utils.to_hex(ofex['extension']['openflowplugin-extension-nicira-match:nxm-nx-reg'], 'value')
490
491     return flow
492
493
494 def get_data_path(res_type, data):
495     if res_type == 'bindings':
496         return 'interface-service-bindings:service-bindings/services-info/{}/{}'.format(data['interface-name'],data['service-mode'])
497     elif res_type == 'flows':
498         return 'opendaylight-inventory:nodes/node/openflow:{}/flow-node-inventory:table/{}/flow/{}'.format(data['dpnid'],data['table'],data['id'])
499
500
501 # Sample method that shows how to use
502 def show_all_tables():
503     of_nodes = odl_inventory_nodes_config.get_nodes_by_key()
504     tables = set()
505     for node in of_nodes.itervalues():
506         for table in node[const.NODE_TABLE]:
507             if table.get('flow'):
508                 tables.add(table['id'])
509     print list(tables)
510
511
512 def show_all_groups():
513     of_nodes = odl_inventory_nodes_config.get_nodes_by_key()
514     groups = get_groups(of_nodes)
515     for dpn in groups:
516         for group_key in groups[dpn]:
517             print 'Dpn:', dpn, 'ID:', group_key, 'Group:', json.dumps(groups[dpn][group_key])
518
519
520 def analyze_trunks():
521     nports = neutron_neutron.get_ports_by_key()
522     ntrunks = neutron_neutron.get_trunks_by_key()
523     vpninterfaces = l3vpn_vpn_interfaces.get_vpn_ids_by_key()
524     ifaces = ietf_interfaces_interfaces.get_interfaces_by_key()
525     ifstates = ietf_interfaces_interfaces_state.get_interfaces_by_key()
526     subport_dict = {}
527     for v in ntrunks.itervalues():
528         nport = nports.get(v.get('port-id'))
529         s_subports = []
530         for subport in v.get('sub-ports'):
531             sport_id = subport.get('port-id')
532             snport = nports.get(sport_id)
533             svpniface = vpninterfaces.get(sport_id)
534             siface = ifaces.get(sport_id)
535             sifstate = ifstates.get(sport_id)
536             subport['SubNeutronPort'] = 'Correct' if snport else 'Wrong'
537             subport['SubVpnInterface'] = 'Correct' if svpniface else 'Wrong'
538             subport['ofport'] = utils.get_ofport_from_ncid(sifstate.get('lower-layer-if')[0]) 
539             if siface:
540                 vlan_mode = siface.get('odl-interface:l2vlan-mode')
541                 parent_iface_id = siface.get('odl-interface:parent-interface')
542                 if vlan_mode !='trunk-member':
543                     subport['SubIface'] = 'WrongMode'
544                 elif parent_iface_id !=v.get('port-id'):
545                     subport['SubIface'] = 'WrongParent'
546                 elif siface.get('odl-interface:vlan-id') !=subport.get('segmentation-id'):
547                     subport['SubIface'] = 'WrongVlanId'
548                 else:
549                     subport['SubIface'] = 'Correct'
550             else:
551                 subport['SubIface'] = 'Wrong'
552             s_subport = 'SegId:{},PortId:{},SubNeutronPort:{},SubIface:{},SubVpnIface:{}'.format(
553                     subport.get('segmentation-id'),subport.get('port-id'),
554                     subport.get('SubNeutronPort'),
555                     subport.get('SubIface'),
556                     subport.get('SubVpnInterface'))
557             s_subports.append(subport)
558             subport_dict[subport['port-id']] = subport
559         s_trunk = 'TrunkName:{},TrunkId:{},PortId:{},NeutronPort:{},SubPorts:{}'.format(
560                 v.get('name'), v.get('uuid'), v.get('port-id'),
561                 'Correct' if nport else 'Wrong', json.dumps(s_subports))
562         print s_trunk
563     print '\n------------------------------------'
564     print   'Analyzing Flow status for SubPorts'
565     print   '------------------------------------'
566     for flow in utils.sort(get_all_flows(['ifm'], ['vlanid']), 'ifname'):
567         subport = subport_dict.get(flow.get('ifname')) or None
568         vlanid = subport.get('segmentation-id') if subport else None
569         ofport = subport.get('ofport') if subport else None
570         flow_status = 'Okay'
571         if flow.get('ofport') and flow.get('ofport') != ofport:
572             flow_status = 'OfPort mismatch for SubPort:{} and Flow:{}'.format(subport, flow.get('flow'))
573         if flow.get('vlanid') and flow.get('vlanid') != vlanid:
574             flow_status = 'VlanId mismatch for SubPort:{} and Flow:{}'.format(subport, flow.get('flow'))
575         if subport:
576             print 'SubPort:{},Table:{},FlowStatus:{}'.format(
577                     subport.get('port-id'), flow.get('table'), flow_status)
578
579
580 def main(args=None):
581     global elan_elan_instances
582     global elan_elan_interfaces
583     global id_manager_id_pools
584     global ietf_interfaces_interfaces
585     global ietf_interfaces_interfaces_state
586     global interface_service_bindings_service_bindings
587     global itm_state_tunnels_state
588     global l3vpn_vpn_interfaces
589     global mip_mac
590     global network_topology_network_topology_config
591     global network_topology_network_topology_operational
592     global neutron_neutron
593     global odl_fib_fib_entries
594     global odl_interface_meta_if_index_interface_map
595     global odl_inventory_nodes_config
596     global odl_inventory_nodes_operational
597     global odl_l3vpn_vpn_instance_to_vpn_id
598     options, args = utils.parse_args()
599
600     elan_elan_instances = elan.elan_instances(Model.CONFIG, modelpath)
601     elan_elan_interfaces = elan.elan_interfaces(Model.CONFIG, modelpath)
602     id_manager_id_pools = id_manager.id_pools(Model.CONFIG, modelpath)
603     ietf_interfaces_interfaces = ietf_interfaces.interfaces(Model.CONFIG, modelpath)
604     ietf_interfaces_interfaces_state = ietf_interfaces.interfaces_state(Model.OPERATIONAL, modelpath)
605     interface_service_bindings_service_bindings = interface_service_bindings.service_bindings(Model.CONFIG, modelpath)
606     itm_state_tunnels_state = itm_state.tunnels_state(Model.OPERATIONAL, modelpath)
607     l3vpn_vpn_interfaces = l3vpn.vpn_instance_to_vpn_id(Model.CONFIG, modelpath)
608     mip_mac = mip.mac(Model.CONFIG, modelpath)
609     neutron_neutron = neutron.neutron(Model.CONFIG, modelpath)
610     network_topology_network_topology_config = network_topology.network_topology(Model.CONFIG, modelpath)
611     network_topology_network_topology_operational = network_topology.network_topology(Model.CONFIG, modelpath)
612     neutron_neutron = neutron.neutron(Model.CONFIG, modelpath)
613     odl_fib_fib_entries = odl_fib.fib_entries(Model.CONFIG, modelpath)
614     odl_interface_meta_if_index_interface_map = odl_interface_meta.if_indexes_interface_map(Model.OPERATIONAL, modelpath)
615     odl_inventory_nodes_config = opendaylight_inventory.nodes(Model.CONFIG, modelpath)
616     odl_inventory_nodes_operational = opendaylight_inventory.nodes(Model.OPERATIONAL, modelpath)
617     odl_l3vpn_vpn_instance_to_vpn_id = odl_l3vpn.vpn_instance_to_vpn_id(Model.CONFIG, modelpath)
618
619     if options.callMethod:
620         if args[1:]:
621             eval(options.callMethod)(args[1:])
622             return
623         else:
624             eval(options.callMethod)()
625             return
626     #print json.dumps(l3vpn_vpn_interfaces.get_vpn_ids_by_key())
627     #show_all_tables()
628     #analyze_inventory('openflow:165862438671169',ifName='tunf94333cc491')
629     #show_stale_flows()
630     #show_stale_bindings()
631     analyze_interface([args[1]])
632     #og.print_flow_dict(og.get_ofctl_flows())
633
634
635 if __name__ == '__main__':
636     main()