blob: 187dd88d27cf7d29b07ebe99c1e169aa1ccc2ddc [file] [log] [blame]
khenaidoofdbad6e2018-11-06 22:26:38 -05001#!/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
24from table import print_pb_as_table, print_pb_list_as_table
25from utils import pb2dict
26from utils import print_flows, print_groups
khenaidoofdbad6e2018-11-06 22:26:38 -050027from google.protobuf.empty_pb2 import Empty
28
Arun Aroraed4b7602019-04-02 18:42:37 +000029from voltha_protos import voltha_pb2
khenaidoofdbad6e2018-11-06 22:26:38 -050030
31
32class 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
99 def do_devices(self, line):
100 """List devices that belong to this logical device"""
101 logical_device = self.get_logical_device()
102 root_device_id = logical_device.root_device_id
103 devices = [self.get_device(root_device_id)]
104 for d in self.get_devices():
105 if d.parent_id == root_device_id:
106 devices.append(d)
107 omit_fields = {
108 'adapter',
109 'vendor',
110 'model',
111 'hardware_version',
112 'software_version',
113 'firmware_version',
114 'serial_number'
115 }
116 print_pb_list_as_table('Devices:', devices, omit_fields, self.poutput)
117