blob: 87f2015d8f1440e25616f080de786e7306628a70 [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
75def verifyFlows( main, expectedFlows, state="ADDED", sleep=1, timeout=120):
76 '''
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
83 timeout: the timeout for pexpect
84 '''
85 cmd = "flows | grep " + state + " | wc -l"
86 for i in range(10):
87 flows = getFlows( main, state, sleep, timeout )
88 if expectedFlows == flows:
89 return main.TRUE
90
91 return main.FALSE
92
93def verifyIntents( main, expectedIntents, state="INSTALLED", sleep=1, timeout=120):
94 '''
95 This function returns main.TRUE if the number of expected intents are in
96 the specified state
97
98 @params
99 expectedFlows: the intents you expect to see in the specified state
100 state: the state of the intent to check for
101 timeout: the timeout for pexpect
102 '''
103 cmd = "intents | grep " + state + " | wc -l"
104 for i in range(10):
105 intents = getIntents( main, state, sleep, timeout )
106 if expectedIntents == intents:
107 return main.TRUE
108
109 return main.FALSE