Merge pull request #131 from stevenvanrossem/master
[osm/vim-emu.git] / src / emuvim / api / sonata / dummygatekeeper.py
1 """
2 This module implements a simple REST API that behaves like SONATA's gatekeeper.
3
4 It is only used to support the development of SONATA's SDK tools and to demonstrate
5 the year 1 version of the emulator until the integration with WP4's orchestrator is done.
6 """
7
8 import logging
9 import os
10 import uuid
11 import hashlib
12 import zipfile
13 import yaml
14 from docker import Client as DockerClient
15 from flask import Flask, request
16 import flask_restful as fr
17 from collections import defaultdict
18
19 logging.basicConfig()
20 LOG = logging.getLogger("sonata-dummy-gatekeeper")
21 LOG.setLevel(logging.DEBUG)
22 logging.getLogger("werkzeug").setLevel(logging.WARNING)
23
24 GK_STORAGE = "/tmp/son-dummy-gk/"
25 UPLOAD_FOLDER = os.path.join(GK_STORAGE, "uploads/")
26 CATALOG_FOLDER = os.path.join(GK_STORAGE, "catalog/")
27
28 # Enable Dockerfile build functionality
29 BUILD_DOCKERFILE = False
30
31 # flag to indicate that we run without the emulator (only the bare API for integration testing)
32 GK_STANDALONE_MODE = False
33
34 # should a new version of an image be pulled even if its available
35 FORCE_PULL = False
36
37 class Gatekeeper(object):
38
39 def __init__(self):
40 self.services = dict()
41 self.dcs = dict()
42 self.vnf_counter = 0 # used to generate short names for VNFs (Mininet limitation)
43 LOG.info("Create SONATA dummy gatekeeper.")
44
45 def register_service_package(self, service_uuid, service):
46 """
47 register new service package
48 :param service_uuid
49 :param service object
50 """
51 self.services[service_uuid] = service
52 # lets perform all steps needed to onboard the service
53 service.onboard()
54
55 def get_next_vnf_name(self):
56 self.vnf_counter += 1
57 return "vnf%d" % self.vnf_counter
58
59
60 class Service(object):
61 """
62 This class represents a NS uploaded as a *.son package to the
63 dummy gatekeeper.
64 Can have multiple running instances of this service.
65 """
66
67 def __init__(self,
68 service_uuid,
69 package_file_hash,
70 package_file_path):
71 self.uuid = service_uuid
72 self.package_file_hash = package_file_hash
73 self.package_file_path = package_file_path
74 self.package_content_path = os.path.join(CATALOG_FOLDER, "services/%s" % self.uuid)
75 self.manifest = None
76 self.nsd = None
77 self.vnfds = dict()
78 self.local_docker_files = dict()
79 self.remote_docker_image_urls = dict()
80 self.instances = dict()
81 self.vnfname2num = dict()
82
83 def onboard(self):
84 """
85 Do all steps to prepare this service to be instantiated
86 :return:
87 """
88 # 1. extract the contents of the package and store them in our catalog
89 self._unpack_service_package()
90 # 2. read in all descriptor files
91 self._load_package_descriptor()
92 self._load_nsd()
93 self._load_vnfd()
94 # 3. prepare container images (e.g. download or build Dockerfile)
95 if BUILD_DOCKERFILE:
96 self._load_docker_files()
97 self._build_images_from_dockerfiles()
98 else:
99 self._load_docker_urls()
100 self._pull_predefined_dockerimages()
101 LOG.info("On-boarded service: %r" % self.manifest.get("package_name"))
102
103 def start_service(self):
104 """
105 This methods creates and starts a new service instance.
106 It computes placements, iterates over all VNFDs, and starts
107 each VNFD as a Docker container in the data center selected
108 by the placement algorithm.
109 :return:
110 """
111 LOG.info("Starting service %r" % self.uuid)
112
113 # 1. each service instance gets a new uuid to identify it
114 instance_uuid = str(uuid.uuid4())
115 # build a instances dict (a bit like a NSR :))
116 self.instances[instance_uuid] = dict()
117 self.instances[instance_uuid]["vnf_instances"] = list()
118
119 # 2. compute placement of this service instance (adds DC names to VNFDs)
120 if not GK_STANDALONE_MODE:
121 self._calculate_placement(FirstDcPlacement)
122 # iterate over all vnfds that we have to start
123 for vnfd in self.vnfds.itervalues():
124 vnfi = None
125 if not GK_STANDALONE_MODE:
126 vnfi = self._start_vnfd(vnfd)
127 self.instances[instance_uuid]["vnf_instances"].append(vnfi)
128
129 # 3. Configure the chaining of the network functions (currently only E-Line links supported)
130 nfid2name = defaultdict(lambda :"NotExistingNode",
131 reduce(lambda x,y: dict(x, **y),
132 map(lambda d:{d["vnf_id"]:d["vnf_name"]},
133 self.nsd["network_functions"])))
134
135 vlinks = self.nsd["virtual_links"]
136 fwd_links = self.nsd["forwarding_graphs"][0]["constituent_virtual_links"]
137 eline_fwd_links = [l for l in vlinks if (l["id"] in fwd_links) and (l["connectivity_type"] == "E-Line")]
138
139 cookie = 1 # not clear why this is needed - to check with Steven
140 for link in eline_fwd_links:
141 src_node, src_port = link["connection_points_reference"][0].split(":")
142 dst_node, dst_port = link["connection_points_reference"][1].split(":")
143
144 srcname = nfid2name[src_node]
145 dstname = nfid2name[dst_node]
146 LOG.debug("src name: "+srcname+" dst name: "+dstname)
147
148 if (srcname in self.vnfds) and (dstname in self.vnfds) :
149 network = self.vnfds[srcname].get("dc").net # there should be a cleaner way to find the DCNetwork
150 src_vnf = self.vnfname2num[srcname]
151 dst_vnf = self.vnfname2num[dstname]
152 ret = network.setChain(src_vnf, dst_vnf, vnf_src_interface=src_port, vnf_dst_interface=dst_port, bidirectional = True, cmd="add-flow", cookie = cookie)
153 cookie += 1
154
155 # 4. run the emulator specific entrypoint scripts in the VNFIs of this service instance
156 self._trigger_emulator_start_scripts_in_vnfis(self.instances[instance_uuid]["vnf_instances"])
157
158 LOG.info("Service started. Instance id: %r" % instance_uuid)
159 return instance_uuid
160
161 def _start_vnfd(self, vnfd):
162 """
163 Start a single VNFD of this service
164 :param vnfd: vnfd descriptor dict
165 :return:
166 """
167 # iterate over all deployment units within each VNFDs
168 for u in vnfd.get("virtual_deployment_units"):
169 # 1. get the name of the docker image to start and the assigned DC
170 vnf_name = vnfd.get("name")
171 if vnf_name not in self.remote_docker_image_urls:
172 raise Exception("No image name for %r found. Abort." % vnf_name)
173 docker_name = self.remote_docker_image_urls.get(vnf_name)
174 target_dc = vnfd.get("dc")
175 # 2. perform some checks to ensure we can start the container
176 assert(docker_name is not None)
177 assert(target_dc is not None)
178 if not self._check_docker_image_exists(docker_name):
179 raise Exception("Docker image %r not found. Abort." % docker_name)
180 # 3. do the dc.startCompute(name="foobar") call to run the container
181 # TODO consider flavors, and other annotations
182 intfs = vnfd.get("connection_points")
183 self.vnfname2num[vnf_name] = GK.get_next_vnf_name()
184 LOG.info("VNF "+vnf_name+" mapped to "+self.vnfname2num[vnf_name]+" on dc "+str(vnfd.get("dc")))
185 vnfi = target_dc.startCompute(self.vnfname2num[vnf_name], network=intfs, image=docker_name, flavor_name="small")
186 return vnfi
187
188 def _trigger_emulator_start_scripts_in_vnfis(self, vnfi_list):
189 for vnfi in vnfi_list:
190 config = vnfi.dcinfo.get("Config", dict())
191 env = config.get("Env", list())
192 for env_var in env:
193 if "SON_EMU_CMD=" in env_var:
194 cmd = str(env_var.split("=")[1])
195 LOG.info("Executing entrypoint script in %r: %r" % (vnfi.name, cmd))
196 vnfi.cmdPrint(cmd)
197
198 def _unpack_service_package(self):
199 """
200 unzip *.son file and store contents in CATALOG_FOLDER/services/<service_uuid>/
201 """
202 LOG.info("Unzipping: %r" % self.package_file_path)
203 with zipfile.ZipFile(self.package_file_path, "r") as z:
204 z.extractall(self.package_content_path)
205
206
207 def _load_package_descriptor(self):
208 """
209 Load the main package descriptor YAML and keep it as dict.
210 :return:
211 """
212 self.manifest = load_yaml(
213 os.path.join(
214 self.package_content_path, "META-INF/MANIFEST.MF"))
215
216 def _load_nsd(self):
217 """
218 Load the entry NSD YAML and keep it as dict.
219 :return:
220 """
221 if "entry_service_template" in self.manifest:
222 nsd_path = os.path.join(
223 self.package_content_path,
224 make_relative_path(self.manifest.get("entry_service_template")))
225 self.nsd = load_yaml(nsd_path)
226 LOG.debug("Loaded NSD: %r" % self.nsd.get("name"))
227
228 def _load_vnfd(self):
229 """
230 Load all VNFD YAML files referenced in MANIFEST.MF and keep them in dict.
231 :return:
232 """
233 if "package_content" in self.manifest:
234 for pc in self.manifest.get("package_content"):
235 if pc.get("content-type") == "application/sonata.function_descriptor":
236 vnfd_path = os.path.join(
237 self.package_content_path,
238 make_relative_path(pc.get("name")))
239 vnfd = load_yaml(vnfd_path)
240 self.vnfds[vnfd.get("name")] = vnfd
241 LOG.debug("Loaded VNFD: %r" % vnfd.get("name"))
242
243 def _load_docker_files(self):
244 """
245 Get all paths to Dockerfiles from VNFDs and store them in dict.
246 :return:
247 """
248 for k, v in self.vnfds.iteritems():
249 for vu in v.get("virtual_deployment_units"):
250 if vu.get("vm_image_format") == "docker":
251 vm_image = vu.get("vm_image")
252 docker_path = os.path.join(
253 self.package_content_path,
254 make_relative_path(vm_image))
255 self.local_docker_files[k] = docker_path
256 LOG.debug("Found Dockerfile (%r): %r" % (k, docker_path))
257
258 def _load_docker_urls(self):
259 """
260 Get all URLs to pre-build docker images in some repo.
261 :return:
262 """
263 for k, v in self.vnfds.iteritems():
264 for vu in v.get("virtual_deployment_units"):
265 if vu.get("vm_image_format") == "docker":
266 url = vu.get("vm_image")
267 if url is not None:
268 url = url.replace("http://", "")
269 self.remote_docker_image_urls[k] = url
270 LOG.debug("Found Docker image URL (%r): %r" % (k, self.remote_docker_image_urls[k]))
271
272 def _build_images_from_dockerfiles(self):
273 """
274 Build Docker images for each local Dockerfile found in the package: self.local_docker_files
275 """
276 if GK_STANDALONE_MODE:
277 return # do not build anything in standalone mode
278 dc = DockerClient()
279 LOG.info("Building %d Docker images (this may take several minutes) ..." % len(self.local_docker_files))
280 for k, v in self.local_docker_files.iteritems():
281 for line in dc.build(path=v.replace("Dockerfile", ""), tag=k, rm=False, nocache=False):
282 LOG.debug("DOCKER BUILD: %s" % line)
283 LOG.info("Docker image created: %s" % k)
284
285 def _pull_predefined_dockerimages(self):
286 """
287 If the package contains URLs to pre-build Docker images, we download them with this method.
288 """
289 dc = DockerClient()
290 for url in self.remote_docker_image_urls.itervalues():
291 if not FORCE_PULL: # only pull if not present (speedup for development)
292 if len(dc.images(name=url)) > 0:
293 LOG.debug("Image %r present. Skipping pull." % url)
294 continue
295 LOG.info("Pulling image: %r" % url)
296 dc.pull(url,
297 insecure_registry=True)
298
299 def _check_docker_image_exists(self, image_name):
300 """
301 Query the docker service and check if the given image exists
302 :param image_name: name of the docker image
303 :return:
304 """
305 return len(DockerClient().images(image_name)) > 0
306
307 def _calculate_placement(self, algorithm):
308 """
309 Do placement by adding the a field "dc" to
310 each VNFD that points to one of our
311 data center objects known to the gatekeeper.
312 """
313 assert(len(self.vnfds) > 0)
314 assert(len(GK.dcs) > 0)
315 # instantiate algorithm an place
316 p = algorithm()
317 p.place(self.nsd, self.vnfds, GK.dcs)
318 LOG.info("Using placement algorithm: %r" % p.__class__.__name__)
319 # lets print the placement result
320 for name, vnfd in self.vnfds.iteritems():
321 LOG.info("Placed VNF %r on DC %r" % (name, str(vnfd.get("dc"))))
322
323
324 """
325 Some (simple) placement algorithms
326 """
327
328
329 class FirstDcPlacement(object):
330 """
331 Placement: Always use one and the same data center from the GK.dcs dict.
332 """
333 def place(self, nsd, vnfds, dcs):
334 for name, vnfd in vnfds.iteritems():
335 vnfd["dc"] = list(dcs.itervalues())[0]
336
337
338 """
339 Resource definitions and API endpoints
340 """
341
342
343 class Packages(fr.Resource):
344
345 def post(self):
346 """
347 Upload a *.son service package to the dummy gatekeeper.
348
349 We expect request with a *.son file and store it in UPLOAD_FOLDER
350 :return: UUID
351 """
352 try:
353 # get file contents
354 print(request.files)
355 # lets search for the package in the request
356 if "package" in request.files:
357 son_file = request.files["package"]
358 # elif "file" in request.files:
359 # son_file = request.files["file"]
360 else:
361 return {"service_uuid": None, "size": 0, "sha1": None, "error": "upload failed. file not found."}, 500
362 # generate a uuid to reference this package
363 service_uuid = str(uuid.uuid4())
364 file_hash = hashlib.sha1(str(son_file)).hexdigest()
365 # ensure that upload folder exists
366 ensure_dir(UPLOAD_FOLDER)
367 upload_path = os.path.join(UPLOAD_FOLDER, "%s.son" % service_uuid)
368 # store *.son file to disk
369 son_file.save(upload_path)
370 size = os.path.getsize(upload_path)
371 # create a service object and register it
372 s = Service(service_uuid, file_hash, upload_path)
373 GK.register_service_package(service_uuid, s)
374 # generate the JSON result
375 return {"service_uuid": service_uuid, "size": size, "sha1": file_hash, "error": None}
376 except Exception as ex:
377 LOG.exception("Service package upload failed:")
378 return {"service_uuid": None, "size": 0, "sha1": None, "error": "upload failed"}, 500
379
380 def get(self):
381 """
382 Return a list of UUID's of uploaded service packages.
383 :return: dict/list
384 """
385 return {"service_uuid_list": list(GK.services.iterkeys())}
386
387
388 class Instantiations(fr.Resource):
389
390 def post(self):
391 """
392 Instantiate a service specified by its UUID.
393 Will return a new UUID to identify the running service instance.
394 :return: UUID
395 """
396 # try to extract the service uuid from the request
397 json_data = request.get_json(force=True)
398 service_uuid = json_data.get("service_uuid")
399
400 # lets be a bit fuzzy here to make testing easier
401 if service_uuid is None and len(GK.services) > 0:
402 # if we don't get a service uuid, we simple start the first service in the list
403 service_uuid = list(GK.services.iterkeys())[0]
404
405 if service_uuid in GK.services:
406 # ok, we have a service uuid, lets start the service
407 service_instance_uuid = GK.services.get(service_uuid).start_service()
408 return {"service_instance_uuid": service_instance_uuid}
409 return "Service not found", 404
410
411 def get(self):
412 """
413 Returns a list of UUIDs containing all running services.
414 :return: dict / list
415 """
416 return {"service_instance_list": [
417 list(s.instances.iterkeys()) for s in GK.services.itervalues()]}
418
419
420 # create a single, global GK object
421 GK = Gatekeeper()
422 # setup Flask
423 app = Flask(__name__)
424 app.config['MAX_CONTENT_LENGTH'] = 512 * 1024 * 1024 # 512 MB max upload
425 api = fr.Api(app)
426 # define endpoints
427 api.add_resource(Packages, '/packages')
428 api.add_resource(Instantiations, '/instantiations')
429
430
431 def start_rest_api(host, port, datacenters=dict()):
432 GK.dcs = datacenters
433 # start the Flask server (not the best performance but ok for our use case)
434 app.run(host=host,
435 port=port,
436 debug=True,
437 use_reloader=False # this is needed to run Flask in a non-main thread
438 )
439
440
441 def ensure_dir(name):
442 if not os.path.exists(name):
443 os.makedirs(name)
444
445
446 def load_yaml(path):
447 with open(path, "r") as f:
448 try:
449 r = yaml.load(f)
450 except yaml.YAMLError as exc:
451 LOG.exception("YAML parse error")
452 r = dict()
453 return r
454
455
456 def make_relative_path(path):
457 if path.startswith("file://"):
458 path = path.replace("file://", "", 1)
459 if path.startswith("/"):
460 path = path.replace("/", "", 1)
461 return path
462
463
464 if __name__ == '__main__':
465 """
466 Lets allow to run the API in standalone mode.
467 """
468 GK_STANDALONE_MODE = True
469 logging.getLogger("werkzeug").setLevel(logging.INFO)
470 start_rest_api("0.0.0.0", 8000)
471