2 Copyright (c) 2015 SONATA-NFV
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
9 http://www.apache.org/licenses/LICENSE-2.0
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
17 Neither the name of the SONATA-NFV [, ANY ADDITIONAL AFFILIATION]
18 nor the names of its contributors may be used to endorse or promote
19 products derived from this software without specific prior written
22 This work has been performed in the framework of the SONATA project,
23 funded by the European Commission under Grant number 671517 through
24 the Horizon 2020 and 5G-PPP programmes. The authors would like to
25 acknowledge the contributions of their colleagues of the SONATA
26 partner consortium (www.sonata-nfv.eu).
29 Distributed Cloud Emulator (dcemulator)
30 Networking and monitoring functions
31 (c) 2015 by Steven Van Rossem <steven.vanrossem@intec.ugent.be>
39 logging
.basicConfig(level
=logging
.INFO
)
42 class ZeroRpcApiEndpointDCNetwork(object):
44 Simple API endpoint that offers a zerorpc-based
45 interface. This interface will be used by the
46 default command line client.
47 It can be used as a reference to implement
48 REST interfaces providing the same semantics,
49 like e.g. OpenStack compute API.
52 def __init__(self
, listenip
, port
, DCNetwork
=None):
54 self
.connectDCNetwork(DCNetwork
)
57 logging
.debug("Created monitoring API endpoint %s(%s:%d)" % (
58 self
.__class
__.__name
__, self
.ip
, self
.port
))
60 def connectDCNetwork(self
, net
):
62 logging
.info("Connected DCNetwork to API endpoint %s(%s:%d)" % (
63 self
.__class
__.__name
__, self
.ip
, self
.port
))
66 thread
= threading
.Thread(target
=self
._api
_server
_thread
, args
=())
69 logging
.debug("Started API endpoint %s(%s:%d)" % (
70 self
.__class
__.__name
__, self
.ip
, self
.port
))
72 def _api_server_thread(self
):
73 s
= zerorpc
.Server(DCNetworkApi(self
.net
))
74 s
.bind("tcp://%s:%d" % (self
.ip
, self
.port
))
78 logging
.info("Stop the monitoring API endpoint")
82 class DCNetworkApi(object):
84 The networking and monitoring commands need the scope of the
85 whole DC network to find the requested vnf. So this API is intended
86 to work with a DCNetwork.
87 Just pass through the corresponding request to the
88 selected data center network. Do not implement provisioning
89 logic here because will will have multiple API
90 endpoint implementations at the end.
93 def __init__(self
, net
):
96 def network_action_start(self
, vnf_src_name
, vnf_dst_name
, kwargs
):
97 # call DCNetwork method, not really datacenter specific API for now...
98 # provided dc name needs to be part of API endpoint
99 # no check if vnfs are really connected to this datacenter...
100 logging
.debug("RPC CALL: network chain start")
102 c
= self
.net
.setChain(
103 vnf_src_name
, vnf_dst_name
,
104 vnf_src_interface
=kwargs
.get('vnf_src_interface'),
105 vnf_dst_interface
=kwargs
.get('vnf_dst_interface'),
107 weight
=kwargs
.get('weight'),
108 match
=kwargs
.get('match'),
109 bidirectional
=kwargs
.get('bidirectional'),
110 cookie
=kwargs
.get('cookie'))
112 except Exception as ex
:
113 logging
.exception("RPC error.")
116 def network_action_stop(self
, vnf_src_name
, vnf_dst_name
, kwargs
):
117 # call DCNetwork method, not really datacenter specific API for now...
118 # provided dc name needs to be part of API endpoint
119 # no check if vnfs are really connected to this datacenter...
120 logging
.debug("RPC CALL: network chain stop")
122 c
= self
.net
.setChain(
123 vnf_src_name
, vnf_dst_name
,
124 vnf_src_interface
=kwargs
.get('vnf_src_interface'),
125 vnf_dst_interface
=kwargs
.get('vnf_dst_interface'),
127 weight
=kwargs
.get('weight'),
128 match
=kwargs
.get('match'),
129 bidirectional
=kwargs
.get('bidirectional'),
130 cookie
=kwargs
.get('cookie'))
132 except Exception as ex
:
133 logging
.exception("RPC error.")
136 # setup the rate measurement for a vnf interface
137 def setup_metric(self
, vnf_name
, vnf_interface
, metric
):
138 logging
.debug("RPC CALL: setup metric")
140 c
= self
.net
.monitor_agent
.setup_metric(vnf_name
, vnf_interface
, metric
)
142 except Exception as ex
:
143 logging
.exception("RPC error.")
146 # remove the rate measurement for a vnf interface
147 def stop_metric(self
, vnf_name
, vnf_interface
, metric
):
148 logging
.debug("RPC CALL: stop metric")
150 c
= self
.net
.monitor_agent
.stop_metric(vnf_name
, vnf_interface
, metric
)
152 except Exception as ex
:
153 logging
.exception("RPC error.")
156 # setup the flow metrics measurement
157 def setup_flow(self
, vnf_name
, vnf_interface
, metric
, cookie
):
158 logging
.debug("RPC CALL: setup flow")
160 c
= self
.net
.monitor_agent
.setup_flow(vnf_name
, vnf_interface
, metric
, cookie
)
162 except Exception as ex
:
163 logging
.exception("RPC error.")
166 # remove the flow metrics measurement
167 def stop_flow(self
, vnf_name
, vnf_interface
, metric
, cookie
):
168 logging
.debug("RPC CALL: stop flow")
170 c
= self
.net
.monitor_agent
.stop_flow(vnf_name
, vnf_interface
, metric
, cookie
)
172 except Exception as ex
:
173 logging
.exception("RPC error.")
176 # do prometheus query
177 def prometheus(self
, dc_label
, vnf_name
, vnf_interface
, query
):
178 logging
.debug("RPC CALL: query prometheus")
179 vnf_status
= self
.net
.dcs
.get(dc_label
).containers
.get(vnf_name
).getStatus()
180 uuid
= vnf_status
['id']
181 query
= query
.replace('<uuid>', uuid
)
182 #if needed, replace interface id with emu-intfs name
183 # query = query.replace('<intf>', vnf_interface)
184 logging
.info('query: {0}'.format(query
))
186 c
= self
.net
.monitor_agent
.query_Prometheus(query
)
188 except Exception as ex
:
189 logging
.exception("RPC error.")