Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | """ |
| 4 | onos.py: ONOS cluster and control network in Mininet |
| 5 | |
| 6 | With onos.py, you can use Mininet to create a complete |
| 7 | ONOS network, including an ONOS cluster with a modeled |
| 8 | control network as well as the usual data nework. |
| 9 | |
| 10 | This is intended to be useful for distributed ONOS |
| 11 | development and testing in the case that you require |
| 12 | a modeled control network. |
| 13 | |
| 14 | Invocation (using OVS as default switch): |
| 15 | |
| 16 | mn --custom onos.py --controller onos,3 --topo torus,4,4 |
| 17 | |
| 18 | Or with the user switch (or CPqD if installed): |
| 19 | |
| 20 | mn --custom onos.py --controller onos,3 \ |
| 21 | --switch onosuser --topo torus,4,4 |
| 22 | |
Bob Lantz | bb37d87 | 2016-05-16 16:26:13 -0700 | [diff] [blame] | 23 | Currently you meed to use a custom switch class |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 24 | because Mininet's Switch() class does't (yet?) handle |
| 25 | controllers with multiple IP addresses directly. |
| 26 | |
| 27 | The classes may also be imported and used via Mininet's |
| 28 | python API. |
| 29 | |
| 30 | Bugs/Gripes: |
| 31 | - We need --switch onosuser for the user switch because |
| 32 | Switch() doesn't currently handle Controller objects |
| 33 | with multiple IP addresses. |
| 34 | - ONOS startup and configuration is painful/undocumented. |
| 35 | - Too many ONOS environment vars - do we need them all? |
| 36 | - ONOS cluster startup is very, very slow. If Linux can |
| 37 | boot in 4 seconds, why can't ONOS? |
| 38 | - It's a pain to mess with the control network from the |
| 39 | CLI |
| 40 | - Setting a default controller for Mininet should be easier |
| 41 | """ |
| 42 | |
| 43 | from mininet.node import Controller, OVSSwitch, UserSwitch |
| 44 | from mininet.nodelib import LinuxBridge |
| 45 | from mininet.net import Mininet |
| 46 | from mininet.topo import SingleSwitchTopo, Topo |
| 47 | from mininet.log import setLogLevel, info |
| 48 | from mininet.cli import CLI |
Bob Lantz | 1451d72 | 2016-05-17 14:40:07 -0700 | [diff] [blame] | 49 | from mininet.util import quietRun, waitListening |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 50 | from mininet.clean import killprocs |
| 51 | from mininet.examples.controlnet import MininetFacade |
| 52 | |
| 53 | from os import environ |
| 54 | from os.path import dirname, join, isfile |
| 55 | from sys import argv |
| 56 | from glob import glob |
| 57 | import time |
| 58 | |
| 59 | |
| 60 | ### ONOS Environment |
| 61 | |
Bob Lantz | bb37d87 | 2016-05-16 16:26:13 -0700 | [diff] [blame] | 62 | KarafPort = 8101 # ssh port indicating karaf is running |
| 63 | GUIPort = 8181 # GUI/REST port |
| 64 | OpenFlowPort = 6653 # OpenFlow port |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 65 | |
| 66 | def defaultUser(): |
| 67 | "Return a reasonable default user" |
| 68 | if 'SUDO_USER' in environ: |
| 69 | return environ[ 'SUDO_USER' ] |
| 70 | try: |
| 71 | user = quietRun( 'who am i' ).split()[ 0 ] |
| 72 | except: |
| 73 | user = 'nobody' |
| 74 | return user |
| 75 | |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 76 | # Module vars, initialized below |
Bob Lantz | 4b51d5c | 2016-05-27 14:47:38 -0700 | [diff] [blame] | 77 | HOME = ONOS_ROOT = ONOS_USER = None |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 78 | ONOS_APPS = ONOS_WEB_USER = ONOS_WEB_PASS = ONOS_TAR = None |
| 79 | |
| 80 | def initONOSEnv(): |
| 81 | """Initialize ONOS environment (and module) variables |
| 82 | This is ugly and painful, but they have to be set correctly |
| 83 | in order for the onos-setup-karaf script to work. |
| 84 | nodes: list of ONOS nodes |
| 85 | returns: ONOS environment variable dict""" |
| 86 | # pylint: disable=global-statement |
Bob Lantz | 4b51d5c | 2016-05-27 14:47:38 -0700 | [diff] [blame] | 87 | global HOME, ONOS_ROOT, ONOS_USER |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 88 | global ONOS_APPS, ONOS_WEB_USER, ONOS_WEB_PASS |
| 89 | env = {} |
| 90 | def sd( var, val ): |
| 91 | "Set default value for environment variable" |
| 92 | env[ var ] = environ.setdefault( var, val ) |
| 93 | return env[ var ] |
Bob Lantz | 4b51d5c | 2016-05-27 14:47:38 -0700 | [diff] [blame] | 94 | assert environ[ 'HOME' ] |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 95 | HOME = sd( 'HOME', environ[ 'HOME' ] ) |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 96 | ONOS_ROOT = sd( 'ONOS_ROOT', join( HOME, 'onos' ) ) |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 97 | environ[ 'ONOS_USER' ] = defaultUser() |
| 98 | ONOS_USER = sd( 'ONOS_USER', defaultUser() ) |
| 99 | ONOS_APPS = sd( 'ONOS_APPS', |
| 100 | 'drivers,openflow,fwd,proxyarp,mobility' ) |
| 101 | # ONOS_WEB_{USER,PASS} isn't respected by onos-karaf: |
| 102 | environ.update( ONOS_WEB_USER='karaf', ONOS_WEB_PASS='karaf' ) |
| 103 | ONOS_WEB_USER = sd( 'ONOS_WEB_USER', 'karaf' ) |
| 104 | ONOS_WEB_PASS = sd( 'ONOS_WEB_PASS', 'karaf' ) |
| 105 | return env |
| 106 | |
| 107 | |
| 108 | def updateNodeIPs( env, nodes ): |
| 109 | "Update env dict and environ with node IPs" |
| 110 | # Get rid of stale junk |
| 111 | for var in 'ONOS_NIC', 'ONOS_CELL', 'ONOS_INSTANCES': |
| 112 | env[ var ] = '' |
| 113 | for var in environ.keys(): |
| 114 | if var.startswith( 'OC' ): |
| 115 | env[ var ] = '' |
| 116 | for index, node in enumerate( nodes, 1 ): |
| 117 | var = 'OC%d' % index |
| 118 | env[ var ] = node.IP() |
| 119 | env[ 'OCI' ] = env[ 'OCN' ] = env[ 'OC1' ] |
| 120 | env[ 'ONOS_INSTANCES' ] = '\n'.join( |
| 121 | node.IP() for node in nodes ) |
| 122 | environ.update( env ) |
| 123 | return env |
| 124 | |
| 125 | |
| 126 | tarDefaultPath = 'buck-out/gen/tools/package/onos-package/onos.tar.gz' |
| 127 | |
Bob Lantz | 1451d72 | 2016-05-17 14:40:07 -0700 | [diff] [blame] | 128 | def unpackONOS( destDir='/tmp', run=quietRun ): |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 129 | "Unpack ONOS and return its location" |
| 130 | global ONOS_TAR |
| 131 | environ.setdefault( 'ONOS_TAR', join( ONOS_ROOT, tarDefaultPath ) ) |
| 132 | ONOS_TAR = environ[ 'ONOS_TAR' ] |
| 133 | tarPath = ONOS_TAR |
| 134 | if not isfile( tarPath ): |
| 135 | raise Exception( 'Missing ONOS tarball %s - run buck build onos?' |
| 136 | % tarPath ) |
| 137 | info( '(unpacking %s)' % destDir) |
Bob Lantz | 1451d72 | 2016-05-17 14:40:07 -0700 | [diff] [blame] | 138 | cmds = ( 'mkdir -p "%s" && cd "%s" && tar xzf "%s"' |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 139 | % ( destDir, destDir, tarPath) ) |
Bob Lantz | 1451d72 | 2016-05-17 14:40:07 -0700 | [diff] [blame] | 140 | run( cmds, shell=True, verbose=True ) |
| 141 | # We can use quietRun for this usually |
| 142 | tarOutput = quietRun( 'tar tzf "%s" | head -1' % tarPath, shell=True) |
| 143 | tarOutput = tarOutput.split()[ 0 ].strip() |
| 144 | assert '/' in tarOutput |
| 145 | onosDir = join( destDir, dirname( tarOutput ) ) |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 146 | # Add symlink to log file |
Bob Lantz | 1451d72 | 2016-05-17 14:40:07 -0700 | [diff] [blame] | 147 | run( 'cd %s; ln -s onos*/apache* karaf;' |
| 148 | 'ln -s karaf/data/log/karaf.log log' % destDir, |
| 149 | shell=True ) |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 150 | return onosDir |
| 151 | |
| 152 | |
| 153 | ### Mininet classes |
| 154 | |
| 155 | def RenamedTopo( topo, *args, **kwargs ): |
| 156 | """Return specialized topo with renamed hosts |
| 157 | topo: topo class/class name to specialize |
| 158 | args, kwargs: topo args |
| 159 | sold: old switch name prefix (default 's') |
| 160 | snew: new switch name prefix |
| 161 | hold: old host name prefix (default 'h') |
| 162 | hnew: new host name prefix |
| 163 | This may be used from the mn command, e.g. |
| 164 | mn --topo renamed,single,spref=sw,hpref=host""" |
| 165 | sold = kwargs.pop( 'sold', 's' ) |
| 166 | hold = kwargs.pop( 'hold', 'h' ) |
| 167 | snew = kwargs.pop( 'snew', 'cs' ) |
| 168 | hnew = kwargs.pop( 'hnew' ,'ch' ) |
| 169 | topos = {} # TODO: use global TOPOS dict |
| 170 | if isinstance( topo, str ): |
| 171 | # Look up in topo directory - this allows us to |
| 172 | # use RenamedTopo from the command line! |
| 173 | if topo in topos: |
| 174 | topo = topos.get( topo ) |
| 175 | else: |
| 176 | raise Exception( 'Unknown topo name: %s' % topo ) |
| 177 | # pylint: disable=no-init |
| 178 | class RenamedTopoCls( topo ): |
| 179 | "Topo subclass with renamed nodes" |
| 180 | def addNode( self, name, *args, **kwargs ): |
| 181 | "Add a node, renaming if necessary" |
| 182 | if name.startswith( sold ): |
| 183 | name = snew + name[ len( sold ): ] |
| 184 | elif name.startswith( hold ): |
| 185 | name = hnew + name[ len( hold ): ] |
| 186 | return topo.addNode( self, name, *args, **kwargs ) |
| 187 | return RenamedTopoCls( *args, **kwargs ) |
| 188 | |
| 189 | |
| 190 | class ONOSNode( Controller ): |
| 191 | "ONOS cluster node" |
| 192 | |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 193 | def __init__( self, name, **kwargs ): |
| 194 | kwargs.update( inNamespace=True ) |
| 195 | Controller.__init__( self, name, **kwargs ) |
| 196 | self.dir = '/tmp/%s' % self.name |
Bob Lantz | 4b51d5c | 2016-05-27 14:47:38 -0700 | [diff] [blame] | 197 | self.client = self.dir + '/karaf/bin/client' |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 198 | self.ONOS_HOME = '/tmp' |
| 199 | |
| 200 | # pylint: disable=arguments-differ |
| 201 | |
| 202 | def start( self, env ): |
| 203 | """Start ONOS on node |
| 204 | env: environment var dict""" |
| 205 | env = dict( env ) |
| 206 | self.cmd( 'rm -rf', self.dir ) |
Bob Lantz | 1451d72 | 2016-05-17 14:40:07 -0700 | [diff] [blame] | 207 | self.ONOS_HOME = unpackONOS( self.dir, run=self.ucmd ) |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 208 | env.update( ONOS_HOME=self.ONOS_HOME ) |
| 209 | self.updateEnv( env ) |
| 210 | karafbin = glob( '%s/apache*/bin' % self.ONOS_HOME )[ 0 ] |
| 211 | onosbin = join( ONOS_ROOT, 'tools/test/bin' ) |
| 212 | self.cmd( 'export PATH=%s:%s:$PATH' % ( onosbin, karafbin ) ) |
| 213 | self.cmd( 'cd', self.ONOS_HOME ) |
Bob Lantz | 1451d72 | 2016-05-17 14:40:07 -0700 | [diff] [blame] | 214 | self.ucmd( 'mkdir -p config && ' |
| 215 | 'onos-gen-partitions config/cluster.json' ) |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 216 | info( '(starting %s)' % self ) |
| 217 | service = join( self.ONOS_HOME, 'bin/onos-service' ) |
Bob Lantz | 1451d72 | 2016-05-17 14:40:07 -0700 | [diff] [blame] | 218 | self.ucmd( service, 'server 1>../onos.log 2>../onos.log' |
| 219 | ' & echo $! > onos.pid; ln -s `pwd`/onos.pid ..' ) |
Bob Lantz | 4b51d5c | 2016-05-27 14:47:38 -0700 | [diff] [blame] | 220 | self.onosPid = int( self.cmd( 'cat onos.pid' ).strip() ) |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 221 | |
| 222 | # pylint: enable=arguments-differ |
| 223 | |
| 224 | def stop( self ): |
| 225 | # XXX This will kill all karafs - too bad! |
| 226 | self.cmd( 'pkill -HUP -f karaf.jar && wait' ) |
| 227 | self.cmd( 'rm -rf', self.dir ) |
| 228 | |
Bob Lantz | 4b51d5c | 2016-05-27 14:47:38 -0700 | [diff] [blame] | 229 | def isRunning( self ): |
| 230 | "Is our ONOS process still running?" |
| 231 | cmd = 'ps -p %d >/dev/null 2>&1 && echo "running" || echo "not running"' |
| 232 | return self.cmd( cmd % self.onosPid ) == 'running' |
| 233 | |
| 234 | def sanityCheck( self ): |
| 235 | "Check whether we've quit or are running out of memory" |
| 236 | if not self.isRunning(): |
| 237 | raise Exception( 'ONOS node %s has died' % self.name ) |
| 238 | |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 239 | def waitStarted( self ): |
| 240 | "Wait until we've really started" |
| 241 | info( '(checking: karaf' ) |
| 242 | while True: |
Bob Lantz | 1451d72 | 2016-05-17 14:40:07 -0700 | [diff] [blame] | 243 | status = self.ucmd( 'karaf status' ).lower() |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 244 | if 'running' in status and 'not running' not in status: |
| 245 | break |
| 246 | info( '.' ) |
| 247 | time.sleep( 1 ) |
| 248 | info( ' ssh-port' ) |
Bob Lantz | bb37d87 | 2016-05-16 16:26:13 -0700 | [diff] [blame] | 249 | waitListening( server=self, port=KarafPort ) |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 250 | info( ' openflow-port' ) |
Bob Lantz | bb37d87 | 2016-05-16 16:26:13 -0700 | [diff] [blame] | 251 | waitListening( server=self, port=OpenFlowPort ) |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 252 | info( ' client' ) |
| 253 | while True: |
Bob Lantz | bb37d87 | 2016-05-16 16:26:13 -0700 | [diff] [blame] | 254 | result = quietRun( 'echo apps -a | %s -h %s' % |
| 255 | ( self.client, self.IP() ), shell=True ) |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 256 | if 'openflow' in result: |
| 257 | break |
| 258 | info( '.' ) |
| 259 | time.sleep( 1 ) |
| 260 | info( ')\n' ) |
| 261 | |
| 262 | def updateEnv( self, envDict ): |
| 263 | "Update environment variables" |
| 264 | cmd = ';'.join( 'export %s="%s"' % ( var, val ) |
| 265 | for var, val in envDict.iteritems() ) |
| 266 | self.cmd( cmd ) |
| 267 | |
Bob Lantz | 1451d72 | 2016-05-17 14:40:07 -0700 | [diff] [blame] | 268 | def ucmd( self, *args, **_kwargs ): |
| 269 | "Run command as $ONOS_USER using sudo -E -u" |
| 270 | if ONOS_USER != 'root': # don't bother with sudo |
| 271 | args = [ "sudo -E -u $ONOS_USER PATH=$PATH " |
| 272 | "bash -c '%s'" % ' '.join( args ) ] |
| 273 | return self.cmd( *args ) |
| 274 | |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 275 | |
| 276 | class ONOSCluster( Controller ): |
| 277 | "ONOS Cluster" |
| 278 | def __init__( self, *args, **kwargs ): |
| 279 | """name: (first parameter) |
| 280 | *args: topology class parameters |
| 281 | ipBase: IP range for ONOS nodes |
Bob Lantz | bb37d87 | 2016-05-16 16:26:13 -0700 | [diff] [blame] | 282 | forward: default port forwarding list, |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 283 | topo: topology class or instance |
| 284 | **kwargs: additional topology parameters""" |
| 285 | args = list( args ) |
| 286 | name = args.pop( 0 ) |
| 287 | topo = kwargs.pop( 'topo', None ) |
Bob Lantz | 4b51d5c | 2016-05-27 14:47:38 -0700 | [diff] [blame] | 288 | nat = kwargs.pop( 'nat', 'nat0' ) |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 289 | # Default: single switch with 1 ONOS node |
| 290 | if not topo: |
| 291 | topo = SingleSwitchTopo |
| 292 | if not args: |
| 293 | args = ( 1, ) |
| 294 | if not isinstance( topo, Topo ): |
| 295 | topo = RenamedTopo( topo, *args, hnew='onos', **kwargs ) |
Bob Lantz | bb37d87 | 2016-05-16 16:26:13 -0700 | [diff] [blame] | 296 | self.ipBase = kwargs.pop( 'ipBase', '192.168.123.0/24' ) |
| 297 | self.forward = kwargs.pop( 'forward', |
| 298 | [ KarafPort, GUIPort, OpenFlowPort ] ) |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 299 | super( ONOSCluster, self ).__init__( name, inNamespace=False ) |
| 300 | fixIPTables() |
| 301 | self.env = initONOSEnv() |
Bob Lantz | bb37d87 | 2016-05-16 16:26:13 -0700 | [diff] [blame] | 302 | self.net = Mininet( topo=topo, ipBase=self.ipBase, |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 303 | host=ONOSNode, switch=LinuxBridge, |
| 304 | controller=None ) |
Bob Lantz | 4b51d5c | 2016-05-27 14:47:38 -0700 | [diff] [blame] | 305 | if nat: |
| 306 | self.net.addNAT( nat ).configDefault() |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 307 | updateNodeIPs( self.env, self.nodes() ) |
| 308 | self._remoteControllers = [] |
| 309 | |
| 310 | def start( self ): |
| 311 | "Start up ONOS cluster" |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 312 | info( '*** ONOS_APPS = %s\n' % ONOS_APPS ) |
| 313 | self.net.start() |
| 314 | for node in self.nodes(): |
| 315 | node.start( self.env ) |
| 316 | info( '\n' ) |
Bob Lantz | bb37d87 | 2016-05-16 16:26:13 -0700 | [diff] [blame] | 317 | self.configPortForwarding( ports=self.forward, action='A' ) |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 318 | self.waitStarted() |
| 319 | return |
| 320 | |
| 321 | def waitStarted( self ): |
| 322 | "Wait until all nodes have started" |
| 323 | startTime = time.time() |
| 324 | for node in self.nodes(): |
| 325 | info( node ) |
| 326 | node.waitStarted() |
Bob Lantz | bb37d87 | 2016-05-16 16:26:13 -0700 | [diff] [blame] | 327 | info( '*** Waited %.2f seconds for ONOS startup' % |
| 328 | ( time.time() - startTime ) ) |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 329 | |
| 330 | def stop( self ): |
| 331 | "Shut down ONOS cluster" |
Bob Lantz | bb37d87 | 2016-05-16 16:26:13 -0700 | [diff] [blame] | 332 | self.configPortForwarding( ports=self.forward, action='D' ) |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 333 | for node in self.nodes(): |
| 334 | node.stop() |
| 335 | self.net.stop() |
| 336 | |
| 337 | def nodes( self ): |
| 338 | "Return list of ONOS nodes" |
| 339 | return [ h for h in self.net.hosts if isinstance( h, ONOSNode ) ] |
| 340 | |
Bob Lantz | bb37d87 | 2016-05-16 16:26:13 -0700 | [diff] [blame] | 341 | def configPortForwarding( self, ports=[], intf='eth0', action='A' ): |
| 342 | """Start or stop ports on intf to all nodes |
| 343 | action: A=add/start, D=delete/stop (default: A)""" |
| 344 | for port in ports: |
| 345 | for index, node in enumerate( self.nodes() ): |
| 346 | ip, inport = node.IP(), port + index |
| 347 | # Configure a destination NAT rule |
| 348 | cmd = ( 'iptables -t nat -{action} PREROUTING -t nat ' |
| 349 | '-i {intf} -p tcp --dport {inport} ' |
| 350 | '-j DNAT --to-destination {ip}:{port}' ) |
| 351 | self.cmd( cmd.format( **locals() ) ) |
| 352 | |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 353 | |
| 354 | class ONOSSwitchMixin( object ): |
| 355 | "Mixin for switches that connect to an ONOSCluster" |
| 356 | def start( self, controllers ): |
| 357 | "Connect to ONOSCluster" |
| 358 | self.controllers = controllers |
| 359 | assert ( len( controllers ) is 1 and |
| 360 | isinstance( controllers[ 0 ], ONOSCluster ) ) |
| 361 | clist = controllers[ 0 ].nodes() |
| 362 | return super( ONOSSwitchMixin, self ).start( clist ) |
| 363 | |
| 364 | class ONOSOVSSwitch( ONOSSwitchMixin, OVSSwitch ): |
| 365 | "OVSSwitch that can connect to an ONOSCluster" |
| 366 | pass |
| 367 | |
| 368 | class ONOSUserSwitch( ONOSSwitchMixin, UserSwitch): |
| 369 | "UserSwitch that can connect to an ONOSCluster" |
| 370 | pass |
| 371 | |
| 372 | |
| 373 | ### Ugly utility routines |
| 374 | |
| 375 | def fixIPTables(): |
| 376 | "Fix LinuxBridge warning" |
| 377 | for s in 'arp', 'ip', 'ip6': |
| 378 | quietRun( 'sysctl net.bridge.bridge-nf-call-%stables=0' % s ) |
| 379 | |
| 380 | |
| 381 | ### Test code |
| 382 | |
| 383 | def test( serverCount ): |
| 384 | "Test this setup" |
| 385 | setLogLevel( 'info' ) |
| 386 | net = Mininet( topo=SingleSwitchTopo( 3 ), |
| 387 | controller=[ ONOSCluster( 'c0', serverCount ) ], |
| 388 | switch=ONOSOVSSwitch ) |
| 389 | net.start() |
| 390 | net.waitConnected() |
| 391 | CLI( net ) |
| 392 | net.stop() |
| 393 | |
| 394 | |
| 395 | ### CLI Extensions |
| 396 | |
| 397 | OldCLI = CLI |
| 398 | |
| 399 | class ONOSCLI( OldCLI ): |
| 400 | "CLI Extensions for ONOS" |
| 401 | |
| 402 | prompt = 'mininet-onos> ' |
| 403 | |
| 404 | def __init__( self, net, **kwargs ): |
| 405 | c0 = net.controllers[ 0 ] |
| 406 | if isinstance( c0, ONOSCluster ): |
| 407 | net = MininetFacade( net, cnet=c0.net ) |
| 408 | OldCLI.__init__( self, net, **kwargs ) |
| 409 | |
Bob Lantz | 4b51d5c | 2016-05-27 14:47:38 -0700 | [diff] [blame] | 410 | def onos1( self ): |
| 411 | "Helper function: return default ONOS node" |
| 412 | return self.mn.controllers[ 0 ].net.hosts[ 0 ] |
| 413 | |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 414 | def do_onos( self, line ): |
| 415 | "Send command to ONOS CLI" |
| 416 | c0 = self.mn.controllers[ 0 ] |
| 417 | if isinstance( c0, ONOSCluster ): |
| 418 | # cmdLoop strips off command name 'onos' |
| 419 | if line.startswith( ':' ): |
| 420 | line = 'onos' + line |
Bob Lantz | 4b51d5c | 2016-05-27 14:47:38 -0700 | [diff] [blame] | 421 | onos1 = self.onos1().name |
| 422 | if line: |
| 423 | line = '"%s"' % line |
| 424 | cmd = '%s client -h %s %s' % ( onos1, onos1, line ) |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 425 | quietRun( 'stty -echo' ) |
| 426 | self.default( cmd ) |
| 427 | quietRun( 'stty echo' ) |
| 428 | |
| 429 | def do_wait( self, line ): |
| 430 | "Wait for switches to connect" |
| 431 | self.mn.waitConnected() |
| 432 | |
| 433 | def do_balance( self, line ): |
| 434 | "Balance switch mastership" |
| 435 | self.do_onos( ':balance-masters' ) |
| 436 | |
| 437 | def do_log( self, line ): |
Bob Lantz | 4b51d5c | 2016-05-27 14:47:38 -0700 | [diff] [blame] | 438 | "Run tail -f /tmp/onos1/log; press control-C to stop" |
| 439 | self.default( self.onos1().name, 'tail -f /tmp/%s/log' % self.onos1() ) |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 440 | |
| 441 | |
| 442 | ### Exports for bin/mn |
| 443 | |
| 444 | CLI = ONOSCLI |
| 445 | |
| 446 | controllers = { 'onos': ONOSCluster, 'default': ONOSCluster } |
| 447 | |
| 448 | # XXX Hack to change default controller as above doesn't work |
| 449 | findController = lambda: ONOSCluster |
| 450 | |
| 451 | switches = { 'onos': ONOSOVSSwitch, |
| 452 | 'onosovs': ONOSOVSSwitch, |
| 453 | 'onosuser': ONOSUserSwitch, |
| 454 | 'default': ONOSOVSSwitch } |
| 455 | |
Bob Lantz | bb37d87 | 2016-05-16 16:26:13 -0700 | [diff] [blame] | 456 | # Null topology so we can control an external/hardware network |
| 457 | topos = { 'none': Topo } |
| 458 | |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 459 | if __name__ == '__main__': |
| 460 | if len( argv ) != 2: |
| 461 | test( 3 ) |
| 462 | else: |
| 463 | test( int( argv[ 1 ] ) ) |