-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnffg_elements.py
More file actions
3179 lines (2753 loc) · 88.1 KB
/
nffg_elements.py
File metadata and controls
3179 lines (2753 loc) · 88.1 KB
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.
"""
Classes for handling the elements of the NF-FG data structure.
"""
import json
import uuid
from collections import Iterable, OrderedDict
from itertools import chain
################################################################################
# ---------- BASE classes of NFFG elements -------------------
################################################################################
class Persistable(object):
"""
Define general persist function for the whole NFFG structure.
"""
__slots__ = ()
def persist (self):
"""
Common function to persist the actual element into a plain text format.
:return: generated empty object fit to JSON
:rtype: dict
"""
return OrderedDict()
def load (self, data, *args, **kwargs):
"""
Common function to fill self with data from JSON data.
:raise: :any:`exceptions.NotImplementedError`
:param data: object structure in JSON
:return: self
"""
pass
@classmethod
def parse (cls, data, *args, **kwargs):
"""
Common function to parse the given JSON object structure as the actual NF-FG
entity type and return a newly created object.
:param data: raw JSON object structure
:type data: object
:return: parsed data as the entity type
:rtype: :any:`Persistable`
"""
return cls().load(data, *args, **kwargs)
def copy (self):
"""
Return the copy of the object. This copy function is meant to use when a new
``NFFG`` object structure is created. It can handles the references pointed
to internal NFFG element in order to avoid unnecessary deep copies. These
references are always None in the copied object which are overwritten by
adder functions in every case.
:return: copied object
:rtype: :any:`Element`
"""
from copy import deepcopy
return deepcopy(self)
class Element(Persistable):
"""
Main base class for NF-FG elements with unique id.
Contains the common functionality.
"""
# Operation constants
OP_CREATE = "create"
OP_REPLACE = "replace"
OP_MERGE = "merge"
OP_REMOVE = "remove"
OP_DELETE = "delete"
# Status constants
STATUS_INIT = "INITIALIZED"
STATUS_PENDING = "PENDING"
STATUS_DEPLOY = "DEPLOYED"
STATUS_RUN = "RUNNING"
STATUS_STOP = "STOPPED"
STATUS_FAIL = "FAILED"
__slots__ = ('id', 'type', 'operation', 'status')
def __init__ (self, id=None, type="ELEMENT", operation=None, status=None):
"""
Init.
:param id: optional identification (generated by default)
:type id: str or int
:param type: explicit object type both for nodes and edges
:type type: str
:return: None
"""
super(Element, self).__init__()
self.id = id if id is not None else self.generate_unique_id()
self.type = type
self.operation = operation
self.status = status
@staticmethod
def generate_unique_id ():
"""
Generate a unique id for the object based on uuid module: :rfc:`4122`.
:return: unique id
:rtype: str
"""
return str(uuid.uuid1())
def regenerate_id (self):
"""
Regenerate and set id. Useful for object copy.
:return: new id
:rtype: str
"""
self.id = self.generate_unique_id()
return self.id
def persist (self):
"""
Persist object.
:return: JSON representation
:rtype: dict
"""
# Need to override
element = super(Element, self).persist()
element['id'] = self.id
if self.operation is not None:
element["operation"] = self.operation
if self.status is not None:
element["status"] = self.status
return element
def load (self, data, *args, **kwargs):
"""
Instantiate object from JSON.
:param data: JSON data
:type data: dict
:return: None
"""
self.id = data['id']
super(Element, self).load(data=data)
self.operation = data.get("operation") # optional
self.status = data.get("status") # optional
return self
def dump (self):
"""
Dump the Element in a pretty format for debugging.
:return: Element in JSON format
:rtype: str
"""
return json.dumps(self.persist(), indent=2, sort_keys=False)
##############################################################################
# dict specific functions
##############################################################################
def __getitem__ (self, item):
"""
Return the attribute of the element given by ``item``.
:param item: attribute name
:type item: str or int
:return: attribute
:rtype: object
"""
if hasattr(self, item):
return getattr(self, item)
else:
raise KeyError(
"%s object has no key: %s" % (self.__class__.__name__, item))
def __setitem__ (self, key, value):
"""
Set the attribute given by ``key`` with ``value``:
:param key: attribute name
:type key: str or int
:param value: new value
:type value: object
:return: new value
:rtype: object
"""
if hasattr(self, key):
return setattr(self, key, value)
else:
raise KeyError(
"%s object has no key: %s" % (self.__class__.__name__, key))
def __contains__ (self, item):
"""
Return true if the given ``item`` is exist.
:param item: searched attribute name
:type item: str or int
:return: given item is exist or not
:rtype: bool
"""
return hasattr(self, item)
def get (self, item, default=None):
"""
Return with the attribute given by ``item``, else ``default``.
:param item: searched attribute name
:type item: str
:param default: default value
:type default: object
:return: found attribute or default
:rtype: object
"""
try:
return self[item]
except KeyError:
return default
def setdefault (self, key, default=None):
"""
Set the attribute given by ``key``. Use the ``default`` value is it is
not given.
:param key: attribute name
:type key: str or int
:param default: default value
:type default: object
:return: None
"""
if key not in self:
self[key] = default
def clear (self):
"""
Overrided for safety reasons.
:raise: :any:`exceptions.RuntimeError`
"""
raise RuntimeError("This standard dict functions is not supported by NFFG!")
def update (self, dict2):
"""
Overrided for safety reasons.
:raise: :any:`exceptions.RuntimeError`
"""
raise RuntimeError(
"This standard dict functions is not supported by NFFG! self: %s dict2: "
"%s" % (self, dict2))
class L3Address(Element):
"""
Wrapper class for storing L3 address values.
"""
__slots__ = ('name', 'configure', 'client', 'requested', 'provided')
def __init__ (self, id, name=None, configure=None, client=None,
requested=None, provided=None):
"""
Init.
:param id: optional id
:type id: str or int
:param name: optional name
:type name: str
:param configure: request address
:type configure: bool
:param client: client of the address request
:type client: str
:param requested: requested IP
:type requested: str
:param provided: provided IP
:type provided: str
:return: None
"""
super(L3Address, self).__init__(id=id, type="L3ADDRESS")
self.name = name
self.configure = configure
self.client = client
self.requested = requested
self.provided = provided
def load (self, data, *args, **kwargs):
"""
Instantiate object from JSON.
:param data: JSON data
:type data: dict
:return: None
"""
super(L3Address, self).load(data=data)
self.name = data.get('name')
self.configure = data.get('configure')
self.requested = data.get('requested')
self.provided = data.get('provided')
return self
def persist (self):
"""
Persist object.
:return: JSON representation
:rtype: dict
"""
l3 = super(L3Address, self).persist()
if self.name is not None:
l3['name'] = self.name
if self.configure is not None:
l3['configure'] = self.configure
if self.client is not None:
l3['client'] = self.client
if self.requested is not None:
l3['requested'] = self.requested
if self.provided is not None:
l3['provided'] = self.provided
return l3
class L3AddressContainer(Persistable):
"""
Container class for storing L3 address data.
"""
__slots__ = ('container',)
def __init__ (self, container=None):
"""
Init.
:param container: optional container for L3 addresses.
:type container: collection.Iterable
:return: None
"""
super(L3AddressContainer, self).__init__()
self.container = container if container is not None else []
def __getitem__ (self, id):
"""
Return with the :any:`L3Address` given by ``id``.
:param id: L3 address id
:type id: str or int
:return: L3 address
:rtype: :any:`L3Address`
"""
for l3 in self.container:
if l3.id == id:
return l3
raise KeyError("L3 address with id: %s is not defined!" % id)
def __iter__ (self):
"""
Return with an iterator over the container.
:return: iterator
:rtype: collection.Iterable
"""
return iter(self.container)
def __len__ (self):
"""
Return the number of stored :any:`L3Address`.
:return: number of addresses
:rtype: int
"""
return len(self.container)
def __contains__ (self, item):
"""
Return True if address given by ``id`` is exist in the container.
:param item: address object
:type: :any:`L3Address`
:return: found address or not
:rtype: bool
"""
if not isinstance(item, L3Address):
raise RuntimeError(
"L3AddressContainer's operator \"in\" works only with L3Address "
"objects (and not ID-s!)")
return item in self.container
def append (self, item):
"""
Add a new address to the container.
:param item: address object
:type: :any:`L3Address`
:return: added address
:rtype: :any:`L3Address`
"""
self.container.append(item)
return item
def remove (self, item):
"""
Remove L3 address from container.
:param item: address object
:type: :any:`L3Address`
:return: removed address
:rtype: :any:`L3Address`
"""
return self.container.remove(item)
def clear (self):
"""
Remove all the stored address from container.
:return: None
"""
del self.container[:]
def __str__ (self):
"""
Return with string representation.
:return: string representation
:rtype: str
"""
return str(self.container)
def __repr__ (self):
"""
Return with specific string representation.
:return: specific representation
:rtype: str
"""
return str(self)
def add_l3address (self, id, name=None, configure=None, client=None,
requested=None, provided=None):
"""
Add a new address to the container based on given :any:`L3Address`
attributes.
:param id: optional id
:type id: str or int
:param name: optional name
:type name: str
:param configure: request address
:type configure: bool
:param client: client of the address request
:type client: str
:param requested: requested IP
:type requested: str
:param provided: provided IP
:type provided: str
:return: None
"""
self.container.append(
L3Address(id, name=name, configure=configure, client=client,
requested=requested, provided=provided))
def persist (self):
"""
Persist object.
:return: JSON representation
:rtype: list
"""
return [l3.persist() for l3 in self.container]
def load (self, data, *args, **kwargs):
"""
Instantiate object from JSON.
:param data: JSON data
:type data: dict
:return: None
"""
for item in data:
self.add_l3address(id=item['id'], name=item.get('name'),
configure=item.get('configure'),
client=item.get('client'),
requested=item.get('requested'),
provided=item.get('provided'))
class Port(Element):
"""
Class for storing a port of an NF.
"""
# Port type
TYPE = "PORT"
"""Port type"""
ROLE_CONSUMER = "consumer"
ROLE_PROVIDER = "provider"
ROLE_EXTERNAL = "EXTERNAL"
__slots__ = ('__node', 'properties', 'metadata', 'name', 'sap', 'capability',
'technology', 'role', 'delay', 'bandwidth', 'cost', 'qos',
'controller', 'orchestrator', 'l2', 'l3', 'l4')
def __init__ (self, node, id=None, name=None, properties=None, sap=None,
capability=None, technology=None, role=None, delay=None,
bandwidth=None, cost=None, qos=None, controller=None,
orchestrator=None, l2=None, l4=None, metadata=None):
"""
Init.
:param node: container node
:type node: :any:`Node`
:param id: optional id
:type id: str or int
:param properties: supported properties of the port
:type properties: str or iterable(str)
:param name: optional name
:type name: str
:param name: optional capabilities
:type name: str
:param sap: inter-domain SAP identifier
:type sap: str
:param technology: supported technologies
:type technology: str
:param delay: delay
:type delay: float
:param bandwidth: bandwidth
:type bandwidth: float
:param cost: cost
:type cost: str
:param qos: traffic QoS class
:type qos: str
:param controller: controller URL
:type controller: str
:param orchestrator: orchestrator URL
:type orchestrator: str
:param l2: l2 address
:param l2: str
:param l4: l4 fields
:type l4: str
:param metadata: metadata related to Node
:type metadata: dict
:return: None
"""
super(Port, self).__init__(id=id, type=self.TYPE)
if not isinstance(node, Node):
raise RuntimeError("Port's container node must be derived from Node!")
self.__node = node
# Set properties list according to given param type
self.properties = OrderedDict(properties if properties else {})
self.metadata = OrderedDict(metadata if metadata else {})
# Virtualizer-related data
self.name = name
self.sap = sap
self.capability = capability
# sap_data
self.technology = technology
# sap_data/role
self.role = role
# sap_data/resources
self.delay = delay
self.bandwidth = bandwidth
self.cost = cost
self.qos = qos
# control
self.controller = controller
self.orchestrator = orchestrator
# addresses
self.l2 = l2
self.l3 = L3AddressContainer()
self.l4 = l4
@property
def node (self):
"""
Return with the container reference.
:return: container reference
:rtype: :any:`Persistable`
"""
return self.__node
def copy (self):
"""
Skip container ``node`` deepcopy in case the :any:`Port` object is copied
directly. Deepcopy called on an upper object has already cloned the
container node when it gets to a Port object and it will skip the re-cloning
due to its internal memoization feature.
:return: copied object
:rtype: :any:`Port`
"""
tmp, self.__node = self.__node, None
clone = super(Port, self).copy()
self.__node = tmp
return clone
@node.deleter
def node (self):
del self.__node
def add_property (self, property, value):
"""
Add a property to the :any:`Port`.
:param property: property
:type property: str
:param value: property value
:type value: str
:return: the Port object to allow function chaining
:rtype: :any:`Port`
"""
self.properties[property] = value
return self
def has_property (self, property):
"""
Return True if :any:`Port` has a property with given `property`.
:param property: property
:type property: str
:return: has a property with given name or not
:rtype: bool
"""
return property in self.properties
def del_property (self, property=None):
"""
Remove the property from the :any:`Port`. If no property is given all the
properties will be removed from the :any:`Port`.
:param property: property name
:type property: str
:return: removed property or None
:rtype: str or None
"""
if property is None:
self.properties.clear()
else:
return self.properties.pop(property, None)
def get_property (self, property):
"""
Return the value of property.
:param property: property
:type property: str
:return: the value of the property
:rtype: str
"""
return self.properties.get(property)
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 :any:`Port` object to allow function chaining
:rtype: :any:`Port`
"""
self.metadata[name] = value
return self
def has_metadata (self, name):
"""
Return True if the :any:`Port` has a metadata with the given `name`.
:param name: metadata name
:type name: str
:return: has metadata with given name or not
:rtype: bool
"""
return name in self.metadata
def del_metadata (self, name=None):
"""
Remove the metadata from the :any:`Port`. 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 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 persist (self):
"""
Persist object.
:return: JSON representation
:rtype: dict
"""
port = super(Port, self).persist()
if self.properties:
port["property"] = self.properties.copy()
if self.name is not None:
port['name'] = self.name
if self.sap is not None:
port['sap'] = self.sap
if self.capability is not None:
port['capability'] = self.capability
if any(v is not None for v in (self.technology, self.role, self.delay,
self.bandwidth, self.cost)):
port['sap_data'] = {}
if self.technology is not None:
port['sap_data']['technology'] = self.technology
if self.role is not None:
port['sap_data']['role'] = self.role
if any(v is not None for v in (self.delay, self.bandwidth, self.cost)):
port['sap_data']['resources'] = {}
if self.delay is not None:
port['sap_data']['resources']['delay'] = self.delay
if self.bandwidth is not None:
port['sap_data']['resources']['bandwidth'] = self.bandwidth
if self.cost is not None:
port['sap_data']['resources']['cost'] = self.cost
if self.qos is not None:
port['sap_data']['resources']['qos'] = self.qos
if any(v is not None for v in (self.controller, self.orchestrator)):
port['control'] = {}
if self.controller is not None:
port['control']['controller'] = self.controller
if self.orchestrator is not None:
port['control']['orchestrator'] = self.orchestrator
if any(v is not None for v in
(self.l2, self.l4, True if self.l3 else None)):
port['addresses'] = {}
if self.l2 is not None:
port['addresses']['l2'] = self.l2
if self.l4 is not None:
port['addresses']['l4'] = self.l4
if len(self.l3):
port['addresses']['l3'] = self.l3.persist()
if self.metadata:
port["metadata"] = self.metadata.copy()
return port
def load (self, data, *args, **kwargs):
"""
Instantiate object from JSON.
:param data: JSON data
:type data: dict
:return: None
"""
super(Port, self).load(data=data)
self.properties = OrderedDict(data.get('property', ()))
self.sap = data.get('sap')
self.name = data.get('name')
self.capability = data.get('capability')
if 'sap_data' in data:
self.technology = data['sap_data'].get('technology')
self.role = data['sap_data'].get('role')
if 'resources' in data['sap_data']:
self.delay = data['sap_data']['resources'].get('delay')
self.bandwidth = data['sap_data']['resources'].get('bandwidth')
self.cost = data['sap_data']['resources'].get('cost')
self.qos = data['sap_data']['resources'].get('qos')
else:
self.technology = self.delay = self.bandwidth = self.cost = None
if 'control' in data:
self.controller = data['control'].get('controller')
self.orchestrator = data['control'].get('orchestrator')
else:
self.controller = self.orchestrator = None
if 'addresses' in data:
self.l2 = data['addresses'].get('l2')
self.l3.load(data=data['addresses'].get('l3', ()))
self.l4 = data['addresses'].get('l4')
else:
self.l2 = self.l4 = None
self.metadata = OrderedDict(data.get('metadata', ()))
return self
def __repr__ (self):
"""
Return with specific string representation.
:return: specific representation
:rtype: str
"""
return "%s(node: %s, id: %s)" % (
self.__class__.__name__, self.node.id, self.id)
class PortContainer(Persistable):
"""
Basic container class for ports.
Implements a Container-like behavior for getting a Port with id:
>>> cont = PortContainer()
>>> ...
>>> cont["port_id"]
"""
__slots__ = ('container',)
def __init__ (self, container=None):
"""
Init.
:param container: use given container for init
:type container: :any:`collections.Container`
"""
self.container = container if container is not None else []
def __getitem__ (self, id):
"""
Return with the :any:`Port` given by ``id``.
:param id: port id
:type id: str or int
:return: port object
:rtype: :any:`Port`
"""
for port in self.container:
if port.id == id:
return port
raise KeyError("Port with id: %s is not defined in: %s!"
% (id, [p.id for p in self.container]))
def __iter__ (self):
"""
Return with an iterator over the container.
:return: iterator
:rtype: collection.Iterable
"""
return iter(self.container)
def __len__ (self):
"""
Return the number of stored :any:`Port`.
:return: number of ports
:rtype: int
"""
return len(self.container)
def __contains__ (self, item):
"""
Return True if port given by ``id`` is exist in the container.
:param item: port object
:type: :any:`Port`
:return: found port or not
:rtype: bool
"""
# this type checking is important because with Port ID input the function
# would silently return False!
if isinstance(item, Port):
return item in self.container
else:
return item in (p.id for p in self.container)
@property
def flowrules (self):
"""
Return with an iterator over the flowrules sored in the ports.
:return: iterator of flowrules
:rtype: collections.Iterator
"""
return chain(*[port.flowrules for port in self.container])
def append (self, item):
"""
Add new port object to the container.
:param item: port object
:type item: :any:`Port`
:return: added object
:rtype: :any:`Port`
"""
self.container.append(item)
return item
def remove (self, item):
"""
Remove port object from the container.
:param item: port object
:type item: :any:`Port`
:return: None
"""
try:
return self.container.remove(item)
except ValueError:
return
def clear (self):
"""
Remove all the stored objects.
:return: None
"""
del self.container[:]
def __str__ (self):
"""
Return with string representation.
:return: string representation
:rtype: str
"""
return str(self.container)
def __repr__ (self):
"""
Return with specific string representation.
:return: specific representation
:rtype: str
"""
return str(self)
def persist (self):
"""
Persist object.
:return: JSON representation
:rtype: list
"""
return [port.persist() for port in self.container]
def load (self, data, *args, **kwargs):
"""
Instantiate object from JSON.
:param data: JSON data
:type data: dict
:return: None
"""
pass
class Constraints(Persistable):
"""
Container class for constraints.
"""
__slots__ = ('affinity', 'antiaffinity', 'variable', 'constraint',
'restorability')
def __init__ (self):
"""
Init.
"""
super(Constraints, self).__init__()
self.affinity = OrderedDict()
self.antiaffinity = OrderedDict()
self.variable = OrderedDict()
self.constraint = OrderedDict()
self.restorability = None
def add_affinity (self, id, value):
"""
Set affinity value.
:param id: unique ID
:type id: str or int
:param value: new value
:type value: str or int
:return: new value
:rtype: str or int
"""