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