go.ligato.io/vpp-agent/v3@v3.5.0/plugins/vpp/srplugin/descriptor/srv6_global.go (about)

     1  package descriptor
     2  
     3  import (
     4  	"go.ligato.io/cn-infra/v2/logging"
     5  	"google.golang.org/protobuf/proto"
     6  
     7  	scheduler "go.ligato.io/vpp-agent/v3/plugins/kvscheduler/api"
     8  	"go.ligato.io/vpp-agent/v3/plugins/vpp/srplugin/descriptor/adapter"
     9  	"go.ligato.io/vpp-agent/v3/plugins/vpp/srplugin/vppcalls"
    10  	srv6 "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/srv6"
    11  )
    12  
    13  const (
    14  	// LocalSIDDescriptorName is the name of the descriptor for VPP LocalSIDs
    15  	SRv6GlobalDescriptorName = "vpp-srv6-global"
    16  )
    17  
    18  const (
    19  	defaultEncapSourceAddress = "::"
    20  )
    21  
    22  var defaultSRv6Global = &srv6.SRv6Global{
    23  	EncapSourceAddress: defaultEncapSourceAddress,
    24  }
    25  
    26  // SRv6GlobalDescriptor teaches KVScheduler how to configure VPP SR Encap Source Address.
    27  type SRv6GlobalDescriptor struct {
    28  	// dependencies
    29  	log       logging.Logger
    30  	srHandler vppcalls.SRv6VppAPI
    31  }
    32  
    33  // NewSRv6GlobalDescriptor creates a new instance of the SRv6GlobalDescriptor.
    34  func NewSRv6GlobalDescriptor(srHandler vppcalls.SRv6VppAPI, log logging.PluginLogger) *scheduler.KVDescriptor {
    35  	ctx := &SRv6GlobalDescriptor{
    36  		log:       log.NewLogger("encapsource-descriptor"),
    37  		srHandler: srHandler,
    38  	}
    39  
    40  	typedDescr := &adapter.SRv6GlobalDescriptor{
    41  		Name:            SRv6GlobalDescriptorName,
    42  		NBKeyPrefix:     srv6.ModelSRv6Global.KeyPrefix(),
    43  		ValueTypeName:   srv6.ModelSRv6Global.ProtoName(),
    44  		KeySelector:     srv6.ModelSRv6Global.IsKeyValid,
    45  		ValueComparator: ctx.EquivalentSRv6Global,
    46  		Create:          ctx.Create,
    47  		Update:          ctx.Update,
    48  		Delete:          ctx.Delete,
    49  	}
    50  	return adapter.NewSRv6GlobalDescriptor(typedDescr)
    51  }
    52  
    53  // EquivalentSRv6Global compares the IP Scan Neighbor values.
    54  func (d *SRv6GlobalDescriptor) EquivalentSRv6Global(key string, oldValue, newValue *srv6.SRv6Global) bool {
    55  	return proto.Equal(withDefaults(oldValue), withDefaults(newValue))
    56  }
    57  
    58  // Create adds VPP IP Scan Neighbor.
    59  func (d *SRv6GlobalDescriptor) Create(key string, value *srv6.SRv6Global) (metadata interface{}, err error) {
    60  	return d.Update(key, defaultSRv6Global, withDefaults(value), nil)
    61  }
    62  
    63  // Delete deletes VPP IP Scan Neighbor.
    64  func (d *SRv6GlobalDescriptor) Delete(key string, value *srv6.SRv6Global, metadata interface{}) error {
    65  	_, err := d.Update(key, withDefaults(value), defaultSRv6Global, metadata)
    66  	return err
    67  }
    68  
    69  // Update modifies VPP IP Scan Neighbor.
    70  func (d *SRv6GlobalDescriptor) Update(key string, oldValue, newValue *srv6.SRv6Global, oldMetadata interface{}) (newMetadata interface{}, err error) {
    71  	if err := d.srHandler.SetEncapsSourceAddress(newValue.EncapSourceAddress); err != nil {
    72  		return nil, err
    73  	}
    74  	return nil, nil
    75  }
    76  
    77  func withDefaults(orig *srv6.SRv6Global) *srv6.SRv6Global {
    78  	var val = proto.Clone(orig).(*srv6.SRv6Global)
    79  	if val.EncapSourceAddress == "" {
    80  		val.EncapSourceAddress = defaultEncapSourceAddress
    81  	}
    82  	return val
    83  }