blob: 24becd388d3da3982d714afdc9a0334df6b292d9 [file] [log] [blame]
stevenvanrossemc5a536a2016-02-16 14:52:39 +01001"""
2Distributed Cloud Emulator (dcemulator)
3(c) 2015 by Manuel Peuster <manuel.peuster@upb.de>
4"""
5
6import logging
7import threading
8import zerorpc
9import site
10from subprocess import Popen
11
12logging.basicConfig(level=logging.INFO)
13
14
15class 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]
stevenvanrossem8dbaa3d2016-02-17 15:07:15 +010035 ryu_path = python_install_path + '/ryu/app/simple_switch_13.py'
36 ryu_path2 = python_install_path + '/ryu/app/ofctl_rest.py'
stevenvanrossemc5a536a2016-02-16 14:52:39 +010037 ryu_cmd = 'ryu-manager'
stevenvanrossem8dbaa3d2016-02-17 15:07:15 +010038 self.ryu_process = Popen([ryu_cmd, ryu_path, ryu_path2])
stevenvanrossemc5a536a2016-02-16 14:52:39 +010039
40
41 def connectDCNetwork(self, net):
42 self.net = net
stevenvanrossem8fbf9782016-02-17 11:40:23 +010043 logging.info("Connected DCNetwork to API endpoint %s(%s:%d)" % (
44 self.__class__.__name__, self.ip, self.port))
stevenvanrossemc5a536a2016-02-16 14:52:39 +010045
46 def start(self):
47 thread = threading.Thread(target=self._api_server_thread, args=())
48 thread.daemon = True
49 thread.start()
50 logging.debug("Started API endpoint %s(%s:%d)" % (
51 self.__class__.__name__, self.ip, self.port))
52
53 def _api_server_thread(self):
54 s = zerorpc.Server(DCNetworkApi(self.net))
55 s.bind("tcp://%s:%d" % (self.ip, self.port))
56 s.run()
57
58 def stop(self):
59 # stop ryu controller
60 logging.info("Stop the monitoring API endpoint")
61 self.ryu_process.terminate()
62 #self.ryu_process.kill()
63 return
64
65
66class DCNetworkApi(object):
67 """
68 Just pass through the corresponding request to the
69 selected data center. Do not implement provisioning
70 logic here because will will have multiple API
71 endpoint implementations at the end.
72 """
73
74 def __init__(self, net):
75 self.net = net
76
77 def network_action_start(self, vnf_src_name, vnf_dst_name):
78 # call DCNetwork method, not really datacenter specific API for now...
79 # provided dc name needs to be part of API endpoint
80 # no check if vnfs are really connected to this datacenter...
81 logging.debug("RPC CALL: network chain start")
82 try:
83 c = self.net.setChain(
84 vnf_src_name, vnf_dst_name)
85 return str(c)
86 except Exception as ex:
87 logging.exception("RPC error.")
88 return ex.message
89
90 def network_action_stop(self, vnf_src_name, vnf_dst_name):
91 # call DCNetwork method, not really datacenter specific API for now...
92 # provided dc name needs to be part of API endpoint
93 # no check if vnfs are really connected to this datacenter...
94 logging.debug("RPC CALL: network chain stop")
95 try:
96 c = self.net.setChain(
97 vnf_src_name, vnf_dst_name, cmd='del-flows')
98 return c
99 except Exception as ex:
100 logging.exception("RPC error.")
101 return ex.message
102
103 # get egress(default) or ingress rate of a vnf
104 def monitor_get_rate(self, vnf_name, direction):
105 logging.debug("RPC CALL: get rate")
106 try:
107 c = self.net.monitor_agent.get_rate(vnf_name, direction)
108 return c
109 except Exception as ex:
110 logging.exception("RPC error.")
111 return ex.message
112
113