go.ligato.io/vpp-agent/v3@v3.5.0/clientv2/vpp/dbadapter/data_change_db.go (about)

     1  // Copyright (c) 2017 Cisco and/or its affiliates.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at:
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package dbadapter
    16  
    17  import (
    18  	"context"
    19  
    20  	"go.ligato.io/cn-infra/v2/db/keyval"
    21  	"go.ligato.io/vpp-agent/v3/pkg/models"
    22  	"go.ligato.io/vpp-agent/v3/plugins/orchestrator/contextdecorator"
    23  
    24  	vppclient "go.ligato.io/vpp-agent/v3/clientv2/vpp"
    25  	abf "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/abf"
    26  	acl "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/acl"
    27  	intf "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/interfaces"
    28  	ipfix "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/ipfix"
    29  	ipsec "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/ipsec"
    30  	l2 "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/l2"
    31  	l3 "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/l3"
    32  	nat "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/nat"
    33  	punt "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/punt"
    34  	stn "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/stn"
    35  )
    36  
    37  // NewDataChangeDSL returns a new instance of DataChangeDSL which implements
    38  // the data change DSL for VPP config.
    39  // Transaction <txn> is used to propagate changes to plugins.
    40  func NewDataChangeDSL(txn keyval.ProtoTxn) *DataChangeDSL {
    41  	return &DataChangeDSL{txn: txn}
    42  }
    43  
    44  // DataChangeDSL is an implementation of Domain Specific Language (DSL)
    45  // for changes of the VPP configuration.
    46  type DataChangeDSL struct {
    47  	txn keyval.ProtoTxn
    48  }
    49  
    50  // PutDSL implements put operations of data change DSL.
    51  type PutDSL struct {
    52  	parent *DataChangeDSL
    53  }
    54  
    55  // DeleteDSL implements delete operations of data change DSL.
    56  type DeleteDSL struct {
    57  	parent *DataChangeDSL
    58  }
    59  
    60  // Put initiates a chained sequence of data change DSL statements declaring
    61  // new configurable objects or changing existing ones.
    62  func (dsl *DataChangeDSL) Put() vppclient.PutDSL {
    63  	return &PutDSL{dsl}
    64  }
    65  
    66  // Delete initiates a chained sequence of data change DSL statements
    67  // removing existing configurable objects.
    68  func (dsl *DataChangeDSL) Delete() vppclient.DeleteDSL {
    69  	return &DeleteDSL{dsl}
    70  }
    71  
    72  // Send propagates requested changes to the plugins.
    73  func (dsl *DataChangeDSL) Send() vppclient.Reply {
    74  	ctx := context.Background()
    75  	ctx = contextdecorator.DataSrcContext(ctx, "localclient")
    76  	err := dsl.txn.Commit(ctx)
    77  	return &Reply{err}
    78  }
    79  
    80  // Interface adds a request to create or update VPP network interface.
    81  func (dsl *PutDSL) Interface(val *intf.Interface) vppclient.PutDSL {
    82  	dsl.parent.txn.Put(intf.InterfaceKey(val.Name), val)
    83  	return dsl
    84  }
    85  
    86  // Span adds VPP span to the change request.
    87  func (dsl *PutDSL) Span(val *intf.Span) vppclient.PutDSL {
    88  	key := intf.SpanKey(val.InterfaceFrom, val.InterfaceTo)
    89  	dsl.parent.txn.Put(key, val)
    90  	return dsl
    91  }
    92  
    93  // ACL adds a request to create or update VPP Access Control List.
    94  func (dsl *PutDSL) ACL(val *acl.ACL) vppclient.PutDSL {
    95  	dsl.parent.txn.Put(acl.Key(val.Name), val)
    96  	return dsl
    97  }
    98  
    99  // ABF adds a request to create or update VPP ACL-based forwarding
   100  func (dsl *PutDSL) ABF(val *abf.ABF) vppclient.PutDSL {
   101  	dsl.parent.txn.Put(models.Key(val), val)
   102  	return dsl
   103  }
   104  
   105  // BD adds a request to create or update VPP Bridge Domain.
   106  func (dsl *PutDSL) BD(val *l2.BridgeDomain) vppclient.PutDSL {
   107  	dsl.parent.txn.Put(l2.BridgeDomainKey(val.Name), val)
   108  	return dsl
   109  }
   110  
   111  // BDFIB adds a request to create or update VPP L2 Forwarding Information Base.
   112  func (dsl *PutDSL) BDFIB(val *l2.FIBEntry) vppclient.PutDSL {
   113  	dsl.parent.txn.Put(l2.FIBKey(val.BridgeDomain, val.PhysAddress), val)
   114  	return dsl
   115  }
   116  
   117  // XConnect adds a request to create or update VPP Cross Connect.
   118  func (dsl *PutDSL) XConnect(val *l2.XConnectPair) vppclient.PutDSL {
   119  	dsl.parent.txn.Put(l2.XConnectKey(val.ReceiveInterface), val)
   120  	return dsl
   121  }
   122  
   123  // VrfTable adds a request to create or update VPP VRF table.
   124  func (dsl *PutDSL) VrfTable(val *l3.VrfTable) vppclient.PutDSL {
   125  	dsl.parent.txn.Put(l3.VrfTableKey(val.Id, val.Protocol), val)
   126  	return dsl
   127  }
   128  
   129  // StaticRoute adds a request to create or update VPP L3 Static Route.
   130  func (dsl *PutDSL) StaticRoute(val *l3.Route) vppclient.PutDSL {
   131  	dsl.parent.txn.Put(models.Key(val), val)
   132  	return dsl
   133  }
   134  
   135  // Arp adds a request to create or update VPP L3 ARP entry.
   136  func (dsl *PutDSL) Arp(arp *l3.ARPEntry) vppclient.PutDSL {
   137  	dsl.parent.txn.Put(l3.ArpEntryKey(arp.Interface, arp.IpAddress), arp)
   138  	return dsl
   139  }
   140  
   141  // ProxyArp adds a request to create or update VPP L3 proxy ARP.
   142  func (dsl *PutDSL) ProxyArp(proxyArp *l3.ProxyARP) vppclient.PutDSL {
   143  	dsl.parent.txn.Put(models.Key(&l3.ProxyARP{}), proxyArp)
   144  	return dsl
   145  }
   146  
   147  // IPScanNeighbor adds L3 IP Scan Neighbor to the RESYNC request.
   148  func (dsl *PutDSL) IPScanNeighbor(ipScanNeigh *l3.IPScanNeighbor) vppclient.PutDSL {
   149  	dsl.parent.txn.Put(models.Key(&l3.IPScanNeighbor{}), ipScanNeigh)
   150  	return dsl
   151  }
   152  
   153  // StnRule adds a request to create or update STN rule.
   154  func (dsl *PutDSL) StnRule(val *stn.Rule) vppclient.PutDSL {
   155  	dsl.parent.txn.Put(stn.Key(val.Interface, val.IpAddress), val)
   156  	return dsl
   157  }
   158  
   159  // NAT44Global adds a request to set global configuration for NAT44
   160  func (dsl *PutDSL) NAT44Global(nat44 *nat.Nat44Global) vppclient.PutDSL {
   161  	dsl.parent.txn.Put(models.Key(&nat.Nat44Global{}), nat44)
   162  	return dsl
   163  }
   164  
   165  // DNAT44 adds a request to create or update DNAT44 configuration
   166  func (dsl *PutDSL) DNAT44(nat44 *nat.DNat44) vppclient.PutDSL {
   167  	dsl.parent.txn.Put(nat.DNAT44Key(nat44.Label), nat44)
   168  	return dsl
   169  }
   170  
   171  // NAT44Interface adds a request to create or update NAT44 interface configuration.
   172  func (dsl *PutDSL) NAT44Interface(natIf *nat.Nat44Interface) vppclient.PutDSL {
   173  	dsl.parent.txn.Put(models.Key(natIf), natIf)
   174  	return dsl
   175  }
   176  
   177  // NAT44AddressPool adds a request to create or update NAT44 address pool.
   178  func (dsl *PutDSL) NAT44AddressPool(pool *nat.Nat44AddressPool) vppclient.PutDSL {
   179  	dsl.parent.txn.Put(models.Key(pool), pool)
   180  	return dsl
   181  }
   182  
   183  // IPSecSA adds request to create a new Security Association
   184  func (dsl *PutDSL) IPSecSA(sa *ipsec.SecurityAssociation) vppclient.PutDSL {
   185  	dsl.parent.txn.Put(ipsec.SAKey(sa.Index), sa)
   186  	return dsl
   187  }
   188  
   189  // IPSecSPD adds request to create a new Security Policy Database
   190  func (dsl *PutDSL) IPSecSPD(spd *ipsec.SecurityPolicyDatabase) vppclient.PutDSL {
   191  	dsl.parent.txn.Put(ipsec.SPDKey(spd.Index), spd)
   192  	return dsl
   193  }
   194  
   195  // IPSecSP adds request to create a new Security Policy
   196  func (dsl *PutDSL) IPSecSP(sp *ipsec.SecurityPolicy) vppclient.PutDSL {
   197  	dsl.parent.txn.Put(models.Key(sp), sp)
   198  	return dsl
   199  }
   200  
   201  // IPSecTunnelProtection adds request to delete an IPSec tunnel protection from an interface
   202  func (dsl *PutDSL) IPSecTunnelProtection(tp *ipsec.TunnelProtection) vppclient.PutDSL {
   203  	dsl.parent.txn.Put(models.Key(tp), tp)
   204  	return dsl
   205  }
   206  
   207  // PuntIPRedirect adds request to create or update rule to punt L3 traffic via interface.
   208  func (dsl *PutDSL) PuntIPRedirect(val *punt.IPRedirect) vppclient.PutDSL {
   209  	dsl.parent.txn.Put(punt.IPRedirectKey(val.L3Protocol, val.TxInterface), val)
   210  	return dsl
   211  }
   212  
   213  // PuntToHost adds request to create or update rule to punt L4 traffic to a host.
   214  func (dsl *PutDSL) PuntToHost(val *punt.ToHost) vppclient.PutDSL {
   215  	dsl.parent.txn.Put(punt.ToHostKey(val.L3Protocol, val.L4Protocol, val.Port), val)
   216  	return dsl
   217  }
   218  
   219  // PuntException adds request to create or update exception to punt specific packets.
   220  func (dsl *PutDSL) PuntException(val *punt.Exception) vppclient.PutDSL {
   221  	dsl.parent.txn.Put(models.Key(val), val)
   222  	return dsl
   223  }
   224  
   225  // IPFIX adds a request to update VPP IP Flow Information eXport configuration.
   226  func (dsl *PutDSL) IPFIX(val *ipfix.IPFIX) vppclient.PutDSL {
   227  	dsl.parent.txn.Put(models.Key(val), val)
   228  	return dsl
   229  }
   230  
   231  // FlowprobeParams adds a request to update VPP Flowprobe Params.
   232  func (dsl *PutDSL) FlowprobeParams(val *ipfix.FlowProbeParams) vppclient.PutDSL {
   233  	dsl.parent.txn.Put(models.Key(val), val)
   234  	return dsl
   235  }
   236  
   237  // FlowprobeFeature adds a request to enable Flowprobe Feature on interface.
   238  func (dsl *PutDSL) FlowprobeFeature(val *ipfix.FlowProbeFeature) vppclient.PutDSL {
   239  	dsl.parent.txn.Put(models.Key(val), val)
   240  	return dsl
   241  }
   242  
   243  // VRRP adds a request to add an existing VPP L3 VRRP entry.
   244  func (dsl *PutDSL) VRRP(val *l3.VRRPEntry) vppclient.PutDSL {
   245  	key := l3.VrrpEntryKey(val.Interface, val.VrId)
   246  	dsl.parent.txn.Put(key, val)
   247  	return dsl
   248  }
   249  
   250  // Delete changes the DSL mode to allow removal of an existing configuration.
   251  func (dsl *PutDSL) Delete() vppclient.DeleteDSL {
   252  	return &DeleteDSL{dsl.parent}
   253  }
   254  
   255  // Send propagates requested changes to the plugins.
   256  func (dsl *PutDSL) Send() vppclient.Reply {
   257  	return dsl.parent.Send()
   258  }
   259  
   260  // Interface adds a request to delete an existing VPP network interface.
   261  func (dsl *DeleteDSL) Interface(interfaceName string) vppclient.DeleteDSL {
   262  	dsl.parent.txn.Delete(intf.InterfaceKey(interfaceName))
   263  	return dsl
   264  }
   265  
   266  // Span adds VPP span to the RESYNC request.
   267  func (dsl *DeleteDSL) Span(val *intf.Span) vppclient.DeleteDSL {
   268  	key := intf.SpanKey(val.InterfaceFrom, val.InterfaceTo)
   269  	dsl.parent.txn.Delete(key)
   270  	return dsl
   271  }
   272  
   273  // ACL adds a request to delete an existing VPP Access Control List.
   274  func (dsl *DeleteDSL) ACL(aclName string) vppclient.DeleteDSL {
   275  	dsl.parent.txn.Delete(acl.Key(aclName))
   276  	return dsl
   277  }
   278  
   279  // ABF adds a request to delete and existing VPP ACL-based forwarding.
   280  func (dsl *DeleteDSL) ABF(abfIndex uint32) vppclient.DeleteDSL {
   281  	dsl.parent.txn.Delete(abf.Key(abfIndex))
   282  	return dsl
   283  }
   284  
   285  // BD adds a request to delete an existing VPP Bridge Domain.
   286  func (dsl *DeleteDSL) BD(bdName string) vppclient.DeleteDSL {
   287  	dsl.parent.txn.Delete(l2.BridgeDomainKey(bdName))
   288  	return dsl
   289  }
   290  
   291  // BDFIB adds a request to delete an existing VPP L2 Forwarding Information
   292  // Base.
   293  func (dsl *DeleteDSL) BDFIB(bdName string, mac string) vppclient.DeleteDSL {
   294  	dsl.parent.txn.Delete(l2.FIBKey(bdName, mac))
   295  	return dsl
   296  }
   297  
   298  // XConnect adds a request to delete an existing VPP Cross Connect.
   299  func (dsl *DeleteDSL) XConnect(rxIfName string) vppclient.DeleteDSL {
   300  	dsl.parent.txn.Delete(l2.XConnectKey(rxIfName))
   301  	return dsl
   302  }
   303  
   304  // VrfTable adds a request to delete existing VPP VRF table.
   305  func (dsl *DeleteDSL) VrfTable(id uint32, proto l3.VrfTable_Protocol) vppclient.DeleteDSL {
   306  	dsl.parent.txn.Delete(l3.VrfTableKey(id, proto))
   307  	return dsl
   308  }
   309  
   310  // StaticRoute adds a request to delete an existing VPP L3 Static Route.
   311  func (dsl *DeleteDSL) StaticRoute(iface string, vrf uint32, dstAddr string, nextHopAddr string) vppclient.DeleteDSL {
   312  	dsl.parent.txn.Delete(l3.RouteKey(iface, vrf, dstAddr, nextHopAddr))
   313  	return dsl
   314  }
   315  
   316  // Arp adds a request to delete an existing VPP L3 ARP entry.
   317  func (dsl *DeleteDSL) Arp(ifaceName string, ipAddr string) vppclient.DeleteDSL {
   318  	dsl.parent.txn.Delete(l3.ArpEntryKey(ifaceName, ipAddr))
   319  	return dsl
   320  }
   321  
   322  // ProxyArp adds a request to delete an existing VPP L3 proxy ARP.
   323  func (dsl *DeleteDSL) ProxyArp() vppclient.DeleteDSL {
   324  	dsl.parent.txn.Delete(models.Key(&l3.ProxyARP{}))
   325  	return dsl
   326  }
   327  
   328  // IPScanNeighbor adds a request to delete an existing VPP L3 IP Scan Neighbor.
   329  func (dsl *DeleteDSL) IPScanNeighbor() vppclient.DeleteDSL {
   330  	dsl.parent.txn.Delete(models.Key(&l3.IPScanNeighbor{}))
   331  	return dsl
   332  }
   333  
   334  // StnRule adds request to delete Stn rule.
   335  func (dsl *DeleteDSL) StnRule(iface, addr string) vppclient.DeleteDSL {
   336  	dsl.parent.txn.Delete(stn.Key(iface, addr))
   337  	return dsl
   338  }
   339  
   340  // NAT44Global adds a request to remove global configuration for NAT44
   341  func (dsl *DeleteDSL) NAT44Global() vppclient.DeleteDSL {
   342  	dsl.parent.txn.Delete(models.Key(&nat.Nat44Global{}))
   343  	return dsl
   344  }
   345  
   346  // DNAT44 adds a request to delete an existing DNAT44 configuration
   347  func (dsl *DeleteDSL) DNAT44(label string) vppclient.DeleteDSL {
   348  	dsl.parent.txn.Delete(nat.DNAT44Key(label))
   349  	return dsl
   350  }
   351  
   352  // NAT44Interface adds a request to delete NAT44 interface configuration.
   353  func (dsl *DeleteDSL) NAT44Interface(natIf *nat.Nat44Interface) vppclient.DeleteDSL {
   354  	dsl.parent.txn.Delete(models.Key(natIf))
   355  	return dsl
   356  }
   357  
   358  // NAT44AddressPool adds a request to create or update NAT44 address pool.
   359  func (dsl *DeleteDSL) NAT44AddressPool(pool *nat.Nat44AddressPool) vppclient.DeleteDSL {
   360  	dsl.parent.txn.Delete(models.Key(pool))
   361  	return dsl
   362  }
   363  
   364  // IPSecSA adds request to create a new Security Association
   365  func (dsl *DeleteDSL) IPSecSA(saIndex uint32) vppclient.DeleteDSL {
   366  	dsl.parent.txn.Delete(ipsec.SAKey(saIndex))
   367  	return dsl
   368  }
   369  
   370  // IPSecSPD adds request to create a new Security Policy Database
   371  func (dsl *DeleteDSL) IPSecSPD(spdIndex uint32) vppclient.DeleteDSL {
   372  	dsl.parent.txn.Delete(ipsec.SPDKey(spdIndex))
   373  	return dsl
   374  }
   375  
   376  // IPSecSP adds request to delete Security Policy
   377  func (dsl *DeleteDSL) IPSecSP(sp *ipsec.SecurityPolicy) vppclient.DeleteDSL {
   378  	dsl.parent.txn.Delete(models.Key(sp))
   379  	return dsl
   380  }
   381  
   382  // IPSecTunnelProtection adds request to delete an IPSec tunnel protection from an interface
   383  func (dsl *DeleteDSL) IPSecTunnelProtection(tp *ipsec.TunnelProtection) vppclient.DeleteDSL {
   384  	dsl.parent.txn.Delete(models.Key(tp))
   385  	return dsl
   386  }
   387  
   388  // PuntIPRedirect adds request to delete a rule used to punt L3 traffic via interface.
   389  func (dsl *DeleteDSL) PuntIPRedirect(l3Proto punt.L3Protocol, txInterface string) vppclient.DeleteDSL {
   390  	dsl.parent.txn.Delete(punt.IPRedirectKey(l3Proto, txInterface))
   391  	return dsl
   392  }
   393  
   394  // PuntToHost adds request to delete a rule used to punt L4 traffic to a host.
   395  func (dsl *DeleteDSL) PuntToHost(l3Proto punt.L3Protocol, l4Proto punt.L4Protocol, port uint32) vppclient.DeleteDSL {
   396  	dsl.parent.txn.Delete(punt.ToHostKey(l3Proto, l4Proto, port))
   397  	return dsl
   398  }
   399  
   400  // PuntException adds request to delete exception to punt specific packets.
   401  func (dsl *DeleteDSL) PuntException(reason string) vppclient.DeleteDSL {
   402  	dsl.parent.txn.Delete(punt.ExceptionKey(reason))
   403  	return dsl
   404  }
   405  
   406  // FlowprobeFeature adds a request to disable Flowprobe Feature on interface.
   407  func (dsl *DeleteDSL) FlowprobeFeature(val *ipfix.FlowProbeFeature) vppclient.DeleteDSL {
   408  	dsl.parent.txn.Delete(models.Key(val))
   409  	return dsl
   410  }
   411  
   412  // Put changes the DSL mode to allow configuration editing.
   413  func (dsl *DeleteDSL) Put() vppclient.PutDSL {
   414  	return &PutDSL{dsl.parent}
   415  }
   416  
   417  // Send propagates requested changes to the plugins.
   418  func (dsl *DeleteDSL) Send() vppclient.Reply {
   419  	return dsl.parent.Send()
   420  }
   421  
   422  // Reply interface allows to wait for a reply to previously called Send() and
   423  // extract the result from it (success/error).
   424  type Reply struct {
   425  	err error
   426  }
   427  
   428  // ReceiveReply waits for a reply to previously called Send() and returns
   429  // the result (error or nil).
   430  func (dsl Reply) ReceiveReply() error {
   431  	return dsl.err
   432  }
   433  
   434  // VRRP adds a request to delete an existing VPP L3 VRRP entry.
   435  func (dsl *DeleteDSL) VRRP(val *l3.VRRPEntry) vppclient.DeleteDSL {
   436  	key := l3.VrrpEntryKey(val.Interface, val.VrId)
   437  	dsl.parent.txn.Delete(key)
   438  	return dsl
   439  }