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 = [] |
You Wang | 5216320 | 2016-07-14 16:37:15 -0700 | [diff] [blame^] | 18 | request.append( 1 ) |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 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: |
You Wang | 5216320 | 2016-07-14 16:37:15 -0700 | [diff] [blame^] | 26 | conn.close() |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 27 | time.sleep( 1 ) |
You Wang | 5216320 | 2016-07-14 16:37:15 -0700 | [diff] [blame^] | 28 | conn = Client( address ) |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 29 | 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 | |
| 45 | def 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 Wang | 5216320 | 2016-07-14 16:37:15 -0700 | [diff] [blame^] | 69 | def 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 Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 82 | if __name__ == '__main__': |
You Wang | 5216320 | 2016-07-14 16:37:15 -0700 | [diff] [blame^] | 83 | #testLoop( 2 ) |
| 84 | replayFromFile() |