blob: 27b8166f3ebc5596a01f50a79ad205808dcd68da [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
9def triggerEvent( type, scheduleMethod, *args ):
10 """
11 This function inserts an event into CHOTestMonkey
12 """
13 host = "localhost"
14 port = 6000
15 address = ( host, port )
16 conn = Client( address )
17 request = []
You Wang52163202016-07-14 16:37:15 -070018 request.append( 1 )
You Wangdb927a52016-02-26 11:03:28 -080019 request.append( type )
20 request.append( scheduleMethod )
21 for arg in args:
22 request.append( arg )
23 conn.send( request )
24 response = conn.recv()
25 while response == 11:
You Wang52163202016-07-14 16:37:15 -070026 conn.close()
You Wangdb927a52016-02-26 11:03:28 -080027 time.sleep( 1 )
You Wang52163202016-07-14 16:37:15 -070028 conn = Client( address )
You Wangdb927a52016-02-26 11:03:28 -080029 conn.send( request )
30 response = conn.recv()
31 if response == 10:
32 print "Event inserted:", type, scheduleMethod, args
33 elif response == 20:
34 print "Unknown message to server"
35 elif response == 21:
36 print "Unknown event type to server"
37 elif response == 22:
38 print "Unknown schedule method to server"
39 elif response == 23:
40 print "Not enough argument"
41 else:
42 print "Unknown response from server:", response
43 conn.close()
44
45def testLoop( sleepTime=5 ):
46 downLinkNum = 0
47 downDeviceNum = 0
48 while True:
49 r = random.random()
50 if r < 0.2:
51 triggerEvent( 'NETWORK_LINK_DOWN', 'RUN_BLOCK', 'random', 'random' )
52 downLinkNum += 1
53 time.sleep( sleepTime )
54 elif r < 0.4:
55 triggerEvent( 'NETWORK_DEVICE_DOWN', 'RUN_BLOCK', 'random' )
56 downDeviceNum += 1
57 time.sleep( sleepTime * 2 )
58 elif r < 0.7 and downLinkNum > 0:
59 triggerEvent( 'NETWORK_LINK_UP', 'RUN_BLOCK', 'random', 'random' )
60 downLinkNum -= 1
61 time.sleep( sleepTime )
62 elif downDeviceNum > 0:
63 triggerEvent( 'NETWORK_DEVICE_UP', 'RUN_BLOCK', 'random' )
64 downDeviceNum -= 1
65 time.sleep( sleepTime * 2 )
66 else:
67 pass
68
You Wang52163202016-07-14 16:37:15 -070069def replayFromFile( filePath='/home/admin/event-list', sleepTime=1 ):
70 try:
71 f = open( filePath, 'r' )
72 for line in f.readlines():
73 event = line.split()
74 if event[ 3 ].startswith( 'CHECK' ):
75 continue
76 triggerEvent( event[ 3 ], 'RUN_BLOCK', *event[ 4: ] )
77 time.sleep( sleepTime )
78 f.close()
79 except Exception as e:
80 print e
81
You Wangdb927a52016-02-26 11:03:28 -080082if __name__ == '__main__':
You Wang52163202016-07-14 16:37:15 -070083 #testLoop( 2 )
84 replayFromFile()