blob: 27527aa759a5c264343e3aa710520505038ee425 [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
stevenvanrossem9ebd0942016-02-22 10:13:05 +01009
stevenvanrossemc5a536a2016-02-16 14:52:39 +010010
11logging.basicConfig(level=logging.INFO)
12
13
14class ZeroRpcApiEndpointDCNetwork(object):
15 """
16 Simple API endpoint that offers a zerorpc-based
17 interface. This interface will be used by the
18 default command line client.
19 It can be used as a reference to implement
20 REST interfaces providing the same semantics,
21 like e.g. OpenStack compute API.
22 """
23
24 def __init__(self, listenip, port, DCNetwork=None):
25 if DCNetwork :
26 self.connectDCNetwork(DCNetwork)
27 self.ip = listenip
28 self.port = port
29 logging.debug("Created monitoring API endpoint %s(%s:%d)" % (
30 self.__class__.__name__, self.ip, self.port))
31
stevenvanrossemc5a536a2016-02-16 14:52:39 +010032 def connectDCNetwork(self, net):
33 self.net = net
stevenvanrossem8fbf9782016-02-17 11:40:23 +010034 logging.info("Connected DCNetwork to API endpoint %s(%s:%d)" % (
35 self.__class__.__name__, self.ip, self.port))
stevenvanrossemc5a536a2016-02-16 14:52:39 +010036
37 def start(self):
38 thread = threading.Thread(target=self._api_server_thread, args=())
39 thread.daemon = True
40 thread.start()
41 logging.debug("Started API endpoint %s(%s:%d)" % (
42 self.__class__.__name__, self.ip, self.port))
43
44 def _api_server_thread(self):
45 s = zerorpc.Server(DCNetworkApi(self.net))
46 s.bind("tcp://%s:%d" % (self.ip, self.port))
47 s.run()
48
49 def stop(self):
stevenvanrossemc5a536a2016-02-16 14:52:39 +010050 logging.info("Stop the monitoring API endpoint")
stevenvanrossemc5a536a2016-02-16 14:52:39 +010051 return
52
53
54class DCNetworkApi(object):
55 """
stevenvanrossem9ebd0942016-02-22 10:13:05 +010056 The networking and monitoring commands need the scope of the
57 whole DC network to find the requested vnf. So this API is intended
58 to work with a DCNetwork.
stevenvanrossemc5a536a2016-02-16 14:52:39 +010059 Just pass through the corresponding request to the
stevenvanrossem9ebd0942016-02-22 10:13:05 +010060 selected data center network. Do not implement provisioning
stevenvanrossemc5a536a2016-02-16 14:52:39 +010061 logic here because will will have multiple API
62 endpoint implementations at the end.
63 """
64
65 def __init__(self, net):
66 self.net = net
67
68 def network_action_start(self, vnf_src_name, vnf_dst_name):
69 # call DCNetwork method, not really datacenter specific API for now...
70 # provided dc name needs to be part of API endpoint
71 # no check if vnfs are really connected to this datacenter...
72 logging.debug("RPC CALL: network chain start")
73 try:
74 c = self.net.setChain(
75 vnf_src_name, vnf_dst_name)
76 return str(c)
77 except Exception as ex:
78 logging.exception("RPC error.")
79 return ex.message
80
81 def network_action_stop(self, vnf_src_name, vnf_dst_name):
82 # call DCNetwork method, not really datacenter specific API for now...
83 # provided dc name needs to be part of API endpoint
84 # no check if vnfs are really connected to this datacenter...
85 logging.debug("RPC CALL: network chain stop")
86 try:
87 c = self.net.setChain(
88 vnf_src_name, vnf_dst_name, cmd='del-flows')
89 return c
90 except Exception as ex:
91 logging.exception("RPC error.")
92 return ex.message
93
94 # get egress(default) or ingress rate of a vnf
95 def monitor_get_rate(self, vnf_name, direction):
96 logging.debug("RPC CALL: get rate")
97 try:
98 c = self.net.monitor_agent.get_rate(vnf_name, direction)
99 return c
100 except Exception as ex:
101 logging.exception("RPC error.")
102 return ex.message
103
104