forked from ligato/vpp-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_change_db.go
352 lines (295 loc) · 11.7 KB
/
data_change_db.go
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
// Copyright (c) 2017 Cisco and/or its affiliates.
//
// 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.
package dbadapter
import (
"context"
"github.com/ligato/cn-infra/db/keyval"
"github.com/ligato/vpp-agent/pkg/models"
abf "github.com/ligato/vpp-agent/api/models/vpp/abf"
acl "github.com/ligato/vpp-agent/api/models/vpp/acl"
intf "github.com/ligato/vpp-agent/api/models/vpp/interfaces"
ipsec "github.com/ligato/vpp-agent/api/models/vpp/ipsec"
l2 "github.com/ligato/vpp-agent/api/models/vpp/l2"
l3 "github.com/ligato/vpp-agent/api/models/vpp/l3"
nat "github.com/ligato/vpp-agent/api/models/vpp/nat"
punt "github.com/ligato/vpp-agent/api/models/vpp/punt"
stn "github.com/ligato/vpp-agent/api/models/vpp/stn"
vppclient "github.com/ligato/vpp-agent/clientv2/vpp"
orch "github.com/ligato/vpp-agent/plugins/orchestrator"
)
// NewDataChangeDSL returns a new instance of DataChangeDSL which implements
// the data change DSL for VPP config.
// Transaction <txn> is used to propagate changes to plugins.
func NewDataChangeDSL(txn keyval.ProtoTxn) *DataChangeDSL {
return &DataChangeDSL{txn: txn}
}
// DataChangeDSL is an implementation of Domain Specific Language (DSL)
// for changes of the VPP configuration.
type DataChangeDSL struct {
txn keyval.ProtoTxn
}
// PutDSL implements put operations of data change DSL.
type PutDSL struct {
parent *DataChangeDSL
}
// DeleteDSL implements delete operations of data change DSL.
type DeleteDSL struct {
parent *DataChangeDSL
}
// Put initiates a chained sequence of data change DSL statements declaring
// new configurable objects or changing existing ones.
func (dsl *DataChangeDSL) Put() vppclient.PutDSL {
return &PutDSL{dsl}
}
// Delete initiates a chained sequence of data change DSL statements
// removing existing configurable objects.
func (dsl *DataChangeDSL) Delete() vppclient.DeleteDSL {
return &DeleteDSL{dsl}
}
// Send propagates requested changes to the plugins.
func (dsl *DataChangeDSL) Send() vppclient.Reply {
ctx := context.Background()
ctx = orch.DataSrcContext(ctx, "localclient")
err := dsl.txn.Commit(ctx)
return &Reply{err}
}
// Interface adds a request to create or update VPP network interface.
func (dsl *PutDSL) Interface(val *intf.Interface) vppclient.PutDSL {
dsl.parent.txn.Put(intf.InterfaceKey(val.Name), val)
return dsl
}
// Span adds VPP span to the change request.
func (dsl *PutDSL) Span(val *intf.Span) vppclient.PutDSL {
key := intf.SpanKey(val.InterfaceFrom, val.InterfaceTo)
dsl.parent.txn.Put(key, val)
return dsl
}
// ACL adds a request to create or update VPP Access Control List.
func (dsl *PutDSL) ACL(val *acl.ACL) vppclient.PutDSL {
dsl.parent.txn.Put(acl.Key(val.Name), val)
return dsl
}
// ABF adds a request to create or update VPP ACL-based forwarding
func (dsl *PutDSL) ABF(val *abf.ABF) vppclient.PutDSL {
dsl.parent.txn.Put(models.Key(val), val)
return dsl
}
// BD adds a request to create or update VPP Bridge Domain.
func (dsl *PutDSL) BD(val *l2.BridgeDomain) vppclient.PutDSL {
dsl.parent.txn.Put(l2.BridgeDomainKey(val.Name), val)
return dsl
}
// BDFIB adds a request to create or update VPP L2 Forwarding Information Base.
func (dsl *PutDSL) BDFIB(val *l2.FIBEntry) vppclient.PutDSL {
dsl.parent.txn.Put(l2.FIBKey(val.BridgeDomain, val.PhysAddress), val)
return dsl
}
// XConnect adds a request to create or update VPP Cross Connect.
func (dsl *PutDSL) XConnect(val *l2.XConnectPair) vppclient.PutDSL {
dsl.parent.txn.Put(l2.XConnectKey(val.ReceiveInterface), val)
return dsl
}
// VrfTable adds a request to create or update VPP VRF table.
func (dsl *PutDSL) VrfTable(val *l3.VrfTable) vppclient.PutDSL {
dsl.parent.txn.Put(l3.VrfTableKey(val.Id, val.Protocol), val)
return dsl
}
// StaticRoute adds a request to create or update VPP L3 Static Route.
func (dsl *PutDSL) StaticRoute(val *l3.Route) vppclient.PutDSL {
dsl.parent.txn.Put(models.Key(val), val)
return dsl
}
// Arp adds a request to create or update VPP L3 ARP entry.
func (dsl *PutDSL) Arp(arp *l3.ARPEntry) vppclient.PutDSL {
dsl.parent.txn.Put(l3.ArpEntryKey(arp.Interface, arp.IpAddress), arp)
return dsl
}
// ProxyArp adds a request to create or update VPP L3 proxy ARP.
func (dsl *PutDSL) ProxyArp(proxyArp *l3.ProxyARP) vppclient.PutDSL {
dsl.parent.txn.Put(models.Key(&l3.ProxyARP{}), proxyArp)
return dsl
}
// IPScanNeighbor adds L3 IP Scan Neighbor to the RESYNC request.
func (dsl *PutDSL) IPScanNeighbor(ipScanNeigh *l3.IPScanNeighbor) vppclient.PutDSL {
dsl.parent.txn.Put(models.Key(&l3.IPScanNeighbor{}), ipScanNeigh)
return dsl
}
// StnRule adds a request to create or update STN rule.
func (dsl *PutDSL) StnRule(val *stn.Rule) vppclient.PutDSL {
dsl.parent.txn.Put(stn.Key(val.Interface, val.IpAddress), val)
return dsl
}
// NAT44Global adds a request to set global configuration for NAT44
func (dsl *PutDSL) NAT44Global(nat44 *nat.Nat44Global) vppclient.PutDSL {
dsl.parent.txn.Put(models.Key(&nat.Nat44Global{}), nat44)
return dsl
}
// DNAT44 adds a request to create or update DNAT44 configuration
func (dsl *PutDSL) DNAT44(nat44 *nat.DNat44) vppclient.PutDSL {
dsl.parent.txn.Put(nat.DNAT44Key(nat44.Label), nat44)
return dsl
}
// IPSecSA adds request to create a new Security Association
func (dsl *PutDSL) IPSecSA(sa *ipsec.SecurityAssociation) vppclient.PutDSL {
dsl.parent.txn.Put(ipsec.SAKey(sa.Index), sa)
return dsl
}
// IPSecSPD adds request to create a new Security Policy Database
func (dsl *PutDSL) IPSecSPD(spd *ipsec.SecurityPolicyDatabase) vppclient.PutDSL {
dsl.parent.txn.Put(ipsec.SPDKey(spd.Index), spd)
return dsl
}
// PuntIPRedirect adds request to create or update rule to punt L3 traffic via interface.
func (dsl *PutDSL) PuntIPRedirect(val *punt.IPRedirect) vppclient.PutDSL {
dsl.parent.txn.Put(punt.IPRedirectKey(val.L3Protocol, val.TxInterface), val)
return dsl
}
// PuntToHost adds request to create or update rule to punt L4 traffic to a host.
func (dsl *PutDSL) PuntToHost(val *punt.ToHost) vppclient.PutDSL {
dsl.parent.txn.Put(punt.ToHostKey(val.L3Protocol, val.L4Protocol, val.Port), val)
return dsl
}
// PuntException adds request to create or update exception to punt specific packets.
func (dsl *PutDSL) PuntException(val *punt.Exception) vppclient.PutDSL {
dsl.parent.txn.Put(models.Key(val), val)
return dsl
}
// Delete changes the DSL mode to allow removal of an existing configuration.
func (dsl *PutDSL) Delete() vppclient.DeleteDSL {
return &DeleteDSL{dsl.parent}
}
// Send propagates requested changes to the plugins.
func (dsl *PutDSL) Send() vppclient.Reply {
return dsl.parent.Send()
}
// Interface adds a request to delete an existing VPP network interface.
func (dsl *DeleteDSL) Interface(interfaceName string) vppclient.DeleteDSL {
dsl.parent.txn.Delete(intf.InterfaceKey(interfaceName))
return dsl
}
// Span adds VPP span to the RESYNC request.
func (dsl *DeleteDSL) Span(val *intf.Span) vppclient.DeleteDSL {
key := intf.SpanKey(val.InterfaceFrom, val.InterfaceTo)
dsl.parent.txn.Delete(key)
return dsl
}
// ACL adds a request to delete an existing VPP Access Control List.
func (dsl *DeleteDSL) ACL(aclName string) vppclient.DeleteDSL {
dsl.parent.txn.Delete(acl.Key(aclName))
return dsl
}
// ABF adds a request to delete and existing VPP ACL-based forwarding.
func (dsl *DeleteDSL) ABF(abfIndex uint32) vppclient.DeleteDSL {
dsl.parent.txn.Delete(abf.Key(abfIndex))
return dsl
}
// BD adds a request to delete an existing VPP Bridge Domain.
func (dsl *DeleteDSL) BD(bdName string) vppclient.DeleteDSL {
dsl.parent.txn.Delete(l2.BridgeDomainKey(bdName))
return dsl
}
// BDFIB adds a request to delete an existing VPP L2 Forwarding Information
// Base.
func (dsl *DeleteDSL) BDFIB(bdName string, mac string) vppclient.DeleteDSL {
dsl.parent.txn.Delete(l2.FIBKey(bdName, mac))
return dsl
}
// XConnect adds a request to delete an existing VPP Cross Connect.
func (dsl *DeleteDSL) XConnect(rxIfName string) vppclient.DeleteDSL {
dsl.parent.txn.Delete(l2.XConnectKey(rxIfName))
return dsl
}
// VrfTable adds a request to delete existing VPP VRF table.
func (dsl *DeleteDSL) VrfTable(id uint32, proto l3.VrfTable_Protocol) vppclient.DeleteDSL {
dsl.parent.txn.Delete(l3.VrfTableKey(id, proto))
return dsl
}
// StaticRoute adds a request to delete an existing VPP L3 Static Route.
func (dsl *DeleteDSL) StaticRoute(iface string, vrf uint32, dstAddr string, nextHopAddr string) vppclient.DeleteDSL {
dsl.parent.txn.Delete(l3.RouteKey(iface, vrf, dstAddr, nextHopAddr))
return dsl
}
// Arp adds a request to delete an existing VPP L3 ARP entry.
func (dsl *DeleteDSL) Arp(ifaceName string, ipAddr string) vppclient.DeleteDSL {
dsl.parent.txn.Delete(l3.ArpEntryKey(ifaceName, ipAddr))
return dsl
}
// ProxyArp adds a request to delete an existing VPP L3 proxy ARP.
func (dsl *DeleteDSL) ProxyArp() vppclient.DeleteDSL {
dsl.parent.txn.Delete(models.Key(&l3.ProxyARP{}))
return dsl
}
// IPScanNeighbor adds a request to delete an existing VPP L3 IP Scan Neighbor.
func (dsl *DeleteDSL) IPScanNeighbor() vppclient.DeleteDSL {
dsl.parent.txn.Delete(models.Key(&l3.IPScanNeighbor{}))
return dsl
}
// StnRule adds request to delete Stn rule.
func (dsl *DeleteDSL) StnRule(iface, addr string) vppclient.DeleteDSL {
dsl.parent.txn.Delete(stn.Key(iface, addr))
return dsl
}
// NAT44Global adds a request to remove global configuration for NAT44
func (dsl *DeleteDSL) NAT44Global() vppclient.DeleteDSL {
dsl.parent.txn.Delete(models.Key(&nat.Nat44Global{}))
return dsl
}
// DNAT44 adds a request to delete an existing DNAT44 configuration
func (dsl *DeleteDSL) DNAT44(label string) vppclient.DeleteDSL {
dsl.parent.txn.Delete(nat.DNAT44Key(label))
return dsl
}
// IPSecSA adds request to create a new Security Association
func (dsl *DeleteDSL) IPSecSA(saIndex string) vppclient.DeleteDSL {
dsl.parent.txn.Delete(ipsec.SAKey(saIndex))
return dsl
}
// IPSecSPD adds request to create a new Security Policy Database
func (dsl *DeleteDSL) IPSecSPD(spdIndex string) vppclient.DeleteDSL {
dsl.parent.txn.Delete(ipsec.SPDKey(spdIndex))
return dsl
}
// PuntIPRedirect adds request to delete a rule used to punt L3 traffic via interface.
func (dsl *DeleteDSL) PuntIPRedirect(l3Proto punt.L3Protocol, txInterface string) vppclient.DeleteDSL {
dsl.parent.txn.Delete(punt.IPRedirectKey(l3Proto, txInterface))
return dsl
}
// PuntToHost adds request to delete a rule used to punt L4 traffic to a host.
func (dsl *DeleteDSL) PuntToHost(l3Proto punt.L3Protocol, l4Proto punt.L4Protocol, port uint32) vppclient.DeleteDSL {
dsl.parent.txn.Delete(punt.ToHostKey(l3Proto, l4Proto, port))
return dsl
}
// PuntException adds request to delete exception to punt specific packets.
func (dsl *DeleteDSL) PuntException(reason string) vppclient.DeleteDSL {
dsl.parent.txn.Delete(punt.ExceptionKey(reason))
return dsl
}
// Put changes the DSL mode to allow configuration editing.
func (dsl *DeleteDSL) Put() vppclient.PutDSL {
return &PutDSL{dsl.parent}
}
// Send propagates requested changes to the plugins.
func (dsl *DeleteDSL) Send() vppclient.Reply {
return dsl.parent.Send()
}
// Reply interface allows to wait for a reply to previously called Send() and
// extract the result from it (success/error).
type Reply struct {
err error
}
// ReceiveReply waits for a reply to previously called Send() and returns
// the result (error or nil).
func (dsl Reply) ReceiveReply() error {
return dsl.err
}