-
Notifications
You must be signed in to change notification settings - Fork 18
/
commands.py
1893 lines (1528 loc) · 63.3 KB
/
commands.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
# 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.
import re
import netaddr
from ovsdbapp.backend.ovs_idl import command as cmd
from ovsdbapp.backend.ovs_idl import idlutils
from ovsdbapp.backend.ovs_idl import rowview
from ovsdbapp import constants as const
from ovsdbapp import utils
class LsAddCommand(cmd.AddCommand):
table_name = 'Logical_Switch'
def __init__(self, api, switch=None, may_exist=False, **columns):
super().__init__(api)
self.switch = switch
self.columns = columns
self.may_exist = may_exist
def run_idl(self, txn):
# There is no requirement for name to be unique, so if a name is
# specified, we always have to do a lookup since adding it won't
# fail. If may_exist is set, we just don't do anything when dup'd
if self.switch:
sw = idlutils.row_by_value(self.api.idl, self.table_name, 'name',
self.switch, None)
if sw:
if self.may_exist:
self.result = rowview.RowView(sw)
return
raise RuntimeError("Switch %s exists" % self.switch)
elif self.may_exist:
raise RuntimeError("may_exist requires name")
sw = txn.insert(self.api.tables[self.table_name])
if self.switch:
sw.name = self.switch
else:
# because ovs.db.idl brokenly requires a changed column
sw.name = ""
self.set_columns(sw, **self.columns)
self.result = sw.uuid
class LsDelCommand(cmd.BaseCommand):
def __init__(self, api, switch, if_exists=False):
super().__init__(api)
self.switch = switch
self.if_exists = if_exists
def run_idl(self, txn):
try:
lswitch = self.api.lookup('Logical_Switch', self.switch)
lswitch.delete()
except idlutils.RowNotFound as e:
if self.if_exists:
return
msg = "Logical Switch %s does not exist" % self.switch
raise RuntimeError(msg) from e
class LsListCommand(cmd.ReadOnlyCommand):
def run_idl(self, txn):
table = self.api.tables['Logical_Switch']
self.result = [rowview.RowView(r) for r in table.rows.values()]
class LsGetCommand(cmd.BaseGetRowCommand):
table = 'Logical_Switch'
class _AclAddHelper(cmd.AddCommand):
table_name = 'ACL'
def __init__(self, api, entity, direction, priority, match, action,
log=False, may_exist=False, severity=None, name=None,
meter=None, **external_ids):
if direction not in ('from-lport', 'to-lport'):
raise TypeError("direction must be either from-lport or to-lport")
if not 0 <= priority <= const.ACL_PRIORITY_MAX:
raise ValueError("priority must be between 0 and %s, inclusive" % (
const.ACL_PRIORITY_MAX))
if action not in ('allow', 'allow-related', 'allow-stateless',
'drop', 'reject'):
raise TypeError("action must be allow/allow-related/"
"allow-stateless/drop/reject")
super().__init__(api)
self.entity = entity
self.direction = direction
self.priority = priority
self.match = match
self.action = action
self.log = log
self.may_exist = may_exist
self.severity = severity
self.name = name
self.meter = meter
self.external_ids = external_ids
def acl_match(self, row):
return (self.direction == row.direction and
self.priority == row.priority and
self.match == row.match)
def run_idl(self, txn):
entity = self.api.lookup(self.lookup_table, self.entity)
acls = [acl for acl in entity.acls if self.acl_match(acl)]
if acls:
if self.may_exist:
self.result = rowview.RowView(acls[0])
return
raise RuntimeError("ACL (%s, %s, %s) already exists" % (
self.direction, self.priority, self.match))
acl = txn.insert(self.api.tables[self.table_name])
acl.direction = self.direction
acl.priority = self.priority
acl.match = self.match
acl.action = self.action
acl.log = self.log
if self.severity:
acl.severity = self.severity
if self.name:
acl.name = self.name
if self.meter:
acl.meter = self.meter
entity.addvalue('acls', acl)
for col, value in self.external_ids.items():
acl.setkey('external_ids', col, value)
self.result = acl.uuid
class AclAddCommand(_AclAddHelper):
lookup_table = 'Logical_Switch'
def __init__(self, api, switch, direction, priority, match, action,
log=False, may_exist=False, severity=None, name=None,
**external_ids):
# NOTE: we're overriding the constructor here to not break any
# existing callers before we introduced Port Groups.
super().__init__(api, switch, direction, priority,
match, action, log, may_exist,
severity, name, **external_ids)
class PgAclAddCommand(_AclAddHelper):
lookup_table = 'Port_Group'
class _AclDelHelper(cmd.BaseCommand):
def __init__(self, api, entity, direction=None,
priority=None, match=None):
if (priority is None) != (match is None):
raise TypeError("Must specify priority and match together")
if priority is not None and not direction:
raise TypeError("Cannot specify priority/match without direction")
super().__init__(api)
self.entity = entity
self.conditions = []
if direction:
self.conditions.append(('direction', '=', direction))
# priority can be 0
if match: # and therefore priority due to the above check
self.conditions += [('priority', '=', priority),
('match', '=', match)]
def run_idl(self, txn):
entity = self.api.lookup(self.lookup_table, self.entity)
for acl in [a for a in entity.acls
if idlutils.row_match(a, self.conditions)]:
entity.delvalue('acls', acl)
acl.delete()
class AclDelCommand(_AclDelHelper):
lookup_table = 'Logical_Switch'
def __init__(self, api, switch, direction=None,
priority=None, match=None):
# NOTE: we're overriding the constructor here to not break any
# existing callers before we introduced Port Groups.
super().__init__(api, switch, direction, priority, match)
class PgAclDelCommand(_AclDelHelper):
lookup_table = 'Port_Group'
class _AclListHelper(cmd.ReadOnlyCommand):
def __init__(self, api, entity):
super().__init__(api)
self.entity = entity
def run_idl(self, txn):
entity = self.api.lookup(self.lookup_table, self.entity)
self.result = [rowview.RowView(acl) for acl in entity.acls]
class AclListCommand(_AclListHelper):
lookup_table = 'Logical_Switch'
class PgAclListCommand(_AclListHelper):
lookup_table = 'Port_Group'
class AddressSetAddCommand(cmd.AddCommand):
table_name = 'Address_Set'
def __init__(self, api, name, addresses=None, may_exist=False):
super().__init__(api)
self.name = name
self.addresses = [str(netaddr.IPAddress(address))
for address in addresses or []]
self.may_exist = may_exist
def run_idl(self, txn):
address_set = self.api.lookup(self.table_name, self.name, None)
if address_set:
if self.may_exist:
self.result = rowview.RowView(address_set)
return
raise RuntimeError("Address set %s exists" % self.name)
address_set = txn.insert(self.api.tables[self.table_name])
address_set.name = self.name
if self.addresses:
address_set.addresses = self.addresses
self.result = address_set.uuid
class AddressSetDelCommand(cmd.BaseCommand):
table_name = 'Address_Set'
def __init__(self, api, address_set, if_exists=False):
super().__init__(api)
self.address_set = address_set
self.if_exists = if_exists
def run_idl(self, txn):
try:
address_set = self.api.lookup(self.table_name, self.address_set)
address_set.delete()
except idlutils.RowNotFound as e:
if self.if_exists:
return
msg = "Address set %s does not exist" % self.address_set
raise RuntimeError(msg) from e
class AddressSetGetCommand(cmd.BaseGetRowCommand):
table = 'Address_Set'
class AddressSetListCommand(cmd.ReadOnlyCommand):
table = 'Address_Set'
def run_idl(self, txn):
self.result = [rowview.RowView(r) for
r in self.api.tables[self.table].rows.values()]
class AddressSetUpdateAddressesCommand(cmd.BaseCommand):
table_name = 'Address_Set'
def __init__(self, api, address_set, addresses):
super().__init__(api)
self.address_set = address_set
if isinstance(addresses, (str, bytes)):
addresses = [addresses]
self.addresses = [str(netaddr.IPAddress(address))
for address in addresses]
class AddressSetAddAddressesCommand(AddressSetUpdateAddressesCommand):
def run_idl(self, txn):
address_set = self.api.lookup(self.table_name, self.address_set)
for address in self.addresses:
address_set.addvalue('addresses', address)
class AddressSetRemoveAddressCommand(AddressSetUpdateAddressesCommand):
def run_idl(self, txn):
address_set = self.api.lookup(self.table_name, self.address_set)
for address in self.addresses:
address_set.delvalue('addresses', address)
class QoSAddCommand(cmd.AddCommand):
table_name = 'QoS'
def __init__(self, api, switch, direction, priority, match, rate=None,
burst=None, dscp=None, external_ids_match=None,
may_exist=False, **columns):
if direction not in ('from-lport', 'to-lport'):
raise TypeError("direction must be either from-lport or to-lport")
if not 0 <= priority <= const.ACL_PRIORITY_MAX:
raise ValueError("priority must be between 0 and %s, inclusive" %
const.ACL_PRIORITY_MAX)
if rate is not None and not 1 <= rate <= const.QOS_BANDWIDTH_MAX:
raise ValueError("rate(%s) must be between 1 and %s, inclusive" %
rate, const.QOS_BANDWIDTH_MAX)
if burst is not None and not 1 <= burst <= const.QOS_BANDWIDTH_MAX:
raise ValueError("burst(%s) must be between 1 and %s, "
"inclusive" % burst, const.QOS_BANDWIDTH_MAX)
if dscp is not None and not 0 <= dscp <= const.QOS_DSCP_MAX:
raise ValueError("dscp(%s) must be between 0 and %s, inclusive" %
dscp, const.QOS_DSCP_MAX)
if rate is None and dscp is None:
raise ValueError("One of the rate or dscp must be configured")
if (external_ids_match is not None and
not isinstance(external_ids_match, dict)):
raise ValueError("external_ids_match must be None or a dictionary")
super().__init__(api)
self.switch = switch
self.direction = direction
self.priority = priority
self.match = match
self.rate = rate
self.burst = burst
self.dscp = dscp
self.may_exist = may_exist
self.columns = columns
self.external_ids_match = external_ids_match
def qos_match(self, row):
if self.external_ids_match:
return self.external_ids_match.items() <= row.external_ids.items()
return (self.direction == row.direction and
self.priority == row.priority and
self.match == row.match)
def run_idl(self, txn):
ls = self.api.lookup('Logical_Switch', self.switch)
for qos_rule in iter(r for r in ls.qos_rules if self.qos_match(r)):
if self.may_exist:
return self._update_qos(qos_rule)
raise RuntimeError("QoS (%s, %s, %s) already exists" % (
self.direction, self.priority, self.match))
self._create_qos(txn, ls)
def _create_qos(self, txn, ls):
row = txn.insert(self.api.tables[self.table_name])
row.direction = self.direction
row.priority = self.priority
row.match = self.match
if self.rate:
row.setkey('bandwidth', 'rate', self.rate)
if self.burst:
row.setkey('bandwidth', 'burst', self.burst)
if self.dscp is not None:
row.setkey('action', 'dscp', self.dscp)
self.set_columns(row, **self.columns)
ls.addvalue('qos_rules', row)
self.result = row.uuid
def _update_qos(self, qos_rule):
qos_rule.direction = self.direction
qos_rule.priority = self.priority
qos_rule.match = self.match
if self.rate:
qos_rule.setkey('bandwidth', 'rate', self.rate)
if self.burst:
qos_rule.setkey('bandwidth', 'burst', self.burst)
else:
qos_rule.delkey('bandwidth', 'burst')
else:
qos_rule.delkey('bandwidth', 'rate')
qos_rule.delkey('bandwidth', 'burst')
if self.dscp:
qos_rule.setkey('action', 'dscp', self.dscp)
else:
qos_rule.delkey('action', 'dscp')
for column, value in self.columns.items():
if getattr(qos_rule, column) != value:
self.set_column(qos_rule, column, value)
self.result = qos_rule.uuid
class QoSDelCommand(cmd.BaseCommand):
def __init__(self, api, switch, direction=None,
priority=None, match=None, if_exists=True):
if (priority is None) != (match is None):
raise TypeError("Must specify priority and match together")
if priority is not None and not direction:
raise TypeError("Cannot specify priority/match without direction")
super().__init__(api)
self.switch = switch
self.conditions = []
self.if_exists = if_exists
if direction:
self.conditions.append(('direction', '=', direction))
# priority can be 0
if match: # and therefor priority due to the above check
self.conditions += [('priority', '=', priority),
('match', '=', match)]
def run_idl(self, txn):
try:
ls = self.api.lookup('Logical_Switch', self.switch)
except idlutils.RowNotFound as e:
if self.if_exists:
return
msg = 'Logical Switch %s does not exist' % self.switch
raise RuntimeError(msg) from e
for row in ls.qos_rules:
if idlutils.row_match(row, self.conditions):
ls.delvalue('qos_rules', row)
row.delete()
class QoSListCommand(cmd.ReadOnlyCommand):
def __init__(self, api, switch):
super().__init__(api)
self.switch = switch
def run_idl(self, txn):
ls = self.api.lookup('Logical_Switch', self.switch)
self.result = [rowview.RowView(row) for row in ls.qos_rules]
class QoSDelExtIdCommand(cmd.BaseCommand):
def __init__(self, api, lswitch, external_ids, if_exists=False):
if not external_ids:
raise TypeError('external_ids dictionary cannot be empty')
super().__init__(api)
self.lswitch = lswitch
self.external_ids = external_ids
self.if_exists = if_exists
def run_idl(self, txn):
try:
lswitch = self.api.lookup('Logical_Switch', self.lswitch)
except idlutils.RowNotFound as e:
if self.if_exists:
return
msg = 'Logical Switch %s does not exist' % self.lswitch
raise RuntimeError(msg) from e
for qos in lswitch.qos_rules:
if self.external_ids.items() <= qos.external_ids.items():
lswitch.delvalue('qos_rules', qos)
qos.delete()
class LspAddCommand(cmd.AddCommand):
table_name = 'Logical_Switch_Port'
def __init__(self, api, switch, port, parent_name=None, tag=None,
may_exist=False, **columns):
if tag and not 0 <= tag <= 4095:
raise TypeError("tag must be 0 to 4095, inclusive")
if (parent_name is None) != (tag is None):
raise TypeError("parent_name and tag must be passed together")
super().__init__(api)
self.switch = switch
self.port = port
self.parent = parent_name
self.tag = tag
self.may_exist = may_exist
self.columns = columns
def run_idl(self, txn):
ls = self.api.lookup('Logical_Switch', self.switch)
try:
lsp = self.api.lookup(self.table_name, self.port)
if self.may_exist:
msg = None
if lsp not in ls.ports:
msg = "%s exists, but is not in %s" % (
self.port, self.switch)
if self.parent:
if not lsp.parent_name:
msg = "%s exists, but has no parent" % self.port
# parent_name, being optional, is stored as list
if self.parent not in lsp.parent_name:
msg = "%s exists with different parent" % self.port
if self.tag not in lsp.tag_request:
msg = "%s exists with different tag request" % (
self.port,)
elif lsp.parent_name:
msg = "%s exists, but with a parent" % self.port
if msg:
raise RuntimeError(msg)
self.result = rowview.RowView(lsp)
return
except idlutils.RowNotFound:
# This is what we want
pass
lsp = txn.insert(self.api.tables[self.table_name])
lsp.name = self.port
if self.tag is not None:
lsp.parent_name = self.parent
lsp.tag_request = self.tag
ls.addvalue('ports', lsp)
self.set_columns(lsp, **self.columns)
self.result = lsp.uuid
class PortDelCommand(cmd.BaseCommand):
def __init__(self, api, table, port, parent_table, parent=None,
if_exists=False):
super().__init__(api)
self.table = table
self.port = port
self.parent_table = parent_table
self.parent = parent
self.if_exists = if_exists
def run_idl(self, txn):
try:
row = self.api.lookup(self.table, self.port)
except idlutils.RowNotFound as e:
if self.if_exists:
return
raise RuntimeError("%s does not exist" % self.port) from e
# We need to delete the port from its parent
if self.parent:
parent = self.api.lookup(self.parent_table, self.parent)
else:
parent = next(iter(
p for p in self.api.tables[self.parent_table].rows.values()
if row in p.ports), None)
if not (parent and row in parent.ports):
raise RuntimeError("%s does not exist in %s" % (
self.port, self.parent))
parent.delvalue('ports', row)
row.delete()
class LspDelCommand(PortDelCommand):
def __init__(self, api, port, switch=None, if_exists=False):
super().__init__(
api, 'Logical_Switch_Port', port, 'Logical_Switch', switch,
if_exists)
class LspListCommand(cmd.ReadOnlyCommand):
def __init__(self, api, switch=None):
super().__init__(api)
self.switch = switch
def run_idl(self, txn):
if self.switch:
ports = self.api.lookup('Logical_Switch', self.switch).ports
else:
ports = self.api.tables['Logical_Switch_Port'].rows.values()
self.result = [rowview.RowView(r) for r in ports]
class LspGetCommand(cmd.BaseGetRowCommand):
table = 'Logical_Switch_Port'
class LspGetParentCommand(cmd.ReadOnlyCommand):
def __init__(self, api, port):
super().__init__(api)
self.port = port
def run_idl(self, txn):
lsp = self.api.lookup('Logical_Switch_Port', self.port)
self.result = next(iter(lsp.parent_name), "")
class LspGetTagCommand(cmd.ReadOnlyCommand):
def __init__(self, api, port):
super().__init__(api)
self.port = port
def run_idl(self, txn):
lsp = self.api.lookup('Logical_Switch_Port', self.port)
self.result = next(iter(lsp.tag), -1)
class LspSetAddressesCommand(cmd.BaseCommand):
addr_re = re.compile(
r'^(router|unknown|dynamic|([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}( .+)*)$')
def __init__(self, api, port, addresses):
for addr in addresses:
if not self.addr_re.match(addr):
raise TypeError(
"address (%s) must be router/unknown/dynamic/"
"ethaddr[ ipaddr...]" % (addr,))
super().__init__(api)
self.port = port
self.addresses = addresses
def run_idl(self, txn):
lsp = self.api.lookup('Logical_Switch_Port', self.port)
lsp.addresses = self.addresses
class LspGetAddressesCommand(cmd.ReadOnlyCommand):
def __init__(self, api, port):
super().__init__(api)
self.port = port
def run_idl(self, txn):
lsp = self.api.lookup('Logical_Switch_Port', self.port)
self.result = lsp.addresses
class LspSetPortSecurityCommand(cmd.BaseCommand):
def __init__(self, api, port, addresses):
# NOTE(twilson) ovn-nbctl.c does not do any checking of addresses
# so neither do we
super().__init__(api)
self.port = port
self.addresses = addresses
def run_idl(self, txn):
lsp = self.api.lookup('Logical_Switch_Port', self.port)
lsp.port_security = self.addresses
class LspGetPortSecurityCommand(cmd.ReadOnlyCommand):
def __init__(self, api, port):
super().__init__(api)
self.port = port
def run_idl(self, txn):
lsp = self.api.lookup('Logical_Switch_Port', self.port)
self.result = lsp.port_security
class LspGetUpCommand(cmd.ReadOnlyCommand):
def __init__(self, api, port):
super().__init__(api)
self.port = port
def run_idl(self, txn):
lsp = self.api.lookup('Logical_Switch_Port', self.port)
# 'up' is optional, but if not up, it's not up :p
self.result = next(iter(lsp.up), False)
class LspSetEnabledCommand(cmd.BaseCommand):
def __init__(self, api, port, is_enabled):
super().__init__(api)
self.port = port
self.is_enabled = is_enabled
def run_idl(self, txn):
lsp = self.api.lookup('Logical_Switch_Port', self.port)
lsp.enabled = self.is_enabled
class LspGetEnabledCommand(cmd.ReadOnlyCommand):
def __init__(self, api, port):
super().__init__(api)
self.port = port
def run_idl(self, txn):
lsp = self.api.lookup('Logical_Switch_Port', self.port)
# enabled is optional, but if not disabled then enabled
self.result = next(iter(lsp.enabled), True)
class LspSetTypeCommand(cmd.BaseCommand):
def __init__(self, api, port, port_type):
super().__init__(api)
self.port = port
self.port_type = port_type
def run_idl(self, txn):
lsp = self.api.lookup('Logical_Switch_Port', self.port)
lsp.type = self.port_type
class LspGetTypeCommand(cmd.ReadOnlyCommand):
def __init__(self, api, port):
super().__init__(api)
self.port = port
def run_idl(self, txn):
lsp = self.api.lookup('Logical_Switch_Port', self.port)
self.result = lsp.type
class LspSetOptionsCommand(cmd.BaseSetOptionsCommand):
table = 'Logical_Switch_Port'
class LspGetOptionsCommand(cmd.BaseGetOptionsCommand):
table = 'Logical_Switch_Port'
class LspSetDhcpV4OptionsCommand(cmd.BaseCommand):
def __init__(self, api, port, dhcpopt_uuid):
super().__init__(api)
self.port = port
self.dhcpopt_uuid = dhcpopt_uuid
def run_idl(self, txn):
lsp = self.api.lookup('Logical_Switch_Port', self.port)
lsp.dhcpv4_options = self.dhcpopt_uuid
class LspGetDhcpV4OptionsCommand(cmd.ReadOnlyCommand):
def __init__(self, api, port):
super().__init__(api)
self.port = port
def run_idl(self, txn):
lsp = self.api.lookup('Logical_Switch_Port', self.port)
self.result = next((rowview.RowView(d)
for d in lsp.dhcpv4_options), [])
class DhcpOptionsAddCommand(cmd.AddCommand):
table_name = 'DHCP_Options'
def __init__(self, api, cidr, **external_ids):
cidr = netaddr.IPNetwork(cidr)
super().__init__(api)
self.cidr = str(cidr)
self.external_ids = external_ids
def run_idl(self, txn):
dhcpopt = txn.insert(self.api.tables[self.table_name])
dhcpopt.cidr = self.cidr
dhcpopt.external_ids = self.external_ids
self.result = dhcpopt.uuid
class DhcpOptionsDelCommand(cmd.BaseCommand):
def __init__(self, api, dhcpopt_uuid):
super().__init__(api)
self.dhcpopt_uuid = dhcpopt_uuid
def run_idl(self, txn):
dhcpopt = self.api.lookup('DHCP_Options', self.dhcpopt_uuid)
dhcpopt.delete()
class DhcpOptionsListCommand(cmd.ReadOnlyCommand):
def run_idl(self, txn):
self.result = [rowview.RowView(r) for
r in self.api.tables['DHCP_Options'].rows.values()]
class DhcpOptionsGetCommand(cmd.BaseGetRowCommand):
table = 'DHCP_Options'
class DhcpOptionsSetOptionsCommand(cmd.BaseSetOptionsCommand):
table = 'DHCP_Options'
class DhcpOptionsGetOptionsCommand(cmd.BaseGetOptionsCommand):
table = 'DHCP_Options'
class LrAddCommand(cmd.BaseCommand):
def __init__(self, api, router=None, may_exist=False, **columns):
super().__init__(api)
self.router = router
self.may_exist = may_exist
self.columns = columns
def run_idl(self, txn):
if self.router:
try:
lr = self.api.lookup('Logical_Router', self.router)
if self.may_exist:
self.result = rowview.RowView(lr)
return
except idlutils.RowNotFound:
pass
lr = txn.insert(self.api.tables['Logical_Router'])
lr.name = self.router if self.router else ""
self.set_columns(lr, **self.columns)
self.result = lr.uuid
def post_commit(self, txn):
real_uuid = txn.get_insert_uuid(self.result)
if real_uuid:
row = self.api.tables['Logical_Router'].rows[real_uuid]
self.result = rowview.RowView(row)
class LrDelCommand(cmd.BaseCommand):
def __init__(self, api, router, if_exists=False):
super().__init__(api)
self.router = router
self.if_exists = if_exists
def run_idl(self, txn):
try:
lr = self.api.lookup('Logical_Router', self.router)
lr.delete()
except idlutils.RowNotFound as e:
if self.if_exists:
return
msg = "Logical Router %s does not exist" % self.router
raise RuntimeError(msg) from e
class LrListCommand(cmd.ReadOnlyCommand):
def run_idl(self, txn):
self.result = [rowview.RowView(r) for
r in self.api.tables['Logical_Router'].rows.values()]
class LrGetCommand(cmd.BaseGetRowCommand):
table = 'Logical_Router'
class LrpAddCommand(cmd.BaseCommand):
def __init__(self, api, router, port, mac, networks,
peer=None, may_exist=False, **columns):
self.mac = str(netaddr.EUI(mac, dialect=netaddr.mac_unix_expanded))
self.networks = [str(netaddr.IPNetwork(net)) for net in networks]
self.router = router
self.port = port
self.peer = peer
self.may_exist = may_exist
self.columns = columns
super().__init__(api)
def run_idl(self, txn):
lr = self.api.lookup('Logical_Router', self.router)
try:
lrp = self.api.lookup('Logical_Router_Port', self.port)
if self.may_exist:
msg = None
if lrp not in lr.ports:
msg = "Port %s exists, but is not in router %s" % (
self.port, self.router)
elif netaddr.EUI(lrp.mac) != netaddr.EUI(self.mac):
msg = "Port %s exists with different mac" % (self.port)
elif set(self.networks) != set(lrp.networks):
msg = "Port %s exists with different networks" % (
self.port)
elif (not self.peer) != (not lrp.peer) or (
self.peer != lrp.peer):
msg = "Port %s exists with different peer" % (self.port)
if msg:
raise RuntimeError(msg)
self.result = rowview.RowView(lrp)
return
except idlutils.RowNotFound:
pass
lrp = txn.insert(self.api.tables['Logical_Router_Port'])
# This is what ovn-nbctl does, though the lookup is by uuid or name
lrp.name = self.port
lrp.mac = self.mac
lrp.networks = self.networks
if self.peer:
lrp.peer = self.peer
lr.addvalue('ports', lrp)
gwcs = self.columns.pop('gateway_chassis', [])
for n, chassis in enumerate(gwcs):
gwc_name = '%s_%s' % (lrp.name, chassis)
cmd = GatewayChassisAddCommand(self.api, gwc_name, chassis,
len(gwcs) - n, may_exist=True)
cmd.run_idl(txn)
lrp.addvalue('gateway_chassis', cmd.result)
self.set_columns(lrp, **self.columns)
self.result = lrp.uuid
def post_commit(self, txn):
real_uuid = txn.get_insert_uuid(self.result)
if real_uuid:
row = self.api.tables['Logical_Router_Port'].rows[real_uuid]
self.result = rowview.RowView(row)
class LrpDelCommand(PortDelCommand):
def __init__(self, api, port, router=None, if_exists=False):
super().__init__(
api, 'Logical_Router_Port', port, 'Logical_Router', router,
if_exists)
class LrpListCommand(cmd.ReadOnlyCommand):
def __init__(self, api, router):
super().__init__(api)
self.router = router
def run_idl(self, txn):
router = self.api.lookup('Logical_Router', self.router)
self.result = [rowview.RowView(r) for r in router.ports]
class LrpGetCommand(cmd.BaseGetRowCommand):
table = 'Logical_Router_Port'
class LrpSetEnabledCommand(cmd.BaseCommand):
def __init__(self, api, port, is_enabled):
super().__init__(api)
self.port = port
self.is_enabled = is_enabled
def run_idl(self, txn):
lrp = self.api.lookup('Logical_Router_Port', self.port)
lrp.enabled = self.is_enabled
class LrpGetEnabledCommand(cmd.ReadOnlyCommand):
def __init__(self, api, port):
super().__init__(api)
self.port = port
def run_idl(self, txn):
lrp = self.api.lookup('Logical_Router_Port', self.port)
# enabled is optional, but if not disabled then enabled
self.result = next(iter(lrp.enabled), True)
class LrpSetOptionsCommand(cmd.BaseSetOptionsCommand):
table = 'Logical_Router_Port'
class LrpGetOptionsCommand(cmd.BaseGetOptionsCommand):
table = 'Logical_Router_Port'
class LrpSetGatewayChassisCommand(cmd.BaseCommand):
table = 'Logical_Router_Port'
def __init__(self, api, port, gateway_chassis, priority):
super().__init__(api)
self.port = port
self.gateway_chassis = gateway_chassis
self.priority = priority
def run_idl(self, txn):
lrp = self.api.lookup(self.table, self.port)
gwc_name = '%s_%s' % (lrp.name, self.gateway_chassis)
cmd = GatewayChassisAddCommand(self.api, gwc_name,
self.gateway_chassis,
self.priority, may_exist=True)
cmd.run_idl(txn)
lrp.addvalue('gateway_chassis', cmd.result)
class LrpGetGatewayChassisCommand(cmd.ReadOnlyCommand):
table = 'Logical_Router_Port'
def __init__(self, api, port):
super().__init__(api)
self.port = port
def run_idl(self, txn):
lrp = self.api.lookup(self.table, self.port)
self.result = [rowview.RowView(d) for d in lrp.gateway_chassis]
class LrpDelGatewayChassisCommand(cmd.BaseCommand):
table = 'Logical_Router_Port'
def __init__(self, api, port, gateway_chassis, if_exists=False):
super().__init__(api)
self.port = port
self.gateway_chassis = gateway_chassis
self.if_exists = if_exists
def run_idl(self, txn):
lrp = self.api.lookup(self.table, self.port)
for gwc in lrp.gateway_chassis:
if gwc.chassis_name == self.gateway_chassis:
lrp.delvalue('gateway_chassis', gwc)
return
if not self.if_exists:
msg = "Gateway chassis '%s' on port %s does not exist" % (
self.gateway_chassis, self.port)
raise RuntimeError(msg)
class LrRouteAddCommand(cmd.BaseCommand):
def __init__(self, api, router, prefix, nexthop, port=None,
policy='dst-ip', may_exist=False):
prefix = str(netaddr.IPNetwork(prefix))
if nexthop != const.ROUTE_DISCARD:
nexthop = str(netaddr.IPAddress(nexthop))
super().__init__(api)
self.router = router
self.prefix = prefix
self.nexthop = nexthop