Merge pull request #30 from cgeoffroy/pr-fix_list_remove_dockernet_cont
[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
10
11 logging.basicConfig(level=logging.INFO)
12
13
14 class 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
32 def connectDCNetwork(self, net):
33 self.net = net
34 logging.info("Connected DCNetwork to API endpoint %s(%s:%d)" % (
35 self.__class__.__name__, self.ip, self.port))
36
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):
50 logging.info("Stop the monitoring API endpoint")
51 return
52
53
54 class DCNetworkApi(object):
55 """
56 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.
59 Just pass through the corresponding request to the
60 selected data center network. Do not implement provisioning
61 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