2 Copyright (c) 2015 SONATA-NFV and Paderborn University
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 This module implements a simple REST API that behaves like SONATA's gatekeeper.
31 It is only used to support the development of SONATA's SDK tools and to demonstrate
32 the year 1 version of the emulator until the integration with WP4's orchestrator is done.
42 from docker
import DockerClient
, APIClient
43 from flask
import Flask
, request
44 import flask_restful
as fr
45 from collections
import defaultdict
47 from subprocess
import Popen
48 from random
import randint
52 LOG
= logging
.getLogger("sonata-dummy-gatekeeper")
53 LOG
.setLevel(logging
.DEBUG
)
54 logging
.getLogger("werkzeug").setLevel(logging
.WARNING
)
56 GK_STORAGE
= "/tmp/son-dummy-gk/"
57 UPLOAD_FOLDER
= os
.path
.join(GK_STORAGE
, "uploads/")
58 CATALOG_FOLDER
= os
.path
.join(GK_STORAGE
, "catalog/")
60 # Enable Dockerfile build functionality
61 BUILD_DOCKERFILE
= False
63 # flag to indicate that we run without the emulator (only the bare API for integration testing)
64 GK_STANDALONE_MODE
= False
66 # should a new version of an image be pulled even if its available
69 # Automatically deploy SAPs (endpoints) of the service as new containers
70 # Attention: This is not a configuration switch but a global variable! Don't change its default value.
73 # flag to indicate if we use bidirectional forwarding rules in the automatic chaining process
74 BIDIRECTIONAL_CHAIN
= False
78 def generate_subnets(prefix
, base
, subnet_size
=50, mask
=24):
79 # Generate a list of ipaddress in subnets
81 for net
in range(base
, base
+ subnet_size
):
82 subnet
= "{0}.{1}.0/{2}".format(prefix
, net
, mask
)
83 r
.append(ipaddress
.ip_network(unicode(subnet
)))
85 # private subnet definitions for the generated interfaces
87 SAP_SUBNETS
= generate_subnets('10.10', 0, subnet_size
=50, mask
=24)
89 ELAN_SUBNETS
= generate_subnets('10.20', 0, subnet_size
=50, mask
=24)
91 ELINE_SUBNETS
= generate_subnets('10.30', 0, subnet_size
=50, mask
=30)
96 class Gatekeeper(object):
99 self
.services
= dict()
102 self
.vnf_counter
= 0 # used to generate short names for VNFs (Mininet limitation)
103 LOG
.info("Create SONATA dummy gatekeeper.")
105 def register_service_package(self
, service_uuid
, service
):
107 register new service package
109 :param service object
111 self
.services
[service_uuid
] = service
112 # lets perform all steps needed to onboard the service
115 def get_next_vnf_name(self
):
116 self
.vnf_counter
+= 1
117 return "vnf%d" % self
.vnf_counter
120 class Service(object):
122 This class represents a NS uploaded as a *.son package to the
124 Can have multiple running instances of this service.
131 self
.uuid
= service_uuid
132 self
.package_file_hash
= package_file_hash
133 self
.package_file_path
= package_file_path
134 self
.package_content_path
= os
.path
.join(CATALOG_FOLDER
, "services/%s" % self
.uuid
)
139 self
.saps_ext
= list()
140 self
.saps_int
= list()
141 self
.local_docker_files
= dict()
142 self
.remote_docker_image_urls
= dict()
143 self
.instances
= dict()
144 self
.vnf_name2docker_name
= dict()
145 self
.vnf_id2vnf_name
= dict()
149 Do all steps to prepare this service to be instantiated
152 # 1. extract the contents of the package and store them in our catalog
153 self
._unpack
_service
_package
()
154 # 2. read in all descriptor files
155 self
._load
_package
_descriptor
()
160 # create dict to translate vnf names
161 self
.vnf_id2vnf_name
= defaultdict(lambda: "NotExistingNode",
162 reduce(lambda x
, y
: dict(x
, **y
),
163 map(lambda d
: {d
["vnf_id"]: d
["vnf_name"]},
164 self
.nsd
["network_functions"])))
165 # 3. prepare container images (e.g. download or build Dockerfile)
167 self
._load
_docker
_files
()
168 self
._build
_images
_from
_dockerfiles
()
170 self
._load
_docker
_urls
()
171 self
._pull
_predefined
_dockerimages
()
172 LOG
.info("On-boarded service: %r" % self
.manifest
.get("name"))
174 def start_service(self
):
176 This methods creates and starts a new service instance.
177 It computes placements, iterates over all VNFDs, and starts
178 each VNFD as a Docker container in the data center selected
179 by the placement algorithm.
182 LOG
.info("Starting service %r" % self
.uuid
)
184 # 1. each service instance gets a new uuid to identify it
185 instance_uuid
= str(uuid
.uuid4())
186 # build a instances dict (a bit like a NSR :))
187 self
.instances
[instance_uuid
] = dict()
188 self
.instances
[instance_uuid
]["vnf_instances"] = list()
190 # 2. compute placement of this service instance (adds DC names to VNFDs)
191 if not GK_STANDALONE_MODE
:
192 #self._calculate_placement(FirstDcPlacement)
193 self
._calculate
_placement
(RoundRobinDcPlacementWithSAPs
)
195 # 3. start all vnfds that we have in the service (except SAPs)
196 for vnfd
in self
.vnfds
.itervalues():
198 if not GK_STANDALONE_MODE
:
199 vnfi
= self
._start
_vnfd
(vnfd
)
200 self
.instances
[instance_uuid
]["vnf_instances"].append(vnfi
)
202 # 4. start all SAPs in the service
203 for sap
in self
.saps
:
204 self
._start
_sap
(self
.saps
[sap
], instance_uuid
)
206 # 5. Deploy E-Line and E_LAN links
207 if "virtual_links" in self
.nsd
:
208 vlinks
= self
.nsd
["virtual_links"]
209 # constituent virtual links are not checked
210 #fwd_links = self.nsd["forwarding_graphs"][0]["constituent_virtual_links"]
211 eline_fwd_links
= [l
for l
in vlinks
if (l
["connectivity_type"] == "E-Line")]
212 elan_fwd_links
= [l
for l
in vlinks
if (l
["connectivity_type"] == "E-LAN")]
214 GK
.net
.deployed_elines
.extend(eline_fwd_links
)
215 GK
.net
.deployed_elans
.extend(elan_fwd_links
)
217 # 5a. deploy E-Line links
218 self
._connect
_elines
(eline_fwd_links
, instance_uuid
)
220 # 5b. deploy E-LAN links
221 self
._connect
_elans
(elan_fwd_links
, instance_uuid
)
223 # 6. run the emulator specific entrypoint scripts in the VNFIs of this service instance
224 self
._trigger
_emulator
_start
_scripts
_in
_vnfis
(self
.instances
[instance_uuid
]["vnf_instances"])
226 LOG
.info("Service started. Instance id: %r" % instance_uuid
)
229 def stop_service(self
, instance_uuid
):
231 This method stops a running service instance.
232 It iterates over all VNF instances, stopping them each
233 and removing them from their data center.
235 :param instance_uuid: the uuid of the service instance to be stopped
237 LOG
.info("Stopping service %r" % self
.uuid
)
238 # get relevant information
239 # instance_uuid = str(self.uuid.uuid4())
240 vnf_instances
= self
.instances
[instance_uuid
]["vnf_instances"]
242 for v
in vnf_instances
:
245 if not GK_STANDALONE_MODE
:
247 # self._remove_placement(RoundRobinPlacement)
250 # last step: remove the instance from the list of all instances
251 del self
.instances
[instance_uuid
]
253 def _start_vnfd(self
, vnfd
):
255 Start a single VNFD of this service
256 :param vnfd: vnfd descriptor dict
259 # iterate over all deployment units within each VNFDs
260 for u
in vnfd
.get("virtual_deployment_units"):
261 # 1. get the name of the docker image to start and the assigned DC
262 vnf_name
= vnfd
.get("name")
263 if vnf_name
not in self
.remote_docker_image_urls
:
264 raise Exception("No image name for %r found. Abort." % vnf_name
)
265 docker_name
= self
.remote_docker_image_urls
.get(vnf_name
)
266 target_dc
= vnfd
.get("dc")
267 # 2. perform some checks to ensure we can start the container
268 assert(docker_name
is not None)
269 assert(target_dc
is not None)
270 if not self
._check
_docker
_image
_exists
(docker_name
):
271 raise Exception("Docker image %r not found. Abort." % docker_name
)
273 # 3. get the resource limits
274 res_req
= u
.get("resource_requirements")
275 cpu_list
= res_req
.get("cpu").get("cores")
276 if not cpu_list
or len(cpu_list
)==0:
278 cpu_bw
= res_req
.get("cpu").get("cpu_bw")
281 mem_num
= str(res_req
.get("memory").get("size"))
284 mem_unit
= str(res_req
.get("memory").get("size_unit"))
287 mem_limit
= float(mem_num
)
289 mem_limit
=mem_limit
*1024*1024*1024
291 mem_limit
=mem_limit
*1024*1024
293 mem_limit
=mem_limit
*1024
294 mem_lim
= int(mem_limit
)
295 cpu_period
, cpu_quota
= self
._calculate
_cpu
_cfs
_values
(float(cpu_bw
))
297 # 4. do the dc.startCompute(name="foobar") call to run the container
298 # TODO consider flavors, and other annotations
299 intfs
= vnfd
.get("connection_points")
301 # TODO: get all vnf id's from the nsd for this vnfd and use those as dockername
302 # use the vnf_id in the nsd as docker name
303 # so deployed containers can be easily mapped back to the nsd
304 vnf_name2id
= defaultdict(lambda: "NotExistingNode",
305 reduce(lambda x
, y
: dict(x
, **y
),
306 map(lambda d
: {d
["vnf_name"]: d
["vnf_id"]},
307 self
.nsd
["network_functions"])))
308 self
.vnf_name2docker_name
[vnf_name
] = vnf_name2id
[vnf_name
]
310 LOG
.info("Starting %r as %r in DC %r" % (vnf_name
, self
.vnf_name2docker_name
[vnf_name
], vnfd
.get("dc")))
311 LOG
.debug("Interfaces for %r: %r" % (vnf_name
, intfs
))
312 vnfi
= target_dc
.startCompute(self
.vnf_name2docker_name
[vnf_name
], network
=intfs
, image
=docker_name
, flavor_name
="small",
313 cpu_quota
=cpu_quota
, cpu_period
=cpu_period
, cpuset
=cpu_list
, mem_limit
=mem_lim
)
316 def _stop_vnfi(self
, vnfi
):
320 :param vnfi: vnf instance to be stopped
322 # Find the correct datacenter
323 status
= vnfi
.getStatus()
326 LOG
.info("Stopping the vnf instance contained in %r in DC %r" % (status
["name"], dc
))
327 dc
.stopCompute(status
["name"])
329 def _get_vnf_instance(self
, instance_uuid
, name
):
331 Returns the Docker object for the given VNF name (or Docker name).
332 :param instance_uuid: UUID of the service instance to search in.
333 :param name: VNF name or Docker name. We are fuzzy here.
337 if name
in self
.vnf_name2docker_name
:
338 dn
= self
.vnf_name2docker_name
[name
]
339 for vnfi
in self
.instances
[instance_uuid
]["vnf_instances"]:
342 LOG
.warning("No container with name: {0} found.".format(dn
))
346 def _vnf_reconfigure_network(vnfi
, if_name
, net_str
):
348 Reconfigure the network configuration of a specific interface
349 of a running container.
350 :param vnfi: container instance
351 :param if_name: interface name
352 :param net_str: network configuration string, e.g., 1.2.3.4/24
355 intf
= vnfi
.intf(intf
=if_name
)
358 LOG
.debug("Reconfigured network of %s:%s to %r" % (vnfi
.name
, if_name
, net_str
))
360 LOG
.warning("Interface not found: %s:%s. Network reconfiguration skipped." % (vnfi
.name
, if_name
))
363 def _trigger_emulator_start_scripts_in_vnfis(self
, vnfi_list
):
364 for vnfi
in vnfi_list
:
365 config
= vnfi
.dcinfo
.get("Config", dict())
366 env
= config
.get("Env", list())
368 var
, cmd
= map(str.strip
, map(str, env_var
.split('=', 1)))
369 LOG
.debug("%r = %r" % (var
, cmd
))
370 if var
=="SON_EMU_CMD":
371 LOG
.info("Executing entry point script in %r: %r" % (vnfi
.name
, cmd
))
372 # execute command in new thread to ensure that GK is not blocked by VNF
373 t
= threading
.Thread(target
=vnfi
.cmdPrint
, args
=(cmd
,))
377 def _unpack_service_package(self
):
379 unzip *.son file and store contents in CATALOG_FOLDER/services/<service_uuid>/
381 LOG
.info("Unzipping: %r" % self
.package_file_path
)
382 with zipfile
.ZipFile(self
.package_file_path
, "r") as z
:
383 z
.extractall(self
.package_content_path
)
386 def _load_package_descriptor(self
):
388 Load the main package descriptor YAML and keep it as dict.
391 self
.manifest
= load_yaml(
393 self
.package_content_path
, "META-INF/MANIFEST.MF"))
397 Load the entry NSD YAML and keep it as dict.
400 if "entry_service_template" in self
.manifest
:
401 nsd_path
= os
.path
.join(
402 self
.package_content_path
,
403 make_relative_path(self
.manifest
.get("entry_service_template")))
404 self
.nsd
= load_yaml(nsd_path
)
405 GK
.net
.deployed_nsds
.append(self
.nsd
)
407 LOG
.debug("Loaded NSD: %r" % self
.nsd
.get("name"))
409 def _load_vnfd(self
):
411 Load all VNFD YAML files referenced in MANIFEST.MF and keep them in dict.
414 if "package_content" in self
.manifest
:
415 for pc
in self
.manifest
.get("package_content"):
416 if pc
.get("content-type") == "application/sonata.function_descriptor":
417 vnfd_path
= os
.path
.join(
418 self
.package_content_path
,
419 make_relative_path(pc
.get("name")))
420 vnfd
= load_yaml(vnfd_path
)
421 self
.vnfds
[vnfd
.get("name")] = vnfd
422 LOG
.debug("Loaded VNFD: %r" % vnfd
.get("name"))
424 def _load_saps(self
):
425 # create list of all SAPs
426 SAPs
= [p
for p
in self
.nsd
["connection_points"]]
429 # endpoint needed in this service
430 sap_id
, sap_interface
, sap_docker_name
= parse_interface(sap
['id'])
431 # make sure SAP has type set (default internal)
432 sap
["type"] = sap
.get("type", 'internal')
434 # Each Service Access Point (connection_point) in the nsd is an IP address on the host
435 if sap
.get
["type"] == "external":
436 # add to vnfds to calculate placement later on
437 sap_net
= SAP_SUBNETS
.pop(0)
438 self
.saps
[sap_docker_name
] = {"name": sap_docker_name
, "type": "external", "net": sap_net
}
439 # add SAP vnf to list in the NSD so it is deployed later on
440 # each SAP get a unique VNFD and vnf_id in the NSD and custom type (only defined in the dummygatekeeper)
441 self
.nsd
["network_functions"].append(
442 {"vnf_id": sap_docker_name
, "vnf_name": sap_docker_name
, "vnf_type": "sap_ext"})
444 # Each Service Access Point (connection_point) in the nsd is getting its own container (default)
445 elif sap
["type"] == "internal":
446 # add SAP to self.vnfds
447 sapfile
= pkg_resources
.resource_filename(__name__
, "sap_vnfd.yml")
448 sap_vnfd
= load_yaml(sapfile
)
449 sap_vnfd
["connection_points"][0]["id"] = sap_interface
450 sap_vnfd
["name"] = sap_docker_name
451 sap_vnfd
["type"] = "internal"
452 # add to vnfds to calculate placement later on and deploy
453 self
.saps
[sap_docker_name
] = sap_vnfd
454 # add SAP vnf to list in the NSD so it is deployed later on
455 # each SAP get a unique VNFD and vnf_id in the NSD
456 self
.nsd
["network_functions"].append(
457 {"vnf_id": sap_docker_name
, "vnf_name": sap_docker_name
, "vnf_type": "sap_int"})
459 LOG
.debug("Loaded SAP: name: {0}, type: {1}".format(sap_docker_name
, sap
['type']))
462 self
.saps_ext
= [self
.saps
[sap
]['name'] for sap
in self
.saps
if self
.saps
[sap
]["type"] == "external"]
463 self
.saps_int
= [self
.saps
[sap
]['name'] for sap
in self
.saps
if self
.saps
[sap
]["type"] == "internal"]
465 def _start_sap(self
, sap
, instance_uuid
):
466 LOG
.info('start SAP: {0} ,type: {1}'.format(sap
['name'],sap
['type']))
467 if sap
["type"] == "internal":
469 if not GK_STANDALONE_MODE
:
470 vnfi
= self
._start
_vnfd
(sap
)
471 self
.instances
[instance_uuid
]["vnf_instances"].append(vnfi
)
473 elif sap
["type"] == "external":
474 target_dc
= sap
.get("dc")
475 # add interface to dc switch
476 target_dc
.attachExternalSAP(sap
['name'], str(sap
['net']))
478 def _connect_elines(self
, eline_fwd_links
, instance_uuid
):
480 Connect all E-LINE links in the NSD
481 :param eline_fwd_links: list of E-LINE links in the NSD
482 :param: instance_uuid of the service
485 # cookie is used as identifier for the flowrules installed by the dummygatekeeper
486 # eg. different services get a unique cookie for their flowrules
488 for link
in eline_fwd_links
:
489 src_id
, src_if_name
, src_sap_id
= parse_interface(link
["connection_points_reference"][0])
490 dst_id
, dst_if_name
, dst_sap_id
= parse_interface(link
["connection_points_reference"][1])
494 # check if there is a SAP in the link and chain everything together
495 if src_sap_id
in self
.saps
and dst_sap_id
in self
.saps
:
496 LOG
.info('2 SAPs cannot be chained together : {0} - {1}'.format(src_sap_id
, dst_sap_id
))
499 elif src_sap_id
in self
.saps_ext
:
501 src_if_name
= src_sap_id
502 src_name
= self
.vnf_id2vnf_name
[src_id
]
503 dst_name
= self
.vnf_id2vnf_name
[dst_id
]
504 dst_vnfi
= self
._get
_vnf
_instance
(instance_uuid
, dst_name
)
505 if dst_vnfi
is not None:
506 # choose first ip address in sap subnet
507 sap_net
= self
.saps
[src_sap_id
]['net']
508 sap_ip
= "{0}/{1}".format(str(sap_net
[1]), sap_net
.prefixlen
)
509 self
._vnf
_reconfigure
_network
(dst_vnfi
, dst_if_name
, sap_ip
)
512 elif dst_sap_id
in self
.saps_ext
:
514 dst_if_name
= dst_sap_id
515 src_name
= self
.vnf_id2vnf_name
[src_id
]
516 dst_name
= self
.vnf_id2vnf_name
[dst_id
]
517 src_vnfi
= self
._get
_vnf
_instance
(instance_uuid
, src_name
)
518 if src_vnfi
is not None:
519 sap_net
= self
.saps
[dst_sap_id
]['net']
520 sap_ip
= "{0}/{1}".format(str(sap_net
[1]), sap_net
.prefixlen
)
521 self
._vnf
_reconfigure
_network
(src_vnfi
, src_if_name
, sap_ip
)
524 # Link between 2 VNFs
526 # make sure we use the correct sap vnf name
527 if src_sap_id
in self
.saps_int
:
529 if dst_sap_id
in self
.saps_int
:
531 src_name
= self
.vnf_id2vnf_name
[src_id
]
532 dst_name
= self
.vnf_id2vnf_name
[dst_id
]
533 # re-configure the VNFs IP assignment and ensure that a new subnet is used for each E-Link
534 src_vnfi
= self
._get
_vnf
_instance
(instance_uuid
, src_name
)
535 dst_vnfi
= self
._get
_vnf
_instance
(instance_uuid
, dst_name
)
536 if src_vnfi
is not None and dst_vnfi
is not None:
537 eline_net
= ELINE_SUBNETS
.pop(0)
538 ip1
= "{0}/{1}".format(str(eline_net
[1]), eline_net
.prefixlen
)
539 ip2
= "{0}/{1}".format(str(eline_net
[2]), eline_net
.prefixlen
)
540 self
._vnf
_reconfigure
_network
(src_vnfi
, src_if_name
, ip1
)
541 self
._vnf
_reconfigure
_network
(dst_vnfi
, dst_if_name
, ip2
)
546 ret
= GK
.net
.setChain(
548 vnf_src_interface
=src_if_name
, vnf_dst_interface
=dst_if_name
,
549 bidirectional
=BIDIRECTIONAL_CHAIN
, cmd
="add-flow", cookie
=cookie
, priority
=10)
551 "Setting up E-Line link. %s(%s:%s) -> %s(%s:%s)" % (
552 src_name
, src_id
, src_if_name
, dst_name
, dst_id
, dst_if_name
))
555 def _connect_elans(self
, elan_fwd_links
, instance_uuid
):
557 Connect all E-LAN links in the NSD
558 :param elan_fwd_links: list of E-LAN links in the NSD
559 :param: instance_uuid of the service
562 for link
in elan_fwd_links
:
566 # check if an external SAP is in the E-LAN (then a subnet is already defined)
567 intfs_elan
= [intf
for intf
in link
["connection_points_reference"]]
568 lan_sap
= self
.check_ext_saps(intfs_elan
)
570 lan_net
= self
.saps
[lan_sap
]['net']
571 lan_hosts
= list(lan_net
.hosts())
572 sap_ip
= str(lan_hosts
.pop(0))
574 lan_net
= ELAN_SUBNETS
.pop(0)
575 lan_hosts
= list(lan_net
.hosts())
577 # generate lan ip address for all interfaces except external SAPs
578 for intf
in link
["connection_points_reference"]:
580 # skip external SAPs, they already have an ip
581 vnf_id
, vnf_interface
, vnf_sap_docker_name
= parse_interface(intf
)
582 if vnf_sap_docker_name
in self
.saps_ext
:
583 elan_vnf_list
.append({'name': vnf_sap_docker_name
, 'interface': vnf_interface
})
586 ip_address
= "{0}/{1}".format(str(lan_hosts
.pop(0)), lan_net
.prefixlen
)
587 vnf_id
, intf_name
, vnf_sap_id
= parse_interface(intf
)
589 # make sure we use the correct sap vnf name
590 src_docker_name
= vnf_id
591 if vnf_sap_id
in self
.saps_int
:
592 src_docker_name
= vnf_sap_id
595 vnf_name
= self
.vnf_id2vnf_name
[vnf_id
]
597 "Setting up E-LAN link. %s(%s:%s) -> %s" % (
598 vnf_name
, vnf_id
, intf_name
, ip_address
))
600 if vnf_name
in self
.vnfds
:
601 # re-configure the VNFs IP assignment and ensure that a new subnet is used for each E-LAN
602 # E-LAN relies on the learning switch capability of Ryu which has to be turned on in the topology
603 # (DCNetwork(controller=RemoteController, enable_learning=True)), so no explicit chaining is necessary.
604 vnfi
= self
._get
_vnf
_instance
(instance_uuid
, vnf_name
)
606 self
._vnf
_reconfigure
_network
(vnfi
, intf_name
, ip_address
)
607 # add this vnf and interface to the E-LAN for tagging
608 elan_vnf_list
.append({'name': src_docker_name
, 'interface': intf_name
})
610 # install the VLAN tags for this E-LAN
611 GK
.net
.setLAN(elan_vnf_list
)
614 def _load_docker_files(self
):
616 Get all paths to Dockerfiles from VNFDs and store them in dict.
619 for k
, v
in self
.vnfds
.iteritems():
620 for vu
in v
.get("virtual_deployment_units"):
621 if vu
.get("vm_image_format") == "docker":
622 vm_image
= vu
.get("vm_image")
623 docker_path
= os
.path
.join(
624 self
.package_content_path
,
625 make_relative_path(vm_image
))
626 self
.local_docker_files
[k
] = docker_path
627 LOG
.debug("Found Dockerfile (%r): %r" % (k
, docker_path
))
629 def _load_docker_urls(self
):
631 Get all URLs to pre-build docker images in some repo.
634 # also merge sap dicts, because internal saps also need a docker container
635 all_vnfs
= self
.vnfds
.copy()
636 all_vnfs
.update(self
.saps
)
638 for k
, v
in all_vnfs
.iteritems():
639 for vu
in v
.get("virtual_deployment_units", {}):
640 if vu
.get("vm_image_format") == "docker":
641 url
= vu
.get("vm_image")
643 url
= url
.replace("http://", "")
644 self
.remote_docker_image_urls
[k
] = url
645 LOG
.debug("Found Docker image URL (%r): %r" % (k
, self
.remote_docker_image_urls
[k
]))
647 def _build_images_from_dockerfiles(self
):
649 Build Docker images for each local Dockerfile found in the package: self.local_docker_files
651 if GK_STANDALONE_MODE
:
652 return # do not build anything in standalone mode
654 LOG
.info("Building %d Docker images (this may take several minutes) ..." % len(self
.local_docker_files
))
655 for k
, v
in self
.local_docker_files
.iteritems():
656 for line
in dc
.build(path
=v
.replace("Dockerfile", ""), tag
=k
, rm
=False, nocache
=False):
657 LOG
.debug("DOCKER BUILD: %s" % line
)
658 LOG
.info("Docker image created: %s" % k
)
660 def _pull_predefined_dockerimages(self
):
662 If the package contains URLs to pre-build Docker images, we download them with this method.
665 for url
in self
.remote_docker_image_urls
.itervalues():
666 if not FORCE_PULL
: # only pull if not present (speedup for development)
667 if len(dc
.images
.list(name
=url
)) > 0:
668 LOG
.debug("Image %r present. Skipping pull." % url
)
670 LOG
.info("Pulling image: %r" % url
)
671 # this seems to fail with latest docker api version 2.0.2
672 # dc.images.pull(url,
673 # insecure_registry=True)
674 #using docker cli instead
684 def _check_docker_image_exists(self
, image_name
):
686 Query the docker service and check if the given image exists
687 :param image_name: name of the docker image
690 return len(DockerClient().images
.list(name
=image_name
)) > 0
692 def _calculate_placement(self
, algorithm
):
694 Do placement by adding the a field "dc" to
695 each VNFD that points to one of our
696 data center objects known to the gatekeeper.
698 assert(len(self
.vnfds
) > 0)
699 assert(len(GK
.dcs
) > 0)
700 # instantiate algorithm an place
702 p
.place(self
.nsd
, self
.vnfds
, self
.saps
, GK
.dcs
)
703 LOG
.info("Using placement algorithm: %r" % p
.__class
__.__name
__)
704 # lets print the placement result
705 for name
, vnfd
in self
.vnfds
.iteritems():
706 LOG
.info("Placed VNF %r on DC %r" % (name
, str(vnfd
.get("dc"))))
707 for sap
in self
.saps
:
708 sap_dict
= self
.saps
[sap
]
709 LOG
.info("Placed SAP %r on DC %r" % (sap
, str(sap_dict
.get("dc"))))
712 def _calculate_cpu_cfs_values(self
, cpu_time_percentage
):
714 Calculate cpu period and quota for CFS
715 :param cpu_time_percentage: percentage of overall CPU to be used
716 :return: cpu_period, cpu_quota
718 if cpu_time_percentage
is None:
720 if cpu_time_percentage
< 0:
722 # (see: https://www.kernel.org/doc/Documentation/scheduler/sched-bwc.txt)
723 # Attention minimum cpu_quota is 1ms (micro)
724 cpu_period
= 1000000 # lets consider a fixed period of 1000000 microseconds for now
725 LOG
.debug("cpu_period is %r, cpu_percentage is %r" % (cpu_period
, cpu_time_percentage
))
726 cpu_quota
= cpu_period
* cpu_time_percentage
# calculate the fraction of cpu time for this container
727 # ATTENTION >= 1000 to avoid a invalid argument system error ... no idea why
729 LOG
.debug("cpu_quota before correcting: %r" % cpu_quota
)
731 LOG
.warning("Increased CPU quota to avoid system error.")
732 LOG
.debug("Calculated: cpu_period=%f / cpu_quota=%f" % (cpu_period
, cpu_quota
))
733 return int(cpu_period
), int(cpu_quota
)
735 def check_ext_saps(self
, intf_list
):
736 # check if the list of interfacs contains an externl SAP
737 saps_ext
= [self
.saps
[sap
]['name'] for sap
in self
.saps
if self
.saps
[sap
]["type"] == "external"]
738 for intf_name
in intf_list
:
739 vnf_id
, vnf_interface
, vnf_sap_docker_name
= parse_interface(intf_name
)
740 if vnf_sap_docker_name
in saps_ext
:
741 return vnf_sap_docker_name
744 Some (simple) placement algorithms
748 class FirstDcPlacement(object):
750 Placement: Always use one and the same data center from the GK.dcs dict.
752 def place(self
, nsd
, vnfds
, saps
, dcs
):
753 for name
, vnfd
in vnfds
.iteritems():
754 vnfd
["dc"] = list(dcs
.itervalues())[0]
757 class RoundRobinDcPlacement(object):
759 Placement: Distribute VNFs across all available DCs in a round robin fashion.
761 def place(self
, nsd
, vnfds
, saps
, dcs
):
763 dcs_list
= list(dcs
.itervalues())
764 for name
, vnfd
in vnfds
.iteritems():
765 vnfd
["dc"] = dcs_list
[c
% len(dcs_list
)]
766 c
+= 1 # inc. c to use next DC
768 class RoundRobinDcPlacementWithSAPs(object):
770 Placement: Distribute VNFs across all available DCs in a round robin fashion,
771 every SAP is instantiated on the same DC as the connected VNF.
773 def place(self
, nsd
, vnfds
, saps
, dcs
):
777 dcs_list
= list(dcs
.itervalues())
778 for name
, vnfd
in vnfds
.iteritems():
779 vnfd
["dc"] = dcs_list
[c
% len(dcs_list
)]
780 c
+= 1 # inc. c to use next DC
783 vlinks
= nsd
.get("virtual_links", [])
784 eline_fwd_links
= [l
for l
in vlinks
if (l
["connectivity_type"] == "E-Line")]
785 elan_fwd_links
= [l
for l
in vlinks
if (l
["connectivity_type"] == "E-LAN")]
787 vnf_id2vnf_name
= defaultdict(lambda: "NotExistingNode",
788 reduce(lambda x
, y
: dict(x
, **y
),
789 map(lambda d
: {d
["vnf_id"]: d
["vnf_name"]},
790 nsd
["network_functions"])))
792 # SAPs on E-Line links are placed on the same DC as the VNF on the E-Line
793 for link
in eline_fwd_links
:
794 src_id
, src_if_name
, src_sap_id
= parse_interface(link
["connection_points_reference"][0])
795 dst_id
, dst_if_name
, dst_sap_id
= parse_interface(link
["connection_points_reference"][1])
797 # check if there is a SAP in the link
798 if src_sap_id
in saps
:
799 dst_vnf_name
= vnf_id2vnf_name
[dst_id
]
800 # get dc where connected vnf is mapped to
801 dc
= vnfds
[dst_vnf_name
]['dc']
802 saps
[src_sap_id
]['dc'] = dc
804 if dst_sap_id
in saps
:
805 src_vnf_name
= vnf_id2vnf_name
[src_id
]
806 # get dc where connected vnf is mapped to
807 dc
= vnfds
[src_vnf_name
]['dc']
808 saps
[dst_sap_id
]['dc'] = dc
810 # SAPs on E-LANs are placed on a random DC
811 dcs_list
= list(dcs
.itervalues())
812 dc_len
= len(dcs_list
)
813 for link
in elan_fwd_links
:
814 for intf
in link
["connection_points_reference"]:
815 # find SAP interfaces
816 intf_id
, intf_name
, intf_sap_id
= parse_interface(intf
)
817 if intf_sap_id
in saps
:
818 dc
= dcs_list
[randint(0, dc_len
-1)]
819 saps
[intf_id
]['dc'] = dc
824 Resource definitions and API endpoints
828 class Packages(fr
.Resource
):
832 Upload a *.son service package to the dummy gatekeeper.
834 We expect request with a *.son file and store it in UPLOAD_FOLDER
839 LOG
.info("POST /packages called")
840 # lets search for the package in the request
841 is_file_object
= False # make API more robust: file can be in data or in files field
842 if "package" in request
.files
:
843 son_file
= request
.files
["package"]
844 is_file_object
= True
845 elif len(request
.data
) > 0:
846 son_file
= request
.data
848 return {"service_uuid": None, "size": 0, "sha1": None, "error": "upload failed. file not found."}, 500
849 # generate a uuid to reference this package
850 service_uuid
= str(uuid
.uuid4())
851 file_hash
= hashlib
.sha1(str(son_file
)).hexdigest()
852 # ensure that upload folder exists
853 ensure_dir(UPLOAD_FOLDER
)
854 upload_path
= os
.path
.join(UPLOAD_FOLDER
, "%s.son" % service_uuid
)
855 # store *.son file to disk
857 son_file
.save(upload_path
)
859 with
open(upload_path
, 'wb') as f
:
861 size
= os
.path
.getsize(upload_path
)
862 # create a service object and register it
863 s
= Service(service_uuid
, file_hash
, upload_path
)
864 GK
.register_service_package(service_uuid
, s
)
865 # generate the JSON result
866 return {"service_uuid": service_uuid
, "size": size
, "sha1": file_hash
, "error": None}, 201
867 except Exception as ex
:
868 LOG
.exception("Service package upload failed:")
869 return {"service_uuid": None, "size": 0, "sha1": None, "error": "upload failed"}, 500
873 Return a list of UUID's of uploaded service packages.
876 LOG
.info("GET /packages")
877 return {"service_uuid_list": list(GK
.services
.iterkeys())}
880 class Instantiations(fr
.Resource
):
884 Instantiate a service specified by its UUID.
885 Will return a new UUID to identify the running service instance.
888 LOG
.info("POST /instantiations (or /reqeusts) called")
889 # try to extract the service uuid from the request
890 json_data
= request
.get_json(force
=True)
891 service_uuid
= json_data
.get("service_uuid")
893 # lets be a bit fuzzy here to make testing easier
894 if (service_uuid
is None or service_uuid
=="latest") and len(GK
.services
) > 0:
895 # if we don't get a service uuid, we simple start the first service in the list
896 service_uuid
= list(GK
.services
.iterkeys())[0]
897 if service_uuid
in GK
.services
:
898 # ok, we have a service uuid, lets start the service
899 service_instance_uuid
= GK
.services
.get(service_uuid
).start_service()
900 return {"service_instance_uuid": service_instance_uuid
}, 201
901 return "Service not found", 404
905 Returns a list of UUIDs containing all running services.
908 LOG
.info("GET /instantiations")
909 return {"service_instantiations_list": [
910 list(s
.instances
.iterkeys()) for s
in GK
.services
.itervalues()]}
914 Stops a running service specified by its service and instance UUID.
916 # try to extract the service and instance UUID from the request
917 json_data
= request
.get_json(force
=True)
918 service_uuid
= json_data
.get("service_uuid")
919 instance_uuid
= json_data
.get("service_instance_uuid")
922 if service_uuid
is None and len(GK
.services
) > 0:
923 #if we don't get a service uuid, we simply stop the last service in the list
924 service_uuid
= list(GK
.services
.iterkeys())[0]
925 if instance_uuid
is None and len(GK
.services
[service_uuid
].instances
) > 0:
926 instance_uuid
= list(GK
.services
[service_uuid
].instances
.iterkeys())[0]
928 if service_uuid
in GK
.services
and instance_uuid
in GK
.services
[service_uuid
].instances
:
929 # valid service and instance UUID, stop service
930 GK
.services
.get(service_uuid
).stop_service(instance_uuid
)
931 return "service instance with uuid %r stopped." % instance_uuid
,200
932 return "Service not found", 404
934 class Exit(fr
.Resource
):
938 Stop the running Containernet instance regardless of data transmitted
940 list(GK
.dcs
.values())[0].net
.stop()
949 # create a single, global GK object
953 app
= Flask(__name__
)
954 app
.config
['MAX_CONTENT_LENGTH'] = 512 * 1024 * 1024 # 512 MB max upload
957 api
.add_resource(Packages
, '/packages', '/api/v2/packages')
958 api
.add_resource(Instantiations
, '/instantiations', '/api/v2/instantiations', '/api/v2/requests')
959 api
.add_resource(Exit
, '/emulator/exit')
962 #def initialize_GK():
967 def start_rest_api(host
, port
, datacenters
=dict()):
969 GK
.net
= get_dc_network()
970 # start the Flask server (not the best performance but ok for our use case)
974 use_reloader
=False # this is needed to run Flask in a non-main thread
978 def ensure_dir(name
):
979 if not os
.path
.exists(name
):
984 with
open(path
, "r") as f
:
987 except yaml
.YAMLError
as exc
:
988 LOG
.exception("YAML parse error")
993 def make_relative_path(path
):
994 if path
.startswith("file://"):
995 path
= path
.replace("file://", "", 1)
996 if path
.startswith("/"):
997 path
= path
.replace("/", "", 1)
1001 def get_dc_network():
1003 retrieve the DCnetwork where this dummygatekeeper (GK) connects to.
1004 Assume at least 1 datacenter is connected to this GK, and that all datacenters belong to the same DCNetwork
1007 assert (len(GK
.dcs
) > 0)
1008 return GK
.dcs
.values()[0].net
1011 def parse_interface(interface_name
):
1013 convert the interface name in the nsd to the according vnf_id, vnf_interface names
1014 :param interface_name:
1018 if ':' in interface_name
:
1019 vnf_id
, vnf_interface
= interface_name
.split(':')
1020 vnf_sap_docker_name
= interface_name
.replace(':', '_')
1022 vnf_id
= interface_name
1023 vnf_interface
= interface_name
1024 vnf_sap_docker_name
= interface_name
1026 return vnf_id
, vnf_interface
, vnf_sap_docker_name
1028 if __name__
== '__main__':
1030 Lets allow to run the API in standalone mode.
1032 GK_STANDALONE_MODE
= True
1033 logging
.getLogger("werkzeug").setLevel(logging
.INFO
)
1034 start_rest_api("0.0.0.0", 8000)