blob: 192b919d80e8728804ef28be945bb57e4b7dd2a5 [file] [log] [blame]
Jon Halla440e872016-03-31 15:15:50 -07001import json
Jon Halle1a3b752015-07-22 13:02:46 -07002
Jon Halla440e872016-03-31 15:15:50 -07003class Counters():
Jon Hall57b50432015-10-22 10:20:10 -07004
Jon Halla440e872016-03-31 15:15:50 -07005 def __init__( self ):
6 self.default = ''
Jon Hall57b50432015-10-22 10:20:10 -07007
Jon Halla440e872016-03-31 15:15:50 -07008 def consistentCheck( self ):
9 """
10 Checks that TestON counters are consistent across all nodes.
Jon Halle1a3b752015-07-22 13:02:46 -070011
Jon Halla440e872016-03-31 15:15:50 -070012 Returns the tuple (onosCounters, consistent)
13 - onosCounters is the parsed json output of the counters command on all nodes
14 - consistent is main.TRUE if all "TestON" counters are consitent across all
15 nodes or main.FALSE
16 """
Jon Halle1a3b752015-07-22 13:02:46 -070017 try:
Jon Halla440e872016-03-31 15:15:50 -070018 correctResults = main.TRUE
19 # Get onos counters results
20 onosCountersRaw = []
21 threads = []
22 for i in main.activeNodes:
23 t = main.Thread( target=utilities.retry,
24 name="counters-" + str( i ),
25 args=[ main.CLIs[i].counters, [ None ] ],
26 kwargs= { 'sleep': 5, 'attempts': 5,
27 'randomTime': True } )
28 threads.append( t )
29 t.start()
30 for t in threads:
31 t.join()
32 onosCountersRaw.append( t.result )
33 onosCounters = []
34 for i in range( len( main.activeNodes ) ):
35 try:
36 onosCounters.append( json.loads( onosCountersRaw[i] ) )
37 except ( ValueError, TypeError ):
38 main.log.error( "Could not parse counters response from ONOS" +
39 str( main.activeNodes[i] + 1 ) )
40 main.log.warn( repr( onosCountersRaw[ i ] ) )
41 onosCounters.append( [] )
42
43 testCounters = {}
44 # make a list of all the "TestON-*" counters in ONOS
45 # lookes like a dict whose keys are the name of the ONOS node and values
46 # are a list of the counters. I.E.
47 # { "ONOS1": [ { "name":"TestON-Partitions","value":56} ]
48 # }
49 # NOTE: There is an assumtion that all nodes are active
50 # based on the above for loops
51 for controller in enumerate( onosCounters ):
52 for key, value in controller[1].iteritems():
53 if 'TestON' in key:
54 node = 'ONOS' + str( controller[0] + 1 )
55 try:
56 testCounters[node].append( { key: value } )
57 except KeyError:
58 testCounters[node] = [ { key: value } ]
59 # compare the counters on each node
60 firstV = testCounters.values()[0]
61 tmp = [ v == firstV for k, v in testCounters.iteritems() ]
62 if all( tmp ):
63 consistent = main.TRUE
64 else:
65 consistent = main.FALSE
66 main.log.error( "ONOS nodes have different values for counters:\n" +
67 testCounters )
68 return ( onosCounters, consistent )
69 except Exception:
70 main.log.exception( "" )
71 main.cleanup()
72 main.exit()
73
74 def counterCheck( self, counterName, counterValue ):
75 """
76 Checks that TestON counters are consistent across all nodes and that
77 specified counter is in ONOS with the given value
78 """
79 try:
80 correctResults = main.TRUE
81 # Get onos counters results and consistentCheck
82 onosCounters, consistent = self.consistentCheck()
83 # Check for correct values
84 for i in range( len( main.activeNodes ) ):
85 current = onosCounters[i]
86 onosValue = None
87 try:
88 onosValue = current.get( counterName )
89 except AttributeError, e:
90 node = str( main.activeNodes[i] + 1 )
91 main.log.error( "ONOS" + node + " counters result " +
92 "is not as expected" )
93 correctResults = main.FALSE
94 if onosValue == counterValue:
95 main.log.info( counterName + " counter value is correct" )
96 else:
97 main.log.error( counterName + " counter value is incorrect," +
98 " expected value: " + str( counterValue )
99 + " current value: " + str( onosValue ) )
100 correctResults = main.FALSE
101 return consistent and correctResults
102 except Exception:
103 main.log.exception( "" )
104 main.cleanup()
105 main.exit()