-
Notifications
You must be signed in to change notification settings - Fork 2
/
nffg.py
3637 lines (3349 loc) · 137 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
# Copyright 2017 Janos Czentye, Balazs Nemeth, Balazs Sonkoly
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Abstract class and implementation for basic operations with a single NF-FG, such
as building, parsing, processing NF-FG, helper functions, etc.
"""
import copy
import itertools
import json
import logging
import math
import pprint
import re
from collections import defaultdict, OrderedDict
from copy import deepcopy
import networkx as nx
from networkx.exception import NetworkXError
from .nffg_elements import (Node, NodeNF, NodeInfra, NodeResource, NodeSAP,
Link, EdgeSGLink, EdgeLink, EdgeReq, Port, Flowrule,
NFFGModel, Element)
VERSION = "1.0"
VERBOSE = 5
class AbstractNFFG(object):
"""
Abstract class for managing single NF-FG data structure.
The NF-FG data model is described in YANG. This class provides the
interfaces with the high level data manipulation functions.
"""
__slots__ = ()
# Default domain value
DEFAULT_DOMAIN = NodeInfra.DEFAULT_DOMAIN
"""Default domain value"""
# Infra types
TYPE_INFRA_SDN_SW = NodeInfra.TYPE_SDN_SWITCH
TYPE_INFRA_EE = NodeInfra.TYPE_EE
TYPE_INFRA_STATIC_EE = NodeInfra.TYPE_STATIC_EE
TYPE_INFRA_BISBIS = NodeInfra.TYPE_BISBIS
# Node types
TYPE_INFRA = Node.INFRA
TYPE_NF = Node.NF
TYPE_SAP = Node.SAP
# Link types
TYPE_LINK_STATIC = Link.STATIC
TYPE_LINK_DYNAMIC = Link.DYNAMIC
TYPE_LINK_SG = Link.SG
TYPE_LINK_REQUIREMENT = Link.REQUIREMENT
# Port constants
PORT_ROLE_CONSUMER = Port.ROLE_CONSUMER
PORT_ROLE_PROVIDER = Port.ROLE_PROVIDER
# Mapping mode operations
MODE_ADD = "ADD"
MODE_DEL = "DELETE"
MODE_REMAP = "REMAP"
# Element operation
OP_CREATE = Element.OP_CREATE
OP_REPLACE = Element.OP_REPLACE
OP_MERGE = Element.OP_MERGE
OP_REMOVE = Element.OP_REMOVE
OP_DELETE = Element.OP_DELETE
# Element status
STATUS_INIT = Element.STATUS_INIT
STATUS_PENDING = Element.STATUS_PENDING
STATUS_DEPLOY = Element.STATUS_DEPLOY
STATUS_RUN = Element.STATUS_RUN
STATUS_STOP = Element.STATUS_STOP
STATUS_FAIL = Element.STATUS_FAIL
# Mapping process status
MAP_STATUS_SKIPPED = "SKIPPED" # mark NFFG as skipped for ESCAPE
version = VERSION
##############################################################################
# NFFG specific functions
##############################################################################
def add_nf (self):
"""
Add a single NF node to the NF-FG.
"""
raise NotImplementedError
def add_sap (self):
"""
Add a single SAP node to the NF-FG.
"""
raise NotImplementedError
def add_infra (self):
"""
Add a single infrastructure node to the NF-FG.
"""
raise NotImplementedError
def add_link (self, src, dst):
"""
Add a static or dynamic infrastructure link to the NF-FG.
:param src: source port
:param dst: destination port
"""
raise NotImplementedError
def add_sglink (self, src, dst):
"""
Add an SG link to the NF-FG.
:param src: source port
:param dst: destination port
"""
raise NotImplementedError
def add_req (self, src, dst):
"""
Add a requirement link to the NF-FG.
:param src: source port
:param dst: destination port
"""
raise NotImplementedError
def add_node (self, node):
"""
Add a single node to the NF-FG.
:param node: node object
"""
raise NotImplementedError
def del_node (self, id):
"""
Remove a single node from the NF-FG.
:param id: id of the node
"""
raise NotImplementedError
def add_edge (self, src, dst, link):
"""
Add an edge to the NF-FG.
:param src: source port
:param dst: destination port
:param link: link object
"""
raise NotImplementedError
def del_edge (self, src, dst):
"""
Remove an edge from the NF-FG.
:param src: source port
:param dst: destination port
"""
raise NotImplementedError
##############################################################################
# General functions for create/parse/dump/convert NFFG
##############################################################################
@classmethod
def parse (cls, data):
"""
General function for parsing data as a new :any::`NFFG` object and return
with its reference.
:param data: raw data
:type data: str
:return: parsed NFFG as an XML object
:rtype: :class:`Virtualizer`
"""
raise NotImplementedError
def dump (self):
"""
General function for dumping :any::`NFFG` according to its format to
plain text.
:return: plain text representation
:rtype: str
"""
raise NotImplementedError
class NFFG(AbstractNFFG):
"""
Internal NFFG representation based on networkx.
"""
__slots__ = ('network', 'id', 'name', 'service_id', 'metadata', 'mode',
'status', 'version')
def __init__ (self, id=None, name=None, service_id=None, mode=None,
metadata=None, status=None, version=VERSION):
"""
Init.
:param id: optional NF-FG identifier (generated by default)
:type id: str or int
:param name: optional NF-FG name (generated by default)
:type name: str
:param service_id: service id this NFFG is originated from
:type service_id: str or int
:param mode: describe how to handle the defined elements (default: ADD)
:type mode: str
:param metadata: optional metadata for NFFG
:type metadata: dict
:param status: optional info for NFFG
:type status: str
:param version: optional version (default: 1.0)
:type version: str
:return: None
"""
super(NFFG, self).__init__()
self.network = nx.MultiDiGraph()
self.id = str(id) if id is not None else Element.generate_unique_id()
self.name = name
self.service_id = service_id
self.metadata = OrderedDict(metadata if metadata else ())
self.mode = mode
self.status = status
self.version = version
##############################################################################
# Element iterators
##############################################################################
@property
def nfs (self):
"""
Iterate over the NF nodes.
:return: iterator of NFs
:rtype: collections.Iterator
"""
return (node for id, node in self.network.nodes_iter(data=True) if
node.type == Node.NF)
@property
def saps (self):
"""
Iterate over the SAP nodes.
:return: iterator of SAPs
:rtype: collections.Iterator
"""
return (node for id, node in self.network.nodes_iter(data=True) if
node.type == Node.SAP)
@property
def infras (self):
"""
Iterate over the Infra nodes.
:return: iterator of Infra node
:rtype: collections.Iterator
"""
return (node for id, node in self.network.nodes_iter(data=True) if
node.type == Node.INFRA)
@property
def links (self):
"""
Iterate over the link edges.
:return: iterator of edges
:rtype: collections.Iterator
"""
return (link for src, dst, link in self.network.edges_iter(data=True) if
link.type == Link.STATIC or link.type == Link.DYNAMIC)
@property
def sg_hops (self):
"""
Iterate over the service graph hops.
:return: iterator of SG edges
:rtype: collections.Iterator
"""
return (link for s, d, link in self.network.edges_iter(data=True) if
link.type == Link.SG)
@property
def reqs (self):
"""
Iterate over the requirement edges.
:return: iterator of requirement edges
:rtype: collections.Iterator
"""
return (link for s, d, link in self.network.edges_iter(data=True) if
link.type == Link.REQUIREMENT)
##############################################################################
# Magic functions mostly for dict specific behaviour
##############################################################################
def __str__ (self):
"""
Return the string representation.
:return: string representation
:rtype: str
"""
return "NFFG(id=%s name=%s, version=%s)" % (
self.id, self.name, self.version)
def __contains__ (self, item):
"""
Return True if item exist in the NFFG, False otherwise.
:param item: node object or id
:type item: :any:`Node` or str
:return: item is in the NFFG
:rtype: bool
"""
if isinstance(item, Node):
item = item.id
return item in self.network
def __iter__ (self, data=False):
"""
Return an iterator over the nodes.
:param data: If True return a two-tuple of node and node data dictionary
:type data: bool
:return: An iterator over nodes.
"""
return self.network.nodes_iter(data=data)
def __len__ (self):
"""
Return the number of nodes.
:return: number of nodes
:rtype: int
"""
return len(self.network)
def __getitem__ (self, item):
"""
Return the object given by the id: item.
:param item: node id
:return: node object
"""
return self.network.node[item]
##############################################################################
# Builder design pattern related functions
##############################################################################
def add_node (self, node):
"""
Add a Node to the structure.
:param node: a Node object
:type node: :any:`Node`
:return: None
"""
self.network.add_node(node.id)
self.network.node[node.id] = node
def del_node (self, node):
"""
Remove the node from the structure.
:param node: node id or node object or a port object of the node
:type node: str or :any:`Node` or :any`Port`
:return: the actual node is found and removed or not
:rtype: bool
"""
try:
if isinstance(node, Node):
node = node.id
elif isinstance(node, Port):
node = node.node.id
self.network.remove_node(node)
return True
except NetworkXError:
# There was no node in the graph
return False
def add_edge (self, src, dst, link):
"""
Add an Edge to the structure.
:param src: source node id or Node object or a Port object
:type src: str or :any:`Node` or :any`Port`
:param dst: destination node id or Node object or a Port object
:type dst: str or :any:`Node` or :any`Port`
:param link: edge data object
:type link: :any:`Link`
:return: None
"""
if isinstance(src, Node):
src = src.id
elif isinstance(src, Port):
src = src.node.id
if isinstance(dst, Node):
dst = dst.id
elif isinstance(dst, Port):
dst = dst.node.id
self.network.add_edge(src, dst, key=link.id)
self.network[src][dst][link.id] = link
def del_edge (self, src, dst, id=None):
"""
Remove the edge(s) between two nodes.
:param src: source node id or Node object or a Port object
:type src: str or :any:`Node` or :any`Port`
:param dst: destination node id or Node object or a Port object
:type dst: str or :any:`Node` or :any`Port`
:param id: unique id of the edge (otherwise remove all)
:type id: str or int
:return: the actual node is found and removed or not
:rtype: bool
"""
try:
if isinstance(src, Node):
src = src.id
elif isinstance(src, Port):
src = src.node.id
if isinstance(dst, Node):
dst = dst.id
elif isinstance(dst, Port):
dst = dst.node.id
if id is not None:
self.network.remove_edge(src, dst, key=id)
else:
self.network[src][dst].clear()
return True
except NetworkXError:
# There was no node in the graph
return False
def add_nf (self, nf=None, id=None, name=None, func_type=None, dep_type=None,
cpu=None, mem=None, storage=None, cost=None, delay=None,
bandwidth=None):
"""
Add a Network Function to the structure.
:param nf: add this explicit NF object instead of create one
:type nf: :any:`NodeNF`
:param id: optional id
:type id: str or ints
:param name: optional name
:type name: str
:param func_type: functional type (default: "None")
:type func_type: str
:param dep_type: deployment type (default: "None")
:type dep_type: str
:param cpu: CPU resource
:type cpu: float
:param mem: memory resource
:type mem: float
:param storage: storage resource
:type storage: float
:type cost: float
:param cost: NF cost deployement limit.
:param delay: delay property of the Node
:type delay: float
:param bandwidth: bandwidth property of the Node
:type bandwidth: float
:return: newly created node
:rtype: :any:`NodeNF`
"""
if nf is None:
if any(i is not None for i in (cpu, mem, storage, delay, bandwidth)):
res = NodeResource(cpu=cpu, mem=mem, storage=storage, delay=delay,
bandwidth=bandwidth, cost=cost)
else:
res = None
nf = NodeNF(id=id, name=name, func_type=func_type, dep_type=dep_type,
res=res)
self.add_node(nf)
return nf
def add_sap (self, sap_obj=None, id=None, name=None, binding=None, sap=None,
technology=None, delay=None, bandwidth=None, cost=None,
controller=None, orchestrator=None, l2=None, l4=None,
metadata=None):
"""
Add a Service Access Point to the structure.
:param sap_obj: add this explicit SAP object instead of create one
:type sap_obj: :any:`NodeSAP`
:param id: optional id
:type id: str or int
:param name: optional name
:type name: str
:param binding: interface binding
:type binding: str
:param sap: inter-domain SAP identifier
:type sap: str
:param technology: technology
:type technology: str
:param delay: delay
:type delay: float
:param bandwidth: bandwidth
:type bandwidth: float
:param cost: cost
:type cost: str
:param controller: controller
:type controller: str
:param orchestrator: orchestrator
:type orchestrator: str
:param l2: l2
:param l2: str
:param l4: l4
:type l4: str
:param metadata: metadata related to Node
:type metadata: dict
:return: newly created node
:rtype: :any:`NodeSAP`
"""
if sap_obj is None:
sap_obj = NodeSAP(id=id, name=name, binding=binding, metadata=metadata)
self.add_node(sap_obj)
return sap_obj
def add_infra (self, infra=None, id=None, name=None, domain=None,
infra_type=None, cpu=None, mem=None, storage=None, cost=None,
zone=None, delay=None, bandwidth=None):
"""
Add an Infrastructure Node to the structure.
:param infra: add this explicit Infra object instead of create one
:type infra: :any:`NodeInfra`
:param id: optional id
:type id: str or int
:param name: optional name
:type name: str
:param domain: domain of the Infrastructure Node (default: None)
:type domain: str
:param infra_type: type of the Infrastructure Node (default: 0)
:type infra_type: int or str
:param cpu: CPU resource
:type cpu: float
:param mem: memory resource
:type mem: float
:param storage: storage resource
:type storage: float
:param cost: cost
:type cost: str
:param zone: zone
:type zone: str
:param delay: delay property of the Node
:type delay: float
:param bandwidth: bandwidth property of the Node
:type bandwidth: float
:return: newly created node
:rtype: :any:`NodeInfra`
"""
if infra is None:
if any(i is not None for i in (cpu, mem, storage, delay, bandwidth)):
res = NodeResource(cpu=cpu, mem=mem, storage=storage, cost=cost,
zone=zone, bandwidth=bandwidth, delay=delay)
else:
res = None
infra = NodeInfra(id=id, name=name, domain=domain, infra_type=infra_type,
res=res)
self.add_node(infra)
return infra
def add_link (self, src_port, dst_port, link=None, id=None, dynamic=False,
backward=False, delay=None, bandwidth=None, cost=None,
qos=None):
"""
Add a Link to the structure.
:param link: add this explicit Link object instead of create one
:type link: :any:`EdgeLink`
:param src_port: source port
:type src_port: :any:`Port`
:param dst_port: destination port
:type dst_port: :any:`Port`
:param id: optional link id
:type id: str or int
:param backward: the link is a backward link compared to an another Link
:type backward: bool
:param delay: delay resource
:type delay: float
:param dynamic: set the link dynamic (default: False)
:type dynamic: bool
:param bandwidth: bandwidth resource
:type bandwidth: float
:param cost: cost
:type cost: str
:param qos: traffic QoS class
:type qos: str
:return: newly created edge
:rtype: :any:`EdgeLink`
"""
if link is None:
type = Link.DYNAMIC if dynamic else Link.STATIC
link = EdgeLink(src=src_port, dst=dst_port, type=type, id=id,
backward=backward, delay=delay, bandwidth=bandwidth,
cost=cost, qos=qos)
else:
link.src, link.dst = src_port, dst_port
self.add_edge(src_port.node, dst_port.node, link)
return link
def add_undirected_link (self, port1, port2, p1p2id=None, p2p1id=None,
dynamic=False, delay=None, bandwidth=None,
cost=None, qos=None):
"""
Add two Links to the structure, in both directions.
:param port1: source port
:type port1: :any:`Port`
:param port2: destination port
:type port2: :any:`Port`
:param p1p2id: optional link id from port1 to port2
:type p1p2id: str or int
:param p2p1id: optional link id from port2 to port1
:type p2p1id: str or int
:param delay: delay resource of both links
:type delay: float
:param dynamic: set the link dynamic (default: False)
:type dynamic: bool
:param bandwidth: bandwidth resource of both links
:type bandwidth: float
:param cost: cost
:type cost: str
:param qos: traffic QoS class
:type qos: str
:return: newly created edge tuple in (p1->p2, p2->p1)
:rtype: :any:(`EdgeLink`, `EdgeLink`)
"""
p1p2Link = self.add_link(port1, port2, id=p1p2id, dynamic=dynamic,
backward=False, delay=delay, bandwidth=bandwidth,
cost=cost, qos=qos)
p2p1Link = self.add_link(port2, port1, id=p2p1id, dynamic=dynamic,
backward=True, delay=delay, bandwidth=bandwidth,
cost=cost, qos=qos)
return p1p2Link, p2p1Link
def add_sglink (self, src_port, dst_port, hop=None, id=None, flowclass=None,
tag_info=None, delay=None, bandwidth=None, constraints=None,
additional_actions=None):
"""
Add a SG next hop edge to the structure.
:param hop: add this explicit SG Link object instead of create one
:type hop: :any:`EdgeSGLink`
:param src_port: source port
:type src_port: :any:`Port`
:param dst_port: destination port
:type dst_port: :any:`Port`
:param id: optional link id
:type id: str or int
:param flowclass: flowclass of SG next hop link
:type flowclass: str
:param tag_info: tag info
:type tag_info: str
:param delay: delay requested on link
:type delay: float
:param bandwidth: bandwidth requested on link
:type bandwidth: float
:param constraints: optional Constraints object
:type constraints: :class:`Constraints`
:param additional_actions: additional actions
:type additional_actions: str
:return: newly created edge
:rtype: :any:`EdgeSGLink`
"""
if hop is None:
hop = EdgeSGLink(src=src_port, dst=dst_port, id=id, flowclass=flowclass,
tag_info=tag_info, bandwidth=bandwidth, delay=delay,
constraints=constraints,
additional_actions=additional_actions)
self.add_edge(src_port.node, dst_port.node, hop)
return hop
def add_req (self, src_port, dst_port, req=None, id=None, delay=None,
bandwidth=None, sg_path=None):
"""
Add a requirement edge to the structure.
:param req: add this explicit Requirement Link object instead of create one
:type req: :any:`EdgeReq`
:param src_port: source port
:type src_port: :any:`Port`
:param dst_port: destination port
:type dst_port: :any:`Port`
:param id: optional link id
:type id: str or int
:param delay: delay resource
:type delay: float
:param bandwidth: bandwidth resource
:type bandwidth: float
:param sg_path: list of ids of sg_links represents end-to-end requirement
:type sg_path: list or tuple
:return: newly created edge
:rtype: :any:`EdgeReq`
"""
if req is None:
req = EdgeReq(src=src_port, dst=dst_port, id=id, delay=delay,
bandwidth=bandwidth, sg_path=sg_path)
self.add_edge(src_port.node, dst_port.node, req)
return req
def add_metadata (self, name, value):
"""
Add metadata with the given `name`.
:param name: metadata name
:type name: str
:param value: metadata value
:type value: str
:return: the :class:`NFFG` object to allow function chaining
:rtype: :class:`NFFG`
"""
self.metadata[name] = value
return self
def get_metadata (self, name):
"""
Return the value of metadata.
:param name: name of the metadata
:type name: str
:return: metadata value
:rtype: str
"""
return self.metadata.get(name)
def del_metadata (self, name):
"""
Remove the metadata from the :class:`NFFG`. If no metadata is given all the
metadata will be removed.
:param name: name of the metadata
:type name: str
:return: removed metadata or None
:rtype: str or None
"""
if name is None:
self.metadata.clear()
else:
return self.metadata.pop(name, None)
def dump (self):
"""
Convert the NF-FG structure to a NFFGModel format and return the plain
text representation.
:return: text representation
:rtype: str
"""
# Create the model
nffg = NFFGModel(id=self.id, name=self.name, service_id=self.service_id,
version=self.version, mode=self.mode,
metadata=self.metadata)
# Load Infras
for infra in self.infras:
nffg.node_infras.append(infra)
# Load SAPs
for sap in self.saps:
nffg.node_saps.append(sap)
# Load NFs
for nf in self.nfs:
nffg.node_nfs.append(nf)
# Load Links
for link in self.links:
nffg.edge_links.append(link)
# Load SG next hops
for hop in self.sg_hops:
nffg.edge_sg_nexthops.append(hop)
# Load Requirements
for req in self.reqs:
nffg.edge_reqs.append(req)
# Dump
return nffg.dump()
def dump_to_json (self):
"""
Return the NF-FG structure in JSON compatible format.
:return: NFFG as a valid JSON
:rtype: dict
"""
return json.loads(self.dump())
@classmethod
def parse (cls, raw_data):
"""
Read the given JSON object structure and try to convert to an NF-FG
representation as an :class:`NFFG`
:param raw_data: raw NF-FG description as a string
:type raw_data: str
:return: the parsed NF-FG representation
:rtype: :class:`NFFG`
"""
# Parse text
model = NFFGModel.parse(raw_data)
# Create new NFFG
nffg = NFFG(id=model.id, name=model.name, service_id=model.service_id,
version=model.version, mode=model.mode, metadata=model.metadata)
# Load Infras
for infra in model.node_infras:
nffg.add_node(infra)
# Load SAPs
for sap in model.node_saps:
nffg.add_node(sap)
# Load NFs
for nf in model.node_nfs:
nffg.add_node(nf)
# Load Links
for link in model.edge_links:
if link.src.node.type == NFFG.TYPE_NF or \
link.dst.node.type == NFFG.TYPE_NF:
link.type = str(NFFG.TYPE_LINK_DYNAMIC)
nffg.add_edge(link.src.node, link.dst.node, link)
# Load SG next hops
for hop in model.edge_sg_nexthops:
nffg.add_edge(hop.src.node, hop.dst.node, hop)
# Load Requirements
for req in model.edge_reqs:
nffg.add_edge(req.src.node, req.dst.node, req)
return nffg
@staticmethod
def parse_from_file (path):
"""
Parse NFFG from file given by the path.
:param path: file path
:type path: str
:return: the parsed NF-FG representation
:rtype: :class:`NFFG`
"""
with open(path) as f:
return NFFG.parse(f.read())
##############################################################################
# Helper functions
##############################################################################
def is_empty (self):
"""
Return True if the NFFG contains no Node.
:return: :class:`NFFG` object is empty or not
:rtype: bool
"""
return len(self.network) == 0
def is_infrastructure (self):
"""
Return True if the NFFG is an infrastructure view with Infrastructure nodes.
:return: the NFFG is an infrastructure view
:rtype: bool
"""
return sum([1 for i in self.infras]) != 0
def is_SBB (self):
"""
Return True if the topology detected as a trivial SingleBiSBiS view,
which consist of only one Infra node with type: ``BiSBiS``.
:return: SingleBiSBiS or not
:rtype: bool
"""
itype = [i.infra_type for i in self.infras]
return len(itype) == 1 and itype.pop() == self.TYPE_INFRA_BISBIS
def is_bare (self):
"""
Return True if the topology does not contain any NF or flowrules need to
install or remap.
:return: is bare topology or not
:rtype: bool
"""
# If there is no VNF
if len([v for v in self.nfs]) == 0:
fr_sum = sum([sum(1 for fr in i.ports.flowrules) for i in self.infras])
# And there is no flowrule in the ports
if fr_sum == 0:
sg_sum = len([sg for sg in self.sg_hops])
# And there is not SG hop
if sg_sum == 0:
e2e_sum = len([sg for sg in self.reqs])
if e2e_sum == 0:
return True
return False
def is_virtualized (self):
"""
Return True if the topology contains at least one virtualized BiSBiS node.
:return: contains any NF or not
:rtype: bool
"""
return len([i for i in self.infras if
i.infra_type not in (self.TYPE_INFRA_SDN_SW, self.TYPE_INFRA_EE,
self.TYPE_INFRA_STATIC_EE)]) > 0
def get_stat (self):
"""
:return:
"""
return dict(infras=[i.id for i in self.infras],
nfs=[n.id for n in self.nfs],
saps=[s.id for s in self.saps],
sg_hops=[h.id for h in self.sg_hops])
def real_neighbors_iter (self, node):
"""
Return with an iterator over the id of neighbours of the given Node not
counting the SG and E2E requirement links.
:param node: examined :any:`Node` id
:type node: str or int
:return: iterator over the filtered neighbors
:rtype: iterator
"""
return (v for u, v, link in self.network.out_edges_iter(node, data=True)
if link.type in (self.TYPE_LINK_STATIC, self.TYPE_LINK_DYNAMIC))
def real_out_edges_iter (self, node):
"""
Return with an iterator over the out edge data of the given Node not
counting the SG and E2E requirement links.
:param node: examined :any:`Node` id
:type node: str or int
:return: iterator over the filtered neighbors (u,v,d)
:rtype: iterator
"""
return (data for data in self.network.out_edges_iter(node, data=True)
if data[2].type in (self.TYPE_LINK_STATIC, self.TYPE_LINK_DYNAMIC))
def duplicate_static_links (self):
"""
Extend the NFFG model with backward links for STATIC links to fit for the
orchestration algorithm.
STATIC links: infra-infra, infra-sap
:return: NF-FG with the duplicated links for function chaining
:rtype: :class:`NFFG`
"""
# Create backward links
backwards = [EdgeLink(src=link.dst, dst=link.src, id=str(link.id) + "-back",
backward=True, delay=link.delay,
bandwidth=link.bandwidth) for u, v, link in
self.network.edges_iter(data=True) if link.type == Link.STATIC]
# Add backward links to the NetworkX structure in a separate step to
# avoid the link reduplication caused by the iterator based for loop
for link in backwards:
self.add_edge(src=link.src, dst=link.dst, link=link)
return self
def merge_duplicated_links (self):
"""
Detect duplicated STATIC links which both are connected to the same
Port/Node and have switched source/destination direction to fit for the
simplified NFFG dumping.
Only leaves one of the links, but that's not defined which one.
:return: NF-FG with the filtered links for function chaining
:rtype: :class:`NFFG`
"""
# Collect backward links
backwards = [(src, dst, key) for src, dst, key, link in
self.network.edges_iter(keys=True, data=True) if (
link.type == Link.STATIC or link.type == Link.DYNAMIC) and
link.backward is True]
# Delete backwards links
for link in backwards:
self.network.remove_edge(*link)
return self