| #!/usr/bin/python |
| # Copyright 2012 Google Inc. All Rights Reserved. |
| |
| """This class defines devices.""" |
| |
| __author__ = 'Lin Xue (linxue@google.com)' |
| |
| """This file defines the base class of all devices, |
| and specific device classes, such as Bruno, Spirent, etc. |
| """ |
| |
| |
| class Device(object): |
| """Base class of all devices. |
| information of device class: |
| addr: address of device |
| user: user name of device |
| pwd: password of device |
| dev_name: device name |
| cmd_prompt: command prompt of device |
| (eg. For late iguana of Bruno, it is gfibertv#; |
| before iguana, it is none) |
| other: other information of device |
| """ |
| |
| dev_info = {'addr': 'None', |
| 'user': 'None', |
| 'pwd': 'None', |
| 'dev_name': 'None', |
| 'cmd_prompt': 'None', |
| 'other': 'None'} |
| |
| def GetDevInfo(self): |
| """This function returns device information.""" |
| |
| return (self.dev_info['dev_name'], self.dev_info['other']) |
| |
| def SetLogging(self, logging): |
| """This function set logging for device""" |
| |
| self.log = logging |
| return |
| |
| # End of Device class |
| |
| |
| class Bruno(Device): |
| """Bruno device class.""" |
| |
| def __init__(self, **kwargs): |
| """ Constructor for Bruno device |
| device name: Bruno, and other device informations |
| """ |
| self.dev_info['dev_name'] = 'Bruno' |
| for s in kwargs: |
| self.dev_info[s] = kwargs[s] |
| |
| self.dev_ssh = None |
| |
| # Bruno Device attributes |
| # serial number |
| self.dev_sn = None |
| # image version |
| self.dev_ver = None |
| # ip address |
| self.dev_ip = None |
| # system time |
| self.dev_time = None |
| # ACS url |
| self.dev_acs = None |
| # downloading image |
| self.dev_image = None |
| # product class |
| self.dev_class = None |
| # EPG primary |
| self.dev_epgpri = None |
| # EPG secondary |
| self.dev_epgsec = None |
| |
| return |
| |
| def GetDevCmd(self, cmd='', dev_attr='', dev_log=''): |
| """This function sends command to Bruno. |
| Then it gets back the results |
| If success, save the results and write log |
| If fail, show Warning message |
| Args: |
| cmd: |
| the command sent to Bruno system |
| to query the device information |
| dev_attr: |
| the device attribute |
| which can be get from this command |
| dev_log: |
| the logging message to be shown in log |
| |
| """ |
| |
| # Send command to query Bruno device information |
| self.dev_ssh.SendCmd(cmd) |
| |
| # Get back results |
| line = self.dev_ssh.GetCmdOutput(2)[1] |
| |
| if line is None: |
| info = self.log.CreateErrorInfo('Warning', |
| 'Can not get ' + dev_log) |
| self.log.SendLine(None, info) |
| else: |
| info = self.log.CreateProgressInfo('---', |
| 'Get ' + dev_log + ': ' + line) |
| self.log.SendLine(None, info) |
| dev_attr = line |
| |
| return line |
| |
| def GetSerialNumber(self): |
| """This function returns Bruno device serial number.""" |
| |
| result = self.GetDevCmd(r'hnvram -r 1st_serial_number', self.dev_sn, |
| 'serial number') |
| |
| return result |
| |
| def GetCurrentVersion(self): |
| """This function returns Bruno device current image version.""" |
| |
| result = self.GetDevCmd(r'more /etc/version', self.dev_ver, |
| 'current image version') |
| |
| return result |
| |
| def GetIPAddress(self): |
| """This function returns Bruno device IP address.""" |
| |
| result = self.GetDevCmd(r'ifconfig br0', self.dev_ip, |
| 'IP address') |
| |
| return result |
| |
| def GetDevTime(self): |
| """This function returns Bruno device current date.""" |
| |
| result = self.GetDevCmd(r'date', self.dev_time, |
| 'current date') |
| |
| return result |
| |
| def GetACSUrl(self): |
| """This function returns Bruno device ACS URL.""" |
| |
| result = self.GetDevCmd(r'cat /tmp/cwmp/acs_url', self.dev_acs, |
| 'ACS URL') |
| |
| return result |
| |
| def GetDnldImage(self): |
| """This function returns Bruno device current downloading image.""" |
| |
| result = self.GetDevCmd(r'ls -l /rw/tr69/dnld', self.dev_image, |
| 'current downloading image') |
| |
| return result |
| |
| def GetProductClass(self): |
| """This function returns Bruno device product class""" |
| |
| result = self.GetDevCmd(r'cat /etc/platform', self.dev_class, |
| 'product class') |
| |
| return result |
| |
| def GetEpgPrimary(self): |
| """This function returns Bruno device primary EPG""" |
| |
| result = self.GetDevCmd(r'cat /tmp/epgprimary', self.dev_epgpri, |
| 'EPG primary') |
| |
| return result |
| |
| def GetEpgSecond(self): |
| """This function returns Bruno device secondary EPG""" |
| |
| result = self.GetDevCmd(r'cat /tmp/epgsecondary', self.dev_epgsec, |
| 'EPG secondary') |
| |
| return result |
| |
| def GetDevInfo(self): |
| """This function returns all Bruno device information.""" |
| # Device name |
| info = self.log.CreateProgressInfo('---', |
| 'The device is ' |
| + self.dev_info['dev_name']) |
| self.log.SendLine(None, info) |
| |
| # Other device informations |
| self.GetSerialNumber() |
| self.GetCurrentVersion() |
| self.GetIPAddress() |
| self.GetDevTime() |
| self.GetACSUrl() |
| self.GetDnldImage() |
| self.GetProductClass() |
| self.GetEpgPrimary() |
| self.GetEpgSecond() |
| |
| return |
| |
| # End of Bruno device class |
| |
| |
| class Spirent(Device): |
| """Spirent device class.""" |
| |
| def __init__(self, **kwargs): |
| """ Constructor for Spirent device |
| device name: Spirent, and other device informations |
| """ |
| |
| self.dev_info['dev_name'] = 'Spirent' |
| for s in ('addr', 'user', 'pwd', 'cmd_prompt', 'other'): |
| if s in kwargs: |
| self.dev_info[s] = kwargs[s] |
| |
| # End of Spirent device class |