blob: 138b071849b946ff31adcf501c318b8f6699568b [file] [log] [blame]
Jonathan Hartce97e5b2016-04-19 01:41:31 -07001"""
2Libraries for using ONOS from within Mininet.
3"""
4
5from mininet.node import OVSBridge
6from mininet.util import netParse, ipStr
7import os, sys, imp
8
9# Import the ONOS classes from onos.py in the ONOS repository
10if not 'ONOS_ROOT' in os.environ:
11 print 'ONOS_ROOT is not set.'
12 print 'Try running the script with \'sudo -E\' to pass your environment in.'
13 sys.exit(1)
14
15onos_path = os.path.join(os.path.abspath(os.environ['ONOS_ROOT']), 'tools/test/topos/onos.py')
16onos = imp.load_source('onos', onos_path)
17from onos import ONOS
18
19class ONOSHostCluster(object):
20 def __init__(self, controlSubnet='192.168.1.0/24', numInstances=1, basename='ONOS',
21 features=[]):
22 self.controlSubnet = controlSubnet
23 self.numInstances = numInstances
24 self.basename = basename
25 self.instances = []
26 self.features = features
27
28 def create(self, topology):
29 cs0 = topology.addSwitch('cs0', cls=OVSBridge)
30
31 ctrlIp, ctrlPrefixLen = netParse(self.controlSubnet)
32
33 for i in range(1, self.numInstances + 1):
34 strCtrlIp = '%s/%i' % (ipStr(ctrlIp + i), ctrlPrefixLen)
35
36 c = topology.addHost('%s%s' % (self.basename, i), cls=ONOS, inNamespace=True,
37 ip=strCtrlIp,
38 features=['onos-app-config', 'onos-app-proxyarp',
39 'onos-core'] + self.features,
40 reactive=False)
41
42 topology.addLink(c, cs0, params1={ 'ip' : strCtrlIp })
43
44 self.instances.append(c)
45
46 # Connect switch to root namespace so that data network
47 # switches will be able to talk to us
48 highestIp = '%s/%i' % (ipStr(ctrlIp + (2 ** (32 - ctrlPrefixLen)) - 2), ctrlPrefixLen)
49 root = topology.addHost('root', inNamespace=False, ip=highestIp)
50 topology.addLink(root, cs0)
51
52class ONOSHostSdnipCluster(ONOSHostCluster):
53
54 def __init__(self, dataSubnet='10.0.0.0/24', features=['onos-app-sdnip'], **kwargs):
55 super(ONOSHostSdnipCluster, self).__init__(features=features, **kwargs)
56
57 self.dataSubnet = dataSubnet
58
59 def create(self, topology):
60 super(ONOSHostSdnipCluster, self).create(topology)
61
62 cs1 = topology.addSwitch('cs1', cls=OVSBridge)
63
64 dataIp, dataPrefixLen = netParse(self.dataSubnet)
65 for i in range(1, len(self.instances) + 1):
66 c = self.instances[i-1]
67 strDataIp = '%s/%i' % (ipStr(dataIp + i), dataPrefixLen)
68 topology.addLink(c, cs1, params1={ 'ip' : strDataIp })
69
70 return cs1