blob: 9e5404aab5210cf68e6fe357e3aeae2109defd69 [file] [log] [blame]
Jon Hall66e001c2015-11-12 09:45:10 -08001"""
2These functions are for use with the Network config system
3"""
4import time
5
Jon Hallbc080f92017-05-24 16:29:55 -07006
Jon Hall66e001c2015-11-12 09:45:10 -08007def compareCfg( main, gossipTime=None ):
8 """
9 Compare the network configurations across all nodes in the network
10 gossipTime is the number of seconds each gossip round take for the netCfg maps
11 """
12 main.step( "Check net config" )
13 if gossipTime:
Devin Lim58046fa2017-07-05 16:55:00 -070014 time.sleep( gossipTime * len( main.RESTs ) )
Jon Hall66e001c2015-11-12 09:45:10 -080015 responses = []
Jeremy Ronquillo4a30ffe2017-06-07 11:36:35 -070016 result = utilities.retry( f=checkNodeResponses,
17 retValue=False,
18 kwargs={'main' : main,'responses' : responses},
19 sleep = main.retrysleep,
20 attempts = main.retrytimes )
21 utilities.assert_equals( expect=True,
22 actual=result,
23 onpass="Net Cfg is the same on all nodes",
24 onfail="Check Net Cfg failed. Check above messages." )
25
26
27def checkNodeResponses ( main, responses ):
28 numberOfFailedNodes = 0 # Tracks the number of nodes that failed to get net configuration
Devin Lim58046fa2017-07-05 16:55:00 -070029 for node in main.RESTs:
Jeremy Ronquillo4a30ffe2017-06-07 11:36:35 -070030 response = node.getNetCfg( )
Jon Hall66e001c2015-11-12 09:45:10 -080031 responses.append( node.pprint( response ) )
32 if response == main.FALSE:
Jeremy Ronquillo4a30ffe2017-06-07 11:36:35 -070033 numberOfFailedNodes += 1
34
Jon Hallbc080f92017-05-24 16:29:55 -070035 compare = [ i == responses[ 0 ] for i in responses ]
Jeremy Ronquillo4a30ffe2017-06-07 11:36:35 -070036 if numberOfFailedNodes == 0 and all( compare ):
37 return True
38
39 # Failed, providing feedback on cli
40 if numberOfFailedNodes > 0:
41 main.log.warn( numberOfFailedNodes + " node(s) failed to GET Net Config" )
Jon Hall66e001c2015-11-12 09:45:10 -080042 if not all( compare ):
Jeremy Ronquillo4a30ffe2017-06-07 11:36:35 -070043 main.log.debug( "Net Cfg is different on some nodes. Net Config results:" )
Jon Hall66e001c2015-11-12 09:45:10 -080044 for i in responses:
45 main.log.debug( i )
Jeremy Ronquillo4a30ffe2017-06-07 11:36:35 -070046 return False
Jeremy Ronquillo4cf537d2017-06-26 11:20:52 -070047
48def checkDeviceAnnotations( main, jsonObj, sw ):
49 id = str( sw.get( 'id' ) )
50 keys = [ 'name', 'owner', 'rackAddress' ]
51 correct = True
52 for k in keys:
53 if str( sw.get( 'annotations', {} ).get( k ) ) != str( jsonObj[ k ] ) :
54 correct = False
55 main.log.debug( "{} is wrong on switch: ".format( k ) + id )
56 if not correct:
57 main.log.error( "Annotations for switch " + id + " are incorrect: {}".format( sw ) )
58 return correct
59
60
61def checkAllDeviceAnnotations( main, json ):
62 devices = main.ONOSrest1.devices( )
63 id = "of:0000000000000001"
64 i = 1
65 result = [ ]
66 try:
67 for sw in json.loads( devices ):
68 if id in sw.get( 'id' ):
69 jsonObj = getattr( main, "s" + str( i ) + "Json" )
70 isDeviceAnnotationCorrect = checkDeviceAnnotations( main, jsonObj, sw )
71 result.append( isDeviceAnnotationCorrect )
72 i += 1
73 id = "of:000000000000000" + str( i )
74 except( TypeError, ValueError ):
75 main.log.error( "Problem loading device" )
76 return False
77 return all( result )