| """ |
| Insert network/ONOS/app events into CHOTestMonkey |
| Author: you@onlab.us |
| """ |
| import time |
| import random |
| from multiprocessing.connection import Client |
| |
| def triggerEvent( type, scheduleMethod, *args ): |
| """ |
| This function inserts an event into CHOTestMonkey |
| """ |
| host = "localhost" |
| port = 6000 |
| address = ( host, port ) |
| conn = Client( address ) |
| request = [] |
| request.append( 1 ) |
| request.append( type ) |
| request.append( scheduleMethod ) |
| for arg in args: |
| request.append( arg ) |
| conn.send( request ) |
| response = conn.recv() |
| while response == 11: |
| conn.close() |
| time.sleep( 1 ) |
| conn = Client( address ) |
| conn.send( request ) |
| response = conn.recv() |
| if response == 10: |
| print "Event inserted:", type, scheduleMethod, args |
| elif response == 20: |
| print "Unknown message to server" |
| elif response == 21: |
| print "Unknown event type to server" |
| elif response == 22: |
| print "Unknown schedule method to server" |
| elif response == 23: |
| print "Not enough argument" |
| else: |
| print "Unknown response from server:", response |
| conn.close() |
| |
| def testLoop( sleepTime=5 ): |
| downLinkNum = 0 |
| downDeviceNum = 0 |
| while True: |
| r = random.random() |
| if r < 0.2: |
| triggerEvent( 'NETWORK_LINK_DOWN', 'RUN_BLOCK', 'random', 'random' ) |
| downLinkNum += 1 |
| time.sleep( sleepTime ) |
| elif r < 0.4: |
| triggerEvent( 'NETWORK_DEVICE_DOWN', 'RUN_BLOCK', 'random' ) |
| downDeviceNum += 1 |
| time.sleep( sleepTime * 2 ) |
| elif r < 0.7 and downLinkNum > 0: |
| triggerEvent( 'NETWORK_LINK_UP', 'RUN_BLOCK', 'random', 'random' ) |
| downLinkNum -= 1 |
| time.sleep( sleepTime ) |
| elif downDeviceNum > 0: |
| triggerEvent( 'NETWORK_DEVICE_UP', 'RUN_BLOCK', 'random' ) |
| downDeviceNum -= 1 |
| time.sleep( sleepTime * 2 ) |
| else: |
| pass |
| |
| def replayFromFile( filePath='/home/admin/event-list', sleepTime=1 ): |
| try: |
| f = open( filePath, 'r' ) |
| for line in f.readlines(): |
| event = line.split() |
| if event[ 3 ].startswith( 'CHECK' ): |
| continue |
| triggerEvent( event[ 3 ], 'RUN_BLOCK', *event[ 4: ] ) |
| time.sleep( sleepTime ) |
| f.close() |
| except Exception as e: |
| print e |
| |
| if __name__ == '__main__': |
| #testLoop( 2 ) |
| replayFromFile() |