Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 1 | def __init__( self ): |
| 2 | self.default = '' |
| 3 | |
Jon Hall | 57b5043 | 2015-10-22 10:20:10 -0700 | [diff] [blame] | 4 | def consistentCheck(): |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 5 | """ |
Jon Hall | 57b5043 | 2015-10-22 10:20:10 -0700 | [diff] [blame] | 6 | Checks that TestON counters are consistent across all nodes. |
| 7 | |
| 8 | Returns the tuple (onosCounters, consistent) |
| 9 | - onosCounters is the parsed json output of the counters command on all nodes |
| 10 | - consistent is main.TRUE if all "TestON" counters are consitent across all |
| 11 | nodes or main.FALSE |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 12 | """ |
| 13 | import json |
| 14 | correctResults = main.TRUE |
| 15 | # Get onos counters results |
Jon Hall | 57b5043 | 2015-10-22 10:20:10 -0700 | [diff] [blame] | 16 | onosCountersRaw = [] |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 17 | threads = [] |
| 18 | for i in range( main.numCtrls ): |
| 19 | t = main.Thread( target=main.CLIs[i].counters, |
| 20 | name="counters-" + str( i ) ) |
| 21 | threads.append( t ) |
| 22 | t.start() |
| 23 | for t in threads: |
| 24 | t.join() |
Jon Hall | 57b5043 | 2015-10-22 10:20:10 -0700 | [diff] [blame] | 25 | onosCountersRaw.append( t.result ) |
| 26 | onosCounters = [] |
| 27 | for i in range( main.numCtrls ): |
| 28 | try: |
| 29 | onosCounters.append( json.loads( onosCountersRaw[i] ) ) |
| 30 | except ( ValueError, TypeError ): |
| 31 | main.log.error( "Could not parse counters response from ONOS" + |
| 32 | str( i + 1 ) ) |
| 33 | main.log.warn( repr( onosCountersRaw[ i ] ) ) |
| 34 | return main.FALSE |
| 35 | |
| 36 | testCounters = {} |
| 37 | # make a list of all the "TestON-*" counters in ONOS |
| 38 | # lookes like a dict whose keys are the name of the ONOS node and values |
| 39 | # are a list of the counters. I.E. |
| 40 | # { "ONOS1": [ {"name":"TestON-inMemory","value":56}, |
| 41 | # {"name":"TestON-Partitions","value":56} ] |
| 42 | # } |
| 43 | # NOTE: There is an assumtion that all nodes are active |
| 44 | # based on the above for loops |
| 45 | for controller in enumerate( onosCounters ): |
| 46 | for dbType in controller[1]: |
| 47 | for dbName, items in dbType.iteritems(): |
| 48 | for item in items: |
| 49 | if 'TestON' in item['name']: |
| 50 | node = 'ONOS' + str( controller[0] + 1 ) |
| 51 | try: |
| 52 | testCounters[node].append( item ) |
| 53 | except KeyError: |
| 54 | testCounters[node] = [ item ] |
| 55 | # compare the counters on each node |
| 56 | tmp = [ v == testCounters['ONOS1'] for k, v in testCounters.iteritems() ] |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 57 | if all( tmp ): |
| 58 | consistent = main.TRUE |
| 59 | else: |
| 60 | consistent = main.FALSE |
Jon Hall | 57b5043 | 2015-10-22 10:20:10 -0700 | [diff] [blame] | 61 | main.log.error( "ONOS nodes have different values for counters:\n" + |
| 62 | testCounters ) |
| 63 | return ( onosCounters, consistent ) |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 64 | |
Jon Hall | 57b5043 | 2015-10-22 10:20:10 -0700 | [diff] [blame] | 65 | def counterCheck( counterName, counterValue ): |
| 66 | """ |
| 67 | Checks that TestON counters are consistent across all nodes and that |
| 68 | specified counter is in ONOS with the given value |
| 69 | """ |
| 70 | import json |
| 71 | correctResults = main.TRUE |
| 72 | # Get onos counters results and consistentCheck |
| 73 | onosCounters, consistent = main.Counters.consistentCheck() |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 74 | # Check for correct values |
| 75 | for i in range( main.numCtrls ): |
Jon Hall | 57b5043 | 2015-10-22 10:20:10 -0700 | [diff] [blame] | 76 | current = onosCounters[i] |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 77 | onosValue = None |
| 78 | try: |
| 79 | for database in current: |
| 80 | database = database.values()[0] |
| 81 | for counter in database: |
| 82 | if counter.get( 'name' ) == counterName: |
| 83 | onosValue = counter.get( 'value' ) |
| 84 | break |
| 85 | except AttributeError, e: |
| 86 | main.log.error( "ONOS" + str( i + 1 ) + " counters result " + |
| 87 | "is not as expected" ) |
| 88 | correctResults = main.FALSE |
| 89 | if onosValue == counterValue: |
| 90 | main.log.info( counterName + " counter value is correct" ) |
| 91 | else: |
| 92 | main.log.error( counterName + " counter value is incorrect," + |
| 93 | " expected value: " + str( counterValue ) |
| 94 | + " current value: " + str( onosValue ) ) |
| 95 | correctResults = main.FALSE |
| 96 | return consistent and correctResults |