Added functionality to read an parse all descriptor files from package.
[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 flask import Flask, request
15 import flask_restful as fr
16
17 LOG = logging.getLogger("sonata-dummy-gatekeeper")
18 LOG.setLevel(logging.DEBUG)
19 logging.getLogger("werkzeug").setLevel(logging.WARNING)
20
21
22 UPLOAD_FOLDER = "/tmp/son-dummy-gk/uploads/"
23 CATALOG_FOLDER = "/tmp/son-dummy-gk/catalog/"
24
25
26 class Gatekeeper(object):
27
28 def __init__(self):
29 self.services = dict()
30 LOG.info("Create SONATA dummy gatekeeper.")
31
32 def register_service_package(self, service_uuid, service):
33 """
34 register new service package
35 :param service_uuid
36 :param service object
37 """
38 self.services[service_uuid] = service
39 # lets perform all steps needed to onboard the service
40 service.onboard()
41
42
43 class Service(object):
44 """
45 This class represents a NS uploaded as a *.son package to the
46 dummy gatekeeper.
47 Can have multiple running instances of this service.
48 """
49
50 def __init__(self,
51 service_uuid,
52 package_file_hash,
53 package_file_path):
54 self.uuid = service_uuid
55 self.package_file_hash = package_file_hash
56 self.package_file_path = package_file_path
57 self.package_content_path = os.path.join(CATALOG_FOLDER, "services/%s" % self.uuid)
58 self.manifest = None
59 self.nsd = None
60 self.vnfds = dict()
61 self.docker_files = dict()
62 self.instances = dict()
63
64 def start_service(self, service_uuid):
65 # TODO implement method
66 # 1. parse descriptors
67 # 2. do the corresponding dc.startCompute(name="foobar") calls
68 # 3. store references to the compute objects in self.instantiations
69 pass
70
71 def onboard(self):
72 """
73 Do all steps to prepare this service to be instantiated
74 :return:
75 """
76 # 1. extract the contents of the package and store them in our catalog
77 self._unpack_service_package()
78 # 2. read in all descriptor files
79 self._load_package_descriptor()
80 self._load_nsd()
81 self._load_vnfd()
82 self._load_docker_files()
83 # 3. prepare container images (e.g. download or build Dockerfile)
84
85 LOG.info("On-boarded service: %r" % self.manifest.get("package_name"))
86
87 def _unpack_service_package(self):
88 """
89 unzip *.son file and store contents in CATALOG_FOLDER/services/<service_uuid>/
90 """
91 with zipfile.ZipFile(self.package_file_path, "r") as z:
92 z.extractall(self.package_content_path)
93
94 def _load_package_descriptor(self):
95 """
96 Load the main package descriptor YAML and keep it as dict.
97 :return:
98 """
99 self.manifest = load_yaml(
100 os.path.join(
101 self.package_content_path, "META-INF/MANIFEST.MF"))
102
103 def _load_nsd(self):
104 """
105 Load the entry NSD YAML and keep it as dict.
106 :return:
107 """
108 if "entry_service_template" in self.manifest:
109 nsd_path = os.path.join(
110 self.package_content_path,
111 make_relative_path(self.manifest.get("entry_service_template")))
112 self.nsd = load_yaml(nsd_path)
113 LOG.debug("Loaded NSD: %r" % self.nsd.get("ns_name"))
114
115 def _load_vnfd(self):
116 """
117 Load all VNFD YAML files referenced in MANIFEST.MF and keep them in dict.
118 :return:
119 """
120 if "package_content" in self.manifest:
121 for pc in self.manifest.get("package_content"):
122 if pc.get("content-type") == "application/sonata.function_descriptor":
123 vnfd_path = os.path.join(
124 self.package_content_path,
125 make_relative_path(pc.get("name")))
126 vnfd = load_yaml(vnfd_path)
127 self.vnfds[vnfd.get("vnf_name")] = vnfd
128 LOG.debug("Loaded VNFD: %r" % vnfd.get("vnf_name"))
129
130 def _load_docker_files(self):
131 """
132 Get all paths to Dockerfiles from MANIFEST.MF and store them in dict.
133 :return:
134 """
135 if "package_content" in self.manifest:
136 for df in self.manifest.get("package_content"):
137 if df.get("content-type") == "application/sonata.docker_files":
138 docker_path = os.path.join(
139 self.package_content_path,
140 make_relative_path(df.get("name")))
141 # FIXME: Mapping to docker image names is hardcoded because of the missing mapping in the example package
142 self.docker_files[helper_map_docker_name(df.get("name"))] = docker_path
143 LOG.debug("Found Dockerfile: %r" % docker_path)
144
145 def _build_images_from_dockerfile(self):
146 pass
147 # TODO implement
148
149
150 """
151 Resource definitions and API endpoints
152 """
153
154
155 class Packages(fr.Resource):
156
157 def post(self):
158 """
159 Upload a *.son service package to the dummy gatekeeper.
160
161 We expect request with a *.son file and store it in UPLOAD_FOLDER
162 :return: UUID
163 """
164 try:
165 # get file contents
166 son_file = request.files['file']
167 # generate a uuid to reference this package
168 service_uuid = str(uuid.uuid4())
169 file_hash = hashlib.sha1(str(son_file)).hexdigest()
170 # ensure that upload folder exists
171 ensure_dir(UPLOAD_FOLDER)
172 upload_path = os.path.join(UPLOAD_FOLDER, "%s.son" % service_uuid)
173 # store *.son file to disk
174 son_file.save(upload_path)
175 size = os.path.getsize(upload_path)
176 # create a service object and register it
177 s = Service(service_uuid, file_hash, upload_path)
178 GK.register_service_package(service_uuid, s)
179 # generate the JSON result
180 return {"service_uuid": service_uuid, "size": size, "sha1": file_hash, "error": None}
181 except Exception as ex:
182 LOG.exception("Service package upload failed:")
183 return {"service_uuid": None, "size": 0, "sha1": None, "error": "upload failed"}
184
185 def get(self):
186 """
187 Return a list of UUID's of uploaded service packages.
188 :return: dict/list
189 """
190 return {"service_uuid_list": list(GK.services.iterkeys())}
191
192
193 class Instantiations(fr.Resource):
194
195 def post(self):
196 """
197 Instantiate a service specified by its UUID.
198 Will return a new UUID to identify the running service instance.
199 :return: UUID
200 """
201 # TODO implement method (start real service)
202 json_data = request.get_json(force=True)
203 service_uuid = json_data.get("service_uuid")
204 if service_uuid is not None:
205 service_instance_uuid = str(uuid.uuid4())
206 LOG.info("Starting service %r" % service_uuid)
207 return {"service_instance_uuid": service_instance_uuid}
208 return None
209
210 def get(self):
211 """
212 Returns a list of UUIDs containing all running services.
213 :return: dict / list
214 """
215 # TODO implement method
216 return {"service_instance_uuid_list": list()}
217
218
219 # create a single, global GK object
220 GK = Gatekeeper()
221 # setup Flask
222 app = Flask(__name__)
223 app.config['MAX_CONTENT_LENGTH'] = 512 * 1024 * 1024 # 512 MB max upload
224 api = fr.Api(app)
225 # define endpoints
226 api.add_resource(Packages, '/api/packages')
227 api.add_resource(Instantiations, '/api/instantiations')
228
229
230 def start_rest_api(host, port):
231 # start the Flask server (not the best performance but ok for our use case)
232 app.run(host=host,
233 port=port,
234 debug=True,
235 use_reloader=False # this is needed to run Flask in a non-main thread
236 )
237
238
239 def ensure_dir(name):
240 if not os.path.exists(name):
241 os.makedirs(name)
242
243
244 def load_yaml(path):
245 with open(path, "r") as f:
246 try:
247 r = yaml.load(f)
248 except yaml.YAMLError as exc:
249 LOG.exception("YAML parse error")
250 r = dict()
251 return r
252
253
254 def make_relative_path(path):
255 if path.startswith("/"):
256 return path.replace("/", "", 1)
257 return path
258
259
260 def helper_map_docker_name(name):
261 """
262 Quick hack to fix missing dependency in example package.
263 """
264 # TODO remove this when package description is fixed
265 mapping = {
266 "/docker_files/iperf/Dockerfile": "iperf_docker",
267 "/docker_files/firewall/Dockerfile": "fw_docker",
268 "/docker_files/tcpdump/Dockerfile": "tcpdump_docker"
269 }
270 return mapping.get(name)
271
272
273 if __name__ == '__main__':
274 """
275 Lets allow to run the API in standalone mode.
276 """
277 logging.getLogger("werkzeug").setLevel(logging.INFO)
278 start_rest_api("0.0.0.0", 8000)
279