Brian O'Connor

updating onos.py

Change-Id: I6ec6bf1205d0d3175495a7ecef4c2a4156d2e25a
...@@ -16,58 +16,95 @@ from functools import partial ...@@ -16,58 +16,95 @@ from functools import partial
16 import time 16 import time
17 from sys import argv 17 from sys import argv
18 from time import sleep 18 from time import sleep
19 +from sets import Set
19 20
20 class ONOS( Controller ): 21 class ONOS( Controller ):
21 - #def __init__( self, name, command='/opt/onos/bin/onos-service', **kwargs ): 22 + "TODO"
22 - # Controller.__init__( self, name, command=command, inNamespace=True, **kwargs ) 23 +
23 - #def __init__( self, name, inNamespace=False, command='controller', 24 + onosDir = '/opt/onos/'
24 - # cargs='-v ptcp:%d', cdir=None, ip="127.0.0.1", 25 +
25 - # port=6633, protocol='tcp', **params ): 26 + def __init__( self, name, onosDir=onosDir,
26 - #self.command = command 27 + reactive=True, features=[ 'onos-app-tvue' ],
27 - #self.cargs = cargs 28 + **kwargs ):
28 - #self.cdir = cdir 29 + '''TODO'''
29 - #self.ip = ip 30 +
30 - #self.port = port 31 + Controller.__init__( self, name, **kwargs )
31 - #self.protocol = protocol 32 + # the following have been done for us:
32 - #Node.__init__( self, name, inNamespace=inNamespace, 33 + #self.ip = ip ('127.0.0.1')
33 - # ip=ip, **params ) 34 + #self.port = port (6633)
34 - #self.checkListening() 35 + #self.protocol = protocol ('tcp')
35 - 36 + #self.checkListening()
36 - ONOS_DIR = '/opt/onos/' 37 +
37 - KARAF_DIR = ONOS_DIR + 'apache-karaf-3.0.1/' 38 + self.onosDir = onosDir
38 - reactive = True 39 + self.karafDir = onosDir + 'apache-karaf-3.0.1/'
40 + self.instanceDir = self.karafDir
41 +
42 + # add default modules
43 + # TODO: consider an ordered set
44 + self.features = Set([ 'webconsole',
45 + 'onos-api',
46 + 'onos-cli',
47 + 'onos-openflow' ])
48 + self.features.update( features )
49 + # add reactive forwarding modules
50 + if reactive:
51 + self.features.update( ['onos-app-fwd',
52 + 'onos-app-proxyarp',
53 + 'onos-app-mobility' ] )
54 + # add the distributed core if we are in a namespace with no trivial core
55 + if self.inNamespace and 'onos-core-trivial' not in self.features:
56 + self.features.add( 'onos-core' )
57 + # if there is no core, add the trivial one
58 + if 'onos-core' not in self.features:
59 + self.features.add( 'onos-core-trivial' )
60 + print self.features
39 61
40 def start( self ): 62 def start( self ):
41 - # switch to the non-root user because karaf gets upset otherwise
42 - # TODO we should look into why....
43 - self.sendCmd( 'sudo su - %s' % self.findUser() )
44 - self.waiting = False
45 -
46 if self.inNamespace: 63 if self.inNamespace:
47 - self.cmd( self.KARAF_DIR + 'bin/instance create %s' % self.name ) 64 + instanceOpts = ( '-furl mvn:org.onlab.onos/onos-features/1.0.0-SNAPSHOT/xml/features '
48 - src = self.KARAF_DIR + 'etc/org.apache.karaf.features.cfg' 65 + '-s 8101' )
49 - dst = self.KARAF_DIR + 'instances/%s/etc/org.apache.karaf.features.cfg' % self.name 66 + self.userCmd( self.karafDir + 'bin/instance create %s %s' % ( instanceOpts, self.name ) )
50 - self.cmd( 'cp %s %s' % (src, dst) ) 67 + self.instanceDir = self.karafDir + 'instances/%s/' % self.name
51 - self.updateProperties( dst )
52 - self.cmd( self.KARAF_DIR + 'bin/instance start %s' % self.name )
53 else: 68 else:
54 # we are running in the root namespace, so let's use the root instance 69 # we are running in the root namespace, so let's use the root instance
55 - self.cmd( 'rm -rf '+ self.KARAF_DIR + 'data/' ) 70 + # clean up the data directory
56 - filename = self.KARAF_DIR + 'etc/org.apache.karaf.features.cfg' 71 + #self.userCmd( 'rm -rf '+ self.karafDir + 'data/' )
57 - self.updateProperties( filename ) 72 + pass
58 - self.cmd( self.KARAF_DIR + 'bin/start' ) 73 +
74 + self.userCmd( 'rm -rf '+ self.instanceDir + 'data/' )
75 +
76 + # Update etc/org.apache.karaf.features.cfg
77 + self.updateFeatures()
59 78
79 + # TODO 2. Update etc/hazelcast.xml : interface lines
80 + #cp etc/hazelcast.xml instances/c1/etc/
81 + self.updateHazelcast()
82 +
83 + # TODO 3. Update etc/system.properties : onos.ip
84 + # TODO 4. Update config/cluster.json : with all nodes
85 +
86 + # start onos
87 + self.userCmd( self.instanceDir + 'bin/start' )
60 #TODO we should wait for startup... 88 #TODO we should wait for startup...
61 89
62 def stop( self ): 90 def stop( self ):
63 - if self.inNamespace: 91 + self.userCmd( self.instanceDir + 'bin/stop' )
64 - self.cmd( '/opt/onos/apache-karaf-3.0.1/bin/instance stop %s' % self.name ) 92 + #if self.inNamespace:
65 - self.cmd( '/opt/onos/apache-karaf-3.0.1/bin/instance destroy %s' % self.name ) 93 + # self.userCmd( self.karafDir + 'bin/instance destroy %s' % self.name )
66 - else:
67 - self.cmd( self.ONOS_DIR + 'apache-karaf-3.0.1/bin/stop' )
68 self.terminate() 94 self.terminate()
69 95
70 - def updateProperties( self, filename ): 96 + def updateHazelcast( self ):
97 + readfile = self.karafDir + 'etc/hazelcast.xml'
98 + writefile = self.instanceDir + 'etc/hazelcast.xml'
99 + with open( readfile, 'r' ) as r:
100 + with open( writefile, 'w' ) as w:
101 + for line in r.readlines():
102 + if '<interface>' in line:
103 + line = '<interface>' + '192.168.123.*' + '</interface>\n'
104 + w.write( line )
105 +
106 + def updateFeatures( self ):
107 + filename = self.instanceDir + 'etc/org.apache.karaf.features.cfg'
71 with open( filename, 'r+' ) as f: 108 with open( filename, 'r+' ) as f:
72 lines = f.readlines() 109 lines = f.readlines()
73 f.seek(0) 110 f.seek(0)
...@@ -75,17 +112,25 @@ class ONOS( Controller ): ...@@ -75,17 +112,25 @@ class ONOS( Controller ):
75 for line in lines: 112 for line in lines:
76 #print '?', line, 113 #print '?', line,
77 if 'featuresBoot=' in line: 114 if 'featuresBoot=' in line:
78 - line = line.rstrip() 115 + # parse the features from the line
79 - #print ord(line[-1]), ord(line[-2]), ord(line[-3]) 116 + features = line.rstrip().split('=')[1].split(',')
80 - if self.reactive: 117 + # add the features to our features set
81 - line += ',onos-app-fwd' 118 + self.features.update( features )
82 - line += '\n' 119 + # generate the new features line
120 + line = 'featuresBoot=' + ','.join( self.features ) + '\n'
83 #print '!', line, 121 #print '!', line,
84 f.write( line ) 122 f.write( line )
85 123
124 +
86 @classmethod 125 @classmethod
87 def isAvailable( self ): 126 def isAvailable( self ):
88 - return quietRun( 'ls /opt/onos' ) 127 + return quietRun( 'ls %s' % self.onosDir )
128 +
129 + def userCmd( self, cmd ):
130 + # switch to the non-root user because karaf gets upset otherwise
131 + # because the .m2repo is not stored with root
132 + cmd = 'sudo -u %s %s' % ( self.findUser(), cmd )
133 + return self.cmd( cmd )
89 134
90 @staticmethod 135 @staticmethod
91 def findUser(): 136 def findUser():
...@@ -111,7 +156,7 @@ class ControlNetwork( Topo ): ...@@ -111,7 +156,7 @@ class ControlNetwork( Topo ):
111 # Connect everything to a single switch 156 # Connect everything to a single switch
112 cs0 = self.addSwitch( 'cs0' ) 157 cs0 = self.addSwitch( 'cs0' )
113 # Add hosts which will serve as data network controllers 158 # Add hosts which will serve as data network controllers
114 - for i in range( 0, n ): 159 + for i in range( 1, n+1 ):
115 c = self.addHost( 'c%s' % i, cls=dataController, 160 c = self.addHost( 'c%s' % i, cls=dataController,
116 inNamespace=True ) 161 inNamespace=True )
117 self.addLink( c, cs0 ) 162 self.addLink( c, cs0 )
...@@ -122,7 +167,7 @@ class ControlNetwork( Topo ): ...@@ -122,7 +167,7 @@ class ControlNetwork( Topo ):
122 167
123 class ONOSCluster( Controller ): 168 class ONOSCluster( Controller ):
124 # TODO 169 # TODO
125 - n = 4 170 + n = 3
126 171
127 def start( self ): 172 def start( self ):
128 ctopo = ControlNetwork( n=self.n, dataController=ONOS ) 173 ctopo = ControlNetwork( n=self.n, dataController=ONOS )
...@@ -137,6 +182,9 @@ class ONOSCluster( Controller ): ...@@ -137,6 +182,9 @@ class ONOSCluster( Controller ):
137 host.start() 182 host.start()
138 183
139 def stop( self ): 184 def stop( self ):
185 + for host in self.cnet.hosts:
186 + if isinstance( host, Controller ):
187 + host.stop()
140 self.cnet.stop() 188 self.cnet.stop()
141 189
142 def clist( self ): 190 def clist( self ):
...@@ -158,10 +206,11 @@ switches = { 'ovso': OVSSwitchONOS } ...@@ -158,10 +206,11 @@ switches = { 'ovso': OVSSwitchONOS }
158 206
159 if __name__ == '__main__': 207 if __name__ == '__main__':
160 # Simple test for ONOS() controller class 208 # Simple test for ONOS() controller class
161 - setLogLevel( 'info' ) 209 + setLogLevel( 'info' ) #TODO info
162 size = 2 if len( argv ) != 2 else int( argv[ 1 ] ) 210 size = 2 if len( argv ) != 2 else int( argv[ 1 ] )
163 net = Mininet( topo=LinearTopo( size ), 211 net = Mininet( topo=LinearTopo( size ),
164 - controller=partial( ONOSCluster, n=4 ), 212 + #controller=ONOS,
213 + controller=partial( ONOSCluster, n=3 ), #TODO
165 switch=OVSSwitchONOS ) 214 switch=OVSSwitchONOS )
166 net.start() 215 net.start()
167 #waitConnected( net.switches ) 216 #waitConnected( net.switches )
......