You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 1 | """ |
| 2 | This file contains intent class for CHOTestMonkey |
| 3 | Author: you@onlab.us |
| 4 | """ |
| 5 | from threading import Lock |
| 6 | |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame^] | 7 | |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 8 | class Controller: |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame^] | 9 | |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 10 | def __init__( self, index ): |
| 11 | self.default = '' |
| 12 | self.index = index |
You Wang | 2d32ef4 | 2017-03-03 14:09:44 -0800 | [diff] [blame] | 13 | self.ip = main.ONOSip[ index - 1 ] |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 14 | self.CLI = None |
| 15 | self.CLILock = Lock() |
| 16 | self.status = 'up' |
| 17 | |
| 18 | def setCLI( self, CLI ): |
| 19 | self.CLI = CLI |
| 20 | |
| 21 | def startCLI( self ): |
| 22 | return self.CLI.startOnosCli( self.ip ) |
| 23 | |
| 24 | def isUp( self ): |
| 25 | return self.status == 'up' |
| 26 | |
| 27 | def bringDown( self ): |
| 28 | self.status = 'down' |
| 29 | |
| 30 | def bringUp( self ): |
| 31 | self.status = 'up' |
| 32 | |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame^] | 33 | |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 34 | class Intent: |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame^] | 35 | |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 36 | def __init__( self, id ): |
| 37 | self.default = '' |
| 38 | self.type = 'INTENT' |
| 39 | self.id = id |
You Wang | 56577c8 | 2016-07-12 10:49:23 -0700 | [diff] [blame] | 40 | self.expectedState = 'UNKNOWN' |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 41 | |
| 42 | def isHostIntent( self ): |
| 43 | return self.type == 'INTENT_HOST' |
| 44 | |
| 45 | def isPointIntent( self ): |
| 46 | return self.type == 'INTENT_POINT' |
| 47 | |
You Wang | 2b687c0 | 2016-05-13 17:01:31 -0700 | [diff] [blame] | 48 | def isFailed( self ): |
| 49 | return self.expectedState == 'FAILED' |
| 50 | |
| 51 | def isInstalled( self ): |
| 52 | return self.expectedState == 'INSTALLED' |
| 53 | |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame^] | 54 | |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 55 | class HostIntent( Intent ): |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame^] | 56 | |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 57 | def __init__( self, id, hostA, hostB ): |
| 58 | Intent.__init__( self, id ) |
| 59 | self.type = 'INTENT_HOST' |
| 60 | self.hostA = hostA |
| 61 | self.hostB = hostB |
You Wang | 2b687c0 | 2016-05-13 17:01:31 -0700 | [diff] [blame] | 62 | self.deviceA = hostA.device |
| 63 | self.deviceB = hostB.device |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 64 | |
You Wang | 56577c8 | 2016-07-12 10:49:23 -0700 | [diff] [blame] | 65 | def setWithdrawn( self ): |
| 66 | self.expectedState = 'WITHDRAWN' |
You Wang | 56577c8 | 2016-07-12 10:49:23 -0700 | [diff] [blame] | 67 | if self.hostB in self.hostA.correspondents: |
| 68 | self.hostA.correspondents.remove( self.hostB ) |
| 69 | if self.hostA in self.hostB.correspondents: |
| 70 | self.hostB.correspondents.remove( self.hostA ) |
| 71 | |
| 72 | def setFailed( self ): |
| 73 | self.expectedState = 'FAILED' |
You Wang | 56577c8 | 2016-07-12 10:49:23 -0700 | [diff] [blame] | 74 | |
| 75 | def setInstalled( self ): |
You Wang | c848af1 | 2016-07-14 09:53:58 -0700 | [diff] [blame] | 76 | if self.expectedState == 'UNKNOWN': |
| 77 | self.hostA.correspondents.append( self.hostB ) |
| 78 | self.hostB.correspondents.append( self.hostA ) |
You Wang | 56577c8 | 2016-07-12 10:49:23 -0700 | [diff] [blame] | 79 | self.expectedState = 'INSTALLED' |
You Wang | 56577c8 | 2016-07-12 10:49:23 -0700 | [diff] [blame] | 80 | |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 81 | def __str__( self ): |
| 82 | return "ID: " + self.id |
| 83 | |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame^] | 84 | |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 85 | class PointIntent( Intent ): |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame^] | 86 | |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 87 | def __init__( self, id, deviceA, deviceB ): |
| 88 | Intent.__init__( self, id ) |
| 89 | self.type = 'INTENT_POINT' |
| 90 | self.deviceA = deviceA |
| 91 | self.deviceB = deviceB |
| 92 | |
You Wang | 56577c8 | 2016-07-12 10:49:23 -0700 | [diff] [blame] | 93 | def setWithdrawn( self ): |
| 94 | self.expectedState = 'WITHDRAWN' |
| 95 | for hostA in self.deviceA.hosts: |
| 96 | for hostB in self.deviceB.hosts: |
| 97 | if hostB in hostA.correspondents: |
| 98 | hostA.correspondents.remove( hostB ) |
| 99 | |
| 100 | def setFailed( self ): |
| 101 | self.expectedState = 'FAILED' |
You Wang | 56577c8 | 2016-07-12 10:49:23 -0700 | [diff] [blame] | 102 | |
| 103 | def setInstalled( self ): |
You Wang | c848af1 | 2016-07-14 09:53:58 -0700 | [diff] [blame] | 104 | if self.expectedState == 'UNKNOWN': |
| 105 | for hostA in self.deviceA.hosts: |
| 106 | for hostB in self.deviceB.hosts: |
| 107 | hostA.correspondents.append( hostB ) |
You Wang | 56577c8 | 2016-07-12 10:49:23 -0700 | [diff] [blame] | 108 | self.expectedState = 'INSTALLED' |
You Wang | 56577c8 | 2016-07-12 10:49:23 -0700 | [diff] [blame] | 109 | |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 110 | def __str__( self ): |
| 111 | return "ID: " + self.id |