khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [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 | |
| 24 | from table import print_pb_as_table, print_pb_list_as_table |
| 25 | from utils import pb2dict |
| 26 | from utils import print_flows, print_groups |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 27 | from google.protobuf.empty_pb2 import Empty |
| 28 | |
Arun Arora | ed4b760 | 2019-04-02 18:42:37 +0000 | [diff] [blame] | 29 | from voltha_protos import voltha_pb2 |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 30 | |
| 31 | |
| 32 | class LogicalDeviceCli(Cmd): |
| 33 | |
| 34 | def __init__(self, logical_device_id, get_stub): |
| 35 | Cmd.__init__(self) |
| 36 | self.get_stub = get_stub |
| 37 | self.logical_device_id = logical_device_id |
| 38 | self.prompt = '(' + self.colorize( |
| 39 | self.colorize('logical device {}'.format(logical_device_id), 'red'), |
| 40 | 'bold') + ') ' |
| 41 | |
| 42 | def cmdloop(self): |
| 43 | self._cmdloop() |
| 44 | |
| 45 | def get_logical_device(self, depth=0): |
| 46 | stub = self.get_stub() |
| 47 | res = stub.GetLogicalDevice(voltha_pb2.ID(id=self.logical_device_id), |
| 48 | metadata=(('get-depth', str(depth)), )) |
| 49 | return res |
| 50 | |
| 51 | def get_device(self, id): |
| 52 | stub = self.get_stub() |
| 53 | return stub.GetDevice(voltha_pb2.ID(id=id)) |
| 54 | |
| 55 | def get_devices(self): |
| 56 | stub = self.get_stub() |
| 57 | res = stub.ListDevices(Empty()) |
| 58 | return res.items |
| 59 | |
| 60 | do_exit = Cmd.do_quit |
| 61 | |
| 62 | def do_show(self, _): |
| 63 | """Show detailed logical device information""" |
| 64 | print_pb_as_table('Logical device {}'.format(self.logical_device_id), |
| 65 | self.get_logical_device(depth=-1)) |
| 66 | |
| 67 | def do_ports(self, _): |
| 68 | """Show ports of logical device""" |
| 69 | device = self.get_logical_device(depth=-1) |
| 70 | omit_fields = { |
| 71 | 'ofp_port.advertised', |
| 72 | 'ofp_port.peer', |
| 73 | 'ofp_port.max_speed' |
| 74 | } |
| 75 | print_pb_list_as_table('Logical device ports:', device.ports, |
| 76 | omit_fields, self.poutput) |
| 77 | |
| 78 | def do_flows(self, _): |
| 79 | """Show flow table for logical device""" |
| 80 | logical_device = pb2dict(self.get_logical_device(-1)) |
| 81 | print_flows( |
| 82 | 'Logical Device', |
| 83 | self.logical_device_id, |
| 84 | type='n/a', |
| 85 | flows=logical_device['flows']['items'], |
| 86 | groups=logical_device['flow_groups']['items'] |
| 87 | ) |
| 88 | |
| 89 | def do_groups(self, _): |
| 90 | """Show flow group table for logical device""" |
| 91 | logical_device = pb2dict(self.get_logical_device(-1)) |
| 92 | print_groups( |
| 93 | 'Logical Device', |
| 94 | self.logical_device_id, |
| 95 | type='n/a', |
| 96 | groups=logical_device['flow_groups']['items'] |
| 97 | ) |
| 98 | |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 99 | def do_meters(self, _): |
| 100 | """Show flow meter table for logical device""" |
| 101 | logical_device = pb2dict(self.get_logical_device(-1)) |
| 102 | print_meters( |
| 103 | 'Logical Device', |
| 104 | self.logical_device_id, |
| 105 | type='n/a', |
| 106 | meters=logical_device['meters']['items'] |
| 107 | ) |
| 108 | |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 109 | def do_devices(self, line): |
| 110 | """List devices that belong to this logical device""" |
| 111 | logical_device = self.get_logical_device() |
| 112 | root_device_id = logical_device.root_device_id |
| 113 | devices = [self.get_device(root_device_id)] |
| 114 | for d in self.get_devices(): |
| 115 | if d.parent_id == root_device_id: |
| 116 | devices.append(d) |
| 117 | omit_fields = { |
| 118 | 'adapter', |
| 119 | 'vendor', |
| 120 | 'model', |
| 121 | 'hardware_version', |
| 122 | 'software_version', |
| 123 | 'firmware_version', |
| 124 | 'serial_number' |
| 125 | } |
| 126 | print_pb_list_as_table('Devices:', devices, omit_fields, self.poutput) |
| 127 | |