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

     1  //  Copyright (c) 2018 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 descriptor
    16  
    17  import (
    18  	"github.com/pkg/errors"
    19  	"go.ligato.io/cn-infra/v2/logging"
    20  	"google.golang.org/protobuf/proto"
    21  
    22  	kvs "go.ligato.io/vpp-agent/v3/plugins/kvscheduler/api"
    23  	"go.ligato.io/vpp-agent/v3/plugins/vpp/l3plugin/descriptor/adapter"
    24  	"go.ligato.io/vpp-agent/v3/plugins/vpp/l3plugin/vppcalls"
    25  	interfaces "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/interfaces"
    26  	l3 "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/l3"
    27  )
    28  
    29  const (
    30  	// ProxyArpInterfaceDescriptorName is the name of the descriptor.
    31  	ProxyArpInterfaceDescriptorName = "vpp-proxy-arp-interface"
    32  
    33  	// dependency labels
    34  	proxyArpInterfaceDep = "interface-exists"
    35  )
    36  
    37  // ProxyArpInterfaceDescriptor teaches KVScheduler how to configure VPP proxy ARP interfaces.
    38  type ProxyArpInterfaceDescriptor struct {
    39  	log             logging.Logger
    40  	proxyArpHandler vppcalls.ProxyArpVppAPI
    41  	scheduler       kvs.KVScheduler
    42  }
    43  
    44  // NewProxyArpInterfaceDescriptor creates a new instance of the ProxyArpInterfaceDescriptor.
    45  func NewProxyArpInterfaceDescriptor(scheduler kvs.KVScheduler,
    46  	proxyArpHandler vppcalls.ProxyArpVppAPI, log logging.PluginLogger) *kvs.KVDescriptor {
    47  
    48  	ctx := &ProxyArpInterfaceDescriptor{
    49  		scheduler:       scheduler,
    50  		proxyArpHandler: proxyArpHandler,
    51  		log:             log.NewLogger("proxy-arp-interface-descriptor"),
    52  	}
    53  
    54  	typedDescr := &adapter.ProxyARPInterfaceDescriptor{
    55  		Name: ProxyArpInterfaceDescriptorName,
    56  		KeySelector: func(key string) bool {
    57  			_, isProxyARPInterfaceKey := l3.ParseProxyARPInterfaceKey(key)
    58  			return isProxyARPInterfaceKey
    59  		},
    60  		ValueTypeName: string(proto.MessageName(&l3.ProxyARP_Interface{})),
    61  		Create:        ctx.Create,
    62  		Delete:        ctx.Delete,
    63  		Dependencies:  ctx.Dependencies,
    64  	}
    65  	return adapter.NewProxyARPInterfaceDescriptor(typedDescr)
    66  }
    67  
    68  // Create enables VPP Proxy ARP for interface.
    69  func (d *ProxyArpInterfaceDescriptor) Create(key string, value *l3.ProxyARP_Interface) (metadata interface{}, err error) {
    70  	if err := d.proxyArpHandler.EnableProxyArpInterface(value.Name); err != nil {
    71  		return nil, errors.Errorf("failed to enable proxy ARP for interface %s: %v", value.Name, err)
    72  	}
    73  	return nil, nil
    74  }
    75  
    76  // Delete disables VPP Proxy ARP for interface.
    77  func (d *ProxyArpInterfaceDescriptor) Delete(key string, value *l3.ProxyARP_Interface, metadata interface{}) error {
    78  	if err := d.proxyArpHandler.DisableProxyArpInterface(value.Name); err != nil {
    79  		return errors.Errorf("failed to disable proxy ARP for interface %s: %v", value.Name, err)
    80  	}
    81  	return nil
    82  }
    83  
    84  // Dependencies returns list of dependencies for VPP Proxy ARP interface.
    85  func (d *ProxyArpInterfaceDescriptor) Dependencies(key string, value *l3.ProxyARP_Interface) (deps []kvs.Dependency) {
    86  	return []kvs.Dependency{
    87  		{
    88  			Label: proxyArpInterfaceDep,
    89  			Key:   interfaces.InterfaceKey(value.Name),
    90  		},
    91  	}
    92  }