ODLTools: Misc CLI improvements
[netvirt.git] / resources / tools / odltools / odltools / netvirt / analyze.py
1 import config
2 import flow_parser
3 import flows
4 import tables
5
6 from odltools.mdsal.models import constants
7 from odltools.mdsal.models import ietf_interfaces
8 from odltools.mdsal.models import itm_state
9 from odltools.mdsal.models import l3vpn
10 from odltools.mdsal.models import neutron
11 from odltools.mdsal.models.opendaylight_inventory import Nodes
12 from odltools.mdsal.models.model import Model
13 from odltools.netvirt import utils
14
15
16 def print_keys(ifaces, ifstates):
17     print "InterfaceNames: {}\n".format(ifaces.keys())
18     print
19     print "IfStateNames: {}".format(ifstates.keys())
20
21
22 def by_ifname(args, ifname, ifstates, ifaces):
23     itm_state_tunnels_state = itm_state.tunnels_state(Model.OPERATIONAL, args)
24     # itm_state_tunnels_list = itm_state.tunnels_list(Model.CONFIG, args)
25     neutron_neutron = neutron.neutron(Model.CONFIG, args)
26     ifstate = ifstates.get(ifname)
27     iface = ifaces.get(ifname)
28     port = None
29     tunnel = None
30     tun_state = None
31     if iface and iface.get('type') == constants.IFTYPE_VLAN:
32         ports = neutron_neutron.get_ports_by_key()
33         port = ports.get(ifname)
34     elif iface and iface.get('type') == constants.IFTYPE_TUNNEL:
35         # tunnels = itm_state_tunnels_state.get_tunnels_by_key()
36         # tunnel = tunnels.get(ifname)
37         tun_states = itm_state_tunnels_state.get_tunnels_by_key()
38         tun_state = tun_states.get(ifname)
39     else:
40         print "UNSUPPORTED IfType"
41     return iface, ifstate, port, tunnel, tun_state
42
43
44 def analyze_interface(args):
45     ietf_interfaces_interfaces = ietf_interfaces.interfaces(Model.CONFIG, args)
46     ifaces = ietf_interfaces_interfaces.get_interfaces_by_key()
47
48     ietf_interfaces_interfaces_state = ietf_interfaces.interfaces_state(Model.OPERATIONAL, args)
49     ifstates = ietf_interfaces_interfaces_state.get_interfaces_by_key()
50
51     if not args.ifName:
52         print_keys(ifaces, ifstates)
53         exit(1)
54
55     ifname = args.ifName
56     iface, ifstate, port, tunnel, tunState = by_ifname(args, ifname, ifstates, ifaces)
57     print "InterfaceConfig: \n{}".format(utils.format_json(args, iface))
58     print "InterfaceState: \n{}".format(utils.format_json(args, ifstate))
59     if port:
60         print "NeutronPort: \n{}".format(utils.format_json(args, port))
61         # analyze_neutron_port(port, iface, ifstate)
62         return
63     if tunnel:
64         print "Tunnel: \n{}".format(utils.format_json(args, tunnel))
65     if tunState:
66         print "TunState: \n{}".format(utils.format_json(args, tunState))
67     if ifstate:
68         ncId = ifstate.get('lower-layer-if')[0]
69         nodeId = ncId[:ncId.rindex(':')]
70         # analyze_inventory(nodeId, True, ncId, ifname)
71         # analyze_inventory(nodeId, False, ncId, ifname)
72
73
74 def analyze_trunks(args):
75     ietf_interfaces_interfaces = ietf_interfaces.interfaces(Model.CONFIG, args)
76     ietf_interfaces_interfaces_state = ietf_interfaces.interfaces_state(Model.OPERATIONAL, args)
77     l3vpn_vpn_interfaces = l3vpn.vpn_instance_to_vpn_id(Model.CONFIG, args)
78     neutron_neutron = neutron.neutron(Model.CONFIG, args)
79
80     nports = neutron_neutron.get_ports_by_key()
81     ntrunks = neutron_neutron.get_trunks_by_key()
82     vpninterfaces = l3vpn_vpn_interfaces.get_vpn_ids_by_key()
83     ifaces = ietf_interfaces_interfaces.get_interfaces_by_key()
84     ifstates = ietf_interfaces_interfaces_state.get_interfaces_by_key()
85     subport_dict = {}
86     for v in ntrunks.itervalues():
87         nport = nports.get(v.get('port-id'))
88         s_subports = []
89         for subport in v.get('sub-ports'):
90             sport_id = subport.get('port-id')
91             snport = nports.get(sport_id)
92             svpniface = vpninterfaces.get(sport_id)
93             siface = ifaces.get(sport_id)
94             sifstate = ifstates.get(sport_id)
95             subport['SubNeutronPort'] = 'Correct' if snport else 'Wrong'
96             subport['SubVpnInterface'] = 'Correct' if svpniface else 'Wrong'
97             subport['ofport'] = Model.get_ofport_from_ncid()
98             if siface:
99                 vlan_mode = siface.get('odl-interface:l2vlan-mode')
100                 parent_iface_id = siface.get('odl-interface:parent-interface')
101                 if vlan_mode !='trunk-member':
102                     subport['SubIface'] = 'WrongMode'
103                 elif parent_iface_id !=v.get('port-id'):
104                     subport['SubIface'] = 'WrongParent'
105                 elif siface.get('odl-interface:vlan-id') !=subport.get('segmentation-id'):
106                     subport['SubIface'] = 'WrongVlanId'
107                 else:
108                     subport['SubIface'] = 'Correct'
109             else:
110                 subport['SubIface'] = 'Wrong'
111             s_subport = 'SegId:{}, PortId:{}, SubNeutronPort:{}, SubIface:{}, SubVpnIface:{}'.format(
112                 subport.get('segmentation-id'), subport.get('port-id'),
113                 subport.get('SubNeutronPort'),
114                 subport.get('SubIface'),
115                 subport.get('SubVpnInterface'))
116             s_subports.append(subport)
117             subport_dict[subport['port-id']] = subport
118         s_trunk = 'TrunkName:{}, TrunkId:{}, PortId:{}, NeutronPort:{}, SubPorts:{}'.format(
119             v.get('name'), v.get('uuid'), v.get('port-id'),
120             'Correct' if nport else 'Wrong', utils.format_json(args, s_subports))
121         print s_trunk
122     print '\n------------------------------------'
123     print   'Analyzing Flow status for SubPorts'
124     print   '------------------------------------'
125     for flow in utils.sort(flows.get_all_flows(['ifm'], ['vlanid']), 'ifname'):
126         subport = subport_dict.get(flow.get('ifname')) or None
127         vlanid = subport.get('segmentation-id') if subport else None
128         ofport = subport.get('ofport') if subport else None
129         flow_status = 'Okay'
130         if flow.get('ofport') and flow.get('ofport') != ofport:
131             flow_status = 'OfPort mismatch for SubPort:{} and Flow:{}'.format(subport, flow.get('flow'))
132         if flow.get('vlanid') and flow.get('vlanid') != vlanid:
133             flow_status = 'VlanId mismatch for SubPort:{} and Flow:{}'.format(subport, flow.get('flow'))
134         if subport:
135             print 'SubPort:{},Table:{}/{},FlowStatus:{}'.format(
136                 subport.get('port-id'), flow.get('table'),
137                 tables.get_table_name(flow.get('table')),
138                 flow_status)
139
140
141 def analyze_neutron_port(port, iface, ifstate):
142     for flow in utils.sort(flows.get_all_flows(['all']), 'table'):
143         if ((flow.get('ifname') == port['uuid']) or
144                 (flow.get('lport') and ifstate and flow['lport'] == ifstate.get('if-index')) or
145                 (iface['name'] == flow.get('ifname'))):
146             result = 'Table:{}/{},FlowId:{}{}'.format(
147                 flow['table'], tables.get_table_name(flow['table']),
148                 flow['id'], utils.show_optionals(flow))
149             print result
150             print 'Flow:', utils.format_json(None, flow_parser.parse_flow(flow.get('flow')))
151
152
153 def analyze_inventory(args):
154     config.get_models(args, {
155         "odl_inventory_nodes_config",
156         "odl_inventory_nodes_operational"})
157
158     if args.isConfig:
159         nodes = config.gmodels.odl_inventory_nodes_config.get_nodes_by_key()
160         print "Inventory Config:"
161     else:
162         print "Inventory Operational:"
163         nodes = config.gmodels.odl_inventory_nodes_operational.get_nodes_by_key()
164     node = nodes.get("openflow:" + args.nodeId)
165     if node is None:
166         print "node: {} was not found".format("openflow:" + args.nodeId)
167         return
168     tables = node.get(Nodes.NODE_TABLE)
169     groups = node.get(Nodes.NODE_GROUP)
170     flow_list = []
171     print "Flows:"
172     for table in tables:
173         for flow in table.get('flow', []):
174             if not args.ifName or args.ifName in utils.nstr(flow.get('flow-name')):
175                 flow_dict = {'table': table['id'], 'id': flow['id'], 'name': flow.get('flow-name'), 'flow': flow}
176                 flow_list.append(flow_dict)
177     flows = sorted(flow_list, key=lambda x: x['table'])
178     for flow in flows:
179         print 'Table:{}/{}'.format(flow['table'],
180                                    tables.get_table_name(flow['table']))
181         print 'FlowId:', flow['id'], 'FlowName:', flow.get('name')