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 | This file contains classes for CHOTestMonkey that are related to application event |
| 23 | Author: you@onlab.us |
| 24 | """ |
| 25 | from tests.CHOTestMonkey.dependencies.events.Event import EventType, EventStates, Event |
| 26 | from tests.CHOTestMonkey.dependencies.elements.ONOSElement import HostIntent, PointIntent |
| 27 | |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 28 | |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 29 | class IntentEvent( Event ): |
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 __init__( self ): |
| 32 | Event.__init__( self ) |
| 33 | # The index of the ONOS CLI that is going to run the command |
| 34 | self.CLIIndex = 0 |
| 35 | |
You Wang | 7a27f3a | 2016-07-05 10:12:27 -0700 | [diff] [blame] | 36 | def getRandomCorrespondent( self, hostA, connected=True ): |
| 37 | import random |
| 38 | if connected: |
| 39 | candidates = hostA.correspondents |
| 40 | else: |
| 41 | candidates = [ host for host in main.hosts if host not in hostA.correspondents and host != hostA ] |
| 42 | if len( candidates ) == 0: |
| 43 | return None |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 44 | hostB = random.sample( candidates, 1 )[ 0 ] |
You Wang | 7a27f3a | 2016-07-05 10:12:27 -0700 | [diff] [blame] | 45 | return hostB |
| 46 | |
| 47 | def getRandomHostPair( self, connected=True ): |
| 48 | import random |
| 49 | candidateDict = {} |
| 50 | with main.variableLock: |
| 51 | for host in main.hosts: |
| 52 | correspondent = self.getRandomCorrespondent( host, connected=connected ) |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 53 | if correspondent is not None: |
You Wang | 7a27f3a | 2016-07-05 10:12:27 -0700 | [diff] [blame] | 54 | candidateDict[ host ] = correspondent |
| 55 | if candidateDict == {}: |
| 56 | return None |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 57 | hostA = random.sample( candidateDict.keys(), 1 )[ 0 ] |
You Wang | 7a27f3a | 2016-07-05 10:12:27 -0700 | [diff] [blame] | 58 | hostB = candidateDict[ hostA ] |
| 59 | return [ hostA, hostB ] |
| 60 | |
| 61 | def getIntentsByType( self, intentType ): |
| 62 | intents = [] |
| 63 | with main.variableLock: |
| 64 | for intent in main.intents: |
| 65 | if intent.type == intentType: |
| 66 | intents.append( intent ) |
| 67 | return intents |
| 68 | |
| 69 | def getRandomIntentByType( self, intentType ): |
| 70 | import random |
| 71 | intents = self.getIntentsByType( intentType ) |
| 72 | if len( intents ) == 0: |
| 73 | return None |
| 74 | intent = random.sample( intents, 1 )[ 0 ] |
| 75 | return intent |
| 76 | |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 77 | |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 78 | class HostIntentEvent( IntentEvent ): |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 79 | |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 80 | def __init__( self ): |
| 81 | IntentEvent.__init__( self ) |
| 82 | self.hostA = None |
| 83 | self.hostB = None |
| 84 | |
| 85 | def startHostIntentEvent( self ): |
| 86 | return EventStates().PASS |
| 87 | |
| 88 | def startEvent( self, args ): |
| 89 | with self.eventLock: |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 90 | # main.log.info( "%s - starting event" % ( self.typeString ) ) |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 91 | if self.typeIndex == EventType().APP_INTENT_HOST_ADD or self.typeIndex == EventType().APP_INTENT_HOST_DEL: |
| 92 | if len( args ) < 3: |
| 93 | main.log.warn( "%s - Not enough arguments: %s" % ( self.typeString, args ) ) |
| 94 | return EventStates().ABORT |
| 95 | elif len( args ) > 3: |
| 96 | main.log.warn( "%s - Too many arguments: %s" % ( self.typeString, args ) ) |
| 97 | return EventStates().ABORT |
You Wang | 59dfeb6 | 2016-07-14 09:47:33 -0700 | [diff] [blame] | 98 | try: |
| 99 | if args[ 0 ] == 'random' or args[ 1 ] == 'random': |
| 100 | if self.typeIndex == EventType().APP_INTENT_HOST_ADD: |
| 101 | hostPairRandom = self.getRandomHostPair( connected=False ) |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 102 | if hostPairRandom is None: |
You Wang | 59dfeb6 | 2016-07-14 09:47:33 -0700 | [diff] [blame] | 103 | main.log.warn( "All host pairs are connected, aborting event" ) |
| 104 | return EventStates().ABORT |
| 105 | self.hostA = hostPairRandom[ 0 ] |
| 106 | self.hostB = hostPairRandom[ 1 ] |
| 107 | elif self.typeIndex == EventType().APP_INTENT_HOST_DEL: |
| 108 | intent = self.getRandomIntentByType( 'INTENT_HOST' ) |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 109 | if intent is None: |
You Wang | 59dfeb6 | 2016-07-14 09:47:33 -0700 | [diff] [blame] | 110 | main.log.warn( "No host intent for deletion, aborting event" ) |
| 111 | return EventStates().ABORT |
| 112 | self.hostA = intent.hostA |
| 113 | self.hostB = intent.hostB |
| 114 | elif args[ 0 ] == args[ 1 ]: |
| 115 | main.log.warn( "%s - invalid argument: %s, %s" % ( self.typeString, args[ 0 ], args[ 1 ] ) ) |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 116 | return EventStates().ABORT |
You Wang | 59dfeb6 | 2016-07-14 09:47:33 -0700 | [diff] [blame] | 117 | else: |
| 118 | for host in main.hosts: |
| 119 | if host.name == args[ 0 ]: |
| 120 | self.hostA = host |
| 121 | elif host.name == args[ 1 ]: |
| 122 | self.hostB = host |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 123 | if self.hostA is not None and self.hostB is not None: |
You Wang | 59dfeb6 | 2016-07-14 09:47:33 -0700 | [diff] [blame] | 124 | break |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 125 | if self.hostA is None: |
You Wang | 59dfeb6 | 2016-07-14 09:47:33 -0700 | [diff] [blame] | 126 | main.log.warn( "Host %s does not exist: " % ( args[ 0 ] ) ) |
| 127 | return EventStates().ABORT |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 128 | if self.hostB is None: |
You Wang | 59dfeb6 | 2016-07-14 09:47:33 -0700 | [diff] [blame] | 129 | main.log.warn( "Host %s does not exist: " % ( args[ 1 ] ) ) |
| 130 | return EventStates().ABORT |
| 131 | index = int( args[ 2 ] ) |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 132 | if index < 1 or index > int( main.Cluster.numCtrls ): |
You Wang | 59dfeb6 | 2016-07-14 09:47:33 -0700 | [diff] [blame] | 133 | main.log.warn( "%s - invalid argument: %s" % ( self.typeString, index ) ) |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 134 | return EventStates().ABORT |
You Wang | 59dfeb6 | 2016-07-14 09:47:33 -0700 | [diff] [blame] | 135 | if not main.controllers[ index - 1 ].isUp(): |
| 136 | main.log.warn( self.typeString + " - invalid argument: onos %s is down" % ( controller.index ) ) |
| 137 | return EventStates().ABORT |
| 138 | self.CLIIndex = index |
| 139 | return self.startHostIntentEvent() |
| 140 | except Exception: |
| 141 | main.log.warn( "Caught exception, aborting event" ) |
You Wang | 7a27f3a | 2016-07-05 10:12:27 -0700 | [diff] [blame] | 142 | return EventStates().ABORT |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 143 | |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 144 | |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 145 | class AddHostIntent( HostIntentEvent ): |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 146 | |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 147 | """ |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 148 | Add a host-to-host intent ( bidirectional ) |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 149 | """ |
| 150 | def __init__( self ): |
| 151 | HostIntentEvent.__init__( self ) |
| 152 | self.typeString = main.params[ 'EVENT' ][ self.__class__.__name__ ][ 'typeString' ] |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 153 | self.typeIndex = int( main.params[ 'EVENT' ][ self.__class__.__name__ ][ 'typeIndex' ] ) |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 154 | |
| 155 | def startHostIntentEvent( self ): |
You Wang | 7a27f3a | 2016-07-05 10:12:27 -0700 | [diff] [blame] | 156 | try: |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 157 | assert self.hostA is not None and self.hostB is not None |
You Wang | 7a27f3a | 2016-07-05 10:12:27 -0700 | [diff] [blame] | 158 | # Check whether there already exists some intent for the host pair |
| 159 | # For now we should avoid installing overlapping intents |
| 160 | for intent in main.intents: |
| 161 | if not intent.type == 'INTENT_HOST': |
| 162 | continue |
| 163 | if intent.hostA == self.hostA and intent.hostB == self.hostB or\ |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 164 | intent.hostB == self.hostA and intent.hostA == self.hostB: |
You Wang | 7a27f3a | 2016-07-05 10:12:27 -0700 | [diff] [blame] | 165 | main.log.warn( self.typeString + " - find an exiting intent for the host pair, abort installation" ) |
| 166 | return EventStates().ABORT |
You Wang | 5216320 | 2016-07-14 16:37:15 -0700 | [diff] [blame] | 167 | main.log.info( "Event recorded: {} {} {} {} {}".format( self.typeIndex, self.typeString, self.hostA.name, self.hostB.name, self.CLIIndex ) ) |
You Wang | 7a27f3a | 2016-07-05 10:12:27 -0700 | [diff] [blame] | 168 | controller = main.controllers[ self.CLIIndex - 1 ] |
| 169 | with controller.CLILock: |
| 170 | id = controller.CLI.addHostIntent( self.hostA.id, self.hostB.id ) |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 171 | if id is None: |
You Wang | 7a27f3a | 2016-07-05 10:12:27 -0700 | [diff] [blame] | 172 | main.log.warn( self.typeString + " - add host intent failed" ) |
| 173 | return EventStates().FAIL |
| 174 | with main.variableLock: |
| 175 | newHostIntent = HostIntent( id, self.hostA, self.hostB ) |
| 176 | if self.hostA.isDown() or self.hostA.isRemoved() or self.hostB.isDown() or self.hostB.isRemoved(): |
| 177 | newHostIntent.setFailed() |
You Wang | 56577c8 | 2016-07-12 10:49:23 -0700 | [diff] [blame] | 178 | else: |
| 179 | newHostIntent.setInstalled() |
You Wang | 7a27f3a | 2016-07-05 10:12:27 -0700 | [diff] [blame] | 180 | main.intents.append( newHostIntent ) |
You Wang | 7a27f3a | 2016-07-05 10:12:27 -0700 | [diff] [blame] | 181 | return EventStates().PASS |
| 182 | except Exception: |
| 183 | main.log.warn( "Caught exception, aborting event" ) |
| 184 | return EventStates().ABORT |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 185 | |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 186 | |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 187 | class DelHostIntent( HostIntentEvent ): |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 188 | |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 189 | """ |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 190 | Delete a host-to-host intent ( bidirectional ) |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 191 | """ |
| 192 | def __init__( self ): |
| 193 | HostIntentEvent.__init__( self ) |
| 194 | self.typeString = main.params[ 'EVENT' ][ self.__class__.__name__ ][ 'typeString' ] |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 195 | self.typeIndex = int( main.params[ 'EVENT' ][ self.__class__.__name__ ][ 'typeIndex' ] ) |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 196 | |
| 197 | def startHostIntentEvent( self ): |
You Wang | 7a27f3a | 2016-07-05 10:12:27 -0700 | [diff] [blame] | 198 | try: |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 199 | assert self.hostA is not None and self.hostB is not None |
You Wang | 7a27f3a | 2016-07-05 10:12:27 -0700 | [diff] [blame] | 200 | targetIntent = None |
| 201 | for intent in main.intents: |
| 202 | if not intent.type == 'INTENT_HOST': |
| 203 | continue |
| 204 | if intent.hostA == self.hostA and intent.hostB == self.hostB or\ |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 205 | intent.hostB == self.hostA and intent.hostA == self.hostB: |
You Wang | 7a27f3a | 2016-07-05 10:12:27 -0700 | [diff] [blame] | 206 | targetIntent = intent |
| 207 | break |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 208 | if targetIntent is None: |
You Wang | 7a27f3a | 2016-07-05 10:12:27 -0700 | [diff] [blame] | 209 | main.log.warn( self.typeString + " - intent does not exist" ) |
| 210 | return EventStates().FAIL |
You Wang | 5216320 | 2016-07-14 16:37:15 -0700 | [diff] [blame] | 211 | main.log.info( "Event recorded: {} {} {} {} {}".format( self.typeIndex, self.typeString, self.hostA.name, self.hostB.name, self.CLIIndex ) ) |
You Wang | 7a27f3a | 2016-07-05 10:12:27 -0700 | [diff] [blame] | 212 | controller = main.controllers[ self.CLIIndex - 1 ] |
| 213 | with controller.CLILock: |
| 214 | result = controller.CLI.removeIntent( targetIntent.id, purge=True ) |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 215 | if result is None or result == main.FALSE: |
You Wang | 7a27f3a | 2016-07-05 10:12:27 -0700 | [diff] [blame] | 216 | main.log.warn( self.typeString + " - delete host intent failed" ) |
| 217 | return EventStates().FAIL |
| 218 | with main.variableLock: |
You Wang | 56577c8 | 2016-07-12 10:49:23 -0700 | [diff] [blame] | 219 | targetIntent.setWithdrawn() |
You Wang | 7a27f3a | 2016-07-05 10:12:27 -0700 | [diff] [blame] | 220 | main.intents.remove( targetIntent ) |
You Wang | 7a27f3a | 2016-07-05 10:12:27 -0700 | [diff] [blame] | 221 | return EventStates().PASS |
| 222 | except Exception: |
| 223 | main.log.warn( "Caught exception, aborting event" ) |
| 224 | return EventStates().ABORT |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 225 | |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 226 | |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 227 | class PointIntentEvent( IntentEvent ): |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 228 | |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 229 | def __init__( self ): |
| 230 | IntentEvent.__init__( self ) |
| 231 | self.deviceA = None |
| 232 | self.deviceB = None |
| 233 | |
| 234 | def startPointIntentEvent( self ): |
| 235 | return EventStates().PASS |
| 236 | |
| 237 | def startEvent( self, args ): |
| 238 | with self.eventLock: |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 239 | # main.log.info( "%s - starting event" % ( self.typeString ) ) |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 240 | if self.typeIndex == EventType().APP_INTENT_POINT_ADD or self.typeIndex == EventType().APP_INTENT_POINT_DEL: |
| 241 | if len( args ) < 3: |
| 242 | main.log.warn( "%s - Not enough arguments: %s" % ( self.typeString, args ) ) |
| 243 | return EventStates().ABORT |
You Wang | 7a27f3a | 2016-07-05 10:12:27 -0700 | [diff] [blame] | 244 | elif len( args ) > 4: |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 245 | main.log.warn( "%s - Too many arguments: %s" % ( self.typeString, args ) ) |
| 246 | return EventStates().ABORT |
You Wang | 59dfeb6 | 2016-07-14 09:47:33 -0700 | [diff] [blame] | 247 | try: |
| 248 | if args[ 0 ] == 'random' or args[ 1 ] == 'random': |
| 249 | if self.typeIndex == EventType().APP_INTENT_POINT_ADD: |
| 250 | hostPairRandom = self.getRandomHostPair( connected=False ) |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 251 | if hostPairRandom is None: |
You Wang | 59dfeb6 | 2016-07-14 09:47:33 -0700 | [diff] [blame] | 252 | main.log.warn( "All host pairs are connected, aborting event" ) |
| 253 | return EventStates().ABORT |
| 254 | self.deviceA = hostPairRandom[ 0 ].device |
| 255 | self.deviceB = hostPairRandom[ 1 ].device |
| 256 | elif self.typeIndex == EventType().APP_INTENT_POINT_DEL: |
| 257 | intent = self.getRandomIntentByType( 'INTENT_POINT' ) |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 258 | if intent is None: |
You Wang | 59dfeb6 | 2016-07-14 09:47:33 -0700 | [diff] [blame] | 259 | main.log.warn( "No point intent for deletion, aborting event" ) |
| 260 | return EventStates().ABORT |
| 261 | self.deviceA = intent.deviceA |
| 262 | self.deviceB = intent.deviceB |
| 263 | elif args[ 0 ] == args[ 1 ]: |
| 264 | main.log.warn( "%s - invalid argument: %s" % ( self.typeString, args[ 0 ], args[ 1 ] ) ) |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 265 | return EventStates().ABORT |
You Wang | 7a27f3a | 2016-07-05 10:12:27 -0700 | [diff] [blame] | 266 | else: |
You Wang | 59dfeb6 | 2016-07-14 09:47:33 -0700 | [diff] [blame] | 267 | for device in main.devices: |
| 268 | if device.name == args[ 0 ]: |
| 269 | self.deviceA = device |
| 270 | elif device.name == args[ 1 ]: |
| 271 | self.deviceB = device |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 272 | if self.deviceA is not None and self.deviceB is not None: |
You Wang | 59dfeb6 | 2016-07-14 09:47:33 -0700 | [diff] [blame] | 273 | break |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 274 | if self.deviceA is None: |
You Wang | 59dfeb6 | 2016-07-14 09:47:33 -0700 | [diff] [blame] | 275 | main.log.warn( "Device %s does not exist: " % ( args[ 0 ] ) ) |
| 276 | return EventStates().ABORT |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 277 | if self.deviceB is None: |
You Wang | 59dfeb6 | 2016-07-14 09:47:33 -0700 | [diff] [blame] | 278 | main.log.warn( "Device %s does not exist: " % ( args[ 1 ] ) ) |
| 279 | return EventStates().ABORT |
| 280 | index = int( args[ 2 ] ) |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 281 | if index < 1 or index > int( main.Cluster.numCtrls ): |
You Wang | 59dfeb6 | 2016-07-14 09:47:33 -0700 | [diff] [blame] | 282 | main.log.warn( "%s - invalid argument: %s" % ( self.typeString, index ) ) |
| 283 | return EventStates().ABORT |
| 284 | if not main.controllers[ index - 1 ].isUp(): |
| 285 | main.log.warn( self.typeString + " - invalid argument: onos %s is down" % ( controller.index ) ) |
| 286 | return EventStates().ABORT |
| 287 | self.CLIIndex = index |
| 288 | if len( args ) == 4 and args[ 3 ] == 'bidirectional': |
| 289 | # Install point intents for both directions |
| 290 | resultA = self.startPointIntentEvent() |
| 291 | [ self.deviceA, self.deviceB ] = [ self.deviceB, self.deviceA ] |
| 292 | resultB = self.startPointIntentEvent() |
| 293 | if resultA == EventStates().PASS and resultB == EventStates().PASS: |
| 294 | return EventStates().PASS |
| 295 | else: |
| 296 | return EventStates().FAIL |
| 297 | else: |
| 298 | return self.startPointIntentEvent() |
| 299 | except Exception: |
| 300 | main.log.warn( "Caught exception, aborting event" ) |
| 301 | return EventStates().ABORT |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 302 | |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 303 | |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 304 | class AddPointIntent( PointIntentEvent ): |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 305 | |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 306 | """ |
| 307 | Add a point-to-point intent |
| 308 | """ |
| 309 | def __init__( self ): |
| 310 | PointIntentEvent.__init__( self ) |
| 311 | self.typeString = main.params[ 'EVENT' ][ self.__class__.__name__ ][ 'typeString' ] |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 312 | self.typeIndex = int( main.params[ 'EVENT' ][ self.__class__.__name__ ][ 'typeIndex' ] ) |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 313 | |
| 314 | def startPointIntentEvent( self ): |
You Wang | 7a27f3a | 2016-07-05 10:12:27 -0700 | [diff] [blame] | 315 | try: |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 316 | assert self.deviceA is not None and self.deviceB is not None |
You Wang | 7a27f3a | 2016-07-05 10:12:27 -0700 | [diff] [blame] | 317 | controller = main.controllers[ self.CLIIndex - 1 ] |
You Wang | 59dfeb6 | 2016-07-14 09:47:33 -0700 | [diff] [blame] | 318 | # TODO: support multiple hosts under one device |
You Wang | 7a27f3a | 2016-07-05 10:12:27 -0700 | [diff] [blame] | 319 | # Check whether there already exists some intent for the device pair |
| 320 | # For now we should avoid installing overlapping intents |
| 321 | for intent in main.intents: |
| 322 | if not intent.type == 'INTENT_POINT': |
| 323 | continue |
| 324 | if intent.deviceA == self.deviceA and intent.deviceB == self.deviceB: |
| 325 | main.log.warn( self.typeString + " - find an exiting intent for the device pair, abort installation" ) |
| 326 | return EventStates().ABORT |
You Wang | 5216320 | 2016-07-14 16:37:15 -0700 | [diff] [blame] | 327 | main.log.info( "Event recorded: {} {} {} {} {}".format( self.typeIndex, self.typeString, self.deviceA.name, self.deviceB.name, self.CLIIndex ) ) |
You Wang | 7a27f3a | 2016-07-05 10:12:27 -0700 | [diff] [blame] | 328 | controller = main.controllers[ self.CLIIndex - 1 ] |
| 329 | with controller.CLILock: |
You Wang | 7a27f3a | 2016-07-05 10:12:27 -0700 | [diff] [blame] | 330 | srcMAC = "" |
| 331 | dstMAC = "" |
| 332 | if len( self.deviceA.hosts ) > 0: |
| 333 | srcMAC = self.deviceA.hosts[ 0 ].mac |
| 334 | if len( self.deviceB.hosts ) > 0: |
| 335 | dstMAC = self.deviceB.hosts[ 0 ].mac |
You Wang | 59dfeb6 | 2016-07-14 09:47:33 -0700 | [diff] [blame] | 336 | id = controller.CLI.addPointIntent( self.deviceA.dpid, self.deviceB.dpid, 1, 1, '', srcMAC, dstMAC ) |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 337 | if id is None: |
You Wang | 7a27f3a | 2016-07-05 10:12:27 -0700 | [diff] [blame] | 338 | main.log.warn( self.typeString + " - add point intent failed" ) |
| 339 | return EventStates().FAIL |
| 340 | with main.variableLock: |
| 341 | newPointIntent = PointIntent( id, self.deviceA, self.deviceB ) |
| 342 | if self.deviceA.isDown() or self.deviceB.isDown() or self.deviceA.isRemoved() or self.deviceB.isRemoved(): |
| 343 | newPointIntent.setFailed() |
You Wang | 56577c8 | 2016-07-12 10:49:23 -0700 | [diff] [blame] | 344 | else: |
| 345 | newPointIntent.setInstalled() |
You Wang | 7a27f3a | 2016-07-05 10:12:27 -0700 | [diff] [blame] | 346 | main.intents.append( newPointIntent ) |
You Wang | 7a27f3a | 2016-07-05 10:12:27 -0700 | [diff] [blame] | 347 | return EventStates().PASS |
| 348 | except Exception: |
| 349 | main.log.warn( "Caught exception, aborting event" ) |
| 350 | return EventStates().ABORT |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 351 | |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 352 | |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 353 | class DelPointIntent( PointIntentEvent ): |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 354 | |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 355 | """ |
| 356 | Delete a point-to-point intent |
| 357 | """ |
| 358 | def __init__( self ): |
| 359 | PointIntentEvent.__init__( self ) |
| 360 | self.typeString = main.params[ 'EVENT' ][ self.__class__.__name__ ][ 'typeString' ] |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 361 | self.typeIndex = int( main.params[ 'EVENT' ][ self.__class__.__name__ ][ 'typeIndex' ] ) |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 362 | |
| 363 | def startPointIntentEvent( self ): |
You Wang | 7a27f3a | 2016-07-05 10:12:27 -0700 | [diff] [blame] | 364 | try: |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 365 | assert self.deviceA is not None and self.deviceB is not None |
You Wang | 7a27f3a | 2016-07-05 10:12:27 -0700 | [diff] [blame] | 366 | targetIntent = None |
| 367 | for intent in main.intents: |
| 368 | if not intent.type == 'INTENT_POINT': |
| 369 | continue |
| 370 | if intent.deviceA == self.deviceA and intent.deviceB == self.deviceB: |
| 371 | targetIntent = intent |
| 372 | break |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 373 | if targetIntent is None: |
You Wang | 7a27f3a | 2016-07-05 10:12:27 -0700 | [diff] [blame] | 374 | main.log.warn( self.typeString + " - intent does not exist" ) |
| 375 | return EventStates().FAIL |
You Wang | 5216320 | 2016-07-14 16:37:15 -0700 | [diff] [blame] | 376 | main.log.info( "Event recorded: {} {} {} {} {}".format( self.typeIndex, self.typeString, self.deviceA.name, self.deviceB.name, self.CLIIndex ) ) |
You Wang | 7a27f3a | 2016-07-05 10:12:27 -0700 | [diff] [blame] | 377 | controller = main.controllers[ self.CLIIndex - 1 ] |
| 378 | with controller.CLILock: |
| 379 | result = controller.CLI.removeIntent( targetIntent.id, purge=True ) |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 380 | if result is None or result == main.FALSE: |
You Wang | 7a27f3a | 2016-07-05 10:12:27 -0700 | [diff] [blame] | 381 | main.log.warn( self.typeString + " - delete point intent failed" ) |
| 382 | return EventStates().FAIL |
| 383 | with main.variableLock: |
You Wang | 56577c8 | 2016-07-12 10:49:23 -0700 | [diff] [blame] | 384 | targetIntent.setWithdrawn() |
You Wang | 7a27f3a | 2016-07-05 10:12:27 -0700 | [diff] [blame] | 385 | main.intents.remove( targetIntent ) |
You Wang | 7a27f3a | 2016-07-05 10:12:27 -0700 | [diff] [blame] | 386 | return EventStates().PASS |
| 387 | except Exception: |
| 388 | main.log.warn( "Caught exception, aborting event" ) |
| 389 | return EventStates().ABORT |