blob: 9c535198a711eca2d61222d106483d1207b27bba [file] [log] [blame]
Zsolt Harasztid036b7e2016-12-23 15:36:01 -08001#!/usr/bin/env python
2#
3# Copyright 2016 the original author or authors.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17
18"""
19Logical device level CLI commands
20"""
21from cmd2 import Cmd
22from simplejson import dumps
23
Zsolt Haraszti85f12852016-12-24 08:30:58 -080024from cli.table import print_pb_as_table, print_pb_list_as_table
Zsolt Harasztid036b7e2016-12-23 15:36:01 -080025from cli.utils import pb2dict
Gamze Abaka53cc0a22019-01-31 12:06:11 +000026from cli.utils import print_flows, print_groups, print_meters
Zsolt Harasztid036b7e2016-12-23 15:36:01 -080027from voltha.protos import third_party
Zsolt Haraszti85f12852016-12-24 08:30:58 -080028from google.protobuf.empty_pb2 import Empty
Zsolt Harasztid036b7e2016-12-23 15:36:01 -080029
30_ = third_party
31from voltha.protos import voltha_pb2
32
33
34class LogicalDeviceCli(Cmd):
35
khenaidoo108f05c2017-07-06 11:15:29 -040036 def __init__(self, logical_device_id, get_stub):
Zsolt Harasztid036b7e2016-12-23 15:36:01 -080037 Cmd.__init__(self)
khenaidoo108f05c2017-07-06 11:15:29 -040038 self.get_stub = get_stub
Zsolt Harasztid036b7e2016-12-23 15:36:01 -080039 self.logical_device_id = logical_device_id
40 self.prompt = '(' + self.colorize(
41 self.colorize('logical device {}'.format(logical_device_id), 'red'),
42 'bold') + ') '
43
Zsolt Haraszti9b485fb2016-12-26 23:11:15 -080044 def cmdloop(self):
45 self._cmdloop()
46
Zsolt Harasztid036b7e2016-12-23 15:36:01 -080047 def get_logical_device(self, depth=0):
khenaidoo108f05c2017-07-06 11:15:29 -040048 stub = self.get_stub()
Zsolt Harasztid036b7e2016-12-23 15:36:01 -080049 res = stub.GetLogicalDevice(voltha_pb2.ID(id=self.logical_device_id),
50 metadata=(('get-depth', str(depth)), ))
51 return res
52
Zsolt Haraszti85f12852016-12-24 08:30:58 -080053 def get_device(self, id):
khenaidoo108f05c2017-07-06 11:15:29 -040054 stub = self.get_stub()
Zsolt Haraszti85f12852016-12-24 08:30:58 -080055 return stub.GetDevice(voltha_pb2.ID(id=id))
56
57 def get_devices(self):
khenaidoo108f05c2017-07-06 11:15:29 -040058 stub = self.get_stub()
Zsolt Haraszti85f12852016-12-24 08:30:58 -080059 res = stub.ListDevices(Empty())
60 return res.items
61
Zsolt Haraszti80175202016-12-24 00:17:51 -080062 do_exit = Cmd.do_quit
63
Zsolt Haraszti85f12852016-12-24 08:30:58 -080064 def do_show(self, _):
Zsolt Harasztid036b7e2016-12-23 15:36:01 -080065 """Show detailed logical device information"""
Zsolt Haraszti85f12852016-12-24 08:30:58 -080066 print_pb_as_table('Logical device {}'.format(self.logical_device_id),
Sergio Slobodriana95f99b2017-03-21 10:22:47 -040067 self.get_logical_device(depth=-1))
Zsolt Harasztid036b7e2016-12-23 15:36:01 -080068
Zsolt Haraszti85f12852016-12-24 08:30:58 -080069 def do_ports(self, _):
70 """Show ports of logical device"""
71 device = self.get_logical_device(depth=-1)
72 omit_fields = {
73 'ofp_port.advertised',
74 'ofp_port.peer',
Sergio Slobodriana95f99b2017-03-21 10:22:47 -040075 'ofp_port.max_speed'
Zsolt Haraszti85f12852016-12-24 08:30:58 -080076 }
77 print_pb_list_as_table('Logical device ports:', device.ports,
78 omit_fields, self.poutput)
79
80 def do_flows(self, _):
Zsolt Harasztid036b7e2016-12-23 15:36:01 -080081 """Show flow table for logical device"""
82 logical_device = pb2dict(self.get_logical_device(-1))
83 print_flows(
84 'Logical Device',
85 self.logical_device_id,
86 type='n/a',
87 flows=logical_device['flows']['items'],
88 groups=logical_device['flow_groups']['items']
89 )
90
Gertjan Van Droogenbroeck308a9cf2017-10-11 15:59:51 -050091 def do_groups(self, _):
92 """Show flow group table for logical device"""
93 logical_device = pb2dict(self.get_logical_device(-1))
94 print_groups(
95 'Logical Device',
96 self.logical_device_id,
97 type='n/a',
98 groups=logical_device['flow_groups']['items']
99 )
100
Gamze Abaka53cc0a22019-01-31 12:06:11 +0000101 def do_meters(self, _):
102 """Show flow meter table for logical device"""
103 logical_device = pb2dict(self.get_logical_device(-1))
104 print_meters(
105 'Logical Device',
106 self.logical_device_id,
107 type='n/a',
108 meters=logical_device['meters']['items']
109 )
110
Zsolt Haraszti85f12852016-12-24 08:30:58 -0800111 def do_devices(self, line):
112 """List devices that belong to this logical device"""
113 logical_device = self.get_logical_device()
114 root_device_id = logical_device.root_device_id
115 devices = [self.get_device(root_device_id)]
116 for d in self.get_devices():
117 if d.parent_id == root_device_id:
118 devices.append(d)
119 omit_fields = {
120 'adapter',
121 'vendor',
122 'model',
123 'hardware_version',
124 'software_version',
125 'firmware_version',
Sergio Slobodriana95f99b2017-03-21 10:22:47 -0400126 'serial_number'
Zsolt Haraszti85f12852016-12-24 08:30:58 -0800127 }
128 print_pb_list_as_table('Devices:', devices, omit_fields, self.poutput)
129