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 |
Bob Lantz | 6438242 | 2016-06-03 22:51:39 -0700 | [diff] [blame] | 47 | from mininet.log import setLogLevel, info, warn, error, debug |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 48 | from mininet.cli import CLI |
Bob Lantz | 6438242 | 2016-06-03 22:51:39 -0700 | [diff] [blame] | 49 | from mininet.util import quietRun, specialClass |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 50 | from mininet.examples.controlnet import MininetFacade |
| 51 | |
| 52 | from os import environ |
| 53 | from os.path import dirname, join, isfile |
| 54 | from sys import argv |
| 55 | from glob import glob |
| 56 | import time |
Bob Lantz | 6438242 | 2016-06-03 22:51:39 -0700 | [diff] [blame] | 57 | from functools import partial |
Bob Lantz | 92d8e05 | 2016-06-17 16:03:58 -0700 | [diff] [blame] | 58 | |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 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 |
Jon Hall | a43d033 | 2016-08-04 15:02:23 -0700 | [diff] [blame] | 65 | CopycatPort = 9876 # Copycat port |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 66 | |
| 67 | def defaultUser(): |
| 68 | "Return a reasonable default user" |
| 69 | if 'SUDO_USER' in environ: |
| 70 | return environ[ 'SUDO_USER' ] |
| 71 | try: |
| 72 | user = quietRun( 'who am i' ).split()[ 0 ] |
| 73 | except: |
| 74 | user = 'nobody' |
| 75 | return user |
| 76 | |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 77 | # Module vars, initialized below |
Bob Lantz | 4b51d5c | 2016-05-27 14:47:38 -0700 | [diff] [blame] | 78 | HOME = ONOS_ROOT = ONOS_USER = None |
Lyndon Fawcett | fbad2e7 | 2017-09-20 11:46:17 +0000 | [diff] [blame] | 79 | ONOS_APPS = ONOS_WEB_USER = ONOS_WEB_PASS = ONOS_TAR = JAVA_OPTS = None |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 80 | |
| 81 | def initONOSEnv(): |
| 82 | """Initialize ONOS environment (and module) variables |
| 83 | This is ugly and painful, but they have to be set correctly |
| 84 | in order for the onos-setup-karaf script to work. |
| 85 | nodes: list of ONOS nodes |
| 86 | returns: ONOS environment variable dict""" |
| 87 | # pylint: disable=global-statement |
Bob Lantz | 4b51d5c | 2016-05-27 14:47:38 -0700 | [diff] [blame] | 88 | global HOME, ONOS_ROOT, ONOS_USER |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 89 | global ONOS_APPS, ONOS_WEB_USER, ONOS_WEB_PASS |
| 90 | env = {} |
| 91 | def sd( var, val ): |
| 92 | "Set default value for environment variable" |
| 93 | env[ var ] = environ.setdefault( var, val ) |
| 94 | return env[ var ] |
Bob Lantz | 4b51d5c | 2016-05-27 14:47:38 -0700 | [diff] [blame] | 95 | assert environ[ 'HOME' ] |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 96 | HOME = sd( 'HOME', environ[ 'HOME' ] ) |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 97 | ONOS_ROOT = sd( 'ONOS_ROOT', join( HOME, 'onos' ) ) |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 98 | environ[ 'ONOS_USER' ] = defaultUser() |
| 99 | ONOS_USER = sd( 'ONOS_USER', defaultUser() ) |
| 100 | ONOS_APPS = sd( 'ONOS_APPS', |
| 101 | 'drivers,openflow,fwd,proxyarp,mobility' ) |
Lyndon Fawcett | fbad2e7 | 2017-09-20 11:46:17 +0000 | [diff] [blame] | 102 | JAVA_OPTS = sd( 'JAVA_OPTS', '-Xms128m -Xmx512m' ) |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 103 | # ONOS_WEB_{USER,PASS} isn't respected by onos-karaf: |
| 104 | environ.update( ONOS_WEB_USER='karaf', ONOS_WEB_PASS='karaf' ) |
| 105 | ONOS_WEB_USER = sd( 'ONOS_WEB_USER', 'karaf' ) |
| 106 | ONOS_WEB_PASS = sd( 'ONOS_WEB_PASS', 'karaf' ) |
| 107 | return env |
| 108 | |
| 109 | |
| 110 | def updateNodeIPs( env, nodes ): |
| 111 | "Update env dict and environ with node IPs" |
| 112 | # Get rid of stale junk |
| 113 | for var in 'ONOS_NIC', 'ONOS_CELL', 'ONOS_INSTANCES': |
| 114 | env[ var ] = '' |
| 115 | for var in environ.keys(): |
| 116 | if var.startswith( 'OC' ): |
| 117 | env[ var ] = '' |
| 118 | for index, node in enumerate( nodes, 1 ): |
| 119 | var = 'OC%d' % index |
| 120 | env[ var ] = node.IP() |
Jon Hall | 2548559 | 2016-08-19 13:56:14 -0700 | [diff] [blame] | 121 | if nodes: |
| 122 | env[ 'OCI' ] = env[ 'OCN' ] = env[ 'OC1' ] |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 123 | env[ 'ONOS_INSTANCES' ] = '\n'.join( |
| 124 | node.IP() for node in nodes ) |
| 125 | environ.update( env ) |
| 126 | return env |
| 127 | |
| 128 | |
| 129 | tarDefaultPath = 'buck-out/gen/tools/package/onos-package/onos.tar.gz' |
| 130 | |
Bob Lantz | 1451d72 | 2016-05-17 14:40:07 -0700 | [diff] [blame] | 131 | def unpackONOS( destDir='/tmp', run=quietRun ): |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 132 | "Unpack ONOS and return its location" |
| 133 | global ONOS_TAR |
| 134 | environ.setdefault( 'ONOS_TAR', join( ONOS_ROOT, tarDefaultPath ) ) |
| 135 | ONOS_TAR = environ[ 'ONOS_TAR' ] |
| 136 | tarPath = ONOS_TAR |
| 137 | if not isfile( tarPath ): |
| 138 | raise Exception( 'Missing ONOS tarball %s - run buck build onos?' |
| 139 | % tarPath ) |
| 140 | info( '(unpacking %s)' % destDir) |
Bob Lantz | a2ccaa5 | 2016-06-29 18:26:06 -0700 | [diff] [blame] | 141 | success = '*** SUCCESS ***' |
| 142 | cmds = ( 'mkdir -p "%s" && cd "%s" && tar xzf "%s" && echo "%s"' |
| 143 | % ( destDir, destDir, tarPath, success ) ) |
| 144 | result = run( cmds, shell=True, verbose=True ) |
| 145 | if success not in result: |
| 146 | raise Exception( 'Failed to unpack ONOS archive %s in %s:\n%s\n' % |
| 147 | ( tarPath, destDir, result ) ) |
Bob Lantz | 1451d72 | 2016-05-17 14:40:07 -0700 | [diff] [blame] | 148 | # We can use quietRun for this usually |
| 149 | tarOutput = quietRun( 'tar tzf "%s" | head -1' % tarPath, shell=True) |
| 150 | tarOutput = tarOutput.split()[ 0 ].strip() |
| 151 | assert '/' in tarOutput |
| 152 | onosDir = join( destDir, dirname( tarOutput ) ) |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 153 | # Add symlink to log file |
Bob Lantz | 1451d72 | 2016-05-17 14:40:07 -0700 | [diff] [blame] | 154 | run( 'cd %s; ln -s onos*/apache* karaf;' |
| 155 | 'ln -s karaf/data/log/karaf.log log' % destDir, |
| 156 | shell=True ) |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 157 | return onosDir |
| 158 | |
| 159 | |
Bob Lantz | 9ba19dc | 2016-06-13 20:22:07 -0700 | [diff] [blame] | 160 | def waitListening( server, port=80, callback=None, sleepSecs=.5, |
| 161 | proc='java' ): |
| 162 | "Simplified netstat version of waitListening" |
| 163 | while True: |
| 164 | lines = server.cmd( 'netstat -natp' ).strip().split( '\n' ) |
| 165 | entries = [ line.split() for line in lines ] |
| 166 | portstr = ':%s' % port |
| 167 | listening = [ entry for entry in entries |
| 168 | if len( entry ) > 6 and portstr in entry[ 3 ] |
| 169 | and proc in entry[ 6 ] ] |
| 170 | if listening: |
| 171 | break |
Bob Lantz | 6438242 | 2016-06-03 22:51:39 -0700 | [diff] [blame] | 172 | info( '.' ) |
| 173 | if callback: |
| 174 | callback() |
| 175 | time.sleep( sleepSecs ) |
Bob Lantz | 6438242 | 2016-06-03 22:51:39 -0700 | [diff] [blame] | 176 | |
| 177 | |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 178 | ### Mininet classes |
| 179 | |
| 180 | def RenamedTopo( topo, *args, **kwargs ): |
| 181 | """Return specialized topo with renamed hosts |
| 182 | topo: topo class/class name to specialize |
| 183 | args, kwargs: topo args |
| 184 | sold: old switch name prefix (default 's') |
| 185 | snew: new switch name prefix |
| 186 | hold: old host name prefix (default 'h') |
| 187 | hnew: new host name prefix |
| 188 | This may be used from the mn command, e.g. |
| 189 | mn --topo renamed,single,spref=sw,hpref=host""" |
| 190 | sold = kwargs.pop( 'sold', 's' ) |
| 191 | hold = kwargs.pop( 'hold', 'h' ) |
| 192 | snew = kwargs.pop( 'snew', 'cs' ) |
| 193 | hnew = kwargs.pop( 'hnew' ,'ch' ) |
| 194 | topos = {} # TODO: use global TOPOS dict |
| 195 | if isinstance( topo, str ): |
| 196 | # Look up in topo directory - this allows us to |
| 197 | # use RenamedTopo from the command line! |
| 198 | if topo in topos: |
| 199 | topo = topos.get( topo ) |
| 200 | else: |
| 201 | raise Exception( 'Unknown topo name: %s' % topo ) |
| 202 | # pylint: disable=no-init |
| 203 | class RenamedTopoCls( topo ): |
| 204 | "Topo subclass with renamed nodes" |
| 205 | def addNode( self, name, *args, **kwargs ): |
| 206 | "Add a node, renaming if necessary" |
| 207 | if name.startswith( sold ): |
| 208 | name = snew + name[ len( sold ): ] |
| 209 | elif name.startswith( hold ): |
| 210 | name = hnew + name[ len( hold ): ] |
| 211 | return topo.addNode( self, name, *args, **kwargs ) |
| 212 | return RenamedTopoCls( *args, **kwargs ) |
| 213 | |
| 214 | |
Bob Lantz | 8e57625 | 2016-08-29 14:05:59 -0700 | [diff] [blame] | 215 | # We accept objects that "claim" to be a particular class, |
| 216 | # since the class definitions can be both execed (--custom) and |
| 217 | # imported (in another custom file), which breaks isinstance(). |
| 218 | # In order for this to work properly, a class should not be |
| 219 | # renamed so as to inappropriately omit or include the class |
| 220 | # name text. Note that mininet.util.specialClass renames classes |
| 221 | # by adding the specialized parameter names and values. |
| 222 | |
| 223 | def isONOSNode( obj ): |
| 224 | "Does obj claim to be some kind of ONOSNode?" |
| 225 | return ( isinstance( obj, ONOSNode) or |
| 226 | 'ONOSNode' in type( obj ).__name__ ) |
| 227 | |
| 228 | def isONOSCluster( obj ): |
| 229 | "Does obj claim to be some kind of ONOSCluster?" |
| 230 | return ( isinstance( obj, ONOSCluster ) or |
| 231 | 'ONOSCluster' in type( obj ).__name__ ) |
| 232 | |
| 233 | |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 234 | class ONOSNode( Controller ): |
| 235 | "ONOS cluster node" |
| 236 | |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 237 | def __init__( self, name, **kwargs ): |
Bob Lantz | 6438242 | 2016-06-03 22:51:39 -0700 | [diff] [blame] | 238 | "alertAction: exception|ignore|warn|exit (exception)" |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 239 | kwargs.update( inNamespace=True ) |
Bob Lantz | 6438242 | 2016-06-03 22:51:39 -0700 | [diff] [blame] | 240 | self.alertAction = kwargs.pop( 'alertAction', 'exception' ) |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 241 | Controller.__init__( self, name, **kwargs ) |
| 242 | self.dir = '/tmp/%s' % self.name |
Bob Lantz | 4b51d5c | 2016-05-27 14:47:38 -0700 | [diff] [blame] | 243 | self.client = self.dir + '/karaf/bin/client' |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 244 | self.ONOS_HOME = '/tmp' |
Bob Lantz | 55562ea | 2016-06-17 18:19:03 -0700 | [diff] [blame] | 245 | self.cmd( 'rm -rf', self.dir ) |
| 246 | self.ONOS_HOME = unpackONOS( self.dir, run=self.ucmd ) |
Jon Hall | 2548559 | 2016-08-19 13:56:14 -0700 | [diff] [blame] | 247 | self.ONOS_ROOT = ONOS_ROOT |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 248 | |
| 249 | # pylint: disable=arguments-differ |
| 250 | |
jaegonkim | 4e36049 | 2018-02-17 08:44:35 +0900 | [diff] [blame] | 251 | |
| 252 | def start( self, env=None, nodes=() ): |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 253 | """Start ONOS on node |
Bob Lantz | 569bbec | 2016-06-03 18:51:16 -0700 | [diff] [blame] | 254 | env: environment var dict |
| 255 | nodes: all nodes in cluster""" |
jaegonkim | 4e36049 | 2018-02-17 08:44:35 +0900 | [diff] [blame] | 256 | if env is not None: |
| 257 | self.genStartConfig(env, nodes) |
| 258 | self.cmd( 'cd', self.ONOS_HOME ) |
| 259 | info( '(starting %s)' % self ) |
| 260 | service = join( self.ONOS_HOME, 'bin/onos-service' ) |
| 261 | self.ucmd( service, 'server 1>../onos.log 2>../onos.log' |
| 262 | ' & echo $! > onos.pid; ln -s `pwd`/onos.pid ..' ) |
| 263 | self.onosPid = int( self.cmd( 'cat onos.pid' ).strip() ) |
| 264 | self.warningCount = 0 |
| 265 | |
| 266 | # pylint: disable=arguments-differ |
| 267 | |
| 268 | def genStartConfig( self, env, nodes=() ): |
| 269 | """generate a start config""" |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 270 | env = dict( env ) |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 271 | env.update( ONOS_HOME=self.ONOS_HOME ) |
| 272 | self.updateEnv( env ) |
| 273 | karafbin = glob( '%s/apache*/bin' % self.ONOS_HOME )[ 0 ] |
| 274 | onosbin = join( ONOS_ROOT, 'tools/test/bin' ) |
| 275 | self.cmd( 'export PATH=%s:%s:$PATH' % ( onosbin, karafbin ) ) |
| 276 | self.cmd( 'cd', self.ONOS_HOME ) |
Bob Lantz | 1451d72 | 2016-05-17 14:40:07 -0700 | [diff] [blame] | 277 | self.ucmd( 'mkdir -p config && ' |
Bob Lantz | 569bbec | 2016-06-03 18:51:16 -0700 | [diff] [blame] | 278 | 'onos-gen-partitions config/cluster.json', |
| 279 | ' '.join( node.IP() for node in nodes ) ) |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 280 | |
Carmelo Cascone | c52a4b1 | 2016-10-24 16:47:17 +0200 | [diff] [blame] | 281 | def intfsDown( self ): |
| 282 | """Bring all interfaces down""" |
| 283 | for intf in self.intfs.values(): |
| 284 | cmdOutput = intf.ifconfig( 'down' ) |
| 285 | # no output indicates success |
| 286 | if cmdOutput: |
| 287 | error( "Error setting %s down: %s " % ( intf.name, cmdOutput ) ) |
| 288 | |
| 289 | def intfsUp( self ): |
| 290 | """Bring all interfaces up""" |
| 291 | for intf in self.intfs.values(): |
| 292 | cmdOutput = intf.ifconfig( 'up' ) |
| 293 | if cmdOutput: |
| 294 | error( "Error setting %s up: %s " % ( intf.name, cmdOutput ) ) |
| 295 | |
jaegonkim | 4e36049 | 2018-02-17 08:44:35 +0900 | [diff] [blame] | 296 | def kill( self ): |
| 297 | """Kill ONOS process""" |
| 298 | self.cmd( 'kill %d && wait' % ( self.onosPid ) ) |
| 299 | |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 300 | def stop( self ): |
| 301 | # XXX This will kill all karafs - too bad! |
| 302 | self.cmd( 'pkill -HUP -f karaf.jar && wait' ) |
| 303 | self.cmd( 'rm -rf', self.dir ) |
| 304 | |
Bob Lantz | 6438242 | 2016-06-03 22:51:39 -0700 | [diff] [blame] | 305 | def sanityAlert( self, *args ): |
| 306 | "Alert to raise on sanityCheck failure" |
| 307 | info( '\n' ) |
| 308 | if self.alertAction == 'exception': |
| 309 | raise Exception( *args ) |
| 310 | if self.alertAction == 'warn': |
| 311 | warn( *args + ( '\n', ) ) |
| 312 | elif self.alertAction == 'exit': |
| 313 | error( '***', *args + |
| 314 | ( '\nExiting. Run "sudo mn -c" to clean up.\n', ) ) |
| 315 | exit( 1 ) |
| 316 | |
Bob Lantz | 4b51d5c | 2016-05-27 14:47:38 -0700 | [diff] [blame] | 317 | def isRunning( self ): |
| 318 | "Is our ONOS process still running?" |
Bob Lantz | 6438242 | 2016-06-03 22:51:39 -0700 | [diff] [blame] | 319 | cmd = ( 'ps -p %d >/dev/null 2>&1 && echo "running" ||' |
| 320 | 'echo "not running"' ) |
| 321 | return self.cmd( cmd % self.onosPid ).strip() == 'running' |
Bob Lantz | 4b51d5c | 2016-05-27 14:47:38 -0700 | [diff] [blame] | 322 | |
Bob Lantz | 6438242 | 2016-06-03 22:51:39 -0700 | [diff] [blame] | 323 | def checkLog( self ): |
| 324 | "Return log file errors and warnings" |
| 325 | log = join( self.dir, 'log' ) |
Bob Lantz | c96e258 | 2016-06-13 18:57:04 -0700 | [diff] [blame] | 326 | errors, warnings = [], [] |
Bob Lantz | 6438242 | 2016-06-03 22:51:39 -0700 | [diff] [blame] | 327 | if isfile( log ): |
| 328 | lines = open( log ).read().split( '\n' ) |
| 329 | errors = [ line for line in lines if 'ERROR' in line ] |
| 330 | warnings = [ line for line in lines if 'WARN'in line ] |
| 331 | return errors, warnings |
| 332 | |
| 333 | def memAvailable( self ): |
| 334 | "Return available memory in KB (or -1 if we can't tell)" |
| 335 | lines = open( '/proc/meminfo' ).read().strip().split( '\n' ) |
| 336 | entries = map( str.split, lines ) |
| 337 | index = { entry[ 0 ]: entry for entry in entries } |
| 338 | # Check MemAvailable if present |
| 339 | default = ( None, '-1', 'kB' ) |
| 340 | _name, count, unit = index.get( 'MemAvailable:', default ) |
| 341 | if unit.lower() == 'kb': |
| 342 | return int( count ) |
| 343 | return -1 |
| 344 | |
| 345 | def sanityCheck( self, lowMem=100000 ): |
| 346 | """Check whether we've quit or are running out of memory |
| 347 | lowMem: low memory threshold in KB (100000)""" |
| 348 | # Are we still running? |
Bob Lantz | 4b51d5c | 2016-05-27 14:47:38 -0700 | [diff] [blame] | 349 | if not self.isRunning(): |
Bob Lantz | 6438242 | 2016-06-03 22:51:39 -0700 | [diff] [blame] | 350 | self.sanityAlert( 'ONOS node %s has died' % self.name ) |
| 351 | # Are there errors in the log file? |
| 352 | errors, warnings = self.checkLog() |
| 353 | if errors: |
| 354 | self.sanityAlert( 'ONOS startup errors:\n<<%s>>' % |
| 355 | '\n'.join( errors ) ) |
| 356 | warningCount = len( warnings ) |
| 357 | if warnings and warningCount > self.warningCount: |
| 358 | warn( '(%d warnings)' % len( warnings ) ) |
| 359 | self.warningCount = warningCount |
| 360 | # Are we running out of memory? |
| 361 | mem = self.memAvailable() |
| 362 | if mem > 0 and mem < lowMem: |
| 363 | self.sanityAlert( 'Running out of memory (only %d KB available)' |
| 364 | % mem ) |
Bob Lantz | 4b51d5c | 2016-05-27 14:47:38 -0700 | [diff] [blame] | 365 | |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 366 | def waitStarted( self ): |
| 367 | "Wait until we've really started" |
| 368 | info( '(checking: karaf' ) |
| 369 | while True: |
Bob Lantz | 1451d72 | 2016-05-17 14:40:07 -0700 | [diff] [blame] | 370 | status = self.ucmd( 'karaf status' ).lower() |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 371 | if 'running' in status and 'not running' not in status: |
| 372 | break |
| 373 | info( '.' ) |
Bob Lantz | 6438242 | 2016-06-03 22:51:39 -0700 | [diff] [blame] | 374 | self.sanityCheck() |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 375 | time.sleep( 1 ) |
| 376 | info( ' ssh-port' ) |
Bob Lantz | 6438242 | 2016-06-03 22:51:39 -0700 | [diff] [blame] | 377 | waitListening( server=self, port=KarafPort, callback=self.sanityCheck ) |
Carmelo Cascone | c3baa4c | 2017-11-21 00:06:11 -0800 | [diff] [blame] | 378 | info( ' protocol' ) |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 379 | while True: |
Bob Lantz | 9ba19dc | 2016-06-13 20:22:07 -0700 | [diff] [blame] | 380 | result = quietRun( '%s -h %s "apps -a"' % |
Bob Lantz | bb37d87 | 2016-05-16 16:26:13 -0700 | [diff] [blame] | 381 | ( self.client, self.IP() ), shell=True ) |
Carmelo Cascone | c3baa4c | 2017-11-21 00:06:11 -0800 | [diff] [blame] | 382 | if 'openflow' in result or 'p4runtime' in result: |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 383 | break |
| 384 | info( '.' ) |
Bob Lantz | 6438242 | 2016-06-03 22:51:39 -0700 | [diff] [blame] | 385 | self.sanityCheck() |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 386 | time.sleep( 1 ) |
Jon Hall | a43d033 | 2016-08-04 15:02:23 -0700 | [diff] [blame] | 387 | info( ' node-status' ) |
| 388 | while True: |
| 389 | result = quietRun( '%s -h %s "nodes"' % |
| 390 | ( self.client, self.IP() ), shell=True ) |
Carmelo Cascone | c3baa4c | 2017-11-21 00:06:11 -0800 | [diff] [blame] | 391 | nodeStr = 'id=%s, address=%s:%s, state=READY' %\ |
Jon Hall | a43d033 | 2016-08-04 15:02:23 -0700 | [diff] [blame] | 392 | ( self.IP(), self.IP(), CopycatPort ) |
| 393 | if nodeStr in result: |
| 394 | break |
| 395 | info( '.' ) |
| 396 | self.sanityCheck() |
| 397 | time.sleep( 1 ) |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 398 | info( ')\n' ) |
| 399 | |
| 400 | def updateEnv( self, envDict ): |
| 401 | "Update environment variables" |
Bob Lantz | 569bbec | 2016-06-03 18:51:16 -0700 | [diff] [blame] | 402 | cmd = ';'.join( ( 'export %s="%s"' % ( var, val ) |
| 403 | if val else 'unset %s' % var ) |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 404 | for var, val in envDict.iteritems() ) |
| 405 | self.cmd( cmd ) |
| 406 | |
Bob Lantz | 1451d72 | 2016-05-17 14:40:07 -0700 | [diff] [blame] | 407 | def ucmd( self, *args, **_kwargs ): |
| 408 | "Run command as $ONOS_USER using sudo -E -u" |
| 409 | if ONOS_USER != 'root': # don't bother with sudo |
| 410 | args = [ "sudo -E -u $ONOS_USER PATH=$PATH " |
| 411 | "bash -c '%s'" % ' '.join( args ) ] |
| 412 | return self.cmd( *args ) |
| 413 | |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 414 | |
| 415 | class ONOSCluster( Controller ): |
| 416 | "ONOS Cluster" |
Bob Lantz | 5763516 | 2016-08-29 15:13:54 -0700 | [diff] [blame] | 417 | # Offset for port forwarding |
| 418 | portOffset = 0 |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 419 | def __init__( self, *args, **kwargs ): |
| 420 | """name: (first parameter) |
| 421 | *args: topology class parameters |
| 422 | ipBase: IP range for ONOS nodes |
Bob Lantz | 5763516 | 2016-08-29 15:13:54 -0700 | [diff] [blame] | 423 | forward: default port forwarding list |
| 424 | portOffset: offset to port base (optional) |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 425 | topo: topology class or instance |
Bob Lantz | 6438242 | 2016-06-03 22:51:39 -0700 | [diff] [blame] | 426 | nodeOpts: ONOSNode options |
Bob Lantz | 5763516 | 2016-08-29 15:13:54 -0700 | [diff] [blame] | 427 | **kwargs: additional topology parameters |
| 428 | By default, multiple ONOSClusters will increment |
| 429 | the portOffset automatically; alternately, it can |
| 430 | be specified explicitly. |
| 431 | """ |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 432 | args = list( args ) |
| 433 | name = args.pop( 0 ) |
| 434 | topo = kwargs.pop( 'topo', None ) |
Bob Lantz | 930138e | 2016-06-23 18:53:19 -0700 | [diff] [blame] | 435 | self.nat = kwargs.pop( 'nat', 'nat0' ) |
Bob Lantz | 6438242 | 2016-06-03 22:51:39 -0700 | [diff] [blame] | 436 | nodeOpts = kwargs.pop( 'nodeOpts', {} ) |
Bob Lantz | 5763516 | 2016-08-29 15:13:54 -0700 | [diff] [blame] | 437 | self.portOffset = kwargs.pop( 'portOffset', ONOSCluster.portOffset ) |
Jon Hall | 9b238ae | 2016-08-09 13:47:43 -0700 | [diff] [blame] | 438 | # Pass in kwargs to the ONOSNodes instead of the cluster |
| 439 | "alertAction: exception|ignore|warn|exit (exception)" |
| 440 | alertAction = kwargs.pop( 'alertAction', None ) |
| 441 | if alertAction: |
| 442 | nodeOpts[ 'alertAction'] = alertAction |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 443 | # Default: single switch with 1 ONOS node |
| 444 | if not topo: |
| 445 | topo = SingleSwitchTopo |
| 446 | if not args: |
| 447 | args = ( 1, ) |
| 448 | if not isinstance( topo, Topo ): |
| 449 | topo = RenamedTopo( topo, *args, hnew='onos', **kwargs ) |
Bob Lantz | bb37d87 | 2016-05-16 16:26:13 -0700 | [diff] [blame] | 450 | self.ipBase = kwargs.pop( 'ipBase', '192.168.123.0/24' ) |
| 451 | self.forward = kwargs.pop( 'forward', |
| 452 | [ KarafPort, GUIPort, OpenFlowPort ] ) |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 453 | super( ONOSCluster, self ).__init__( name, inNamespace=False ) |
| 454 | fixIPTables() |
| 455 | self.env = initONOSEnv() |
Bob Lantz | bb37d87 | 2016-05-16 16:26:13 -0700 | [diff] [blame] | 456 | self.net = Mininet( topo=topo, ipBase=self.ipBase, |
Bob Lantz | 6438242 | 2016-06-03 22:51:39 -0700 | [diff] [blame] | 457 | host=partial( ONOSNode, **nodeOpts ), |
| 458 | switch=LinuxBridge, |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 459 | controller=None ) |
Bob Lantz | 930138e | 2016-06-23 18:53:19 -0700 | [diff] [blame] | 460 | if self.nat: |
| 461 | self.net.addNAT( self.nat ).configDefault() |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 462 | updateNodeIPs( self.env, self.nodes() ) |
| 463 | self._remoteControllers = [] |
Bob Lantz | 5763516 | 2016-08-29 15:13:54 -0700 | [diff] [blame] | 464 | # Update port offset for more ONOS clusters |
| 465 | ONOSCluster.portOffset += len( self.nodes() ) |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 466 | |
| 467 | def start( self ): |
| 468 | "Start up ONOS cluster" |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 469 | info( '*** ONOS_APPS = %s\n' % ONOS_APPS ) |
| 470 | self.net.start() |
| 471 | for node in self.nodes(): |
Bob Lantz | 569bbec | 2016-06-03 18:51:16 -0700 | [diff] [blame] | 472 | node.start( self.env, self.nodes() ) |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 473 | info( '\n' ) |
Bob Lantz | bb37d87 | 2016-05-16 16:26:13 -0700 | [diff] [blame] | 474 | self.configPortForwarding( ports=self.forward, action='A' ) |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 475 | self.waitStarted() |
| 476 | return |
| 477 | |
| 478 | def waitStarted( self ): |
| 479 | "Wait until all nodes have started" |
| 480 | startTime = time.time() |
| 481 | for node in self.nodes(): |
| 482 | info( node ) |
| 483 | node.waitStarted() |
Bob Lantz | bb37d87 | 2016-05-16 16:26:13 -0700 | [diff] [blame] | 484 | info( '*** Waited %.2f seconds for ONOS startup' % |
| 485 | ( time.time() - startTime ) ) |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 486 | |
| 487 | def stop( self ): |
| 488 | "Shut down ONOS cluster" |
Bob Lantz | bb37d87 | 2016-05-16 16:26:13 -0700 | [diff] [blame] | 489 | self.configPortForwarding( ports=self.forward, action='D' ) |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 490 | for node in self.nodes(): |
| 491 | node.stop() |
| 492 | self.net.stop() |
| 493 | |
| 494 | def nodes( self ): |
| 495 | "Return list of ONOS nodes" |
Bob Lantz | 8e57625 | 2016-08-29 14:05:59 -0700 | [diff] [blame] | 496 | return [ h for h in self.net.hosts if isONOSNode( h ) ] |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 497 | |
Bob Lantz | 930138e | 2016-06-23 18:53:19 -0700 | [diff] [blame] | 498 | def configPortForwarding( self, ports=[], action='A' ): |
| 499 | """Start or stop port forwarding (any intf) for all nodes |
| 500 | ports: list of ports to forward |
Bob Lantz | bb37d87 | 2016-05-16 16:26:13 -0700 | [diff] [blame] | 501 | action: A=add/start, D=delete/stop (default: A)""" |
Bob Lantz | 930138e | 2016-06-23 18:53:19 -0700 | [diff] [blame] | 502 | self.cmd( 'iptables -' + action, 'FORWARD -d', self.ipBase, |
| 503 | '-j ACCEPT' ) |
Bob Lantz | bb37d87 | 2016-05-16 16:26:13 -0700 | [diff] [blame] | 504 | for port in ports: |
| 505 | for index, node in enumerate( self.nodes() ): |
Bob Lantz | 5763516 | 2016-08-29 15:13:54 -0700 | [diff] [blame] | 506 | ip, inport = node.IP(), port + self.portOffset + index |
Bob Lantz | bb37d87 | 2016-05-16 16:26:13 -0700 | [diff] [blame] | 507 | # Configure a destination NAT rule |
Bob Lantz | 930138e | 2016-06-23 18:53:19 -0700 | [diff] [blame] | 508 | self.cmd( 'iptables -t nat -' + action, |
| 509 | 'PREROUTING -t nat -p tcp --dport', inport, |
| 510 | '-j DNAT --to-destination %s:%s' % ( ip, port ) ) |
Bob Lantz | bb37d87 | 2016-05-16 16:26:13 -0700 | [diff] [blame] | 511 | |
jaegonkim | 4e36049 | 2018-02-17 08:44:35 +0900 | [diff] [blame] | 512 | def getONOSNode( self, instance ): |
| 513 | """Return ONOS node which name is 'instance' |
| 514 | instance: ONOS instance name""" |
| 515 | try: |
| 516 | onos = self.net.getNodeByName( instance ) |
| 517 | if isONOSNode( onos ): |
| 518 | return onos |
| 519 | else: |
| 520 | info( 'instance %s is not ONOS.\n' % instance ) |
| 521 | except KeyError: |
| 522 | info( 'No such ONOS instance %s.\n' % instance ) |
Bob Lantz | 5763516 | 2016-08-29 15:13:54 -0700 | [diff] [blame] | 523 | |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 524 | class ONOSSwitchMixin( object ): |
| 525 | "Mixin for switches that connect to an ONOSCluster" |
| 526 | def start( self, controllers ): |
| 527 | "Connect to ONOSCluster" |
| 528 | self.controllers = controllers |
| 529 | assert ( len( controllers ) is 1 and |
Bob Lantz | 8e57625 | 2016-08-29 14:05:59 -0700 | [diff] [blame] | 530 | isONOSCluster( controllers[ 0 ] ) ) |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 531 | clist = controllers[ 0 ].nodes() |
| 532 | return super( ONOSSwitchMixin, self ).start( clist ) |
| 533 | |
| 534 | class ONOSOVSSwitch( ONOSSwitchMixin, OVSSwitch ): |
| 535 | "OVSSwitch that can connect to an ONOSCluster" |
| 536 | pass |
| 537 | |
| 538 | class ONOSUserSwitch( ONOSSwitchMixin, UserSwitch): |
| 539 | "UserSwitch that can connect to an ONOSCluster" |
| 540 | pass |
| 541 | |
| 542 | |
| 543 | ### Ugly utility routines |
| 544 | |
| 545 | def fixIPTables(): |
| 546 | "Fix LinuxBridge warning" |
| 547 | for s in 'arp', 'ip', 'ip6': |
| 548 | quietRun( 'sysctl net.bridge.bridge-nf-call-%stables=0' % s ) |
| 549 | |
| 550 | |
| 551 | ### Test code |
| 552 | |
| 553 | def test( serverCount ): |
| 554 | "Test this setup" |
| 555 | setLogLevel( 'info' ) |
| 556 | net = Mininet( topo=SingleSwitchTopo( 3 ), |
| 557 | controller=[ ONOSCluster( 'c0', serverCount ) ], |
| 558 | switch=ONOSOVSSwitch ) |
| 559 | net.start() |
| 560 | net.waitConnected() |
| 561 | CLI( net ) |
| 562 | net.stop() |
| 563 | |
| 564 | |
| 565 | ### CLI Extensions |
| 566 | |
| 567 | OldCLI = CLI |
| 568 | |
| 569 | class ONOSCLI( OldCLI ): |
| 570 | "CLI Extensions for ONOS" |
| 571 | |
| 572 | prompt = 'mininet-onos> ' |
| 573 | |
| 574 | def __init__( self, net, **kwargs ): |
Bob Lantz | 503a402 | 2016-08-29 18:08:37 -0700 | [diff] [blame] | 575 | clusters = [ c.net for c in net.controllers |
| 576 | if isONOSCluster( c ) ] |
| 577 | net = MininetFacade( net, *clusters ) |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 578 | OldCLI.__init__( self, net, **kwargs ) |
| 579 | |
Bob Lantz | 4b51d5c | 2016-05-27 14:47:38 -0700 | [diff] [blame] | 580 | def onos1( self ): |
| 581 | "Helper function: return default ONOS node" |
| 582 | return self.mn.controllers[ 0 ].net.hosts[ 0 ] |
| 583 | |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 584 | def do_onos( self, line ): |
| 585 | "Send command to ONOS CLI" |
| 586 | c0 = self.mn.controllers[ 0 ] |
Bob Lantz | 8e57625 | 2016-08-29 14:05:59 -0700 | [diff] [blame] | 587 | if isONOSCluster( c0 ): |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 588 | # cmdLoop strips off command name 'onos' |
| 589 | if line.startswith( ':' ): |
| 590 | line = 'onos' + line |
Bob Lantz | 4b51d5c | 2016-05-27 14:47:38 -0700 | [diff] [blame] | 591 | onos1 = self.onos1().name |
| 592 | if line: |
| 593 | line = '"%s"' % line |
| 594 | cmd = '%s client -h %s %s' % ( onos1, onos1, line ) |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 595 | quietRun( 'stty -echo' ) |
| 596 | self.default( cmd ) |
| 597 | quietRun( 'stty echo' ) |
| 598 | |
| 599 | def do_wait( self, line ): |
| 600 | "Wait for switches to connect" |
| 601 | self.mn.waitConnected() |
| 602 | |
| 603 | def do_balance( self, line ): |
| 604 | "Balance switch mastership" |
| 605 | self.do_onos( ':balance-masters' ) |
| 606 | |
| 607 | def do_log( self, line ): |
Bob Lantz | 4b51d5c | 2016-05-27 14:47:38 -0700 | [diff] [blame] | 608 | "Run tail -f /tmp/onos1/log; press control-C to stop" |
Bob Lantz | 9ba19dc | 2016-06-13 20:22:07 -0700 | [diff] [blame] | 609 | self.default( '%s tail -f /tmp/%s/log' % |
| 610 | ( self.onos1(), self.onos1() ) ) |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 611 | |
Bob Lantz | 6438242 | 2016-06-03 22:51:39 -0700 | [diff] [blame] | 612 | def do_status( self, line ): |
| 613 | "Return status of ONOS cluster(s)" |
| 614 | for c in self.mn.controllers: |
Bob Lantz | 8e57625 | 2016-08-29 14:05:59 -0700 | [diff] [blame] | 615 | if isONOSCluster( c ): |
Bob Lantz | 6438242 | 2016-06-03 22:51:39 -0700 | [diff] [blame] | 616 | for node in c.net.hosts: |
Bob Lantz | 8e57625 | 2016-08-29 14:05:59 -0700 | [diff] [blame] | 617 | if isONOSNode( node ): |
Bob Lantz | 6438242 | 2016-06-03 22:51:39 -0700 | [diff] [blame] | 618 | errors, warnings = node.checkLog() |
| 619 | running = ( 'Running' if node.isRunning() |
| 620 | else 'Exited' ) |
| 621 | status = '' |
| 622 | if errors: |
| 623 | status += '%d ERRORS ' % len( errors ) |
| 624 | if warnings: |
| 625 | status += '%d warnings' % len( warnings ) |
| 626 | status = status if status else 'OK' |
| 627 | info( node, '\t', running, '\t', status, '\n' ) |
| 628 | |
Bob Lantz | c96e258 | 2016-06-13 18:57:04 -0700 | [diff] [blame] | 629 | def do_arp( self, line ): |
| 630 | "Send gratuitous arps from all data network hosts" |
| 631 | startTime = time.time() |
| 632 | try: |
| 633 | count = int( line ) |
| 634 | except: |
| 635 | count = 1 |
| 636 | # Technically this check should be on the host |
Bob Lantz | c3de515 | 2016-06-17 17:13:57 -0700 | [diff] [blame] | 637 | if '-U' not in quietRun( 'arping -h', shell=True ): |
| 638 | warn( 'Please install iputils-arping.\n' ) |
Bob Lantz | c96e258 | 2016-06-13 18:57:04 -0700 | [diff] [blame] | 639 | return |
| 640 | # This is much faster if we do it in parallel |
| 641 | for host in self.mn.net.hosts: |
| 642 | intf = host.defaultIntf() |
| 643 | # -b: keep using broadcasts; -f: quit after 1 reply |
| 644 | # -U: gratuitous ARP update |
| 645 | host.sendCmd( 'arping -bf -c', count, '-U -I', |
| 646 | intf.name, intf.IP() ) |
| 647 | for host in self.mn.net.hosts: |
| 648 | # We could check the output here if desired |
| 649 | host.waitOutput() |
| 650 | info( '.' ) |
| 651 | info( '\n' ) |
| 652 | elapsed = time.time() - startTime |
| 653 | debug( 'Completed in %.2f seconds\n' % elapsed ) |
| 654 | |
Carmelo Cascone | c52a4b1 | 2016-10-24 16:47:17 +0200 | [diff] [blame] | 655 | def onosupdown( self, cmd, instance ): |
| 656 | if not instance: |
| 657 | info( 'Provide the name of an ONOS instance.\n' ) |
| 658 | return |
| 659 | c0 = self.mn.controllers[ 0 ] |
| 660 | if isONOSCluster( c0 ): |
jaegonkim | 4e36049 | 2018-02-17 08:44:35 +0900 | [diff] [blame] | 661 | onos = c0.getONOSNode( instance ) |
| 662 | if onos: |
| 663 | info('Bringing %s %s...\n' % ( instance, cmd ) ) |
| 664 | if cmd == 'up': |
| 665 | onos.intfsUp() |
| 666 | else: |
| 667 | onos.intfsDown() |
Carmelo Cascone | c52a4b1 | 2016-10-24 16:47:17 +0200 | [diff] [blame] | 668 | |
| 669 | def do_onosdown( self, instance=None ): |
| 670 | """Disconnects an ONOS instance from the network""" |
| 671 | self.onosupdown( 'down', instance ) |
| 672 | |
| 673 | def do_onosup( self, instance=None ): |
| 674 | """"Connects an ONOS instance to the network""" |
| 675 | self.onosupdown( 'up', instance ) |
| 676 | |
jaegonkim | 4e36049 | 2018-02-17 08:44:35 +0900 | [diff] [blame] | 677 | def do_start( self, instance=None ): |
| 678 | """Start ONOS instance""" |
| 679 | if not instance: |
| 680 | info( 'Provide the name of an ONOS instance.\n' ) |
| 681 | return |
| 682 | c0 = self.mn.controllers[ 0 ] |
| 683 | if isONOSCluster( c0 ): |
| 684 | onos = c0.getONOSNode( instance ) |
| 685 | if onos: |
| 686 | info('Starting %s...\n' % ( instance ) ) |
| 687 | onos.start() |
| 688 | |
| 689 | def do_kill( self, instance=None ): |
| 690 | """Kill ONOS instance""" |
| 691 | if not instance: |
| 692 | info( 'Provide the name of an ONOS instance.\n' ) |
| 693 | return |
| 694 | c0 = self.mn.controllers[ 0 ] |
| 695 | if isONOSCluster( c0 ): |
| 696 | onos = c0.getONOSNode( instance ) |
| 697 | if onos: |
| 698 | info('Killing %s...\n' % ( instance ) ) |
| 699 | onos.kill() |
Bob Lantz | 6438242 | 2016-06-03 22:51:39 -0700 | [diff] [blame] | 700 | |
| 701 | # For interactive use, exit on error |
| 702 | exitOnError = dict( nodeOpts={ 'alertAction': 'exit' } ) |
| 703 | ONOSClusterInteractive = specialClass( ONOSCluster, defaults=exitOnError ) |
| 704 | |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 705 | |
| 706 | ### Exports for bin/mn |
| 707 | |
| 708 | CLI = ONOSCLI |
Bob Lantz | 6438242 | 2016-06-03 22:51:39 -0700 | [diff] [blame] | 709 | controllers = { 'onos': ONOSClusterInteractive, |
| 710 | 'default': ONOSClusterInteractive } |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 711 | |
| 712 | # XXX Hack to change default controller as above doesn't work |
Bob Lantz | 6438242 | 2016-06-03 22:51:39 -0700 | [diff] [blame] | 713 | findController = lambda: controllers[ 'default' ] |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 714 | |
| 715 | switches = { 'onos': ONOSOVSSwitch, |
| 716 | 'onosovs': ONOSOVSSwitch, |
| 717 | 'onosuser': ONOSUserSwitch, |
| 718 | 'default': ONOSOVSSwitch } |
| 719 | |
Bob Lantz | bb37d87 | 2016-05-16 16:26:13 -0700 | [diff] [blame] | 720 | # Null topology so we can control an external/hardware network |
| 721 | topos = { 'none': Topo } |
| 722 | |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 723 | if __name__ == '__main__': |
| 724 | if len( argv ) != 2: |
| 725 | test( 3 ) |
| 726 | else: |
| 727 | test( int( argv[ 1 ] ) ) |