blob: 128ef6ba6d3403e4c2081c4e24e195903254c07e [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])
24 main.log.info(state + "intents: " + str(intents))
25 except pexpect.TIMEOUT:
26 main.log.exception("Timeout exception found")
27 except pexpect.EOF:
28 main.log.error("EOF exception found")
GlennRCb3202c52015-08-24 14:43:30 -070029 return intents
30
GlennRCb3202c52015-08-24 14:43:30 -070031def getFlows( main, state="ADDED", sleep=1, timeout=120 ):
GlennRCd0e5ed22015-09-03 11:54:27 -070032 flows = 0
33 try:
34 cmd = "flows | grep " + state + " | wc -l"
35 main.log.info("Sending: " + cmd)
36 main.CLIs[0].handle.sendline(cmd)
GlennRCb3202c52015-08-24 14:43:30 -070037
GlennRCd0e5ed22015-09-03 11:54:27 -070038 time.sleep(sleep)
GlennRCb3202c52015-08-24 14:43:30 -070039
GlennRCd0e5ed22015-09-03 11:54:27 -070040 main.CLIs[0].handle.expect("onos>", timeout=timeout)
41 raw = main.CLIs[0].handle.before
42 flows = int(main.CLIs[0].handle.before.split()[7])
43 main.log.info(state + "flows: " + str(flows))
44 except pexpect.TIMEOUT:
45 main.log.exception("Timeout exception found")
46 except pexpect.EOF:
47 main.log.error("EOF exception found")
GlennRCb3202c52015-08-24 14:43:30 -070048 return flows
49
50
51def pushIntents( main,
52 switch,
53 ingress,
54 egress,
55 batch,
56 offset,
57 sleep=1,
58 options="",
59 timeout=120):
60 '''
GlennRCd0e5ed22015-09-03 11:54:27 -070061 Pushes intents using the push-test-intents cli command.
GlennRCb3202c52015-08-24 14:43:30 -070062 '''
GlennRCd0e5ed22015-09-03 11:54:27 -070063 try:
64 cmd = "push-test-intents " + options + " " + switch + ingress + " " +\
65 switch + egress + " " + str(batch) + " " + str(offset)
66 main.log.info("Installing " + str(offset+batch) + " intents")
67 main.log.debug("Sending: " + cmd)
68 main.CLIs[0].handle.sendline(cmd)
69 time.sleep(sleep)
70 main.CLIs[0].handle.expect("onos>", timeout=timeout)
GlennRCb3202c52015-08-24 14:43:30 -070071
GlennRCd0e5ed22015-09-03 11:54:27 -070072 raw = main.CLIs[0].handle.before
73 if "Failure:" not in raw and "GC" not in raw:
74 return main.TRUE
75 except pexpect.TIMEOUT:
76 main.log.exception("Timeout exception found")
77 except pexpect.EOF:
78 main.log.error("EOF exception found")
79 return main.FALSE
GlennRCb3202c52015-08-24 14:43:30 -070080
81def verifyFlows( main, expectedFlows, state="ADDED", sleep=1, timeout=120):
82 '''
83 This function returns main.TRUE if the number of expected flows are in
84 the specified state
85
86 @params
87 expectedFlows: the flows you expect to see in the specified state
88 state: the state of the flow to check for
89 timeout: the timeout for pexpect
90 '''
91 cmd = "flows | grep " + state + " | wc -l"
92 for i in range(10):
93 flows = getFlows( main, state, sleep, timeout )
94 if expectedFlows == flows:
95 return main.TRUE
96
97 return main.FALSE
98
99def verifyIntents( main, expectedIntents, state="INSTALLED", sleep=1, timeout=120):
100 '''
101 This function returns main.TRUE if the number of expected intents are in
102 the specified state
103
104 @params
105 expectedFlows: the intents you expect to see in the specified state
106 state: the state of the intent to check for
107 timeout: the timeout for pexpect
108 '''
109 cmd = "intents | grep " + state + " | wc -l"
110 for i in range(10):
111 intents = getIntents( main, state, sleep, timeout )
112 if expectedIntents == intents:
113 return main.TRUE
114
115 return main.FALSE