cleanup for open sourcing
[osm/vim-emu.git] / src / emuvim / api / zerorpc / network.py
1 """
2 Copyright (c) 2015 SONATA-NFV
3 ALL RIGHTS RESERVED.
4
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
8
9 http://www.apache.org/licenses/LICENSE-2.0
10
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.
16
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
20 permission.
21
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).
27 """
28 """
29 Distributed Cloud Emulator (dcemulator)
30 Networking and monitoring functions
31 (c) 2015 by Steven Van Rossem <steven.vanrossem@intec.ugent.be>
32 """
33
34 import logging
35 import threading
36 import zerorpc
37
38
39 logging.basicConfig(level=logging.INFO)
40
41
42 class ZeroRpcApiEndpointDCNetwork(object):
43 """
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.
50 """
51
52 def __init__(self, listenip, port, DCNetwork=None):
53 if DCNetwork :
54 self.connectDCNetwork(DCNetwork)
55 self.ip = listenip
56 self.port = port
57 logging.debug("Created monitoring API endpoint %s(%s:%d)" % (
58 self.__class__.__name__, self.ip, self.port))
59
60 def connectDCNetwork(self, net):
61 self.net = net
62 logging.info("Connected DCNetwork to API endpoint %s(%s:%d)" % (
63 self.__class__.__name__, self.ip, self.port))
64
65 def start(self):
66 thread = threading.Thread(target=self._api_server_thread, args=())
67 thread.daemon = True
68 thread.start()
69 logging.debug("Started API endpoint %s(%s:%d)" % (
70 self.__class__.__name__, self.ip, self.port))
71
72 def _api_server_thread(self):
73 s = zerorpc.Server(DCNetworkApi(self.net))
74 s.bind("tcp://%s:%d" % (self.ip, self.port))
75 s.run()
76
77 def stop(self):
78 logging.info("Stop the monitoring API endpoint")
79 return
80
81
82 class DCNetworkApi(object):
83 """
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.
91 """
92
93 def __init__(self, net):
94 self.net = net
95
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")
101 try:
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'),
106 cmd='add-flow',
107 weight=kwargs.get('weight'),
108 match=kwargs.get('match'),
109 bidirectional=kwargs.get('bidirectional'),
110 cookie=kwargs.get('cookie'))
111 return str(c)
112 except Exception as ex:
113 logging.exception("RPC error.")
114 return ex.message
115
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")
121 try:
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'),
126 cmd='del-flows',
127 weight=kwargs.get('weight'),
128 match=kwargs.get('match'),
129 bidirectional=kwargs.get('bidirectional'),
130 cookie=kwargs.get('cookie'))
131 return c
132 except Exception as ex:
133 logging.exception("RPC error.")
134 return ex.message
135
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")
139 try:
140 c = self.net.monitor_agent.setup_metric(vnf_name, vnf_interface, metric)
141 return c
142 except Exception as ex:
143 logging.exception("RPC error.")
144 return ex.message
145
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")
149 try:
150 c = self.net.monitor_agent.stop_metric(vnf_name, vnf_interface, metric)
151 return c
152 except Exception as ex:
153 logging.exception("RPC error.")
154 return ex.message
155
156 # setup the flow metrics measurement
157 def setup_flow(self, vnf_name, vnf_interface, metric, cookie):
158 logging.debug("RPC CALL: setup flow")
159 try:
160 c = self.net.monitor_agent.setup_flow(vnf_name, vnf_interface, metric, cookie)
161 return c
162 except Exception as ex:
163 logging.exception("RPC error.")
164 return ex.message
165
166 # remove the flow metrics measurement
167 def stop_flow(self, vnf_name, vnf_interface, metric, cookie):
168 logging.debug("RPC CALL: stop flow")
169 try:
170 c = self.net.monitor_agent.stop_flow(vnf_name, vnf_interface, metric, cookie)
171 return c
172 except Exception as ex:
173 logging.exception("RPC error.")
174 return ex.message
175
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))
185 try:
186 c = self.net.monitor_agent.query_Prometheus(query)
187 return c
188 except Exception as ex:
189 logging.exception("RPC error.")
190 return ex.message
191
192
193