Jon Hall | 66e001c | 2015-11-12 09:45:10 -0800 | [diff] [blame] | 1 | """ |
| 2 | These functions are for use with the Network config system |
| 3 | """ |
| 4 | import time |
| 5 | |
Jon Hall | bc080f9 | 2017-05-24 16:29:55 -0700 | [diff] [blame] | 6 | |
Jon Hall | 66e001c | 2015-11-12 09:45:10 -0800 | [diff] [blame] | 7 | def 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 Lim | 58046fa | 2017-07-05 16:55:00 -0700 | [diff] [blame] | 14 | time.sleep( gossipTime * len( main.RESTs ) ) |
Jon Hall | 66e001c | 2015-11-12 09:45:10 -0800 | [diff] [blame] | 15 | responses = [] |
Jeremy Ronquillo | 4a30ffe | 2017-06-07 11:36:35 -0700 | [diff] [blame] | 16 | 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 | |
| 27 | def checkNodeResponses ( main, responses ): |
| 28 | numberOfFailedNodes = 0 # Tracks the number of nodes that failed to get net configuration |
Devin Lim | 58046fa | 2017-07-05 16:55:00 -0700 | [diff] [blame] | 29 | for node in main.RESTs: |
Jeremy Ronquillo | 4a30ffe | 2017-06-07 11:36:35 -0700 | [diff] [blame] | 30 | response = node.getNetCfg( ) |
Jon Hall | 66e001c | 2015-11-12 09:45:10 -0800 | [diff] [blame] | 31 | responses.append( node.pprint( response ) ) |
| 32 | if response == main.FALSE: |
Jeremy Ronquillo | 4a30ffe | 2017-06-07 11:36:35 -0700 | [diff] [blame] | 33 | numberOfFailedNodes += 1 |
| 34 | |
Jon Hall | bc080f9 | 2017-05-24 16:29:55 -0700 | [diff] [blame] | 35 | compare = [ i == responses[ 0 ] for i in responses ] |
Jeremy Ronquillo | 4a30ffe | 2017-06-07 11:36:35 -0700 | [diff] [blame] | 36 | 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 Hall | 66e001c | 2015-11-12 09:45:10 -0800 | [diff] [blame] | 42 | if not all( compare ): |
Jeremy Ronquillo | 4a30ffe | 2017-06-07 11:36:35 -0700 | [diff] [blame] | 43 | main.log.debug( "Net Cfg is different on some nodes. Net Config results:" ) |
Jon Hall | 66e001c | 2015-11-12 09:45:10 -0800 | [diff] [blame] | 44 | for i in responses: |
| 45 | main.log.debug( i ) |
Jeremy Ronquillo | 4a30ffe | 2017-06-07 11:36:35 -0700 | [diff] [blame] | 46 | return False |
Jeremy Ronquillo | 4cf537d | 2017-06-26 11:20:52 -0700 | [diff] [blame] | 47 | |
| 48 | def 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 | |
| 61 | def 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 ) |