blob: 878071e346482d28132e696c978ff35ad8ab63d5 [file] [log] [blame]
You Wangdb927a52016-02-26 11:03:28 -08001"""
Jeremy Ronquillob27ce4c2017-07-17 12:41:28 -07002Copyright 2016 Open Networking Foundation (ONF)
3
4Please refer questions to either the onos test mailing list at <onos-test@onosproject.org>,
5the System Testing Plans and Results wiki page at <https://wiki.onosproject.org/x/voMg>,
6or the System Testing Guide page at <https://wiki.onosproject.org/x/WYQg>
7
8 TestON is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 2 of the License, or
11 (at your option) any later version.
12
13 TestON is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with TestON. If not, see <http://www.gnu.org/licenses/>.
20"""
21
22"""
You Wangdb927a52016-02-26 11:03:28 -080023Insert network/ONOS/app events into CHOTestMonkey
24Author: you@onlab.us
25"""
26import time
27import random
28from multiprocessing.connection import Client
29
Jon Hall2bb3e212017-05-24 17:07:25 -070030
You Wangdb927a52016-02-26 11:03:28 -080031def triggerEvent( type, scheduleMethod, *args ):
32 """
33 This function inserts an event into CHOTestMonkey
34 """
35 host = "localhost"
36 port = 6000
37 address = ( host, port )
38 conn = Client( address )
39 request = []
You Wang52163202016-07-14 16:37:15 -070040 request.append( 1 )
You Wangdb927a52016-02-26 11:03:28 -080041 request.append( type )
42 request.append( scheduleMethod )
43 for arg in args:
44 request.append( arg )
45 conn.send( request )
46 response = conn.recv()
47 while response == 11:
You Wang52163202016-07-14 16:37:15 -070048 conn.close()
You Wangdb927a52016-02-26 11:03:28 -080049 time.sleep( 1 )
You Wang52163202016-07-14 16:37:15 -070050 conn = Client( address )
You Wangdb927a52016-02-26 11:03:28 -080051 conn.send( request )
52 response = conn.recv()
53 if response == 10:
54 print "Event inserted:", type, scheduleMethod, args
55 elif response == 20:
56 print "Unknown message to server"
57 elif response == 21:
58 print "Unknown event type to server"
59 elif response == 22:
60 print "Unknown schedule method to server"
61 elif response == 23:
62 print "Not enough argument"
63 else:
64 print "Unknown response from server:", response
65 conn.close()
66
Jon Hall2bb3e212017-05-24 17:07:25 -070067
You Wangdb927a52016-02-26 11:03:28 -080068def testLoop( sleepTime=5 ):
69 downLinkNum = 0
70 downDeviceNum = 0
71 while True:
72 r = random.random()
73 if r < 0.2:
74 triggerEvent( 'NETWORK_LINK_DOWN', 'RUN_BLOCK', 'random', 'random' )
75 downLinkNum += 1
76 time.sleep( sleepTime )
77 elif r < 0.4:
78 triggerEvent( 'NETWORK_DEVICE_DOWN', 'RUN_BLOCK', 'random' )
79 downDeviceNum += 1
80 time.sleep( sleepTime * 2 )
81 elif r < 0.7 and downLinkNum > 0:
82 triggerEvent( 'NETWORK_LINK_UP', 'RUN_BLOCK', 'random', 'random' )
83 downLinkNum -= 1
84 time.sleep( sleepTime )
85 elif downDeviceNum > 0:
86 triggerEvent( 'NETWORK_DEVICE_UP', 'RUN_BLOCK', 'random' )
87 downDeviceNum -= 1
88 time.sleep( sleepTime * 2 )
89 else:
90 pass
91
Jon Hall2bb3e212017-05-24 17:07:25 -070092
You Wang52163202016-07-14 16:37:15 -070093def replayFromFile( filePath='/home/admin/event-list', sleepTime=1 ):
94 try:
95 f = open( filePath, 'r' )
96 for line in f.readlines():
97 event = line.split()
98 if event[ 3 ].startswith( 'CHECK' ):
99 continue
100 triggerEvent( event[ 3 ], 'RUN_BLOCK', *event[ 4: ] )
101 time.sleep( sleepTime )
102 f.close()
103 except Exception as e:
104 print e
105
You Wangdb927a52016-02-26 11:03:28 -0800106if __name__ == '__main__':
You Wang52163202016-07-14 16:37:15 -0700107 #testLoop( 2 )
108 replayFromFile()