blob: fc17d47c115604d4eb36bd8b8cd21d4728319ac2 [file] [log] [blame]
adminbae64d82013-08-01 10:50:15 -07001#!/usr/bin/env python
kelvin-onlab2f4ef842015-01-19 12:49:33 -08002"""
adminbae64d82013-08-01 10:50:15 -07003Created on 24-Oct-2012
Jeremy Songsterae01bba2016-07-11 15:39:17 -07004Modified 2015 by ON.Lab
kelvin-onlab2f4ef842015-01-19 12:49:33 -08005
Jeremy Songsterae01bba2016-07-11 15:39:17 -07006Please refer questions to either the onos test mailing list at <onos-test@onosproject.org>,
7the System Testing Plans and Results wiki page at <https://wiki.onosproject.org/x/voMg>,
8or the System Testing Guide page at <https://wiki.onosproject.org/x/WYQg>
adminbae64d82013-08-01 10:50:15 -07009
10 TestON is free software: you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation, either version 2 of the License, or
kelvin-onlab2f4ef842015-01-19 12:49:33 -080013 ( at your option ) any later version.
adminbae64d82013-08-01 10:50:15 -070014
15 TestON is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
kelvin-onlab2f4ef842015-01-19 12:49:33 -080021 along with TestON. If not, see <http://www.gnu.org/licenses/>.
kelvin-onlab2f4ef842015-01-19 12:49:33 -080022"""
Jon Hallfd8ce432015-07-21 13:07:53 -070023import logging
24
kelvin-onlab2f4ef842015-01-19 12:49:33 -080025
26class Component( object ):
27
28 """
adminbae64d82013-08-01 10:50:15 -070029 This is the tempalte class for components
kelvin-onlab2f4ef842015-01-19 12:49:33 -080030 """
31 def __init__( self ):
adminbae64d82013-08-01 10:50:15 -070032 self.default = ''
kelvin-onlab2f4ef842015-01-19 12:49:33 -080033 self.wrapped = sys.modules[ __name__ ]
Jon Halleb3a6842015-02-18 15:17:51 -080034 self.count = 0
kelvin-onlab2f4ef842015-01-19 12:49:33 -080035
36 def __getattr__( self, name ):
37 """
adminbae64d82013-08-01 10:50:15 -070038 This will invoke, if the attribute wasn't found the usual ways.
Jon Hall20ead012015-02-18 14:00:42 -080039 Here it will look for assert_attribute and will execute when
40 AttributeError occurs.
41 It will return the result of the assert_attribute.
kelvin-onlab2f4ef842015-01-19 12:49:33 -080042 """
adminbae64d82013-08-01 10:50:15 -070043 try:
kelvin-onlab2f4ef842015-01-19 12:49:33 -080044 return getattr( self.wrapped, name )
Jon Hall20ead012015-02-18 14:00:42 -080045 except AttributeError as error:
Jon Halleb3a6842015-02-18 15:17:51 -080046 # NOTE: The first time we load a driver module we get this error
Jon Hall0fc0d452015-07-14 09:49:58 -070047 if "'module' object has no attribute '__path__'" in error:
48 pass
Jon Halleb3a6842015-02-18 15:17:51 -080049 else:
50 main.log.error( str(error.__class__) + " " + str(error) )
adminbae64d82013-08-01 10:50:15 -070051 try:
Jon Hall20ead012015-02-18 14:00:42 -080052 def experimentHandling( *args, **kwargs ):
adminbae64d82013-08-01 10:50:15 -070053 if main.EXPERIMENTAL_MODE == main.TRUE:
Jon Hall20ead012015-02-18 14:00:42 -080054 result = self.experimentRun( *args, **kwargs )
55 main.log.info( "EXPERIMENTAL MODE. API " +
56 str( name ) +
57 " not yet implemented. " +
58 "Returning dummy values" )
kelvin-onlab2f4ef842015-01-19 12:49:33 -080059 return result
adminbae64d82013-08-01 10:50:15 -070060 else:
61 return main.FALSE
62 return experimentHandling
kelvin-onlab2f4ef842015-01-19 12:49:33 -080063 except TypeError as e:
Jon Hall20ead012015-02-18 14:00:42 -080064 main.log.error( "Arguments for experimental mode does not" +
65 " have key 'retruns'" + e )
kelvin-onlab2f4ef842015-01-19 12:49:33 -080066
67 def connect( self ):
68
69 vars( main )[ self.name + 'log' ] = logging.getLogger( self.name )
70
71 session_file = main.logdir + "/" + self.name + ".session"
72 self.log_handler = logging.FileHandler( session_file )
73 self.log_handler.setLevel( logging.DEBUG )
74
75 vars( main )[ self.name + 'log' ].setLevel( logging.DEBUG )
76 _formatter = logging.Formatter(
77 "%(asctime)s %(name)-10s: %(levelname)-8s: %(message)s" )
78 self.log_handler.setFormatter( _formatter )
79 vars( main )[ self.name + 'log' ].addHandler( self.log_handler )
80 # Adding header for the component log
81 vars( main )[ self.name + 'log' ].info( main.logHeader )
adminbae64d82013-08-01 10:50:15 -070082 # Opening the session log to append command's execution output
kelvin-onlab2f4ef842015-01-19 12:49:33 -080083 self.logfile_handler = open( session_file, "a" )
84
adminbae64d82013-08-01 10:50:15 -070085 return "Dummy"
kelvin-onlab2f4ef842015-01-19 12:49:33 -080086
87 def execute( self, cmd ):
adminbae64d82013-08-01 10:50:15 -070088 return main.TRUE
kelvin-onlab2f4ef842015-01-19 12:49:33 -080089 # import commands
90 # return commands.getoutput( cmd )
91
92 def disconnect( self ):
93 return main.TRUE
94
95 def config( self ):
adminbae64d82013-08-01 10:50:15 -070096 self = self
97 # Need to update the configuration code
kelvin-onlab2f4ef842015-01-19 12:49:33 -080098
99 def cleanup( self ):
adminbae64d82013-08-01 10:50:15 -0700100 return main.TRUE
kelvin-onlab2f4ef842015-01-19 12:49:33 -0800101
102 def log( self, message ):
103 """
104 Here finding the for the component to which the
adminbae64d82013-08-01 10:50:15 -0700105 log message based on the called child object.
kelvin-onlab2f4ef842015-01-19 12:49:33 -0800106 """
107 vars( main )[ self.name + 'log' ].info( "\n" + message + "\n" )
108
109 def close_log_handles( self ):
110 vars( main )[ self.name + 'log' ].removeHandler( self.log_handler )
adminbae64d82013-08-01 10:50:15 -0700111 if self.logfile_handler:
112 self.logfile_handler.close()
kelvin-onlab2f4ef842015-01-19 12:49:33 -0800113
114 def get_version( self ):
adminbae64d82013-08-01 10:50:15 -0700115 return "Version unknown"
116
Jon Hall20ead012015-02-18 14:00:42 -0800117 def experimentRun( self, *args, **kwargs ):
118 # FIXME handle *args
kelvin-onlab2f4ef842015-01-19 12:49:33 -0800119 args = utilities.parse_args( [ "RETURNS" ], **kwargs )
120 return args[ "RETURNS" ]
adminbae64d82013-08-01 10:50:15 -0700121
122
123if __name__ != "__main__":
124 import sys
kelvin-onlab2f4ef842015-01-19 12:49:33 -0800125 sys.modules[ __name__ ] = Component()
126