Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 1 | """ |
| 2 | Functions for the vpls tests |
| 3 | """ |
| 4 | import time |
| 5 | import json |
| 6 | |
Jon Hall | ed25823 | 2017-05-24 17:30:18 -0700 | [diff] [blame^] | 7 | |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 8 | def sanitizeConfig( config ): |
| 9 | """ |
| 10 | Take a python json object for vpls config and normalize it. |
| 11 | Things it does: |
| 12 | Converts all strings to the same format |
| 13 | Make sure each network has an encapsulation key:value |
| 14 | Makes sure encapsulation type is all uppercase |
| 15 | Make sure an empty list of interfaces is formated consistently |
| 16 | Sorts the list of interfaces |
Jon Hall | 0dccfa5 | 2017-05-18 11:21:11 -0700 | [diff] [blame] | 17 | Sorts the list of networks |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 18 | """ |
| 19 | # Convert to same string formats |
| 20 | config = json.loads( json.dumps( config ) ) |
| 21 | for network in config: |
| 22 | encap = network.get( 'encapsulation', None ) |
| 23 | if encap is None: |
| 24 | encap = "NONE" |
| 25 | network[ 'encapsulation' ] = encap.upper() |
| 26 | ifaces = network.get( 'interfaces' ) |
Jon Hall | ed25823 | 2017-05-24 17:30:18 -0700 | [diff] [blame^] | 27 | if ifaces == [ '' ]: |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 28 | ifaces = [] |
| 29 | else: |
| 30 | ifaces = sorted( ifaces ) |
Jon Hall | ed25823 | 2017-05-24 17:30:18 -0700 | [diff] [blame^] | 31 | network[ 'interfaces' ] = ifaces |
| 32 | config = sorted( config, key=lambda k: k[ 'name' ] ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 33 | return config |
| 34 | |
Jon Hall | ed25823 | 2017-05-24 17:30:18 -0700 | [diff] [blame^] | 35 | |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 36 | def verify( main ): |
| 37 | """ |
| 38 | Runs some tests to verify the vpls configurations. |
| 39 | - Compare sent vpls network configuration to what is stored in each: |
| 40 | - ONOS network configuration |
| 41 | - ONOS VPLS application configuration |
| 42 | - Ping between each pair of hosts to check connectivity |
| 43 | |
| 44 | NOTE: This requires the expected/sent network config json for the vpls |
| 45 | application be stored in main.vplsConfig |
| 46 | """ |
| 47 | # Variables |
Jon Hall | ed25823 | 2017-05-24 17:30:18 -0700 | [diff] [blame^] | 48 | app = main.params[ 'vpls' ][ 'name' ] |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 49 | pprint = main.ONOSrest1.pprint |
Jon Hall | ed25823 | 2017-05-24 17:30:18 -0700 | [diff] [blame^] | 50 | SLEEP = int( main.params[ 'SLEEP' ][ 'netcfg' ] ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 51 | |
| 52 | main.step( "Check network configurations for vpls application" ) |
| 53 | clusterResult = True |
| 54 | for node in main.RESTs: |
| 55 | result = False |
| 56 | getVPLS = node.getNetCfg( subjectClass="apps", |
| 57 | subjectKey=app ) |
| 58 | onosCfg = json.loads( getVPLS ).get( 'vpls' ).get( 'vplsList' ) |
| 59 | onosCfg = pprint( sanitizeConfig( onosCfg ) ) |
| 60 | sentCfg = pprint( sanitizeConfig( main.vplsConfig ) ) |
| 61 | result = onosCfg == sentCfg |
| 62 | if result: |
| 63 | main.log.info( "ONOS NetCfg matches what was sent" ) |
| 64 | else: |
| 65 | clusterResult = False |
| 66 | main.log.error( "ONOS NetCfg doesn't match what was sent" ) |
| 67 | main.log.debug( "ONOS config: {}".format( onosCfg ) ) |
| 68 | main.log.debug( "Sent config: {}".format( sentCfg ) ) |
| 69 | utilities.assert_equals( expect=True, |
| 70 | actual=clusterResult, |
| 71 | onpass="Net Cfg added for vpls", |
| 72 | onfail="Net Cfg not added for vpls" ) |
| 73 | |
| 74 | main.step( "Check vpls app configurations" ) |
| 75 | clusterResult = True |
| 76 | for node in main.CLIs: |
| 77 | result = False |
| 78 | #TODO Read from vpls show and match to pushed json |
| 79 | vpls = node.parseVplsShow() |
| 80 | parsedVpls = pprint( sanitizeConfig( vpls ) ) |
| 81 | sentVpls = pprint( sanitizeConfig( main.vplsConfig ) ) |
| 82 | result = parsedVpls == sentVpls |
| 83 | if result: |
| 84 | main.log.info( "VPLS config matches sent NetCfg" ) |
| 85 | else: |
| 86 | clusterResult = False |
| 87 | main.log.error( "VPLS config doesn't match sent NetCfg" ) |
| 88 | main.log.debug( "ONOS config: {}".format( parsedVpls ) ) |
| 89 | main.log.debug( "Sent config: {}".format( sentVpls ) ) |
| 90 | utilities.assert_equals( expect=True, |
| 91 | actual=clusterResult, |
| 92 | onpass="VPLS successfully configured", |
| 93 | onfail="VPLS not configured correctly" ) |
| 94 | |
| 95 | # FIXME This doesn't work, some will be withdrawn if interfaces are removed |
| 96 | # TODO: if encapsulation is set, look for that |
| 97 | # TODO: can we look at the intent keys? |
| 98 | """ |
| 99 | main.step( "Check intent states" ) |
| 100 | # Print the intent states |
Jon Hall | ed25823 | 2017-05-24 17:30:18 -0700 | [diff] [blame^] | 101 | intents = main.CLIs[ 0 ].intents() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 102 | count = 0 |
| 103 | while count <= 5: |
| 104 | installedCheck = True |
| 105 | try: |
| 106 | for intent in json.loads( intents ): |
| 107 | state = intent.get( 'state', None ) |
| 108 | if "INSTALLED" not in state: |
| 109 | installedCheck = False |
| 110 | except ( ValueError, TypeError ): |
| 111 | main.log.exception( "Error parsing intents" ) |
| 112 | if installedCheck: |
| 113 | break |
| 114 | count += 1 |
| 115 | utilities.assert_equals( expect=True, |
| 116 | actual=installedCheck , |
| 117 | onpass="All Intents in installed state", |
| 118 | onfail="Not all Intents in installed state" ) |
| 119 | """ |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 120 | main.step( "Check connectivity" ) |
| 121 | connectivityCheck = True |
Jon Hall | ed25823 | 2017-05-24 17:30:18 -0700 | [diff] [blame^] | 122 | hosts = int( main.params[ 'vpls' ][ 'hosts' ] ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 123 | networks = [] |
| 124 | for network in main.vplsConfig: |
| 125 | nodes = network.get( 'interfaces', None ) |
| 126 | if nodes: |
| 127 | networks.append( nodes ) |
| 128 | for i in range( 1, hosts + 1 ): |
| 129 | src = "h" + str( i ) |
| 130 | for j in range( 1, hosts + 1 ): |
| 131 | if j == i: |
| 132 | continue |
| 133 | dst = "h" + str( j ) |
| 134 | pingResult = main.Mininet1.pingHost( SRC=src, TARGET=dst ) |
| 135 | expected = main.FALSE |
| 136 | for network in networks: |
| 137 | if src in network and dst in network: |
| 138 | expected = main.TRUE |
| 139 | break |
| 140 | if pingResult != expected: |
| 141 | connectivityCheck = False |
| 142 | main.log.error( "%s <-> %s: %s; Expected: %s" % |
| 143 | ( src, dst, pingResult, expected ) ) |
| 144 | utilities.assert_equals( expect=True, |
| 145 | actual=connectivityCheck, |
| 146 | onpass="Connectivity is as expected", |
| 147 | onfail="Connectivity is not as expected" ) |