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