go.ligato.io/vpp-agent/v3@v3.5.0/plugins/vpp/abfplugin/abfidx/abfidx.go (about)

     1  //  Copyright (c) 2019 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 abfidx
    16  
    17  import (
    18  	"go.ligato.io/cn-infra/v2/idxmap"
    19  	"go.ligato.io/cn-infra/v2/logging"
    20  
    21  	"go.ligato.io/vpp-agent/v3/pkg/idxvpp"
    22  	abf "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/abf"
    23  )
    24  
    25  // ABFMetadataIndex provides read-only access to mapping between ABF indexes (generated in the ABF plugin)
    26  // and ABF names.
    27  type ABFMetadataIndex interface {
    28  	// LookupByName looks up previously stored item identified by name in the mapping.
    29  	LookupByName(name string) (metadata *ABFMetadata, exists bool)
    30  
    31  	// LookupByIndex looks up previously stored item identified by index in the mapping.
    32  	LookupByIndex(idx uint32) (name string, metadata *ABFMetadata, exists bool)
    33  }
    34  
    35  // ABFMetadataIndexRW is mapping between ABF indexes (generated in the ABF plugin) and ABF names.
    36  type ABFMetadataIndexRW interface {
    37  	ABFMetadataIndex
    38  	idxmap.NamedMappingRW
    39  }
    40  
    41  // ABFMetadata represents metadata for ABF.
    42  type ABFMetadata struct {
    43  	Index    uint32
    44  	Attached []*abf.ABF_AttachedInterface
    45  }
    46  
    47  // Attached is helper struct for metadata (ABF attached interface).
    48  type Attached struct {
    49  	Name     string
    50  	IsIPv6   bool
    51  	Priority uint32
    52  }
    53  
    54  // GetIndex returns index of the ABF.
    55  func (m *ABFMetadata) GetIndex() uint32 {
    56  	return m.Index
    57  }
    58  
    59  // ABFMetadataDto represents an item sent through watch channel in abfIndex.
    60  type ABFMetadataDto struct {
    61  	idxmap.NamedMappingEvent
    62  	Metadata *ABFMetadata
    63  }
    64  
    65  type abfMetadataIndex struct {
    66  	idxmap.NamedMappingRW
    67  
    68  	log         logging.Logger
    69  	nameToIndex idxvpp.NameToIndex
    70  }
    71  
    72  // NewABFIndex creates new instance of abfMetadataIndex.
    73  func NewABFIndex(logger logging.Logger, title string) ABFMetadataIndexRW {
    74  	mapping := idxvpp.NewNameToIndex(logger, title, indexAbfMetadata)
    75  	return &abfMetadataIndex{
    76  		NamedMappingRW: mapping,
    77  		log:            logger,
    78  		nameToIndex:    mapping,
    79  	}
    80  }
    81  
    82  // LookupByName looks up previously stored item identified by index in mapping.
    83  func (abfIdx *abfMetadataIndex) LookupByName(name string) (metadata *ABFMetadata, exists bool) {
    84  	meta, found := abfIdx.GetValue(name)
    85  	if found {
    86  		if typedMeta, ok := meta.(*ABFMetadata); ok {
    87  			return typedMeta, found
    88  		}
    89  	}
    90  	return nil, false
    91  }
    92  
    93  // LookupByIndex looks up previously stored item identified by name in mapping.
    94  func (abfIdx *abfMetadataIndex) LookupByIndex(idx uint32) (name string, metadata *ABFMetadata, exists bool) {
    95  	var item idxvpp.WithIndex
    96  	name, item, exists = abfIdx.nameToIndex.LookupByIndex(idx)
    97  	if exists {
    98  		var isIfaceMeta bool
    99  		metadata, isIfaceMeta = item.(*ABFMetadata)
   100  		if !isIfaceMeta {
   101  			exists = false
   102  		}
   103  	}
   104  	return
   105  }
   106  
   107  // indexMetadata is an index function used for ABF metadata.
   108  func indexAbfMetadata(metaData interface{}) map[string][]string {
   109  	indexes := make(map[string][]string)
   110  
   111  	ifMeta, ok := metaData.(*ABFMetadata)
   112  	if !ok || ifMeta == nil {
   113  		return indexes
   114  	}
   115  
   116  	return indexes
   117  }