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 | |
| 9 | def 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 = [] |
| 18 | request.append( 2 ) |
| 19 | 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: |
| 26 | time.sleep( 1 ) |
| 27 | conn.send( request ) |
| 28 | response = conn.recv() |
| 29 | if response == 10: |
| 30 | print "Event inserted:", type, scheduleMethod, args |
| 31 | elif response == 20: |
| 32 | print "Unknown message to server" |
| 33 | elif response == 21: |
| 34 | print "Unknown event type to server" |
| 35 | elif response == 22: |
| 36 | print "Unknown schedule method to server" |
| 37 | elif response == 23: |
| 38 | print "Not enough argument" |
| 39 | else: |
| 40 | print "Unknown response from server:", response |
| 41 | conn.close() |
| 42 | |
| 43 | def testLoop( sleepTime=5 ): |
| 44 | downLinkNum = 0 |
| 45 | downDeviceNum = 0 |
| 46 | while True: |
| 47 | r = random.random() |
| 48 | if r < 0.2: |
| 49 | triggerEvent( 'NETWORK_LINK_DOWN', 'RUN_BLOCK', 'random', 'random' ) |
| 50 | downLinkNum += 1 |
| 51 | time.sleep( sleepTime ) |
| 52 | elif r < 0.4: |
| 53 | triggerEvent( 'NETWORK_DEVICE_DOWN', 'RUN_BLOCK', 'random' ) |
| 54 | downDeviceNum += 1 |
| 55 | time.sleep( sleepTime * 2 ) |
| 56 | elif r < 0.7 and downLinkNum > 0: |
| 57 | triggerEvent( 'NETWORK_LINK_UP', 'RUN_BLOCK', 'random', 'random' ) |
| 58 | downLinkNum -= 1 |
| 59 | time.sleep( sleepTime ) |
| 60 | elif downDeviceNum > 0: |
| 61 | triggerEvent( 'NETWORK_DEVICE_UP', 'RUN_BLOCK', 'random' ) |
| 62 | downDeviceNum -= 1 |
| 63 | time.sleep( sleepTime * 2 ) |
| 64 | else: |
| 65 | pass |
| 66 | |
| 67 | if __name__ == '__main__': |
| 68 | testLoop( 2 ) |