blob: 66148870cb50b22bf795dc078831ead3d67982f5 [file] [log] [blame]
Jon Halle1a3b752015-07-22 13:02:46 -07001def __init__( self ):
2 self.default = ''
3
Jon Hall57b50432015-10-22 10:20:10 -07004def consistentCheck():
Jon Halle1a3b752015-07-22 13:02:46 -07005 """
Jon Hall57b50432015-10-22 10:20:10 -07006 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 Halle1a3b752015-07-22 13:02:46 -070012 """
13 import json
14 correctResults = main.TRUE
15 # Get onos counters results
Jon Hall57b50432015-10-22 10:20:10 -070016 onosCountersRaw = []
Jon Halle1a3b752015-07-22 13:02:46 -070017 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 Hall57b50432015-10-22 10:20:10 -070025 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 Halle1a3b752015-07-22 13:02:46 -070057 if all( tmp ):
58 consistent = main.TRUE
59 else:
60 consistent = main.FALSE
Jon Hall57b50432015-10-22 10:20:10 -070061 main.log.error( "ONOS nodes have different values for counters:\n" +
62 testCounters )
63 return ( onosCounters, consistent )
Jon Halle1a3b752015-07-22 13:02:46 -070064
Jon Hall57b50432015-10-22 10:20:10 -070065def 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 Halle1a3b752015-07-22 13:02:46 -070074 # Check for correct values
75 for i in range( main.numCtrls ):
Jon Hall57b50432015-10-22 10:20:10 -070076 current = onosCounters[i]
Jon Halle1a3b752015-07-22 13:02:46 -070077 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