You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 1 | """ |
Jeremy Ronquillo | b27ce4c | 2017-07-17 12:41:28 -0700 | [diff] [blame] | 2 | Copyright 2016 Open Networking Foundation (ONF) |
| 3 | |
| 4 | Please refer questions to either the onos test mailing list at <onos-test@onosproject.org>, |
| 5 | the System Testing Plans and Results wiki page at <https://wiki.onosproject.org/x/voMg>, |
| 6 | or 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 |
| 11 | (at your option) any later version. |
| 12 | |
| 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 | """ |
| 21 | |
| 22 | """ |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 23 | Insert network/ONOS/app events into CHOTestMonkey |
| 24 | Author: you@onlab.us |
| 25 | """ |
| 26 | import time |
| 27 | import random |
| 28 | from multiprocessing.connection import Client |
| 29 | |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 30 | |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 31 | def triggerEvent( type, scheduleMethod, *args ): |
| 32 | """ |
| 33 | This function inserts an event into CHOTestMonkey |
| 34 | """ |
| 35 | host = "localhost" |
| 36 | port = 6000 |
| 37 | address = ( host, port ) |
| 38 | conn = Client( address ) |
| 39 | request = [] |
You Wang | 5216320 | 2016-07-14 16:37:15 -0700 | [diff] [blame] | 40 | request.append( 1 ) |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 41 | request.append( type ) |
| 42 | request.append( scheduleMethod ) |
| 43 | for arg in args: |
| 44 | request.append( arg ) |
| 45 | conn.send( request ) |
| 46 | response = conn.recv() |
| 47 | while response == 11: |
You Wang | 5216320 | 2016-07-14 16:37:15 -0700 | [diff] [blame] | 48 | conn.close() |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 49 | time.sleep( 1 ) |
You Wang | 5216320 | 2016-07-14 16:37:15 -0700 | [diff] [blame] | 50 | conn = Client( address ) |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 51 | conn.send( request ) |
| 52 | response = conn.recv() |
| 53 | if response == 10: |
| 54 | print "Event inserted:", type, scheduleMethod, args |
| 55 | elif response == 20: |
| 56 | print "Unknown message to server" |
| 57 | elif response == 21: |
| 58 | print "Unknown event type to server" |
| 59 | elif response == 22: |
| 60 | print "Unknown schedule method to server" |
| 61 | elif response == 23: |
| 62 | print "Not enough argument" |
| 63 | else: |
| 64 | print "Unknown response from server:", response |
| 65 | conn.close() |
| 66 | |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 67 | |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 68 | def testLoop( sleepTime=5 ): |
| 69 | downLinkNum = 0 |
| 70 | downDeviceNum = 0 |
| 71 | while True: |
| 72 | r = random.random() |
| 73 | if r < 0.2: |
| 74 | triggerEvent( 'NETWORK_LINK_DOWN', 'RUN_BLOCK', 'random', 'random' ) |
| 75 | downLinkNum += 1 |
| 76 | time.sleep( sleepTime ) |
| 77 | elif r < 0.4: |
| 78 | triggerEvent( 'NETWORK_DEVICE_DOWN', 'RUN_BLOCK', 'random' ) |
| 79 | downDeviceNum += 1 |
| 80 | time.sleep( sleepTime * 2 ) |
| 81 | elif r < 0.7 and downLinkNum > 0: |
| 82 | triggerEvent( 'NETWORK_LINK_UP', 'RUN_BLOCK', 'random', 'random' ) |
| 83 | downLinkNum -= 1 |
| 84 | time.sleep( sleepTime ) |
| 85 | elif downDeviceNum > 0: |
| 86 | triggerEvent( 'NETWORK_DEVICE_UP', 'RUN_BLOCK', 'random' ) |
| 87 | downDeviceNum -= 1 |
| 88 | time.sleep( sleepTime * 2 ) |
| 89 | else: |
| 90 | pass |
| 91 | |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 92 | |
You Wang | 5216320 | 2016-07-14 16:37:15 -0700 | [diff] [blame] | 93 | def replayFromFile( filePath='/home/admin/event-list', sleepTime=1 ): |
| 94 | try: |
| 95 | f = open( filePath, 'r' ) |
| 96 | for line in f.readlines(): |
| 97 | event = line.split() |
| 98 | if event[ 3 ].startswith( 'CHECK' ): |
| 99 | continue |
| 100 | triggerEvent( event[ 3 ], 'RUN_BLOCK', *event[ 4: ] ) |
| 101 | time.sleep( sleepTime ) |
| 102 | f.close() |
| 103 | except Exception as e: |
| 104 | print e |
| 105 | |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 106 | if __name__ == '__main__': |
You Wang | 5216320 | 2016-07-14 16:37:15 -0700 | [diff] [blame] | 107 | #testLoop( 2 ) |
| 108 | replayFromFile() |