Jon Hall | 66e001c | 2015-11-12 09:45:10 -0800 | [diff] [blame] | 1 | |
| 2 | # Testing the basic intent functionality of ONOS |
| 3 | |
| 4 | class FUNCnetCfg: |
| 5 | |
| 6 | def __init__( self ): |
| 7 | self.default = '' |
| 8 | |
| 9 | def CASE1( self, main ): |
| 10 | import imp |
| 11 | import re |
| 12 | |
| 13 | """ |
| 14 | - Construct tests variables |
| 15 | - GIT ( optional ) |
| 16 | - Checkout ONOS master branch |
| 17 | - Pull latest ONOS code |
| 18 | - Building ONOS ( optional ) |
| 19 | - Install ONOS package |
| 20 | - Build ONOS package |
| 21 | """ |
| 22 | |
| 23 | main.case( "Constructing test variables and building ONOS package" ) |
| 24 | main.step( "Constructing test variables" ) |
| 25 | main.caseExplanation = "This test case is mainly for loading " +\ |
| 26 | "from params file, and pull and build the " +\ |
| 27 | " latest ONOS package" |
| 28 | stepResult = main.FALSE |
| 29 | |
| 30 | # Test variables |
| 31 | try: |
| 32 | main.testOnDirectory = re.sub( "(/tests)$", "", main.testDir ) |
| 33 | main.apps = main.params[ 'ENV' ][ 'cellApps' ] |
| 34 | gitBranch = main.params[ 'GIT' ][ 'branch' ] |
| 35 | main.dependencyPath = main.testOnDirectory + \ |
| 36 | main.params[ 'DEPENDENCY' ][ 'path' ] |
| 37 | if main.ONOSbench.maxNodes: |
| 38 | main.maxNodes = int( main.ONOSbench.maxNodes ) |
| 39 | else: |
| 40 | main.maxNodes = 0 |
| 41 | wrapperFile1 = main.params[ 'DEPENDENCY' ][ 'wrapper1' ] |
| 42 | wrapperFile2 = main.params[ 'DEPENDENCY' ][ 'wrapper2' ] |
| 43 | wrapperFile3 = main.params[ 'DEPENDENCY' ][ 'wrapper3' ] |
| 44 | main.startUpSleep = int( main.params[ 'SLEEP' ][ 'startup' ] ) |
| 45 | main.gossipTime = int( main.params[ 'SLEEP'][ 'cfgGossip' ] ) |
| 46 | gitPull = main.params[ 'GIT' ][ 'pull' ] |
| 47 | main.cellData = {} # for creating cell file |
| 48 | main.hostsData = {} |
| 49 | main.nodes = [] |
| 50 | main.ONOSip = [] |
| 51 | |
| 52 | main.ONOSip = main.ONOSbench.getOnosIps() |
| 53 | |
| 54 | # Assigning ONOS cli handles to a list |
| 55 | try: |
| 56 | for i in range( 1, main.maxNodes + 1 ): |
| 57 | main.nodes.append( getattr( main, 'ONOSrest' + str( i ) ) ) |
| 58 | except AttributeError: |
| 59 | main.log.warn( "A " + str( main.maxNodes ) + " node cluster " + |
| 60 | "was defined in env variables, but only " + |
| 61 | str( len( main.nodes ) ) + |
| 62 | " nodes were defined in the .topo file. " + |
| 63 | "Using " + str( len( main.nodes ) ) + |
| 64 | " nodes for the test." ) |
| 65 | |
| 66 | main.numCtrls = len( main.nodes ) |
| 67 | |
| 68 | # -- INIT SECTION, SHOULD ONLY BE RUN ONCE -- # |
| 69 | main.startUp = imp.load_source( wrapperFile1, |
| 70 | main.dependencyPath + |
| 71 | wrapperFile1 + |
| 72 | ".py" ) |
| 73 | |
| 74 | main.netCfg = imp.load_source( wrapperFile2, |
| 75 | main.dependencyPath + |
| 76 | wrapperFile2 + |
| 77 | ".py" ) |
| 78 | |
| 79 | main.topo = imp.load_source( wrapperFile3, |
| 80 | main.dependencyPath + |
| 81 | wrapperFile3 + |
| 82 | ".py" ) |
| 83 | |
| 84 | if main.nodes: |
| 85 | stepResult = main.TRUE |
| 86 | else: |
| 87 | main.log.error( "Did not properly created list of ONOS handle" ) |
| 88 | stepResult = main.FALSE |
| 89 | except Exception as e: |
| 90 | main.log.exception(e) |
| 91 | main.cleanup() |
| 92 | main.exit() |
| 93 | |
| 94 | utilities.assert_equals( expect=main.TRUE, |
| 95 | actual=stepResult, |
| 96 | onpass="Successfully construct " + |
| 97 | "test variables ", |
| 98 | onfail="Failed to construct test variables" ) |
| 99 | |
| 100 | if gitPull == 'True': |
| 101 | main.step( "Building ONOS in " + gitBranch + " branch" ) |
| 102 | onosBuildResult = main.startUp.onosBuild( main, gitBranch ) |
| 103 | stepResult = onosBuildResult |
| 104 | utilities.assert_equals( expect=main.TRUE, |
| 105 | actual=stepResult, |
| 106 | onpass="Successfully compiled " + |
| 107 | "latest ONOS", |
| 108 | onfail="Failed to compile " + |
| 109 | "latest ONOS" ) |
| 110 | else: |
| 111 | main.log.warn( "Did not pull new code so skipping mvn " + |
| 112 | "clean install" ) |
| 113 | main.ONOSbench.getVersion( report=True ) |
| 114 | |
| 115 | def CASE2( self, main ): |
| 116 | """ |
| 117 | - Set up cell |
| 118 | - Create cell file |
| 119 | - Set cell file |
| 120 | - Verify cell file |
| 121 | - Kill ONOS process |
| 122 | - Uninstall ONOS cluster |
| 123 | - Verify ONOS start up |
| 124 | - Install ONOS cluster |
| 125 | - Connect to cli |
| 126 | """ |
| 127 | import time |
| 128 | main.case( "Starting up " + str( main.numCtrls ) + |
| 129 | " node(s) ONOS cluster" ) |
| 130 | main.caseExplanation = "Set up ONOS with " + str( main.numCtrls ) +\ |
| 131 | " node(s) ONOS cluster" |
| 132 | |
| 133 | # kill off all onos processes |
| 134 | main.log.info( "Safety check, killing all ONOS processes" + |
| 135 | " before initiating environment setup" ) |
| 136 | |
| 137 | for i in range( main.maxNodes ): |
| 138 | main.ONOSbench.onosStop( main.ONOSip[ i ] ) |
| 139 | main.ONOSbench.onosDie( main.ONOSip[ i ] ) |
| 140 | |
| 141 | tempOnosIp = [] |
| 142 | for i in range( main.numCtrls ): |
| 143 | tempOnosIp.append( main.ONOSip[i] ) |
| 144 | |
| 145 | main.ONOSbench.createCellFile( main.ONOSbench.ip_address, |
| 146 | "temp", main.Mininet1.ip_address, |
| 147 | main.apps, tempOnosIp ) |
| 148 | |
| 149 | main.step( "Apply cell to environment" ) |
| 150 | cellResult = main.ONOSbench.setCell( "temp" ) |
| 151 | verifyResult = main.ONOSbench.verifyCell() |
| 152 | stepResult = cellResult and verifyResult |
| 153 | utilities.assert_equals( expect=main.TRUE, |
| 154 | actual=stepResult, |
| 155 | onpass="Successfully applied cell to environment", |
| 156 | onfail="Failed to apply cell to environment " ) |
| 157 | |
| 158 | main.step( "Creating ONOS package" ) |
| 159 | packageResult = main.ONOSbench.onosPackage() |
| 160 | stepResult = packageResult |
| 161 | utilities.assert_equals( expect=main.TRUE, |
| 162 | actual=stepResult, |
| 163 | onpass="Successfully created ONOS package", |
| 164 | onfail="Failed to create ONOS package" ) |
| 165 | |
| 166 | time.sleep( main.startUpSleep ) |
| 167 | main.step( "Uninstalling ONOS package" ) |
| 168 | onosUninstallResult = main.TRUE |
| 169 | for ip in main.ONOSip: |
| 170 | onosUninstallResult = onosUninstallResult and \ |
| 171 | main.ONOSbench.onosUninstall( nodeIp=ip ) |
| 172 | stepResult = onosUninstallResult |
| 173 | utilities.assert_equals( expect=main.TRUE, |
| 174 | actual=stepResult, |
| 175 | onpass="Successfully uninstalled ONOS package", |
| 176 | onfail="Failed to uninstall ONOS package" ) |
| 177 | |
| 178 | time.sleep( main.startUpSleep ) |
| 179 | main.step( "Installing ONOS package" ) |
| 180 | onosInstallResult = main.TRUE |
| 181 | for i in range( main.numCtrls ): |
| 182 | onosInstallResult = onosInstallResult and \ |
| 183 | main.ONOSbench.onosInstall( node=main.ONOSip[ i ] ) |
| 184 | stepResult = onosInstallResult |
| 185 | utilities.assert_equals( expect=main.TRUE, |
| 186 | actual=stepResult, |
| 187 | onpass="Successfully installed ONOS package", |
| 188 | onfail="Failed to install ONOS package" ) |
| 189 | |
| 190 | time.sleep( main.startUpSleep ) |
| 191 | main.step( "Starting ONOS service" ) |
| 192 | stopResult = main.TRUE |
| 193 | startResult = main.TRUE |
| 194 | onosIsUp = main.TRUE |
| 195 | |
| 196 | for i in range( main.numCtrls ): |
Jeremy Songster | 5c3fdb4 | 2016-04-27 12:19:27 -0700 | [diff] [blame] | 197 | isUp = main.ONOSbench.isup( main.ONOSip[ i ] ) |
| 198 | onosIsUp = onosIsUp and isUp |
| 199 | if isUp == main.TRUE: |
| 200 | main.log.report( "ONOS instance {0} is up and ready".format( i + 1 ) ) |
| 201 | else: |
| 202 | main.log.report( "ONOS instance {0} may not be up, stop and ".format( i + 1 ) + |
| 203 | "start ONOS again " ) |
Jon Hall | 66e001c | 2015-11-12 09:45:10 -0800 | [diff] [blame] | 204 | stopResult = stopResult and \ |
| 205 | main.ONOSbench.onosStop( main.ONOSip[ i ] ) |
Jon Hall | 66e001c | 2015-11-12 09:45:10 -0800 | [diff] [blame] | 206 | startResult = startResult and \ |
| 207 | main.ONOSbench.onosStart( main.ONOSip[ i ] ) |
Jeremy Songster | 5c3fdb4 | 2016-04-27 12:19:27 -0700 | [diff] [blame] | 208 | stepResult = onosIsUp and stopResult and startResult |
Jon Hall | 66e001c | 2015-11-12 09:45:10 -0800 | [diff] [blame] | 209 | utilities.assert_equals( expect=main.TRUE, |
| 210 | actual=stepResult, |
| 211 | onpass="ONOS service is ready", |
| 212 | onfail="ONOS service did not start properly" ) |
| 213 | |
| 214 | def CASE8( self, main ): |
| 215 | """ |
| 216 | Compare Topo |
| 217 | """ |
| 218 | import json |
| 219 | |
| 220 | main.case( "Compare ONOS Topology view to Mininet topology" ) |
| 221 | main.caseExplanation = "Compare topology elements between Mininet" +\ |
| 222 | " and ONOS" |
| 223 | |
| 224 | main.step( "Gathering topology information" ) |
| 225 | # TODO: add a parameterized sleep here |
| 226 | devicesResults = main.TRUE |
| 227 | linksResults = main.TRUE |
| 228 | hostsResults = main.TRUE |
| 229 | devices = main.topo.getAllDevices( main ) |
| 230 | hosts = main.topo.getAllHosts( main ) |
| 231 | ports = main.topo.getAllPorts( main ) |
| 232 | links = main.topo.getAllLinks( main ) |
| 233 | clusters = main.topo.getAllClusters( main ) |
| 234 | |
| 235 | mnSwitches = main.Mininet1.getSwitches() |
| 236 | mnLinks = main.Mininet1.getLinks() |
| 237 | mnHosts = main.Mininet1.getHosts() |
| 238 | |
| 239 | main.step( "Comparing MN topology to ONOS topology" ) |
| 240 | for controller in range( main.numCtrls ): |
| 241 | controllerStr = str( controller + 1 ) |
| 242 | if devices[ controller ] and ports[ controller ] and\ |
| 243 | "Error" not in devices[ controller ] and\ |
| 244 | "Error" not in ports[ controller ]: |
| 245 | |
| 246 | currentDevicesResult = main.Mininet1.compareSwitches( |
| 247 | mnSwitches, |
| 248 | json.loads( devices[ controller ] ), |
| 249 | json.loads( ports[ controller ] ) ) |
| 250 | else: |
| 251 | currentDevicesResult = main.FALSE |
| 252 | utilities.assert_equals( expect=main.TRUE, |
| 253 | actual=currentDevicesResult, |
| 254 | onpass="ONOS" + controllerStr + |
| 255 | " Switches view is correct", |
| 256 | onfail="ONOS" + controllerStr + |
| 257 | " Switches view is incorrect" ) |
| 258 | |
| 259 | if links[ controller ] and "Error" not in links[ controller ]: |
| 260 | currentLinksResult = main.Mininet1.compareLinks( |
| 261 | mnSwitches, mnLinks, |
| 262 | json.loads( links[ controller ] ) ) |
| 263 | else: |
| 264 | currentLinksResult = main.FALSE |
| 265 | utilities.assert_equals( expect=main.TRUE, |
| 266 | actual=currentLinksResult, |
| 267 | onpass="ONOS" + controllerStr + |
| 268 | " links view is correct", |
| 269 | onfail="ONOS" + controllerStr + |
| 270 | " links view is incorrect" ) |
| 271 | |
| 272 | if hosts[ controller ] or "Error" not in hosts[ controller ]: |
| 273 | currentHostsResult = main.Mininet1.compareHosts( |
| 274 | mnHosts, |
| 275 | json.loads( hosts[ controller ] ) ) |
| 276 | else: |
| 277 | currentHostsResult = main.FALSE |
| 278 | utilities.assert_equals( expect=main.TRUE, |
| 279 | actual=currentHostsResult, |
| 280 | onpass="ONOS" + controllerStr + |
| 281 | " hosts exist in Mininet", |
| 282 | onfail="ONOS" + controllerStr + |
| 283 | " hosts don't match Mininet" ) |
| 284 | |
| 285 | def CASE9( self, main ): |
| 286 | ''' |
| 287 | Report errors/warnings/exceptions |
| 288 | ''' |
| 289 | main.log.info( "Error report: \n" ) |
| 290 | main.ONOSbench.logReport( |
| 291 | globalONOSip[0], |
| 292 | [ "INFO", "WARN", "ERROR" , "Except" ], |
| 293 | "s" ) |
| 294 | # main.ONOSbench.logReport( globalONOSip[1], [ "INFO" ], "d" ) |
| 295 | |
| 296 | def CASE10( self, main ): |
| 297 | """ |
| 298 | Start Mininet topology with OF 1.0 switches |
| 299 | """ |
| 300 | main.OFProtocol = "1.0" |
| 301 | main.log.report( "Start Mininet topology with OF 1.0 switches" ) |
| 302 | main.case( "Start Mininet topology with OF 1.0 switches" ) |
| 303 | main.caseExplanation = "Start mininet topology with OF 1.0 " +\ |
| 304 | "switches to test intents, exits out if " +\ |
| 305 | "topology did not start correctly" |
| 306 | |
| 307 | main.step( "Starting Mininet topology with OF 1.0 switches" ) |
Jon Hall | 53c5e66 | 2016-04-13 16:06:56 -0700 | [diff] [blame] | 308 | args = "--controller none --switch ovs,protocols=OpenFlow10" |
Jon Hall | 66e001c | 2015-11-12 09:45:10 -0800 | [diff] [blame] | 309 | switches = int( main.params['MININET']['switch'] ) |
| 310 | cmd = "mn --topo linear,{} {}".format( switches, args ) |
| 311 | topoResult = main.Mininet1.startNet( mnCmd = cmd ) |
| 312 | stepResult = topoResult |
| 313 | utilities.assert_equals( expect=main.TRUE, |
| 314 | actual=stepResult, |
| 315 | onpass="Successfully loaded topology", |
| 316 | onfail="Failed to load topology" ) |
| 317 | # Exit if topology did not load properly |
| 318 | if not topoResult: |
| 319 | main.cleanup() |
| 320 | main.exit() |
| 321 | |
| 322 | def CASE11( self, main ): |
| 323 | """ |
| 324 | Start Mininet topology with OF 1.3 switches |
| 325 | """ |
| 326 | import re |
| 327 | main.OFProtocol = "1.3" |
| 328 | main.log.report( "Start Mininet topology with OF 1.3 switches" ) |
| 329 | main.case( "Start Mininet topology with OF 1.3 switches" ) |
| 330 | main.caseExplanation = "Start mininet topology with OF 1.3 " +\ |
| 331 | "switches to test intents, exits out if " +\ |
| 332 | "topology did not start correctly" |
| 333 | |
| 334 | main.step( "Starting Mininet topology with OF 1.3 switches" ) |
Jon Hall | 53c5e66 | 2016-04-13 16:06:56 -0700 | [diff] [blame] | 335 | args = "--controller none --switch ovs,protocols=OpenFlow13" |
Jon Hall | 66e001c | 2015-11-12 09:45:10 -0800 | [diff] [blame] | 336 | switches = int( main.params['MININET']['switch'] ) |
| 337 | cmd = "mn --topo linear,{} {}".format( switches, args ) |
| 338 | topoResult = main.Mininet1.startNet( mnCmd = cmd ) |
| 339 | stepResult = topoResult |
| 340 | utilities.assert_equals( expect=main.TRUE, |
| 341 | actual=stepResult, |
| 342 | onpass="Successfully loaded topology", |
| 343 | onfail="Failed to load topology" ) |
| 344 | # Exit if topology did not load properly |
| 345 | if not topoResult: |
| 346 | main.cleanup() |
| 347 | main.exit() |
| 348 | |
| 349 | tempONOSip = [] |
| 350 | for i in range( main.numCtrls ): |
| 351 | tempONOSip.append( main.ONOSip[ i ] ) |
| 352 | |
| 353 | swList = [ "s" + str( i ) for i in range( 1, switches + 1 ) ] |
| 354 | assignResult = main.Mininet1.assignSwController( sw=swList, |
| 355 | ip=tempONOSip, |
| 356 | port='6653' ) |
| 357 | if not assignResult: |
| 358 | main.cleanup() |
| 359 | main.exit() |
| 360 | |
| 361 | assignResult = main.TRUE |
| 362 | for sw in swList: |
| 363 | response = main.Mininet1.getSwController( "s" + str( i ) ) |
| 364 | main.log.info( "Response is " + str( response ) ) |
| 365 | for ip in tempONOSip: |
| 366 | if re.search( "tcp:" + ip, response ): |
| 367 | assignResult = assignResult and main.TRUE |
| 368 | else: |
| 369 | assignResult = assignResult and main.FALSE |
| 370 | stepResult = assignResult |
| 371 | utilities.assert_equals( expect=main.TRUE, |
| 372 | actual=stepResult, |
| 373 | onpass="Successfully assigned switches" + |
| 374 | "to controller", |
| 375 | onfail="Failed to assign switches to " + |
| 376 | "controller" ) |
| 377 | |
| 378 | def CASE14( self, main ): |
| 379 | """ |
| 380 | Stop mininet |
| 381 | """ |
| 382 | main.log.report( "Stop Mininet topology" ) |
| 383 | main.case( "Stop Mininet topology" ) |
| 384 | main.caseExplanation = "Stopping the current mininet topology " +\ |
| 385 | "to start up fresh" |
| 386 | |
| 387 | main.step( "Stopping Mininet Topology" ) |
| 388 | topoResult = main.Mininet1.stopNet( ) |
| 389 | stepResult = topoResult |
| 390 | utilities.assert_equals( expect=main.TRUE, |
| 391 | actual=stepResult, |
| 392 | onpass="Successfully stop mininet", |
| 393 | onfail="Failed to stop mininet" ) |
| 394 | # Exit if topology did not load properly |
| 395 | if not topoResult: |
| 396 | main.cleanup() |
| 397 | main.exit() |
| 398 | |
| 399 | def CASE20( self, main ): |
| 400 | """ |
| 401 | Add some device configurations and then check they are distributed |
| 402 | to all nodes |
| 403 | """ |
| 404 | main.case( "Add Network configurations to the cluster" ) |
| 405 | main.caseExplanation = "Add Network Configurations for devices" +\ |
| 406 | " not discovered yet. One device is allowed" +\ |
| 407 | ", the other disallowed." |
| 408 | pprint = main.nodes[0].pprint |
| 409 | |
| 410 | main.step( "Add Net Cfg for switch1" ) |
| 411 | s1Json = { "rackAddress": 1, |
| 412 | "name": "Switch1", |
| 413 | "owner": "Jimmy", |
| 414 | "allowed": True } |
| 415 | main.s1Json = s1Json |
| 416 | setS1Allow = main.ONOSrest1.setNetCfg( s1Json, |
| 417 | subjectClass="devices", |
| 418 | subjectKey="of:0000000000000001", |
| 419 | configKey="basic" ) |
| 420 | s1Result = False |
| 421 | if setS1Allow: |
| 422 | # Check what we set is what is in ONOS |
| 423 | getS1 = main.ONOSrest1.getNetCfg( subjectClass="devices", |
| 424 | subjectKey="of:0000000000000001", |
| 425 | configKey="basic" ) |
| 426 | onosCfg = pprint( getS1 ) |
| 427 | sentCfg = pprint( s1Json ) |
| 428 | if onosCfg == sentCfg: |
| 429 | s1Result = True |
| 430 | else: |
| 431 | main.log.error( "ONOS NetCfg doesn't match what was sent" ) |
| 432 | main.log.debug( "ONOS config: {}".format( onosCfg ) ) |
| 433 | main.log.debug( "Sent config: {}".format( sentCfg ) ) |
| 434 | utilities.assert_equals( expect=True, |
| 435 | actual=s1Result, |
| 436 | onpass="Net Cfg added for device s1", |
| 437 | onfail="Net Cfg for device s1 not correctly set" ) |
| 438 | |
| 439 | main.step( "Add Net Cfg for switch3" ) |
| 440 | s3Json = { "rackAddress": 3, |
| 441 | "name": "Switch3", |
| 442 | "owner": "Jane", |
| 443 | "allowed": False } |
| 444 | main.s3Json = s3Json |
| 445 | setS3Disallow = main.ONOSrest1.setNetCfg( s3Json, |
| 446 | subjectClass="devices", |
| 447 | subjectKey="of:0000000000000003", |
| 448 | configKey="basic" ) |
| 449 | s3Result = False |
| 450 | if setS3Disallow: |
| 451 | # Check what we set is what is in ONOS |
| 452 | getS3 = main.ONOSrest1.getNetCfg( subjectClass="devices", |
| 453 | subjectKey="of:0000000000000003", |
| 454 | configKey="basic" ) |
| 455 | onosCfg = pprint( getS3 ) |
| 456 | sentCfg = pprint( s3Json ) |
| 457 | if onosCfg == sentCfg: |
| 458 | s3Result = True |
| 459 | else: |
| 460 | main.log.error( "ONOS NetCfg doesn't match what was sent" ) |
| 461 | main.log.debug( "ONOS config: {}".format( onosCfg ) ) |
| 462 | main.log.debug( "Sent config: {}".format( sentCfg ) ) |
| 463 | utilities.assert_equals( expect=True, |
| 464 | actual=s3Result, |
| 465 | onpass="Net Cfg added for device s3", |
| 466 | onfail="Net Cfg for device s3 not correctly set" ) |
| 467 | main.netCfg.compareCfg( main, main.gossipTime ) |
| 468 | |
| 469 | def CASE21( self, main ): |
| 470 | """ |
| 471 | Initial check of devices |
| 472 | """ |
| 473 | import json |
| 474 | try: |
| 475 | assert main.s1Json, "s1Json not defined" |
| 476 | except AssertionError: |
| 477 | main.log.exception( "Case Prerequisites not set: " ) |
| 478 | main.cleanup() |
| 479 | main.exit() |
| 480 | main.case( "Check Devices After they initially connect to ONOS" ) |
| 481 | |
| 482 | main.netCfg.compareCfg( main ) |
| 483 | |
| 484 | main.step( "ONOS should only show devices S1, S2, and S4" ) |
| 485 | devices = main.ONOSrest1.devices() |
| 486 | main.log.debug( main.ONOSrest1.pprint( devices ) ) |
| 487 | allowedDevices = [ "of:{}".format( str( i ).zfill( 16 ) ) for i in [ 1, 2, 4 ] ] |
| 488 | print allowedDevices |
| 489 | onosDevices = [] |
Jeremy Songster | 5c3fdb4 | 2016-04-27 12:19:27 -0700 | [diff] [blame] | 490 | try: |
| 491 | for sw in json.loads( devices ): |
| 492 | onosDevices.append( str( sw['id'] ) ) |
| 493 | onosDevices.sort() |
| 494 | print onosDevices |
| 495 | except( TypeError, ValueError ): |
| 496 | main.log.error( "Problem loading devices" ) |
Jon Hall | 66e001c | 2015-11-12 09:45:10 -0800 | [diff] [blame] | 497 | utilities.assert_equals( expect=allowedDevices, |
| 498 | actual=onosDevices, |
| 499 | onpass="Only allowed devices are in ONOS", |
| 500 | onfail="ONOS devices doesn't match the list" + |
| 501 | " of allowed devices" ) |
| 502 | |
| 503 | main.step( "Check device annotations" ) |
| 504 | keys = [ 'name', 'owner', 'rackAddress' ] |
Jeremy Songster | 5c3fdb4 | 2016-04-27 12:19:27 -0700 | [diff] [blame] | 505 | try: |
| 506 | for sw in json.loads( devices ): |
| 507 | if "of:0000000000000001" in sw['id']: |
| 508 | s1Correct = True |
| 509 | for k in keys: |
| 510 | if str( sw.get( 'annotations', {} ).get( k ) ) != str( main.s1Json[k] ): |
| 511 | s1Correct = False |
| 512 | main.log.debug( "{} is wrong on s1".format( k ) ) |
| 513 | if not s1Correct: |
| 514 | main.log.error( "Annotations for s1 are incorrect: {}".format( sw ) ) |
| 515 | except( TypeError, ValueError ): |
| 516 | main.log.error( "Problem loading devices" ) |
| 517 | s1Correct = False |
Jon Hall | 66e001c | 2015-11-12 09:45:10 -0800 | [diff] [blame] | 518 | try: |
| 519 | stepResult = s1Correct |
| 520 | except NameError: |
| 521 | stepResult = False |
| 522 | main.log.error( "s1 not found in devices" ) |
| 523 | utilities.assert_equals( expect=True, |
| 524 | actual=stepResult, |
| 525 | onpass="Configured device's annotations are correct", |
| 526 | onfail="Incorrect annotations for configured devices." ) |
| 527 | |
| 528 | def CASE22( self, main ): |
| 529 | """ |
| 530 | Add some device configurations for connected devices and then check |
| 531 | they are distributed to all nodes |
| 532 | """ |
| 533 | main.case( "Add Network configurations for connected devices to the cluster" ) |
| 534 | main.caseExplanation = "Add Network Configurations for discovered " +\ |
| 535 | "devices. One device is allowed" +\ |
| 536 | ", the other disallowed." |
| 537 | pprint = main.nodes[0].pprint |
| 538 | |
| 539 | main.step( "Add Net Cfg for switch2" ) |
| 540 | s2Json = { "rackAddress": 2, |
| 541 | "name": "Switch2", |
| 542 | "owner": "Jenny", |
| 543 | "allowed": True } |
| 544 | main.s2Json = s2Json |
| 545 | setS2Allow = main.ONOSrest2.setNetCfg( s2Json, |
| 546 | subjectClass="devices", |
| 547 | subjectKey="of:0000000000000002", |
| 548 | configKey="basic" ) |
| 549 | s2Result = False |
| 550 | if setS2Allow: |
| 551 | # Check what we set is what is in ONOS |
| 552 | getS2 = main.ONOSrest2.getNetCfg( subjectClass="devices", |
| 553 | subjectKey="of:0000000000000002", |
| 554 | configKey="basic" ) |
| 555 | onosCfg = pprint( getS2 ) |
| 556 | sentCfg = pprint( s2Json ) |
| 557 | if onosCfg == sentCfg: |
| 558 | s2Result = True |
| 559 | else: |
| 560 | main.log.error( "ONOS NetCfg doesn't match what was sent" ) |
| 561 | main.log.debug( "ONOS config: {}".format( onosCfg ) ) |
| 562 | main.log.debug( "Sent config: {}".format( sentCfg ) ) |
| 563 | utilities.assert_equals( expect=True, |
| 564 | actual=s2Result, |
| 565 | onpass="Net Cfg added for device s2", |
| 566 | onfail="Net Cfg for device s2 not correctly set" ) |
| 567 | |
| 568 | main.step( "Add Net Cfg for switch4" ) |
| 569 | s4Json = { "rackAddress": 4, |
| 570 | "name": "Switch4", |
| 571 | "owner": "John", |
| 572 | "allowed": False } |
| 573 | main.s4Json = s4Json |
| 574 | setS4Disallow = main.ONOSrest4.setNetCfg( s4Json, |
| 575 | subjectClass="devices", |
| 576 | subjectKey="of:0000000000000004", |
| 577 | configKey="basic" ) |
| 578 | s4Result = False |
| 579 | if setS4Disallow: |
| 580 | # Check what we set is what is in ONOS |
| 581 | getS4 = main.ONOSrest4.getNetCfg( subjectClass="devices", |
| 582 | subjectKey="of:0000000000000004", |
| 583 | configKey="basic" ) |
| 584 | onosCfg = pprint( getS4 ) |
| 585 | sentCfg = pprint( s4Json ) |
| 586 | if onosCfg == sentCfg: |
| 587 | s4Result = True |
| 588 | else: |
| 589 | main.log.error( "ONOS NetCfg doesn't match what was sent" ) |
| 590 | main.log.debug( "ONOS config: {}".format( onosCfg ) ) |
| 591 | main.log.debug( "Sent config: {}".format( sentCfg ) ) |
| 592 | utilities.assert_equals( expect=True, |
| 593 | actual=s4Result, |
| 594 | onpass="Net Cfg added for device s4", |
| 595 | onfail="Net Cfg for device s3 not correctly set" ) |
| 596 | |
| 597 | main.netCfg.compareCfg( main, main.gossipTime ) |
| 598 | |
| 599 | def CASE23( self, main ): |
| 600 | """ |
| 601 | Check of devices after all Network Configurations are set |
| 602 | """ |
| 603 | import json |
| 604 | try: |
| 605 | assert main.s1Json, "s1Json not defined" |
| 606 | assert main.s2Json, "s2Json not defined" |
| 607 | except AssertionError: |
| 608 | main.log.exception( "Case Prerequisites not set: " ) |
| 609 | main.cleanup() |
| 610 | main.exit() |
| 611 | main.case( "Check Devices after all configurations are set" ) |
| 612 | |
| 613 | main.netCfg.compareCfg( main ) |
| 614 | |
| 615 | main.step( "ONOS should only show devices S1 and S2" ) |
| 616 | devices = main.ONOSrest1.devices() |
| 617 | main.log.debug( main.ONOSrest1.pprint( devices ) ) |
| 618 | allowedDevices = [ "of:{}".format( str( i ).zfill( 16 ) ) for i in [ 1, 2 ] ] |
| 619 | onosDevices = [] |
Jeremy Songster | 5c3fdb4 | 2016-04-27 12:19:27 -0700 | [diff] [blame] | 620 | try: |
| 621 | for sw in json.loads( devices ): |
| 622 | onosDevices.append( str( sw.get( 'id' ) ) ) |
| 623 | onosDevices.sort() |
| 624 | failMsg = "ONOS devices doesn't match the list of allowed devices.\n" |
| 625 | failMsg += "Expected devices: {}\nActual devices: {}".format( allowedDevices, |
| 626 | onosDevices ) |
| 627 | except( TypeError, ValueError ): |
| 628 | main.log.error( "Problem loading devices" ) |
Jon Hall | 66e001c | 2015-11-12 09:45:10 -0800 | [diff] [blame] | 629 | utilities.assert_equals( expect=allowedDevices, |
| 630 | actual=onosDevices, |
| 631 | onpass="Only allowed devices are in ONOS", |
| 632 | onfail=failMsg ) |
| 633 | |
| 634 | main.step( "Check device annotations" ) |
| 635 | keys = [ 'name', 'owner', 'rackAddress' ] |
Jeremy Songster | 5c3fdb4 | 2016-04-27 12:19:27 -0700 | [diff] [blame] | 636 | try: |
| 637 | for sw in json.loads( devices ): |
| 638 | if "of:0000000000000001" in sw.get( 'id' ): |
| 639 | s1Correct = True |
| 640 | for k in keys: |
| 641 | if str( sw.get( 'annotations', {} ).get( k ) ) != str( main.s1Json[k] ): |
| 642 | s1Correct = False |
| 643 | main.log.debug( "{} is wrong on s1".format( k ) ) |
| 644 | if not s1Correct: |
| 645 | main.log.error( "Annotations for s1 are incorrect: {}".format( sw ) ) |
| 646 | elif "of:0000000000000002" in sw['id']: |
| 647 | s2Correct = True |
| 648 | for k in keys: |
| 649 | if str( sw.get( 'annotations', {} ).get( k ) ) != str( main.s2Json[k] ): |
| 650 | s2Correct = False |
| 651 | main.log.debug( "{} is wrong on s2".format( k ) ) |
| 652 | if not s2Correct: |
| 653 | main.log.error( "Annotations for s2 are incorrect: {}".format( sw ) ) |
| 654 | except( TypeError, ValueError ): |
| 655 | main.log.error( "Problem loading devices" ) |
| 656 | stepResult = False |
Jon Hall | 66e001c | 2015-11-12 09:45:10 -0800 | [diff] [blame] | 657 | try: |
| 658 | stepResult = s1Correct and s2Correct |
| 659 | except NameError: |
| 660 | stepResult = False |
| 661 | main.log.error( "s1 and/or s2 not found in devices" ) |
| 662 | utilities.assert_equals( expect=True, |
| 663 | actual=stepResult, |
| 664 | onpass="Configured device's annotations are correct", |
| 665 | onfail="Incorrect annotations for configured devices." ) |
| 666 | |
| 667 | def CASE24( self, main ): |
| 668 | """ |
| 669 | Testing removal of configurations |
| 670 | """ |
Jon Hall | 541b8e0 | 2015-12-14 19:29:01 -0800 | [diff] [blame] | 671 | import time |
Jon Hall | 66e001c | 2015-11-12 09:45:10 -0800 | [diff] [blame] | 672 | try: |
| 673 | assert main.s1Json, "s1Json not defined" |
| 674 | assert main.s2Json, "s2Json not defined" |
| 675 | assert main.s3Json, "s3Json not defined" |
| 676 | assert main.s4Json, "s4Json not defined" |
| 677 | except AssertionError: |
| 678 | main.log.exception( "Case Prerequisites not set: " ) |
| 679 | main.cleanup() |
| 680 | main.exit() |
| 681 | main.case( "Testing removal of configurations" ) |
| 682 | main.step( "Remove 'allowed' configuration from all devices" ) |
| 683 | |
| 684 | s1Json = main.s1Json # NOTE: This is a reference |
| 685 | try: |
| 686 | del s1Json['allowed'] |
| 687 | except KeyError: |
| 688 | main.log.exception( "Key not found" ) |
| 689 | setS1 = main.ONOSrest1.setNetCfg( s1Json, |
| 690 | subjectClass="devices", |
| 691 | subjectKey="of:0000000000000001", |
| 692 | configKey="basic" ) |
| 693 | |
| 694 | s2Json = main.s2Json # NOTE: This is a reference |
| 695 | try: |
Jon Hall | 541b8e0 | 2015-12-14 19:29:01 -0800 | [diff] [blame] | 696 | time.sleep( main.gossipTime ) |
Jon Hall | 66e001c | 2015-11-12 09:45:10 -0800 | [diff] [blame] | 697 | del s2Json['allowed'] |
| 698 | except KeyError: |
| 699 | main.log.exception( "Key not found" ) |
| 700 | setS2 = main.ONOSrest2.setNetCfg( s2Json, |
| 701 | subjectClass="devices", |
| 702 | subjectKey="of:0000000000000002", |
| 703 | configKey="basic" ) |
| 704 | |
| 705 | s3Json = main.s3Json # NOTE: This is a reference |
| 706 | try: |
Jon Hall | 541b8e0 | 2015-12-14 19:29:01 -0800 | [diff] [blame] | 707 | time.sleep( main.gossipTime ) |
Jon Hall | 66e001c | 2015-11-12 09:45:10 -0800 | [diff] [blame] | 708 | del s3Json['allowed'] |
| 709 | except KeyError: |
| 710 | main.log.exception( "Key not found" ) |
| 711 | setS3 = main.ONOSrest3.setNetCfg( s3Json, |
| 712 | subjectClass="devices", |
| 713 | subjectKey="of:0000000000000003", |
| 714 | configKey="basic" ) |
| 715 | |
| 716 | s4Json = main.s4Json # NOTE: This is a reference |
| 717 | try: |
Jon Hall | 541b8e0 | 2015-12-14 19:29:01 -0800 | [diff] [blame] | 718 | time.sleep( main.gossipTime ) |
Jon Hall | 66e001c | 2015-11-12 09:45:10 -0800 | [diff] [blame] | 719 | del s4Json['allowed'] |
| 720 | except KeyError: |
| 721 | main.log.exception( "Key not found" ) |
| 722 | setS4 = main.ONOSrest4.setNetCfg( s4Json, |
| 723 | subjectClass="devices", |
| 724 | subjectKey="of:0000000000000004", |
| 725 | configKey="basic" ) |
| 726 | removeAllowed = setS1 and setS2 and setS3 and setS4 |
| 727 | utilities.assert_equals( expect=main.TRUE, |
| 728 | actual=removeAllowed, |
| 729 | onpass="Successfully removed 'allowed' config from devices", |
| 730 | onfail="Failed to remove the 'allowed' config key." ) |
| 731 | |
| 732 | main.netCfg.compareCfg( main, main.gossipTime ) |
| 733 | |
| 734 | main.step( "Delete basic config for s1 and s2" ) |
| 735 | removeS1 = main.ONOSrest1.removeNetCfg( subjectClass="devices", |
| 736 | subjectKey="of:0000000000000001", |
| 737 | configKey="basic" ) |
| 738 | removeS2 = main.ONOSrest2.removeNetCfg( subjectClass="devices", |
| 739 | subjectKey="of:0000000000000002", |
| 740 | configKey="basic" ) |
| 741 | removeSingles = removeS1 and removeS2 |
| 742 | utilities.assert_equals( expect=main.TRUE, |
| 743 | actual=removeSingles, |
| 744 | onpass="Successfully removed S1 and S2 basic config", |
| 745 | onfail="Failed to removed S1 and S2 basic config" ) |
| 746 | |
| 747 | main.netCfg.compareCfg( main, main.gossipTime ) |
| 748 | |
| 749 | main.step( "Delete the net config for S3" ) |
| 750 | removeS3 = main.ONOSrest3.removeNetCfg( subjectClass="devices", |
| 751 | subjectKey="of:0000000000000003" ) |
| 752 | utilities.assert_equals( expect=main.TRUE, |
| 753 | actual=removeS3, |
| 754 | onpass="Successfully removed S3's config", |
| 755 | onfail="Failed to removed S3's config" ) |
| 756 | |
| 757 | main.netCfg.compareCfg( main, main.gossipTime ) |
| 758 | |
| 759 | main.step( "Delete the net config for all devices" ) |
| 760 | remove = main.ONOSrest3.removeNetCfg( subjectClass="devices" ) |
| 761 | utilities.assert_equals( expect=main.TRUE, |
| 762 | actual=remove, |
| 763 | onpass="Successfully removed device config", |
| 764 | onfail="Failed to remove device config" ) |
| 765 | |
| 766 | main.netCfg.compareCfg( main, main.gossipTime ) |
| 767 | |
| 768 | main.step( "Assert the net config for devices is empty" ) |
| 769 | get = main.ONOSrest3.getNetCfg( subjectClass="devices" ) |
| 770 | utilities.assert_equals( expect='{}', |
| 771 | actual=get, |
| 772 | onpass="Successfully removed device config", |
| 773 | onfail="Failed to remove device config" ) |