kelvin-onlab | e5239e5 | 2015-05-13 14:46:45 -0700 | [diff] [blame] | 1 | """ |
| 2 | Wrapper functions for FuncIntent |
| 3 | This functions include Onosclidriver and Mininetclidriver driver functions |
| 4 | """ |
| 5 | def __init__( self ): |
| 6 | self.default = '' |
| 7 | |
kelvin-onlab | 2ccad6e | 2015-05-18 10:36:54 -0700 | [diff] [blame^] | 8 | def hostIntent( main, |
| 9 | name="", |
| 10 | host1="", |
| 11 | host2="", |
| 12 | host1Id="", |
| 13 | host2Id="", |
| 14 | mac1="", |
| 15 | mac2="", |
| 16 | vlan1="-1", |
| 17 | vlan2="-1", |
| 18 | sw1="", |
| 19 | sw2="", |
| 20 | expectedLink=0 ): |
kelvin-onlab | e5239e5 | 2015-05-13 14:46:45 -0700 | [diff] [blame] | 21 | """ |
| 22 | Add host intents |
| 23 | """ |
| 24 | import time |
kelvin-onlab | 2ccad6e | 2015-05-18 10:36:54 -0700 | [diff] [blame^] | 25 | |
| 26 | # Assert variables |
| 27 | assert main, "There is no main variable" |
| 28 | assert name, "variable name is empty" |
| 29 | assert host1 and host2, "You must specify hosts" |
| 30 | |
kelvin-onlab | e5239e5 | 2015-05-13 14:46:45 -0700 | [diff] [blame] | 31 | global itemName |
kelvin-onlab | 2ccad6e | 2015-05-18 10:36:54 -0700 | [diff] [blame^] | 32 | itemName = name |
| 33 | h1Name = host1 |
| 34 | h2Name = host2 |
| 35 | h1Id = host1Id |
| 36 | h2Id = host2Id |
| 37 | h1Mac = mac1 |
| 38 | h2Mac = mac2 |
| 39 | vlan1 = vlan1 |
| 40 | vlan2 = vlan2 |
kelvin-onlab | e5239e5 | 2015-05-13 14:46:45 -0700 | [diff] [blame] | 41 | intentsId = [] |
kelvin-onlab | 2ccad6e | 2015-05-18 10:36:54 -0700 | [diff] [blame^] | 42 | stepResult = main.TRUE |
kelvin-onlab | e5239e5 | 2015-05-13 14:46:45 -0700 | [diff] [blame] | 43 | pingResult = main.TRUE |
| 44 | intentResult = main.TRUE |
| 45 | flowResult = main.TRUE |
| 46 | topoResult = main.TRUE |
| 47 | linkDownResult = main.TRUE |
| 48 | linkUpResult = main.TRUE |
| 49 | |
kelvin-onlab | 2ccad6e | 2015-05-18 10:36:54 -0700 | [diff] [blame^] | 50 | if main.hostsData: |
| 51 | if not h1Mac: |
| 52 | h1Mac = main.hostsData[ h1Name ][ 'mac' ] |
| 53 | if not h2Mac: |
| 54 | h2Mac = main.hostsData[ h2Name ][ 'mac' ] |
| 55 | if main.hostsData[ h1Name ][ 'vlan' ] != '-1': |
| 56 | vlan1 = main.hostsData[ h1Name ][ 'vlan' ] |
| 57 | if main.hostsData[ h2Name ][ 'vlan' ] != '-1': |
| 58 | vlan2 = main.hostsData[ h2Name ][ 'vlan' ] |
| 59 | if not h1Id: |
| 60 | h1Id = main.hostsData[ h1Name ][ 'id' ] |
| 61 | if not h2Id: |
| 62 | h2Id = main.hostsData[ h2Name ][ 'id' ] |
| 63 | |
| 64 | assert h1Id and h2Id, "You must specify host IDs" |
| 65 | if not ( h1Id and h2Id ): |
| 66 | main.log.info( "There are no host IDs" ) |
| 67 | return main.FALSE |
| 68 | |
kelvin-onlab | e5239e5 | 2015-05-13 14:46:45 -0700 | [diff] [blame] | 69 | # Discover hosts using arping |
| 70 | main.log.info( itemName + ": Discover host using arping" ) |
| 71 | main.Mininet1.arping( host=h1Name ) |
| 72 | main.Mininet1.arping( host=h2Name ) |
| 73 | host1 = main.CLIs[ 0 ].getHost( mac=h1Mac ) |
| 74 | host2 = main.CLIs[ 0 ].getHost( mac=h2Mac ) |
| 75 | |
| 76 | # Adding host intents |
| 77 | main.log.info( itemName + ": Adding host intents" ) |
| 78 | intent1 = main.CLIs[ 0 ].addHostIntent( hostIdOne=h1Id, |
| 79 | hostIdTwo=h2Id ) |
| 80 | intentsId.append( intent1 ) |
| 81 | time.sleep( 5 ) |
| 82 | intent2 = main.CLIs[ 0 ].addHostIntent( hostIdOne=h2Id, |
| 83 | hostIdTwo=h1Id ) |
| 84 | intentsId.append( intent2 ) |
| 85 | |
| 86 | # Check intents state |
| 87 | time.sleep( 50 ) |
| 88 | intentResult = checkIntentState( main, intentsId ) |
| 89 | |
| 90 | # Verify flows |
| 91 | checkFlowsState( main ) |
| 92 | |
| 93 | # Ping hosts |
| 94 | pingHost( main, h1Name, h2Name ) |
| 95 | # Ping hosts again... |
| 96 | pingResult = pingHost( main, h1Name, h2Name ) |
| 97 | time.sleep( 5 ) |
| 98 | |
kelvin-onlab | 2ccad6e | 2015-05-18 10:36:54 -0700 | [diff] [blame^] | 99 | # Test rerouting if these variables exist |
| 100 | if sw1 and sw2 and expectedLink: |
| 101 | # link down |
| 102 | link( main, sw1, sw2, "down" ) |
| 103 | intentResult = intentResult and checkIntentState( main, intentsId ) |
kelvin-onlab | e5239e5 | 2015-05-13 14:46:45 -0700 | [diff] [blame] | 104 | |
kelvin-onlab | 2ccad6e | 2015-05-18 10:36:54 -0700 | [diff] [blame^] | 105 | # Verify flows |
| 106 | checkFlowsState( main ) |
kelvin-onlab | e5239e5 | 2015-05-13 14:46:45 -0700 | [diff] [blame] | 107 | |
kelvin-onlab | 2ccad6e | 2015-05-18 10:36:54 -0700 | [diff] [blame^] | 108 | # Check OnosTopology |
| 109 | topoResult = checkTopology( main, expectedLink ) |
kelvin-onlab | e5239e5 | 2015-05-13 14:46:45 -0700 | [diff] [blame] | 110 | |
kelvin-onlab | 2ccad6e | 2015-05-18 10:36:54 -0700 | [diff] [blame^] | 111 | # Ping hosts |
| 112 | pingResult = pingResult and pingHost( main, h1Name, h2Name ) |
kelvin-onlab | e5239e5 | 2015-05-13 14:46:45 -0700 | [diff] [blame] | 113 | |
kelvin-onlab | 2ccad6e | 2015-05-18 10:36:54 -0700 | [diff] [blame^] | 114 | intentResult = checkIntentState( main, intentsId ) |
kelvin-onlab | e5239e5 | 2015-05-13 14:46:45 -0700 | [diff] [blame] | 115 | |
kelvin-onlab | 2ccad6e | 2015-05-18 10:36:54 -0700 | [diff] [blame^] | 116 | # link up |
| 117 | link( main, sw1, sw2, "up" ) |
| 118 | time.sleep( 5 ) |
kelvin-onlab | e5239e5 | 2015-05-13 14:46:45 -0700 | [diff] [blame] | 119 | |
kelvin-onlab | 2ccad6e | 2015-05-18 10:36:54 -0700 | [diff] [blame^] | 120 | # Verify flows |
| 121 | checkFlowsState( main ) |
kelvin-onlab | e5239e5 | 2015-05-13 14:46:45 -0700 | [diff] [blame] | 122 | |
kelvin-onlab | 2ccad6e | 2015-05-18 10:36:54 -0700 | [diff] [blame^] | 123 | # Check OnosTopology |
| 124 | topoResult = checkTopology( main, expectedLink ) |
kelvin-onlab | e5239e5 | 2015-05-13 14:46:45 -0700 | [diff] [blame] | 125 | |
kelvin-onlab | 2ccad6e | 2015-05-18 10:36:54 -0700 | [diff] [blame^] | 126 | # Ping hosts |
| 127 | pingResult = pingResult and pingHost( main, h1Name, h2Name ) |
kelvin-onlab | e5239e5 | 2015-05-13 14:46:45 -0700 | [diff] [blame] | 128 | |
| 129 | # Remove intents |
| 130 | for intent in intentsId: |
| 131 | main.CLIs[ 0 ].removeIntent( intentId=intent, purge=True ) |
| 132 | |
| 133 | print main.CLIs[ 0 ].intents() |
| 134 | stepResult = pingResult and linkDownResult and linkUpResult \ |
| 135 | and intentResult |
| 136 | |
| 137 | return stepResult |
| 138 | |
kelvin-onlab | 2ccad6e | 2015-05-18 10:36:54 -0700 | [diff] [blame^] | 139 | def pointIntent( main, |
| 140 | name="", |
| 141 | host1="", |
| 142 | host2="", |
| 143 | deviceId1="", |
| 144 | deviceId2="", |
| 145 | port1="", |
| 146 | port2="", |
| 147 | ethType="", |
| 148 | mac1="", |
| 149 | mac2="", |
| 150 | bandwidth="", |
| 151 | lambdaAlloc=False, |
| 152 | ipProto="", |
| 153 | ip1="", |
| 154 | ip2="", |
| 155 | tcp1="", |
| 156 | tcp2="", |
| 157 | sw1="", |
| 158 | sw2="", |
| 159 | expectedLink=0 ): |
kelvin-onlab | e5239e5 | 2015-05-13 14:46:45 -0700 | [diff] [blame] | 160 | """ |
| 161 | Add Point intents |
| 162 | """ |
| 163 | import time |
kelvin-onlab | 2ccad6e | 2015-05-18 10:36:54 -0700 | [diff] [blame^] | 164 | assert main, "There is no main variable" |
| 165 | assert name, "variable name is empty" |
| 166 | assert host1 and host2, "You must specify hosts" |
| 167 | |
kelvin-onlab | e5239e5 | 2015-05-13 14:46:45 -0700 | [diff] [blame] | 168 | global itemName |
kelvin-onlab | 2ccad6e | 2015-05-18 10:36:54 -0700 | [diff] [blame^] | 169 | itemName = name |
| 170 | h1Name = host1 |
| 171 | h2Name = host2 |
kelvin-onlab | e5239e5 | 2015-05-13 14:46:45 -0700 | [diff] [blame] | 172 | intentsId = [] |
kelvin-onlab | b223560 | 2015-05-13 17:51:06 -0700 | [diff] [blame] | 173 | |
kelvin-onlab | e5239e5 | 2015-05-13 14:46:45 -0700 | [diff] [blame] | 174 | pingResult = main.TRUE |
| 175 | intentResult = main.TRUE |
| 176 | flowResult = main.TRUE |
| 177 | topoResult = main.TRUE |
| 178 | linkDownResult = main.TRUE |
| 179 | linkUpResult = main.TRUE |
| 180 | |
kelvin-onlab | b223560 | 2015-05-13 17:51:06 -0700 | [diff] [blame] | 181 | # Adding bidirectional point intents |
kelvin-onlab | e5239e5 | 2015-05-13 14:46:45 -0700 | [diff] [blame] | 182 | main.log.info( itemName + ": Adding host intents" ) |
kelvin-onlab | 2ccad6e | 2015-05-18 10:36:54 -0700 | [diff] [blame^] | 183 | intent1 = main.CLIs[ 0 ].addPointIntent( ingressDevice=deviceId1, |
| 184 | egressDevice=deviceId2, |
| 185 | portIngress=port1, |
| 186 | portEgress=port2, |
kelvin-onlab | b223560 | 2015-05-13 17:51:06 -0700 | [diff] [blame] | 187 | ethType=ethType, |
kelvin-onlab | 2ccad6e | 2015-05-18 10:36:54 -0700 | [diff] [blame^] | 188 | ethSrc=mac1, |
| 189 | ethDst=mac2, |
kelvin-onlab | b223560 | 2015-05-13 17:51:06 -0700 | [diff] [blame] | 190 | bandwidth=bandwidth, |
| 191 | lambdaAlloc=lambdaAlloc, |
| 192 | ipProto=ipProto, |
kelvin-onlab | 2ccad6e | 2015-05-18 10:36:54 -0700 | [diff] [blame^] | 193 | ipSrc=ip1, |
| 194 | ipDst=ip2, |
| 195 | tcpSrc=tcp1, |
| 196 | tcpDst=tcp2 ) |
kelvin-onlab | b223560 | 2015-05-13 17:51:06 -0700 | [diff] [blame] | 197 | |
kelvin-onlab | e5239e5 | 2015-05-13 14:46:45 -0700 | [diff] [blame] | 198 | intentsId.append( intent1 ) |
| 199 | time.sleep( 5 ) |
kelvin-onlab | 2ccad6e | 2015-05-18 10:36:54 -0700 | [diff] [blame^] | 200 | intent2 = main.CLIs[ 0 ].addPointIntent( ingressDevice=deviceId2, |
| 201 | egressDevice=deviceId1, |
| 202 | portIngress=port2, |
| 203 | portEgress=port1, |
kelvin-onlab | b223560 | 2015-05-13 17:51:06 -0700 | [diff] [blame] | 204 | ethType=ethType, |
kelvin-onlab | 2ccad6e | 2015-05-18 10:36:54 -0700 | [diff] [blame^] | 205 | ethSrc=mac2, |
| 206 | ethDst=mac1, |
kelvin-onlab | b223560 | 2015-05-13 17:51:06 -0700 | [diff] [blame] | 207 | bandwidth=bandwidth, |
| 208 | lambdaAlloc=lambdaAlloc, |
| 209 | ipProto=ipProto, |
kelvin-onlab | 2ccad6e | 2015-05-18 10:36:54 -0700 | [diff] [blame^] | 210 | ipSrc=ip2, |
| 211 | ipDst=ip1, |
| 212 | tcpSrc=tcp2, |
| 213 | tcpDst=tcp1 ) |
kelvin-onlab | e5239e5 | 2015-05-13 14:46:45 -0700 | [diff] [blame] | 214 | intentsId.append( intent2 ) |
| 215 | |
| 216 | # Check intents state |
| 217 | time.sleep( 50 ) |
| 218 | intentResult = checkIntentState( main, intentsId ) |
| 219 | |
| 220 | # Verify flows |
| 221 | checkFlowsState( main ) |
| 222 | |
| 223 | # Ping hosts |
| 224 | pingHost( main, h1Name, h2Name ) |
| 225 | # Ping hosts again... |
| 226 | pingResult = pingHost( main, h1Name, h2Name ) |
| 227 | time.sleep( 5 ) |
| 228 | |
kelvin-onlab | 2ccad6e | 2015-05-18 10:36:54 -0700 | [diff] [blame^] | 229 | if sw1 and sw2 and expectedLink: |
| 230 | # link down |
| 231 | link( main, sw1, sw2, "down" ) |
| 232 | intentResult = intentResult and checkIntentState( main, intentsId ) |
kelvin-onlab | e5239e5 | 2015-05-13 14:46:45 -0700 | [diff] [blame] | 233 | |
kelvin-onlab | 2ccad6e | 2015-05-18 10:36:54 -0700 | [diff] [blame^] | 234 | # Verify flows |
| 235 | checkFlowsState( main ) |
kelvin-onlab | e5239e5 | 2015-05-13 14:46:45 -0700 | [diff] [blame] | 236 | |
kelvin-onlab | 2ccad6e | 2015-05-18 10:36:54 -0700 | [diff] [blame^] | 237 | # Check OnosTopology |
| 238 | topoResult = checkTopology( main, expectedLink ) |
kelvin-onlab | e5239e5 | 2015-05-13 14:46:45 -0700 | [diff] [blame] | 239 | |
kelvin-onlab | 2ccad6e | 2015-05-18 10:36:54 -0700 | [diff] [blame^] | 240 | # Ping hosts |
| 241 | pingResult = pingResult and pingHost( main, h1Name, h2Name ) |
kelvin-onlab | e5239e5 | 2015-05-13 14:46:45 -0700 | [diff] [blame] | 242 | |
kelvin-onlab | 2ccad6e | 2015-05-18 10:36:54 -0700 | [diff] [blame^] | 243 | intentResult = checkIntentState( main, intentsId ) |
kelvin-onlab | e5239e5 | 2015-05-13 14:46:45 -0700 | [diff] [blame] | 244 | |
kelvin-onlab | 2ccad6e | 2015-05-18 10:36:54 -0700 | [diff] [blame^] | 245 | # link up |
| 246 | link( main, sw1, sw2, "up" ) |
| 247 | time.sleep( 5 ) |
kelvin-onlab | e5239e5 | 2015-05-13 14:46:45 -0700 | [diff] [blame] | 248 | |
kelvin-onlab | 2ccad6e | 2015-05-18 10:36:54 -0700 | [diff] [blame^] | 249 | # Verify flows |
| 250 | checkFlowsState( main ) |
kelvin-onlab | e5239e5 | 2015-05-13 14:46:45 -0700 | [diff] [blame] | 251 | |
kelvin-onlab | 2ccad6e | 2015-05-18 10:36:54 -0700 | [diff] [blame^] | 252 | # Check OnosTopology |
| 253 | topoResult = checkTopology( main, expectedLink ) |
kelvin-onlab | e5239e5 | 2015-05-13 14:46:45 -0700 | [diff] [blame] | 254 | |
kelvin-onlab | 2ccad6e | 2015-05-18 10:36:54 -0700 | [diff] [blame^] | 255 | # Ping hosts |
| 256 | pingResult = pingResult and pingHost( main, h1Name, h2Name ) |
kelvin-onlab | e5239e5 | 2015-05-13 14:46:45 -0700 | [diff] [blame] | 257 | |
| 258 | # Remove intents |
| 259 | for intent in intentsId: |
| 260 | main.CLIs[ 0 ].removeIntent( intentId=intent, purge=True ) |
| 261 | |
| 262 | print main.CLIs[ 0 ].intents() |
| 263 | stepResult = pingResult and linkDownResult and linkUpResult \ |
| 264 | and intentResult |
| 265 | |
| 266 | return stepResult |
| 267 | |
| 268 | def link( main, sw1, sw2, option): |
| 269 | |
| 270 | # link down |
| 271 | main.log.info( itemName + ": Bring link " + option + "between " + |
| 272 | sw1 + " and " + sw2 ) |
| 273 | main.Mininet1.link( end1=sw1, end2=sw2, option=option ) |
| 274 | |
| 275 | def pingHost( main, h1Name, h2Name ): |
| 276 | |
| 277 | # Ping hosts |
| 278 | main.log.info( itemName + ": Ping " + h1Name + " and " + |
| 279 | h2Name ) |
| 280 | pingResult1 = main.Mininet1.pingHost( src=h1Name , target=h2Name ) |
| 281 | if not pingResult1: |
| 282 | main.log.info( itemName + ": " + h1Name + " cannot ping " |
| 283 | + h2Name ) |
| 284 | pingResult2 = main.Mininet1.pingHost( src=h2Name , target=h1Name ) |
| 285 | if not pingResult2: |
| 286 | main.log.info( itemName + ": " + h2Name + " cannot ping " |
| 287 | + h1Name ) |
| 288 | pingResult = pingResult1 and pingResult2 |
| 289 | if pingResult: |
| 290 | main.log.info( itemName + ": Successfully pinged " + |
| 291 | "both hosts" ) |
| 292 | else: |
| 293 | main.log.info( itemName + ": Failed to ping " + |
| 294 | "both hosts" ) |
| 295 | return pingResult |
| 296 | |
kelvin-onlab | 2ccad6e | 2015-05-18 10:36:54 -0700 | [diff] [blame^] | 297 | def getHostsData( main ): |
kelvin-onlab | e5239e5 | 2015-05-13 14:46:45 -0700 | [diff] [blame] | 298 | """ |
kelvin-onlab | 2ccad6e | 2015-05-18 10:36:54 -0700 | [diff] [blame^] | 299 | Use fwd app and pingall to discover all the hosts |
kelvin-onlab | e5239e5 | 2015-05-13 14:46:45 -0700 | [diff] [blame] | 300 | """ |
kelvin-onlab | 2ccad6e | 2015-05-18 10:36:54 -0700 | [diff] [blame^] | 301 | """ |
| 302 | hosts json format: |
| 303 | """ |
| 304 | import json |
| 305 | activateResult = main.TRUE |
| 306 | appCheck = main.TRUE |
| 307 | main.log.info( "Activating reactive forwarding app " ) |
| 308 | activateResult = main.CLIs[ 0 ].activateApp( "org.onosproject.fwd" ) |
| 309 | |
| 310 | for i in range( main.numCtrls ): |
| 311 | appCheck = appCheck and main.CLIs[ i ].appToIDCheck() |
| 312 | |
| 313 | if appCheck != main.TRUE: |
| 314 | main.log.warn( main.CLIs[ 0 ].apps() ) |
| 315 | main.log.warn( main.CLIs[ 0 ].appIDs() ) |
| 316 | |
| 317 | pingResult = main.Mininet1.pingall() |
| 318 | hostsJson = json.loads( main.CLIs[ 0 ].hosts() ) |
| 319 | hosts = main.Mininet1.getHosts() |
| 320 | for host in hosts: |
| 321 | main.hostsData[ host ] = {} |
| 322 | main.hostsData[ host ][ 'mac' ] = main.Mininet1.getMacAddress( host ) |
| 323 | for hostj in hostsJson: |
| 324 | if main.hostsData[ host ][ 'mac' ] == hostj[ 'mac' ]: |
| 325 | main.hostsData[ host ][ 'id' ] = hostj[ 'id' ] |
| 326 | main.hostsData[ host ][ 'vlan' ] = hostj[ 'vlan' ] |
| 327 | main.hostsData[ host ][ 'location' ] = \ |
| 328 | hostj[ 'location' ][ 'elementId' ] + '/' + \ |
| 329 | hostj[ 'location' ][ 'port' ] |
| 330 | main.hostsData[ host ][ 'ipAddresses' ] = hostj[ 'ipAddresses' ] |
| 331 | |
| 332 | print main.hostsData |
| 333 | return pingResult |
kelvin-onlab | e5239e5 | 2015-05-13 14:46:45 -0700 | [diff] [blame] | 334 | |
| 335 | def checkTopology( main, expectedLink ): |
| 336 | statusResult = main.TRUE |
| 337 | # Check onos topology |
| 338 | main.log.info( itemName + ": Checking ONOS topology " ) |
| 339 | |
| 340 | for i in range( main.numCtrls ): |
| 341 | topologyResult = main.CLIs[ i ].topology() |
| 342 | statusResult = main.ONOSbench.checkStatus( topologyResult, |
| 343 | main.numSwitch, |
| 344 | expectedLink )\ |
| 345 | and statusResult |
| 346 | if not statusResult: |
| 347 | main.log.info( itemName + ": Topology mismatch" ) |
| 348 | else: |
| 349 | main.log.info( itemName + ": Topology match" ) |
| 350 | return statusResult |
| 351 | |
| 352 | def checkIntentState( main, intentsId ): |
| 353 | |
| 354 | main.log.info( itemName + ": Check host intents state" ) |
| 355 | for i in range( main.numCtrls ): |
| 356 | intentResult = main.CLIs[ i ].checkIntentState( intentsId=intentsId ) |
| 357 | if not intentResult: |
| 358 | main.log.info( itemName + ": Check host intents state again") |
| 359 | for i in range( main.numCtrls ): |
| 360 | intentResult = main.CLIs[ i ].checkIntentState( |
| 361 | intentsId=intentsId ) |
| 362 | return intentResult |
| 363 | |
| 364 | def checkFlowsState( main ): |
| 365 | |
| 366 | main.log.info( itemName + ": Check flows state" ) |
| 367 | checkFlowsResult = main.CLIs[ 0 ].checkFlowsState() |
| 368 | return checkFlowsResult |
| 369 | |
| 370 | def printMsg( main, h1Name, h2Name ): |
| 371 | main.log.info("PINGING HOST INSIDE printMSG") |
| 372 | pingHost( main, itemName, h1Name, h2Name ) |
| 373 | print 'lala' |
| 374 | |