Merge branch 'master' of https://github.com/stevenvanrossem/son-emu
[osm/vim-emu.git] / emuvim / api / zerorpcapi_DCNetwork.py
1 """
2 Distributed Cloud Emulator (dcemulator)
3 (c) 2015 by Manuel Peuster <manuel.peuster@upb.de>
4 """
5
6 import logging
7 import threading
8 import zerorpc
9 import site
10 from subprocess import Popen
11
12 logging.basicConfig(level=logging.INFO)
13
14
15 class ZeroRpcApiEndpointDCNetwork(object):
16 """
17 Simple API endpoint that offers a zerorpc-based
18 interface. This interface will be used by the
19 default command line client.
20 It can be used as a reference to implement
21 REST interfaces providing the same semantics,
22 like e.g. OpenStack compute API.
23 """
24
25 def __init__(self, listenip, port, DCNetwork=None):
26 if DCNetwork :
27 self.connectDCNetwork(DCNetwork)
28 self.ip = listenip
29 self.port = port
30 logging.debug("Created monitoring API endpoint %s(%s:%d)" % (
31 self.__class__.__name__, self.ip, self.port))
32
33 # start Ryu controller with rest-API
34 python_install_path = site.getsitepackages()[0]
35 ryu_path = python_install_path + '/ryu/app/simple_switch_13.py'
36 ryu_path2 = python_install_path + '/ryu/app/ofctl_rest.py'
37 # change the default Openflow controller port to 6653 (official IANA-assigned port number), as used by Mininet
38 # Ryu still uses 6633 as default
39 ryu_option = '--ofp-tcp-listen-port'
40 ryu_of_port = '6653'
41 ryu_cmd = 'ryu-manager'
42 self.ryu_process = Popen([ryu_cmd, ryu_path, ryu_path2, ryu_option, ryu_of_port])
43
44 def connectDCNetwork(self, net):
45 self.net = net
46 logging.info("Connected DCNetwork to API endpoint %s(%s:%d)" % (
47 self.__class__.__name__, self.ip, self.port))
48
49 def start(self):
50 thread = threading.Thread(target=self._api_server_thread, args=())
51 thread.daemon = True
52 thread.start()
53 logging.debug("Started API endpoint %s(%s:%d)" % (
54 self.__class__.__name__, self.ip, self.port))
55
56 def _api_server_thread(self):
57 s = zerorpc.Server(DCNetworkApi(self.net))
58 s.bind("tcp://%s:%d" % (self.ip, self.port))
59 s.run()
60
61 def stop(self):
62 # stop ryu controller
63 logging.info("Stop the monitoring API endpoint")
64 self.ryu_process.terminate()
65 #self.ryu_process.kill()
66 return
67
68
69 class DCNetworkApi(object):
70 """
71 Just pass through the corresponding request to the
72 selected data center. Do not implement provisioning
73 logic here because will will have multiple API
74 endpoint implementations at the end.
75 """
76
77 def __init__(self, net):
78 self.net = net
79
80 def network_action_start(self, vnf_src_name, vnf_dst_name):
81 # call DCNetwork method, not really datacenter specific API for now...
82 # provided dc name needs to be part of API endpoint
83 # no check if vnfs are really connected to this datacenter...
84 logging.debug("RPC CALL: network chain start")
85 try:
86 c = self.net.setChain(
87 vnf_src_name, vnf_dst_name)
88 return str(c)
89 except Exception as ex:
90 logging.exception("RPC error.")
91 return ex.message
92
93 def network_action_stop(self, vnf_src_name, vnf_dst_name):
94 # call DCNetwork method, not really datacenter specific API for now...
95 # provided dc name needs to be part of API endpoint
96 # no check if vnfs are really connected to this datacenter...
97 logging.debug("RPC CALL: network chain stop")
98 try:
99 c = self.net.setChain(
100 vnf_src_name, vnf_dst_name, cmd='del-flows')
101 return c
102 except Exception as ex:
103 logging.exception("RPC error.")
104 return ex.message
105
106 # get egress(default) or ingress rate of a vnf
107 def monitor_get_rate(self, vnf_name, direction):
108 logging.debug("RPC CALL: get rate")
109 try:
110 c = self.net.monitor_agent.get_rate(vnf_name, direction)
111 return c
112 except Exception as ex:
113 logging.exception("RPC error.")
114 return ex.message
115
116