-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnffg.py
1658 lines (1533 loc) · 81.3 KB
/
nffg.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
"""
Created on Oct 14, 2015
@author: fabiomignini
@author: stefanopetrangeli
"""
import sys
import uuid
import json
import copy
from .exception import InexistentLabelFound, WrongNumberOfPorts
class NF_FG(object):
def __init__(self, _id=None, name=None, vnfs=None, end_points=None, flow_rules=None, description=None, domain=None,
unify_monitoring=None):
"""
:param _id:
:param name:
:param vnfs:
:param end_points:
:param flow_rules:
:param description:
:param domain:
:param unify_monitoring:
:type _id:
:type name: str
:type vnfs: list of VNF
:type end_points: list of EndPoint
:type flow_rules: list of FlowRule
:type description:
:type domain: str
:type unify_monitoring:
"""
self.id = _id
self.name = name
self.description = description
self.vnfs = vnfs or []
self.end_points = end_points or []
self.flow_rules = flow_rules or []
self.domain = domain
self.unify_monitoring = unify_monitoring
self.db_id = None
def parseDict(self, nffg_dict):
if 'domain' in nffg_dict['forwarding-graph']:
self.domain = nffg_dict['forwarding-graph']['domain']
if 'unify-monitoring' in nffg_dict['forwarding-graph']:
self.unify_monitoring = nffg_dict['forwarding-graph']['unify-monitoring']
if 'name' in nffg_dict['forwarding-graph']:
self.name = nffg_dict['forwarding-graph']['name']
if 'description' in nffg_dict['forwarding-graph']:
self.description = nffg_dict['forwarding-graph']['description']
if 'VNFs' in nffg_dict['forwarding-graph']:
for vnf_dict in nffg_dict['forwarding-graph']['VNFs']:
vnf = VNF()
vnf.parseDict(vnf_dict)
self.vnfs.append(vnf)
if 'end-points' in nffg_dict['forwarding-graph']:
for end_point_dict in nffg_dict['forwarding-graph']['end-points']:
endpoint = EndPoint()
endpoint.parseDict(end_point_dict)
self.end_points.append(endpoint)
if 'big-switch' in nffg_dict['forwarding-graph']:
if 'flow-rules' in nffg_dict['forwarding-graph']['big-switch']:
for flow_rule_dict in nffg_dict['forwarding-graph']['big-switch']['flow-rules']:
flow_rule = FlowRule()
flow_rule.parseDict(flow_rule_dict)
self.flow_rules.append(flow_rule)
def getDict(self, extended=False, domain=False):
nffg_dict = dict()
nffg_dict['forwarding-graph'] = dict()
if self.name is not None:
nffg_dict['forwarding-graph']['name'] = self.name
if self.description is not None:
nffg_dict['forwarding-graph']['description'] = self.description
if self.unify_monitoring is not None:
nffg_dict['forwarding-graph']['unify-monitoring'] = self.unify_monitoring
vnfs_dict = []
for vnf in self.vnfs:
vnfs_dict.append(vnf.getDict(extended, domain))
if vnfs_dict:
nffg_dict['forwarding-graph']['VNFs'] = vnfs_dict
end_points_dict = []
for end_point in self.end_points:
end_points_dict.append(end_point.getDict(extended, domain))
if end_points_dict:
nffg_dict['forwarding-graph']['end-points'] = end_points_dict
flow_rules_dict = []
for flow_rule in self.flow_rules:
flow_rules_dict.append(flow_rule.getDict(extended))
if flow_rules_dict:
nffg_dict['forwarding-graph']['big-switch'] = dict()
nffg_dict['forwarding-graph']['big-switch']['flow-rules'] = flow_rules_dict
if domain is True:
if self.domain is not None:
nffg_dict['forwarding-graph']['domain'] = self.domain
return nffg_dict
def getJSON(self, extended=False, domain=False):
return json.dumps(self.getDict(extended, domain))
def sanitizeEpIDs(self):
for ep in self.end_points:
ep.id = ep.id.replace(':', '.')
for flow_rule in self.flow_rules:
port_in = flow_rule.match.port_in
if port_in is not None and port_in.split(':')[0] == 'endpoint':
flow_rule.match.port_in = port_in.split(':')[0]+':'+port_in.split(':', 1)[1].replace(':', '.')
for action in flow_rule.actions:
if action.output is not None and action.output.split(':')[0] == 'endpoint':
action.output = action.output.split(':')[0]+':'+action.output.split(':', 1)[1].replace(':', '.')
def getVNF(self, vnf_id):
for vnf in self.vnfs:
if vnf.id == vnf_id:
return vnf
def getVNFByDBID(self, vnf_db_id):
for vnf in self.vnfs:
if vnf.db_id == vnf_db_id:
return vnf
def getEndPoint(self, end_point_id):
for end_point in self.end_points:
if end_point.id == end_point_id:
return end_point
def getFlowRule(self, flow_rule_id):
for flow_rule in self.flow_rules:
if flow_rule.id == flow_rule_id:
return flow_rule
def addVNF(self, vnf):
if type(vnf) is VNF:
self.vnfs.append(vnf)
else:
raise TypeError("Tried to add a vnf with a wrong type. Expected VNF, found "+type(vnf))
def addEndPoint(self, end_point):
if type(end_point) is EndPoint:
self.end_points.append(end_point)
else:
raise TypeError("Tried to add a end-point with a wrong type. Expected EndPoint, found "+type(end_point))
def addFlowRule(self, flow_rule):
if type(flow_rule) is FlowRule:
self.flow_rules.append(flow_rule)
else:
raise TypeError("Tried to add a flow-rules with a wrong type. Expected FlowRule, found "+type(flow_rule))
def getEndPointsFromName(self, end_point_name):
endpoints = []
for end_point in self.end_points:
if end_point.name == end_point_name:
endpoints.append(end_point)
return endpoints
def getFlowRulesSendingTrafficToEndPoint(self, endpoint_id):
return self.getFlowRuleSendingTrafficToNode("endpoint:"+endpoint_id)
def getFlowRulesSendingTrafficFromEndPoint(self, endpoint_id):
return self.getFlowRuleSendingTrafficFromNode("endpoint:"+endpoint_id)
def getFlowRulesSendingTrafficFromPort(self, vnf_id, port_id):
return self.getFlowRuleSendingTrafficFromNode("vnf:"+vnf_id+":"+port_id)
def getFlowRulesSendingTrafficToPort(self, vnf_id, port_id):
return self.getFlowRuleSendingTrafficToNode("vnf:"+vnf_id+":"+port_id)
def getFlowRulesSendingTrafficToVNF(self, vnf):
flow_rules = []
for port in vnf.ports:
flow_rules = flow_rules + self.getFlowRuleSendingTrafficToNode("vnf:"+vnf.id+":"+port.id)
return flow_rules
def getFlowRulesSendingTrafficFromVNF(self, vnf):
"""
:param vnf:
:return:
:rtype: list of FlowRule
"""
flow_rules = []
for port in vnf.ports:
flow_rules = flow_rules + self.getFlowRuleSendingTrafficFromNode("vnf:"+vnf.id+":"+port.id)
return flow_rules
def getFlowRuleSendingTrafficToNode(self, node_id):
flow_rules = []
for flow_rule in self.flow_rules:
for action in flow_rule.actions:
if action.output == node_id:
flow_rules.append(flow_rule)
continue
return flow_rules
def getFlowRuleSendingTrafficFromNode(self, node_id):
flow_rules = []
for flow_rule in self.flow_rules:
if flow_rule.match.port_in == node_id:
flow_rules.append(flow_rule)
return flow_rules
def getEndPointsSendingTrafficToPort(self, vnf_id, port_id):
end_points = []
flow_rules = self.getFlowRulesSendingTrafficToPort(vnf_id, port_id)
for flow_rule in flow_rules:
if flow_rule.match.port_in.split(':')[0] == 'endpoint':
end_points.append(self.getEndPoint(flow_rule.match.port_in.split(':')[1]))
return end_points
def isDuplicatedFlowrule(self, other_flowrule):
"""
Checks if other_flowrule is already present in the nffg. The comparison is based on all fields except the ID
"""
for flow_rule in self.flow_rules:
current_flowrule_dict = flow_rule.getDict()
del current_flowrule_dict['id']
other_flowrule_dict = other_flowrule.getDict()
del other_flowrule_dict['id']
if current_flowrule_dict == other_flowrule_dict and flow_rule.id != other_flowrule.id:
return True
return False
def expandNode(self, old_vnf, internal_nffg):
"""
WARNING: Only 1to1 or 1toN connection among VNFs of internal graph and VNFs of external graph are supported.
"""
external_nffg = self
# Add new VNFs in graph
for internal_vnf in internal_nffg.vnfs:
external_nffg.addVNF(internal_vnf)
# List on ports of the old VNF. These ports represent the end-point of the internal graph (VNF expanded).
for end_point_port in old_vnf.ports:
# Add connections from internal graph (VNF expanded) to external graph
internal_outgoing_flowrules = internal_nffg.getFlowRulesSendingTrafficToEndPoint(
end_point_port.id.split(':')[0])
external_outgoing_flowrules = external_nffg.getFlowRulesSendingTrafficFromPort(
old_vnf.id, end_point_port.id)
external_nffg.flow_rules = external_nffg.flow_rules + self.mergeFlowrules(internal_outgoing_flowrules,
external_outgoing_flowrules)
# Delete external_outgoing_flowrules from external_nffg.flow_rules
for external_outgoing_flowrule in external_outgoing_flowrules:
external_nffg.flow_rules.remove(external_outgoing_flowrule)
for internal_outgoing_flowrule in internal_outgoing_flowrules:
internal_nffg.flow_rules.remove(internal_outgoing_flowrule)
# Add connections from external graph to internal graph
internal_ingoing_flowrules = internal_nffg.getFlowRulesSendingTrafficFromEndPoint(
end_point_port.id.split(':')[0])
external_ingoing_flowrules = external_nffg.getFlowRulesSendingTrafficToPort(old_vnf.id, end_point_port.id)
external_nffg.flow_rules = external_nffg.flow_rules + self.mergeFlowrules(external_ingoing_flowrules,
internal_ingoing_flowrules)
# Delete external_ingoing_flowrules from external_nffg.flow_rules?
for external_ingoing_flowrule in external_ingoing_flowrules:
external_nffg.flow_rules.remove(external_ingoing_flowrule)
for internal_ingoing_flowrule in internal_ingoing_flowrules:
internal_nffg.flow_rules.remove(internal_ingoing_flowrule)
# Add Flow-rules
for internal_flow_rule in internal_nffg.flow_rules:
internal_flow_rule.id = uuid.uuid4().hex
external_nffg.flow_rules.append(internal_flow_rule)
# Delete old VNF
self.vnfs.remove(old_vnf)
def attachNF_FG(self, attaching_nffg, end_point_name):
try:
attaching_end_point = attaching_nffg.getEndPointsFromName(end_point_name)[0]
end_point = self.getEndPointsFromName(end_point_name)[0]
except IndexError:
return
if attaching_end_point is None or end_point is None:
raise Exception("WARNING: Impossible to attach the graph: " + self.id + " with graph: " + attaching_nffg.id
+ " on an end-point called: " + end_point_name + ", doesn't exit the common end_point.")
# Add connections from internal graph (VNF expanded) to external graph
attaching_outgoing_flowrules = attaching_nffg.getFlowRulesSendingTrafficToEndPoint(attaching_end_point.id)
ingoing_flowrules = self.getFlowRulesSendingTrafficFromEndPoint(end_point.id)
self.flow_rules = self.flow_rules + self.mergeFlowrules(attaching_outgoing_flowrules, ingoing_flowrules)
# Delete external_outgoing_flowrules from external_nffg.flow_rules
for ingoing_flowrule in ingoing_flowrules:
self.flow_rules.remove(ingoing_flowrule)
for attaching_outgoing_flowrule in attaching_outgoing_flowrules:
attaching_nffg.flow_rules.remove(attaching_outgoing_flowrule)
# Add connections from external graph to internal graph
attaching_ingoing_flowrules = attaching_nffg.getFlowRulesSendingTrafficFromEndPoint(attaching_end_point.id)
outgoing_flowrules = self.getFlowRulesSendingTrafficToEndPoint(end_point.id)
self.flow_rules = self.flow_rules + self.mergeFlowrules(outgoing_flowrules, attaching_ingoing_flowrules)
# Delete external_ingoing_flowrules from external_nffg.flow_rules?
for outgoing_flowrule in outgoing_flowrules:
self.flow_rules.remove(outgoing_flowrule)
for attaching_ingoing_flowrule in attaching_ingoing_flowrules:
attaching_nffg.flow_rules.remove(attaching_ingoing_flowrule)
# Add the VNFs of the attaching graph
for new_vnf in attaching_nffg.vnfs:
self.vnfs.append(new_vnf)
# Add the end-points of the attaching graph
for new_end_point in attaching_nffg.end_points:
self.end_points.append(new_end_point)
# Add the end-points of the attaching graph
for new_flow_rule in attaching_nffg.flow_rules:
new_flow_rule.id = uuid.uuid4().hex
self.flow_rules.append(new_flow_rule)
# Delete the end-point of the attachment in the graph
self.end_points.remove(end_point)
self.end_points.remove(attaching_end_point)
def mergeFlowrules(self, outgoing_flow_rules, ingoing_flow_rules, maintain_id=False):
"""
if maintaind_id is True the flowrule_id is preserved (except last two chars).
This parameter is used when graphs are joined
"""
flowrules = []
for outgoing_flow_rule in outgoing_flow_rules:
for ingoing_flow_rule in ingoing_flow_rules:
final_match = self.mergeMatches(outgoing_flow_rule.match, ingoing_flow_rule.match)
if outgoing_flow_rule.match.port_in is not None:
final_match.port_in = outgoing_flow_rule.match.port_in
if maintain_id:
flowrules.append(FlowRule(_id=ingoing_flow_rule.id[:-2], priority=ingoing_flow_rule.priority,
actions=ingoing_flow_rule.actions, match=final_match))
else:
flowrules.append(FlowRule(_id=uuid.uuid4().hex, priority=ingoing_flow_rule.priority,
actions=ingoing_flow_rule.actions, match=final_match))
return flowrules
def mergeMatches(self, first_match, second_match):
match = Match()
if first_match.ether_type is not None and second_match.ether_type is not None:
if first_match.ether_type == second_match.ether_type:
match.ether_type = second_match.ether_type
elif first_match.ether_type is not None:
match.ether_type = first_match.ether_type
elif second_match.ether_type is not None:
match.ether_type = second_match.ether_type
if first_match.vlan_id is not None and second_match.vlan_id is not None:
if first_match.vlan_id == second_match.vlan_id:
match.vlan_id = second_match.vlan_id
elif first_match.vlan_id is not None:
match.vlan_id = first_match.vlan_id
elif second_match.vlan_id is not None:
match.vlan_id = second_match.vlan_id
if first_match.vlan_priority is not None and second_match.vlan_priority is not None:
if first_match.vlan_priority == second_match.vlan_priority:
match.vlan_priority = second_match.vlan_priority
elif first_match.vlan_priority is not None:
match.vlan_priority = first_match.vlan_priority
elif second_match.vlan_priority is not None:
match.vlan_priority = second_match.vlan_priority
if first_match.source_mac is not None and second_match.source_mac is not None:
if first_match.source_mac == second_match.source_mac:
match.source_mac = second_match.source_mac
elif first_match.source_mac is not None:
match.source_mac = first_match.source_mac
elif second_match.source_mac is not None:
match.source_mac = second_match.source_mac
if first_match.dest_mac is not None and second_match.dest_mac is not None:
if first_match.dest_mac == second_match.dest_mac:
match.dest_mac = second_match.dest_mac
elif first_match.dest_mac is not None:
match.dest_mac = first_match.dest_mac
elif second_match.dest_mac:
match.dest_mac = second_match.dest_mac
if first_match.source_ip is not None and second_match.source_ip is not None:
if first_match.source_ip == second_match.source_ip:
match.source_ip = second_match.source_ip
elif first_match.source_ip is not None:
match.source_ip = first_match.source_ip
elif second_match.source_ip is not None:
match.source_ip = second_match.source_ip
if first_match.dest_ip is not None and second_match.dest_ip is not None:
if first_match.dest_ip == second_match.dest_ip:
match.dest_ip = second_match.dest_ip
elif first_match.dest_ip is not None:
match.dest_ip = first_match.dest_ip
elif second_match.dest_ip is not None:
match.dest_ip = second_match.dest_ip
if first_match.tos_bits is not None and second_match.tos_bits is not None:
if first_match.tos_bits == second_match.tos_bits:
match.tos_bits = second_match.tos_bits
elif first_match.tos_bits is not None:
match.tos_bits = first_match.tos_bits
elif second_match.tos_bits is not None:
match.tos_bits = second_match.tos_bits
if first_match.source_port is not None and second_match.source_port is not None:
if first_match.source_port == second_match.source_port:
match.source_port = second_match.source_port
elif first_match.source_port is not None:
match.source_port = first_match.source_port
elif second_match.source_port is not None:
match.source_port = second_match.source_port
if first_match.dest_port is not None and second_match.dest_port is not None:
if first_match.dest_port == second_match.dest_port:
match.dest_port = second_match.dest_port
elif first_match.dest_port is not None:
match.dest_port = first_match.dest_port
elif second_match.dest_port is not None:
match.dest_port = second_match.dest_port
if first_match.protocol is not None and second_match.protocol is not None:
if first_match.protocol == second_match.protocol:
match.protocol = second_match.protocol
elif first_match.protocol is not None:
match.protocol = first_match.protocol
elif second_match.protocol is not None:
match.protocol = second_match.protocol
return match
def diff(self, nffg_new):
nffg = NF_FG()
nffg.id = nffg_new.id
nffg.name = nffg_new.name
nffg.vnfs = nffg_new.vnfs
nffg.end_points = nffg_new.end_points
nffg.flow_rules = nffg_new.flow_rules
# VNFs
for new_vnf in nffg_new.vnfs:
new_vnf.status = 'new'
for old_vnf in self.vnfs:
vnf_found = False
for new_vnf in nffg_new.vnfs:
if old_vnf.id == new_vnf.id and old_vnf.name == new_vnf.name \
and old_vnf.vnf_template_location == new_vnf.vnf_template_location:
new_vnf.status = 'already_deployed'
new_vnf.db_id = old_vnf.db_id
new_vnf.internal_id = old_vnf.internal_id
# check ports
for new_port in new_vnf.ports:
new_port.status = 'new'
for old_port in old_vnf.ports:
port_found = False
for new_port in new_vnf.ports:
if old_port.id == new_port.id:
new_port.status = 'already_deployed'
new_port.db_id = old_port.db_id
new_port.internal_id = old_port.internal_id
port_found = True
break
if port_found is False:
old_port.status = 'to_be_deleted'
new_vnf.ports.append(old_port)
vnf_found = True
break
if vnf_found is False:
old_vnf.status = 'to_be_deleted'
nffg.vnfs.append(old_vnf)
# Endpoints
for new_endpoint in nffg_new.end_points:
new_endpoint.status = 'new'
for old_endpoint in self.end_points:
endpoint_found = False
for new_endpoint in nffg_new.end_points:
if old_endpoint.id == new_endpoint.id and old_endpoint.type == new_endpoint.type\
and old_endpoint.vlan_id == new_endpoint.vlan_id and old_endpoint.node_id == new_endpoint.node_id\
and old_endpoint.interface == new_endpoint.interface and old_endpoint.remote_ip == new_endpoint.remote_ip\
and old_endpoint.local_ip == new_endpoint.local_ip and old_endpoint.ttl == new_endpoint.ttl\
and old_endpoint.local_ip == new_endpoint.local_ip and old_endpoint.ttl == new_endpoint.ttl\
and old_endpoint.gre_key == new_endpoint.gre_key and old_endpoint.internal_group == new_endpoint.internal_group:
new_endpoint.status = 'already_deployed'
new_endpoint.db_id = old_endpoint.db_id
endpoint_found = True
break
if endpoint_found is False:
old_endpoint.status = 'to_be_deleted'
nffg.end_points.append(old_endpoint)
# Flowrules
for new_flowrule in nffg.flow_rules:
new_flowrule.status = 'new'
for old_flowrule in self.flow_rules:
flowrule_found = False
for new_flowrule in nffg.flow_rules:
if old_flowrule.getDict() == new_flowrule.getDict():
new_flowrule.status = 'already_deployed'
new_flowrule.db_id = old_flowrule.db_id
new_flowrule.internal_id = old_flowrule.internal_id
flowrule_found = True
break
if flowrule_found is False:
old_flowrule.status = 'to_be_deleted'
nffg.flow_rules.append(old_flowrule)
return nffg
def split(self, left_list, right_list, only_left=True, flow_prefix=None):
"""
Splits a nffg adding endpoints and returns both subgraphs (left and right)
Only_left set to true returns only the left subgraph (useful when splitting multiple times).
Please note that the right_subgraph may be wrong because this function is now used to return only the left_subraph
:param left_list:
:param right_list:
:param only_left:
:param flow_prefix:
:return:
:rtype: NF_FG
"""
nffg_left = copy.deepcopy(self)
left_list_original = copy.deepcopy(left_list)
right_list_original = copy.deepcopy(right_list)
domains_dict = dict()
# debug purpose only
left_list_dict = []
right_list_dict = []
if flow_prefix is None:
flow_prefix = ""
else:
flow_prefix = str(flow_prefix)
# Check if there are admissible elements in the lists
total_list = left_list + right_list
for element in total_list:
if type(element) is VNF:
if element not in self.vnfs:
raise Exception("VNF not present in nffg")
if element in left_list:
left_list_dict.append("vnf:"+element.id)
else:
right_list_dict.append("vnf:"+element.id)
elif type(element) is EndPoint:
if element not in self.end_points:
raise Exception("EndPoint not present in nffg")
if element in left_list:
left_list_dict.append("endpoint:"+element.id)
else:
right_list_dict.append("endpoint:"+element.id)
else:
raise TypeError()
# Check links to break
links = dict()
left_flows = []
right_flows = []
for l_element in left_list:
# Endpoint
if type(l_element) is EndPoint:
flowrules = nffg_left.getFlowRulesSendingTrafficFromEndPoint(l_element.id)
for flowrule in flowrules:
for action in flowrule.actions:
if action.output is not None:
if action.output.split(":")[0] == 'endpoint':
for r_element in right_list:
if type(r_element) is EndPoint and r_element.id == action.output.split(":")[1]:
if flowrule.match.port_in not in links:
links[flowrule.match.port_in] = []
links[flowrule.match.port_in].append(action.output)
if flowrule.id not in left_flows:
left_flows.append(flowrule.id)
if action.output not in domains_dict:
domains_dict[action.output] = r_element.domain
break
elif action.output.split(":")[0] == 'vnf':
for r_element in right_list:
# action.output is vnf:vnf_id:port_id
if type(r_element) is VNF and r_element.id == action.output.split(":")[1]:
if flowrule.match.port_in not in links:
links[flowrule.match.port_in] = []
links[flowrule.match.port_in].append(action.output)
if flowrule.id not in left_flows:
left_flows.append(flowrule.id)
if action.output not in domains_dict:
domains_dict[action.output] = r_element.domain
break
# Check flowrules sending traffic to this endpoint
flowrules = nffg_left.getFlowRulesSendingTrafficToEndPoint(l_element.id)
for flowrule in flowrules:
if flowrule.match is not None and flowrule.match.port_in is not None:
if flowrule.match.port_in.split(":")[0] == 'endpoint':
for r_element in right_list:
if type(r_element) is EndPoint and flowrule.match.port_in.split(":")[1] == r_element.id:
if flowrule.match.port_in not in links:
links[flowrule.match.port_in] = []
links[flowrule.match.port_in].append("endpoint:"+l_element.id)
if flowrule.id not in right_flows:
right_flows.append(flowrule.id)
if flowrule.match.port_in not in domains_dict:
domains_dict[flowrule.match.port_in] = r_element.domain
break
elif flowrule.match.port_in.split(":")[0] == 'vnf':
for r_element in right_list:
if type(r_element) is VNF and flowrule.match.port_in.split(":")[1] == r_element.id:
if flowrule.match.port_in not in links:
links[flowrule.match.port_in] = []
links[flowrule.match.port_in].append("endpoint:"+l_element.id)
if flowrule.id not in right_flows:
right_flows.append(flowrule.id)
if flowrule.match.port_in not in domains_dict:
domains_dict[flowrule.match.port_in] = r_element.domain
break
# VNF
if type(l_element) is VNF:
flowrules = nffg_left.getFlowRulesSendingTrafficFromVNF(l_element)
for flowrule in flowrules:
for action in flowrule.actions:
if action.output is not None:
if action.output.split(":")[0] == 'endpoint':
for r_element in right_list:
if type(r_element) is EndPoint and r_element.id == action.output.split(":")[1]:
if flowrule.match.port_in not in links:
links[flowrule.match.port_in] = []
links[flowrule.match.port_in].append(action.output)
if flowrule.id not in left_flows:
left_flows.append(flowrule.id)
if action.output not in domains_dict:
domains_dict[action.output] = r_element.domain
break
elif action.output.split(":")[0] == 'vnf':
for r_element in right_list:
# action.output is vnf:vnf_id:port_id
if type(r_element) is VNF and r_element.id == action.output.split(":")[1]:
if flowrule.match.port_in not in links:
links[flowrule.match.port_in] = []
links[flowrule.match.port_in].append(action.output)
if flowrule.id not in left_flows:
left_flows.append(flowrule.id)
if action.output not in domains_dict:
domains_dict[action.output] = r_element.domain
break
# Check flowrules sending traffic to this vnf
flowrules = nffg_left.getFlowRulesSendingTrafficToVNF(l_element)
for flowrule in flowrules:
if flowrule.match is not None and flowrule.match.port_in is not None:
if flowrule.match.port_in.split(":")[0] == 'endpoint':
for r_element in right_list:
if type(r_element) is EndPoint and flowrule.match.port_in.split(":")[1] == r_element.id:
if flowrule.match.port_in not in links:
links[flowrule.match.port_in] = []
for action in flowrule.actions:
if action.output is not None and action.output.split(":")[1] == l_element.id:
links[flowrule.match.port_in].append(action.output)
if flowrule.id not in right_flows:
right_flows.append(flowrule.id)
if flowrule.match.port_in not in domains_dict:
domains_dict[flowrule.match.port_in] = r_element.domain
break
elif flowrule.match.port_in.split(":")[0] == 'vnf':
for r_element in right_list:
if type(r_element) is VNF and flowrule.match.port_in.split(":")[1] == r_element.id:
if flowrule.match.port_in not in links:
links[flowrule.match.port_in] = []
for action in flowrule.actions:
if action.output is not None and action.output.split(":")[1] == l_element.id:
links[flowrule.match.port_in].append(action.output)
if flowrule.id not in right_flows:
right_flows.append(flowrule.id)
if flowrule.match.port_in not in domains_dict:
domains_dict[flowrule.match.port_in] = r_element.domain
break
# Break links and create endpoints
nffg_right = copy.deepcopy(nffg_left)
# Link already split pointing to endpoint_id of generated endpoint
link_split = dict()
for flow_id in left_flows:
flowrule = nffg_left.getFlowRule(flow_id)
for action in flowrule.actions:
if action.output is not None and action.output in links[flowrule.match.port_in]:
#TODO: multiple output actions to be split not supported
if flowrule.match.port_in+"<->"+action.output not in link_split:
# endpoint_id = "autogenerated_split_"+ flow_prefix + flow_id
endpoint_id = "auto-generated-split_" + flowrule.match.port_in.split(':')[0]\
.replace("endpoint", "ep").replace("vnf", "nf") + ':' + \
flowrule.match.port_in.split(':')[1] + '/' + action.output.split(':')[0]\
.replace("endpoint", "ep").replace("vnf", "nf") +\
':' + action.output.split(':')[1]
link_split[flowrule.match.port_in+"<->"+action.output] = endpoint_id
else:
endpoint_id = link_split[flowrule.match.port_in+"<->"+action.output]
flowrule_left = flowrule
output = action.output
action.output = "endpoint:"+endpoint_id
flowrule_left.id = flow_prefix + flow_id+"_1"
if nffg_left.getEndPoint(endpoint_id) is None:
nffg_left.addEndPoint(EndPoint(_id=endpoint_id, remote_domain=domains_dict[output]))
if nffg_left.isDuplicatedFlowrule(flowrule_left) is True:
nffg_left.flow_rules.remove(flowrule_left)
# Right graph - may be broken
flowrule_2 = nffg_right.getFlowRule(flow_id)
flowrule_2.id = flow_prefix + flow_id+"_2"
flowrule_2.match = Match(port_in="endpoint:"+endpoint_id)
flowrule_2.actions = []
flowrule_2.actions.append(Action(output=output))
if nffg_right.getEndPoint(endpoint_id) is None:
nffg_right.addEndPoint(EndPoint(_id=endpoint_id))
for flow_id in right_flows:
flowrule = nffg_right.getFlowRule(flow_id)
for action in flowrule.actions:
if action.output is not None and action.output in links[flowrule.match.port_in]:
flowrule_right = flowrule
output = action.output
if action.output+"<->"+flowrule.match.port_in not in link_split:
link_split[action.output+"<->"+flowrule.match.port_in] = "auto-generated-split_" +\
action.output.split(':')[0].replace("endpoint", "ep").replace("vnf", "nf") + ':' +\
action.output.split(':')[1] + '/' + flowrule.match.port_in.split(':')[0]\
.replace("endpoint", "ep").replace("vnf", "nf") + ':' + flowrule.match.port_in.split(':')[1]
endpoint_id = link_split[action.output+"<->"+flowrule.match.port_in]
action.output = "endpoint:"+endpoint_id
flowrule_right.id = flow_prefix + flow_id+"_1"
if nffg_right.getEndPoint(endpoint_id) is None:
nffg_right.addEndPoint(EndPoint(_id=endpoint_id))
# Left graph
flowrule_left = nffg_left.getFlowRule(flow_id)
flowrule_left.id = flow_prefix + flow_id+"_2"
flowrule_left.match = Match(port_in="endpoint:"+endpoint_id)
flowrule_left.actions = []
flowrule_left.actions.append(Action(output=output))
if nffg_left.getEndPoint(endpoint_id) is None:
nffg_left.addEndPoint(EndPoint(_id=endpoint_id,
remote_domain=domains_dict[flowrule.match.port_in]))
if nffg_left.isDuplicatedFlowrule(flowrule_left) is True:
nffg_left.flow_rules.remove(flowrule_left)
# Delete obsolete parts from graphs
marked_vnfs = None
marked_endpoints=None
for element in right_list_original:
if type(element) is VNF:
marked_vnfs, marked_endpoints = self.deleteVNFAndConnections(nffg_left, element, marked_vnfs,
marked_endpoints)
elif type(element) is EndPoint:
marked_vnfs, marked_endpoints = self.deleteEndpointAndConnections(nffg_left, element.id, marked_vnfs,
marked_endpoints)
marked_vnfs = None
marked_endpoints=None
for element in left_list_original:
if type(element) is VNF:
marked_vnfs, marked_endpoints = self.deleteVNFAndConnections(nffg_right, element, marked_vnfs,
marked_endpoints)
elif type(element) is EndPoint:
marked_vnfs, marked_endpoints = self.deleteEndpointAndConnections(nffg_right, element.id, marked_vnfs,
marked_endpoints)
if only_left is True:
return nffg_left
return nffg_left, nffg_right
def deleteVNFAndConnections(self, nffg, vnf, marked_vnfs=None, marked_endpoints=None):
"""
Deletes the VNF and recursively connected VNFs and Endpoints.
"""
marked_vnfs = marked_vnfs or []
if vnf is not None and vnf.id not in marked_vnfs:
marked_vnfs.append(vnf.id)
for port in vnf.ports:
out_flows = nffg.deleteIncomingFlowrules("vnf:"+vnf.id+":"+port.id)
for flow in out_flows:
for action in flow.actions:
if action.output is not None:
if action.output.split(":")[0] == "vnf":
marked_vnfs, marked_endpoints = self.deleteVNFAndConnections(nffg, nffg.getVNF(
action.output.split(":")[1]), marked_vnfs, marked_endpoints)
break
elif action.output.split(":")[0] == "endpoint":
marked_vnfs, marked_endpoints = self.deleteEndpointAndConnections(
nffg, action.output.split(":")[1], marked_vnfs, marked_endpoints)
break
in_flows = nffg.deleteOutcomingFlowrules("vnf:"+vnf.id+":"+port.id)
for flow in in_flows:
if flow.match is not None and flow.match.port_in is not None:
if flow.match.port_in.split(":")[0] == "vnf":
marked_vnfs, marked_endpoints = self.deleteVNFAndConnections(
nffg, nffg.getVNF(flow.match.port_in.split(":")[1]), marked_vnfs, marked_endpoints)
elif flow.match.port_in.split(":")[0] == "endpoint":
marked_vnfs, marked_endpoints = self.deleteEndpointAndConnections(
nffg, flow.match.port_in.split(":")[1], marked_vnfs, marked_endpoints)
nffg.vnfs.remove(nffg.getVNF(vnf.id))
return marked_vnfs, marked_endpoints
def deleteEndpointAndConnections(self, nffg, endpoint_id, marked_vnfs=None, marked_endpoints=None):
"""
Deletes the Endpoint and recursively connected VNFs and Endpoints.
"""
marked_endpoints = marked_endpoints or []
if endpoint_id is not None and endpoint_id not in marked_endpoints:
marked_endpoints.append(endpoint_id)
out_flows = nffg.deleteIncomingFlowrules("endpoint:"+endpoint_id)
for flow in out_flows:
for action in flow.actions:
if action.output is not None:
if action.output.split(":")[0] == "vnf":
marked_vnfs, marked_endpoints = self.deleteVNFAndConnections(
nffg, nffg.getVNF(action.output.split(":")[1]), marked_vnfs, marked_endpoints)
break
elif action.output.split(":")[0] == "endpoint":
marked_vnfs, marked_endpoints = self.deleteEndpointAndConnections(
nffg, action.output.split(":")[1], marked_vnfs, marked_endpoints)
break
in_flows = nffg.deleteOutcomingFlowrules("endpoint:"+endpoint_id)
for flow in in_flows:
if flow.match is not None and flow.match.port_in is not None:
if flow.match.port_in.split(":")[0] == "vnf":
marked_vnfs, marked_endpoints = self.deleteVNFAndConnections(
nffg, nffg.getVNF(flow.match.port_in.split(":")[1]), marked_vnfs, marked_endpoints)
elif flow.match.port_in.split(":")[0] == "endpoint":
marked_vnfs, marked_endpoints = self.deleteEndpointAndConnections(
nffg, flow.match.port_in.split(":")[1], marked_vnfs, marked_endpoints)
nffg.end_points.remove(nffg.getEndPoint(endpoint_id))
return marked_vnfs, marked_endpoints
def changeEndpointId(self, old_id, new_id):
ep = self.getEndPoint(old_id)
ep.id = new_id
for flow in self.getFlowRulesSendingTrafficFromEndPoint(old_id):
flow.match.port_in = "endpoint:" + new_id
for flow in self.getFlowRulesSendingTrafficToEndPoint(old_id):
for action in flow.actions:
if action.output is not None:
action.output = new_id
def getRemoteGeneratedEndpoints(self, domain_2, remote_nffg):
"""
Given the local autogenerated endpoints associated to domain_2, returns the same endpoints present on remote_nffg (they can have different IDs)
"""
remote_endpoints = []
for local_endpoint in self.end_points:
found = False
if local_endpoint.remote_domain == domain_2:
for local_flowrule in self.getFlowRulesSendingTrafficToEndPoint(local_endpoint.id):
remote_flowrule = remote_nffg.getFlowRule(local_flowrule.id[:-1] + "2")
if remote_flowrule is not None:
port_in = remote_flowrule.match.port_in
if port_in is not None and port_in.startswith("endpoint:autogenerated_split_"):
remote_endpoints.append(remote_nffg.getEndPoint(port_in.split(":")[1]))
found = True
break
if found is True:
continue
for local_flowrule in self.getFlowRulesSendingTrafficFromEndPoint(local_endpoint.id):
remote_flowrule = remote_nffg.getFlowRule(local_flowrule.id[:-1] + "1")
if remote_flowrule is not None:
for action in remote_flowrule.actions:
if action.output is not None and action.output.startswith("endpoint.autogenerated_split_"):
remote_endpoints.append(remote_nffg.getEndPoint(action.output.split(":")[1]))
found = True
break
if found is True:
break
return remote_endpoints
def getAutogeneratedEndpoints(self, remote_domain):
"""
Returns the autogenerated endpoints associated to remote_domain
:param remote_domain:
:return:
:rtype: list of EndPoint
"""
endpoints = []
for end_point in self.end_points:
if end_point.id.startswith("autogenerated_split_") and end_point.remote_domain == remote_domain:
endpoints.append(end_point)
return endpoints
"""
def join(self, nffg_2):
'''
Merges two graphs that have been split previously. nffg_2 is left intact
'''
nffg_2 = copy.deepcopy(nffg_2)
removed_endpoints = []
#Search for flowrules split and join them
for flowrule in self.flow_rules[:]:
if flowrule.id[-2] == '_':
if flowrule.match is not None and flowrule.match.port_in is not None
and flowrule.match.port_in.startswith("endpoint:autogenerated_split_"):
attaching_outgoing_flowrule = []
external_flowrule = nffg_2.getFlowRule(flowrule.id[:-1]+"1")
if external_flowrule is None:
continue
attaching_outgoing_flowrule.append(external_flowrule)
ingoing_flowrules = []
ingoing_flowrules.append(flowrule)
self.flow_rules = self.flow_rules + self.mergeFlowrules(attaching_outgoing_flowrule,
ingoing_flowrules, True)
self.flow_rules.remove(flowrule)
nffg_2.flow_rules.remove(external_flowrule)
if flowrule.match.port_in.split(":")[1] not in removed_endpoints:
removed_endpoints.append(flowrule.match.port_in.split(":")[1])
elif flowrule.actions is not None:
for action in flowrule.actions:
if action.output is not None and action.output.startswith("endpoint:autogenerated_split_"):
attaching_ingoing_flowrule = []
external_flowrule = nffg_2.getFlowRule(flowrule.id[:-1]+"2")
if external_flowrule is None:
continue
attaching_ingoing_flowrule.append(external_flowrule)
outgoing_flowrules = []
outgoing_flowrules.append(flowrule)
self.flow_rules = self.flow_rules + self.mergeFlowrules(outgoing_flowrules,
attaching_ingoing_flowrule, True)
self.flow_rules.remove(flowrule)
nffg_2.flow_rules.remove(external_flowrule)
if action.output.split(":")[1] not in removed_endpoints:
removed_endpoints.append(action.output.split(":")[1])
self.flow_rules = self.flow_rules + nffg_2.flow_rules
# Add the VNFs of the second graph
for new_vnf in nffg_2.vnfs:
self.vnfs.append(new_vnf)
# Add the end-points of the second graph
for new_end_point in nffg_2.end_points:
self.end_points.append(new_end_point)
for end_point in self.end_points[:]:
if end_point.id in removed_endpoints:
self.end_points.remove(end_point)
"""
def deleteEndPointConnections(self, endpoint_id):
return self.deleteConnections("endpoint:"+endpoint_id)
def deleteConnectionsBetweenVNFs(self, vnf1_id, port_vnf1_id, vnf2_id, port_vnf2_id):
deleted_flows=[]
for flow_rule in self.flow_rules[:]:
if flow_rule.match.port_in == 'vnf:'+vnf1_id+':'+port_vnf1_id:
for action in flow_rule.actions:
if action.output == 'vnf:'+vnf2_id+':'+port_vnf2_id:
deleted_flows.append(copy.deepcopy(flow_rule))
self.flow_rules.remove(flow_rule)
if flow_rule.match.port_in == 'vnf:'+vnf2_id+':'+port_vnf2_id:
for action in flow_rule.actions:
if action.output == 'vnf:'+vnf1_id+':'+port_vnf1_id:
deleted_flows.append(copy.deepcopy(flow_rule))
self.flow_rules.remove(flow_rule)
return deleted_flows
def deleteVNFConnections(self, vnf_id):
vnf = self.getVNF(vnf_id)
deleted_flows=[]
for port in vnf.ports:
deleted_flows = deleted_flows + self.deleteConnections("vnf:"+vnf_id+":"+port.id)
return deleted_flows
def deleteConnections(self, node_id):
deleted_flows = self.deleteIncomingFlowrules(node_id)
deleted_flows = deleted_flows + self.deleteOutcomingFlowrules(node_id)
return deleted_flows
def deleteIncomingFlowrules(self, node_id):
deleted_flows = []
for flow_rule in self.flow_rules[:]:
if flow_rule.match.port_in == node_id:
deleted_flows.append(copy.deepcopy(flow_rule))
self.flow_rules.remove(flow_rule)
continue
return deleted_flows
def deleteOutcomingFlowrules(self, node_id):
deleted_flows = []
for flow_rule in self.flow_rules[:]:
for action in flow_rule.actions:
if action.output == node_id:
deleted_flows.append(copy.deepcopy(flow_rule))
self.flow_rules.remove(flow_rule)
return deleted_flows
def getNextAvailableEndPointId(self):
for id_number in range(1, len(self.end_points) + 2):
if self.getEndPoint(str(id_number).zfill(8)) is None:
return str(id_number).zfill(8)
def getNextAvailableFlowRuleId(self):
for id_number in range(1, len(self.flow_rules) + 2):
if self.getFlowRule(str(id_number).zfill(8)) is None:
return str(id_number).zfill(8)
class VNF(object):
def __init__(self, _id=None, name=None, functional_capability=None, vnf_template_location=None, ports=None,
groups=None, template=None, status=None, db_id=None, internal_id=None, availabilty_zone=None,
domain=None, unify_control=None, unify_env_variables=None):
self.id = _id
self.name = name
self.functional_capability = functional_capability
self.vnf_template_location = vnf_template_location
self.template = template