blob: 268279c5f5436096453194be3d345c2429fe66fb [file] [log] [blame]
GlennRCb3202c52015-08-24 14:43:30 -07001'''
2 Wrapper functions for maxIntent
3'''
4
5import json
6import time
GlennRCd0e5ed22015-09-03 11:54:27 -07007import pexpect
GlennRCb3202c52015-08-24 14:43:30 -07008
9def __init__( self ):
10 self.default = ""
11
12def getIntents( main, state="INSTALLED", sleep=1, timeout=120 ):
GlennRCd0e5ed22015-09-03 11:54:27 -070013 intents = 0
14 try:
15 cmd = "intents | grep " + state + " | wc -l"
16 main.log.info("Sending: " + cmd)
17 main.CLIs[0].handle.sendline(cmd)
GlennRCb3202c52015-08-24 14:43:30 -070018
GlennRCd0e5ed22015-09-03 11:54:27 -070019 time.sleep(sleep)
GlennRCb3202c52015-08-24 14:43:30 -070020
GlennRCd0e5ed22015-09-03 11:54:27 -070021 main.CLIs[0].handle.expect("onos>", timeout=timeout)
22 raw = main.CLIs[0].handle.before
23 intents = int(main.CLIs[0].handle.before.split()[7])
GlennRC5719ff02015-09-08 17:25:30 -070024 main.log.info(state + " intents: " + str(intents))
GlennRCd0e5ed22015-09-03 11:54:27 -070025 except pexpect.TIMEOUT:
GlennRC5719ff02015-09-08 17:25:30 -070026 main.log.exception("Timeout exception caught in getIntent")
GlennRCb3202c52015-08-24 14:43:30 -070027 return intents
28
GlennRCb3202c52015-08-24 14:43:30 -070029def getFlows( main, state="ADDED", sleep=1, timeout=120 ):
GlennRCd0e5ed22015-09-03 11:54:27 -070030 flows = 0
31 try:
32 cmd = "flows | grep " + state + " | wc -l"
33 main.log.info("Sending: " + cmd)
34 main.CLIs[0].handle.sendline(cmd)
GlennRCb3202c52015-08-24 14:43:30 -070035
GlennRCd0e5ed22015-09-03 11:54:27 -070036 time.sleep(sleep)
GlennRCb3202c52015-08-24 14:43:30 -070037
GlennRCd0e5ed22015-09-03 11:54:27 -070038 main.CLIs[0].handle.expect("onos>", timeout=timeout)
39 raw = main.CLIs[0].handle.before
40 flows = int(main.CLIs[0].handle.before.split()[7])
GlennRC5719ff02015-09-08 17:25:30 -070041 main.log.info(state + " flows: " + str(flows))
GlennRCd0e5ed22015-09-03 11:54:27 -070042 except pexpect.TIMEOUT:
GlennRC5719ff02015-09-08 17:25:30 -070043 main.log.exception("Timeout exception caught in getFlows")
GlennRCb3202c52015-08-24 14:43:30 -070044 return flows
45
46
47def pushIntents( main,
48 switch,
49 ingress,
50 egress,
51 batch,
52 offset,
53 sleep=1,
54 options="",
55 timeout=120):
56 '''
GlennRCd0e5ed22015-09-03 11:54:27 -070057 Pushes intents using the push-test-intents cli command.
GlennRCb3202c52015-08-24 14:43:30 -070058 '''
GlennRCd0e5ed22015-09-03 11:54:27 -070059 try:
60 cmd = "push-test-intents " + options + " " + switch + ingress + " " +\
61 switch + egress + " " + str(batch) + " " + str(offset)
62 main.log.info("Installing " + str(offset+batch) + " intents")
63 main.log.debug("Sending: " + cmd)
64 main.CLIs[0].handle.sendline(cmd)
65 time.sleep(sleep)
66 main.CLIs[0].handle.expect("onos>", timeout=timeout)
GlennRCb3202c52015-08-24 14:43:30 -070067
GlennRCd0e5ed22015-09-03 11:54:27 -070068 raw = main.CLIs[0].handle.before
69 if "Failure:" not in raw and "GC" not in raw:
70 return main.TRUE
71 except pexpect.TIMEOUT:
GlennRC5719ff02015-09-08 17:25:30 -070072 main.log.exception("Timeout exception caught in pushIntents")
GlennRCd0e5ed22015-09-03 11:54:27 -070073 return main.FALSE
GlennRCb3202c52015-08-24 14:43:30 -070074
GlennRC4c745432015-09-17 17:09:13 -070075def verifyFlows( main, expectedFlows, state="ADDED", sleep=1, numcheck=10, timeout=120):
GlennRCb3202c52015-08-24 14:43:30 -070076 '''
77 This function returns main.TRUE if the number of expected flows are in
78 the specified state
79
80 @params
81 expectedFlows: the flows you expect to see in the specified state
82 state: the state of the flow to check for
GlennRC4c745432015-09-17 17:09:13 -070083 sleep: how long it should sleep for each check
84 numcheck: how many times it should check
GlennRCb3202c52015-08-24 14:43:30 -070085 timeout: the timeout for pexpect
86 '''
87 cmd = "flows | grep " + state + " | wc -l"
GlennRC4c745432015-09-17 17:09:13 -070088 for i in range(numcheck):
GlennRCb3202c52015-08-24 14:43:30 -070089 flows = getFlows( main, state, sleep, timeout )
90 if expectedFlows == flows:
91 return main.TRUE
92
93 return main.FALSE
94
GlennRC4c745432015-09-17 17:09:13 -070095def verifyIntents( main, expectedIntents, state="INSTALLED", sleep=1, numcheck=10, timeout=120):
GlennRCb3202c52015-08-24 14:43:30 -070096 '''
97 This function returns main.TRUE if the number of expected intents are in
98 the specified state
99
100 @params
101 expectedFlows: the intents you expect to see in the specified state
102 state: the state of the intent to check for
GlennRC4c745432015-09-17 17:09:13 -0700103 sleep: how long it should sleep for each check
104 numcheck: how many times it should check
GlennRCb3202c52015-08-24 14:43:30 -0700105 timeout: the timeout for pexpect
106 '''
107 cmd = "intents | grep " + state + " | wc -l"
GlennRC4c745432015-09-17 17:09:13 -0700108 for i in range(numcheck):
GlennRCb3202c52015-08-24 14:43:30 -0700109 intents = getIntents( main, state, sleep, timeout )
110 if expectedIntents == intents:
111 return main.TRUE
GlennRC4c745432015-09-17 17:09:13 -0700112 time.sleep(sleep)
GlennRCb3202c52015-08-24 14:43:30 -0700113
114 return main.FALSE