37b34f9e04c8e8488b16edf248694bd30060bff9
2 Distributed Cloud Emulator (dcemulator)
10 logging
.basicConfig(level
=logging
.INFO
)
13 class ZeroRpcApiEndpointDCNetwork(object):
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.
23 def __init__(self
, listenip
, port
, DCNetwork
=None):
25 self
.connectDCNetwork(DCNetwork
)
28 logging
.debug("Created monitoring API endpoint %s(%s:%d)" % (
29 self
.__class
__.__name
__, self
.ip
, self
.port
))
31 def connectDCNetwork(self
, net
):
33 logging
.info("Connected DCNetwork to API endpoint %s(%s:%d)" % (
34 self
.__class
__.__name
__, self
.ip
, self
.port
))
37 thread
= threading
.Thread(target
=self
._api
_server
_thread
, args
=())
40 logging
.debug("Started API endpoint %s(%s:%d)" % (
41 self
.__class
__.__name
__, self
.ip
, self
.port
))
43 def _api_server_thread(self
):
44 s
= zerorpc
.Server(DCNetworkApi(self
.net
))
45 s
.bind("tcp://%s:%d" % (self
.ip
, self
.port
))
49 logging
.info("Stop the monitoring API endpoint")
53 class DCNetworkApi(object):
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.
64 def __init__(self
, net
):
67 def network_action_start(self
, vnf_src_name
, vnf_dst_name
, kwargs
):
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")
73 c
= self
.net
.setChain(
74 vnf_src_name
, vnf_dst_name
,
75 vnf_src_interface
=kwargs
.get('vnf_src_interface'),
76 vnf_dst_interface
=kwargs
.get('vnf_dst_interface'),
78 weight
=kwargs
.get('weight'),
79 match
=kwargs
.get('match'),
80 bidirectional
=kwargs
.get('bidirectional'),
81 cookie
=kwargs
.get('cookie'))
83 except Exception as ex
:
84 logging
.exception("RPC error.")
87 def network_action_stop(self
, vnf_src_name
, vnf_dst_name
, kwargs
):
88 # call DCNetwork method, not really datacenter specific API for now...
89 # provided dc name needs to be part of API endpoint
90 # no check if vnfs are really connected to this datacenter...
91 logging
.debug("RPC CALL: network chain stop")
93 c
= self
.net
.setChain(
94 vnf_src_name
, vnf_dst_name
,
95 vnf_src_interface
=kwargs
.get('vnf_src_interface'),
96 vnf_dst_interface
=kwargs
.get('vnf_dst_interface'),
98 weight
=kwargs
.get('weight'),
99 match
=kwargs
.get('match'),
100 bidirectional
=kwargs
.get('bidirectional'),
101 cookie
=kwargs
.get('cookie'))
103 except Exception as ex
:
104 logging
.exception("RPC error.")
107 # setup the rate measurement for a vnf interface
108 def setup_metric(self
, vnf_name
, vnf_interface
, metric
):
109 logging
.debug("RPC CALL: setup metric")
111 c
= self
.net
.monitor_agent
.setup_metric(vnf_name
, vnf_interface
, metric
)
113 except Exception as ex
:
114 logging
.exception("RPC error.")
117 # remove the rate measurement for a vnf interface
118 def stop_metric(self
, vnf_name
, vnf_interface
, metric
):
119 logging
.debug("RPC CALL: stop metric")
121 c
= self
.net
.monitor_agent
.stop_metric(vnf_name
, vnf_interface
, metric
)
123 except Exception as ex
:
124 logging
.exception("RPC error.")
127 # setup the flow metrics measurement
128 def setup_flow(self
, vnf_name
, vnf_interface
, metric
, cookie
):
129 logging
.debug("RPC CALL: setup flow")
131 c
= self
.net
.monitor_agent
.setup_flow(vnf_name
, vnf_interface
, metric
, cookie
)
133 except Exception as ex
:
134 logging
.exception("RPC error.")
137 # do prometheus query
138 def prometheus(self
, dc_label
, vnf_name
, vnf_interface
, query
):
139 logging
.debug("RPC CALL: query prometheus")
140 vnf_status
= self
.net
.dcs
.get(dc_label
).containers
.get(vnf_name
).getStatus()
141 uuid
= vnf_status
['id']
142 query
= query
.replace('<uuid>', uuid
)
143 #if needed, replace interface id with emu-intfs name
144 # query = query.replace('<intf>', vnf_interface)
145 logging
.info('query: {0}'.format(query
))
147 c
= self
.net
.monitor_agent
.query_Prometheus(query
)
149 except Exception as ex
:
150 logging
.exception("RPC error.")