go.ligato.io/vpp-agent/v3@v3.5.0/examples/kvscheduler/mock_plugins/l2plugin/model/keys.go (about)

     1  // Copyright (c) 2017 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 mock_l2
    16  
    17  import (
    18  	"strings"
    19  
    20  	"go.ligato.io/vpp-agent/v3/pkg/models"
    21  )
    22  
    23  // ModuleName is the module name used for models of the mock l2plugin.
    24  const ModuleName = "mock"
    25  
    26  var (
    27  	ModelBridgeDomain = models.Register(&BridgeDomain{}, models.Spec{
    28  		Module:  ModuleName,
    29  		Type:    "bridge-domain",
    30  		Version: "v1",
    31  	})
    32  
    33  	ModelFIBEntry = models.Register(&FIBEntry{}, models.Spec{
    34  		Module:  ModuleName,
    35  		Type:    "fib",
    36  		Version: "v1",
    37  	}, models.WithNameTemplate("{{.BridgeDomain}}/mac/{{.PhysAddress}}"))
    38  )
    39  
    40  // BridgeDomainKey returns the key used in NB DB to store the configuration of the
    41  // given mock bridge domain.
    42  func BridgeDomainKey(bdName string) string {
    43  	return models.Key(&BridgeDomain{
    44  		Name: bdName,
    45  	})
    46  }
    47  
    48  // FIBKey returns the key used in NB DB to store the configuration of the
    49  // given mock L2 FIB entry.
    50  func FIBKey(bdName string, fibMac string) string {
    51  	return models.Key(&FIBEntry{
    52  		BridgeDomain: bdName,
    53  		PhysAddress:  fibMac,
    54  	})
    55  }
    56  
    57  /* Note: We do not yet provide tools to build models for derived values, therefore
    58     the key template and the key building/parsing methods have to be defined
    59     from the scratch for the time being.
    60  */
    61  
    62  /* BD <-> interface binding (derived value) */
    63  const (
    64  	// bdInterfaceKeyTemplate is a template for (derived) key representing binding
    65  	// between interface and a bridge domain.
    66  	bdInterfaceKeyTemplate = "mock/bd/{bd}/interface/{iface}"
    67  )
    68  
    69  const (
    70  	// InvalidKeyPart is used in key for parts which are invalid
    71  	InvalidKeyPart = "<invalid>"
    72  )
    73  
    74  // BDInterfaceKey returns the key used to represent binding between the given interface
    75  // and the bridge domain.
    76  func BDInterfaceKey(bdName string, iface string) string {
    77  	if bdName == "" {
    78  		bdName = InvalidKeyPart
    79  	}
    80  	if iface == "" {
    81  		iface = InvalidKeyPart
    82  	}
    83  	key := strings.Replace(bdInterfaceKeyTemplate, "{bd}", bdName, 1)
    84  	key = strings.Replace(key, "{iface}", iface, 1)
    85  	return key
    86  }
    87  
    88  // ParseBDInterfaceKey parses key representing binding between interface and a bridge
    89  // domain.
    90  func ParseBDInterfaceKey(key string) (bdName string, iface string, isBDIfaceKey bool) {
    91  	keyComps := strings.Split(key, "/")
    92  	if len(keyComps) >= 5 && keyComps[0] == "mock" && keyComps[1] == "bd" && keyComps[3] == "interface" {
    93  		iface = strings.Join(keyComps[4:], "/")
    94  		return keyComps[2], iface, true
    95  	}
    96  	return "", "", false
    97  }