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 | Logical 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 pb2dict |
| 26 | from cli.utils import print_flows |
| 27 | from voltha.protos import third_party |
Zsolt Haraszti | 85f1285 | 2016-12-24 08:30:58 -0800 | [diff] [blame] | 28 | from google.protobuf.empty_pb2 import Empty |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 29 | |
| 30 | _ = third_party |
| 31 | from voltha.protos import voltha_pb2 |
| 32 | |
| 33 | |
| 34 | class LogicalDeviceCli(Cmd): |
| 35 | |
| 36 | def __init__(self, get_channel, logical_device_id): |
| 37 | Cmd.__init__(self) |
| 38 | self.get_channel = get_channel |
| 39 | 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 Haraszti | 9b485fb | 2016-12-26 23:11:15 -0800 | [diff] [blame] | 44 | def cmdloop(self): |
| 45 | self._cmdloop() |
| 46 | |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 47 | def get_logical_device(self, depth=0): |
| 48 | stub = voltha_pb2.VolthaLocalServiceStub(self.get_channel()) |
| 49 | res = stub.GetLogicalDevice(voltha_pb2.ID(id=self.logical_device_id), |
| 50 | metadata=(('get-depth', str(depth)), )) |
| 51 | return res |
| 52 | |
Zsolt Haraszti | 85f1285 | 2016-12-24 08:30:58 -0800 | [diff] [blame] | 53 | def get_device(self, id): |
| 54 | stub = voltha_pb2.VolthaLocalServiceStub(self.get_channel()) |
| 55 | return stub.GetDevice(voltha_pb2.ID(id=id)) |
| 56 | |
| 57 | def get_devices(self): |
| 58 | stub = voltha_pb2.VolthaLocalServiceStub(self.get_channel()) |
| 59 | res = stub.ListDevices(Empty()) |
| 60 | return res.items |
| 61 | |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame] | 62 | do_exit = Cmd.do_quit |
| 63 | |
Zsolt Haraszti | 85f1285 | 2016-12-24 08:30:58 -0800 | [diff] [blame] | 64 | def do_show(self, _): |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 65 | """Show detailed logical device information""" |
Sergio Slobodrian | b5ef348 | 2017-03-17 16:56:16 -0400 | [diff] [blame] | 66 | omit_fields = { |
| 67 | 'flow_groups', |
| 68 | 'flows', |
| 69 | 'ports', |
| 70 | 'switch_features.auxiliary_id', |
| 71 | 'switch_features.capabilities', |
| 72 | 'switch_features.datapath_id', |
| 73 | 'switch_features.n_buffers', |
| 74 | 'switch_features.n_tables', |
| 75 | 'desc.dp_desc', |
| 76 | 'desc.hw_desc', |
| 77 | 'desc.mfr_desc', |
| 78 | 'desc.serial_num', |
| 79 | 'desc.sw_desc', |
| 80 | 'datapath_id' |
| 81 | } |
Zsolt Haraszti | 85f1285 | 2016-12-24 08:30:58 -0800 | [diff] [blame] | 82 | print_pb_as_table('Logical device {}'.format(self.logical_device_id), |
Sergio Slobodrian | b5ef348 | 2017-03-17 16:56:16 -0400 | [diff] [blame] | 83 | self.get_logical_device(depth=-1), omit_fields) |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 84 | |
Zsolt Haraszti | 85f1285 | 2016-12-24 08:30:58 -0800 | [diff] [blame] | 85 | def do_ports(self, _): |
| 86 | """Show ports of logical device""" |
| 87 | device = self.get_logical_device(depth=-1) |
| 88 | omit_fields = { |
| 89 | 'ofp_port.advertised', |
| 90 | 'ofp_port.peer', |
| 91 | 'ofp_port.max_speed', |
| 92 | } |
| 93 | print_pb_list_as_table('Logical device ports:', device.ports, |
| 94 | omit_fields, self.poutput) |
| 95 | |
| 96 | def do_flows(self, _): |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 97 | """Show flow table for logical device""" |
| 98 | logical_device = pb2dict(self.get_logical_device(-1)) |
| 99 | print_flows( |
| 100 | 'Logical Device', |
| 101 | self.logical_device_id, |
| 102 | type='n/a', |
| 103 | flows=logical_device['flows']['items'], |
| 104 | groups=logical_device['flow_groups']['items'] |
| 105 | ) |
| 106 | |
Zsolt Haraszti | 85f1285 | 2016-12-24 08:30:58 -0800 | [diff] [blame] | 107 | def do_devices(self, line): |
| 108 | """List devices that belong to this logical device""" |
| 109 | logical_device = self.get_logical_device() |
| 110 | root_device_id = logical_device.root_device_id |
| 111 | devices = [self.get_device(root_device_id)] |
| 112 | for d in self.get_devices(): |
| 113 | if d.parent_id == root_device_id: |
| 114 | devices.append(d) |
| 115 | omit_fields = { |
| 116 | 'adapter', |
| 117 | 'vendor', |
| 118 | 'model', |
| 119 | 'hardware_version', |
| 120 | 'software_version', |
| 121 | 'firmware_version', |
Sergio Slobodrian | b5ef348 | 2017-03-17 16:56:16 -0400 | [diff] [blame] | 122 | 'serial_number', |
| 123 | 'parent_port_no', |
| 124 | 'vlan', |
| 125 | 'ports', |
| 126 | 'reason', |
| 127 | 'root', |
| 128 | 'parent_id' |
Zsolt Haraszti | 85f1285 | 2016-12-24 08:30:58 -0800 | [diff] [blame] | 129 | } |
| 130 | print_pb_list_as_table('Devices:', devices, omit_fields, self.poutput) |
| 131 | |