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

     1  // Copyright (c) 2020 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  	"errors"
    19  
    20  	"go.ligato.io/cn-infra/v2/logging"
    21  
    22  	kvs "go.ligato.io/vpp-agent/v3/plugins/kvscheduler/api"
    23  	"go.ligato.io/vpp-agent/v3/plugins/vpp/ipfixplugin/descriptor/adapter"
    24  	"go.ligato.io/vpp-agent/v3/plugins/vpp/ipfixplugin/vppcalls"
    25  	interfaces "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/interfaces"
    26  	ipfix "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/ipfix"
    27  )
    28  
    29  const (
    30  	// FPFeatureDescriptorName is the name of the descriptor for
    31  	// VPP Flowprobe Feature configuration.
    32  	FPFeatureDescriptorName = "vpp-flowprobe-feature"
    33  )
    34  
    35  // Validation errors:
    36  var (
    37  	// ErrIfaceNotDefined returned when interface in confiugration is empty string.
    38  	ErrIfaceNotDefined = errors.New("missing interface name for Flowprobe Feature")
    39  )
    40  
    41  // FPFeatureDescriptor configures Flowprobe Feature for VPP.
    42  type FPFeatureDescriptor struct {
    43  	ipfixHandler vppcalls.IpfixVppAPI
    44  	log          logging.Logger
    45  }
    46  
    47  // NewFPFeatureDescriptor creates a new instance of FPFeatureDescriptor.
    48  func NewFPFeatureDescriptor(ipfixHandler vppcalls.IpfixVppAPI, log logging.PluginLogger) *kvs.KVDescriptor {
    49  	ctx := &FPFeatureDescriptor{
    50  		ipfixHandler: ipfixHandler,
    51  		log:          log.NewLogger("flowprobe-feature-descriptor"),
    52  	}
    53  	typedDescr := &adapter.FlowProbeFeatureDescriptor{
    54  		Name:          FPFeatureDescriptorName,
    55  		NBKeyPrefix:   ipfix.ModelFlowprobeFeature.KeyPrefix(),
    56  		ValueTypeName: ipfix.ModelFlowprobeFeature.ProtoName(),
    57  		KeySelector:   ipfix.ModelFlowprobeFeature.IsKeyValid,
    58  		KeyLabel:      ipfix.ModelFlowprobeFeature.StripKeyPrefix,
    59  		WithMetadata:  true,
    60  		Validate:      ctx.Validate,
    61  		Create:        ctx.Create,
    62  		Delete:        ctx.Delete,
    63  		Retrieve:      ctx.Retrieve,
    64  		Dependencies:  ctx.Dependencies,
    65  	}
    66  	return adapter.NewFlowProbeFeatureDescriptor(typedDescr)
    67  }
    68  
    69  // Validate checks if Flowprobe Feature configuration is good to send to VPP.
    70  func (d *FPFeatureDescriptor) Validate(key string, value *ipfix.FlowProbeFeature) error {
    71  	if value.GetInterface() == "" {
    72  		return kvs.NewInvalidValueError(ErrIfaceNotDefined, "interface")
    73  	}
    74  	return nil
    75  }
    76  
    77  // Create uses vppcalls to pass Flowprobe Feature configuration for interface to VPP.
    78  func (d *FPFeatureDescriptor) Create(key string, val *ipfix.FlowProbeFeature) (metadata interface{}, err error) {
    79  	err = d.ipfixHandler.AddFPFeature(val)
    80  	return val, err
    81  }
    82  
    83  // Delete uses vppcalls to remove Flowprobe Feature configuration for interface.
    84  func (d *FPFeatureDescriptor) Delete(key string, val *ipfix.FlowProbeFeature, metadata interface{}) (err error) {
    85  	err = d.ipfixHandler.DelFPFeature(val)
    86  	return
    87  }
    88  
    89  // Dependencies sets Flowprobe Params as a dependency which must be created
    90  // before enabling Flowprobe Feature on an interface.
    91  func (d *FPFeatureDescriptor) Dependencies(key string, val *ipfix.FlowProbeFeature) []kvs.Dependency {
    92  	return []kvs.Dependency{
    93  		{
    94  			Label: "flowprobe-params",
    95  			Key:   ipfix.FlowprobeParamsKey(),
    96  		},
    97  		{
    98  			Label: "interface",
    99  			Key:   interfaces.InterfaceKey(val.Interface),
   100  		},
   101  	}
   102  }
   103  
   104  // Retrieve hopes that configuration in correlate is actual configuration in VPP.
   105  // As soon as VPP devs will add dump API calls, this methods should be fixed.
   106  // TODO: waiting for https://jira.fd.io/browse/VPP-1861.
   107  //
   108  // Also, this method sets metadata, so descriptor for Flowprobe Params would know
   109  // that there are some interfaces with Flowprobe Feature enabled.
   110  func (d *FPFeatureDescriptor) Retrieve(correlate []adapter.FlowProbeFeatureKVWithMetadata) (retrieved []adapter.FlowProbeFeatureKVWithMetadata, err error) {
   111  	corr := make([]adapter.FlowProbeFeatureKVWithMetadata, len(correlate))
   112  	for i, c := range correlate {
   113  		corr[i] = adapter.FlowProbeFeatureKVWithMetadata{
   114  			Key:      c.Key,
   115  			Value:    c.Value,
   116  			Metadata: c.Value,
   117  			Origin:   c.Origin,
   118  		}
   119  	}
   120  	return corr, nil
   121  }