merge network/monitoring cli commands
[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/ofctl_rest.py'
36 ryu_cmd = 'ryu-manager'
37 self.ryu_process = Popen([ryu_cmd,ryu_path])
38
39
40 def connectDCNetwork(self, net):
41 self.net = net
42 logging.info("Connected DCNetwork(%s) to API endpoint %s(%s:%d)" % (
43 net.name, self.__class__.__name__, self.ip, self.port))
44
45 def start(self):
46 thread = threading.Thread(target=self._api_server_thread, args=())
47 thread.daemon = True
48 thread.start()
49 logging.debug("Started API endpoint %s(%s:%d)" % (
50 self.__class__.__name__, self.ip, self.port))
51
52 def _api_server_thread(self):
53 s = zerorpc.Server(DCNetworkApi(self.net))
54 s.bind("tcp://%s:%d" % (self.ip, self.port))
55 s.run()
56
57 def stop(self):
58 # stop ryu controller
59 logging.info("Stop the monitoring API endpoint")
60 self.ryu_process.terminate()
61 #self.ryu_process.kill()
62 return
63
64
65 class DCNetworkApi(object):
66 """
67 Just pass through the corresponding request to the
68 selected data center. Do not implement provisioning
69 logic here because will will have multiple API
70 endpoint implementations at the end.
71 """
72
73 def __init__(self, net):
74 self.net = net
75
76 def network_action_start(self, vnf_src_name, vnf_dst_name):
77 # call DCNetwork method, not really datacenter specific API for now...
78 # provided dc name needs to be part of API endpoint
79 # no check if vnfs are really connected to this datacenter...
80 logging.debug("RPC CALL: network chain start")
81 try:
82 c = self.net.setChain(
83 vnf_src_name, vnf_dst_name)
84 return str(c)
85 except Exception as ex:
86 logging.exception("RPC error.")
87 return ex.message
88
89 def network_action_stop(self, vnf_src_name, vnf_dst_name):
90 # call DCNetwork method, not really datacenter specific API for now...
91 # provided dc name needs to be part of API endpoint
92 # no check if vnfs are really connected to this datacenter...
93 logging.debug("RPC CALL: network chain stop")
94 try:
95 c = self.net.setChain(
96 vnf_src_name, vnf_dst_name, cmd='del-flows')
97 return c
98 except Exception as ex:
99 logging.exception("RPC error.")
100 return ex.message
101
102 # get egress(default) or ingress rate of a vnf
103 def monitor_get_rate(self, vnf_name, direction):
104 logging.debug("RPC CALL: get rate")
105 try:
106 c = self.net.monitor_agent.get_rate(vnf_name, direction)
107 return c
108 except Exception as ex:
109 logging.exception("RPC error.")
110 return ex.message
111
112