Switch to use new ovs nsh version
[groupbasedpolicy.git] / demos / gbpsfc-env / infrastructure_launch.py
1 #!/usr/bin/python
2
3 import socket
4 import os
5 import re
6 import time
7 import sys
8 import ipaddr
9 import commands
10 from subprocess import call
11 from subprocess import check_output
12 from infrastructure_config import *
13
14 def addController(sw, ip):
15     call(['ovs-vsctl', 'set-controller', sw, 'tcp:%s:6653' % ip ])
16
17 def addManager(ip):
18     cmd="ovs-vsctl set-manager tcp:%s:6640" % ip
19     listcmd=cmd.split()
20     print check_output(listcmd)
21
22 def addSwitch(name, dpid=None):
23     call(['ovs-vsctl', 'add-br', name]) #Add bridge
24     if dpid:
25         if len(dpid) < 16: #DPID must be 16-bytes in later versions of OVS
26             filler='0000000000000000'
27             dpid=filler[:len(filler)-len(dpid)]+dpid
28         elif len(dpid) > 16:
29             print 'DPID: %s is too long' % dpid
30             sys.exit(3)
31         call(['ovs-vsctl','set','bridge', name,'other-config:datapath-id=%s'%dpid])
32
33 def addHost(net, switch, name, ip, mac):
34     containerID=launchContainer()
35
36 def setOFVersion(sw, version='OpenFlow13,OpenFlow12,OpenFlow10'):
37     call(['ovs-vsctl', 'set', 'bridge', sw, 'protocols={}'.format(version)])
38
39 def addTunnel(sw, sourceIp=None):
40     ifaceName = '{}-vxlan-0'.format(sw)
41     cmd = ['ovs-vsctl', 'add-port', sw, ifaceName,
42            '--', 'set', 'Interface', ifaceName,
43            'type=vxlan',
44            'options:remote_ip=flow',
45            'options:key=flow']
46 #    if sourceIp is not None:
47 #        cmd.append('options:source_ip={}'.format(sourceIp))
48     call(cmd)
49
50 def addGpeTunnel(sw, sourceIp=None):
51     ifaceName = '{}-vxlangpe-0'.format(sw)
52     cmd = ['ovs-vsctl', 'add-port', sw, ifaceName,
53            '--', 'set', 'Interface', ifaceName,
54            'type=vxlan',
55            'options:exts=gpe',
56            'options:remote_ip=flow',
57            'options:dst_port=6633',
58            'options:nshc1=flow',
59            'options:nshc2=flow',
60            'options:nshc3=flow',
61            'options:nshc4=flow',
62            'options:nsp=flow',
63            'options:nsi=flow',
64            'options:key=flow']
65 #    if sourceIp is not None:
66 #        cmd.append('options:source_ip={}'.format(sourceIp))
67     call(cmd)
68
69 def launchContainer(host,containerImage):
70     containerID= check_output(['docker','run','-d','--net=none','--name=%s'%host['name'],'-h',host['name'],'-t', '-i','--privileged=True',containerImage,'/bin/bash']) #docker run -d --net=none --name={name} -h {name} -t -i {image} /bin/bash
71     #print "created container:", containerID[:-1]
72     return containerID[:-1] #Remove extraneous \n from output of above
73
74 def connectContainerToSwitch(sw,host,containerID,of_port):
75     hostIP=host['ip']
76     mac=host['mac']
77     nw = ipaddr.IPv4Network(hostIP)
78     broadcast = "{}".format(nw.broadcast)
79     router = "{}".format(nw.network + 1)
80     cmd=['/vagrant/ovswork.sh',sw,containerID,hostIP,broadcast,router,mac,of_port,host['name']]
81     if host.has_key('vlan'):
82         cmd.append(host['vlan'])
83     call(cmd)
84
85 def doCmd(cmd):
86     listcmd=cmd.split()
87     print check_output(listcmd)
88
89 def launch(switches, hosts, contIP='127.0.0.1'):
90
91     for sw in switches:
92         addManager(contIP)
93         ports=0
94         first_host=True
95         for host in hosts:
96             if host['switch'] == sw['name']:
97                 if first_host:
98                     dpid=sw['dpid']
99                     addSwitch(sw['name'],sw['dpid'])
100                     setOFVersion(sw['name'])
101                     addController(sw['name'], contIP)
102                     addGpeTunnel(sw['name'])
103                     addTunnel(sw['name'])
104                 first_host=False
105                 containerImage=defaultContainerImage #from Config
106                 if host.has_key('container_image'): #from Config
107                     containerImage=host['container_image']
108                 containerID=launchContainer(host,containerImage)
109                 ports+=1
110                 connectContainerToSwitch(sw['name'],host,containerID,str(ports))
111                 host['port-name']='vethl-'+host['name']
112                 print "Created container: %s with IP: %s. Connect using 'docker attach %s', disconnect with ctrl-p-q." % (host['name'],host['ip'],host['name'])
113
114 if __name__ == "__main__" :
115 #    print "Cleaning environment..."
116 #    doCmd('/vagrant/clean.sh')
117     sw_index=int(socket.gethostname().split("gbpsfc",1)[1])-1
118     if sw_index in range(0,len(switches)+1):
119
120        controller=os.environ.get('ODL')
121        sw_type = switches[sw_index]['type']
122        sw_name = switches[sw_index]['name']
123        if sw_type == 'gbp':
124            print "*****************************"
125            print "Configuring %s as a GBP node." % sw_name
126            print "*****************************"
127            print
128            launch([switches[sw_index]],hosts,controller)
129            print "*****************************"
130            print "OVS status:"
131            print "-----------"
132            print
133            doCmd('ovs-vsctl show')
134            print
135            print "Docker containers:"
136            print "------------------"
137            doCmd('docker ps')
138            print "*****************************"
139        elif sw_type == 'sff':
140            print "*****************************"
141            print "Configuring %s as an SFF." % sw_name
142            print "*****************************"
143            doCmd('sudo ovs-vsctl set-manager tcp:%s:6640' % controller)
144            print
145        elif sw_type == 'sf':
146            print "*****************************"
147            print "Configuring %s as an SF." % sw_name
148            print "*****************************"
149            doCmd('sudo /vagrant/sf-config.sh')
150            #addGpeTunnel(switches[sw_index]['name'])
151        elif sw_type == 'sflow':
152            print "*****************************"
153            print "Configuring %s as an sFlow-RT collector." % sw_name
154            print "*****************************"
155            doCmd('sudo /vagrant/install_java.sh')
156            doCmd('sudo /vagrant/sflow/install_sflow-rt.sh')
157            call('sudo /home/vagrant/sflow-rt/gbp_start.sh &'.split())
158            doCmd('sudo /vagrant/sflow/set_collector_ip.sh')
159            doCmd('sudo /vagrant/sflow/curl_put_collector.sh')
160            print "Configuring finished."
161
162