blob: 2dc95e17865cc504568ba4e1939f1c233cf6d355 [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
Jon Halle9b1fa32015-12-08 15:32:21 -080014 try:
15 correctResults = main.TRUE
16 # Get onos counters results
17 onosCountersRaw = []
18 threads = []
19 for i in range( main.numCtrls ):
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( main.numCtrls ):
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( i + 1 ) )
34 main.log.warn( repr( onosCountersRaw[ i ] ) )
35 onosCounters.append( [] )
36 return main.FALSE
Jon Hall57b50432015-10-22 10:20:10 -070037
Jon Halle9b1fa32015-12-08 15:32:21 -080038 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( 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 tmp = [ v == testCounters['ONOS1'] 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()
Jon Halle1a3b752015-07-22 13:02:46 -070070
Jon Hall57b50432015-10-22 10:20:10 -070071def 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()
Jon Halle1a3b752015-07-22 13:02:46 -070080 # Check for correct values
81 for i in range( main.numCtrls ):
Jon Hall57b50432015-10-22 10:20:10 -070082 current = onosCounters[i]
Jon Halle1a3b752015-07-22 13:02:46 -070083 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 main.log.error( "ONOS" + str( i + 1 ) + " counters result " +
93 "is not as expected" )
94 correctResults = main.FALSE
95 if onosValue == counterValue:
96 main.log.info( counterName + " counter value is correct" )
97 else:
98 main.log.error( counterName + " counter value is incorrect," +
99 " expected value: " + str( counterValue )
100 + " current value: " + str( onosValue ) )
101 correctResults = main.FALSE
102 return consistent and correctResults