Bug 4988: OF statistics & REST client
[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:remote_ip=flow',
56            'options:dst_port=6633',
57            'options:nshc1=flow',
58            'options:nshc2=flow',
59            'options:nshc3=flow',
60            'options:nshc4=flow',
61            'options:nsp=flow',
62            'options:nsi=flow',
63            'options:key=flow']
64 #    if sourceIp is not None:
65 #        cmd.append('options:source_ip={}'.format(sourceIp))
66     call(cmd)
67
68 def launchContainer(host,containerImage):
69     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
70     #print "created container:", containerID[:-1]
71     return containerID[:-1] #Remove extraneous \n from output of above
72
73 def connectContainerToSwitch(sw,host,containerID,of_port):
74     hostIP=host['ip']
75     mac=host['mac']
76     nw = ipaddr.IPv4Network(hostIP)
77     broadcast = "{}".format(nw.broadcast)
78     router = "{}".format(nw.network + 1)
79     cmd=['/vagrant/ovswork.sh',sw,containerID,hostIP,broadcast,router,mac,of_port,host['name']]
80     if host.has_key('vlan'):
81         cmd.append(host['vlan'])
82     call(cmd)
83
84 def doCmd(cmd):
85     listcmd=cmd.split()
86     print check_output(listcmd)
87
88 def launch(switches, hosts, contIP='127.0.0.1'):
89
90     for sw in switches:
91         addManager(contIP)
92         ports=0
93         first_host=True
94         for host in hosts:
95             if host['switch'] == sw['name']:
96                 if first_host:
97                     dpid=sw['dpid']
98                     addSwitch(sw['name'],sw['dpid'])
99                     setOFVersion(sw['name'])
100                     addController(sw['name'], contIP)
101                     addGpeTunnel(sw['name'])
102                     addTunnel(sw['name'])
103                 first_host=False
104                 containerImage=defaultContainerImage #from Config
105                 if host.has_key('container_image'): #from Config
106                     containerImage=host['container_image']
107                 containerID=launchContainer(host,containerImage)
108                 ports+=1
109                 connectContainerToSwitch(sw['name'],host,containerID,str(ports))
110                 host['port-name']='vethl-'+host['name']
111                 print "Created container: %s with IP: %s. Connect using 'docker attach %s', disconnect with ctrl-p-q." % (host['name'],host['ip'],host['name'])
112
113 if __name__ == "__main__" :
114 #    print "Cleaning environment..."
115 #    doCmd('/vagrant/clean.sh')
116     sw_index=int(socket.gethostname().split("gbpsfc",1)[1])-1
117     if sw_index in range(0,len(switches)+1):
118
119        controller=os.environ.get('ODL')
120        sw_type = switches[sw_index]['type']
121        sw_name = switches[sw_index]['name']
122        if sw_type == 'gbp':
123            print "*****************************"
124            print "Configuring %s as a GBP node." % sw_name
125            print "*****************************"
126            print
127            launch([switches[sw_index]],hosts,controller)
128            print "*****************************"
129            print "OVS status:"
130            print "-----------"
131            print
132            doCmd('ovs-vsctl show')
133            print
134            print "Docker containers:"
135            print "------------------"
136            doCmd('docker ps')
137            print "*****************************"
138        elif sw_type == 'sff':
139            print "*****************************"
140            print "Configuring %s as an SFF." % sw_name
141            print "*****************************"
142            doCmd('sudo ovs-vsctl set-manager tcp:%s:6640' % controller)
143            print
144        elif sw_type == 'sf':
145            print "*****************************"
146            print "Configuring %s as an SF." % sw_name
147            print "*****************************"
148            doCmd('sudo /vagrant/sf-config.sh')
149            #addGpeTunnel(switches[sw_index]['name'])
150        elif sw_type == 'sflow':
151            print "*****************************"
152            print "Configuring %s as an sFlow-RT collector." % sw_name
153            print "*****************************"
154            doCmd('sudo /vagrant/install_java.sh')
155            doCmd('sudo /vagrant/sflow/install_sflow-rt.sh')
156            call('sudo /home/vagrant/sflow-rt/gbp_start.sh &'.split())
157            doCmd('sudo /vagrant/sflow/set_collector_ip.sh')
158            doCmd('sudo /vagrant/sflow/curl_put_collector.sh')
159            print "Configuring finished."
160
161