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