blob: cd991c65ef882e5673e60ba3f8a21e5616387122 [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
27from python.protos import third_party
28from google.protobuf.empty_pb2 import Empty
29
30_ = third_party
31from python.protos import voltha_pb2
32
33
34class LogicalDeviceCli(Cmd):
35
36 def __init__(self, logical_device_id, get_stub):
37 Cmd.__init__(self)
38 self.get_stub = get_stub
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
44 def cmdloop(self):
45 self._cmdloop()
46
47 def get_logical_device(self, depth=0):
48 stub = self.get_stub()
49 res = stub.GetLogicalDevice(voltha_pb2.ID(id=self.logical_device_id),
50 metadata=(('get-depth', str(depth)), ))
51 return res
52
53 def get_device(self, id):
54 stub = self.get_stub()
55 return stub.GetDevice(voltha_pb2.ID(id=id))
56
57 def get_devices(self):
58 stub = self.get_stub()
59 res = stub.ListDevices(Empty())
60 return res.items
61
62 do_exit = Cmd.do_quit
63
64 def do_show(self, _):
65 """Show detailed logical device information"""
66 print_pb_as_table('Logical device {}'.format(self.logical_device_id),
67 self.get_logical_device(depth=-1))
68
69 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',
75 'ofp_port.max_speed'
76 }
77 print_pb_list_as_table('Logical device ports:', device.ports,
78 omit_fields, self.poutput)
79
80 def do_flows(self, _):
81 """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
91 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
101 def do_devices(self, line):
102 """List devices that belong to this logical device"""
103 logical_device = self.get_logical_device()
104 root_device_id = logical_device.root_device_id
105 devices = [self.get_device(root_device_id)]
106 for d in self.get_devices():
107 if d.parent_id == root_device_id:
108 devices.append(d)
109 omit_fields = {
110 'adapter',
111 'vendor',
112 'model',
113 'hardware_version',
114 'software_version',
115 'firmware_version',
116 'serial_number'
117 }
118 print_pb_list_as_table('Devices:', devices, omit_fields, self.poutput)
119