blob: 10930e375b8c5e3766097f2919aab722498d0cdc [file] [log] [blame]
kelvin-onlabe5239e52015-05-13 14:46:45 -07001"""
2 Wrapper functions for FuncIntent
3 This functions include Onosclidriver and Mininetclidriver driver functions
kelvin-onlab381b34c2015-05-21 09:21:06 -07004 Author: kelvin@onlab.us
kelvin-onlabe5239e52015-05-13 14:46:45 -07005"""
6def __init__( self ):
7 self.default = ''
8
kelvin-onlab2ccad6e2015-05-18 10:36:54 -07009def hostIntent( main,
kelvin-onlabf0594d72015-05-19 17:25:12 -070010 name,
11 host1,
12 host2,
kelvin-onlab2ccad6e2015-05-18 10:36:54 -070013 host1Id="",
14 host2Id="",
15 mac1="",
16 mac2="",
17 vlan1="-1",
18 vlan2="-1",
19 sw1="",
20 sw2="",
21 expectedLink=0 ):
kelvin-onlabe5239e52015-05-13 14:46:45 -070022 """
kelvin-onlab381b34c2015-05-21 09:21:06 -070023 Description:
24 Verify add-host-intent
25 Steps:
26 - Discover hosts
27 - Add host intents
28 - Check intents
29 - Verify flows
30 - Ping hosts
31 - Reroute
32 - Link down
33 - Verify flows
34 - Check topology
35 - Ping hosts
36 - Link up
37 - Verify flows
38 - Check topology
39 - Ping hosts
40 - Remove intents
41 Required:
42 name - Type of host intent to add eg. IPV4 | VLAN | Dualstack
43 host1 - Name of first host
44 host2 - Name of second host
45 Optional:
46 host1Id - ONOS id of the first host eg. 00:00:00:00:00:01/-1
47 host2Id - ONOS id of the second host
48 mac1 - Mac address of first host
49 mac2 - Mac address of the second host
50 vlan1 - Vlan tag of first host, defaults to -1
51 vlan2 - Vlan tag of second host, defaults to -1
52 sw1 - First switch to bring down & up for rerouting purpose
53 sw2 - Second switch to bring down & up for rerouting purpose
54 expectedLink - Expected link when the switches are down, it should
55 be two links lower than the links before the two
56 switches are down
kelvin-onlabe5239e52015-05-13 14:46:45 -070057 """
58 import time
kelvin-onlab2ccad6e2015-05-18 10:36:54 -070059
60 # Assert variables
61 assert main, "There is no main variable"
62 assert name, "variable name is empty"
63 assert host1 and host2, "You must specify hosts"
64
kelvin-onlabe5239e52015-05-13 14:46:45 -070065 global itemName
kelvin-onlab2ccad6e2015-05-18 10:36:54 -070066 itemName = name
kelvin-onlab2ccad6e2015-05-18 10:36:54 -070067 h1Id = host1Id
68 h2Id = host2Id
69 h1Mac = mac1
70 h2Mac = mac2
71 vlan1 = vlan1
72 vlan2 = vlan2
kelvin-onlab08900012015-05-20 14:30:44 -070073 hostNames = [ host1 , host2 ]
kelvin-onlabe5239e52015-05-13 14:46:45 -070074 intentsId = []
kelvin-onlab2ccad6e2015-05-18 10:36:54 -070075 stepResult = main.TRUE
kelvin-onlabe5239e52015-05-13 14:46:45 -070076 pingResult = main.TRUE
77 intentResult = main.TRUE
kelvin-onlabf0594d72015-05-19 17:25:12 -070078 removeIntentResult = main.TRUE
kelvin-onlabe5239e52015-05-13 14:46:45 -070079 flowResult = main.TRUE
80 topoResult = main.TRUE
81 linkDownResult = main.TRUE
82 linkUpResult = main.TRUE
83
kelvin-onlab2ccad6e2015-05-18 10:36:54 -070084 if main.hostsData:
85 if not h1Mac:
kelvin-onlab08900012015-05-20 14:30:44 -070086 h1Mac = main.hostsData[ host1 ][ 'mac' ]
kelvin-onlab2ccad6e2015-05-18 10:36:54 -070087 if not h2Mac:
kelvin-onlab08900012015-05-20 14:30:44 -070088 h2Mac = main.hostsData[ host2 ][ 'mac' ]
89 if main.hostsData[ host1 ][ 'vlan' ] != '-1':
90 vlan1 = main.hostsData[ host1 ][ 'vlan' ]
91 if main.hostsData[ host2 ][ 'vlan' ] != '-1':
92 vlan2 = main.hostsData[ host2 ][ 'vlan' ]
kelvin-onlab2ccad6e2015-05-18 10:36:54 -070093 if not h1Id:
kelvin-onlab08900012015-05-20 14:30:44 -070094 h1Id = main.hostsData[ host1 ][ 'id' ]
kelvin-onlab2ccad6e2015-05-18 10:36:54 -070095 if not h2Id:
kelvin-onlab08900012015-05-20 14:30:44 -070096 h2Id = main.hostsData[ host2 ][ 'id' ]
kelvin-onlab2ccad6e2015-05-18 10:36:54 -070097
98 assert h1Id and h2Id, "You must specify host IDs"
99 if not ( h1Id and h2Id ):
100 main.log.info( "There are no host IDs" )
101 return main.FALSE
102
kelvin-onlabe5239e52015-05-13 14:46:45 -0700103 # Discover hosts using arping
104 main.log.info( itemName + ": Discover host using arping" )
kelvin-onlab08900012015-05-20 14:30:44 -0700105 main.Mininet1.arping( host=host1 )
106 main.Mininet1.arping( host=host2 )
kelvin-onlabe5239e52015-05-13 14:46:45 -0700107 host1 = main.CLIs[ 0 ].getHost( mac=h1Mac )
108 host2 = main.CLIs[ 0 ].getHost( mac=h2Mac )
109
110 # Adding host intents
111 main.log.info( itemName + ": Adding host intents" )
112 intent1 = main.CLIs[ 0 ].addHostIntent( hostIdOne=h1Id,
113 hostIdTwo=h2Id )
114 intentsId.append( intent1 )
115 time.sleep( 5 )
kelvin-onlabe5239e52015-05-13 14:46:45 -0700116
117 # Check intents state
kelvin-onlabf0594d72015-05-19 17:25:12 -0700118 time.sleep( 30 )
kelvin-onlabe5239e52015-05-13 14:46:45 -0700119 intentResult = checkIntentState( main, intentsId )
120
kelvin-onlab381b34c2015-05-21 09:21:06 -0700121 # Check intents state again if first check fails...
122 if not intentResult:
123 intentResult = checkIntentState( main, intentsId )
124
kelvin-onlabe5239e52015-05-13 14:46:45 -0700125 # Verify flows
126 checkFlowsState( main )
127
128 # Ping hosts
kelvin-onlab08900012015-05-20 14:30:44 -0700129 firstPingResult = pingallHosts( main, hostNames )
kelvin-onlabf0594d72015-05-19 17:25:12 -0700130 if not firstPingResult:
kelvin-onlab08900012015-05-20 14:30:44 -0700131 main.log.debug( "First ping failed, there must be" +
kelvin-onlabf0594d72015-05-19 17:25:12 -0700132 " something wrong with ONOS performance" )
133
kelvin-onlabe5239e52015-05-13 14:46:45 -0700134 # Ping hosts again...
kelvin-onlab08900012015-05-20 14:30:44 -0700135 pingResult = pingResult and pingallHosts( main, hostNames )
kelvin-onlabe5239e52015-05-13 14:46:45 -0700136 time.sleep( 5 )
137
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700138 # Test rerouting if these variables exist
139 if sw1 and sw2 and expectedLink:
140 # link down
kelvin-onlabf0594d72015-05-19 17:25:12 -0700141 linkDownResult = link( main, sw1, sw2, "down" )
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700142 intentResult = intentResult and checkIntentState( main, intentsId )
kelvin-onlabe5239e52015-05-13 14:46:45 -0700143
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700144 # Verify flows
145 checkFlowsState( main )
kelvin-onlabe5239e52015-05-13 14:46:45 -0700146
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700147 # Check OnosTopology
148 topoResult = checkTopology( main, expectedLink )
kelvin-onlabe5239e52015-05-13 14:46:45 -0700149
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700150 # Ping hosts
kelvin-onlab08900012015-05-20 14:30:44 -0700151 pingResult = pingResult and pingallHosts( main, hostNames )
kelvin-onlabe5239e52015-05-13 14:46:45 -0700152
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700153 intentResult = checkIntentState( main, intentsId )
kelvin-onlabe5239e52015-05-13 14:46:45 -0700154
kelvin-onlabf0594d72015-05-19 17:25:12 -0700155 # Checks ONOS state in link down
156 if linkDownResult and topoResult and pingResult and intentResult:
157 main.log.info( itemName + ": Successfully brought link down" )
158 else:
kelvin-onlab08900012015-05-20 14:30:44 -0700159 main.log.error( itemName + ": Failed to bring link down" )
kelvin-onlabf0594d72015-05-19 17:25:12 -0700160
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700161 # link up
kelvin-onlabf0594d72015-05-19 17:25:12 -0700162 linkUpResult = link( main, sw1, sw2, "up" )
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700163 time.sleep( 5 )
kelvin-onlabe5239e52015-05-13 14:46:45 -0700164
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700165 # Verify flows
166 checkFlowsState( main )
kelvin-onlabe5239e52015-05-13 14:46:45 -0700167
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700168 # Check OnosTopology
kelvin-onlabf0594d72015-05-19 17:25:12 -0700169 topoResult = checkTopology( main, main.numLinks )
kelvin-onlabe5239e52015-05-13 14:46:45 -0700170
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700171 # Ping hosts
kelvin-onlab08900012015-05-20 14:30:44 -0700172 pingResult = pingResult and pingallHosts( main, hostNames )
kelvin-onlabe5239e52015-05-13 14:46:45 -0700173
kelvin-onlabf0594d72015-05-19 17:25:12 -0700174 intentResult = checkIntentState( main, intentsId )
kelvin-onlabe5239e52015-05-13 14:46:45 -0700175
kelvin-onlabf0594d72015-05-19 17:25:12 -0700176 # Checks ONOS state in link up
177 if linkUpResult and topoResult and pingResult and intentResult:
178 main.log.info( itemName + ": Successfully brought link back up" )
179 else:
kelvin-onlab08900012015-05-20 14:30:44 -0700180 main.log.error( itemName + ": Failed to bring link back up" )
kelvin-onlabf0594d72015-05-19 17:25:12 -0700181
182 # Remove all intents
183 removeIntentResult = removeAllIntents( main, intentsId )
184
kelvin-onlabe5239e52015-05-13 14:46:45 -0700185 stepResult = pingResult and linkDownResult and linkUpResult \
kelvin-onlabf0594d72015-05-19 17:25:12 -0700186 and intentResult and removeIntentResult
kelvin-onlabe5239e52015-05-13 14:46:45 -0700187
188 return stepResult
189
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700190def pointIntent( main,
kelvin-onlabf0594d72015-05-19 17:25:12 -0700191 name,
192 host1,
193 host2,
kelvin-onlab381b34c2015-05-21 09:21:06 -0700194 deviceId1="",
195 deviceId2="",
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700196 port1="",
197 port2="",
198 ethType="",
199 mac1="",
200 mac2="",
201 bandwidth="",
202 lambdaAlloc=False,
203 ipProto="",
204 ip1="",
205 ip2="",
206 tcp1="",
207 tcp2="",
208 sw1="",
209 sw2="",
210 expectedLink=0 ):
kelvin-onlab381b34c2015-05-21 09:21:06 -0700211
kelvin-onlabe5239e52015-05-13 14:46:45 -0700212 """
kelvin-onlab381b34c2015-05-21 09:21:06 -0700213 Description:
214 Verify add-point-intent
215 Steps:
216 - Get device ids | ports
217 - Add point intents
218 - Check intents
219 - Verify flows
220 - Ping hosts
221 - Reroute
222 - Link down
223 - Verify flows
224 - Check topology
225 - Ping hosts
226 - Link up
227 - Verify flows
228 - Check topology
229 - Ping hosts
230 - Remove intents
231 Required:
232 name - Type of point intent to add eg. IPV4 | VLAN | Dualstack
233 host1 - Name of first host
234 host2 - Name of second host
235 Optional:
236 deviceId1 - ONOS device id of the first switch, the same as the
237 location of the first host eg. of:0000000000000001/1,
238 located at device 1 port 1
239 deviceId2 - ONOS device id of the second switch
240 port1 - The port number where the first host is attached
241 port2 - The port number where the second host is attached
242 ethType - Ethernet type eg. IPV4, IPV6
243 mac1 - Mac address of first host
244 mac2 - Mac address of the second host
245 bandwidth - Bandwidth capacity
246 lambdaAlloc - Allocate lambda, defaults to False
247 ipProto - IP protocol
248 ip1 - IP address of first host
249 ip2 - IP address of second host
250 tcp1 - TCP port of first host
251 tcp2 - TCP port of second host
252 sw1 - First switch to bring down & up for rerouting purpose
253 sw2 - Second switch to bring down & up for rerouting purpose
254 expectedLink - Expected link when the switches are down, it should
255 be two links lower than the links before the two
256 switches are down
kelvin-onlabe5239e52015-05-13 14:46:45 -0700257 """
kelvin-onlab381b34c2015-05-21 09:21:06 -0700258
kelvin-onlabe5239e52015-05-13 14:46:45 -0700259 import time
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700260 assert main, "There is no main variable"
261 assert name, "variable name is empty"
262 assert host1 and host2, "You must specify hosts"
263
kelvin-onlabe5239e52015-05-13 14:46:45 -0700264 global itemName
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700265 itemName = name
kelvin-onlab08900012015-05-20 14:30:44 -0700266 host1 = host1
267 host2 = host2
268 hostNames = [ host1, host2 ]
kelvin-onlabe5239e52015-05-13 14:46:45 -0700269 intentsId = []
kelvin-onlabb2235602015-05-13 17:51:06 -0700270
kelvin-onlabe5239e52015-05-13 14:46:45 -0700271 pingResult = main.TRUE
272 intentResult = main.TRUE
kelvin-onlabf0594d72015-05-19 17:25:12 -0700273 removeIntentResult = main.TRUE
kelvin-onlabe5239e52015-05-13 14:46:45 -0700274 flowResult = main.TRUE
275 topoResult = main.TRUE
276 linkDownResult = main.TRUE
277 linkUpResult = main.TRUE
278
kelvin-onlabb2235602015-05-13 17:51:06 -0700279 # Adding bidirectional point intents
kelvin-onlabf0594d72015-05-19 17:25:12 -0700280 main.log.info( itemName + ": Adding point intents" )
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700281 intent1 = main.CLIs[ 0 ].addPointIntent( ingressDevice=deviceId1,
282 egressDevice=deviceId2,
283 portIngress=port1,
284 portEgress=port2,
kelvin-onlabb2235602015-05-13 17:51:06 -0700285 ethType=ethType,
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700286 ethSrc=mac1,
287 ethDst=mac2,
kelvin-onlabb2235602015-05-13 17:51:06 -0700288 bandwidth=bandwidth,
289 lambdaAlloc=lambdaAlloc,
290 ipProto=ipProto,
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700291 ipSrc=ip1,
292 ipDst=ip2,
293 tcpSrc=tcp1,
294 tcpDst=tcp2 )
kelvin-onlabb2235602015-05-13 17:51:06 -0700295
kelvin-onlabe5239e52015-05-13 14:46:45 -0700296 intentsId.append( intent1 )
297 time.sleep( 5 )
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700298 intent2 = main.CLIs[ 0 ].addPointIntent( ingressDevice=deviceId2,
299 egressDevice=deviceId1,
300 portIngress=port2,
301 portEgress=port1,
kelvin-onlabb2235602015-05-13 17:51:06 -0700302 ethType=ethType,
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700303 ethSrc=mac2,
304 ethDst=mac1,
kelvin-onlabb2235602015-05-13 17:51:06 -0700305 bandwidth=bandwidth,
306 lambdaAlloc=lambdaAlloc,
307 ipProto=ipProto,
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700308 ipSrc=ip2,
309 ipDst=ip1,
310 tcpSrc=tcp2,
311 tcpDst=tcp1 )
kelvin-onlabe5239e52015-05-13 14:46:45 -0700312 intentsId.append( intent2 )
313
314 # Check intents state
kelvin-onlabf0594d72015-05-19 17:25:12 -0700315 time.sleep( 30 )
kelvin-onlabe5239e52015-05-13 14:46:45 -0700316 intentResult = checkIntentState( main, intentsId )
317
kelvin-onlab381b34c2015-05-21 09:21:06 -0700318 # Check intents state again if first check fails...
319 if not intentResult:
320 intentResult = checkIntentState( main, intentsId )
321
kelvin-onlabe5239e52015-05-13 14:46:45 -0700322 # Verify flows
323 checkFlowsState( main )
324
325 # Ping hosts
kelvin-onlab08900012015-05-20 14:30:44 -0700326 firstPingResult = pingallHosts( main, hostNames )
kelvin-onlabf0594d72015-05-19 17:25:12 -0700327 if not firstPingResult:
kelvin-onlab08900012015-05-20 14:30:44 -0700328 main.log.debug( "First ping failed, there must be" +
kelvin-onlabf0594d72015-05-19 17:25:12 -0700329 " something wrong with ONOS performance" )
330
kelvin-onlabe5239e52015-05-13 14:46:45 -0700331 # Ping hosts again...
kelvin-onlab08900012015-05-20 14:30:44 -0700332 pingResult = pingResult and pingallHosts( main, hostNames )
kelvin-onlabe5239e52015-05-13 14:46:45 -0700333 time.sleep( 5 )
334
kelvin-onlabf0594d72015-05-19 17:25:12 -0700335 # Test rerouting if these variables exist
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700336 if sw1 and sw2 and expectedLink:
337 # link down
kelvin-onlabf0594d72015-05-19 17:25:12 -0700338 linkDownResult = link( main, sw1, sw2, "down" )
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700339 intentResult = intentResult and checkIntentState( main, intentsId )
kelvin-onlabe5239e52015-05-13 14:46:45 -0700340
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700341 # Verify flows
342 checkFlowsState( main )
kelvin-onlabe5239e52015-05-13 14:46:45 -0700343
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700344 # Check OnosTopology
345 topoResult = checkTopology( main, expectedLink )
kelvin-onlabe5239e52015-05-13 14:46:45 -0700346
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700347 # Ping hosts
kelvin-onlab08900012015-05-20 14:30:44 -0700348 pingResult = pingResult and pingallHosts( main, hostNames )
kelvin-onlabe5239e52015-05-13 14:46:45 -0700349
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700350 intentResult = checkIntentState( main, intentsId )
kelvin-onlabe5239e52015-05-13 14:46:45 -0700351
kelvin-onlabf0594d72015-05-19 17:25:12 -0700352 # Checks ONOS state in link down
353 if linkDownResult and topoResult and pingResult and intentResult:
354 main.log.info( itemName + ": Successfully brought link down" )
355 else:
kelvin-onlab08900012015-05-20 14:30:44 -0700356 main.log.error( itemName + ": Failed to bring link down" )
kelvin-onlabf0594d72015-05-19 17:25:12 -0700357
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700358 # link up
kelvin-onlabf0594d72015-05-19 17:25:12 -0700359 linkUpResult = link( main, sw1, sw2, "up" )
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700360 time.sleep( 5 )
kelvin-onlabe5239e52015-05-13 14:46:45 -0700361
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700362 # Verify flows
363 checkFlowsState( main )
kelvin-onlabe5239e52015-05-13 14:46:45 -0700364
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700365 # Check OnosTopology
kelvin-onlabf0594d72015-05-19 17:25:12 -0700366 topoResult = checkTopology( main, main.numLinks )
kelvin-onlabe5239e52015-05-13 14:46:45 -0700367
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700368 # Ping hosts
kelvin-onlab08900012015-05-20 14:30:44 -0700369 pingResult = pingResult and pingallHosts( main, hostNames )
kelvin-onlabe5239e52015-05-13 14:46:45 -0700370
kelvin-onlabf0594d72015-05-19 17:25:12 -0700371 intentResult = checkIntentState( main, intentsId )
kelvin-onlabe5239e52015-05-13 14:46:45 -0700372
kelvin-onlabf0594d72015-05-19 17:25:12 -0700373 # Checks ONOS state in link up
374 if linkUpResult and topoResult and pingResult and intentResult:
375 main.log.info( itemName + ": Successfully brought link back up" )
376 else:
kelvin-onlab08900012015-05-20 14:30:44 -0700377 main.log.error( itemName + ": Failed to bring link back up" )
kelvin-onlabf0594d72015-05-19 17:25:12 -0700378
379 # Remove all intents
380 removeIntentResult = removeAllIntents( main, intentsId )
381
kelvin-onlabe5239e52015-05-13 14:46:45 -0700382 stepResult = pingResult and linkDownResult and linkUpResult \
kelvin-onlabf0594d72015-05-19 17:25:12 -0700383 and intentResult and removeIntentResult
kelvin-onlabe5239e52015-05-13 14:46:45 -0700384
385 return stepResult
386
kelvin-onlabe7270fa2015-05-18 14:35:03 -0700387def singleToMultiIntent( main,
kelvin-onlabf0594d72015-05-19 17:25:12 -0700388 name,
389 hostNames,
kelvin-onlab90b6b042015-05-18 20:57:13 -0700390 devices="",
kelvin-onlabe7270fa2015-05-18 14:35:03 -0700391 ports=None,
392 ethType="",
393 macs=None,
394 bandwidth="",
395 lambdaAlloc=False,
396 ipProto="",
397 ipAddresses="",
398 tcp="",
399 sw1="",
400 sw2="",
401 expectedLink=0 ):
402 """
kelvin-onlab381b34c2015-05-21 09:21:06 -0700403 Verify Single to Multi Point intents
404 NOTE:If main.hostsData is not defined, variables data should be passed in the
kelvin-onlabf0594d72015-05-19 17:25:12 -0700405 same order index wise. All devices in the list should have the same
406 format, either all the devices have its port or it doesn't.
kelvin-onlab90b6b042015-05-18 20:57:13 -0700407 eg. hostName = [ 'h1', 'h2' ,.. ]
408 devices = [ 'of:0000000000000001', 'of:0000000000000002', ...]
409 ports = [ '1', '1', ..]
410 ...
kelvin-onlab381b34c2015-05-21 09:21:06 -0700411 Description:
kelvin-onlab583fcb52015-05-21 13:10:46 -0700412 Verify add-single-to-multi-intent iterates through the list of given
413 host | devices and add intents
kelvin-onlab381b34c2015-05-21 09:21:06 -0700414 Steps:
415 - Get device ids | ports
416 - Add single to multi point intents
417 - Check intents
418 - Verify flows
419 - Ping hosts
420 - Reroute
421 - Link down
422 - Verify flows
423 - Check topology
424 - Ping hosts
425 - Link up
426 - Verify flows
427 - Check topology
428 - Ping hosts
429 - Remove intents
430 Required:
431 name - Type of point intent to add eg. IPV4 | VLAN | Dualstack
432 hostNames - List of host names
433 Optional:
434 devices - List of device ids in the same order as the hosts
435 in hostNames
436 ports - List of port numbers in the same order as the device in
437 devices
438 ethType - Ethernet type eg. IPV4, IPV6
439 macs - List of hosts mac address in the same order as the hosts in
440 hostNames
441 bandwidth - Bandwidth capacity
442 lambdaAlloc - Allocate lambda, defaults to False
443 ipProto - IP protocol
444 ipAddresses - IP addresses of host in the same order as the hosts in
445 hostNames
446 tcp - TCP ports in the same order as the hosts in hostNames
447 sw1 - First switch to bring down & up for rerouting purpose
448 sw2 - Second switch to bring down & up for rerouting purpose
449 expectedLink - Expected link when the switches are down, it should
450 be two links lower than the links before the two
451 switches are down
kelvin-onlabe7270fa2015-05-18 14:35:03 -0700452 """
kelvin-onlab381b34c2015-05-21 09:21:06 -0700453
kelvin-onlabe7270fa2015-05-18 14:35:03 -0700454 import time
kelvin-onlabf0594d72015-05-19 17:25:12 -0700455 import copy
kelvin-onlabe7270fa2015-05-18 14:35:03 -0700456 assert main, "There is no main variable"
kelvin-onlabe7270fa2015-05-18 14:35:03 -0700457 assert hostNames, "You must specify hosts"
458 assert devices or main.hostsData, "You must specify devices"
459
460 global itemName
461 itemName = name
kelvin-onlab90b6b042015-05-18 20:57:13 -0700462 tempHostsData = {}
kelvin-onlabe7270fa2015-05-18 14:35:03 -0700463 intentsId = []
464
kelvin-onlab08900012015-05-20 14:30:44 -0700465 macsDict = {}
466 ipDict = {}
kelvin-onlab90b6b042015-05-18 20:57:13 -0700467 if hostNames and devices:
468 if len( hostNames ) != len( devices ):
kelvin-onlab08900012015-05-20 14:30:44 -0700469 main.log.debug( "hosts and devices does not have the same length" )
470 #print "len hostNames = ", len( hostNames )
471 #print "len devices = ", len( devices )
kelvin-onlab90b6b042015-05-18 20:57:13 -0700472 return main.FALSE
473 if ports:
474 if len( ports ) != len( devices ):
475 main.log.error( "Ports and devices does " +
476 "not have the same length" )
kelvin-onlab08900012015-05-20 14:30:44 -0700477 #print "len devices = ", len( devices )
478 #print "len ports = ", len( ports )
kelvin-onlab90b6b042015-05-18 20:57:13 -0700479 return main.FALSE
kelvin-onlab08900012015-05-20 14:30:44 -0700480 for i in range( len( devices ) ):
481 macsDict[ devices[ i ] ] = macs[ i ]
kelvin-onlab90b6b042015-05-18 20:57:13 -0700482 else:
483 main.log.info( "Device Ports are not specified" )
484 elif hostNames and not devices and main.hostsData:
kelvin-onlab08900012015-05-20 14:30:44 -0700485 devices = []
kelvin-onlab90b6b042015-05-18 20:57:13 -0700486 main.log.info( "singleToMultiIntent function is using main.hostsData" )
kelvin-onlab08900012015-05-20 14:30:44 -0700487 for host in hostNames:
kelvin-onlab08900012015-05-20 14:30:44 -0700488 devices.append( main.hostsData.get( host ).get( 'location' ) )
489 macsDict[ main.hostsData.get( host ).get( 'location' ) ] = \
490 main.hostsData.get( host ).get( 'mac' )
491 ipDict[ main.hostsData.get( host ).get( 'location' ) ] = \
492 main.hostsData.get( host ).get( 'ipAddresses' )
kelvin-onlab381b34c2015-05-21 09:21:06 -0700493 #print main.hostsData
kelvin-onlabe7270fa2015-05-18 14:35:03 -0700494
kelvin-onlab381b34c2015-05-21 09:21:06 -0700495 #print 'host names = ', hostNames
496 #print 'devices = ', devices
497 #print "macsDict = ", macsDict
kelvin-onlabf0594d72015-05-19 17:25:12 -0700498
kelvin-onlabe7270fa2015-05-18 14:35:03 -0700499 pingResult = main.TRUE
500 intentResult = main.TRUE
kelvin-onlabf0594d72015-05-19 17:25:12 -0700501 removeIntentResult = main.TRUE
kelvin-onlabe7270fa2015-05-18 14:35:03 -0700502 flowResult = main.TRUE
503 topoResult = main.TRUE
504 linkDownResult = main.TRUE
505 linkUpResult = main.TRUE
506
kelvin-onlabf0594d72015-05-19 17:25:12 -0700507 devicesCopy = copy.copy( devices )
508 if ports:
509 portsCopy = copy.copy( ports )
510 main.log.info( itemName + ": Adding single point to multi point intents" )
kelvin-onlabe7270fa2015-05-18 14:35:03 -0700511 # Adding bidirectional point intents
kelvin-onlabf0594d72015-05-19 17:25:12 -0700512 for i in range( len( devices ) ):
513 ingressDevice = devicesCopy[ i ]
514 egressDeviceList = copy.copy( devicesCopy )
515 egressDeviceList.remove( ingressDevice )
516 if ports:
517 portIngress = portsCopy[ i ]
518 portEgressList = copy.copy( portsCopy )
519 del portEgressList[ i ]
520 else:
521 portIngress = ""
522 portEgressList = None
kelvin-onlab08900012015-05-20 14:30:44 -0700523 if not macsDict:
524 srcMac = ""
525 else:
526 srcMac = macsDict[ ingressDevice ]
527 if srcMac == None:
528 srcMac = ""
529
kelvin-onlabf0594d72015-05-19 17:25:12 -0700530 intentsId.append( main.CLIs[ 0 ].addSinglepointToMultipointIntent(
531 ingressDevice=ingressDevice,
532 egressDeviceList=egressDeviceList,
533 portIngress=portIngress,
534 portEgressList=portEgressList,
kelvin-onlab90b6b042015-05-18 20:57:13 -0700535 ethType=ethType,
kelvin-onlab08900012015-05-20 14:30:44 -0700536 ethSrc=srcMac,
kelvin-onlab90b6b042015-05-18 20:57:13 -0700537 bandwidth=bandwidth,
538 lambdaAlloc=lambdaAlloc,
539 ipProto=ipProto,
kelvin-onlabf0594d72015-05-19 17:25:12 -0700540 ipSrc="",
541 ipDst="",
542 tcpSrc="",
543 tcpDst="" ) )
kelvin-onlabe7270fa2015-05-18 14:35:03 -0700544
kelvin-onlab08900012015-05-20 14:30:44 -0700545 pingResult = pingallHosts( main, hostNames )
kelvin-onlabe7270fa2015-05-18 14:35:03 -0700546
547 # Check intents state
kelvin-onlabf0594d72015-05-19 17:25:12 -0700548 time.sleep( 30 )
kelvin-onlabe7270fa2015-05-18 14:35:03 -0700549 intentResult = checkIntentState( main, intentsId )
550
kelvin-onlab381b34c2015-05-21 09:21:06 -0700551 # Check intents state again if first check fails...
552 if not intentResult:
553 intentResult = checkIntentState( main, intentsId )
554
kelvin-onlabe7270fa2015-05-18 14:35:03 -0700555 # Verify flows
556 checkFlowsState( main )
557
558 # Ping hosts
kelvin-onlab08900012015-05-20 14:30:44 -0700559 pingResult = pingResult and pingallHosts( main, hostNames )
kelvin-onlabe7270fa2015-05-18 14:35:03 -0700560 # Ping hosts again...
kelvin-onlab08900012015-05-20 14:30:44 -0700561 pingResult = pingResult and pingallHosts( main, hostNames )
kelvin-onlabe7270fa2015-05-18 14:35:03 -0700562 time.sleep( 5 )
563
kelvin-onlabf0594d72015-05-19 17:25:12 -0700564 # Test rerouting if these variables exist
kelvin-onlabe7270fa2015-05-18 14:35:03 -0700565 if sw1 and sw2 and expectedLink:
566 # link down
kelvin-onlabf0594d72015-05-19 17:25:12 -0700567 linkDownResult = link( main, sw1, sw2, "down" )
kelvin-onlabe7270fa2015-05-18 14:35:03 -0700568 intentResult = intentResult and checkIntentState( main, intentsId )
569
570 # Verify flows
571 checkFlowsState( main )
572
573 # Check OnosTopology
574 topoResult = checkTopology( main, expectedLink )
575
576 # Ping hosts
kelvin-onlab08900012015-05-20 14:30:44 -0700577 pingResult = pingResult and pingallHosts( main, hostNames )
kelvin-onlabe7270fa2015-05-18 14:35:03 -0700578
579 intentResult = checkIntentState( main, intentsId )
580
kelvin-onlabf0594d72015-05-19 17:25:12 -0700581 # Checks ONOS state in link down
582 if linkDownResult and topoResult and pingResult and intentResult:
583 main.log.info( itemName + ": Successfully brought link down" )
584 else:
kelvin-onlab08900012015-05-20 14:30:44 -0700585 main.log.error( itemName + ": Failed to bring link down" )
kelvin-onlabf0594d72015-05-19 17:25:12 -0700586
kelvin-onlabe7270fa2015-05-18 14:35:03 -0700587 # link up
kelvin-onlabf0594d72015-05-19 17:25:12 -0700588 linkUpResult = link( main, sw1, sw2, "up" )
kelvin-onlabe7270fa2015-05-18 14:35:03 -0700589 time.sleep( 5 )
590
591 # Verify flows
592 checkFlowsState( main )
593
594 # Check OnosTopology
kelvin-onlabf0594d72015-05-19 17:25:12 -0700595 topoResult = checkTopology( main, main.numLinks )
kelvin-onlabe7270fa2015-05-18 14:35:03 -0700596
597 # Ping hosts
kelvin-onlab08900012015-05-20 14:30:44 -0700598 pingResult = pingResult and pingallHosts( main, hostNames )
kelvin-onlabe7270fa2015-05-18 14:35:03 -0700599
kelvin-onlabf0594d72015-05-19 17:25:12 -0700600 intentResult = checkIntentState( main, intentsId )
kelvin-onlabe7270fa2015-05-18 14:35:03 -0700601
kelvin-onlabf0594d72015-05-19 17:25:12 -0700602 # Checks ONOS state in link up
603 if linkUpResult and topoResult and pingResult and intentResult:
604 main.log.info( itemName + ": Successfully brought link back up" )
605 else:
kelvin-onlab08900012015-05-20 14:30:44 -0700606 main.log.error( itemName + ": Failed to bring link back up" )
kelvin-onlabf0594d72015-05-19 17:25:12 -0700607
608 # Remove all intents
609 removeIntentResult = removeAllIntents( main, intentsId )
610
kelvin-onlabe7270fa2015-05-18 14:35:03 -0700611 stepResult = pingResult and linkDownResult and linkUpResult \
kelvin-onlabf0594d72015-05-19 17:25:12 -0700612 and intentResult and removeIntentResult
kelvin-onlabe7270fa2015-05-18 14:35:03 -0700613
614 return stepResult
615
kelvin-onlab08900012015-05-20 14:30:44 -0700616def pingallHosts( main, hostList, pingType="ipv4" ):
kelvin-onlabf0594d72015-05-19 17:25:12 -0700617 # Ping all host in the hosts list variable
kelvin-onlab08900012015-05-20 14:30:44 -0700618 print "Pinging : ", hostList
kelvin-onlabf0594d72015-05-19 17:25:12 -0700619 pingResult = main.TRUE
kelvin-onlab08900012015-05-20 14:30:44 -0700620 pingResult = main.Mininet1.pingallHosts( hostList, pingType )
kelvin-onlabf0594d72015-05-19 17:25:12 -0700621 return pingResult
622
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700623def getHostsData( main ):
kelvin-onlabe5239e52015-05-13 14:46:45 -0700624 """
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700625 Use fwd app and pingall to discover all the hosts
kelvin-onlabe5239e52015-05-13 14:46:45 -0700626 """
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700627 import json
628 activateResult = main.TRUE
629 appCheck = main.TRUE
kelvin-onlab08900012015-05-20 14:30:44 -0700630 getDataResult = main.TRUE
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700631 main.log.info( "Activating reactive forwarding app " )
632 activateResult = main.CLIs[ 0 ].activateApp( "org.onosproject.fwd" )
633
634 for i in range( main.numCtrls ):
635 appCheck = appCheck and main.CLIs[ i ].appToIDCheck()
kelvin-onlabf0594d72015-05-19 17:25:12 -0700636 if appCheck != main.TRUE:
kelvin-onlab08900012015-05-20 14:30:44 -0700637 main.log.warn( main.CLIs[ i ].apps() )
638 main.log.warn( main.CLIs[ i ].appIDs() )
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700639
640 pingResult = main.Mininet1.pingall()
641 hostsJson = json.loads( main.CLIs[ 0 ].hosts() )
642 hosts = main.Mininet1.getHosts()
643 for host in hosts:
644 main.hostsData[ host ] = {}
kelvin-onlabe7270fa2015-05-18 14:35:03 -0700645 main.hostsData[ host ][ 'mac' ] = \
646 main.Mininet1.getMacAddress( host ).upper()
kelvin-onlab2ccad6e2015-05-18 10:36:54 -0700647 for hostj in hostsJson:
648 if main.hostsData[ host ][ 'mac' ] == hostj[ 'mac' ]:
649 main.hostsData[ host ][ 'id' ] = hostj[ 'id' ]
650 main.hostsData[ host ][ 'vlan' ] = hostj[ 'vlan' ]
651 main.hostsData[ host ][ 'location' ] = \
652 hostj[ 'location' ][ 'elementId' ] + '/' + \
653 hostj[ 'location' ][ 'port' ]
654 main.hostsData[ host ][ 'ipAddresses' ] = hostj[ 'ipAddresses' ]
655
kelvin-onlabe7270fa2015-05-18 14:35:03 -0700656 main.log.info( "Deactivating reactive forwarding app " )
kelvin-onlabf0594d72015-05-19 17:25:12 -0700657 deactivateResult = main.CLIs[ 0 ].deactivateApp( "org.onosproject.fwd" )
kelvin-onlab08900012015-05-20 14:30:44 -0700658 if activateResult and deactivateResult and main.hostsData:
kelvin-onlabf0594d72015-05-19 17:25:12 -0700659 main.log.info( "Successfully used fwd app to discover hosts " )
kelvin-onlab08900012015-05-20 14:30:44 -0700660 getDataResult = main.TRUE
kelvin-onlabf0594d72015-05-19 17:25:12 -0700661 else:
662 main.log.info( "Failed to use fwd app to discover hosts " )
kelvin-onlab08900012015-05-20 14:30:44 -0700663 getDataResult = main.FALSE
kelvin-onlabf0594d72015-05-19 17:25:12 -0700664
kelvin-onlab08900012015-05-20 14:30:44 -0700665 print main.hostsData
kelvin-onlabf0594d72015-05-19 17:25:12 -0700666
kelvin-onlab08900012015-05-20 14:30:44 -0700667 return getDataResult
kelvin-onlabe5239e52015-05-13 14:46:45 -0700668
669def checkTopology( main, expectedLink ):
670 statusResult = main.TRUE
671 # Check onos topology
672 main.log.info( itemName + ": Checking ONOS topology " )
673
674 for i in range( main.numCtrls ):
675 topologyResult = main.CLIs[ i ].topology()
676 statusResult = main.ONOSbench.checkStatus( topologyResult,
677 main.numSwitch,
678 expectedLink )\
679 and statusResult
680 if not statusResult:
kelvin-onlab381b34c2015-05-21 09:21:06 -0700681 main.log.error( itemName + ": Topology mismatch" )
kelvin-onlabe5239e52015-05-13 14:46:45 -0700682 else:
683 main.log.info( itemName + ": Topology match" )
684 return statusResult
685
686def checkIntentState( main, intentsId ):
687
kelvin-onlab381b34c2015-05-21 09:21:06 -0700688 intentResult = main.TRUE
689
690 main.log.info( itemName + ": Checking intents state" )
kelvin-onlabe5239e52015-05-13 14:46:45 -0700691 for i in range( main.numCtrls ):
kelvin-onlab381b34c2015-05-21 09:21:06 -0700692 intentResult = intentResult and \
693 main.CLIs[ i ].checkIntentState( intentsId=intentsId )
694
kelvin-onlabe5239e52015-05-13 14:46:45 -0700695 return intentResult
696
697def checkFlowsState( main ):
698
699 main.log.info( itemName + ": Check flows state" )
700 checkFlowsResult = main.CLIs[ 0 ].checkFlowsState()
701 return checkFlowsResult
702
kelvin-onlabf0594d72015-05-19 17:25:12 -0700703def link( main, sw1, sw2, option):
kelvin-onlabe5239e52015-05-13 14:46:45 -0700704
kelvin-onlabf0594d72015-05-19 17:25:12 -0700705 # link down
706 main.log.info( itemName + ": Bring link " + option + "between " +
707 sw1 + " and " + sw2 )
708 linkResult = main.Mininet1.link( end1=sw1, end2=sw2, option=option )
709 return linkResult
710
711def removeAllIntents( main, intentsId ):
712 """
713 Remove all intents in the intentsId
714 """
715 import time
716 intentsRemaining = []
717 removeIntentResult = main.TRUE
718 # Remove intents
719 for intent in intentsId:
720 main.CLIs[ 0 ].removeIntent( intentId=intent, purge=True )
721
722 time.sleep( 5 )
723 # Checks if there is remaining intents using intents()
kelvin-onlab583fcb52015-05-21 13:10:46 -0700724 intentsRemaining = main.CLIs[ 0 ].intents( jsonFormat=False )
kelvin-onlabf0594d72015-05-19 17:25:12 -0700725 # If there is remianing intents then remove intents should fail
kelvin-onlab583fcb52015-05-21 13:10:46 -0700726
727 if intentsRemaining:
kelvin-onlabf0594d72015-05-19 17:25:12 -0700728 main.log.info( itemName + ": There are " +
729 str( len( intentsRemaining ) ) + " intents remaining, "
730 + "failed to remove all the intents " )
731 removeIntentResult = main.FALSE
kelvin-onlab08900012015-05-20 14:30:44 -0700732 main.log.info( intentsRemaining )
kelvin-onlabf0594d72015-05-19 17:25:12 -0700733 else:
734 main.log.info( itemName + ": There are no intents remaining, " +
735 "successfully removed all the intents." )
736 removeIntentResult = main.TRUE
737 return removeIntentResult