blob: 1c5b0f1bcbcc57bddee91b24e585d8faf96244eb [file] [log] [blame]
Matteo Scandolo48d3d2d2017-08-08 13:05:27 -07001
2# Copyright 2017-present Open Networking Foundation
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16
A R Karthick10d5ecc2017-01-05 15:31:03 -080017#
Chetan Gaonkercfcce782016-05-10 10:10:42 -070018# Copyright 2016-present Ciena Corporation
19#
20# Licensed under the Apache License, Version 2.0 (the "License");
21# you may not use this file except in compliance with the License.
22# You may obtain a copy of the License at
A R Karthick10d5ecc2017-01-05 15:31:03 -080023#
Chetan Gaonkercfcce782016-05-10 10:10:42 -070024# http://www.apache.org/licenses/LICENSE-2.0
A R Karthick10d5ecc2017-01-05 15:31:03 -080025#
Chetan Gaonkercfcce782016-05-10 10:10:42 -070026# Unless required by applicable law or agreed to in writing, software
27# distributed under the License is distributed on an "AS IS" BASIS,
28# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29# See the License for the specific language governing permissions and
30# limitations under the License.
31#
Chetan Gaonker3ff8eae2016-04-12 14:50:26 -070032"""
33Created on 24-Oct-2012
34
35author:s: Anil Kumar ( anilkumar.s@paxterrasolutions.com ),
36 Raghav Kashyap( raghavkashyap@paxterrasolutions.com )
37
38
39 TestON is free software: you can redistribute it and/or modify
40 it under the terms of the GNU General Public License as published by
41 the Free Software Foundation, either version 2 of the License, or
42 ( at your option ) any later version.
43
44 TestON is distributed in the hope that it will be useful,
45 but WITHOUT ANY WARRANTY; without even the implied warranty of
46 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
47 GNU General Public License for more details.
48
49 You should have received a copy of the GNU General Public License
50 along with TestON. If not, see <http://www.gnu.org/licenses/>.
51
52
53
54"""
55import logging
56from clicommon import *
57
58class Component( object ):
59
60 """
61 This is the tempalte class for components
62 """
63 def __init__( self ):
64 self.default = ''
65 self.wrapped = sys.modules[ __name__ ]
66 self.count = 0
67
68 def __getattr__( self, name ):
69 """
70 This will invoke, if the attribute wasn't found the usual ways.
71 Here it will look for assert_attribute and will execute when
72 AttributeError occurs.
73 It will return the result of the assert_attribute.
74 """
75 try:
76 return getattr( self.wrapped, name )
77 except AttributeError as error:
78 # NOTE: The first time we load a driver module we get this error
79 if "'module' object has no attribute '__path__'" in error:
80 pass
81 else:
82 main.log.error( str(error.__class__) + " " + str(error) )
83 try:
84 def experimentHandling( *args, **kwargs ):
85 if main.EXPERIMENTAL_MODE == main.TRUE:
86 result = self.experimentRun( *args, **kwargs )
87 main.log.info( "EXPERIMENTAL MODE. API " +
88 str( name ) +
89 " not yet implemented. " +
90 "Returning dummy values" )
91 return result
92 else:
93 return main.FALSE
94 return experimentHandling
95 except TypeError as e:
96 main.log.error( "Arguments for experimental mode does not" +
97 " have key 'retruns'" + e )
98
99 def connect( self ):
100
101 vars( main )[ self.name + 'log' ] = logging.getLogger( self.name )
102
103 session_file = main.logdir + "/" + self.name + ".session"
104 self.log_handler = logging.FileHandler( session_file )
105 self.log_handler.setLevel( logging.DEBUG )
106
107 vars( main )[ self.name + 'log' ].setLevel( logging.DEBUG )
108 _formatter = logging.Formatter(
109 "%(asctime)s %(name)-10s: %(levelname)-8s: %(message)s" )
110 self.log_handler.setFormatter( _formatter )
111 vars( main )[ self.name + 'log' ].addHandler( self.log_handler )
112 # Adding header for the component log
113 vars( main )[ self.name + 'log' ].info( main.logHeader )
114 # Opening the session log to append command's execution output
A R Karthick10d5ecc2017-01-05 15:31:03 -0800115 self.logfile_handler = open( session_file, "w" )
Chetan Gaonker3ff8eae2016-04-12 14:50:26 -0700116
117 return "Dummy"
118
119 def execute( self, cmd ):
120 return main.TRUE
121 # import commands
122 # return commands.getoutput( cmd )
123
124 def disconnect( self ):
125 return main.TRUE
126
127 def config( self ):
128 self = self
129 # Need to update the configuration code
130
131 def cleanup( self ):
132 return main.TRUE
133
134 def log( self, message ):
135 """
136 Here finding the for the component to which the
137 log message based on the called child object.
138 """
139 vars( main )[ self.name + 'log' ].info( "\n" + message + "\n" )
140
141 def close_log_handles( self ):
142 vars( main )[ self.name + 'log' ].removeHandler( self.log_handler )
143 if self.logfile_handler:
144 self.logfile_handler.close()
145
146 def get_version( self ):
147 return "Version unknown"
148
149 def experimentRun( self, *args, **kwargs ):
150 # FIXME handle *args
151 args = utilities.parse_args( [ "RETURNS" ], **kwargs )
152 return args[ "RETURNS" ]
153
154
155if __name__ != "__main__":
156 import sys
157 sys.modules[ __name__ ] = Component()