Bob Lantz | c7c0540 | 2013-12-16 21:22:12 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | """ |
Bob Lantz | 7ead053 | 2013-12-17 20:41:20 -0800 | [diff] [blame] | 4 | onos.py: A basic (?) ONOS Controller() subclass for Mininet |
Bob Lantz | c7c0540 | 2013-12-16 21:22:12 -0800 | [diff] [blame] | 5 | |
| 6 | We implement the following classes: |
| 7 | |
| 8 | ONOSController: a custom Controller() subclass to start ONOS |
| 9 | OVSSwitchONOS: a custom OVSSwitch() switch that connects to multiple controllers. |
| 10 | |
Bob Lantz | 63bbe4c | 2014-02-06 19:29:55 -0800 | [diff] [blame] | 11 | We use single Zookeeper and Ramcloud instances for now. |
Bob Lantz | c7c0540 | 2013-12-16 21:22:12 -0800 | [diff] [blame] | 12 | |
| 13 | As a custom file, exports: |
| 14 | |
| 15 | --controller onos |
| 16 | --switch ovso |
| 17 | |
| 18 | Usage: |
| 19 | |
Bob Lantz | 21b41f6 | 2013-12-18 18:16:38 -0800 | [diff] [blame] | 20 | $ sudo -E ./onos.py |
Bob Lantz | c7c0540 | 2013-12-16 21:22:12 -0800 | [diff] [blame] | 21 | |
| 22 | This will start up a simple 2-host, 2 ONOS network |
| 23 | |
Bob Lantz | 21b41f6 | 2013-12-18 18:16:38 -0800 | [diff] [blame] | 24 | $ sudo -E mn --custom onos.py --controller onos,2 --switch ovso |
Bob Lantz | c7c0540 | 2013-12-16 21:22:12 -0800 | [diff] [blame] | 25 | """ |
| 26 | |
Bob Lantz | 63bbe4c | 2014-02-06 19:29:55 -0800 | [diff] [blame] | 27 | from mininet.node import Controller, OVSSwitch, CPULimitedHost, RemoteController |
Bob Lantz | c7c0540 | 2013-12-16 21:22:12 -0800 | [diff] [blame] | 28 | from mininet.net import Mininet |
| 29 | from mininet.cli import CLI |
Bob Lantz | 7ead053 | 2013-12-17 20:41:20 -0800 | [diff] [blame] | 30 | from mininet.topo import LinearTopo |
| 31 | from mininet.log import setLogLevel, info, warn |
Bob Lantz | 63bbe4c | 2014-02-06 19:29:55 -0800 | [diff] [blame] | 32 | from mininet.util import quietRun, numCores |
Bob Lantz | c7c0540 | 2013-12-16 21:22:12 -0800 | [diff] [blame] | 33 | |
Bob Lantz | a093082 | 2013-12-17 21:00:41 -0800 | [diff] [blame] | 34 | # This should be cleaned up to avoid interfering with mn |
Bob Lantz | c7c0540 | 2013-12-16 21:22:12 -0800 | [diff] [blame] | 35 | from shutil import copyfile |
Bob Lantz | 21b41f6 | 2013-12-18 18:16:38 -0800 | [diff] [blame] | 36 | from os import environ, path |
Bob Lantz | c7c0540 | 2013-12-16 21:22:12 -0800 | [diff] [blame] | 37 | from functools import partial |
Bob Lantz | a093082 | 2013-12-17 21:00:41 -0800 | [diff] [blame] | 38 | import time |
Bob Lantz | 7ead053 | 2013-12-17 20:41:20 -0800 | [diff] [blame] | 39 | from sys import argv |
Bob Lantz | 63bbe4c | 2014-02-06 19:29:55 -0800 | [diff] [blame] | 40 | from time import sleep |
Bob Lantz | c7c0540 | 2013-12-16 21:22:12 -0800 | [diff] [blame] | 41 | |
| 42 | class ONOS( Controller ): |
| 43 | "Custom controller class for ONOS" |
| 44 | |
| 45 | # Directories and configuration templates |
| 46 | home = environ[ 'HOME' ] |
| 47 | onosDir = home + "/ONOS" |
Yuta HIGUCHI | fb1905a | 2014-06-09 14:07:34 -0700 | [diff] [blame] | 48 | zookeeperDir = home + "/zookeeper-3.4.6" |
Bob Lantz | c7c0540 | 2013-12-16 21:22:12 -0800 | [diff] [blame] | 49 | dirBase = '/tmp' |
Bob Lantz | 63bbe4c | 2014-02-06 19:29:55 -0800 | [diff] [blame] | 50 | logDir = dirBase + '/onos-logs' |
Bob Lantz | 4ed6cff | 2013-12-17 21:43:36 -0800 | [diff] [blame] | 51 | logbackFile = dirBase + '/onos-%s.logback.xml' |
Bob Lantz | 7ead053 | 2013-12-17 20:41:20 -0800 | [diff] [blame] | 52 | |
Bob Lantz | 7ead053 | 2013-12-17 20:41:20 -0800 | [diff] [blame] | 53 | # Additions for reactive forwarding |
| 54 | reactiveModules = ( |
Jonathan Hart | 0961fe8 | 2014-04-03 09:56:25 -0700 | [diff] [blame] | 55 | 'net.onrc.onos.apps.proxyarp.ProxyArpManager', |
Jonathan Hart | 51f6f5b | 2014-04-03 10:32:10 -0700 | [diff] [blame] | 56 | 'net.onrc.onos.core.main.config.DefaultConfiguration', |
Jonathan Hart | 0961fe8 | 2014-04-03 09:56:25 -0700 | [diff] [blame] | 57 | 'net.onrc.onos.apps.forwarding.Forwarding' |
Bob Lantz | 7ead053 | 2013-12-17 20:41:20 -0800 | [diff] [blame] | 58 | ) |
| 59 | |
| 60 | # Module parameters |
Bob Lantz | c7c0540 | 2013-12-16 21:22:12 -0800 | [diff] [blame] | 61 | ofbase = 6633 |
Bob Lantz | 7ead053 | 2013-12-17 20:41:20 -0800 | [diff] [blame] | 62 | restbase = 8080 |
| 63 | jmxbase = 7189 |
Naoki Shiota | d8ea71a | 2014-04-23 17:04:51 -0700 | [diff] [blame] | 64 | hcbase = 5701 |
Bob Lantz | c7c0540 | 2013-12-16 21:22:12 -0800 | [diff] [blame] | 65 | |
Bob Lantz | c7c0540 | 2013-12-16 21:22:12 -0800 | [diff] [blame] | 66 | fc = 'net.floodlightcontroller.' |
Bob Lantz | c7c0540 | 2013-12-16 21:22:12 -0800 | [diff] [blame] | 67 | |
Bob Lantz | 21b41f6 | 2013-12-18 18:16:38 -0800 | [diff] [blame] | 68 | # Things that vary per ONOS id |
Bob Lantz | 7ead053 | 2013-12-17 20:41:20 -0800 | [diff] [blame] | 69 | perNodeConfigBase = { |
| 70 | fc + 'core.FloodlightProvider.openflowport': ofbase, |
| 71 | fc + 'restserver.RestApiServer.port': restbase, |
| 72 | fc + 'core.FloodlightProvider.controllerid': 0 |
| 73 | } |
| 74 | |
Bob Lantz | 7ead053 | 2013-12-17 20:41:20 -0800 | [diff] [blame] | 75 | proctag = 'mn-onos-id' |
Bob Lantz | c7c0540 | 2013-12-16 21:22:12 -0800 | [diff] [blame] | 76 | |
Bob Lantz | 63bbe4c | 2014-02-06 19:29:55 -0800 | [diff] [blame] | 77 | # List of scripts that we need/use |
Naoki Shiota | be433a1 | 2014-04-07 12:03:49 -0700 | [diff] [blame] | 78 | scripts = ( 'onos.sh', 'start-rest.sh' ) |
Bob Lantz | 63bbe4c | 2014-02-06 19:29:55 -0800 | [diff] [blame] | 79 | |
Bob Lantz | 7ead053 | 2013-12-17 20:41:20 -0800 | [diff] [blame] | 80 | def __init__( self, name, n=1, reactive=True, runAsRoot=False, **params): |
Bob Lantz | c7c0540 | 2013-12-16 21:22:12 -0800 | [diff] [blame] | 81 | """n: number of ONOS instances to run (1) |
Bob Lantz | 7ead053 | 2013-12-17 20:41:20 -0800 | [diff] [blame] | 82 | reactive: run in reactive mode (True) |
| 83 | runAsRoot: run ONOS as root (False)""" |
Bob Lantz | c7c0540 | 2013-12-16 21:22:12 -0800 | [diff] [blame] | 84 | self.check() |
Bob Lantz | c7c0540 | 2013-12-16 21:22:12 -0800 | [diff] [blame] | 85 | self.count = n |
Bob Lantz | 7ead053 | 2013-12-17 20:41:20 -0800 | [diff] [blame] | 86 | self.reactive = reactive |
| 87 | self.runAsRoot = runAsRoot |
Bob Lantz | c7c0540 | 2013-12-16 21:22:12 -0800 | [diff] [blame] | 88 | self.ids = range( 0, self.count ) |
| 89 | Controller.__init__( self, name, **params ) |
Bob Lantz | 63bbe4c | 2014-02-06 19:29:55 -0800 | [diff] [blame] | 90 | self.proxies = [] |
Bob Lantz | c7c0540 | 2013-12-16 21:22:12 -0800 | [diff] [blame] | 91 | # We don't need to run as root, and it can interfere |
| 92 | # with starting Zookeeper manually |
Bob Lantz | 7ead053 | 2013-12-17 20:41:20 -0800 | [diff] [blame] | 93 | self.user = None |
| 94 | if not self.runAsRoot: |
| 95 | try: |
| 96 | self.user = quietRun( 'who am i' ).split()[ 0 ] |
| 97 | self.sendCmd( 'su', self.user ) |
| 98 | self.waiting = False |
| 99 | except: |
| 100 | warn( '__init__: failed to drop privileges\n' ) |
Bob Lantz | 63bbe4c | 2014-02-06 19:29:55 -0800 | [diff] [blame] | 101 | self.cmd( 'mkdir -p', self.logDir ) |
Bob Lantz | c7c0540 | 2013-12-16 21:22:12 -0800 | [diff] [blame] | 102 | # Need to run commands from ONOS dir |
| 103 | self.cmd( 'cd', self.onosDir ) |
| 104 | self.cmd( 'export PATH=$PATH:%s' % self.onosDir ) |
Bob Lantz | c7c0540 | 2013-12-16 21:22:12 -0800 | [diff] [blame] | 105 | |
| 106 | def check( self ): |
Bob Lantz | 21b41f6 | 2013-12-18 18:16:38 -0800 | [diff] [blame] | 107 | "Set onosDir and check for ONOS prerequisites" |
Bob Lantz | c7c0540 | 2013-12-16 21:22:12 -0800 | [diff] [blame] | 108 | if not quietRun( 'which java' ): |
| 109 | raise Exception( 'java not found -' |
| 110 | ' make sure it is installed and in $PATH' ) |
Bob Lantz | 21b41f6 | 2013-12-18 18:16:38 -0800 | [diff] [blame] | 111 | if 'ONOS_HOME' in environ: |
| 112 | self.onosDir = environ[ 'ONOS_HOME' ] |
| 113 | else: |
| 114 | warn( '* $ONOS_HOME is not set - assuming %s\n' % self.onosDir ) |
Bob Lantz | 63bbe4c | 2014-02-06 19:29:55 -0800 | [diff] [blame] | 115 | for script in self.scripts: |
Bob Lantz | 21b41f6 | 2013-12-18 18:16:38 -0800 | [diff] [blame] | 116 | script = path.join( self.onosDir, script ) |
| 117 | if not path.exists( script ): |
| 118 | msg = '%s not found' % script |
| 119 | if 'ONOS_HOME' not in environ: |
| 120 | msg += ' (try setting $ONOS_HOME and/or sudo -E)' |
| 121 | raise Exception( msg ) |
Bob Lantz | 7ead053 | 2013-12-17 20:41:20 -0800 | [diff] [blame] | 122 | |
| 123 | def waitNetstat( self, pid ): |
| 124 | """Wait for pid to show up in netstat |
| 125 | We assume that once a process is listening on some |
| 126 | port, it is ready to go!""" |
| 127 | while True: |
Bob Lantz | 63bbe4c | 2014-02-06 19:29:55 -0800 | [diff] [blame] | 128 | output = self.cmd( 'sudo netstat -natup | grep %s/' % pid ) |
Bob Lantz | 7ead053 | 2013-12-17 20:41:20 -0800 | [diff] [blame] | 129 | if output: |
Bob Lantz | 63bbe4c | 2014-02-06 19:29:55 -0800 | [diff] [blame] | 130 | break |
Bob Lantz | 7ead053 | 2013-12-17 20:41:20 -0800 | [diff] [blame] | 131 | info( '.' ) |
Bob Lantz | a093082 | 2013-12-17 21:00:41 -0800 | [diff] [blame] | 132 | time.sleep( 1 ) |
Bob Lantz | 63bbe4c | 2014-02-06 19:29:55 -0800 | [diff] [blame] | 133 | info( '\n* Process %d is listening\n' % pid ) |
Bob Lantz | 7ead053 | 2013-12-17 20:41:20 -0800 | [diff] [blame] | 134 | |
Bob Lantz | 63bbe4c | 2014-02-06 19:29:55 -0800 | [diff] [blame] | 135 | def waitStart( self, procname, pattern, maxWait=10 ): |
| 136 | "Wait for proces to start up and be visible to pgrep" |
Bob Lantz | 21b41f6 | 2013-12-18 18:16:38 -0800 | [diff] [blame] | 137 | # Check script exit code |
| 138 | exitCode = int( self.cmd( 'echo $?' ) ) |
| 139 | if exitCode != 0: |
| 140 | raise Exception( '%s startup failed with code %d' % |
| 141 | ( procname, exitCode ) ) |
Bob Lantz | 7ead053 | 2013-12-17 20:41:20 -0800 | [diff] [blame] | 142 | info( '* Waiting for %s startup' % procname ) |
Bob Lantz | 63bbe4c | 2014-02-06 19:29:55 -0800 | [diff] [blame] | 143 | while True: |
| 144 | result = self.cmd( 'pgrep -f %s' % pattern ) |
| 145 | if result: |
| 146 | break |
| 147 | info( '.' ) |
| 148 | sleep( 1 ) |
| 149 | pid = int( result.split()[ 0 ] ) |
| 150 | return pid |
Bob Lantz | 7ead053 | 2013-12-17 20:41:20 -0800 | [diff] [blame] | 151 | |
Bob Lantz | 63bbe4c | 2014-02-06 19:29:55 -0800 | [diff] [blame] | 152 | def startRamcloud( self, cpu=.6 ): |
| 153 | """Start Ramcloud |
| 154 | cpu: CPU usage limit (in seconds/s)""" |
Bob Lantz | 63bbe4c | 2014-02-06 19:29:55 -0800 | [diff] [blame] | 155 | # Create a cgroup so Ramcloud doesn't eat all of our CPU |
| 156 | ramcloud = CPULimitedHost( 'ramcloud', inNamespace=False, period_us=5000 ) |
| 157 | ramcloud.setCPUFrac( cpu / numCores() ) |
Naoki Shiota | ac74d91 | 2014-04-25 12:01:55 -0700 | [diff] [blame] | 158 | info( '\n' ) |
Bob Lantz | 63bbe4c | 2014-02-06 19:29:55 -0800 | [diff] [blame] | 159 | ramcloud.cmd( 'export PATH=%s:$PATH' % self.onosDir ) |
| 160 | ramcloud.cmd( 'export ONOS_LOGDIR=%s' % self.logDir ) |
Naoki Shiota | be433a1 | 2014-04-07 12:03:49 -0700 | [diff] [blame] | 161 | for daemon in 'coord', 'server': |
| 162 | ramcloud.cmd( 'onos.sh rc-%s start' % daemon ) |
Bob Lantz | 63bbe4c | 2014-02-06 19:29:55 -0800 | [diff] [blame] | 163 | pid = self.waitStart( 'Ramcloud %s' % daemon, 'obj.master/' + daemon ) |
| 164 | self.waitNetstat( pid ) |
Naoki Shiota | be433a1 | 2014-04-07 12:03:49 -0700 | [diff] [blame] | 165 | status = self.cmd( 'onos.sh rc-%s.sh status' % daemon ) |
Bob Lantz | 63bbe4c | 2014-02-06 19:29:55 -0800 | [diff] [blame] | 166 | if 'running' not in status: |
| 167 | raise Exception( 'Ramcloud %s startup failed: ' % daemon + status ) |
| 168 | self.ramcloud = ramcloud |
Bob Lantz | c7c0540 | 2013-12-16 21:22:12 -0800 | [diff] [blame] | 169 | |
Bob Lantz | 63bbe4c | 2014-02-06 19:29:55 -0800 | [diff] [blame] | 170 | def stopRamcloud( self ): |
| 171 | "Stop Ramcloud" |
Naoki Shiota | be433a1 | 2014-04-07 12:03:49 -0700 | [diff] [blame] | 172 | for daemon in 'coord', 'server': |
Naoki Shiota | ac74d91 | 2014-04-25 12:01:55 -0700 | [diff] [blame] | 173 | self.ramcloud.cmd( 'onos.sh rc-%s stop' % daemon ) |
Bob Lantz | 63bbe4c | 2014-02-06 19:29:55 -0800 | [diff] [blame] | 174 | self.ramcloud.terminate() |
Bob Lantz | c7c0540 | 2013-12-16 21:22:12 -0800 | [diff] [blame] | 175 | |
| 176 | def startZookeeper( self, initcfg=True ): |
| 177 | "Start Zookeeper" |
Naoki Shiota | be433a1 | 2014-04-07 12:03:49 -0700 | [diff] [blame] | 178 | self.cmd( 'onos.sh zk start' ) |
Bob Lantz | 63bbe4c | 2014-02-06 19:29:55 -0800 | [diff] [blame] | 179 | pid = self.waitStart( 'Zookeeper', 'zookeeper' ) |
| 180 | self.waitNetstat( pid ) |
Naoki Shiota | be433a1 | 2014-04-07 12:03:49 -0700 | [diff] [blame] | 181 | status = self.cmd( 'onos.sh zk status' ) |
Bob Lantz | c7c0540 | 2013-12-16 21:22:12 -0800 | [diff] [blame] | 182 | if 'Error' in status: |
| 183 | raise Exception( 'Zookeeper startup failed: ' + status ) |
| 184 | |
| 185 | def stopZookeeper( self ): |
| 186 | "Stop Zookeeper" |
Naoki Shiota | be433a1 | 2014-04-07 12:03:49 -0700 | [diff] [blame] | 187 | self.cmd( 'onos.sh zk stop' ) |
Bob Lantz | c7c0540 | 2013-12-16 21:22:12 -0800 | [diff] [blame] | 188 | |
Naoki Shiota | d8ea71a | 2014-04-23 17:04:51 -0700 | [diff] [blame] | 189 | def getPropsFilename( self, id, path ): |
| 190 | return path + '/onos-%s.properties' % id |
| 191 | |
Bob Lantz | 7ead053 | 2013-12-17 20:41:20 -0800 | [diff] [blame] | 192 | def genProperties( self, id, path='/tmp' ): |
Bob Lantz | 42543db | 2014-02-24 16:07:34 -0800 | [diff] [blame] | 193 | "Generate ONOS properties file and return its full pathname" |
| 194 | defaultProps = self.onosDir + '/conf/onos.properties' |
Naoki Shiota | d8ea71a | 2014-04-23 17:04:51 -0700 | [diff] [blame] | 195 | propsFile = self.getPropsFilename( id, path ) |
Bob Lantz | 42543db | 2014-02-24 16:07:34 -0800 | [diff] [blame] | 196 | with open( propsFile, 'w' ) as f: |
| 197 | with open( defaultProps ) as d: |
| 198 | for line in d.readlines(): |
| 199 | prop = line.split( ' ' )[ 0 ] |
| 200 | val = self.perNodeConfigBase.get( prop, None ) |
| 201 | if val: |
| 202 | # Write updated property |
| 203 | f.write( '%s = %s\n' % ( prop, val + id) ) |
| 204 | else: |
| 205 | # Write original property |
| 206 | f.write( line ) |
| 207 | if prop == 'floodlight.modules' and ',\\' in line: |
| 208 | if self.reactive: |
| 209 | # Insert reactive modules into list |
| 210 | for module in self.reactiveModules: |
| 211 | f.write( '%s,\\\n' % module ) |
| 212 | return propsFile |
Bob Lantz | 7ead053 | 2013-12-17 20:41:20 -0800 | [diff] [blame] | 213 | |
Naoki Shiota | d8ea71a | 2014-04-23 17:04:51 -0700 | [diff] [blame] | 214 | def getConfsFilename( self, id, path ): |
| 215 | return path + '/onos-%s.conf' % id |
| 216 | |
| 217 | def genConfig( self, id, path='/tmp' ): |
| 218 | "Generate ONOS node config file and return its full pathname" |
| 219 | confsFile = self.getConfsFilename( id, path ) |
| 220 | with open( confsFile, 'w' ) as f: |
| 221 | f.write( 'host.ip = 127.0.0.1\n' ) |
| 222 | f.write( 'host.backend = ramcloud\n' ) |
| 223 | f.write( 'hazelcast.host.port = %s\n' % ( self.hcbase + 10 * id ) ) |
| 224 | return confsFile |
| 225 | |
| 226 | def setVarsGlobal( self, path='/tmp'): |
| 227 | logdir = self.logDir |
| 228 | self.cmd( 'export ONOS_LOGDIR=%s' % logdir ) |
| 229 | self.cmd( 'export ZK_LOG_DIR=%s' % logdir ) |
| 230 | |
| 231 | def setVarsLocal( self, id, path='/tmp' ): |
Bob Lantz | 7ead053 | 2013-12-17 20:41:20 -0800 | [diff] [blame] | 232 | """Set and return environment vars |
| 233 | id: ONOS instance number |
| 234 | propsFile: properties file name""" |
Bob Lantz | c7c0540 | 2013-12-16 21:22:12 -0800 | [diff] [blame] | 235 | logback = self.logbackFile % id |
| 236 | jmxport = self.jmxbase + id |
Bob Lantz | 63bbe4c | 2014-02-06 19:29:55 -0800 | [diff] [blame] | 237 | logdir = self.logDir |
Bob Lantz | 63bbe4c | 2014-02-06 19:29:55 -0800 | [diff] [blame] | 238 | self.cmd( 'export ONOS_LOGBASE=onos-%d.`hostname`' % id) |
Bob Lantz | c7c0540 | 2013-12-16 21:22:12 -0800 | [diff] [blame] | 239 | self.cmd( 'export ONOS_LOGBACK="%s"' % logback ) |
| 240 | self.cmd( 'export JMX_PORT=%s' % jmxport ) |
Bob Lantz | 7ead053 | 2013-12-17 20:41:20 -0800 | [diff] [blame] | 241 | self.cmd( 'export JVM_OPTS="-D%s=%s"' % ( |
| 242 | self.proctag, id ) ) |
Naoki Shiota | d8ea71a | 2014-04-23 17:04:51 -0700 | [diff] [blame] | 243 | propsFile = self.getPropsFilename( id, path ) |
Bob Lantz | 7ead053 | 2013-12-17 20:41:20 -0800 | [diff] [blame] | 244 | self.cmd( 'export ONOS_PROPS="%s"' % propsFile ) |
Naoki Shiota | d8ea71a | 2014-04-23 17:04:51 -0700 | [diff] [blame] | 245 | confsFile = self.getConfsFilename( id, path ) |
| 246 | self.cmd( 'export ONOS_CONF="%s"' % confsFile ) |
| 247 | self.cmd( 'export HC_CONF="%s/hazelcast.%s.conf"' % ( path, id ) ) |
Bob Lantz | c7c0540 | 2013-12-16 21:22:12 -0800 | [diff] [blame] | 248 | |
Naoki Shiota | d8ea71a | 2014-04-23 17:04:51 -0700 | [diff] [blame] | 249 | def setupONOS (self, id): |
| 250 | propsFile = self.genProperties( id ) |
| 251 | confFile = self.genConfig( id ) |
| 252 | self.setVarsLocal( id ) |
| 253 | self.cmd( 'onos.sh setup -f' ) |
| 254 | |
Bob Lantz | c7c0540 | 2013-12-16 21:22:12 -0800 | [diff] [blame] | 255 | def startONOS( self, id ): |
| 256 | """Start ONOS |
Bob Lantz | 7ead053 | 2013-12-17 20:41:20 -0800 | [diff] [blame] | 257 | id: new instance number""" |
Bob Lantz | a093082 | 2013-12-17 21:00:41 -0800 | [diff] [blame] | 258 | start = time.time() |
Naoki Shiota | d8ea71a | 2014-04-23 17:04:51 -0700 | [diff] [blame] | 259 | self.setVarsLocal( id ) |
Bob Lantz | 7ead053 | 2013-12-17 20:41:20 -0800 | [diff] [blame] | 260 | self.stopONOS( id ) |
Naoki Shiota | be433a1 | 2014-04-07 12:03:49 -0700 | [diff] [blame] | 261 | self.cmd( 'onos.sh core startnokill' ) |
Naoki Shiota | ac74d91 | 2014-04-25 12:01:55 -0700 | [diff] [blame] | 262 | # onos.sh waits for ONOS startup |
Bob Lantz | a093082 | 2013-12-17 21:00:41 -0800 | [diff] [blame] | 263 | elapsed = time.time() - start |
Bob Lantz | 7ead053 | 2013-12-17 20:41:20 -0800 | [diff] [blame] | 264 | info( '* ONOS %s started in %.2f seconds\n' % ( id, elapsed ) ) |
Bob Lantz | c7c0540 | 2013-12-16 21:22:12 -0800 | [diff] [blame] | 265 | |
| 266 | def stopONOS( self, id ): |
| 267 | """Shut down ONOS |
| 268 | id: identifier for instance""" |
| 269 | pid = self.cmd( "jps -v | grep %s=%s | awk '{print $1}'" % |
| 270 | ( self.proctag, id ) ).strip() |
| 271 | if pid: |
Bob Lantz | 63bbe4c | 2014-02-06 19:29:55 -0800 | [diff] [blame] | 272 | self.cmd( 'kill', pid ) |
Bob Lantz | c7c0540 | 2013-12-16 21:22:12 -0800 | [diff] [blame] | 273 | |
| 274 | def start( self, *args ): |
| 275 | "Start ONOS instances" |
Naoki Shiota | d8ea71a | 2014-04-23 17:04:51 -0700 | [diff] [blame] | 276 | self.setVarsGlobal() |
| 277 | # TODO: use onos-cluster.sh to setup/start/stop ONOS cluster |
| 278 | for id in self.ids: |
| 279 | info( '* Setting up ONOS %s\n' % id ) |
| 280 | self.setupONOS( id ) |
Bob Lantz | c7c0540 | 2013-12-16 21:22:12 -0800 | [diff] [blame] | 281 | info( '* Starting Zookeeper\n' ) |
| 282 | self.startZookeeper() |
Bob Lantz | 63bbe4c | 2014-02-06 19:29:55 -0800 | [diff] [blame] | 283 | info( '* Starting Ramcloud\n' ) |
| 284 | self.startRamcloud() |
Bob Lantz | c7c0540 | 2013-12-16 21:22:12 -0800 | [diff] [blame] | 285 | for id in self.ids: |
| 286 | info( '* Starting ONOS %s\n' % id ) |
| 287 | self.startONOS( id ) |
Bob Lantz | 63bbe4c | 2014-02-06 19:29:55 -0800 | [diff] [blame] | 288 | self.cmd( 'start-rest.sh start' ) |
| 289 | # Initialize proxies for clist() |
| 290 | self.proxies = [ RemoteController( 'onos-%d' % id, port=(self.ofbase + id ) ) |
| 291 | for id in range( 0, self.count ) ] |
Bob Lantz | c7c0540 | 2013-12-16 21:22:12 -0800 | [diff] [blame] | 292 | |
| 293 | def stop( self, *args ): |
| 294 | "Stop ONOS instances" |
Bob Lantz | 63bbe4c | 2014-02-06 19:29:55 -0800 | [diff] [blame] | 295 | self.cmd( 'start-rest.sh stop' ) |
Bob Lantz | c7c0540 | 2013-12-16 21:22:12 -0800 | [diff] [blame] | 296 | for id in self.ids: |
| 297 | info( '* Stopping ONOS %s\n' % id ) |
| 298 | self.stopONOS( id ) |
Bob Lantz | 7ead053 | 2013-12-17 20:41:20 -0800 | [diff] [blame] | 299 | info( '* Stopping Zookeeper\n' ) |
Bob Lantz | c7c0540 | 2013-12-16 21:22:12 -0800 | [diff] [blame] | 300 | self.stopZookeeper() |
Bob Lantz | 63bbe4c | 2014-02-06 19:29:55 -0800 | [diff] [blame] | 301 | info( '* Stopping Ramcloud\n' ) |
| 302 | self.stopRamcloud() |
| 303 | for p in self.proxies: |
| 304 | p.stop() |
| 305 | p.proxies = [] |
Bob Lantz | c7c0540 | 2013-12-16 21:22:12 -0800 | [diff] [blame] | 306 | |
| 307 | def clist( self ): |
Bob Lantz | 63bbe4c | 2014-02-06 19:29:55 -0800 | [diff] [blame] | 308 | "Return list of Controller proxies for this ONOS cluster" |
| 309 | return self.proxies |
Bob Lantz | c7c0540 | 2013-12-16 21:22:12 -0800 | [diff] [blame] | 310 | |
| 311 | |
| 312 | class OVSSwitchONOS( OVSSwitch ): |
| 313 | "OVS switch which connects to multiple controllers" |
| 314 | def start( self, controllers ): |
Bob Lantz | c7c0540 | 2013-12-16 21:22:12 -0800 | [diff] [blame] | 315 | assert len( controllers ) == 1 |
| 316 | c0 = controllers[ 0 ] |
| 317 | assert type( c0 ) == ONOS |
Bob Lantz | 63bbe4c | 2014-02-06 19:29:55 -0800 | [diff] [blame] | 318 | controllers = c0.clist() |
| 319 | OVSSwitch.start( self, controllers ) |
Bob Lantz | c7c0540 | 2013-12-16 21:22:12 -0800 | [diff] [blame] | 320 | |
| 321 | |
Bob Lantz | 7ead053 | 2013-12-17 20:41:20 -0800 | [diff] [blame] | 322 | def waitConnected( switches ): |
| 323 | "Wait until all switches connect to controllers" |
Bob Lantz | a093082 | 2013-12-17 21:00:41 -0800 | [diff] [blame] | 324 | start = time.time() |
Bob Lantz | 7ead053 | 2013-12-17 20:41:20 -0800 | [diff] [blame] | 325 | info( '* Waiting for switches to connect...\n' ) |
| 326 | for s in switches: |
| 327 | info( s ) |
| 328 | while not s.connected(): |
| 329 | info( '.' ) |
Bob Lantz | a093082 | 2013-12-17 21:00:41 -0800 | [diff] [blame] | 330 | time.sleep( 1 ) |
Bob Lantz | 7ead053 | 2013-12-17 20:41:20 -0800 | [diff] [blame] | 331 | info( ' ' ) |
Bob Lantz | a093082 | 2013-12-17 21:00:41 -0800 | [diff] [blame] | 332 | elapsed = time.time() - start |
Bob Lantz | 7ead053 | 2013-12-17 20:41:20 -0800 | [diff] [blame] | 333 | info( '\n* Connected in %.2f seconds\n' % elapsed ) |
| 334 | |
| 335 | |
Bob Lantz | c7c0540 | 2013-12-16 21:22:12 -0800 | [diff] [blame] | 336 | controllers = { 'onos': ONOS } |
| 337 | switches = { 'ovso': OVSSwitchONOS } |
| 338 | |
| 339 | |
| 340 | if __name__ == '__main__': |
Bob Lantz | 7ead053 | 2013-12-17 20:41:20 -0800 | [diff] [blame] | 341 | # Simple test for ONOS() controller class |
Bob Lantz | c7c0540 | 2013-12-16 21:22:12 -0800 | [diff] [blame] | 342 | setLogLevel( 'info' ) |
Bob Lantz | 7ead053 | 2013-12-17 20:41:20 -0800 | [diff] [blame] | 343 | size = 2 if len( argv ) != 2 else int( argv[ 1 ] ) |
| 344 | net = Mininet( topo=LinearTopo( size ), |
Bob Lantz | c7c0540 | 2013-12-16 21:22:12 -0800 | [diff] [blame] | 345 | controller=partial( ONOS, n=2 ), |
| 346 | switch=OVSSwitchONOS ) |
| 347 | net.start() |
Bob Lantz | 7ead053 | 2013-12-17 20:41:20 -0800 | [diff] [blame] | 348 | waitConnected( net.switches ) |
Bob Lantz | c7c0540 | 2013-12-16 21:22:12 -0800 | [diff] [blame] | 349 | CLI( net ) |
| 350 | net.stop() |