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