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