blob: 2bfdd357fa12bb831407fa192ecfd763d6d009a8 [file] [log] [blame]
You Wangdb927a52016-02-26 11:03:28 -08001"""
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07002Copyright 2016 Open Networking Foundation ( ONF )
Jeremy Ronquillob27ce4c2017-07-17 12:41:28 -07003
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
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -070011 ( at your option ) any later version.
Jeremy Ronquillob27ce4c2017-07-17 12:41:28 -070012
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"""
Jeremy Ronquillob27ce4c2017-07-17 12:41:28 -070021"""
You Wangdb927a52016-02-26 11:03:28 -080022Insert network/ONOS/app events into CHOTestMonkey
23Author: you@onlab.us
24"""
25import time
26import random
27from multiprocessing.connection import Client
28
Jon Hall2bb3e212017-05-24 17:07:25 -070029
You Wangdb927a52016-02-26 11:03:28 -080030def triggerEvent( type, scheduleMethod, *args ):
31 """
32 This function inserts an event into CHOTestMonkey
33 """
34 host = "localhost"
35 port = 6000
36 address = ( host, port )
37 conn = Client( address )
38 request = []
You Wang52163202016-07-14 16:37:15 -070039 request.append( 1 )
You Wangdb927a52016-02-26 11:03:28 -080040 request.append( type )
41 request.append( scheduleMethod )
42 for arg in args:
43 request.append( arg )
44 conn.send( request )
45 response = conn.recv()
46 while response == 11:
You Wang52163202016-07-14 16:37:15 -070047 conn.close()
You Wangdb927a52016-02-26 11:03:28 -080048 time.sleep( 1 )
You Wang52163202016-07-14 16:37:15 -070049 conn = Client( address )
You Wangdb927a52016-02-26 11:03:28 -080050 conn.send( request )
51 response = conn.recv()
52 if response == 10:
53 print "Event inserted:", type, scheduleMethod, args
54 elif response == 20:
55 print "Unknown message to server"
56 elif response == 21:
57 print "Unknown event type to server"
58 elif response == 22:
59 print "Unknown schedule method to server"
60 elif response == 23:
61 print "Not enough argument"
62 else:
63 print "Unknown response from server:", response
64 conn.close()
65
Jon Hall2bb3e212017-05-24 17:07:25 -070066
You Wangdb927a52016-02-26 11:03:28 -080067def testLoop( sleepTime=5 ):
68 downLinkNum = 0
69 downDeviceNum = 0
70 while True:
71 r = random.random()
72 if r < 0.2:
73 triggerEvent( 'NETWORK_LINK_DOWN', 'RUN_BLOCK', 'random', 'random' )
74 downLinkNum += 1
75 time.sleep( sleepTime )
76 elif r < 0.4:
77 triggerEvent( 'NETWORK_DEVICE_DOWN', 'RUN_BLOCK', 'random' )
78 downDeviceNum += 1
79 time.sleep( sleepTime * 2 )
80 elif r < 0.7 and downLinkNum > 0:
81 triggerEvent( 'NETWORK_LINK_UP', 'RUN_BLOCK', 'random', 'random' )
82 downLinkNum -= 1
83 time.sleep( sleepTime )
84 elif downDeviceNum > 0:
85 triggerEvent( 'NETWORK_DEVICE_UP', 'RUN_BLOCK', 'random' )
86 downDeviceNum -= 1
87 time.sleep( sleepTime * 2 )
88 else:
89 pass
90
Jon Hall2bb3e212017-05-24 17:07:25 -070091
You Wang52163202016-07-14 16:37:15 -070092def replayFromFile( filePath='/home/admin/event-list', sleepTime=1 ):
93 try:
94 f = open( filePath, 'r' )
95 for line in f.readlines():
96 event = line.split()
97 if event[ 3 ].startswith( 'CHECK' ):
98 continue
99 triggerEvent( event[ 3 ], 'RUN_BLOCK', *event[ 4: ] )
100 time.sleep( sleepTime )
101 f.close()
102 except Exception as e:
103 print e
104
You Wangdb927a52016-02-26 11:03:28 -0800105if __name__ == '__main__':
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700106 # testLoop( 2 )
You Wang52163202016-07-14 16:37:15 -0700107 replayFromFile()