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