go.ligato.io/vpp-agent/v3@v3.5.0/plugins/vpp/natplugin/descriptor/nat44_global_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  	"go.ligato.io/cn-infra/v2/logging"
    19  	"google.golang.org/protobuf/proto"
    20  
    21  	kvs "go.ligato.io/vpp-agent/v3/plugins/kvscheduler/api"
    22  	"go.ligato.io/vpp-agent/v3/plugins/vpp/natplugin/descriptor/adapter"
    23  	"go.ligato.io/vpp-agent/v3/plugins/vpp/natplugin/vppcalls"
    24  	interfaces "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/interfaces"
    25  	nat "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/nat"
    26  )
    27  
    28  const (
    29  	// NAT44GlobalInterfaceDescriptorName is the name of the descriptor for VPP NAT44
    30  	// features applied to interfaces.
    31  	NAT44GlobalInterfaceDescriptorName = "vpp-nat44-global-interface"
    32  
    33  	// dependency labels
    34  	natInterfaceGlobalCfgDep = "nat44-global-config-exists"
    35  	natInterfaceDep          = "interface-exists"
    36  )
    37  
    38  // NAT44GlobalInterfaceDescriptor teaches KVScheduler how to configure VPP NAT interface
    39  // features.
    40  // Deprecated. Functionality moved to NAT44InterfaceDescriptor. Kept for backward compatibility.
    41  type NAT44GlobalInterfaceDescriptor struct {
    42  	log        logging.Logger
    43  	natHandler vppcalls.NatVppAPI
    44  }
    45  
    46  // NewNAT44GlobalInterfaceDescriptor creates a new instance of the NAT44GlobalInterface descriptor.
    47  // Deprecated. Functionality moved to NAT44InterfaceDescriptor. Kept for backward compatibility.
    48  func NewNAT44GlobalInterfaceDescriptor(natHandler vppcalls.NatVppAPI, log logging.PluginLogger) *kvs.KVDescriptor {
    49  	ctx := &NAT44GlobalInterfaceDescriptor{
    50  		natHandler: natHandler,
    51  		log:        log.NewLogger("nat44-global-iface-descriptor"),
    52  	}
    53  
    54  	typedDescr := &adapter.NAT44GlobalInterfaceDescriptor{
    55  		Name:          NAT44GlobalInterfaceDescriptorName,
    56  		KeySelector:   ctx.IsNAT44DerivedInterfaceKey,
    57  		ValueTypeName: string(proto.MessageName(&nat.Nat44Global_Interface{})),
    58  		Create:        ctx.Create,
    59  		Delete:        ctx.Delete,
    60  		Dependencies:  ctx.Dependencies,
    61  	}
    62  	return adapter.NewNAT44GlobalInterfaceDescriptor(typedDescr)
    63  }
    64  
    65  // IsNAT44DerivedInterfaceKey returns true if the key is identifying NAT-44 configuration
    66  // for interface.
    67  func (d *NAT44GlobalInterfaceDescriptor) IsNAT44DerivedInterfaceKey(key string) bool {
    68  	_, _, isNATIfaceKey := nat.ParseDerivedInterfaceNAT44Key(key)
    69  	return isNATIfaceKey
    70  }
    71  
    72  // Create enables NAT44 for an interface.
    73  func (d *NAT44GlobalInterfaceDescriptor) Create(key string, natIface *nat.Nat44Global_Interface) (metadata interface{}, err error) {
    74  	err = d.natHandler.EnableNat44Interface(natIface.Name, natIface.IsInside, natIface.OutputFeature)
    75  	if err != nil {
    76  		d.log.Error(err)
    77  		return nil, err
    78  
    79  	}
    80  	return nil, nil
    81  }
    82  
    83  // Delete disables NAT44 for an interface.
    84  func (d *NAT44GlobalInterfaceDescriptor) Delete(key string, natIface *nat.Nat44Global_Interface, metadata interface{}) error {
    85  	err := d.natHandler.DisableNat44Interface(natIface.Name, natIface.IsInside, natIface.OutputFeature)
    86  	if err != nil {
    87  		d.log.Error(err)
    88  		return err
    89  
    90  	}
    91  	return nil
    92  }
    93  
    94  // Dependencies lists the interface as the only dependency.
    95  func (d *NAT44GlobalInterfaceDescriptor) Dependencies(key string, natIface *nat.Nat44Global_Interface) []kvs.Dependency {
    96  	return []kvs.Dependency{
    97  		{
    98  			Label: natInterfaceDep,
    99  			Key:   interfaces.InterfaceKey(natIface.Name),
   100  		},
   101  	}
   102  }