blob: 3e90f4172563db2314136180043e37854d08cffe [file] [log] [blame]
Zsolt Harasztid036b7e2016-12-23 15:36:01 -08001#!/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
Zsolt Haraszti85f12852016-12-24 08:30:58 -080024from cli.table import print_pb_as_table, print_pb_list_as_table
Zsolt Harasztid036b7e2016-12-23 15:36:01 -080025from cli.utils import pb2dict
26from cli.utils import print_flows
27from voltha.protos import third_party
Zsolt Haraszti85f12852016-12-24 08:30:58 -080028from google.protobuf.empty_pb2 import Empty
Zsolt Harasztid036b7e2016-12-23 15:36:01 -080029
30_ = third_party
31from voltha.protos import voltha_pb2
32
33
34class 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
44 def get_logical_device(self, depth=0):
45 stub = voltha_pb2.VolthaLocalServiceStub(self.get_channel())
46 res = stub.GetLogicalDevice(voltha_pb2.ID(id=self.logical_device_id),
47 metadata=(('get-depth', str(depth)), ))
48 return res
49
Zsolt Haraszti85f12852016-12-24 08:30:58 -080050 def get_device(self, id):
51 stub = voltha_pb2.VolthaLocalServiceStub(self.get_channel())
52 return stub.GetDevice(voltha_pb2.ID(id=id))
53
54 def get_devices(self):
55 stub = voltha_pb2.VolthaLocalServiceStub(self.get_channel())
56 res = stub.ListDevices(Empty())
57 return res.items
58
Zsolt Haraszti80175202016-12-24 00:17:51 -080059 do_exit = Cmd.do_quit
60
Zsolt Haraszti85f12852016-12-24 08:30:58 -080061 def do_show(self, _):
Zsolt Harasztid036b7e2016-12-23 15:36:01 -080062 """Show detailed logical device information"""
Zsolt Haraszti85f12852016-12-24 08:30:58 -080063 print_pb_as_table('Logical device {}'.format(self.logical_device_id),
64 self.get_logical_device(depth=-1))
Zsolt Harasztid036b7e2016-12-23 15:36:01 -080065
Zsolt Haraszti85f12852016-12-24 08:30:58 -080066 def do_ports(self, _):
67 """Show ports of logical device"""
68 device = self.get_logical_device(depth=-1)
69 omit_fields = {
70 'ofp_port.advertised',
71 'ofp_port.peer',
72 'ofp_port.max_speed',
73 }
74 print_pb_list_as_table('Logical device ports:', device.ports,
75 omit_fields, self.poutput)
76
77 def do_flows(self, _):
Zsolt Harasztid036b7e2016-12-23 15:36:01 -080078 """Show flow table for logical device"""
79 logical_device = pb2dict(self.get_logical_device(-1))
80 print_flows(
81 'Logical Device',
82 self.logical_device_id,
83 type='n/a',
84 flows=logical_device['flows']['items'],
85 groups=logical_device['flow_groups']['items']
86 )
87
Zsolt Haraszti85f12852016-12-24 08:30:58 -080088 def do_devices(self, line):
89 """List devices that belong to this logical device"""
90 logical_device = self.get_logical_device()
91 root_device_id = logical_device.root_device_id
92 devices = [self.get_device(root_device_id)]
93 for d in self.get_devices():
94 if d.parent_id == root_device_id:
95 devices.append(d)
96 omit_fields = {
97 'adapter',
98 'vendor',
99 'model',
100 'hardware_version',
101 'software_version',
102 'firmware_version',
103 'serial_number'
104 }
105 print_pb_list_as_table('Devices:', devices, omit_fields, self.poutput)
106