-
Notifications
You must be signed in to change notification settings - Fork 0
/
orchestrator.py
1771 lines (1607 loc) · 85.1 KB
/
orchestrator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import networkx as nx
from graph_classes import ServiceGraph, VNF, VirtualLink, SAP, PhysicalNode, ResourceGraph, Fog, PhysicalLink
import topology_gen
# from simulator import Simulator as dark
import dark
import copy
import random
import simulator
class NoCoreable(Exception):
def __init__(self, msg):
self.message = msg
def __str__(self):
return repr(self.value)
class NoNodeInSameFog(Exception):
def __init__(self, msg):
self.message = msg
def __str__(self):
return repr(self.value)
class NoCompatibleFog(Exception):
def __init__(self, msg):
self.message = msg
def __str__(self):
return repr(self.value)
class MigratingNotPossible(Exception):
def __init__(self, msg):
self.message = msg
def __str__(self):
return repr(self.value)
class DARKOrchestrator:
"""
"""
def __init__(self, resource_graph=None, delay_matrix=None, migrate_cost=1, alpha=1, cpu_limit=10,
rollback_level=10):
"""
:param resource_graph:
:type resource_graph: ResourceGraph
"""
self.migrate_cost = migrate_cost
if resource_graph is None:
resource_graph = topology_gen.generate_topology()
self.__resource_graph = resource_graph
self._running = copy.deepcopy(self.__resource_graph)
self.__previous_resource_graph = None
self._delay_matrix = delay_matrix
self._core_network = None
self._core_clouds = []
for r in self._running.nodes:
if r.type == "core_network":
self._core_network = r
if r.type == "core_cloud":
self._core_clouds.append(r)
self._previous_mappings = []
self.service_graphs = []
self._actual_service_graph = None
self.alpha = alpha
self.expense = 0
self.cpu_limit = cpu_limit
self.rollback_level = rollback_level
self.mig_expense = 0
self.strategy = 0
# TODO: Should come from config
self.corable_delay_limit = 100
def MAP(self, service_graph, disable_migrating, choose_strategy=0):
"""
:param service_graph:
:type service_graph: ServiceGraph
:return:
"""
self.strategy = choose_strategy
self._running = copy.deepcopy(self.__resource_graph)
self._core_clouds = []
for r in self._running.nodes:
if r.type == "core_network":
self._core_network = r
if r.type == "core_cloud":
self._core_clouds.append(r)
everything_ok = True
self._actual_service_graph = service_graph
# service_graph backup
service_graphs_backup = copy.deepcopy(self.service_graphs)
self.service_graphs.append(service_graph)
mapping = {'service_id': service_graph.id, 'mapping': []}
mapped_vnodes = []
self.expense = 0
self.mig_expense = 0
map_list = self._order_service_graph(service_graph)
mapped_vnodes.append(next(x[0] if isinstance(x[0], SAP) else x[1] for x in map_list).id)
i = 0
rollback_num = 0
need_migrate = False
max_i = 0
allow_migrate = False
changed_sgs = []
while i < len(map_list):
previous_element, min_delay_link, actual_element, retry = map_list[i]
service_graph = next(x for x in self.service_graphs if x.id == service_graph.id)
self._actual_service_graph = service_graph
if not isinstance(previous_element, SAP):
previous_element = self._get_vnf_from_id(previous_element.id)
if previous_element != map_list[i][0]:
tuple_to_list = list(map_list[i])
tuple_to_list[0] = previous_element
map_list[i] = tuple(tuple_to_list)
if not isinstance(actual_element, SAP):
actual_element = self._get_vnf_from_id(actual_element.id)
if actual_element != map_list[i][2]:
tuple_to_list = list(map_list[i])
tuple_to_list[2] = actual_element
map_list[i] = tuple(tuple_to_list)
if actual_element.mapped_to is not None:
mapped_vnodes.append(actual_element.id)
for i in range(len(service_graph.VNFS)):
if service_graph.VNFS[i].id == actual_element.id:
service_graph.VNFS[i] = actual_element
if min_delay_link.node1 in mapped_vnodes and min_delay_link.node2 in mapped_vnodes:
if isinstance(previous_element, SAP):
previous_element_host = previous_element.id
else:
previous_element_host = previous_element.mapped_to
if isinstance(actual_element, SAP):
actual_element_host = actual_element.id
else:
actual_element_host = actual_element.mapped_to
length, path = self._get_shortest_path_and_length(previous_element_host, actual_element_host)
if min_delay_link.required_delay < self.corable_delay_limit:
for prev_map in self._previous_mappings:
if prev_map['service_id'] == actual_element.service_graph:
for y in prev_map['mapping']:
if y['vnf'].id == actual_element.id:
y['coreable'] = False
# FIXME: Bandwidth check
if length <= min_delay_link.required_delay:
self._map_virtual_link(min_delay_link, service_graph)
else:
need_migrate = True
allow_migrate = True
else:
if isinstance(actual_element, VNF):
try:
vnf_mapping = self._map_vnf(previous_element, actual_element, min_delay_link, retry)
self._map_virtual_link(min_delay_link, service_graph)
mapping['mapping'].append(vnf_mapping)
mapped_vnodes.append(actual_element.id)
except Exception as e:
if 'RETRY' not in e.message and 'COMPATIBLE NODE' not in e.message:
dark.log(e.message, 'ERROR')
# ROLLBACK OR MIGRATE
if 'RETRY' in e.message and not isinstance(previous_element, SAP):
t = list(map_list[i])
t[3] = 0
map_list[i] = tuple(t)
for j in range(len(map_list)):
if map_list[j][2].id == previous_element.id:
if isinstance(map_list[j][0], SAP):
need_migrate = True
if i != max_i:
for k in range(i, -1, -1):
self._increase_bandwidth_previous_links(map_list[k][0],
map_list[k][2])
if k > 0:
self._clear_previous_mapping(map_list[k][0], mapping)
i = -1
mapped_vnodes = [self._actual_service_graph.saps[0].id]
else:
allow_migrate = True
elif (not need_migrate or i != max_i) and rollback_num < self.rollback_level:
self._increase_bandwidth_previous_links(previous_element, actual_element)
self._clear_previous_mapping(previous_element, mapping)
t = list(map_list[j])
t[3] += 1
map_list[j] = tuple(t)
i = j - 1
if previous_element.id in mapped_vnodes:
mapped_vnodes.remove(previous_element.id)
rollback_num += 1
else:
need_migrate = True
if i != max_i:
for k in range(i, -1, -1):
self._increase_bandwidth_previous_links(map_list[k][0],
map_list[k][2])
if k > 0:
self._clear_previous_mapping(map_list[k][0], mapping)
i = -1
mapped_vnodes = [self._actual_service_graph.saps[0].id]
else:
allow_migrate = True
break
elif not isinstance(previous_element, SAP):
succ_put_away = self._try_put_away_previous_vnf(previous_element, actual_element, mapping,
min_delay_link)
if succ_put_away:
try:
vnf_mapping = self._map_vnf(previous_element, actual_element, min_delay_link, retry)
self._map_virtual_link(min_delay_link, service_graph)
mapping['mapping'].append(vnf_mapping)
mapped_vnodes.append(actual_element.id)
except:
succ_put_away = False
if not succ_put_away:
if rollback_num < self.rollback_level:
for j in range(len(map_list)):
if map_list[j][2].id == previous_element.id:
if isinstance(map_list[j][0], SAP):
need_migrate = True
if i != max_i:
for k in range(i, -1, -1):
t = list(map_list[k])
t[3] = 0
map_list[k] = tuple(t)
self._increase_bandwidth_previous_links(map_list[k][0],
map_list[k][2])
if k > 0:
self._clear_previous_mapping(map_list[k][0], mapping)
i = -1
mapped_vnodes = [self._actual_service_graph.saps[0].id]
else:
allow_migrate = True
else:
self._increase_bandwidth_previous_links(previous_element,
actual_element)
self._clear_previous_mapping(previous_element, mapping)
t = list(map_list[j])
t[3] += 1
map_list[j] = tuple(t)
i = j - 1
if previous_element.id in mapped_vnodes:
mapped_vnodes.remove(previous_element.id)
rollback_num += 1
break
else:
need_migrate = True
allow_migrate = True
else:
need_migrate = True
if i != max_i:
for k in range(i, -1, -1):
t = list(map_list[k])
t[3] = 0
map_list[k] = tuple(t)
self._increase_bandwidth_previous_links(map_list[k][0],
map_list[k][2])
if k > 0:
self._clear_previous_mapping(map_list[k][0], mapping)
i = -1
mapped_vnodes = [self._actual_service_graph.saps[0].id]
else:
allow_migrate = True
else:
# THIS MEANS THE ACTUAL ELEMENT IS A SAP
phy_node = self._get_phy_node_from_id(actual_element.id)
l, p = self._get_shortest_path_and_length(self._get_phy_node_from_id(previous_element.mapped_to),
phy_node)
l = self._delay_matrix[
self._get_fog_from_phy_node(self._get_phy_node_from_id(previous_element.mapped_to)).id][
self._get_fog_from_phy_node(phy_node).id]
# TODO: BANDWIDTH CHECK
if l > min_delay_link.required_delay:
# We have to put the previous VNF away ---------------------------------------------------------
succ_put_away = self._try_put_away_previous_vnf(previous_element, actual_element, mapping,
min_delay_link)
if not succ_put_away:
if rollback_num < self.rollback_level:
for j in range(len(map_list)):
if map_list[j][2].id == previous_element.id:
if isinstance(map_list[j][0], SAP):
need_migrate = True
if i != max_i:
for k in range(i, -1, -1):
t = list(map_list[k])
t[3] = 0
map_list[k] = tuple(t)
self._increase_bandwidth_previous_links(map_list[k][0],
map_list[k][2])
if k > 0:
self._clear_previous_mapping(map_list[k][0], mapping)
i = -1
mapped_vnodes = [self._actual_service_graph.saps[0].id]
else:
allow_migrate = True
else:
self._increase_bandwidth_previous_links(previous_element, actual_element)
self._clear_previous_mapping(previous_element, mapping)
t = list(map_list[j])
t[3] += 1
map_list[j] = tuple(t)
i = j - 1
if previous_element.id in mapped_vnodes:
mapped_vnodes.remove(previous_element.id)
rollback_num += 1
break
else:
need_migrate = True
if i != max_i:
for k in range(i, -1, -1):
t = list(map_list[k])
t[3] = 0
map_list[k] = tuple(t)
self._increase_bandwidth_previous_links(map_list[k][0],
map_list[k][2])
if k > 0:
self._clear_previous_mapping(map_list[k][0], mapping)
i = -1
mapped_vnodes = [self._actual_service_graph.saps[0].id]
else:
allow_migrate = True
# ----------------------------------------------------------------------------------
if need_migrate and allow_migrate:
try:
if isinstance(actual_element, SAP):
for j in range(len(map_list)):
if map_list[j][2].id == previous_element.id:
previous_element, min_delay_link, actual_element, retry = map_list[j]
vnf_mapping, ex, actual_element, previous_element, changed_sg_id = self._migrate(actual_element,
previous_element,
self._previous_mappings,
disable_migrating)
if all(i for i in changed_sgs if i.id != changed_sg_id):
changed_sgs.append(self._get_service_graph_from_id(changed_sg_id))
service_graph = self.service_graphs[-1]
self.mig_expense += ex
mapping['mapping'].append(vnf_mapping)
mapped_vnodes.append(actual_element.id)
rollback_num = 0
need_migrate = False
allow_migrate = False
map_list[i][1].mapped_to = [x.id for x in self._running.links if
map_list[i][1].id in x.mapped_virtual_links]
map_list[i][2].mapped_to = actual_element.mapped_to
except Exception as e:
service_graph = self.service_graphs[-1]
if not isinstance(e, NoCoreable) and not \
isinstance(e, MigratingNotPossible) and not \
isinstance(e, NoCompatibleFog):
dark.log(e.message, 'WARNING')
everything_ok = False
break
if everything_ok:
i += 1
if i > max_i:
max_i = i
else:
break
sum_mapped_CPU = 0
new_service = None
if everything_ok:
self._patch_mapping(mapping, service_graph)
for sc in self._previous_mappings:
service = self._get_service_graph_from_id(sc["service_id"])
for vnf in service.VNFS:
sum_mapped_CPU += vnf.required_CPU
new_service = self.service_graphs[-1]
else:
self.service_graphs = service_graphs_backup
return everything_ok, self.expense, self.__resource_graph, sum_mapped_CPU, new_service, changed_sgs
def __validate_mappings_in_case_of_using_chains(self, resource_graph, previous_resource_graph):
# Check mapped service graphs --------------------------------------------------------------------------
for prev_sc in self._previous_mappings:
sc = self._get_service_graph_from_id(prev_sc["service_id"])
for vlink in sc.VLinks:
# Check network requirements
req_bandwidth = vlink.required_bandwidth
req_delay = vlink.required_delay
phy_delay_path = 0
fogs = set()
for phylink_id in vlink.mapped_to:
phylink = next(x for x in resource_graph.links if x.id == phylink_id)
if vlink.id not in phylink.mapped_virtual_links:
return False, "vlink is not among the mapped vlinks of the physical link"
if (phylink.bandwidth["available"] + req_bandwidth) < req_bandwidth:
return False, "Phy link: '" + str(phylink.id) + "' has no available bandwidth (" + str(
phylink.bandwidth["available"] + req_bandwidth) + ") enough for the req (" + str(
req_bandwidth) + ") of mapped vlink:'" + str(vlink.id) + "'!"
if ("NETWORK" not in phylink.node1) and ("NETWORK" not in phylink.node2):
phy_delay_path += phylink.delay
else:
if "NETWORK" in phylink.node1:
fog = self._get_fog_from_phy_node(phylink.node2, for_validate=True)
fogs.add(fog.id)
else:
fog = self._get_fog_from_phy_node(phylink.node1, for_validate=True)
fogs.add(fog.id)
fogs = list(fogs)
if len(fogs) == 2:
phy_delay_path += self._delay_matrix[fogs[0]][fogs[1]]
if phy_delay_path > req_delay:
return False, "Delay is bigger than required!"
# Check resource requirements
vnf1_id = vlink.node1
vnf2_id = vlink.node2
for vnf_id in [vnf1_id, vnf2_id]:
if not self._is_SAP_from_id(vnf_id):
vnf = self._get_vnf_from_id(vnf_id)
try:
phy_node = next(
x for x in resource_graph.nodes + resource_graph.saps if x.id == vnf.mapped_to)
except:
pass
if vnf.id not in phy_node.mapped_VNFS:
return False, "VNF is not among the mapped VNFS inside the physical node"
if phy_node.CPU["available"] + vnf.required_CPU < vnf.required_CPU:
return False, "Phy node doesn't contain free CPU enough"
if phy_node.RAM["available"] + vnf.required_RAM < vnf.required_RAM:
return False, "Phy node doesn't contain free RAM enough"
if phy_node.STORAGE["available"] + vnf.required_STORAGE < vnf.required_STORAGE:
return False, "Phy node doesn't contain free STORAGE enough"
# -------------------------------------------------------------------------------------------------
# Check links and nodes of resource graphs --------------------------------------------------------
for phy_link in resource_graph.links:
if phy_link.bandwidth['available'] < 0:
return False, "On physical link: '" + str(
phy_link.id) + "' the available bandwidth is below than 0!"
if phy_link.bandwidth['available'] > phy_link.bandwidth['max']:
# TODO: Sure is it good if the cloud links are not decreased?
if not self._is_cloud_link(phy_link):
return False, "On physical link: '" + str(
phy_link.id) + "' the available bandwidth is greater than the theoretical maximum!"
for phy_node in resource_graph.nodes:
if phy_node.CPU["available"] < 0:
return False, "Something went wrong"
if phy_node.RAM["available"] < 0:
return False, "Something went wrong"
if phy_node.STORAGE["available"] < 0:
return False, "Something went wrong"
if self.__previous_resource_graph is not None:
for phy_link in resource_graph.links:
prev_phy_link = next(x for x in self.__previous_resource_graph.links if phy_link.id == x.id)
sum_changed_bandwidth = 0
# if phy_link is not an inside fog link and not cloud link
if ("NETWORK" in phy_link.node1) or ("NETWORK" in phy_link.node2):
if set(phy_link.mapped_virtual_links) != set(prev_phy_link.mapped_virtual_links):
added_vlink_ids = []
deleted_vlink_ids = []
for vlink in phy_link.mapped_virtual_links:
if vlink not in prev_phy_link.mapped_virtual_links:
added_vlink_ids.append(vlink)
for vlink in prev_phy_link.mapped_virtual_links:
if vlink not in phy_link.mapped_virtual_links:
deleted_vlink_ids.append(vlink)
for vlink in added_vlink_ids:
sum_changed_bandwidth += self._get_virtual_link_from_id(vlink).required_bandwidth
for vlink in deleted_vlink_ids:
sum_changed_bandwidth -= self._get_virtual_link_from_id(vlink).required_bandwidth
if not (phy_link.bandwidth["available"] == (
prev_phy_link.bandwidth["available"] - sum_changed_bandwidth) or \
phy_link.bandwidth["available"] == (
prev_phy_link.bandwidth["available"] + sum_changed_bandwidth)):
return False, "The available BW of a phylink is not equal with the available BW from " \
"the previous iterate + changed BW!"
if phy_link.bandwidth["available"] != prev_phy_link.bandwidth["available"]:
if set(phy_link.mapped_virtual_links) == set(prev_phy_link.mapped_virtual_links):
return False, "Mapped virtual links were not changed in phylink:'" + str(
phy_link.id) + "' however the available bandwidth did!"
else:
pass
for phy_node in resource_graph.nodes:
prev_phy_node = next(x for x in self.__previous_resource_graph.nodes if phy_node.id == x.id)
sum_changed_CPU = 0
sum_changed_RAM = 0
sum_changed_STORAGE = 0
if phy_node.mapped_VNFS != prev_phy_node.mapped_VNFS:
added_vnf_ids = []
deleted_vnf_ids = []
for vnf in phy_node.mapped_VNFS:
if vnf not in prev_phy_node.mapped_VNFS:
added_vnf_ids.append(vnf)
for vnf in prev_phy_node.mapped_VNFS:
if vnf not in phy_node.mapped_VNFS:
deleted_vnf_ids.append(vnf)
for vnf in added_vnf_ids:
sum_changed_CPU += self._get_vnf_from_id(vnf).required_CPU
sum_changed_RAM += self._get_vnf_from_id(vnf).required_RAM
sum_changed_STORAGE += self._get_vnf_from_id(vnf).required_STORAGE
for vnf in deleted_vnf_ids:
sum_changed_CPU -= self._get_vnf_from_id(vnf).required_CPU
sum_changed_RAM -= self._get_vnf_from_id(vnf).required_RAM
sum_changed_STORAGE -= self._get_vnf_from_id(vnf).required_STORAGE
if not (phy_node.CPU["available"] == (
prev_phy_node.CPU["available"] - sum_changed_CPU) or
phy_node.CPU["available"] == (
prev_phy_node.CPU["available"] + sum_changed_CPU)):
return False, "Number of mapped vnfs was changed on physical node:'" + str(
phy_node.id) + "' however the number of available CPUs of phy node was not!"
if not (phy_node.RAM["available"] == (
prev_phy_node.RAM["available"] - sum_changed_RAM) or
phy_node.RAM["available"] == (
prev_phy_node.RAM["available"] + sum_changed_RAM)):
return False, "Something went wrong"
if not (phy_node.STORAGE["available"] == (
prev_phy_node.STORAGE["available"] - sum_changed_STORAGE) or
phy_node.STORAGE["available"] == (
prev_phy_node.STORAGE["available"] + sum_changed_STORAGE)):
return False, "Something went wrong"
if phy_node.CPU["available"] != prev_phy_node.CPU["available"]:
if phy_node.mapped_VNFS == prev_phy_node.mapped_VNFS:
return False, "Something went wrong"
if phy_node.RAM["available"] != prev_phy_node.RAM["available"]:
if phy_node.mapped_VNFS == prev_phy_node.mapped_VNFS:
return False, "Something went wrong"
if phy_node.STORAGE["available"] != prev_phy_node.STORAGE["available"]:
if phy_node.mapped_VNFS == prev_phy_node.mapped_VNFS:
return False, "Something went wrong"
self.__previous_resource_graph = copy.deepcopy(resource_graph)
# -------------------------------------------------------------------------------------------------
return True, "Everything is awesome :)"
def _clear_previous_mapping(self, previous_element, mapping):
self._decrease_node_resource(previous_element.mapped_to, previous_element.required_CPU * -1,
previous_element.required_RAM * -1,
previous_element.required_STORAGE * -1)
previous_element.mapped_to = None
clear_element = next(x for x in mapping['mapping'] if x['vnf'].id == previous_element.id)
mapping['mapping'].remove(clear_element)
def _map_vnf(self, previous_element, actual_element, min_delay_link, retry):
compatible_nodes, contains_core = self._get_compatible_nodes_for_vnf_v2(previous_element,
actual_element, min_delay_link)
dark.log('COMPATIBLE NODES FOR VNF: {} {}'.format(actual_element.id, [x.id for x in compatible_nodes]),
'DEBUG')
if len(compatible_nodes) > 0:
compatible_nodes = self.choose_from_available_nodes(compatible_nodes, actual_element, previous_element)
if len(compatible_nodes) <= retry:
raise Exception('RETRY HIGHER THAN NUMBER OF NODES')
the_chosen_one = compatible_nodes[retry]
dark.log('CHOSEN PHYSICAL NODE {}'.format(the_chosen_one), 'DEBUG')
return_dict = {'vnf': actual_element, 'mapping_nodes': compatible_nodes,
'chosen': the_chosen_one, 'previous': previous_element,
'coreable': contains_core}
actual_element.mapped_to = the_chosen_one
self._decrease_node_resource(the_chosen_one, actual_element.required_CPU,
actual_element.required_RAM, actual_element.required_STORAGE)
if not isinstance(previous_element, SAP):
prev = self._get_phy_node_from_id(previous_element.mapped_to)
else:
prev = previous_element
return return_dict
else:
raise Exception('NO COMPATIBLE NODE FOUND')
def nova_scheduler(self, service_graph, strategy):
self._running = copy.deepcopy(self.__resource_graph)
map_list = self._order_service_graph(service_graph)
mapped_vnodes = []
mapped_vnodes.append(next(x[0] if isinstance(x[0], SAP) else x[1] for x in map_list).id)
i = 0
sum_mapped_CPU = 0
mapping = {'service_id': service_graph.id, 'mapping': []}
sum_d = 0
for p, l, a, r in map_list:
sum_d += l.required_delay
while i < len(map_list):
previous_element, min_delay_link, actual_element, retry = map_list[i]
if isinstance(actual_element, SAP):
break
compatible_nodes = []
# DUMB NOVA
if strategy == 1:
compatible_nodes = self._filter_physical_nodes_by_resource(actual_element)
# SMARTER NOVA
if strategy == 2:
compatible_nodes = self._filter_physical_nodes_by_resource(actual_element)
for n in self._running.nodes:
dd = 0
if n.fog_cloud is not None and n in compatible_nodes:
l, path = self._get_shortest_path_and_length(mapped_vnodes[0], n)
for j in range(len(path)-1):
ll = self._get_link_between_two_phy_node(path[j], path[j+1])
if 'NETWORK' not in ll.node1 and 'NETWORK' not in ll.node2:
dd += ll.delay
if dd+self._delay_matrix[self._get_fog_from_phy_node(mapped_vnodes[0]).id][n.fog_cloud] > sum_d:
compatible_nodes.remove(n)
# MORE SMARTER NOVA
if strategy == 3:
compatible_nodes = self._filter_physical_nodes_by_resource(actual_element)
for n in self._running.nodes:
dd = 0
if n.fog_cloud is not None and n in compatible_nodes:
l, path = self._get_shortest_path_and_length(mapped_vnodes[0], n)
for j in range(len(path) - 1):
ll = self._get_link_between_two_phy_node(path[j], path[j + 1])
if 'NETWORK' not in ll.node1 and 'NETWORK' not in ll.node2:
dd += ll.delay
if dd + self._delay_matrix[self._get_fog_from_phy_node(mapped_vnodes[0]).id][
n.fog_cloud] > map_list[i][1].required_delay:
compatible_nodes.remove(n)
if len(compatible_nodes) > 0:
compatible_nodes = self.choose_for_nova(compatible_nodes)
the_chosen_one = compatible_nodes[0]
dark.log('CHOSEN PHYSICAL NODE {}'.format(the_chosen_one), 'DEBUG')
return_dict = {'vnf': actual_element, 'mapping_nodes': compatible_nodes,
'chosen': the_chosen_one, 'previous': previous_element}
actual_element.mapped_to = the_chosen_one
# -----------------------------------------------------------------------------------------------------
if not isinstance(previous_element, SAP):
fog2 = self._get_fog_from_phy_node(previous_element.mapped_to)
else:
fog2 = self._get_fog_from_phy_node(previous_element)
fog1 = self._get_fog_from_phy_node(the_chosen_one)
gw1 = self._get_core_gw(fog1)
gw2 = self._get_core_gw(fog2)
if min_delay_link.required_delay < self._delay_matrix[fog1.id][fog2.id]:
dark.log("DELAY ERROR", "INFO")
return False, 0, self.__resource_graph, sum_mapped_CPU
l1 = self._get_link_between_two_phy_node(self._core_network, gw1)
l2 = self._get_link_between_two_phy_node(self._core_network, gw2)
if min_delay_link.required_bandwidth > l1.bandwidth['available'] or min_delay_link.required_bandwidth > \
l2.bandwidth['available']:
dark.log("BW ERROR", "INFO")
return False, 0, self.__resource_graph, sum_mapped_CPU
# -----------------------------------------------------------------------------------------------------
l1.bandwidth['available'] -= min_delay_link.required_bandwidth
l2.bandwidth['available'] -= min_delay_link.required_bandwidth
self._decrease_node_resource(the_chosen_one, actual_element.required_CPU,
actual_element.required_RAM, actual_element.required_STORAGE)
rg_node = next(x for x in self._running.nodes if the_chosen_one == x.id)
if actual_element.id not in rg_node.mapped_VNFS:
rg_node.mapped_VNFS.append(actual_element.id)
mapping['mapping'].append(return_dict)
i += 1
else:
dark.log("NO COMPATIBLE NODES", "INFO")
return False, 0, self.__resource_graph, sum_mapped_CPU
self._previous_mappings.append(mapping)
self.service_graphs.append(service_graph)
self.__resource_graph = self._running
self._running = None
for sc in self._previous_mappings:
service = self._get_service_graph_from_id(sc["service_id"])
for vnf in service.VNFS:
sum_mapped_CPU += vnf.required_CPU
return True, 0, self.__resource_graph, sum_mapped_CPU
def choose_for_nova(self, compatible_nodes):
ram_mult = 1.0
disk_mult = 1.0
nodes_with_weight = []
for node in compatible_nodes:
w = (float(node.RAM['available']) / float(node.RAM['max'])) * ram_mult + \
(float(node.STORAGE['available']) / float(node.STORAGE['max'])) * disk_mult
nodes_with_weight.append((node, w))
compatible_nodes = [s[0].id for s in sorted(nodes_with_weight, key=lambda k: k[1], reverse=True)]
return compatible_nodes
def _check_previous_vlinks(self, previous_element, mn):
"""
:param previous_element:
:return:
"""
for vl in self._actual_service_graph.VLinks:
vn = None
if vl.node1 == previous_element.id:
vn = self._get_vnf_from_id(vl.node2)
elif vl.node2 == previous_element.id:
vn = self._get_vnf_from_id(vl.node1)
if vn is not None and vn.mapped_to is not None:
length, path = self._get_shortest_path_and_length(mn, self._get_phy_node_from_id(vn.mapped_to))
length = self._delay_matrix[self._get_fog_from_phy_node(mn).id][
self._get_fog_from_phy_node(self._get_phy_node_from_id(vn.mapped_to)).id]
if length > vl.required_delay:
return False
return True
def _try_put_away_previous_vnf(self, previous_element, actual_element, mapping, min_delay_link):
"""
:param previous_element:
:param mapping:
:param min_delay_link:
:return:
"""
previous_mapping = next(x for x in mapping['mapping'] if x['vnf'].id == previous_element.id)
succ_remap = False
prev_map_node = previous_mapping['chosen']
all_good = True
if isinstance(actual_element, SAP):
phy_node = self._get_phy_node_from_id(actual_element.id)
for mn in previous_mapping['mapping_nodes']:
if mn != previous_mapping['chosen']:
all_good = self._check_previous_vlinks(previous_element, mn)
if all_good:
ll, pp = self._get_shortest_path_and_length(mn, phy_node)
ll = self._delay_matrix[self._get_fog_from_phy_node(mn).id][
self._get_fog_from_phy_node(phy_node).id]
if ll <= min_delay_link.required_delay:
succ_remap = True
self._decrease_node_resource(prev_map_node, previous_element.required_CPU * -1,
previous_element.required_RAM * -1,
previous_element.required_STORAGE * -1)
self._increase_bandwidth_previous_links(previous_element, actual_element)
previous_element.mapped_to = mn
previous_mapping['chosen'] = mn
vlinks = self._get_virtual_links_from_vnf(previous_element)
for link in vlinks:
if len(link.mapped_to) > 0:
self._map_virtual_link(link, self._actual_service_graph)
break
else:
for mn in previous_mapping['mapping_nodes']:
if mn != previous_mapping['chosen']:
all_good = self._check_previous_vlinks(previous_element, mn)
if all_good:
prev_copy = copy.deepcopy(previous_element)
prev_copy.mapped_to = mn
com_nodes, contains_core = self._get_compatible_nodes_for_vnf_v2(prev_copy, actual_element,
min_delay_link)
if len(com_nodes) > 0:
succ_remap = True
self._decrease_node_resource(prev_map_node, previous_element.required_CPU * -1,
previous_element.required_RAM * -1,
previous_element.required_STORAGE * -1)
self._increase_bandwidth_previous_links(previous_element, actual_element)
previous_element.mapped_to = mn
previous_mapping['chosen'] = mn
self._decrease_node_resource(mn, previous_element.required_CPU,
previous_element.required_RAM,
previous_element.required_STORAGE)
vlinks = self._get_virtual_links_from_vnf(previous_element)
for link in vlinks:
if len(link.mapped_to) > 0:
self._map_virtual_link(link, self._actual_service_graph)
break
return succ_remap
def _increase_bandwidth_previous_links(self, previous_element, actual_element):
vlinks = self._get_virtual_links_from_vnf(previous_element)
for link in vlinks:
if len(link.mapped_to) > 0:
vn1 = next(x for x in self._actual_service_graph.VNFS + self._actual_service_graph.saps
if link.node1 == x.id)
vn2 = next(x for x in self._actual_service_graph.VNFS + self._actual_service_graph.saps
if link.node2 == x.id)
if vn1.id != actual_element.id and vn2.id != actual_element.id:
bw = link.required_bandwidth
if isinstance(vn1, SAP):
node_from = self._get_phy_node_from_id(vn1.id)
node_to = self._get_phy_node_from_id(vn2.mapped_to)
elif isinstance(vn2, SAP):
node_from = self._get_phy_node_from_id(vn1.mapped_to)
node_to = self._get_phy_node_from_id(vn2.id)
else:
node_from = self._get_phy_node_from_id(vn1.mapped_to)
node_to = self._get_phy_node_from_id(vn2.mapped_to)
self._increase_bandwidth_at_inter_fog_links(node_from, node_to, bw)
for phy_link_id in link.mapped_to:
self._get_phy_link_from_id(phy_link_id).mapped_virtual_links.remove(link.id)
link.mapped_to = []
def _order_service_graph(self, service_graph):
"""
:param service_graph:
:return:
"""
return_list = []
mapped_vnodes = [service_graph.saps[0].id]
dark.log('Mapped Virtual Nodes: {}'.format(str([x for x in mapped_vnodes])), 'DEBUG')
mapped_vlinks = []
dark.log('Mapped Virtual Links: {}'.format(str([x for x in mapped_vlinks])), 'DEBUG')
available_vlinks = self._get_available_vlinks(service_graph, mapped_vnodes, mapped_vlinks)
dark.log('AVAILABLE VIRTUAL LINKS: {}'.format(str(available_vlinks)), 'DEBUG')
min_delay_link = available_vlinks.pop(0)
dark.log('STRICTEST VIRTUAL LINK FOR DELAY: {} WITH DELAY {}'.format(min_delay_link.id,
str(min_delay_link.required_delay)),
'DEBUG')
dark.log('MAPPED VIRTUAL LINK LIST: {}'.format(str([x for x in mapped_vlinks])), 'DEBUG')
while len(mapped_vnodes) != len(service_graph.saps + service_graph.VNFS) and \
len(mapped_vlinks) != len(service_graph.VLinks):
if min_delay_link.node1 in mapped_vnodes and min_delay_link.node2 in mapped_vnodes:
previous_element = next(x for x in service_graph.VNFS + service_graph.saps if
x.id == min_delay_link.node1)
actual_element = next(x for x in service_graph.VNFS + service_graph.saps if
x.id == min_delay_link.node2)
mapped_vlinks.append(min_delay_link.id)
else:
if min_delay_link.node1 in mapped_vnodes:
previous_element = next(x for x in service_graph.VNFS + service_graph.saps if
x.id == min_delay_link.node1)
actual_element = next(x for x in service_graph.VNFS + service_graph.saps if
x.id == min_delay_link.node2)
elif min_delay_link.node2 in mapped_vnodes:
previous_element = next(x for x in service_graph.VNFS + service_graph.saps if
x.id == min_delay_link.node2)
actual_element = next(x for x in service_graph.VNFS + service_graph.saps if
x.id == min_delay_link.node1)
else:
dark.log('MIN DELAY LINK DOESN\'T CONTAINS ANY NODE FROM MAPPED NODES', 'ERROR')
raise Exception
mapped_vlinks.append(min_delay_link.id)
mapped_vnodes.append(actual_element.id)
return_list.append((previous_element, min_delay_link, actual_element, 0))
available_vlinks = self._get_available_vlinks(service_graph, mapped_vnodes, mapped_vlinks)
if len(available_vlinks) == 0:
break
dark.log('AVAILABLE VIRTUAL LINKS: {}'.format(str(available_vlinks)), 'DEBUG')
min_delay_link = available_vlinks.pop(0)
return return_list
def choose_from_available_nodes(self, compatible_nodes, actual_element, previous_element):
"""
:param compatible_nodes:
:param actual_element:
:param previous_element:
:return:
"""
# random
fogs = set([x.fog_cloud for x in compatible_nodes])
# CPU
cpu = 0
fog_cpu = 0
return_node = None
fog_list = []
if None in fogs:
fogs.remove(None)
for ff in fogs:
max_cpu = 0
ava_cpu = 0
c = 0
f = self._get_fog_from_id(ff)
for n in f.compute_nodes:
max_cpu += self._get_phy_node_from_id(n).CPU['max']
ava_cpu += self._get_phy_node_from_id(n).CPU['available']
c = ava_cpu / max_cpu
fog_list.append({'fog': ff, 'cpu': c, 'nodes': []})
fog_list = sorted(fog_list, key=lambda l: l['cpu'], reverse=True)
for fog in fog_list:
for tn in compatible_nodes:
if tn.fog_cloud == fog['fog']:
fog['nodes'].append({'node': tn.id, 'cpu': tn.CPU['available'] / tn.CPU['max']})
fog['nodes'] = sorted(fog['nodes'], key=lambda l: l['cpu'], reverse=True)
return_list = []
for fog in fog_list:
return_list += [x['node'] for x in fog['nodes']]
for x in compatible_nodes:
if x in self._core_clouds:
if self.strategy == 0:
return_list.append(x.id)
elif self.strategy == 1:
return_list.insert(0, x.id)
elif self.strategy == 2 and self.cpu_limit > actual_element.required_CPU:
return_list.insert(0, x.id)
elif self.strategy == 3 and self.cpu_limit < actual_element.required_CPU:
return_list.insert(0, x.id)
else:
return_list.append(x.id)
if isinstance(previous_element, SAP):
prev_fog = self._get_fog_from_phy_node(previous_element)
else:
prev_fog = self._get_fog_from_phy_node(previous_element.mapped_to)
if any(x in return_list for x in prev_fog.compute_nodes):
nodes_in_prev_fog = [x for x in prev_fog.compute_nodes if x in return_list]
for n in nodes_in_prev_fog:
return_list.insert(0, return_list.pop(return_list.index(n)))
return return_list
def _get_connected_SAPS(self, vnf):
neighbors = self._get_vnf_neighbors(vnf)
SAP_list = []
for neighbor in neighbors:
if self._is_SAP_from_id(neighbor.id):
SAP_list.append(neighbor)
return SAP_list
def _is_bigger_VNF(self, vnf_a, vnf_b):
"""
Return TRUE if vnf_b is bigger than vnf_a in terms of compute resources (CPU, RAM, storage)
:param vnf_a:
:param vnf_b:
:return:
"""
if vnf_a.required_CPU <= vnf_b.required_CPU and \
vnf_a.required_RAM <= vnf_b.required_RAM and \
vnf_a.required_STORAGE <= vnf_b.required_STORAGE:
return True
else:
return False
def _insert_phy_node_list_according_CPU_free_spaces(self, node_list, phy_node):
for i in range(0, len(node_list)):
if node_list[i].CPU["available"] <= phy_node.CPU["available"]:
node_list.insert(i, phy_node)
return
node_list.append(phy_node)
def _delete_vlink_from_resource_graph(self, vlink):
"""
Delete vlink from physical links and increase their available BWs.
:param vlink:
:return:
"""
for plink_id in vlink.mapped_to:
plink = self._get_phy_link_from_id(plink_id)
try:
plink.mapped_virtual_links.remove(vlink.id)
except:
pass
if ("NETWORK" in plink.node1) or ("NETWORK" in plink.node2):
plink.bandwidth["available"] += vlink.required_bandwidth
vlink.mapped_to = []
def _delete_vnf_from_resource_graph(self, vnf):
"""
Delete vnf from physical node and increase its available compute resources.
:param vnf:
:return:
"""
# VNF got fog?
phy_nod, asd = self._get_physical_and_fog_from_virtual_node(vnf)
phy_nod.mapped_VNFS.remove(vnf.id)
phy_nod.CPU["available"] += vnf.required_CPU
phy_nod.RAM["available"] += vnf.required_RAM
phy_nod.STORAGE["available"] += vnf.required_STORAGE
vnf.mapped_to = None
def _is_phy_node_applicable_for_VNF(self, phy_node, vnf):
"""
Return TRUE if free spaces of phy_node is enough for requirements of VNF
"""
if vnf.required_CPU <= phy_node.CPU["available"] and \
vnf.required_RAM <= phy_node.RAM["available"] and \
vnf.required_STORAGE <= phy_node.STORAGE["available"]:
return True
return False
def _map_vlinks_of_vnf_for_migrate(self, vnf):
"""
Maps virtual links of the given vnf. PLEASE NOTE if the return value FALSE then the mapping was not successful.
In this case you have to set back the used resources of links to the original.
:param vnf:
:return:
"""
for vlink_id in vnf.connected_virtual_links:
vlink = self._get_virtual_link_from_id(vlink_id)
vlink.mapped_to = []
sg = self._get_service_graph_from_virtual_link(vlink)
ll = self._map_virtual_link(vlink, sg)
sum_delay = 0
fogs = set()
for l_id in ll:
pl = self._get_phy_link_from_id(l_id)
if vlink.id not in pl.mapped_virtual_links:
pl.mapped_virtual_links.append(vlink_id)
if pl.bandwidth["available"] < 0:
return False
if ("NETWORK" not in pl.node1) and ("NETWORK" not in pl.node2):
sum_delay += pl.delay
else:
if "NETWORK" in pl.node1:
fog = self._get_fog_from_phy_node(pl.node2)
fogs.add(fog.id)
else:
fog = self._get_fog_from_phy_node(pl.node1)
fogs.add(fog.id)
fogs = list(fogs)
if len(fogs) == 2:
try:
sum_delay += self._delay_matrix[fogs[0]][fogs[1]]
except Exception as e:
dark.log(