Jon Hall | ca31989 | 2017-06-15 15:25:22 -0700 | [diff] [blame^] | 1 | """ |
| 2 | Copyright 2017 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 | |
| 23 | class Cluster(): |
| 24 | |
| 25 | def __str__( self ): |
| 26 | return self.name |
| 27 | def __repr__( self ): |
| 28 | #TODO use repr of cli's? |
| 29 | controllers = [] |
| 30 | for ctrl in self.controllers: |
| 31 | controllers.append( str( ctrl ) ) |
| 32 | return "%s[%s]" % ( self.name, ", ".join( controllers ) ) |
| 33 | |
| 34 | |
| 35 | def __init__( self, ctrlList=[], name="Cluster" ): |
| 36 | #assert isInstance( ctrlList, Controller ), "ctrlList should be a list of ONOS Controllers" |
| 37 | self.controllers = ctrlList |
| 38 | self.name = str( name ) |
| 39 | self.iterator = iter( self.active() ) |
| 40 | |
| 41 | def getIps( self, activeOnly=False): |
| 42 | ips = [] |
| 43 | if activeOnly: |
| 44 | nodeList = self.active() |
| 45 | else: |
| 46 | nodeList = self.controllers |
| 47 | for ctrl in nodeList: |
| 48 | ips.append( ctrl.ipAddress ) |
| 49 | return ips |
| 50 | |
| 51 | def active( self ): |
| 52 | """ |
| 53 | Return a list of active controllers in the cluster |
| 54 | """ |
| 55 | return [ ctrl for ctrl in self.controllers |
| 56 | if ctrl.active ] |
| 57 | |
| 58 | def next( self ): |
| 59 | """ |
| 60 | An iterator for the cluster's controllers that |
| 61 | resets when there are no more elements. |
| 62 | |
| 63 | Returns the next controller in the cluster |
| 64 | """ |
| 65 | try: |
| 66 | return self.iterator.next() |
| 67 | except StopIteration: |
| 68 | self.reset() |
| 69 | try: |
| 70 | return self.iterator.next() |
| 71 | except StopIteration: |
| 72 | raise RuntimeError( "There are no active nodes in the cluster" ) |
| 73 | |
| 74 | def reset( self ): |
| 75 | """ |
| 76 | Resets the cluster iterator. |
| 77 | |
| 78 | This should be used whenever a node's active state is changed |
| 79 | and is also used internally when the iterator has been exhausted. |
| 80 | """ |
| 81 | self.iterator = iter( self.active() ) |
| 82 | |
| 83 | def install( self ): |
| 84 | """ |
| 85 | Install ONOS on all controller nodes in the cluster |
| 86 | """ |
| 87 | result = main.TRUE |
| 88 | # FIXME: use the correct onosdriver function |
| 89 | # TODO: Use threads to install in parallel, maybe have an option for sequential installs |
| 90 | for ctrl in self.controllers: |
| 91 | result &= ctrl.installOnos( ctrl.ipAddress ) |
| 92 | return result |
| 93 | |
| 94 | def startCLIs( self ): |
| 95 | """ |
| 96 | Start the ONOS cli on all controller nodes in the cluster |
| 97 | """ |
| 98 | cliResults = main.TRUE |
| 99 | threads = [] |
| 100 | for ctrl in self.controllers: |
| 101 | t = main.Thread( target=ctrl.CLI.startOnosCli, |
| 102 | name="startCli-" + ctrl.name, |
| 103 | args=[ ctrl.ipAddress ] ) |
| 104 | threads.append( t ) |
| 105 | t.start() |
| 106 | ctrl.active = True |
| 107 | |
| 108 | for t in threads: |
| 109 | t.join() |
| 110 | cliResults = cliResults and t.result |
| 111 | return cliResults |
| 112 | |
| 113 | def command( self, function, args=(), kwargs={} ): |
| 114 | """ |
| 115 | Send a command to all ONOS nodes and return the results as a list |
| 116 | """ |
| 117 | threads = [] |
| 118 | results = [] |
| 119 | for ctrl in self.active(): |
| 120 | f = getattr( ctrl, function ) |
| 121 | t = main.Thread( target=f, |
| 122 | name=function + "-" + ctrl.name, |
| 123 | args=args, |
| 124 | kwargs=kwargs ) |
| 125 | threads.append( t ) |
| 126 | t.start() |
| 127 | |
| 128 | for t in threads: |
| 129 | t.join() |
| 130 | results.append( t.result ) |
| 131 | return results |