Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 1 | #!/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 | """ |
| 19 | Device level CLI commands |
| 20 | """ |
| 21 | from cmd2 import Cmd |
| 22 | from simplejson import dumps |
| 23 | |
Zsolt Haraszti | 85f1285 | 2016-12-24 08:30:58 -0800 | [diff] [blame] | 24 | from cli.table import print_pb_as_table, print_pb_list_as_table |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 25 | from cli.utils import print_flows, pb2dict |
| 26 | from voltha.protos import third_party |
| 27 | |
| 28 | _ = third_party |
| 29 | from voltha.protos import voltha_pb2 |
| 30 | |
| 31 | |
| 32 | class DeviceCli(Cmd): |
| 33 | |
| 34 | def __init__(self, get_channel, device_id): |
| 35 | Cmd.__init__(self) |
| 36 | self.get_channel = get_channel |
| 37 | self.device_id = device_id |
| 38 | self.prompt = '(' + self.colorize( |
| 39 | self.colorize('device {}'.format(device_id), 'red'), 'bold') + ') ' |
| 40 | |
Zsolt Haraszti | 9b485fb | 2016-12-26 23:11:15 -0800 | [diff] [blame] | 41 | def cmdloop(self): |
| 42 | self._cmdloop() |
| 43 | |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 44 | def get_device(self, depth=0): |
| 45 | stub = voltha_pb2.VolthaLocalServiceStub(self.get_channel()) |
| 46 | res = stub.GetDevice(voltha_pb2.ID(id=self.device_id), |
| 47 | metadata=(('get-depth', str(depth)), )) |
| 48 | return res |
| 49 | |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame] | 50 | do_exit = Cmd.do_quit |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 51 | |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame] | 52 | def do_show(self, line): |
| 53 | """Show detailed device information""" |
Zsolt Haraszti | 85f1285 | 2016-12-24 08:30:58 -0800 | [diff] [blame] | 54 | print_pb_as_table('Device {}'.format(self.device_id), |
| 55 | self.get_device(depth=-1)) |
| 56 | |
| 57 | def do_ports(self, line): |
| 58 | """Show ports of device""" |
| 59 | device = self.get_device(depth=-1) |
| 60 | omit_fields = { |
| 61 | } |
| 62 | print_pb_list_as_table('Device ports:', device.ports, |
| 63 | omit_fields, self.poutput) |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame] | 64 | |
| 65 | def do_flows(self, line): |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 66 | """Show flow table for device""" |
| 67 | device = pb2dict(self.get_device(-1)) |
| 68 | print_flows( |
| 69 | 'Device', |
| 70 | self.device_id, |
| 71 | type=device['type'], |
| 72 | flows=device['flows']['items'], |
| 73 | groups=device['flow_groups']['items'] |
| 74 | ) |
| 75 | |