You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 1 | """ |
| 2 | Insert network/ONOS/app events into CHOTestMonkey |
| 3 | Author: you@onlab.us |
| 4 | """ |
| 5 | import time |
| 6 | import random |
| 7 | from multiprocessing.connection import Client |
| 8 | |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 9 | |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 10 | def 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 Wang | 5216320 | 2016-07-14 16:37:15 -0700 | [diff] [blame] | 19 | request.append( 1 ) |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 20 | 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 Wang | 5216320 | 2016-07-14 16:37:15 -0700 | [diff] [blame] | 27 | conn.close() |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 28 | time.sleep( 1 ) |
You Wang | 5216320 | 2016-07-14 16:37:15 -0700 | [diff] [blame] | 29 | conn = Client( address ) |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 30 | 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 Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 46 | |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 47 | def 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 Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 71 | |
You Wang | 5216320 | 2016-07-14 16:37:15 -0700 | [diff] [blame] | 72 | def 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 Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 85 | if __name__ == '__main__': |
You Wang | 5216320 | 2016-07-14 16:37:15 -0700 | [diff] [blame] | 86 | #testLoop( 2 ) |
| 87 | replayFromFile() |