start cadvisor and prometheus at startup
[osm/vim-emu.git] / src / emuvim / api / zerorpc / network.py
1 """
2 Distributed Cloud Emulator (dcemulator)
3 """
4
5 import logging
6 import threading
7 import zerorpc
8
9
10 logging.basicConfig(level=logging.INFO)
11
12
13 class ZeroRpcApiEndpointDCNetwork(object):
14 """
15 Simple API endpoint that offers a zerorpc-based
16 interface. This interface will be used by the
17 default command line client.
18 It can be used as a reference to implement
19 REST interfaces providing the same semantics,
20 like e.g. OpenStack compute API.
21 """
22
23 def __init__(self, listenip, port, DCNetwork=None):
24 if DCNetwork :
25 self.connectDCNetwork(DCNetwork)
26 self.ip = listenip
27 self.port = port
28 logging.debug("Created monitoring API endpoint %s(%s:%d)" % (
29 self.__class__.__name__, self.ip, self.port))
30
31 def connectDCNetwork(self, net):
32 self.net = net
33 logging.info("Connected DCNetwork to API endpoint %s(%s:%d)" % (
34 self.__class__.__name__, self.ip, self.port))
35
36 def start(self):
37 thread = threading.Thread(target=self._api_server_thread, args=())
38 thread.daemon = True
39 thread.start()
40 logging.debug("Started API endpoint %s(%s:%d)" % (
41 self.__class__.__name__, self.ip, self.port))
42
43 def _api_server_thread(self):
44 s = zerorpc.Server(DCNetworkApi(self.net))
45 s.bind("tcp://%s:%d" % (self.ip, self.port))
46 s.run()
47
48 def stop(self):
49 logging.info("Stop the monitoring API endpoint")
50 return
51
52
53 class DCNetworkApi(object):
54 """
55 The networking and monitoring commands need the scope of the
56 whole DC network to find the requested vnf. So this API is intended
57 to work with a DCNetwork.
58 Just pass through the corresponding request to the
59 selected data center network. Do not implement provisioning
60 logic here because will will have multiple API
61 endpoint implementations at the end.
62 """
63
64 def __init__(self, net):
65 self.net = net
66
67 def network_action_start(self, vnf_src_name, vnf_dst_name, vnf_src_interface=None, vnf_dst_interface=None):
68 # call DCNetwork method, not really datacenter specific API for now...
69 # provided dc name needs to be part of API endpoint
70 # no check if vnfs are really connected to this datacenter...
71 logging.debug("RPC CALL: network chain start")
72 try:
73 c = self.net.setChain(
74 vnf_src_name, vnf_dst_name, vnf_src_interface, vnf_dst_interface)
75 return str(c)
76 except Exception as ex:
77 logging.exception("RPC error.")
78 return ex.message
79
80 def network_action_stop(self, vnf_src_name, vnf_dst_name, vnf_src_interface=None, vnf_dst_interface=None):
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 stop")
85 try:
86 c = self.net.setChain(
87 vnf_src_name, vnf_dst_name, vnf_src_interface, vnf_dst_interface, cmd='del-flows')
88 return c
89 except Exception as ex:
90 logging.exception("RPC error.")
91 return ex.message
92
93 # setup the rate measurement for a vnf interface
94 def setup_metric(self, vnf_name, vnf_interface, metric):
95 logging.debug("RPC CALL: setup metric")
96 try:
97 c = self.net.monitor_agent.setup_metric(vnf_name, vnf_interface, metric)
98 return c
99 except Exception as ex:
100 logging.exception("RPC error.")
101 return ex.message
102
103 # remove the rate measurement for a vnf interface
104 def stop_metric(self, vnf_name, vnf_interface, metric):
105 logging.debug("RPC CALL: setup metric")
106 try:
107 c = self.net.monitor_agent.remove_metric(vnf_name, vnf_interface, metric)
108 return c
109 except Exception as ex:
110 logging.exception("RPC error.")
111 return ex.message
112
113 # setup the rate measurement for a vnf interface
114 def monitor_setup_rate_measurement(self, vnf_name, vnf_interface, metric):
115 logging.debug("RPC CALL: get rate")
116 try:
117 c = self.net.monitor_agent.setup_rate_measurement(vnf_name, vnf_interface, metric)
118 return c
119 except Exception as ex:
120 logging.exception("RPC error.")
121 return ex.message
122
123 # get egress(default) or ingress rate of a vnf
124 def monitor_get_rate(self, vnf_name, vnf_interface, metric):
125 logging.debug("RPC CALL: get rate")
126 try:
127 c = self.net.monitor_agent.get_rate(vnf_name, vnf_interface, metric)
128 return c
129 except Exception as ex:
130 logging.exception("RPC error.")
131 return ex.message
132
133