blob: 94e24e6f5aaf13fa955d99fd628c2939c387b67f [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"""
19Device level CLI commands
20"""
21from cmd2 import Cmd
22from simplejson import dumps
23
24from cli.utils import print_flows, pb2dict
25from voltha.protos import third_party
26
27_ = third_party
28from voltha.protos import voltha_pb2
29
30
31class DeviceCli(Cmd):
32
33 def __init__(self, get_channel, device_id):
34 Cmd.__init__(self)
35 self.get_channel = get_channel
36 self.device_id = device_id
37 self.prompt = '(' + self.colorize(
38 self.colorize('device {}'.format(device_id), 'red'), 'bold') + ') '
39
40 def get_device(self, depth=0):
41 stub = voltha_pb2.VolthaLocalServiceStub(self.get_channel())
42 res = stub.GetDevice(voltha_pb2.ID(id=self.device_id),
43 metadata=(('get-depth', str(depth)), ))
44 return res
45
Zsolt Haraszti80175202016-12-24 00:17:51 -080046 do_exit = Cmd.do_quit
Zsolt Harasztid036b7e2016-12-23 15:36:01 -080047
Zsolt Haraszti80175202016-12-24 00:17:51 -080048 def do_show(self, line):
49 """Show detailed device information"""
50 self.poutput(dumps(pb2dict(self.get_device(depth=-1)),
51 indent=4, sort_keys=True))
52
53 def do_flows(self, line):
Zsolt Harasztid036b7e2016-12-23 15:36:01 -080054 """Show flow table for device"""
55 device = pb2dict(self.get_device(-1))
56 print_flows(
57 'Device',
58 self.device_id,
59 type=device['type'],
60 flows=device['flows']['items'],
61 groups=device['flow_groups']['items']
62 )
63