blob: 7e912d07ac959c9af66509c280bd6efbd82fd999 [file] [log] [blame]
You Wangdb927a52016-02-26 11:03:28 -08001"""
2Insert network/ONOS/app events into CHOTestMonkey
3Author: you@onlab.us
4"""
5import time
6import random
7from multiprocessing.connection import Client
8
Jon Hall2bb3e212017-05-24 17:07:25 -07009
You Wangdb927a52016-02-26 11:03:28 -080010def triggerEvent( type, scheduleMethod, *args ):
11 """
12 This function inserts an event into CHOTestMonkey
13 """
14 host = "localhost"
15 port = 6000
16 address = ( host, port )
17 conn = Client( address )
18 request = []
You Wang52163202016-07-14 16:37:15 -070019 request.append( 1 )
You Wangdb927a52016-02-26 11:03:28 -080020 request.append( type )
21 request.append( scheduleMethod )
22 for arg in args:
23 request.append( arg )
24 conn.send( request )
25 response = conn.recv()
26 while response == 11:
You Wang52163202016-07-14 16:37:15 -070027 conn.close()
You Wangdb927a52016-02-26 11:03:28 -080028 time.sleep( 1 )
You Wang52163202016-07-14 16:37:15 -070029 conn = Client( address )
You Wangdb927a52016-02-26 11:03:28 -080030 conn.send( request )
31 response = conn.recv()
32 if response == 10:
33 print "Event inserted:", type, scheduleMethod, args
34 elif response == 20:
35 print "Unknown message to server"
36 elif response == 21:
37 print "Unknown event type to server"
38 elif response == 22:
39 print "Unknown schedule method to server"
40 elif response == 23:
41 print "Not enough argument"
42 else:
43 print "Unknown response from server:", response
44 conn.close()
45
Jon Hall2bb3e212017-05-24 17:07:25 -070046
You Wangdb927a52016-02-26 11:03:28 -080047def testLoop( sleepTime=5 ):
48 downLinkNum = 0
49 downDeviceNum = 0
50 while True:
51 r = random.random()
52 if r < 0.2:
53 triggerEvent( 'NETWORK_LINK_DOWN', 'RUN_BLOCK', 'random', 'random' )
54 downLinkNum += 1
55 time.sleep( sleepTime )
56 elif r < 0.4:
57 triggerEvent( 'NETWORK_DEVICE_DOWN', 'RUN_BLOCK', 'random' )
58 downDeviceNum += 1
59 time.sleep( sleepTime * 2 )
60 elif r < 0.7 and downLinkNum > 0:
61 triggerEvent( 'NETWORK_LINK_UP', 'RUN_BLOCK', 'random', 'random' )
62 downLinkNum -= 1
63 time.sleep( sleepTime )
64 elif downDeviceNum > 0:
65 triggerEvent( 'NETWORK_DEVICE_UP', 'RUN_BLOCK', 'random' )
66 downDeviceNum -= 1
67 time.sleep( sleepTime * 2 )
68 else:
69 pass
70
Jon Hall2bb3e212017-05-24 17:07:25 -070071
You Wang52163202016-07-14 16:37:15 -070072def replayFromFile( filePath='/home/admin/event-list', sleepTime=1 ):
73 try:
74 f = open( filePath, 'r' )
75 for line in f.readlines():
76 event = line.split()
77 if event[ 3 ].startswith( 'CHECK' ):
78 continue
79 triggerEvent( event[ 3 ], 'RUN_BLOCK', *event[ 4: ] )
80 time.sleep( sleepTime )
81 f.close()
82 except Exception as e:
83 print e
84
You Wangdb927a52016-02-26 11:03:28 -080085if __name__ == '__main__':
You Wang52163202016-07-14 16:37:15 -070086 #testLoop( 2 )
87 replayFromFile()