Bob Lantz
Committed by Gerrit Code Review

Remove obsolete/confusing onos.py

Change-Id: I21e6dee9164352140cfa50c48904ec8139854c84
1 -#!/usr/bin/env python
2 -
3 -# TODO add onos-app-fwd to features
4 -# TODO check if service is running... i think this might already be done by mn
5 -
6 -from mininet.node import Controller, OVSSwitch, CPULimitedHost, RemoteController
7 -from mininet.net import Mininet
8 -from mininet.cli import CLI
9 -from mininet.topo import LinearTopo, Topo
10 -from mininet.log import setLogLevel, info, warn
11 -from mininet.util import quietRun, numCores
12 -
13 -from shutil import copyfile
14 -from os import environ, path
15 -from functools import partial
16 -import time
17 -from sys import argv
18 -from time import sleep
19 -from sets import Set
20 -
21 -class ONOS( Controller ):
22 - "TODO"
23 -
24 - onosDir = '/opt/onos/'
25 -
26 - def __init__( self, name, onosDir=onosDir,
27 - reactive=True, features=[ 'onos-app-tvue' ],
28 - **kwargs ):
29 - '''TODO'''
30 -
31 - Controller.__init__( self, name, **kwargs )
32 - # the following have been done for us:
33 - #self.ip = ip ('127.0.0.1')
34 - #self.port = port (6653)
35 - #self.protocol = protocol ('tcp')
36 - #self.checkListening()
37 -
38 - self.onosDir = onosDir
39 - self.karafDir = onosDir + 'apache-karaf-3.0.5/'
40 - self.instanceDir = self.karafDir
41 -
42 - # add default modules
43 - # TODO: consider an ordered set
44 - self.features = Set([ 'webconsole',
45 - 'onos-rest',
46 - 'onos-api',
47 - 'onos-cli',
48 - 'onos-openflow' ])
49 - self.features.update( features )
50 - # add reactive forwarding modules
51 - if reactive:
52 - self.features.update( ['onos-app-fwd',
53 - 'onos-app-proxyarp',
54 - 'onos-app-mobility' ] )
55 - # add the distributed core if we are in a namespace with no trivial core
56 - if self.inNamespace and 'onos-core-trivial' not in self.features:
57 - self.features.add( 'onos-core' )
58 - # if there is no core, add the trivial one
59 - if 'onos-core' not in self.features:
60 - self.features.add( 'onos-core-trivial' )
61 - print self.features
62 -
63 - def start( self ):
64 - if self.inNamespace:
65 - instanceOpts = ( '-furl mvn:org.onosproject/onos-features/1.7.0-SNAPSHOT/xml/features '
66 - '-s 8101' )
67 - if self.ip is not None:
68 - instanceOpts += (' -a %s' % self.IP() )
69 - self.userCmd( self.karafDir + 'bin/instance create %s %s' % ( instanceOpts, self.name ) )
70 - self.instanceDir = self.karafDir + 'instances/%s/' % self.name
71 - else:
72 - # we are running in the root namespace, so let's use the root instance
73 - # clean up the data directory
74 - #self.userCmd( 'rm -rf '+ self.karafDir + 'data/' )
75 - pass
76 -
77 - self.userCmd( 'rm -rf '+ self.instanceDir + 'data/' )
78 -
79 - # Update etc/org.apache.karaf.features.cfg
80 - self.updateFeatures()
81 -
82 - # TODO 2. Update etc/hazelcast.xml : interface lines
83 - #cp etc/hazelcast.xml instances/c1/etc/
84 - self.updateHazelcast()
85 -
86 - # TODO 3. Update etc/system.properties : onos.ip
87 - # TODO 4. Update config/cluster.json : with all nodes
88 -
89 - # start onos
90 - self.userCmd( '%sbin/instance start -d %s' % ( self.karafDir, self.name ) )
91 - #TODO we should wait for startup...
92 -
93 - def stop( self ):
94 - self.userCmd( self.instanceDir + 'bin/stop' )
95 - #if self.inNamespace:
96 - # self.userCmd( self.karafDir + 'bin/instance destroy %s' % self.name )
97 - self.terminate()
98 -
99 - def updateHazelcast( self ):
100 - hz = '192.168.123.*'
101 - if self.ip is not None:
102 - hz = '.'.join(self.ip.split('.')[:-1]) + '.*'
103 -
104 - readfile = self.karafDir + 'etc/hazelcast.xml'
105 - writefile = self.instanceDir + 'etc/hazelcast.xml'
106 - with open( readfile, 'r' ) as r:
107 - with open( writefile, 'w' ) as w:
108 - for line in r.readlines():
109 - if '<interface>' in line:
110 - line = '<interface>' + hz + '</interface>\n'
111 - w.write( line )
112 -
113 - def updateFeatures( self ):
114 - filename = self.instanceDir + 'etc/org.apache.karaf.features.cfg'
115 - with open( filename, 'r+' ) as f:
116 - lines = f.readlines()
117 - f.seek(0)
118 - f.truncate()
119 - for line in lines:
120 - #print '?', line,
121 - if 'featuresBoot=' in line:
122 - # parse the features from the line
123 - features = line.rstrip().split('=')[1].split(',')
124 - # add the features to our features set
125 - self.features.update( features )
126 - # generate the new features line
127 - line = 'featuresBoot=' + ','.join( self.features ) + '\n'
128 - #print '!', line,
129 - f.write( line )
130 -
131 -
132 - @classmethod
133 - def isAvailable( self ):
134 - return quietRun( 'ls %s' % self.onosDir )
135 -
136 - def userCmd( self, cmd ):
137 - # switch to the non-root user because karaf gets upset otherwise
138 - # because the .m2repo is not stored with root
139 - cmd = 'sudo -u %s %s' % ( self.findUser(), cmd )
140 - return self.cmd( cmd )
141 -
142 - @staticmethod
143 - def findUser():
144 - "Try to return logged-in (usually non-root) user"
145 - try:
146 - # If we're running sudo
147 - return os.environ[ 'SUDO_USER' ]
148 - except:
149 - try:
150 - # Logged-in user (if we have a tty)
151 - return quietRun( 'who am i' ).split()[ 0 ]
152 - except:
153 - # Give up and return effective user
154 - return quietRun( 'whoami' )
155 -
156 -
157 -class ControlNetwork( Topo ):
158 - "Control Network Topology"
159 - def __init__( self, n, dataController=ONOS, **kwargs ):
160 - """n: number of data network controller nodes
161 - dataController: class for data network controllers"""
162 - Topo.__init__( self, **kwargs )
163 - # Connect everything to a single switch
164 - cs0 = self.addSwitch( 'cs0' )
165 - # Add hosts which will serve as data network controllers
166 - for i in range( 1, n+1 ):
167 - c = self.addHost( 'c%s' % i, cls=dataController,
168 - inNamespace=True )
169 - self.addLink( c, cs0 )
170 - # Connect switch to root namespace so that data network
171 - # switches will be able to talk to us
172 - root = self.addHost( 'root', inNamespace=False )
173 - self.addLink( root, cs0 )
174 -
175 -class ONOSCluster( Controller ):
176 - # TODO
177 - n = 3
178 -
179 - def start( self ):
180 - ctopo = ControlNetwork( n=self.n, dataController=ONOS )
181 - self.cnet = Mininet( topo=ctopo, ipBase='192.168.123.0/24', controller=None )
182 - self.cnet.addController( 'cc0', controller=Controller )
183 - self.cnet.start()
184 -
185 - self.ctrls = []
186 - for host in self.cnet.hosts:
187 - if isinstance( host, Controller ):
188 - self.ctrls.append( host )
189 - host.start()
190 -
191 - def stop( self ):
192 - for host in self.cnet.hosts:
193 - if isinstance( host, Controller ):
194 - host.stop()
195 - self.cnet.stop()
196 -
197 - def clist( self ):
198 - "Return list of Controller proxies for this ONOS cluster"
199 - print 'controllers:', self.ctrls
200 - return self.ctrls
201 -
202 -class OVSSwitchONOS( OVSSwitch ):
203 - "OVS switch which connects to multiple controllers"
204 - def start( self, controllers ):
205 - assert len( controllers ) == 1
206 - c0 = controllers[ 0 ]
207 - assert type( c0 ) == ONOSCluster
208 - controllers = c0.clist()
209 - OVSSwitch.start( self, controllers )
210 -
211 -controllers = { 'onos': ONOS }
212 -switches = { 'ovso': OVSSwitchONOS }
213 -
214 -if __name__ == '__main__':
215 - # Simple test for ONOS() controller class
216 - setLogLevel( 'info' ) #TODO info
217 - size = 2 if len( argv ) != 2 else int( argv[ 1 ] )
218 - net = Mininet( topo=LinearTopo( size ),
219 - #controller=ONOS,
220 - controller=partial( ONOSCluster, n=3 ), #TODO
221 - switch=OVSSwitchONOS )
222 - net.start()
223 - #waitConnected( net.switches )
224 - CLI( net )
225 - net.stop()