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